OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SORTING_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SORTING_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SORTING_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SORTING_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "chrome/browser/extensions/extension_prefs.h" |
12 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
13 #include "chrome/browser/extensions/extension_prefs.h" | 14 #include "sync/api/string_ordinal.h" |
14 #include "chrome/common/string_ordinal.h" | |
15 | 15 |
16 class ExtensionScopedPrefs; | 16 class ExtensionScopedPrefs; |
17 class ExtensionServiceInterface; | 17 class ExtensionServiceInterface; |
18 class PrefService; | 18 class PrefService; |
19 | 19 |
20 class ExtensionSorting { | 20 class ExtensionSorting { |
21 public: | 21 public: |
22 ExtensionSorting(ExtensionScopedPrefs* extension_scoped_prefs, | 22 ExtensionSorting(ExtensionScopedPrefs* extension_scoped_prefs, |
23 PrefService* pref_service); | 23 PrefService* pref_service); |
24 ~ExtensionSorting(); | 24 ~ExtensionSorting(); |
(...skipping 21 matching lines...) Expand all Loading... |
46 void OnExtensionMoved(const std::string& moved_extension_id, | 46 void OnExtensionMoved(const std::string& moved_extension_id, |
47 const std::string& predecessor_extension_id, | 47 const std::string& predecessor_extension_id, |
48 const std::string& successor_extension_id); | 48 const std::string& successor_extension_id); |
49 | 49 |
50 // Get the application launch ordinal for an app with |extension_id|. This | 50 // Get the application launch ordinal for an app with |extension_id|. This |
51 // determines the order in which the app appears on the page it's on in the | 51 // determines the order in which the app appears on the page it's on in the |
52 // New Tab Page (Note that you can compare app launch ordinals only if the | 52 // New Tab Page (Note that you can compare app launch ordinals only if the |
53 // apps are on the same page). A string value close to |a*| generally | 53 // apps are on the same page). A string value close to |a*| generally |
54 // indicates top left. If the extension has no launch ordinal, an invalid | 54 // indicates top left. If the extension has no launch ordinal, an invalid |
55 // StringOrdinal is returned. | 55 // StringOrdinal is returned. |
56 StringOrdinal GetAppLaunchOrdinal(const std::string& extension_id) const; | 56 syncer::StringOrdinal GetAppLaunchOrdinal( |
| 57 const std::string& extension_id) const; |
57 | 58 |
58 // Sets a specific launch ordinal for an app with |extension_id|. | 59 // Sets a specific launch ordinal for an app with |extension_id|. |
59 void SetAppLaunchOrdinal(const std::string& extension_id, | 60 void SetAppLaunchOrdinal(const std::string& extension_id, |
60 const StringOrdinal& new_app_launch_ordinal); | 61 const syncer::StringOrdinal& new_app_launch_ordinal); |
61 | 62 |
62 // Returns a StringOrdinal that is lower than any app launch ordinal for the | 63 // Returns a StringOrdinal that is lower than any app launch ordinal for the |
63 // given page. | 64 // given page. |
64 StringOrdinal CreateFirstAppLaunchOrdinal(const StringOrdinal& page_ordinal) | 65 syncer::StringOrdinal CreateFirstAppLaunchOrdinal( |
65 const; | 66 const syncer::StringOrdinal& page_ordinal) const; |
66 | 67 |
67 // Returns a StringOrdinal that is higher than any app launch ordinal for the | 68 // Returns a StringOrdinal that is higher than any app launch ordinal for the |
68 // given page. | 69 // given page. |
69 StringOrdinal CreateNextAppLaunchOrdinal(const StringOrdinal& page_ordinal) | 70 syncer::StringOrdinal CreateNextAppLaunchOrdinal( |
70 const; | 71 const syncer::StringOrdinal& page_ordinal) const; |
71 | 72 |
72 // Returns a StringOrdinal that is lower than any existing page ordinal. | 73 // Returns a StringOrdinal that is lower than any existing page ordinal. |
73 StringOrdinal CreateFirstAppPageOrdinal() const; | 74 syncer::StringOrdinal CreateFirstAppPageOrdinal() const; |
74 | 75 |
75 // Gets the page a new app should install to, which is the earliest non-full | 76 // Gets the page a new app should install to, which is the earliest non-full |
76 // page. The returned ordinal may correspond to a page that doesn't yet exist | 77 // page. The returned ordinal may correspond to a page that doesn't yet exist |
77 // if all pages are full. | 78 // if all pages are full. |
78 StringOrdinal GetNaturalAppPageOrdinal() const; | 79 syncer::StringOrdinal GetNaturalAppPageOrdinal() const; |
79 | 80 |
80 // Get the page ordinal for an app with |extension_id|. This determines | 81 // Get the page ordinal for an app with |extension_id|. This determines |
81 // which page an app will appear on in page-based NTPs. If the app has no | 82 // which page an app will appear on in page-based NTPs. If the app has no |
82 // page specified, an invalid StringOrdinal is returned. | 83 // page specified, an invalid StringOrdinal is returned. |
83 StringOrdinal GetPageOrdinal(const std::string& extension_id) const; | 84 syncer::StringOrdinal GetPageOrdinal(const std::string& extension_id) const; |
84 | 85 |
85 // Sets a specific page ordinal for an app with |extension_id|. | 86 // Sets a specific page ordinal for an app with |extension_id|. |
86 void SetPageOrdinal(const std::string& extension_id, | 87 void SetPageOrdinal(const std::string& extension_id, |
87 const StringOrdinal& new_page_ordinal); | 88 const syncer::StringOrdinal& new_page_ordinal); |
88 | 89 |
89 // Removes the ordinal values for an app. | 90 // Removes the ordinal values for an app. |
90 void ClearOrdinals(const std::string& extension_id); | 91 void ClearOrdinals(const std::string& extension_id); |
91 | 92 |
92 // Convert the page StringOrdinal value to its integer equivalent. This takes | 93 // Convert the page StringOrdinal value to its integer equivalent. This takes |
93 // O(# of apps) worst-case. | 94 // O(# of apps) worst-case. |
94 int PageStringOrdinalAsInteger(const StringOrdinal& page_ordinal) const; | 95 int PageStringOrdinalAsInteger( |
| 96 const syncer::StringOrdinal& page_ordinal) const; |
95 | 97 |
96 // Converts the page index integer to its StringOrdinal equivalent. This takes | 98 // Converts the page index integer to its StringOrdinal equivalent. This takes |
97 // O(# of apps) worst-case. | 99 // O(# of apps) worst-case. |
98 StringOrdinal PageIntegerAsStringOrdinal(size_t page_index); | 100 syncer::StringOrdinal PageIntegerAsStringOrdinal(size_t page_index); |
99 | 101 |
100 private: | 102 private: |
101 // Unit tests. | 103 // Unit tests. |
102 friend class ExtensionSortingGetMinOrMaxAppLaunchOrdinalsOnPage; | 104 friend class ExtensionSortingGetMinOrMaxAppLaunchOrdinalsOnPage; |
103 friend class ExtensionSortingInitializeWithNoApps; | 105 friend class ExtensionSortingInitializeWithNoApps; |
104 friend class ExtensionSortingPageOrdinalMapping; | 106 friend class ExtensionSortingPageOrdinalMapping; |
105 | 107 |
106 // An enum used by GetMinOrMaxAppLaunchOrdinalsOnPage to specify which | 108 // An enum used by GetMinOrMaxAppLaunchOrdinalsOnPage to specify which |
107 // value should be returned. | 109 // value should be returned. |
108 enum AppLaunchOrdinalReturn {MIN_ORDINAL, MAX_ORDINAL}; | 110 enum AppLaunchOrdinalReturn {MIN_ORDINAL, MAX_ORDINAL}; |
109 | 111 |
110 // This function returns the lowest ordinal on |page_ordinal| if | 112 // This function returns the lowest ordinal on |page_ordinal| if |
111 // |return_value| == AppLaunchOrdinalReturn::MIN_ORDINAL, otherwise it returns | 113 // |return_value| == AppLaunchOrdinalReturn::MIN_ORDINAL, otherwise it returns |
112 // the largest ordinal on |page_ordinal|. If there are no apps on the page | 114 // the largest ordinal on |page_ordinal|. If there are no apps on the page |
113 // then an invalid StringOrdinal is returned. It is an error to call this | 115 // then an invalid StringOrdinal is returned. It is an error to call this |
114 // function with an invalid |page_ordinal|. | 116 // function with an invalid |page_ordinal|. |
115 StringOrdinal GetMinOrMaxAppLaunchOrdinalsOnPage( | 117 syncer::StringOrdinal GetMinOrMaxAppLaunchOrdinalsOnPage( |
116 const StringOrdinal& page_ordinal, | 118 const syncer::StringOrdinal& page_ordinal, |
117 AppLaunchOrdinalReturn return_type) const; | 119 AppLaunchOrdinalReturn return_type) const; |
118 | 120 |
119 // Initialize the |page_ordinal_map_| with the page ordinals used by the | 121 // Initialize the |page_ordinal_map_| with the page ordinals used by the |
120 // given extensions. | 122 // given extensions. |
121 void InitializePageOrdinalMap( | 123 void InitializePageOrdinalMap( |
122 const extensions::ExtensionPrefs::ExtensionIds& extension_ids); | 124 const extensions::ExtensionPrefs::ExtensionIds& extension_ids); |
123 | 125 |
124 // Migrates the app launcher and page index values. | 126 // Migrates the app launcher and page index values. |
125 void MigrateAppIndex( | 127 void MigrateAppIndex( |
126 const extensions::ExtensionPrefs::ExtensionIds& extension_ids); | 128 const extensions::ExtensionPrefs::ExtensionIds& extension_ids); |
127 | 129 |
128 // Called to add a new mapping value for |extension_id| with a page ordinal | 130 // Called to add a new mapping value for |extension_id| with a page ordinal |
129 // of |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. This | 131 // of |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. This |
130 // works with valid and invalid StringOrdinals. | 132 // works with valid and invalid StringOrdinals. |
131 void AddOrdinalMapping(const std::string& extension_id, | 133 void AddOrdinalMapping(const std::string& extension_id, |
132 const StringOrdinal& page_ordinal, | 134 const syncer::StringOrdinal& page_ordinal, |
133 const StringOrdinal& app_launch_ordinal); | 135 const syncer::StringOrdinal& app_launch_ordinal); |
134 | 136 |
135 // Ensures |ntp_ordinal_map_| is of |minimum_size| number of entries. | 137 // Ensures |ntp_ordinal_map_| is of |minimum_size| number of entries. |
136 void CreateOrdinalsIfNecessary(size_t minimum_size); | 138 void CreateOrdinalsIfNecessary(size_t minimum_size); |
137 | 139 |
138 // Removes the mapping for |extension_id| with a page ordinal of | 140 // Removes the mapping for |extension_id| with a page ordinal of |
139 // |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. If there | 141 // |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. If there |
140 // is not matching map, nothing happens. This works with valid and invalid | 142 // is not matching map, nothing happens. This works with valid and invalid |
141 // StringOrdinals. | 143 // StringOrdinals. |
142 void RemoveOrdinalMapping(const std::string& extension_id, | 144 void RemoveOrdinalMapping(const std::string& extension_id, |
143 const StringOrdinal& page_ordinal, | 145 const syncer::StringOrdinal& page_ordinal, |
144 const StringOrdinal& app_launch_ordinal); | 146 const syncer::StringOrdinal& app_launch_ordinal); |
145 | 147 |
146 // Syncs the extension if needed. It is an error to call this if the | 148 // Syncs the extension if needed. It is an error to call this if the |
147 // extension is not an application. | 149 // extension is not an application. |
148 void SyncIfNeeded(const std::string& extension_id); | 150 void SyncIfNeeded(const std::string& extension_id); |
149 | 151 |
150 ExtensionScopedPrefs* extension_scoped_prefs_; // Weak, owns this instance. | 152 ExtensionScopedPrefs* extension_scoped_prefs_; // Weak, owns this instance. |
151 PrefService* pref_service_; // Weak. | 153 PrefService* pref_service_; // Weak. |
152 ExtensionServiceInterface* extension_service_; // Weak. | 154 ExtensionServiceInterface* extension_service_; // Weak. |
153 | 155 |
154 // A map of all the StringOrdinal page ordinals mapping to the collections of | 156 // A map of all the StringOrdinal page ordinals mapping to the collections of |
155 // app launch ordinals that exist on that page. This is used for mapping | 157 // app launch ordinals that exist on that page. This is used for mapping |
156 // StringOrdinals to their Integer equivalent as well as quick lookup of the | 158 // StringOrdinals to their Integer equivalent as well as quick lookup of the |
157 // any collision of on the NTP (icons with the same page and same app launch | 159 // any collision of on the NTP (icons with the same page and same app launch |
158 // ordinals). The possiblity of collisions means that a multimap must be used | 160 // ordinals). The possiblity of collisions means that a multimap must be used |
159 // (although the collisions must all be resolved once all the syncing is | 161 // (although the collisions must all be resolved once all the syncing is |
160 // done). | 162 // done). |
161 // The StringOrdinal is the app launch ordinal and the string is the extension | 163 // The StringOrdinal is the app launch ordinal and the string is the extension |
162 // id. | 164 // id. |
163 typedef std::multimap< | 165 typedef std::multimap< |
164 StringOrdinal, std::string, StringOrdinalLessThan> AppLaunchOrdinalMap; | 166 syncer::StringOrdinal, std::string, |
| 167 syncer::StringOrdinal::LessThanFn> AppLaunchOrdinalMap; |
165 // The StringOrdinal is the page ordinal and the AppLaunchOrdinalMap is the | 168 // The StringOrdinal is the page ordinal and the AppLaunchOrdinalMap is the |
166 // contents of that page. | 169 // contents of that page. |
167 typedef std::map< | 170 typedef std::map< |
168 StringOrdinal, AppLaunchOrdinalMap, StringOrdinalLessThan> PageOrdinalMap; | 171 syncer::StringOrdinal, AppLaunchOrdinalMap, |
| 172 syncer::StringOrdinal::LessThanFn> PageOrdinalMap; |
169 PageOrdinalMap ntp_ordinal_map_; | 173 PageOrdinalMap ntp_ordinal_map_; |
170 | 174 |
171 DISALLOW_COPY_AND_ASSIGN(ExtensionSorting); | 175 DISALLOW_COPY_AND_ASSIGN(ExtensionSorting); |
172 }; | 176 }; |
173 | 177 |
174 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SORTING_H_ | 178 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SORTING_H_ |
OLD | NEW |