extension_app_model_builder.h revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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 17class AppListControllerDelegate; 18class ExtensionAppItem; 19class Profile; 20 21namespace app_list { 22class AppListSyncableService; 23} 24 25namespace extensions { 26class Extension; 27class ExtensionSet; 28class InstallTracker; 29} 30 31namespace gfx { 32class ImageSkia; 33} 34 35// This class populates and maintains the given |model| with information from 36// |profile|. 37class 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 OnExtensionLoaded( 63 const extensions::Extension* extension) OVERRIDE; 64 virtual void OnExtensionUnloaded( 65 const extensions::Extension* extension) OVERRIDE; 66 virtual void OnExtensionUninstalled( 67 const extensions::Extension* extension) OVERRIDE; 68 virtual void OnDisabledExtensionUpdated( 69 const extensions::Extension* extension) OVERRIDE; 70 virtual void OnAppInstalledToAppList( 71 const std::string& extension_id) OVERRIDE; 72 virtual void OnShutdown() OVERRIDE; 73 74 // AppListItemListObserver 75 virtual void OnListItemMoved(size_t from_index, 76 size_t to_index, 77 app_list::AppListItem* item) OVERRIDE; 78 79 scoped_ptr<ExtensionAppItem> CreateAppItem( 80 const std::string& extension_id, 81 const std::string& extension_name, 82 const gfx::ImageSkia& installing_icon, 83 bool is_platform_app); 84 85 // Populates the model with apps. 86 void PopulateApps(); 87 88 // Re-sort apps in case app ordinal prefs are changed. 89 void ResortApps(); 90 91 // Inserts an app based on app ordinal prefs. 92 void InsertApp(scoped_ptr<ExtensionAppItem> app); 93 94 // Sets which app is intended to be highlighted. Will remove the highlight 95 // from a currently highlighted app. 96 void SetHighlightedApp(const std::string& extension_id); 97 98 // Sets the application app with |highlight_app_id_| in |model_| as 99 // highlighted if |highlighted_app_pending_| is true. If such an app is found, 100 // reset |highlighted_app_pending_| so that won't be highlighted again until 101 // another call to SetHighlightedApp() is made. 102 void UpdateHighlight(); 103 104 // Returns app instance matching |extension_id| or NULL. 105 ExtensionAppItem* GetExtensionAppItem(const std::string& extension_id); 106 107 // Initializes the |extension_pref_change_registrar| to listen for extension 108 // prefs changes. OnExtensionPreferenceChanged() is called when extension 109 // prefs change. 110 void InitializePrefChangeRegistrar(); 111 112 // Handles extension prefs changes. 113 void OnExtensionPreferenceChanged(); 114 115 // Unowned pointers to the service that owns this and associated profile. 116 app_list::AppListSyncableService* service_; 117 Profile* profile_; 118 119 // Registrar used to monitor the extension prefs. 120 PrefChangeRegistrar extension_pref_change_registrar_; 121 122 // Unowned pointer to the app list controller. 123 AppListControllerDelegate* controller_; 124 125 // Unowned pointer to the app list model. 126 app_list::AppListModel* model_; 127 128 std::string highlight_app_id_; 129 130 // True if we haven't set |highlight_app_id_| to be highlighted. This happens 131 // if we try to highlight an app that doesn't exist in the list yet. 132 bool highlighted_app_pending_; 133 134 // We listen to this to show app installing progress. 135 extensions::InstallTracker* tracker_; 136 137 DISALLOW_COPY_AND_ASSIGN(ExtensionAppModelBuilder); 138}; 139 140#endif // CHROME_BROWSER_UI_APP_LIST_EXTENSION_APP_MODEL_BUILDER_H_ 141