| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/app_list/extension_app_model_builder.h" | 5 #include "chrome/browser/ui/app_list/extension_app_model_builder.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "chrome/browser/extensions/extension_ui_util.h" | 13 #include "chrome/browser/extensions/extension_ui_util.h" |
| 14 #include "chrome/browser/extensions/extension_util.h" | 14 #include "chrome/browser/extensions/extension_util.h" |
| 15 #include "chrome/browser/extensions/install_tracker.h" | 15 #include "chrome/browser/extensions/install_tracker.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" | 17 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" |
| 18 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" | 18 #include "chrome/browser/ui/app_list/app_list_syncable_service.h" |
| 19 #include "chrome/browser/ui/app_list/extension_app_item.h" | 19 #include "chrome/browser/ui/app_list/extension_app_item.h" |
| 20 #include "chrome/browser/ui/ash/launcher/launcher_extension_app_updater.h" |
| 20 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 21 #include "extensions/browser/extension_prefs.h" | 22 #include "extensions/browser/extension_prefs.h" |
| 22 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
| 23 #include "extensions/browser/extension_system.h" | 24 #include "extensions/browser/extension_system.h" |
| 24 #include "extensions/browser/extensions_browser_client.h" | 25 #include "extensions/browser/extensions_browser_client.h" |
| 25 #include "extensions/browser/pref_names.h" | 26 #include "extensions/browser/pref_names.h" |
| 26 #include "extensions/common/constants.h" | 27 #include "extensions/common/constants.h" |
| 27 #include "extensions/common/extension.h" | 28 #include "extensions/common/extension.h" |
| 28 #include "extensions/common/extension_set.h" | 29 #include "extensions/common/extension_set.h" |
| 29 #include "ui/gfx/image/image_skia.h" | 30 #include "ui/gfx/image/image_skia.h" |
| 30 #include "ui/gfx/image/image_skia_operations.h" | 31 #include "ui/gfx/image/image_skia_operations.h" |
| 31 | 32 |
| 32 using extensions::Extension; | 33 using extensions::Extension; |
| 33 | 34 |
| 34 ExtensionAppModelBuilder::ExtensionAppModelBuilder( | 35 ExtensionAppModelBuilder::ExtensionAppModelBuilder( |
| 35 AppListControllerDelegate* controller) | 36 AppListControllerDelegate* controller) |
| 36 : AppListModelBuilder(controller, ExtensionAppItem::kItemType) { | 37 : AppListModelBuilder(controller, ExtensionAppItem::kItemType) { |
| 37 } | 38 } |
| 38 | 39 |
| 39 ExtensionAppModelBuilder::~ExtensionAppModelBuilder() { | 40 ExtensionAppModelBuilder::~ExtensionAppModelBuilder() { |
| 40 OnShutdown(); | 41 OnShutdown(); |
| 41 OnShutdown(extension_registry_); | |
| 42 } | 42 } |
| 43 | 43 |
| 44 void ExtensionAppModelBuilder::InitializePrefChangeRegistrars() { | 44 void ExtensionAppModelBuilder::InitializePrefChangeRegistrars() { |
| 45 profile_pref_change_registrar_.Init(profile()->GetPrefs()); | 45 profile_pref_change_registrar_.Init(profile()->GetPrefs()); |
| 46 profile_pref_change_registrar_.Add( | 46 profile_pref_change_registrar_.Add( |
| 47 prefs::kHideWebStoreIcon, | 47 prefs::kHideWebStoreIcon, |
| 48 base::Bind(&ExtensionAppModelBuilder::OnProfilePreferenceChanged, | 48 base::Bind(&ExtensionAppModelBuilder::OnProfilePreferenceChanged, |
| 49 base::Unretained(this))); | 49 base::Unretained(this))); |
| 50 | 50 |
| 51 if (!extensions::util::IsNewBookmarkAppsEnabled()) | 51 if (!extensions::util::IsNewBookmarkAppsEnabled()) |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 if (!item) | 125 if (!item) |
| 126 return; | 126 return; |
| 127 item->SetPercentDownloaded(percent_downloaded); | 127 item->SetPercentDownloaded(percent_downloaded); |
| 128 } | 128 } |
| 129 | 129 |
| 130 void ExtensionAppModelBuilder::OnInstallFailure( | 130 void ExtensionAppModelBuilder::OnInstallFailure( |
| 131 const std::string& extension_id) { | 131 const std::string& extension_id) { |
| 132 model()->DeleteItem(extension_id); | 132 model()->DeleteItem(extension_id); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void ExtensionAppModelBuilder::OnExtensionLoaded( | 135 void ExtensionAppModelBuilder::OnAppInstalled( |
| 136 content::BrowserContext* browser_context, | 136 content::BrowserContext* browser_context, |
| 137 const extensions::Extension* extension) { | 137 const std::string& app_id) { |
| 138 extensions::ExtensionRegistry* extension_registry = |
| 139 extensions::ExtensionRegistry::Get(profile()); |
| 140 |
| 141 const Extension* extension = |
| 142 extension_registry ? extension_registry->GetInstalledExtension(app_id) |
| 143 : nullptr; |
| 144 if (!extension) { |
| 145 NOTREACHED(); |
| 146 return; |
| 147 } |
| 148 |
| 138 if (!extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile())) | 149 if (!extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile())) |
| 139 return; | 150 return; |
| 140 | 151 |
| 141 DVLOG(2) << service() << ": OnExtensionLoaded: " | 152 DVLOG(2) << service() << ": OnAppInstalled: " << app_id.substr(0, 8); |
| 142 << extension->id().substr(0, 8); | 153 ExtensionAppItem* existing_item = GetExtensionAppItem(app_id); |
| 143 ExtensionAppItem* existing_item = GetExtensionAppItem(extension->id()); | |
| 144 if (existing_item) { | 154 if (existing_item) { |
| 145 existing_item->Reload(); | 155 existing_item->UpdateIcon(); |
| 146 if (service()) | 156 if (service()) |
| 147 service()->UpdateItem(existing_item); | 157 service()->UpdateItem(existing_item); |
| 148 return; | 158 return; |
| 149 } | 159 } |
| 150 | 160 |
| 151 InsertApp(CreateAppItem(extension->id(), | 161 InsertApp(CreateAppItem(app_id, "", gfx::ImageSkia(), |
| 152 "", | |
| 153 gfx::ImageSkia(), | |
| 154 extension->is_platform_app())); | 162 extension->is_platform_app())); |
| 155 } | 163 } |
| 156 | 164 |
| 157 void ExtensionAppModelBuilder::OnExtensionUnloaded( | 165 void ExtensionAppModelBuilder::OnAppUpdated( |
| 158 content::BrowserContext* browser_context, | 166 content::BrowserContext* browser_context, |
| 159 const extensions::Extension* extension, | 167 const std::string& app_id) { |
| 160 extensions::UnloadedExtensionInfo::Reason reason) { | 168 ExtensionAppItem* item = GetExtensionAppItem(app_id); |
| 161 ExtensionAppItem* item = GetExtensionAppItem(extension->id()); | |
| 162 if (!item) | 169 if (!item) |
| 163 return; | 170 return; |
| 164 item->UpdateIcon(); | 171 item->UpdateIcon(); |
| 165 } | 172 } |
| 166 | 173 |
| 167 void ExtensionAppModelBuilder::OnExtensionUninstalled( | 174 void ExtensionAppModelBuilder::OnAppUninstalled( |
| 168 content::BrowserContext* browser_context, | 175 content::BrowserContext* browser_context, |
| 169 const extensions::Extension* extension, | 176 const std::string& app_id) { |
| 170 extensions::UninstallReason reason) { | |
| 171 if (service()) { | 177 if (service()) { |
| 172 DVLOG(2) << service() << ": OnExtensionUninstalled: " | 178 DVLOG(2) << service() << ": OnAppUninstalled: " << app_id.substr(0, 8); |
| 173 << extension->id().substr(0, 8); | 179 service()->RemoveUninstalledItem(app_id); |
| 174 service()->RemoveUninstalledItem(extension->id()); | |
| 175 return; | 180 return; |
| 176 } | 181 } |
| 177 model()->DeleteUninstalledItem(extension->id()); | 182 model()->DeleteUninstalledItem(app_id); |
| 178 } | 183 } |
| 179 | 184 |
| 180 void ExtensionAppModelBuilder::OnDisabledExtensionUpdated( | 185 void ExtensionAppModelBuilder::OnDisabledExtensionUpdated( |
| 181 const Extension* extension) { | 186 const Extension* extension) { |
| 182 if (!extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile())) | 187 if (!extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile())) |
| 183 return; | 188 return; |
| 184 | 189 |
| 185 ExtensionAppItem* existing_item = GetExtensionAppItem(extension->id()); | 190 ExtensionAppItem* existing_item = GetExtensionAppItem(extension->id()); |
| 186 if (existing_item) | 191 if (existing_item) |
| 187 existing_item->Reload(); | 192 existing_item->Reload(); |
| 188 } | 193 } |
| 189 | 194 |
| 190 void ExtensionAppModelBuilder::OnShutdown() { | 195 void ExtensionAppModelBuilder::OnShutdown() { |
| 191 if (tracker_) { | 196 if (tracker_) { |
| 192 tracker_->RemoveObserver(this); | 197 tracker_->RemoveObserver(this); |
| 193 tracker_ = nullptr; | 198 tracker_ = nullptr; |
| 194 } | 199 } |
| 195 } | 200 app_updater_.reset(); |
| 196 | |
| 197 void ExtensionAppModelBuilder::OnShutdown( | |
| 198 extensions::ExtensionRegistry* registry) { | |
| 199 if (!extension_registry_) | |
| 200 return; | |
| 201 | |
| 202 DCHECK_EQ(extension_registry_, registry); | |
| 203 extension_registry_->RemoveObserver(this); | |
| 204 extension_registry_ = nullptr; | |
| 205 } | 201 } |
| 206 | 202 |
| 207 std::unique_ptr<ExtensionAppItem> ExtensionAppModelBuilder::CreateAppItem( | 203 std::unique_ptr<ExtensionAppItem> ExtensionAppModelBuilder::CreateAppItem( |
| 208 const std::string& extension_id, | 204 const std::string& extension_id, |
| 209 const std::string& extension_name, | 205 const std::string& extension_name, |
| 210 const gfx::ImageSkia& installing_icon, | 206 const gfx::ImageSkia& installing_icon, |
| 211 bool is_platform_app) { | 207 bool is_platform_app) { |
| 212 return base::WrapUnique( | 208 return base::WrapUnique( |
| 213 new ExtensionAppItem(profile(), GetSyncItem(extension_id), extension_id, | 209 new ExtensionAppItem(profile(), GetSyncItem(extension_id), extension_id, |
| 214 extension_name, installing_icon, is_platform_app)); | 210 extension_name, installing_icon, is_platform_app)); |
| 215 } | 211 } |
| 216 | 212 |
| 217 void ExtensionAppModelBuilder::BuildModel() { | 213 void ExtensionAppModelBuilder::BuildModel() { |
| 218 DCHECK(!tracker_); | 214 DCHECK(!tracker_); |
| 219 | 215 |
| 220 InitializePrefChangeRegistrars(); | 216 InitializePrefChangeRegistrars(); |
| 221 | 217 |
| 222 tracker_ = controller()->GetInstallTrackerFor(profile()); | 218 tracker_ = controller()->GetInstallTrackerFor(profile()); |
| 223 extension_registry_ = extensions::ExtensionRegistry::Get(profile()); | |
| 224 | 219 |
| 225 PopulateApps(); | 220 PopulateApps(); |
| 226 | 221 |
| 227 // Start observing after model is built. | 222 // Start observing after model is built. |
| 228 if (tracker_) | 223 if (tracker_) |
| 229 tracker_->AddObserver(this); | 224 tracker_->AddObserver(this); |
| 230 | 225 |
| 231 if (extension_registry_) | 226 app_updater_.reset(new LauncherExtensionAppUpdater(this, profile())); |
| 232 extension_registry_->AddObserver(this); | |
| 233 } | 227 } |
| 234 | 228 |
| 235 void ExtensionAppModelBuilder::PopulateApps() { | 229 void ExtensionAppModelBuilder::PopulateApps() { |
| 236 extensions::ExtensionSet extensions; | 230 extensions::ExtensionSet extensions; |
| 237 controller()->GetApps(profile(), &extensions); | 231 controller()->GetApps(profile(), &extensions); |
| 238 | 232 |
| 239 for (extensions::ExtensionSet::const_iterator app = extensions.begin(); | 233 for (extensions::ExtensionSet::const_iterator app = extensions.begin(); |
| 240 app != extensions.end(); ++app) { | 234 app != extensions.end(); ++app) { |
| 241 if (!extensions::ui_util::ShouldDisplayInAppLauncher(app->get(), profile())) | 235 if (!extensions::ui_util::ShouldDisplayInAppLauncher(app->get(), profile())) |
| 242 continue; | 236 continue; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 app_list::AppListItem* item = item_list->item_at(idx + 1); | 270 app_list::AppListItem* item = item_list->item_at(idx + 1); |
| 277 if (item->GetItemType() == ExtensionAppItem::kItemType) { | 271 if (item->GetItemType() == ExtensionAppItem::kItemType) { |
| 278 next = static_cast<ExtensionAppItem*>(item); | 272 next = static_cast<ExtensionAppItem*>(item); |
| 279 break; | 273 break; |
| 280 } | 274 } |
| 281 } | 275 } |
| 282 // item->Move will call set_position, overriding the item's position. | 276 // item->Move will call set_position, overriding the item's position. |
| 283 if (prev || next) | 277 if (prev || next) |
| 284 static_cast<ExtensionAppItem*>(item)->Move(prev, next); | 278 static_cast<ExtensionAppItem*>(item)->Move(prev, next); |
| 285 } | 279 } |
| OLD | NEW |