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"
18 class ExtensionSyncService
;
21 namespace extensions
{
23 class ExtensionScopedPrefs
;
25 class ChromeAppSorting
: public AppSorting
{
28 ~ChromeAppSorting() override
;
30 // AppSorting implementation:
31 void SetExtensionScopedPrefs(ExtensionScopedPrefs
* prefs
) override
;
32 void SetExtensionSyncService(
33 ExtensionSyncService
* extension_sync_service
) override
;
34 void Initialize(const extensions::ExtensionIdList
& extension_ids
) override
;
35 void FixNTPOrdinalCollisions() override
;
36 void EnsureValidOrdinals(
37 const std::string
& extension_id
,
38 const syncer::StringOrdinal
& suggested_page
) override
;
39 void OnExtensionMoved(const std::string
& moved_extension_id
,
40 const std::string
& predecessor_extension_id
,
41 const std::string
& successor_extension_id
) override
;
42 syncer::StringOrdinal
GetAppLaunchOrdinal(
43 const std::string
& extension_id
) const override
;
44 void SetAppLaunchOrdinal(
45 const std::string
& extension_id
,
46 const syncer::StringOrdinal
& new_app_launch_ordinal
) override
;
47 syncer::StringOrdinal
CreateFirstAppLaunchOrdinal(
48 const syncer::StringOrdinal
& page_ordinal
) const override
;
49 syncer::StringOrdinal
CreateNextAppLaunchOrdinal(
50 const syncer::StringOrdinal
& page_ordinal
) const override
;
51 syncer::StringOrdinal
CreateFirstAppPageOrdinal() const override
;
52 syncer::StringOrdinal
GetNaturalAppPageOrdinal() const override
;
53 syncer::StringOrdinal
GetPageOrdinal(
54 const std::string
& extension_id
) const override
;
55 void SetPageOrdinal(const std::string
& extension_id
,
56 const syncer::StringOrdinal
& new_page_ordinal
) override
;
57 void ClearOrdinals(const std::string
& extension_id
) override
;
58 int PageStringOrdinalAsInteger(
59 const syncer::StringOrdinal
& page_ordinal
) const override
;
60 syncer::StringOrdinal
PageIntegerAsStringOrdinal(size_t page_index
) override
;
61 void SetExtensionVisible(const std::string
& extension_id
,
62 bool visible
) override
;
65 // The StringOrdinal is the app launch ordinal and the string is the extension
67 typedef std::multimap
<
68 syncer::StringOrdinal
, std::string
,
69 syncer::StringOrdinal::LessThanFn
> AppLaunchOrdinalMap
;
70 // The StringOrdinal is the page ordinal and the AppLaunchOrdinalMap is the
71 // contents of that page.
73 syncer::StringOrdinal
, AppLaunchOrdinalMap
,
74 syncer::StringOrdinal::LessThanFn
> PageOrdinalMap
;
77 friend class ChromeAppSortingDefaultOrdinalsBase
;
78 friend class ChromeAppSortingGetMinOrMaxAppLaunchOrdinalsOnPage
;
79 friend class ChromeAppSortingInitializeWithNoApps
;
80 friend class ChromeAppSortingPageOrdinalMapping
;
81 friend class ChromeAppSortingSetExtensionVisible
;
83 // An enum used by GetMinOrMaxAppLaunchOrdinalsOnPage to specify which
84 // value should be returned.
85 enum AppLaunchOrdinalReturn
{MIN_ORDINAL
, MAX_ORDINAL
};
87 // Maps an app id to its ordinals.
92 syncer::StringOrdinal page_ordinal
;
93 syncer::StringOrdinal app_launch_ordinal
;
95 typedef std::map
<std::string
, AppOrdinals
> AppOrdinalsMap
;
97 // This function returns the lowest ordinal on |page_ordinal| if
98 // |return_value| == AppLaunchOrdinalReturn::MIN_ORDINAL, otherwise it returns
99 // the largest ordinal on |page_ordinal|. If there are no apps on the page
100 // then an invalid StringOrdinal is returned. It is an error to call this
101 // function with an invalid |page_ordinal|.
102 syncer::StringOrdinal
GetMinOrMaxAppLaunchOrdinalsOnPage(
103 const syncer::StringOrdinal
& page_ordinal
,
104 AppLaunchOrdinalReturn return_type
) const;
106 // Initialize the |page_ordinal_map_| with the page ordinals used by the
108 void InitializePageOrdinalMap(
109 const extensions::ExtensionIdList
& extension_ids
);
111 // Migrates the app launcher and page index values.
112 void MigrateAppIndex(
113 const extensions::ExtensionIdList
& extension_ids
);
115 // Called to add a new mapping value for |extension_id| with a page ordinal
116 // of |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. This
117 // works with valid and invalid StringOrdinals.
118 void AddOrdinalMapping(const std::string
& extension_id
,
119 const syncer::StringOrdinal
& page_ordinal
,
120 const syncer::StringOrdinal
& app_launch_ordinal
);
122 // Ensures |ntp_ordinal_map_| is of |minimum_size| number of entries.
123 void CreateOrdinalsIfNecessary(size_t minimum_size
);
125 // Removes the mapping for |extension_id| with a page ordinal of
126 // |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. If there
127 // is not matching map, nothing happens. This works with valid and invalid
129 void RemoveOrdinalMapping(const std::string
& extension_id
,
130 const syncer::StringOrdinal
& page_ordinal
,
131 const syncer::StringOrdinal
& app_launch_ordinal
);
133 // Syncs the extension if needed. It is an error to call this if the
134 // extension is not an application.
135 void SyncIfNeeded(const std::string
& extension_id
);
137 // Creates the default ordinals.
138 void CreateDefaultOrdinals();
140 // Gets the default ordinals for |extension_id|. Returns false if no default
141 // ordinals for |extension_id| is defined. Otherwise, returns true and
142 // ordinals is updated with corresponding ordinals.
143 bool GetDefaultOrdinals(const std::string
& extension_id
,
144 syncer::StringOrdinal
* page_ordinal
,
145 syncer::StringOrdinal
* app_launch_ordinal
);
147 // Returns |app_launch_ordinal| if it has no collision in the page specified
148 // by |page_ordinal|. Otherwise, returns an ordinal after |app_launch_ordinal|
149 // that has no conflict.
150 syncer::StringOrdinal
ResolveCollision(
151 const syncer::StringOrdinal
& page_ordinal
,
152 const syncer::StringOrdinal
& app_launch_ordinal
) const;
154 // Returns the number of items in |m| visible on the new tab page.
155 size_t CountItemsVisibleOnNtp(const AppLaunchOrdinalMap
& m
) const;
157 ExtensionScopedPrefs
* extension_scoped_prefs_
; // Weak, owns this instance.
158 ExtensionSyncService
* extension_sync_service_
; // Weak.
160 // A map of all the StringOrdinal page ordinals mapping to the collections of
161 // app launch ordinals that exist on that page. This is used for mapping
162 // StringOrdinals to their Integer equivalent as well as quick lookup of the
163 // any collision of on the NTP (icons with the same page and same app launch
164 // ordinals). The possiblity of collisions means that a multimap must be used
165 // (although the collisions must all be resolved once all the syncing is
167 PageOrdinalMap ntp_ordinal_map_
;
169 // Defines the default ordinals.
170 AppOrdinalsMap default_ordinals_
;
172 // Used to construct the default ordinals once when needed instead of on
173 // construction when the app order may not have been determined.
174 bool default_ordinals_created_
;
176 // The set of extensions that don't appear in the new tab page.
177 std::set
<std::string
> ntp_hidden_extensions_
;
179 DISALLOW_COPY_AND_ASSIGN(ChromeAppSorting
);
182 } // namespace extensions
184 #endif // CHROME_BROWSER_EXTENSIONS_CHROME_APP_SORTING_H_