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