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