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#include "chrome/browser/ui/app_list/extension_app_model_builder.h" 6 7#include <algorithm> 8 9#include "base/auto_reset.h" 10#include "base/prefs/pref_service.h" 11#include "chrome/browser/chrome_notification_types.h" 12#include "chrome/browser/extensions/extension_prefs.h" 13#include "chrome/browser/extensions/install_tracker.h" 14#include "chrome/browser/extensions/install_tracker_factory.h" 15#include "chrome/browser/profiles/profile.h" 16#include "chrome/browser/ui/app_list/app_list_controller_delegate.h" 17#include "chrome/browser/ui/app_list/extension_app_item.h" 18#include "chrome/common/extensions/extension_constants.h" 19#include "chrome/common/extensions/extension_set.h" 20#include "chrome/common/pref_names.h" 21#include "content/public/browser/notification_service.h" 22#include "extensions/common/extension.h" 23#include "ui/gfx/image/image_skia.h" 24 25using extensions::Extension; 26 27namespace { 28 29bool ShouldDisplayInAppLauncher(Profile* profile, 30 scoped_refptr<const Extension> app) { 31 // If it's the web store, check the policy. 32 bool blocked_by_policy = 33 (app->id() == extension_misc::kWebStoreAppId || 34 app->id() == extension_misc::kEnterpriseWebStoreAppId) && 35 profile->GetPrefs()->GetBoolean(prefs::kHideWebStoreIcon); 36 return app->ShouldDisplayInAppLauncher() && !blocked_by_policy; 37} 38 39} // namespace 40 41ExtensionAppModelBuilder::ExtensionAppModelBuilder( 42 Profile* profile, 43 app_list::AppListModel* model, 44 AppListControllerDelegate* controller) 45 : profile_(profile), 46 controller_(controller), 47 model_(model), 48 highlighted_app_pending_(false), 49 tracker_(NULL) { 50 model_->item_list()->AddObserver(this); 51 BuildModel(); 52} 53 54ExtensionAppModelBuilder::~ExtensionAppModelBuilder() { 55 OnShutdown(); 56 model_->item_list()->RemoveObserver(this); 57} 58 59void ExtensionAppModelBuilder::OnBeginExtensionInstall( 60 const ExtensionInstallParams& params) { 61 if (!params.is_app || params.is_ephemeral) 62 return; 63 64 ExtensionAppItem* existing_item = GetExtensionAppItem(params.extension_id); 65 if (existing_item) { 66 existing_item->SetIsInstalling(true); 67 return; 68 } 69 70 InsertApp(new ExtensionAppItem(profile_, 71 params.extension_id, 72 params.extension_name, 73 params.installing_icon, 74 params.is_platform_app)); 75 SetHighlightedApp(params.extension_id); 76} 77 78void ExtensionAppModelBuilder::OnDownloadProgress( 79 const std::string& extension_id, 80 int percent_downloaded) { 81 ExtensionAppItem* item = GetExtensionAppItem(extension_id); 82 if (!item) 83 return; 84 item->SetPercentDownloaded(percent_downloaded); 85} 86 87void ExtensionAppModelBuilder::OnInstallFailure( 88 const std::string& extension_id) { 89 model_->item_list()->DeleteItem(extension_id); 90} 91 92void ExtensionAppModelBuilder::OnExtensionLoaded(const Extension* extension) { 93 if (!extension->ShouldDisplayInAppLauncher()) 94 return; 95 96 ExtensionAppItem* existing_item = GetExtensionAppItem(extension->id()); 97 if (existing_item) { 98 existing_item->Reload(); 99 return; 100 } 101 102 InsertApp(new ExtensionAppItem(profile_, 103 extension->id(), 104 "", 105 gfx::ImageSkia(), 106 extension->is_platform_app())); 107 UpdateHighlight(); 108} 109 110void ExtensionAppModelBuilder::OnExtensionUnloaded(const Extension* extension) { 111 ExtensionAppItem* item = GetExtensionAppItem(extension->id()); 112 if (!item) 113 return; 114 item->UpdateIcon(); 115} 116 117void ExtensionAppModelBuilder::OnExtensionUninstalled( 118 const Extension* extension) { 119 model_->item_list()->DeleteItem(extension->id()); 120} 121 122void ExtensionAppModelBuilder::OnAppsReordered() { 123 // Do nothing; App List order does not track extensions order. 124} 125 126void ExtensionAppModelBuilder::OnAppInstalledToAppList( 127 const std::string& extension_id) { 128 SetHighlightedApp(extension_id); 129} 130 131void ExtensionAppModelBuilder::OnShutdown() { 132 if (tracker_) { 133 tracker_->RemoveObserver(this); 134 tracker_ = NULL; 135 } 136} 137 138void ExtensionAppModelBuilder::AddApps(const ExtensionSet* extensions, 139 ExtensionAppList* apps) { 140 for (ExtensionSet::const_iterator app = extensions->begin(); 141 app != extensions->end(); ++app) { 142 if (ShouldDisplayInAppLauncher(profile_, *app)) 143 apps->push_back(new ExtensionAppItem(profile_, 144 (*app)->id(), 145 "", 146 gfx::ImageSkia(), 147 (*app)->is_platform_app())); 148 } 149} 150 151void ExtensionAppModelBuilder::BuildModel() { 152 // Delete any extension apps. 153 model_->item_list()->DeleteItemsByType(ExtensionAppItem::kAppType); 154 155 if (tracker_) 156 tracker_->RemoveObserver(this); 157 158 tracker_ = controller_->GetInstallTrackerFor(profile_); 159 160 PopulateApps(); 161 UpdateHighlight(); 162 163 // Start observing after model is built. 164 if (tracker_) 165 tracker_->AddObserver(this); 166} 167 168void ExtensionAppModelBuilder::PopulateApps() { 169 ExtensionSet extensions; 170 controller_->GetApps(profile_, &extensions); 171 ExtensionAppList apps; 172 AddApps(&extensions, &apps); 173 174 if (apps.empty()) 175 return; 176 177 for (size_t i = 0; i < apps.size(); ++i) 178 InsertApp(apps[i]); 179} 180 181void ExtensionAppModelBuilder::InsertApp(ExtensionAppItem* app) { 182 model_->item_list()->AddItem(app); 183} 184 185void ExtensionAppModelBuilder::SetHighlightedApp( 186 const std::string& extension_id) { 187 if (extension_id == highlight_app_id_) 188 return; 189 ExtensionAppItem* old_app = GetExtensionAppItem(highlight_app_id_); 190 if (old_app) 191 old_app->SetHighlighted(false); 192 highlight_app_id_ = extension_id; 193 ExtensionAppItem* new_app = GetExtensionAppItem(highlight_app_id_); 194 highlighted_app_pending_ = !new_app; 195 if (new_app) 196 new_app->SetHighlighted(true); 197} 198 199ExtensionAppItem* ExtensionAppModelBuilder::GetExtensionAppItem( 200 const std::string& extension_id) { 201 app_list::AppListItemModel* item = 202 model_->item_list()->FindItem(extension_id); 203 LOG_IF(ERROR, item && 204 item->GetAppType() != ExtensionAppItem::kAppType) 205 << "App Item matching id: " << extension_id 206 << " has incorrect type: '" << item->GetAppType() << "'"; 207 return static_cast<ExtensionAppItem*>(item); 208} 209 210void ExtensionAppModelBuilder::UpdateHighlight() { 211 DCHECK(model_); 212 if (!highlighted_app_pending_ || highlight_app_id_.empty()) 213 return; 214 ExtensionAppItem* item = GetExtensionAppItem(highlight_app_id_); 215 if (!item) 216 return; 217 item->SetHighlighted(true); 218 highlighted_app_pending_ = false; 219} 220 221void ExtensionAppModelBuilder::OnListItemMoved( 222 size_t from_index, 223 size_t to_index, 224 app_list::AppListItemModel* item) { 225 // This will get called from AppListItemList::ListItemMoved after 226 // set_position is called for the item. 227 app_list::AppListItemList* item_list = model_->item_list(); 228 if (item->GetAppType() != ExtensionAppItem::kAppType) 229 return; 230 231 ExtensionAppItem* prev = NULL; 232 for (size_t idx = to_index; idx > 0; --idx) { 233 app_list::AppListItemModel* item = item_list->item_at(idx - 1); 234 if (item->GetAppType() == ExtensionAppItem::kAppType) { 235 prev = static_cast<ExtensionAppItem*>(item); 236 break; 237 } 238 } 239 ExtensionAppItem* next = NULL; 240 for (size_t idx = to_index; idx < item_list->item_count() - 1; ++idx) { 241 app_list::AppListItemModel* item = item_list->item_at(idx + 1); 242 if (item->GetAppType() == ExtensionAppItem::kAppType) { 243 next = static_cast<ExtensionAppItem*>(item); 244 break; 245 } 246 } 247 // item->Move will call set_position, overriding the item's position. 248 if (prev || next) 249 static_cast<ExtensionAppItem*>(item)->Move(prev, next); 250} 251