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