Home | History | Annotate | Download | only in extensions
      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 "chrome/browser/extensions/extension_prefs.h"
     14 #include "extensions/browser/app_sorting.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 MarkExtensionAsHidden(const std::string& extension_id) OVERRIDE;
     66 
     67  private:
     68   // The StringOrdinal is the app launch ordinal and the string is the extension
     69   // id.
     70   typedef std::multimap<
     71       syncer::StringOrdinal, std::string,
     72     syncer::StringOrdinal::LessThanFn> AppLaunchOrdinalMap;
     73   // The StringOrdinal is the page ordinal and the AppLaunchOrdinalMap is the
     74   // contents of that page.
     75   typedef std::map<
     76       syncer::StringOrdinal, AppLaunchOrdinalMap,
     77     syncer::StringOrdinal::LessThanFn> PageOrdinalMap;
     78 
     79   // Unit tests.
     80   friend class ChromeAppSortingDefaultOrdinalsBase;
     81   friend class ChromeAppSortingGetMinOrMaxAppLaunchOrdinalsOnPage;
     82   friend class ChromeAppSortingInitializeWithNoApps;
     83   friend class ChromeAppSortingPageOrdinalMapping;
     84 
     85   // An enum used by GetMinOrMaxAppLaunchOrdinalsOnPage to specify which
     86   // value should be returned.
     87   enum AppLaunchOrdinalReturn {MIN_ORDINAL, MAX_ORDINAL};
     88 
     89   // Maps an app id to its ordinals.
     90   struct AppOrdinals {
     91     AppOrdinals();
     92     ~AppOrdinals();
     93 
     94     syncer::StringOrdinal page_ordinal;
     95     syncer::StringOrdinal app_launch_ordinal;
     96   };
     97   typedef std::map<std::string, AppOrdinals> AppOrdinalsMap;
     98 
     99   // This function returns the lowest ordinal on |page_ordinal| if
    100   // |return_value| == AppLaunchOrdinalReturn::MIN_ORDINAL, otherwise it returns
    101   // the largest ordinal on |page_ordinal|. If there are no apps on the page
    102   // then an invalid StringOrdinal is returned. It is an error to call this
    103   // function with an invalid |page_ordinal|.
    104   syncer::StringOrdinal GetMinOrMaxAppLaunchOrdinalsOnPage(
    105       const syncer::StringOrdinal& page_ordinal,
    106       AppLaunchOrdinalReturn return_type) const;
    107 
    108   // Initialize the |page_ordinal_map_| with the page ordinals used by the
    109   // given extensions.
    110   void InitializePageOrdinalMap(
    111       const extensions::ExtensionIdList& extension_ids);
    112 
    113   // Migrates the app launcher and page index values.
    114   void MigrateAppIndex(
    115       const extensions::ExtensionIdList& extension_ids);
    116 
    117   // Called to add a new mapping value for |extension_id| with a page ordinal
    118   // of |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. This
    119   // works with valid and invalid StringOrdinals.
    120   void AddOrdinalMapping(const std::string& extension_id,
    121                          const syncer::StringOrdinal& page_ordinal,
    122                          const syncer::StringOrdinal& app_launch_ordinal);
    123 
    124   // Ensures |ntp_ordinal_map_| is of |minimum_size| number of entries.
    125   void CreateOrdinalsIfNecessary(size_t minimum_size);
    126 
    127   // Removes the mapping for |extension_id| with a page ordinal of
    128   // |page_ordinal| and a app launch ordinal of |app_launch_ordinal|. If there
    129   // is not matching map, nothing happens. This works with valid and invalid
    130   // StringOrdinals.
    131   void RemoveOrdinalMapping(const std::string& extension_id,
    132                             const syncer::StringOrdinal& page_ordinal,
    133                             const syncer::StringOrdinal& app_launch_ordinal);
    134 
    135   // Syncs the extension if needed. It is an error to call this if the
    136   // extension is not an application.
    137   void SyncIfNeeded(const std::string& extension_id);
    138 
    139   // Creates the default ordinals.
    140   void CreateDefaultOrdinals();
    141 
    142   // Gets the default ordinals for |extension_id|. Returns false if no default
    143   // ordinals for |extension_id| is defined. Otherwise, returns true and
    144   // ordinals is updated with corresponding ordinals.
    145   bool GetDefaultOrdinals(const std::string& extension_id,
    146                           syncer::StringOrdinal* page_ordinal,
    147                           syncer::StringOrdinal* app_launch_ordinal);
    148 
    149   // Returns |app_launch_ordinal| if it has no collision in the page specified
    150   // by |page_ordinal|. Otherwise, returns an ordinal after |app_launch_ordinal|
    151   // that has no conflict.
    152   syncer::StringOrdinal ResolveCollision(
    153       const syncer::StringOrdinal& page_ordinal,
    154       const syncer::StringOrdinal& app_launch_ordinal) const;
    155 
    156   // Returns the number of items in |m| visible on the new tab page.
    157   size_t CountItemsVisibleOnNtp(const AppLaunchOrdinalMap& m) const;
    158 
    159   ExtensionScopedPrefs* extension_scoped_prefs_;  // Weak, owns this instance.
    160   ExtensionSyncService* extension_sync_service_;  // Weak.
    161 
    162   // A map of all the StringOrdinal page ordinals mapping to the collections of
    163   // app launch ordinals that exist on that page. This is used for mapping
    164   // StringOrdinals to their Integer equivalent as well as quick lookup of the
    165   // any collision of on the NTP (icons with the same page and same app launch
    166   // ordinals). The possiblity of collisions means that a multimap must be used
    167   // (although the collisions must all be resolved once all the syncing is
    168   // done).
    169   PageOrdinalMap ntp_ordinal_map_;
    170 
    171   // Defines the default ordinals.
    172   AppOrdinalsMap default_ordinals_;
    173 
    174   // Used to construct the default ordinals once when needed instead of on
    175   // construction when the app order may not have been determined.
    176   bool default_ordinals_created_;
    177 
    178   // The set of extensions that don't appear in the new tab page.
    179   std::set<std::string> ntp_hidden_extensions_;
    180 
    181   DISALLOW_COPY_AND_ASSIGN(ChromeAppSorting);
    182 };
    183 
    184 }  // namespace extensions
    185 
    186 #endif  // CHROME_BROWSER_EXTENSIONS_CHROME_APP_SORTING_H_
    187