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.
5 #ifndef CHROME_BROWSER_UI_APP_LIST_EXTENSION_APP_MODEL_BUILDER_H_
6 #define CHROME_BROWSER_UI_APP_LIST_EXTENSION_APP_MODEL_BUILDER_H_
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"
19 class AppListControllerDelegate;
20 class ExtensionAppItem;
24 class AppListSyncableService;
27 namespace extensions {
29 class ExtensionRegistry;
38 // This class populates and maintains the given |model| with information from
40 class ExtensionAppModelBuilder : public extensions::InstallObserver,
41 public extensions::ExtensionRegistryObserver,
42 public app_list::AppListItemListObserver {
44 explicit ExtensionAppModelBuilder(AppListControllerDelegate* controller);
45 virtual ~ExtensionAppModelBuilder();
47 // Initialize to use app-list sync and sets |service_| to |service|.
48 void InitializeWithService(app_list::AppListSyncableService* service);
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);
55 typedef std::vector<ExtensionAppItem*> ExtensionAppList;
57 // Builds the model with the current profile.
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;
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,
83 extensions::UninstallReason reason) OVERRIDE;
84 virtual void OnShutdown(extensions::ExtensionRegistry* registry) OVERRIDE;
86 // AppListItemListObserver.
87 virtual void OnListItemMoved(size_t from_index,
89 app_list::AppListItem* item) OVERRIDE;
91 scoped_ptr<ExtensionAppItem> CreateAppItem(
92 const std::string& extension_id,
93 const std::string& extension_name,
94 const gfx::ImageSkia& installing_icon,
95 bool is_platform_app);
97 // Populates the model with apps.
100 // Inserts an app based on app ordinal prefs.
101 void InsertApp(scoped_ptr<ExtensionAppItem> app);
103 // Sets which app is intended to be highlighted. Will remove the highlight
104 // from a currently highlighted app.
105 void SetHighlightedApp(const std::string& extension_id);
107 // Sets the application app with |highlight_app_id_| in |model_| as
108 // highlighted if |highlighted_app_pending_| is true. If such an app is found,
109 // reset |highlighted_app_pending_| so that won't be highlighted again until
110 // another call to SetHighlightedApp() is made.
111 void UpdateHighlight();
113 // Returns app instance matching |extension_id| or NULL.
114 ExtensionAppItem* GetExtensionAppItem(const std::string& extension_id);
116 // Initializes the |profile_pref_change_registrar_| and the
117 // |extension_pref_change_registrar_| to listen for changes to profile and
118 // extension prefs, and call OnProfilePreferenceChanged() or
119 // OnExtensionPreferenceChanged().
120 void InitializePrefChangeRegistrars();
122 // Handles profile prefs changes.
123 void OnProfilePreferenceChanged();
125 // Handles extension prefs changes.
126 void OnExtensionPreferenceChanged();
128 // Unowned pointers to the service that owns this and associated profile.
129 app_list::AppListSyncableService* service_;
132 // Registrar used to monitor the profile prefs.
133 PrefChangeRegistrar profile_pref_change_registrar_;
135 // Registrar used to monitor the extension prefs.
136 PrefChangeRegistrar extension_pref_change_registrar_;
138 // Unowned pointer to the app list controller.
139 AppListControllerDelegate* controller_;
141 // Unowned pointer to the app list model.
142 app_list::AppListModel* model_;
144 std::string highlight_app_id_;
146 // True if we haven't set |highlight_app_id_| to be highlighted. This happens
147 // if we try to highlight an app that doesn't exist in the list yet.
148 bool highlighted_app_pending_;
150 // We listen to this to show app installing progress.
151 extensions::InstallTracker* tracker_;
153 // Listen extension's load, unload, uninstalled.
154 extensions::ExtensionRegistry* extension_registry_;
156 DISALLOW_COPY_AND_ASSIGN(ExtensionAppModelBuilder);
159 #endif // CHROME_BROWSER_UI_APP_LIST_EXTENSION_APP_MODEL_BUILDER_H_