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