Home | History | Annotate | Download | only in app_list
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_APP_LIST_EXTENSION_APP_MODEL_BUILDER_H_
      6 #define CHROME_BROWSER_UI_APP_LIST_EXTENSION_APP_MODEL_BUILDER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/gtest_prod_util.h"
     12 #include "base/prefs/pref_change_registrar.h"
     13 #include "base/scoped_observer.h"
     14 #include "chrome/browser/extensions/install_observer.h"
     15 #include "extensions/browser/extension_registry_observer.h"
     16 #include "ui/app_list/app_list_model.h"
     17 #include "ui/base/models/list_model_observer.h"
     18 
     19 class AppListControllerDelegate;
     20 class ExtensionAppItem;
     21 class Profile;
     22 
     23 namespace app_list {
     24 class AppListSyncableService;
     25 }
     26 
     27 namespace extensions {
     28 class Extension;
     29 class ExtensionRegistry;
     30 class ExtensionSet;
     31 class InstallTracker;
     32 }
     33 
     34 namespace gfx {
     35 class ImageSkia;
     36 }
     37 
     38 // This class populates and maintains the given |model| with information from
     39 // |profile|.
     40 class ExtensionAppModelBuilder : public extensions::InstallObserver,
     41                                  public extensions::ExtensionRegistryObserver,
     42                                  public app_list::AppListItemListObserver {
     43  public:
     44   explicit ExtensionAppModelBuilder(AppListControllerDelegate* controller);
     45   virtual ~ExtensionAppModelBuilder();
     46 
     47   // Initialize to use app-list sync and sets |service_| to |service|.
     48   void InitializeWithService(app_list::AppListSyncableService* service);
     49 
     50   // Initialize to use extension sync and sets |service_| to NULL. Used in
     51   // tests and when AppList sync is not enabled.
     52   void InitializeWithProfile(Profile* profile, app_list::AppListModel* model);
     53 
     54  private:
     55   typedef std::vector<ExtensionAppItem*> ExtensionAppList;
     56 
     57   // Builds the model with the current profile.
     58   void BuildModel();
     59 
     60   // extensions::InstallObserver.
     61   virtual void OnBeginExtensionInstall(
     62       const ExtensionInstallParams& params) OVERRIDE;
     63   virtual void OnDownloadProgress(const std::string& extension_id,
     64                                   int percent_downloaded) OVERRIDE;
     65   virtual void OnInstallFailure(const std::string& extension_id) OVERRIDE;
     66   virtual void OnDisabledExtensionUpdated(
     67       const extensions::Extension* extension) OVERRIDE;
     68   virtual void OnAppInstalledToAppList(
     69       const std::string& extension_id) OVERRIDE;
     70   virtual void OnShutdown() OVERRIDE;
     71 
     72   // extensions::ExtensionRegistryObserver.
     73   virtual void OnExtensionLoaded(
     74       content::BrowserContext* browser_context,
     75       const extensions::Extension* extension) OVERRIDE;
     76   virtual void OnExtensionUnloaded(
     77       content::BrowserContext* browser_context,
     78       const extensions::Extension* extension,
     79       extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
     80   virtual void OnExtensionUninstalled(
     81       content::BrowserContext* browser_context,
     82       const extensions::Extension* extension) OVERRIDE;
     83   virtual void OnShutdown(extensions::ExtensionRegistry* registry) OVERRIDE;
     84 
     85   // AppListItemListObserver.
     86   virtual void OnListItemMoved(size_t from_index,
     87                                size_t to_index,
     88                                app_list::AppListItem* item) OVERRIDE;
     89 
     90   scoped_ptr<ExtensionAppItem> CreateAppItem(
     91       const std::string& extension_id,
     92       const std::string& extension_name,
     93       const gfx::ImageSkia& installing_icon,
     94       bool is_platform_app);
     95 
     96   // Populates the model with apps.
     97   void PopulateApps();
     98 
     99   // Re-sort apps in case app ordinal prefs are changed.
    100   void ResortApps();
    101 
    102   // Inserts an app based on app ordinal prefs.
    103   void InsertApp(scoped_ptr<ExtensionAppItem> app);
    104 
    105   // Sets which app is intended to be highlighted. Will remove the highlight
    106   // from a currently highlighted app.
    107   void SetHighlightedApp(const std::string& extension_id);
    108 
    109   // Sets the application app with |highlight_app_id_| in |model_| as
    110   // highlighted if |highlighted_app_pending_| is true. If such an app is found,
    111   // reset |highlighted_app_pending_| so that won't be highlighted again until
    112   // another call to SetHighlightedApp() is made.
    113   void UpdateHighlight();
    114 
    115   // Returns app instance matching |extension_id| or NULL.
    116   ExtensionAppItem* GetExtensionAppItem(const std::string& extension_id);
    117 
    118   // Initializes the |extension_pref_change_registrar| to listen for extension
    119   // prefs changes. OnExtensionPreferenceChanged() is called when extension
    120   // prefs change.
    121   void InitializePrefChangeRegistrar();
    122 
    123   // Handles extension prefs changes.
    124   void OnExtensionPreferenceChanged();
    125 
    126   // Unowned pointers to the service that owns this and associated profile.
    127   app_list::AppListSyncableService* service_;
    128   Profile* profile_;
    129 
    130   // Registrar used to monitor the extension prefs.
    131   PrefChangeRegistrar extension_pref_change_registrar_;
    132 
    133   // Unowned pointer to the app list controller.
    134   AppListControllerDelegate* controller_;
    135 
    136   // Unowned pointer to the app list model.
    137   app_list::AppListModel* model_;
    138 
    139   std::string highlight_app_id_;
    140 
    141   // True if we haven't set |highlight_app_id_| to be highlighted. This happens
    142   // if we try to highlight an app that doesn't exist in the list yet.
    143   bool highlighted_app_pending_;
    144 
    145   // We listen to this to show app installing progress.
    146   extensions::InstallTracker* tracker_;
    147 
    148   // Listen extension's load, unload, uninstalled.
    149   extensions::ExtensionRegistry* extension_registry_;
    150 
    151   DISALLOW_COPY_AND_ASSIGN(ExtensionAppModelBuilder);
    152 };
    153 
    154 #endif  // CHROME_BROWSER_UI_APP_LIST_EXTENSION_APP_MODEL_BUILDER_H_
    155