Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / chrome_app_sorting.h
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   ~ChromeAppSorting() override;
29
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;
63
64  private:
65   // The StringOrdinal is the app launch ordinal and the string is the extension
66   // id.
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.
72   typedef std::map<
73       syncer::StringOrdinal, AppLaunchOrdinalMap,
74     syncer::StringOrdinal::LessThanFn> PageOrdinalMap;
75
76   // Unit tests.
77   friend class ChromeAppSortingDefaultOrdinalsBase;
78   friend class ChromeAppSortingGetMinOrMaxAppLaunchOrdinalsOnPage;
79   friend class ChromeAppSortingInitializeWithNoApps;
80   friend class ChromeAppSortingPageOrdinalMapping;
81   friend class ChromeAppSortingSetExtensionVisible;
82
83   // An enum used by GetMinOrMaxAppLaunchOrdinalsOnPage to specify which
84   // value should be returned.
85   enum AppLaunchOrdinalReturn {MIN_ORDINAL, MAX_ORDINAL};
86
87   // Maps an app id to its ordinals.
88   struct AppOrdinals {
89     AppOrdinals();
90     ~AppOrdinals();
91
92     syncer::StringOrdinal page_ordinal;
93     syncer::StringOrdinal app_launch_ordinal;
94   };
95   typedef std::map<std::string, AppOrdinals> AppOrdinalsMap;
96
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;
105
106   // Initialize the |page_ordinal_map_| with the page ordinals used by the
107   // given extensions.
108   void InitializePageOrdinalMap(
109       const extensions::ExtensionIdList& extension_ids);
110
111   // Migrates the app launcher and page index values.
112   void MigrateAppIndex(
113       const extensions::ExtensionIdList& extension_ids);
114
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);
121
122   // Ensures |ntp_ordinal_map_| is of |minimum_size| number of entries.
123   void CreateOrdinalsIfNecessary(size_t minimum_size);
124
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
128   // StringOrdinals.
129   void RemoveOrdinalMapping(const std::string& extension_id,
130                             const syncer::StringOrdinal& page_ordinal,
131                             const syncer::StringOrdinal& app_launch_ordinal);
132
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);
136
137   // Creates the default ordinals.
138   void CreateDefaultOrdinals();
139
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);
146
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;
153
154   // Returns the number of items in |m| visible on the new tab page.
155   size_t CountItemsVisibleOnNtp(const AppLaunchOrdinalMap& m) const;
156
157   ExtensionScopedPrefs* extension_scoped_prefs_;  // Weak, owns this instance.
158   ExtensionSyncService* extension_sync_service_;  // Weak.
159
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
166   // done).
167   PageOrdinalMap ntp_ordinal_map_;
168
169   // Defines the default ordinals.
170   AppOrdinalsMap default_ordinals_;
171
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_;
175
176   // The set of extensions that don't appear in the new tab page.
177   std::set<std::string> ntp_hidden_extensions_;
178
179   DISALLOW_COPY_AND_ASSIGN(ChromeAppSorting);
180 };
181
182 }  // namespace extensions
183
184 #endif  // CHROME_BROWSER_EXTENSIONS_CHROME_APP_SORTING_H_