1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_CHROME_APP_SORTING_H_
6 #define CHROME_BROWSER_EXTENSIONS_CHROME_APP_SORTING_H_
12 #include "base/basictypes.h"
13 #include "extensions/browser/app_sorting.h"
14 #include "extensions/browser/extension_prefs.h"
15 #include "extensions/common/extension.h"
16 #include "sync/api/string_ordinal.h"
20 namespace extensions
{
22 class ExtensionScopedPrefs
;
24 class ChromeAppSorting
: public AppSorting
{
26 explicit ChromeAppSorting(content::BrowserContext
* browser_context
);
27 ~ChromeAppSorting() override
;
29 // AppSorting implementation:
30 void FixNTPOrdinalCollisions() override
;
31 void EnsureValidOrdinals(
32 const std::string
& extension_id
,
33 const syncer::StringOrdinal
& suggested_page
) override
;
34 void OnExtensionMoved(const std::string
& moved_extension_id
,
35 const std::string
& predecessor_extension_id
,
36 const std::string
& successor_extension_id
) override
;
37 syncer::StringOrdinal
GetAppLaunchOrdinal(
38 const std::string
& extension_id
) const override
;
39 void SetAppLaunchOrdinal(
40 const std::string
& extension_id
,
41 const syncer::StringOrdinal
& new_app_launch_ordinal
) override
;
42 syncer::StringOrdinal
CreateFirstAppLaunchOrdinal(
43 const syncer::StringOrdinal
& page_ordinal
) const override
;
44 syncer::StringOrdinal
CreateNextAppLaunchOrdinal(
45 const syncer::StringOrdinal
& page_ordinal
) const override
;
46 syncer::StringOrdinal
CreateFirstAppPageOrdinal() const override
;
47 syncer::StringOrdinal
GetNaturalAppPageOrdinal() const override
;
48 syncer::StringOrdinal
GetPageOrdinal(
49 const std::string
& extension_id
) const override
;
50 void SetPageOrdinal(const std::string
& extension_id
,
51 const syncer::StringOrdinal
& new_page_ordinal
) override
;
52 void ClearOrdinals(const std::string
& extension_id
) override
;
53 int PageStringOrdinalAsInteger(
54 const syncer::StringOrdinal
& page_ordinal
) const override
;
55 syncer::StringOrdinal
PageIntegerAsStringOrdinal(size_t page_index
) override
;
56 void SetExtensionVisible(const std::string
& extension_id
,
57 bool visible
) override
;
60 // The StringOrdinal is the app launch ordinal and the string is the extension
62 typedef std::multimap
<
63 syncer::StringOrdinal
, std::string
,
64 syncer::StringOrdinal::LessThanFn
> AppLaunchOrdinalMap
;
65 // The StringOrdinal is the page ordinal and the AppLaunchOrdinalMap is the
66 // contents of that page.
68 syncer::StringOrdinal
, AppLaunchOrdinalMap
,
69 syncer::StringOrdinal::LessThanFn
> PageOrdinalMap
;
72 friend class ChromeAppSortingDefaultOrdinalsBase
;
73 friend class ChromeAppSortingGetMinOrMaxAppLaunchOrdinalsOnPage
;
74 friend class ChromeAppSortingInitialize
;
75 friend class ChromeAppSortingInitializeWithNoApps
;
76 friend class ChromeAppSortingPageOrdinalMapping
;
77 friend class ChromeAppSortingSetExtensionVisible
;
79 // An enum used by GetMinOrMaxAppLaunchOrdinalsOnPage to specify which
80 // value should be returned.
81 enum AppLaunchOrdinalReturn
{MIN_ORDINAL
, MAX_ORDINAL
};
83 // Maps an app id to its ordinals.
88 syncer::StringOrdinal page_ordinal
;
89 syncer::StringOrdinal app_launch_ordinal
;
91 typedef std::map
<std::string
, AppOrdinals
> AppOrdinalsMap
;
93 // This function returns the lowest ordinal on |page_ordinal| if
94 // |return_value| == AppLaunchOrdinalReturn::MIN_ORDINAL, otherwise it returns
95 // the largest ordinal on |page_ordinal|. If there are no apps on the page
96 // then an invalid StringOrdinal is returned. It is an error to call this
97 // function with an invalid |page_ordinal|.
98 syncer::StringOrdinal
GetMinOrMaxAppLaunchOrdinalsOnPage(
99 const syncer::StringOrdinal
& page_ordinal
,
100 AppLaunchOrdinalReturn return_type
) const;
102 // Initialize the |page_ordinal_map_| with the page ordinals used by the
104 void InitializePageOrdinalMap(
105 const extensions::ExtensionIdList
& extension_ids
);
107 // Migrates the app launcher and page index values.
108 void MigrateAppIndex(
109 const extensions::ExtensionIdList
& extension_ids
);
111 // Called to add a new mapping value for |extension_id| with a page ordinal
112 // of |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. This
113 // works with valid and invalid StringOrdinals.
114 void AddOrdinalMapping(const std::string
& extension_id
,
115 const syncer::StringOrdinal
& page_ordinal
,
116 const syncer::StringOrdinal
& app_launch_ordinal
);
118 // Ensures |ntp_ordinal_map_| is of |minimum_size| number of entries.
119 void CreateOrdinalsIfNecessary(size_t minimum_size
);
121 // Removes the mapping for |extension_id| with a page ordinal of
122 // |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. If there
123 // is not matching map, nothing happens. This works with valid and invalid
125 void RemoveOrdinalMapping(const std::string
& extension_id
,
126 const syncer::StringOrdinal
& page_ordinal
,
127 const syncer::StringOrdinal
& app_launch_ordinal
);
129 // Syncs the extension if needed. It is an error to call this if the
130 // extension is not an application.
131 void SyncIfNeeded(const std::string
& extension_id
);
133 // Creates the default ordinals.
134 void CreateDefaultOrdinals();
136 // Gets the default ordinals for |extension_id|. Returns false if no default
137 // ordinals for |extension_id| is defined. Otherwise, returns true and
138 // ordinals is updated with corresponding ordinals.
139 bool GetDefaultOrdinals(const std::string
& extension_id
,
140 syncer::StringOrdinal
* page_ordinal
,
141 syncer::StringOrdinal
* app_launch_ordinal
);
143 // Returns |app_launch_ordinal| if it has no collision in the page specified
144 // by |page_ordinal|. Otherwise, returns an ordinal after |app_launch_ordinal|
145 // that has no conflict.
146 syncer::StringOrdinal
ResolveCollision(
147 const syncer::StringOrdinal
& page_ordinal
,
148 const syncer::StringOrdinal
& app_launch_ordinal
) const;
150 // Returns the number of items in |m| visible on the new tab page.
151 size_t CountItemsVisibleOnNtp(const AppLaunchOrdinalMap
& m
) const;
153 content::BrowserContext
* browser_context_
;
155 // A map of all the StringOrdinal page ordinals mapping to the collections of
156 // app launch ordinals that exist on that page. This is used for mapping
157 // StringOrdinals to their Integer equivalent as well as quick lookup of the
158 // any collision of on the NTP (icons with the same page and same app launch
159 // ordinals). The possiblity of collisions means that a multimap must be used
160 // (although the collisions must all be resolved once all the syncing is
162 PageOrdinalMap ntp_ordinal_map_
;
164 // Defines the default ordinals.
165 AppOrdinalsMap default_ordinals_
;
167 // Used to construct the default ordinals once when needed instead of on
168 // construction when the app order may not have been determined.
169 bool default_ordinals_created_
;
171 // The set of extensions that don't appear in the new tab page.
172 std::set
<std::string
> ntp_hidden_extensions_
;
174 DISALLOW_COPY_AND_ASSIGN(ChromeAppSorting
);
177 } // namespace extensions
179 #endif // CHROME_BROWSER_EXTENSIONS_CHROME_APP_SORTING_H_