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