blob: 47d75c3bef4b398b3e5516735d9d8e67b8893ea7 [file] [log] [blame]
[email protected]098fa7a2013-03-08 22:11:171// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]695b5712012-12-06 23:55:288#include <iterator>
[email protected]97d2f1d2011-01-15 00:41:089#include <set>
[email protected]654512b2010-09-01 02:09:4210
[email protected]62433d32011-10-12 22:33:1211#include "base/command_line.h"
[email protected]835d7c82010-10-14 04:38:3812#include "base/metrics/histogram.h"
[email protected]3ea1b182013-02-08 22:38:4113#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1714#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1815#include "base/strings/utf_string_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1416#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0617#include "base/threading/thread_restrictions.h"
[email protected]41a17c52013-06-28 00:27:5318#include "base/time/time.h"
[email protected]15730c42009-09-03 00:03:2019#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3620#include "chrome/browser/chrome_notification_types.h"
[email protected]d8c8f25f2011-11-02 18:18:0121#include "chrome/browser/extensions/component_loader.h"
[email protected]13e062e2014-08-09 10:21:5522#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3823#include "chrome/browser/extensions/data_deleter.h"
rdevlin.cronin2813c1b2014-08-26 23:51:0824#include "chrome/browser/extensions/extension_action_storage_manager.h"
[email protected]5a145e82014-05-29 22:19:0725#include "chrome/browser/extensions/extension_assets_manager.h"
[email protected]62f051c2012-03-29 17:04:4426#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]373daf972014-04-10 01:50:4427#include "chrome/browser/extensions/extension_error_controller.h"
[email protected]00b38242012-07-18 18:43:2228#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]19eb80152011-02-26 00:28:4329#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5230#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]617342a42013-12-18 23:34:0331#include "chrome/browser/extensions/extension_util.h"
[email protected]2894a512014-06-26 19:03:5632#include "chrome/browser/extensions/external_install_manager.h"
[email protected]5df038b2012-07-16 19:03:2733#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4634#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0135#include "chrome/browser/extensions/installed_loader.h"
[email protected]f3d3b382014-03-14 21:19:2836#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]13e062e2014-08-09 10:21:5537#include "chrome/browser/extensions/permissions_updater.h"
[email protected]90878c52014-04-04 18:21:0238#include "chrome/browser/extensions/shared_module_service.h"
[email protected]13e062e2014-08-09 10:21:5539#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]044e86992014-01-24 22:59:1140#include "chrome/browser/extensions/updater/extension_cache.h"
[email protected]02c967fe2014-08-12 14:48:4341#include "chrome/browser/extensions/updater/extension_downloader.h"
[email protected]13e062e2014-08-09 10:21:5542#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3343#include "chrome/browser/profiles/profile.h"
[email protected]130e92a2014-08-15 08:37:2744#include "chrome/browser/signin/profile_identity_provider.h"
45#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
46#include "chrome/browser/signin/signin_manager_factory.h"
[email protected]b5a507b22013-11-08 20:41:5747#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1748#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3149#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]130e92a2014-08-15 08:37:2750#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
[email protected]b07e606e2012-09-15 20:16:1551#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5452#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4553#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1954#include "chrome/common/extensions/extension_constants.h"
[email protected]15d4d2d2013-08-09 06:49:0355#include "chrome/common/extensions/features/feature_channel.h"
[email protected]65348062013-01-15 07:27:2256#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]25b34332009-06-05 21:53:1957#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1458#include "chrome/common/url_constants.h"
[email protected]fdd28372014-08-21 02:27:2659#include "components/crx_file/id_util.h"
[email protected]130e92a2014-08-15 08:37:2760#include "components/signin/core/browser/signin_manager.h"
[email protected]7a83dd072013-09-03 12:09:1761#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]dccba4f82014-05-29 00:52:5662#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3763#include "content/public/browser/notification_service.h"
[email protected]f3b1a082011-11-18 00:34:3064#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4265#include "content/public/browser/storage_partition.h"
[email protected]34423532013-11-21 18:13:1066#include "extensions/browser/event_router.h"
[email protected]22401dc2014-03-21 01:38:5767#include "extensions/browser/extension_host.h"
[email protected]dccba4f82014-05-29 00:52:5668#include "extensions/browser/extension_prefs.h"
[email protected]bb1bc9b32013-12-21 03:09:1469#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2470#include "extensions/browser/extension_system.h"
[email protected]4a1d9c0d2014-06-13 12:50:1171#include "extensions/browser/install_flag.h"
[email protected]234fc5ff2014-01-16 23:32:2872#include "extensions/browser/pref_names.h"
[email protected]45f5b7d2014-01-22 23:47:1373#include "extensions/browser/runtime_data.h"
[email protected]e43c61f2014-07-20 21:46:3474#include "extensions/browser/uninstall_reason.h"
[email protected]a9aa62b312013-11-29 05:35:0675#include "extensions/browser/update_observer.h"
[email protected]fb820c02014-03-13 15:07:0876#include "extensions/common/extension_messages.h"
[email protected]5ef835a2013-11-08 20:42:5777#include "extensions/common/feature_switch.h"
[email protected]85df9d12014-04-15 17:02:1478#include "extensions/common/file_util.h"
[email protected]0c3c9732013-09-16 08:53:4179#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5180#include "extensions/common/manifest_handlers/background_info.h"
[email protected]dccba4f82014-05-29 00:52:5681#include "extensions/common/one_shot_event.h"
[email protected]c41003472013-10-19 15:37:2582#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4183#include "extensions/common/permissions/permissions_data.h"
[email protected]79a60642012-10-20 21:03:1884
[email protected]eed367e2011-04-12 03:43:3185#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:1886#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:3287#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:0788#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:3189#endif
90
[email protected]55eb70e762012-02-20 17:38:3991using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:3992using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:3693using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:2794using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:3495using extensions::Extension;
[email protected]130e92a2014-08-15 08:37:2796using extensions::ExtensionDownloader;
97using extensions::ExtensionDownloaderDelegate;
[email protected]1c321ee52012-05-21 03:02:3498using extensions::ExtensionIdSet;
99using extensions::ExtensionInfo;
[email protected]599539802014-01-07 23:06:00100using extensions::ExtensionRegistry;
[email protected]289c44b2013-12-17 03:26:57101using extensions::ExtensionSet;
[email protected]215a7be2012-10-22 19:53:42102using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46103using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36104using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40105using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06106using extensions::PermissionMessage;
107using extensions::PermissionMessages;
108using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19109using extensions::SharedModuleInfo;
[email protected]90878c52014-04-04 18:21:02110using extensions::SharedModuleService;
[email protected]e410b5f2012-12-14 14:02:24111using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05112
[email protected]0c3c9732013-09-16 08:53:41113namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06114
[email protected]b6ab96d2009-08-20 18:58:19115namespace {
116
[email protected]0db124b02012-11-07 04:55:05117// Wait this many seconds after an extensions becomes idle before updating it.
[email protected]ddc6a122014-06-27 04:52:32118const int kUpdateIdleDelay = 5;
[email protected]0db124b02012-11-07 04:55:05119
[email protected]130e92a2014-08-15 08:37:27120scoped_ptr<IdentityProvider> CreateWebstoreIdentityProvider(Profile* profile) {
121 return make_scoped_ptr<IdentityProvider>(new ProfileIdentityProvider(
122 SigninManagerFactory::GetForProfile(profile),
123 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
124 LoginUIServiceFactory::GetForProfile(profile)));
125}
[email protected]130e92a2014-08-15 08:37:27126
[email protected]c6d474f82009-12-16 21:11:06127} // namespace
[email protected]b6ab96d2009-08-20 18:58:19128
[email protected]eaa7dd182010-12-14 11:09:00129// ExtensionService.
[email protected]6014d672008-12-05 00:38:25130
[email protected]8e4560b62011-01-14 10:09:14131void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12132 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20133
[email protected]7a4c6852010-09-16 03:44:22134 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27135 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21136 for (i = external_extension_providers_.begin();
137 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14138 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21139 if (i->get()->HasExtension(id))
140 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22141 }
142
[email protected]0f48fca2011-05-19 18:46:35143 // We get the list of external extensions to check from preferences.
144 // It is possible that an extension has preferences but is not loaded.
145 // For example, an extension that requires experimental permissions
146 // will not be loaded if the experimental command line flag is not used.
147 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40148 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35149 // We can't call UninstallExtension with an unloaded/invalid
150 // extension ID.
151 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
152 << "with id: " << id;
153 return;
154 }
[email protected]42d58f62014-07-31 01:32:45155 UninstallExtension(id,
156 extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION,
157 base::Bind(&base::DoNothing),
158 NULL);
[email protected]7a4c6852010-09-16 03:44:22159}
160
[email protected]7f8f24f2012-11-15 19:40:14161void ExtensionService::SetFileTaskRunnerForTesting(
162 base::SequencedTaskRunner* task_runner) {
163 file_task_runner_ = task_runner;
164}
165
[email protected]8e4560b62011-01-14 10:09:14166void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22167 external_extension_providers_.clear();
168}
169
[email protected]8e4560b62011-01-14 10:09:14170void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27171 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12172 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21173 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27174 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22175}
176
[email protected]8f959f522014-08-06 06:26:28177void ExtensionService::BlacklistExtensionForTest(
178 const std::string& extension_id) {
179 ExtensionIdSet blocked;
180 ExtensionIdSet unchanged;
181 blocked.insert(extension_id);
182 UpdateBlockedExtensions(blocked, unchanged);
183}
184
[email protected]9060d8b02012-01-13 02:14:30185bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22186 const std::string& id,
[email protected]d8fd0fd2014-03-24 13:16:06187 const std::string& install_parameter,
[email protected]21a5a672010-11-04 10:47:42188 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48189 Manifest::Location location,
190 int creation_flags,
191 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38192 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:26193 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20194
[email protected]044e86992014-01-24 22:59:11195 if (Manifest::IsExternalLocation(location)) {
196 // All extensions that are not user specific can be cached.
197 extensions::ExtensionCache::GetInstance()->AllowCaching(id);
198 }
199
[email protected]8a87a5332011-08-11 17:54:59200 const Extension* extension = GetExtensionById(id, true);
201 if (extension) {
202 // Already installed. Skip this install if the current location has
203 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40204 Manifest::Location current = extension->location();
205 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30206 return false;
[email protected]8a87a5332011-08-11 17:54:59207 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22208 }
[email protected]9060d8b02012-01-13 02:14:30209
210 // Add |id| to the set of pending extensions. If it can not be added,
211 // then there is already a pending record from a higher-priority install
212 // source. In this case, signal that this extension will not be
213 // installed by returning false.
214 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]d8fd0fd2014-03-24 13:16:06215 id,
216 install_parameter,
217 update_url,
218 location,
219 creation_flags,
220 mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30221 return false;
[email protected]31bb5ee62012-09-12 22:58:40222 }
[email protected]9060d8b02012-01-13 02:14:30223
[email protected]94fde232012-04-27 10:22:30224 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30225 return true;
[email protected]7a4c6852010-09-16 03:44:22226}
227
[email protected]6aeac8342010-10-01 20:21:18228// static
[email protected]cc2f55c2014-07-08 02:19:04229// This function is used to uninstall an extension via sync. The LOG statements
230// within this function are used to inform the user if the uninstall cannot be
231// done.
[email protected]eaa7dd182010-12-14 11:09:00232bool ExtensionService::UninstallExtensionHelper(
233 ExtensionService* extensions_service,
[email protected]cc2f55c2014-07-08 02:19:04234 const std::string& extension_id,
[email protected]e43c61f2014-07-20 21:46:34235 extensions::UninstallReason reason) {
[email protected]d6ebc9792011-04-07 18:18:33236 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40237 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18238 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33239 << "id: " << extension_id;
240 return false;
[email protected]6aeac8342010-10-01 20:21:18241 }
242
[email protected]d6ebc9792011-04-07 18:18:33243 // The following call to UninstallExtension will not allow an uninstall of a
244 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09245 base::string16 error;
[email protected]42d58f62014-07-31 01:32:45246 if (!extensions_service->UninstallExtension(
247 extension_id, reason, base::Bind(&base::DoNothing), &error)) {
[email protected]d6ebc9792011-04-07 18:18:33248 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
249 << ": " << error;
250 return false;
251 }
[email protected]95da88c42011-03-31 10:07:33252
[email protected]6aeac8342010-10-01 20:21:18253 return true;
254}
255
[email protected]eaa7dd182010-12-14 11:09:00256ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24257 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45258 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23259 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39260 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03261 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35262 bool extensions_enabled,
263 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39264 : extensions::Blacklist::Observer(blacklist),
265 profile_(profile),
[email protected]bd306722012-07-11 20:43:59266 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43267 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28268 blacklist_(blacklist),
[email protected]f8aefb132013-10-30 09:29:52269 extension_sync_service_(NULL),
[email protected]5fdfa562013-12-27 17:43:59270 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]6c9bedf2014-05-21 03:55:51271 pending_extension_manager_(profile),
[email protected]a9b00ac2009-06-25 21:03:23272 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03273 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13274 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33275 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35276 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18277 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54278 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19279 installs_delayed_for_gc_(false),
[email protected]e5f8fffe2014-04-02 00:30:44280 is_first_run_(false),
[email protected]90878c52014-04-04 18:21:02281 shared_module_service_(new extensions::SharedModuleService(profile_)) {
[email protected]a29a517a2011-01-21 21:11:12282 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20283
[email protected]36a784c2009-06-23 06:21:08284 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02285 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
286 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23287 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08288
[email protected]3c4abc82012-10-22 22:25:54289 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
290 content::NotificationService::AllBrowserContextsAndSources());
[email protected]adf5a102014-07-31 12:44:06291 registrar_.Add(this,
292 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07293 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27294 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07295 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08296 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
297 content::NotificationService::AllBrowserContextsAndSources());
[email protected]ebe07772014-05-22 04:16:06298 registrar_.Add(this,
299 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
300 content::Source<Profile>(profile_));
[email protected]2fb7dc982010-09-29 12:24:28301 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03302 base::Closure callback =
303 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
304 base::Unretained(this));
[email protected]234fc5ff2014-01-16 23:32:28305 pref_change_registrar_.Add(extensions::pref_names::kInstallAllowList,
306 callback);
307 pref_change_registrar_.Add(extensions::pref_names::kInstallDenyList,
308 callback);
309 pref_change_registrar_.Add(extensions::pref_names::kAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29310
[email protected]93fd78f42009-07-10 16:43:17311 // Set up the ExtensionUpdater
312 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02313 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17314 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25315 base::StringToInt(command_line->GetSwitchValueASCII(
316 switches::kExtensionsUpdateFrequency),
317 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17318 }
[email protected]044e86992014-01-24 22:59:11319 updater_.reset(new extensions::ExtensionUpdater(
320 this,
321 extension_prefs,
322 profile->GetPrefs(),
323 profile,
324 update_frequency,
[email protected]c8521972014-08-08 08:35:07325 extensions::ExtensionCache::GetInstance(),
[email protected]130e92a2014-08-15 08:37:27326 base::Bind(&ExtensionService::CreateExtensionDownloader,
327 base::Unretained(this))));
[email protected]93fd78f42009-07-10 16:43:17328 }
[email protected]93fd78f42009-07-10 16:43:17329
[email protected]25ae0152011-11-18 14:40:02330 component_loader_.reset(
331 new extensions::ComponentLoader(this,
332 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08333 g_browser_process->local_state(),
334 profile));
[email protected]8e4560b62011-01-14 10:09:14335
[email protected]0436b102011-04-15 18:30:03336 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25337 extensions::ExternalProviderImpl::CreateExternalProviders(
338 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05339 }
[email protected]b671760b2010-07-15 21:13:47340
[email protected]74474042013-11-21 12:03:54341 // Set this as the ExtensionService for app sorting to ensure it causes syncs
342 // if required.
[email protected]b3aa7182013-04-25 04:45:23343 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
344
[email protected]373daf972014-04-10 01:50:44345 error_controller_.reset(
346 new extensions::ExtensionErrorController(profile_, is_first_run_));
[email protected]374ceb6f2014-07-02 19:25:34347 external_install_manager_.reset(
348 new extensions::ExternalInstallManager(profile_, is_first_run_));
[email protected]373daf972014-04-10 01:50:44349
[email protected]c77f2352012-08-08 22:07:58350 extension_action_storage_manager_.reset(
351 new extensions::ExtensionActionStorageManager(profile_));
[email protected]c77f2352012-08-08 22:07:58352
[email protected]cb0e50312011-05-09 15:03:07353 // How long is the path to the Extensions directory?
354 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
355 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25356}
357
[email protected]84df8332011-12-06 18:22:46358const ExtensionSet* ExtensionService::extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14359 return &registry_->enabled_extensions();
[email protected]ddb1e5a2010-12-13 20:10:45360}
361
[email protected]3f213ad2012-07-26 23:39:41362extensions::PendingExtensionManager*
363 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37364 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45365}
366
[email protected]eaa7dd182010-12-14 11:09:00367ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17368 // No need to unload extensions here because they are profile-scoped, and the
369 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17370
[email protected]5df038b2012-07-16 19:03:27371 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14372 for (i = external_extension_providers_.begin();
373 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27374 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14375 provider->ServiceShutdown();
376 }
[email protected]6014d672008-12-05 00:38:25377}
378
[email protected]037228a2012-10-05 01:36:16379void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21380 system_->management_policy()->UnregisterProvider(
381 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16382}
383
[email protected]b2907fd2011-03-25 16:43:37384const Extension* ExtensionService::GetExtensionById(
385 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00386 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28387 if (include_disabled) {
388 // Include blacklisted extensions here because there are hundreds of
389 // callers of this function, and many might assume that this includes those
390 // that have been disabled due to blacklisting.
[email protected]599539802014-01-07 23:06:00391 include_mask |= ExtensionRegistry::DISABLED |
392 ExtensionRegistry::BLACKLISTED;
[email protected]695b5712012-12-06 23:55:28393 }
[email protected]599539802014-01-07 23:06:00394 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28395}
396
[email protected]eaa7dd182010-12-14 11:09:00397void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12398 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20399
[email protected]3350d712013-11-18 09:32:24400 base::Time begin_time = base::Time::Now();
401
[email protected]4a10006a2013-05-17 23:18:35402 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14403 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32404
[email protected]820d9bd2013-04-03 03:46:03405 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03406 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
407 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
408 // The sole purpose of this launch is to install a new extension from CWS
409 // and immediately terminate: loading already installed extensions is
410 // unnecessary and may interfere with the inline install dialog (e.g. if an
411 // extension listens to onStartup and opens a window).
412 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25413 } else {
[email protected]47b60072013-06-20 10:35:56414 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
415 component_loader_->LoadAll();
416 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19417
[email protected]2edd57852013-08-14 22:07:40418 // Attempt to re-enable extensions whose only disable reason is reloading.
419 std::vector<std::string> extensions_to_enable;
[email protected]bb1bc9b32013-12-21 03:09:14420 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
421 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
422 iter != disabled_extensions.end(); ++iter) {
[email protected]2edd57852013-08-14 22:07:40423 const Extension* e = iter->get();
424 if (extension_prefs_->GetDisableReasons(e->id()) ==
425 Extension::DISABLE_RELOAD) {
426 extensions_to_enable.push_back(e->id());
427 }
428 }
429 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
430 it != extensions_to_enable.end(); ++it) {
431 EnableExtension(*it);
432 }
433
[email protected]9f4e4f082013-06-21 07:11:19434 // Finish install (if possible) of extensions that were still delayed while
435 // the browser was shut down.
436 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
437 extension_prefs_->GetAllDelayedInstallInfo());
438 for (size_t i = 0; i < delayed_info->size(); ++i) {
439 ExtensionInfo* info = delayed_info->at(i).get();
440 scoped_refptr<const Extension> extension(NULL);
441 if (info->extension_manifest) {
442 std::string error;
443 extension = Extension::Create(
444 info->extension_path,
445 info->extension_location,
446 *info->extension_manifest,
447 extension_prefs_->GetDelayedInstallCreationFlags(
448 info->extension_id),
449 info->extension_id,
450 &error);
451 if (extension.get())
452 delayed_installs_.Insert(extension);
453 }
454 }
455 MaybeFinishDelayedInstallations();
456
457 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
458 extension_prefs_->GetAllDelayedInstallInfo());
459 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
460 delayed_info2->size() - delayed_info->size());
461
[email protected]47b60072013-06-20 10:35:56462 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25463
[email protected]47b60072013-06-20 10:35:56464 // TODO(erikkay) this should probably be deferred to a future point
465 // rather than running immediately at startup.
466 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57467
[email protected]2d19eb6e2014-01-27 17:30:00468 LoadGreylistFromPrefs();
[email protected]399583b2012-12-11 09:33:42469 }
[email protected]3350d712013-11-18 09:32:24470
471 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
472 base::Time::Now() - begin_time);
[email protected]6014d672008-12-05 00:38:25473}
474
[email protected]2d19eb6e2014-01-27 17:30:00475void ExtensionService::LoadGreylistFromPrefs() {
[email protected]f47f7172014-03-19 19:27:10476 scoped_ptr<ExtensionSet> all_extensions =
477 registry_->GenerateInstalledExtensionsSet();
[email protected]2d19eb6e2014-01-27 17:30:00478
479 for (ExtensionSet::const_iterator it = all_extensions->begin();
480 it != all_extensions->end(); ++it) {
481 extensions::BlacklistState state =
482 extension_prefs_->GetExtensionBlacklistState((*it)->id());
483 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
484 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
485 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
486 greylist_.Insert(*it);
487 }
488}
489
[email protected]31bb5ee62012-09-12 22:58:40490bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45491 const base::FilePath& extension_path,
[email protected]044e86992014-01-24 22:59:11492 bool file_ownership_passed,
[email protected]31bb5ee62012-09-12 22:58:40493 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12494 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54495 if (browser_terminating_) {
496 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
497 // Leak the temp file at extension_path. We don't want to add to the disk
498 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
499 // the file is in the OS temp directory which should be cleaned up for us.
500 return false;
501 }
[email protected]a8af9fdb2010-10-28 21:52:20502
[email protected]3f213ad2012-07-26 23:39:41503 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06504 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32505
[email protected]695b5712012-12-06 23:55:28506 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06507 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33508 LOG(WARNING) << "Will not update extension " << id
509 << " because it is not installed or pending";
510 // Delete extension_path since we're not creating a CrxInstaller
511 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14512 if (!GetFileTaskRunner()->PostTask(
513 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36514 base::Bind(
[email protected]85df9d12014-04-15 17:02:14515 &extensions::file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36516 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03517
518 return false;
[email protected]e957fe52009-06-23 16:51:05519 }
520
[email protected]aa142702010-03-26 01:26:33521 // We want a silent install only for non-pending extensions and
522 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37523 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05524 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37525 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33526
[email protected]f8636f92013-08-09 21:02:37527 scoped_refptr<CrxInstaller> installer(
528 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16529 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48530 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06531 if (pending_extension_info) {
532 installer->set_install_source(pending_extension_info->install_source());
533 if (pending_extension_info->install_silently())
534 installer->set_allow_silent_install(true);
[email protected]21db9ef2014-05-16 02:06:27535 if (pending_extension_info->remote_install())
536 installer->set_grant_permissions(false);
[email protected]464213a2013-10-15 01:06:48537 creation_flags = pending_extension_info->creation_flags();
538 if (pending_extension_info->mark_acknowledged())
[email protected]374ceb6f2014-07-02 19:25:34539 external_install_manager_->AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06540 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41541 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06542 }
[email protected]7d8b7072012-04-07 01:07:46543 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45544 // its auto-update URL is from the webstore, treat it as a webstore install.
545 // Note that we ignore some older extensions with blank auto-update URLs
546 // because we are mostly concerned with restrictions on NaCl extensions,
547 // which are newer.
[email protected]75764512011-12-19 19:54:28548 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38549 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35550 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06551 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53552 creation_flags |= Extension::FROM_WEBSTORE;
553 }
554
555 // Bookmark apps being updated is kind of a contradiction, but that's because
556 // we mark the default apps as bookmark apps, and they're hosted in the web
557 // store, thus they can get updated. See http://crbug.com/101605 for more
558 // details.
559 if (extension && extension->from_bookmark())
560 creation_flags |= Extension::FROM_BOOKMARK;
561
[email protected]e33bbc22012-08-27 22:05:46562 if (extension && extension->was_installed_by_default())
563 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
564
[email protected]acc3c7c22014-03-19 06:23:39565 if (extension && extension->was_installed_by_oem())
566 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
567
[email protected]6338fa32014-07-16 21:41:59568 if (extension && extension->was_installed_by_custodian())
569 creation_flags |= Extension::WAS_INSTALLED_BY_CUSTODIAN;
570
[email protected]658eae52014-06-14 20:28:05571 if (extension) {
[email protected]411f8ae2014-05-22 11:12:23572 installer->set_is_ephemeral(extension_prefs_->IsEphemeralApp(id));
[email protected]c30bda262014-06-19 04:10:13573 installer->set_do_not_sync(extension_prefs_->DoNotSync(id));
[email protected]658eae52014-06-14 20:28:05574 }
[email protected]ab6c7be42014-01-16 02:05:54575
[email protected]a12ce8b22012-01-17 18:40:53576 installer->set_creation_flags(creation_flags);
577
[email protected]044e86992014-01-24 22:59:11578 installer->set_delete_source(file_ownership_passed);
[email protected]cb0e50312011-05-09 15:03:07579 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16580 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03581
582 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09583 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03584
585 return true;
[email protected]e957fe52009-06-23 16:51:05586}
587
[email protected]130e92a2014-08-15 08:37:27588scoped_ptr<ExtensionDownloader> ExtensionService::CreateExtensionDownloader(
589 ExtensionDownloaderDelegate* delegate) {
590 scoped_ptr<ExtensionDownloader> downloader;
[email protected]130e92a2014-08-15 08:37:27591 scoped_ptr<IdentityProvider> identity_provider =
592 CreateWebstoreIdentityProvider(profile_);
593 downloader.reset(new ExtensionDownloader(
594 delegate,
595 profile_->GetRequestContext()));
596 downloader->SetWebstoreIdentityProvider(identity_provider.Pass());
[email protected]130e92a2014-08-15 08:37:27597 return downloader.Pass();
598}
599
[email protected]bca4b832014-07-17 20:22:34600void ExtensionService::ReloadExtensionImpl(
[email protected]757d60a2014-05-23 00:11:44601 // "transient" because the process of reloading may cause the reference
602 // to become invalid. Instead, use |extension_id|, a copy.
[email protected]bca4b832014-07-17 20:22:34603 const std::string& transient_extension_id,
604 bool be_noisy) {
[email protected]a29a517a2011-01-21 21:11:12605 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05606
607 // If the extension is already reloading, don't reload again.
[email protected]757d60a2014-05-23 00:11:44608 if (extension_prefs_->GetDisableReasons(transient_extension_id) &
[email protected]2a947202013-03-06 04:58:05609 Extension::DISABLE_RELOAD) {
610 return;
611 }
612
[email protected]757d60a2014-05-23 00:11:44613 // Ignore attempts to reload a blacklisted extension. Sometimes this can
614 // happen in a convoluted reload sequence triggered by the termination of a
615 // blacklisted extension and a naive attempt to reload it. For an example see
616 // http://crbug.com/373842.
617 if (registry_->blacklisted_extensions().Contains(transient_extension_id))
618 return;
619
[email protected]650b2d52013-02-10 03:41:45620 base::FilePath path;
[email protected]757d60a2014-05-23 00:11:44621
622 std::string extension_id = transient_extension_id;
623 const Extension* transient_current_extension =
624 GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40625
[email protected]f17dbd42010-08-16 23:21:10626 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]757d60a2014-05-23 00:11:44627 if (transient_current_extension) {
[email protected]4814b512009-11-07 00:12:29628 // If the extension has an inspector open for its background page, detach
629 // the inspector and hang onto a cookie for it, so that we can reattach
630 // later.
[email protected]31d8f5f22012-04-02 15:22:08631 // TODO(yoz): this is not incognito-safe!
[email protected]98b6d942013-11-10 00:34:07632 extensions::ProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13633 extensions::ExtensionHost* host =
634 manager->GetBackgroundHostForExtension(extension_id);
[email protected]b3f957e62014-08-08 10:09:02635 if (host && DevToolsAgentHost::HasFor(host->host_contents())) {
[email protected]4814b512009-11-07 00:12:29636 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09637 scoped_refptr<DevToolsAgentHost> agent_host =
[email protected]b3f957e62014-08-08 10:09:02638 DevToolsAgentHost::GetOrCreateFor(host->host_contents());
639 agent_host->DisconnectWebContents();
[email protected]04ea1bb2013-07-10 09:26:09640 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29641 }
642
[email protected]757d60a2014-05-23 00:11:44643 path = transient_current_extension->path();
[email protected]58076192013-07-19 19:43:50644 // BeingUpgraded is set back to false when the extension is added.
[email protected]757d60a2014-05-23 00:11:44645 system_->runtime_data()->SetBeingUpgraded(transient_current_extension,
646 true);
[email protected]44d62b62012-04-11 00:06:03647 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03648 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16649 } else {
[email protected]d32c02a2014-06-16 22:58:36650 std::map<std::string, base::FilePath>::const_iterator iter =
651 unloaded_extension_paths_.find(extension_id);
652 if (iter == unloaded_extension_paths_.end()) {
653 return;
654 }
[email protected]1eb175082010-02-10 09:26:16655 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06656 }
657
[email protected]757d60a2014-05-23 00:11:44658 transient_current_extension = NULL;
659
[email protected]9f4e4f082013-06-21 07:11:19660 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48661 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05662 return;
663 }
664
[email protected]43ceb002012-02-10 23:19:15665 // If we're reloading a component extension, use the component extension
666 // loader's reloader.
667 if (component_loader_->Exists(extension_id)) {
[email protected]43ceb002012-02-10 23:19:15668 component_loader_->Reload(extension_id);
[email protected]43ceb002012-02-10 23:19:15669 return;
670 }
671
[email protected]e6090e42010-03-23 22:44:08672 // Check the installed extensions to see if what we're reloading was already
673 // installed.
[email protected]e6090e42010-03-23 22:44:08674 scoped_ptr<ExtensionInfo> installed_extension(
675 extension_prefs_->GetInstalledExtensionInfo(extension_id));
676 if (installed_extension.get() &&
677 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01678 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08679 } else {
[email protected]d8c8f25f2011-11-02 18:18:01680 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08681 // We should always be able to remember the extension's path. If it's not in
682 // the map, someone failed to update |unloaded_extension_paths_|.
683 CHECK(!path.empty());
[email protected]bca4b832014-07-17 20:22:34684 scoped_refptr<extensions::UnpackedInstaller> unpacked_installer =
685 extensions::UnpackedInstaller::Create(this);
686 unpacked_installer->set_be_noisy_on_failure(be_noisy);
687 unpacked_installer->Load(path);
[email protected]e6090e42010-03-23 22:44:08688 }
[email protected]9cddd4702009-07-27 22:09:40689}
690
[email protected]bca4b832014-07-17 20:22:34691void ExtensionService::ReloadExtension(const std::string& extension_id) {
692 ReloadExtensionImpl(extension_id, true); // be_noisy
693}
694
695void ExtensionService::ReloadExtensionWithQuietFailure(
696 const std::string& extension_id) {
697 ReloadExtensionImpl(extension_id, false); // be_noisy
698}
699
[email protected]757d60a2014-05-23 00:11:44700bool ExtensionService::UninstallExtension(
701 // "transient" because the process of uninstalling may cause the reference
702 // to become invalid. Instead, use |extenson->id()|.
703 const std::string& transient_extension_id,
[email protected]e43c61f2014-07-20 21:46:34704 extensions::UninstallReason reason,
[email protected]42d58f62014-07-31 01:32:45705 const base::Closure& deletion_done_callback,
[email protected]757d60a2014-05-23 00:11:44706 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12707 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20708
[email protected]757d60a2014-05-23 00:11:44709 scoped_refptr<const Extension> extension =
710 GetInstalledExtension(transient_extension_id);
[email protected]631cf822009-05-15 07:01:25711
[email protected]e7afe2452010-08-22 16:19:13712 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09713 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32714
[email protected]95da88c42011-03-31 10:07:33715 // Policy change which triggers an uninstall will always set
716 // |external_uninstall| to true so this is the only way to uninstall
717 // managed extensions.
[email protected]90878c52014-04-04 18:21:02718 // Shared modules being uninstalled will also set |external_uninstall| to true
719 // so that we can guarantee users don't uninstall a shared module.
720 // (crbug.com/273300)
721 // TODO(rdevlin.cronin): This is probably not right. We should do something
722 // else, like include an enum IS_INTERNAL_UNINSTALL or IS_USER_UNINSTALL so
723 // we don't do this.
[email protected]cc2f55c2014-07-08 02:19:04724 bool external_uninstall =
[email protected]e43c61f2014-07-20 21:46:34725 (reason == extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT) ||
726 (reason == extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION) ||
[email protected]6ef60012014-07-22 19:08:12727 (reason == extensions::UNINSTALL_REASON_ORPHANED_SHARED_MODULE) ||
728 (reason == extensions::UNINSTALL_REASON_SYNC &&
729 extension->was_installed_by_custodian());
[email protected]65187152012-06-02 13:14:14730 if (!external_uninstall &&
731 !system_->management_policy()->UserMayModifySettings(
732 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07733 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06734 extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53735 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09736 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33737 return false;
738 }
[email protected]95da88c42011-03-31 10:07:33739
[email protected]65f173552012-06-28 22:43:58740 syncer::SyncChange sync_change;
[email protected]f8aefb132013-10-30 09:29:52741 if (extension_sync_service_) {
742 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
743 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30744 }
745
[email protected]d820bc52014-03-13 05:35:23746 system_->install_verifier()->Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:46747
[email protected]9b217652010-10-08 22:04:23748 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08749 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09750 RecordPermissionMessagesHistogram(extension.get(),
[email protected]4c86be582014-06-04 08:39:11751 "Extensions.Permissions_Uninstall2");
[email protected]9b217652010-10-08 22:04:23752
[email protected]831aa212010-03-26 13:55:19753 // Unload before doing more cleanup to ensure that nothing is hanging on to
754 // any of these resources.
[email protected]757d60a2014-05-23 00:11:44755 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19756
[email protected]9f1087e2009-06-15 17:29:32757 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05758 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14759 if (!GetFileTaskRunner()->PostTask(
760 FROM_HERE,
[email protected]5a145e82014-05-29 22:19:07761 base::Bind(&ExtensionService::UninstallExtensionOnFileThread,
762 extension->id(),
763 profile_,
[email protected]85df9d12014-04-15 17:02:14764 install_directory_,
[email protected]5a145e82014-05-29 22:19:07765 extension->path())))
[email protected]14908b72011-04-20 06:54:36766 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32767 }
768
[email protected]42d58f62014-07-31 01:32:45769 extensions::DataDeleter::StartDeleting(
770 profile_, extension.get(), deletion_done_callback);
[email protected]0d6ec3a72011-09-02 02:09:43771
[email protected]757d60a2014-05-23 00:11:44772 UntrackTerminatedExtension(extension->id());
[email protected]211030342010-09-30 18:41:06773
774 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07775 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06776 extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:53777 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09778 content::Details<const Extension>(extension.get()));
[email protected]e43c61f2014-07-20 21:46:34779 ExtensionRegistry::Get(profile_)
780 ->TriggerOnUninstalled(extension.get(), reason);
[email protected]d6ebc9792011-04-07 18:18:33781
[email protected]f8aefb132013-10-30 09:29:52782 if (extension_sync_service_) {
[email protected]757d60a2014-05-23 00:11:44783 extension_sync_service_->ProcessSyncUninstallExtension(extension->id(),
[email protected]f8aefb132013-10-30 09:29:52784 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30785 }
786
[email protected]757d60a2014-05-23 00:11:44787 delayed_installs_.Remove(extension->id());
[email protected]0db124b02012-11-07 04:55:05788
[email protected]757d60a2014-05-23 00:11:44789 extension_prefs_->OnExtensionUninstalled(
790 extension->id(), extension->location(), external_uninstall);
[email protected]d4eda592013-09-18 03:37:57791
[email protected]333b1de2011-09-12 18:28:50792 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18793 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
794
[email protected]d6ebc9792011-04-07 18:18:33795 return true;
[email protected]c10da4b02010-03-25 14:38:32796}
797
[email protected]5a145e82014-05-29 22:19:07798// static
799void ExtensionService::UninstallExtensionOnFileThread(
800 const std::string& id,
801 Profile* profile,
802 const base::FilePath& install_dir,
803 const base::FilePath& extension_path) {
804 extensions::ExtensionAssetsManager* assets_manager =
805 extensions::ExtensionAssetsManager::GetInstance();
806 assets_manager->UninstallExtension(id, profile, install_dir, extension_path);
807}
808
[email protected]c3cfb012011-04-06 22:07:35809bool ExtensionService::IsExtensionEnabled(
810 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14811 if (registry_->enabled_extensions().Contains(extension_id) ||
812 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18813 return true;
[email protected]dc9a74f72012-08-17 18:07:21814 }
[email protected]36429da2011-07-11 20:25:18815
[email protected]bb1bc9b32013-12-21 03:09:14816 if (registry_->disabled_extensions().Contains(extension_id) ||
817 registry_->blacklisted_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25818 return false;
[email protected]695b5712012-12-06 23:55:28819 }
[email protected]ad83ca242011-07-29 01:32:25820
821 // If the extension hasn't been loaded yet, check the prefs for it. Assume
822 // enabled unless otherwise noted.
823 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]27e528322014-05-27 20:54:30824 !extension_prefs_->IsExtensionBlacklisted(extension_id) &&
[email protected]f574c402012-12-04 23:20:31825 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35826}
827
[email protected]eaa7dd182010-12-14 11:09:00828void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12829 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20830
[email protected]06f92562011-04-29 19:27:31831 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39832 return;
[email protected]bb1bc9b32013-12-21 03:09:14833 const Extension* extension =
834 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36835
836 ManagementPolicy* policy = system_->management_policy();
837 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
838 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
839 return;
840 }
[email protected]0c6da502009-08-14 22:32:39841
[email protected]b3317ad2011-04-28 23:46:00842 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28843 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52844
[email protected]06f92562011-04-29 19:27:31845 // This can happen if sync enables an extension that is not
846 // installed yet.
847 if (!extension)
848 return;
849
[email protected]0c6da502009-08-14 22:32:39850 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14851 registry_->AddEnabled(make_scoped_refptr(extension));
852 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39853
[email protected]62d30f42009-10-01 22:36:06854 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30855
[email protected]f74d7f32012-06-19 19:22:51856 // Notify listeners that the extension was enabled.
857 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06858 extensions::NOTIFICATION_EXTENSION_ENABLED,
[email protected]f74d7f32012-06-19 19:22:51859 content::Source<Profile>(profile_),
860 content::Details<const Extension>(extension));
861
[email protected]f8aefb132013-10-30 09:29:52862 if (extension_sync_service_)
863 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:39864}
865
[email protected]44d62b62012-04-11 00:06:03866void ExtensionService::DisableExtension(
867 const std::string& extension_id,
868 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12869 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20870
[email protected]8f959f522014-08-06 06:26:28871 // The extension may have been disabled already. Just add a disable reason.
872 if (!IsExtensionEnabled(extension_id)) {
873 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52874 return;
[email protected]8f959f522014-08-06 06:26:28875 }
[email protected]1784e83a2009-09-08 21:01:52876
[email protected]06f92562011-04-29 19:27:31877 const Extension* extension = GetInstalledExtension(extension_id);
878 // |extension| can be NULL if sync disables an extension that is not
879 // installed yet.
[email protected]ab894542014-07-03 12:35:39880 // EXTERNAL_COMPONENT extensions are not generally modifiable by users, but
881 // can be uninstalled by the browser if the user sets extension-specific
882 // preferences.
[email protected]65187152012-06-02 13:14:14883 if (extension &&
[email protected]505ccdc2013-06-20 20:15:35884 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]ab894542014-07-03 12:35:39885 !system_->management_policy()->UserMayModifySettings(extension, NULL) &&
886 extension->location() != Manifest::EXTERNAL_COMPONENT) {
[email protected]95da88c42011-03-31 10:07:33887 return;
[email protected]65187152012-06-02 13:14:14888 }
[email protected]95da88c42011-03-31 10:07:33889
[email protected]b3317ad2011-04-28 23:46:00890 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28891 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52892
[email protected]599539802014-01-07 23:06:00893 int include_mask =
894 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
895 extension = registry_->GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31896 if (!extension)
897 return;
898
[email protected]45f5b7d2014-01-22 23:47:13899 // The extension is either enabled or terminated.
900 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
901 registry_->terminated_extensions().Contains(extension->id()));
[email protected]64742de32013-04-22 08:44:34902
[email protected]5c094792012-09-07 19:44:53903 // Move it over to the disabled list. Don't send a second unload notification
904 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:14905 registry_->AddDisabled(make_scoped_refptr(extension));
906 if (registry_->enabled_extensions().Contains(extension->id())) {
907 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:13908 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:53909 } else {
[email protected]bb1bc9b32013-12-21 03:09:14910 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:53911 }
[email protected]3bdba0d2011-08-23 07:17:30912
[email protected]f8aefb132013-10-30 09:29:52913 if (extension_sync_service_)
914 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:52915}
916
[email protected]1abf05e2013-07-09 17:04:36917void ExtensionService::DisableUserExtensions(
918 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14919 extensions::ManagementPolicy* management_policy =
920 system_->management_policy();
921 extensions::ExtensionList to_disable;
922
[email protected]bb1bc9b32013-12-21 03:09:14923 const ExtensionSet& enabled_set = registry_->enabled_extensions();
924 for (ExtensionSet::const_iterator extension = enabled_set.begin();
925 extension != enabled_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39926 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14927 to_disable.push_back(*extension);
928 }
[email protected]bb1bc9b32013-12-21 03:09:14929 const ExtensionSet& terminated_set = registry_->terminated_extensions();
930 for (ExtensionSet::const_iterator extension = terminated_set.begin();
931 extension != terminated_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39932 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14933 to_disable.push_back(*extension);
934 }
935
936 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
937 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:38938 if ((*extension)->was_installed_by_default() &&
939 extension_urls::IsWebstoreUpdateUrl(
dchengc7047942014-08-26 05:05:31940 extensions::ManifestURL::GetUpdateURL(extension->get())))
[email protected]3082fe32013-08-06 11:12:38941 continue;
[email protected]1abf05e2013-07-09 17:04:36942 const std::string& id = (*extension)->id();
943 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
944 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:14945 }
946}
947
[email protected]eaa7dd182010-12-14 11:09:00948void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:28949 const Extension* extension) {
950 GrantPermissions(extension);
[email protected]4c86be582014-06-04 08:39:11951 RecordPermissionMessagesHistogram(extension,
952 "Extensions.Permissions_ReEnable2");
[email protected]8d888c12010-11-30 00:00:25953 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
954 EnableExtension(extension->id());
955}
956
[email protected]009633c2013-03-07 22:08:28957void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:45958 CHECK(extension);
[email protected]8d42db52014-06-20 21:50:50959 extensions::PermissionsUpdater(profile()).GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:45960}
961
[email protected]fe2dd7742011-04-19 22:52:49962// static
963void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:05964 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:17965 // Since this is called from multiple sources, and since the histogram macros
966 // use statics, we need to manually lookup the histogram ourselves.
967 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:49968 histogram,
969 1,
[email protected]c2e66e12012-06-27 06:27:06970 PermissionMessage::kEnumBoundary,
971 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:17972 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:49973
[email protected]a6910e72014-06-06 05:04:36974 PermissionMessages permissions =
975 extension->permissions_data()->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49976 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:06977 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49978 } else {
[email protected]c2e66e12012-06-27 06:27:06979 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:52980 it != permissions.end(); ++it)
981 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49982 }
983}
984
[email protected]eaa7dd182010-12-14 11:09:00985void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]f9c8c7c72014-07-31 16:42:31986 // The URLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06987 // was loaded, otherwise a race can arise where a renderer that is created
988 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42989 // that the request context doesn't yet know about. The profile is responsible
990 // for ensuring its URLRequestContexts appropriately discover the loaded
991 // extension.
[email protected]31d8f5f22012-04-02 15:22:08992 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06993
[email protected]d5949312012-12-03 22:13:30994 // Tell renderers about the new extension, unless it's a theme (renderers
995 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:19996 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:49997 for (content::RenderProcessHost::iterator i(
998 content::RenderProcessHost::AllHostsIterator());
999 !i.IsAtEnd(); i.Advance()) {
1000 content::RenderProcessHost* host = i.GetCurrentValue();
1001 Profile* host_profile =
1002 Profile::FromBrowserContext(host->GetBrowserContext());
1003 if (host_profile->GetOriginalProfile() ==
1004 profile_->GetOriginalProfile()) {
1005 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1006 1, ExtensionMsg_Loaded_Params(extension));
1007 host->Send(
1008 new ExtensionMsg_Loaded(loaded_extensions));
1009 }
[email protected]c8d407e2011-04-28 21:27:171010 }
[email protected]77a6970c2011-04-23 16:58:561011 }
1012
[email protected]3442a662012-01-12 08:06:171013 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1014 // extension.
1015 //
1016 // NOTE: It is important that this happen after notifying the renderers about
1017 // the new extensions so that if we navigate to an extension URL in
[email protected]fdc76192014-04-20 21:54:411018 // ExtensionRegistryObserver::OnLoaded or
1019 // NOTIFICATION_EXTENSION_LOADED_DEPRECATED, the
[email protected]dcc47642014-03-26 22:03:491020 // renderer is guaranteed to know about it.
1021 registry_->TriggerOnLoaded(extension);
1022
[email protected]3442a662012-01-12 08:06:171023 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061024 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]3442a662012-01-12 08:06:171025 content::Source<Profile>(profile_),
1026 content::Details<const Extension>(extension));
1027
[email protected]dcc47642014-03-26 22:03:491028 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1029 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171030 profile_->GetExtensionSpecialStoragePolicy()->
1031 GrantRightsForExtension(extension);
1032
[email protected]dcc47642014-03-26 22:03:491033 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1034 // work properly multi-profile. Besides which, it should be using
1035 // ExtensionRegistryObserver. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171036 UpdateActiveExtensionsInCrashReporter();
1037
[email protected]d695bb12014-06-05 16:16:301038 const extensions::PermissionsData* permissions_data =
[email protected]a6910e72014-06-06 05:04:361039 extension->permissions_data();
[email protected]d695bb12014-06-05 16:16:301040
[email protected]c8d407e2011-04-28 21:27:171041 // If the extension has permission to load chrome://favicon/ resources we need
1042 // to make sure that the FaviconSource is registered with the
1043 // ChromeURLDataManager.
[email protected]d695bb12014-06-05 16:16:301044 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171045 FaviconSource* favicon_source = new FaviconSource(profile_,
1046 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531047 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171048 }
[email protected]b07e606e2012-09-15 20:16:151049
[email protected]b07e606e2012-09-15 20:16:151050 // Same for chrome://theme/ resources.
[email protected]d695bb12014-06-05 16:16:301051 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151052 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531053 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151054 }
[email protected]b07e606e2012-09-15 20:16:151055
[email protected]5eddc3e2011-10-26 04:33:311056 // Same for chrome://thumb/ resources.
[email protected]d695bb12014-06-05 16:16:301057 if (permissions_data->HasHostPermission(
1058 GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511059 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531060 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311061 }
[email protected]62d30f42009-10-01 22:36:061062}
1063
[email protected]a9f39a312010-12-23 22:14:271064void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591065 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131066 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271067 UnloadedExtensionInfo details(extension, reason);
[email protected]e51232f32014-04-18 20:05:361068
1069 registry_->TriggerOnUnloaded(extension, reason);
1070
[email protected]ad50def52011-10-19 23:17:071071 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061072 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:531073 content::Source<Profile>(profile_),
1074 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061075
[email protected]f3b1a082011-11-18 00:34:301076 for (content::RenderProcessHost::iterator i(
1077 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561078 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301079 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081080 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301081 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081082 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171083 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561084 }
1085
[email protected]31d8f5f22012-04-02 15:22:081086 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]dcc47642014-03-26 22:03:491087
1088 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1089 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171090 profile_->GetExtensionSpecialStoragePolicy()->
1091 RevokeRightsForExtension(extension);
1092
[email protected]b777b332011-04-16 04:01:081093#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231094 // Revoke external file access for the extension from its file system context.
1095 // It is safe to access the extension's storage partition at this point. The
1096 // storage partition may get destroyed only after the extension gets unloaded.
[email protected]3a746ec2014-03-15 05:30:561097 GURL site =
1098 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
[email protected]cd501a72014-08-22 19:58:311099 storage::FileSystemContext* filesystem_context =
1100 BrowserContext::GetStoragePartitionForSite(profile_, site)
1101 ->GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321102 if (filesystem_context && filesystem_context->external_backend()) {
1103 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171104 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061105 }
[email protected]c8d407e2011-04-28 21:27:171106#endif
1107
[email protected]dcc47642014-03-26 22:03:491108 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1109 // work properly multi-profile. Besides which, it should be using
1110 // ExtensionRegistryObserver::OnExtensionLoaded. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171111 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061112}
1113
[email protected]599539802014-01-07 23:06:001114content::BrowserContext* ExtensionService::GetBrowserContext() const {
1115 // Implemented in the .cc file to avoid adding a profile.h dependency to
1116 // extension_service.h.
1117 return profile_;
1118}
1119
[email protected]25ae0152011-11-18 14:40:021120bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351121 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021122}
1123
[email protected]7f8f24f2012-11-15 19:40:141124base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091125 if (file_task_runner_.get())
1126 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141127
1128 // We should be able to interrupt any part of extension install process during
1129 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1130 // will be ignored/deleted while we will block on started tasks.
1131 std::string token("ext_install-");
1132 token.append(profile_->GetPath().AsUTF8Unsafe());
1133 file_task_runner_ = BrowserThread::GetBlockingPool()->
1134 GetSequencedTaskRunnerWithShutdownBehavior(
1135 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1136 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091137 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141138}
1139
[email protected]4ee07c62012-08-21 12:40:421140void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361141 std::vector<std::string> to_unload;
1142 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281143
[email protected]0d54b682013-11-05 14:15:361144 // Loop through the extensions list, finding extensions we need to unload or
1145 // disable.
[email protected]bb1bc9b32013-12-21 03:09:141146 const ExtensionSet& extensions = registry_->enabled_extensions();
1147 for (ExtensionSet::const_iterator iter = extensions.begin();
1148 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361149 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421150 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361151 to_unload.push_back(extension->id());
1152 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1153 if (system_->management_policy()->MustRemainDisabled(
1154 extension, &disable_reason, NULL))
1155 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251156 }
1157
[email protected]0d54b682013-11-05 14:15:361158 for (size_t i = 0; i < to_unload.size(); ++i)
1159 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1160
1161 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1162 to_disable.begin(); i != to_disable.end(); ++i)
1163 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251164}
1165
[email protected]31206602011-04-13 23:07:321166void ExtensionService::CheckForUpdatesSoon() {
[email protected]90878c52014-04-04 18:21:021167 // This can legitimately happen in unit tests.
1168 if (!updater_.get())
1169 return;
1170
1171 if (AreAllExternalProvidersReady()) {
1172 updater_->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351173 } else {
[email protected]90878c52014-04-04 18:21:021174 // Sync can start updating before all the external providers are ready
1175 // during startup. Start the update as soon as those providers are ready,
1176 // but not before.
1177 update_once_all_providers_are_ready_ = true;
[email protected]c3cfb012011-04-06 22:07:351178 }
[email protected]c3cfb012011-04-06 22:07:351179}
1180
[email protected]8e4560b62011-01-14 10:09:141181// Some extensions will autoupdate themselves externally from Chrome. These
1182// are typically part of some larger client application package. To support
1183// these, the extension will register its location in the the preferences file
1184// (and also, on Windows, in the registry) and this code will periodically
1185// check that location for a .crx file, which it will then install locally if
1186// a new version is available.
1187// Errors are reported through ExtensionErrorReporter. Succcess is not
1188// reported.
[email protected]eaa7dd182010-12-14 11:09:001189void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121190 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141191
1192 // Note that this installation is intentionally silent (since it didn't
1193 // go through the front-end). Extensions that are registered in this
1194 // way are effectively considered 'pre-bundled', and so implicitly
1195 // trusted. In general, if something has HKLM or filesystem access,
1196 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121197
[email protected]8e4560b62011-01-14 10:09:141198 // Ask each external extension provider to give us a call back for each
1199 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271200 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141201 for (i = external_extension_providers_.begin();
1202 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271203 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141204 provider->VisitRegisteredExtension();
1205 }
1206
[email protected]50067e52011-10-20 23:17:071207 // Do any required work that we would have done after completion of all
1208 // providers.
[email protected]373daf972014-04-10 01:50:441209 if (external_extension_providers_.empty())
[email protected]50067e52011-10-20 23:17:071210 OnAllExternalProvidersReady();
[email protected]9f1087e2009-06-15 17:29:321211}
1212
[email protected]50067e52011-10-20 23:17:071213void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271214 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121215 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071216 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121217
1218 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141219 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301220 if (AreAllExternalProvidersReady())
1221 OnAllExternalProvidersReady();
1222}
1223
1224bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271225 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141226 for (i = external_extension_providers_.begin();
1227 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301228 if (!i->get()->IsReady())
1229 return false;
[email protected]8e4560b62011-01-14 10:09:141230 }
[email protected]94fde232012-04-27 10:22:301231 return true;
[email protected]50067e52011-10-20 23:17:071232}
1233
1234void ExtensionService::OnAllExternalProvidersReady() {
1235 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301236 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1237 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071238
1239 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301240 if (update_once_all_providers_are_ready_ && updater()) {
1241 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141242 extensions::ExtensionUpdater::CheckParams params;
1243 params.callback = external_updates_finished_callback_;
1244 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141245 }
1246
1247 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231248 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141249 extension_prefs_->GetInstalledExtensionsInfo());
1250 for (size_t i = 0; i < extensions_info->size(); ++i) {
1251 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401252 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141253 CheckExternalUninstall(info->extension_id);
1254 }
[email protected]e5af875f2011-10-10 21:09:141255
[email protected]373daf972014-04-10 01:50:441256 error_controller_->ShowErrorIfNeeded();
[email protected]a9aa5932012-01-25 08:27:401257
[email protected]374ceb6f2014-07-02 19:25:341258 external_install_manager_->UpdateExternalExtensionAlert();
[email protected]e5af875f2011-10-10 21:09:141259}
1260
[email protected]a9f39a312010-12-23 22:14:271261void ExtensionService::UnloadExtension(
1262 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131263 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091264 // Make sure the extension gets deleted after we return from this function.
[email protected]599539802014-01-07 23:06:001265 int include_mask =
1266 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
[email protected]9adb9692010-10-29 23:14:021267 scoped_refptr<const Extension> extension(
[email protected]599539802014-01-07 23:06:001268 registry_->GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251269
[email protected]fa0c96732010-11-17 00:14:231270 // This method can be called via PostTask, so the extension may have been
1271 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091272 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171273 // In case the extension may have crashed/uninstalled. Allow the profile to
1274 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081275 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231276 return;
[email protected]dd163fb02011-05-04 22:22:171277 }
[email protected]0c6da502009-08-14 22:32:391278
[email protected]1eb175082010-02-10 09:26:161279 // Keep information about the extension so that we can reload it later
1280 // even if it's not permanently installed.
1281 unloaded_extension_paths_[extension->id()] = extension->path();
1282
[email protected]f17dbd42010-08-16 23:21:101283 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031284 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101285
[email protected]bb1bc9b32013-12-21 03:09:141286 if (registry_->disabled_extensions().Contains(extension->id())) {
1287 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171288 // Make sure the profile cleans up its RequestContexts when an already
1289 // disabled extension is unloaded (since they are also tracking the disabled
1290 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081291 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]45f5b7d2014-01-22 23:47:131292 // Don't send the unloaded notification. It was sent when the extension
1293 // was disabled.
[email protected]60a174a2013-08-02 20:29:281294 } else {
[email protected]bb1bc9b32013-12-21 03:09:141295 // Remove the extension from the enabled list.
1296 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281297 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391298 }
1299
[email protected]fb789532013-08-27 02:40:211300 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061301 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:211302 content::Source<Profile>(profile_),
1303 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251304}
1305
[email protected]8b1ec202013-09-05 02:09:501306void ExtensionService::RemoveComponentExtension(
1307 const std::string& extension_id) {
1308 scoped_refptr<const Extension> extension(
1309 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131310 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]ebe07772014-05-22 04:16:061311 if (extension.get()) {
1312 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061313 extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
[email protected]ebe07772014-05-22 04:16:061314 content::Source<Profile>(profile_),
1315 content::Details<const Extension>(extension.get()));
[email protected]e43c61f2014-07-20 21:46:341316 ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(
1317 extension.get(), extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT);
[email protected]ebe07772014-05-22 04:16:061318 }
[email protected]8b1ec202013-09-05 02:09:501319}
1320
[email protected]bb1bc9b32013-12-21 03:09:141321void ExtensionService::UnloadAllExtensionsForTest() {
1322 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321323}
1324
[email protected]bb1bc9b32013-12-21 03:09:141325void ExtensionService::ReloadExtensionsForTest() {
1326 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1327 // warning about calling test code in production.
1328 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451329 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011330 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351331 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1332 // times.
[email protected]9f1087e2009-06-15 17:29:321333}
1334
[email protected]820d9bd2013-04-03 03:46:031335void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351336 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031337 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061338 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
[email protected]820d9bd2013-04-03 03:46:031339 content::Source<Profile>(profile_),
1340 content::NotificationService::NoDetails());
1341}
1342
[email protected]eaa7dd182010-12-14 11:09:001343void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111344 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171345 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501346
[email protected]bc151cf92013-02-12 04:57:261347 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511348}
1349
[email protected]fcb58a862012-05-01 01:03:151350void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171351 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1352 // default extension state to DISABLED when the --disable-extensions flag
1353 // is set (http://crbug.com/29067).
1354 if (!extensions_enabled() &&
1355 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401356 extension->location() != Manifest::COMPONENT &&
1357 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151358 return;
[email protected]330840c2012-08-29 22:21:011359 }
[email protected]c8d407e2011-04-28 21:27:171360
[email protected]b914e2952013-04-26 07:10:031361 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171362 bool is_extension_installed = false;
1363 const Extension* old = GetInstalledExtension(extension->id());
1364 if (old) {
1365 is_extension_installed = true;
1366 int version_compare_result =
1367 extension->version()->CompareTo(*(old->version()));
1368 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031369 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1370 // that we aren't downgrading.
1371 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171372 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031373 }
[email protected]45f5b7d2014-01-22 23:47:131374 system_->runtime_data()->SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171375
[email protected]1eb175082010-02-10 09:26:161376 // The extension is now loaded, remove its data from unloaded extension map.
1377 unloaded_extension_paths_.erase(extension->id());
1378
[email protected]bb7f40952011-01-13 00:21:201379 // If a terminated extension is loaded, remove it from the terminated list.
1380 UntrackTerminatedExtension(extension->id());
1381
[email protected]f17dbd42010-08-16 23:21:101382 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411383 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101384
[email protected]b914e2952013-04-26 07:10:031385 // Check if the extension's privileges have changed and mark the
1386 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171387 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031388
[email protected]116d40e2013-08-08 17:23:171389 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411390 // To upgrade an extension in place, unload the old one and then load the
1391 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131392 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031393 }
[email protected]0c6da502009-08-14 22:32:391394
[email protected]695b5712012-12-06 23:55:281395 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091396 // Only prefs is checked for the blacklist. We rely on callers to check the
1397 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071398 // installation then threads through the install and pending install flow
1399 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141400 registry_->AddBlacklisted(extension);
[email protected]ca2e82022013-04-29 16:48:411401 } else if (!reloading &&
1402 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141403 registry_->AddDisabled(extension);
[email protected]f8aefb132013-10-30 09:29:521404 if (extension_sync_service_)
1405 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031406 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061407 extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]44d62b62012-04-11 00:06:031408 content::Source<Profile>(profile_),
1409 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441410
[email protected]21db9ef2014-05-16 02:06:271411 // Show the extension disabled error if a permissions increase or a remote
1412 // installation is the reason it was disabled, and no other reasons exist.
1413 int reasons = extension_prefs_->GetDisableReasons(extension->id());
1414 const int kReasonMask = Extension::DISABLE_PERMISSIONS_INCREASE |
1415 Extension::DISABLE_REMOTE_INSTALL;
1416 if (reasons & kReasonMask && !(reasons & ~kReasonMask)) {
1417 extensions::AddExtensionDisabledError(
1418 this,
1419 extension,
1420 extension_prefs_->HasDisableReason(
1421 extension->id(), Extension::DISABLE_REMOTE_INSTALL));
[email protected]62f051c2012-03-29 17:04:441422 }
[email protected]ca2e82022013-04-29 16:48:411423 } else if (reloading) {
1424 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141425 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411426 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281427 } else {
1428 // All apps that are displayed in the launcher are ordered by their ordinals
1429 // so we must ensure they have valid ordinals.
1430 if (extension->RequiresSortOrdinal()) {
[email protected]db378322014-07-21 23:19:341431 extension_prefs_->app_sorting()->SetExtensionVisible(
1432 extension->id(),
1433 extension->ShouldDisplayInNewTabPage() &&
1434 !extension_prefs_->IsEphemeralApp(extension->id()));
1435 if (!extension_prefs_->IsEphemeralApp(extension->id())) {
1436 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1437 extension->id(), syncer::StringOrdinal());
[email protected]bd36b652013-02-27 02:13:251438 }
[email protected]695b5712012-12-06 23:55:281439 }
1440
[email protected]bb1bc9b32013-12-21 03:09:141441 registry_->AddEnabled(extension);
[email protected]f8aefb132013-10-30 09:29:521442 if (extension_sync_service_)
1443 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281444 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311445 }
[email protected]45f5b7d2014-01-22 23:47:131446 system_->runtime_data()->SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:351447}
1448
[email protected]8c484b742012-11-29 06:05:361449void ExtensionService::AddComponentExtension(const Extension* extension) {
1450 const std::string old_version_string(
1451 extension_prefs_->GetVersionString(extension->id()));
[email protected]c5e4a2222014-01-03 16:06:131452 const Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361453
[email protected]0181a7d2013-11-12 01:08:421454 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361455 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1456 VLOG(1) << "Component extension " << extension->name() << " ("
1457 << extension->id() << ") installing/upgrading from '"
1458 << old_version_string << "' to " << extension->version()->GetString();
1459
1460 AddNewOrUpdatedExtension(extension,
[email protected]9ae73962014-08-14 16:53:411461 Extension::ENABLED,
[email protected]4a1d9c0d2014-06-13 12:50:111462 extensions::kInstallFlagNone,
[email protected]d8fd0fd2014-03-24 13:16:061463 syncer::StringOrdinal(),
1464 std::string());
[email protected]8c484b742012-11-29 06:05:361465 return;
1466 }
1467
1468 AddExtension(extension);
1469}
1470
[email protected]b914e2952013-04-26 07:10:031471void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171472 bool is_extension_installed) {
[email protected]23a85362014-07-07 23:26:191473 extensions::PermissionsUpdater(profile_).InitializePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311474
[email protected]8d888c12010-11-30 00:00:251475 // We keep track of all permissions the user has granted each extension.
1476 // This allows extensions to gracefully support backwards compatibility
1477 // by including unknown permissions in their manifests. When the user
1478 // installs the extension, only the recognized permissions are recorded.
1479 // When the unknown permissions become recognized (e.g., through browser
1480 // upgrade), we can prompt the user to accept these new permissions.
1481 // Extensions can also silently upgrade to less permissions, and then
1482 // silently upgrade to a version that adds these permissions back.
1483 //
1484 // For example, pretend that Chrome 10 includes a permission "omnibox"
1485 // for an API that adds suggestions to the omnibox. An extension can
1486 // maintain backwards compatibility while still having "omnibox" in the
1487 // manifest. If a user installs the extension on Chrome 9, the browser
1488 // will record the permissions it recognized, not including "omnibox."
1489 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1490 // will disable the extension and prompt the user to approve the increase
1491 // in privileges. The extension could then release a new version that
1492 // removes the "omnibox" permission. When the user upgrades, Chrome will
1493 // still remember that "omnibox" had been granted, so that if the
1494 // extension once again includes "omnibox" in an upgrade, the extension
1495 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281496 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251497
[email protected]211a3f32013-05-28 21:48:181498 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361499 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]45928b682013-11-28 08:20:271500 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031501 // Silently grant all active permissions to default apps only on install.
1502 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181503 // Silently grant all active permissions to apps install in kiosk mode on both
1504 // install and update.
1505 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031506 GrantPermissions(extension);
1507
1508 bool is_privilege_increase = false;
1509 // We only need to compare the granted permissions to the current permissions
1510 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051511 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181512 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251513 // Add all the recognized permissions if the granted permissions list
1514 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061515 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311516 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521517 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251518
1519 // Here, we check if an extension's privileges have increased in a manner
1520 // that requires the user's approval. This could occur because the browser
1521 // upgraded and recognized additional privileges, or an extension upgrades
1522 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251523 is_privilege_increase =
1524 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
dchengc7047942014-08-26 05:05:311525 granted_permissions.get(),
[email protected]076ebeda2014-06-06 21:47:261526 extension->permissions_data()->active_permissions().get(),
1527 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251528 }
1529
[email protected]116d40e2013-08-08 17:23:171530 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031531 // If the extension was already disabled, suppress any alerts for becoming
1532 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031533 bool previously_disabled =
1534 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191535 // Legacy disabled extensions do not have a disable reason. Infer that if
1536 // there was no permission increase, it was likely disabled by the user.
1537 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031538 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191539 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031540 }
[email protected]7e9e2422012-12-13 19:54:191541 // Extensions that came to us disabled from sync need a similar inference,
1542 // except based on the new version's permissions.
1543 if (previously_disabled &&
1544 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1545 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1546 extension_prefs_->ClearDisableReasons(extension->id());
1547 if (!is_privilege_increase)
1548 disable_reasons |= Extension::DISABLE_USER_ACTION;
1549 }
[email protected]b914e2952013-04-26 07:10:031550 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251551 }
1552
1553 // Extension has changed permissions significantly. Disable it. A
[email protected]21db9ef2014-05-16 02:06:271554 // notification should be sent by the caller. If the extension is already
1555 // disabled because it was installed remotely, don't add another disable
1556 // reason, but instead always set the "did escalate permissions" flag, to
1557 // ensure enabling it will always show a warning.
1558 if (disable_reasons == Extension::DISABLE_REMOTE_INSTALL) {
1559 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1560 } else if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191561 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491562 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]4c86be582014-06-04 08:39:111563 RecordPermissionMessagesHistogram(extension,
1564 "Extensions.Permissions_AutoDisable2");
[email protected]fe2dd7742011-04-19 22:52:491565 }
[email protected]195c1582013-10-29 18:42:231566 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251567 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031568 }
1569 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281570 extension_prefs_->AddDisableReason(
1571 extension->id(),
1572 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251573 }
[email protected]8d888c12010-11-30 00:00:251574}
1575
[email protected]eaa7dd182010-12-14 11:09:001576void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391577 std::set<std::string> extension_ids;
[email protected]bb1bc9b32013-12-21 03:09:141578 const ExtensionSet& extensions = registry_->enabled_extensions();
1579 for (ExtensionSet::const_iterator iter = extensions.begin();
1580 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361581 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401582 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461583 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351584 }
1585
[email protected]dcc47642014-03-26 22:03:491586 // TODO(kalman): This is broken. ExtensionService is per-profile.
1587 // crash_keys::SetActiveExtensions is per-process. See
1588 // http://crbug.com/355029.
[email protected]f6431be82013-09-07 02:53:451589 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251590}
1591
[email protected]8266d662011-07-12 21:53:261592void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:501593 const Extension* extension,
[email protected]98270432012-09-11 20:51:241594 const syncer::StringOrdinal& page_ordinal,
[email protected]4a1d9c0d2014-06-13 12:50:111595 int install_flags) {
[email protected]a29a517a2011-01-21 21:11:121596 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201597
[email protected]c3cfb012011-04-06 22:07:351598 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:031599 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]d8fd0fd2014-03-24 13:16:061600 std::string install_parameter;
[email protected]96aebe22014-07-16 04:07:511601 const extensions::PendingExtensionInfo* pending_extension_info =
1602 pending_extension_manager()->GetById(id);
1603 if (pending_extension_info) {
[email protected]8f3bcbd2013-06-05 08:42:401604 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:061605 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321606
[email protected]31bb5ee62012-09-12 22:58:401607 LOG(WARNING) << "ShouldAllowInstall() returned false for "
1608 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:221609 << " and update URL "
1610 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:401611 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541612
[email protected]4416c5a2010-06-26 01:28:571613 // Delete the extension directory since we're not going to
1614 // load it.
[email protected]7f8f24f2012-11-15 19:40:141615 if (!GetFileTaskRunner()->PostTask(
1616 FROM_HERE,
[email protected]85df9d12014-04-15 17:02:141617 base::Bind(&extensions::file_util::DeleteFile,
1618 extension->path(),
1619 true))) {
[email protected]14908b72011-04-20 06:54:361620 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401621 }
[email protected]4416c5a2010-06-26 01:28:571622 return;
1623 }
[email protected]51a3bf8b2012-06-08 22:53:061624
[email protected]d8fd0fd2014-03-24 13:16:061625 install_parameter = pending_extension_info->install_parameter();
[email protected]51a3bf8b2012-06-08 22:53:061626 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:571627 } else {
[email protected]c3cfb012011-04-06 22:07:351628 // We explicitly want to re-enable an uninstalled external
1629 // extension; if we're here, that means the user is manually
1630 // installing the extension.
[email protected]46f3e102014-03-25 01:22:451631 if (extension_prefs_->IsExternalExtensionUninstalled(id)) {
[email protected]6cc7dbae2011-04-29 21:18:331632 initial_enable = true;
1633 }
[email protected]aa142702010-03-26 01:26:331634 }
1635
[email protected]98270432012-09-11 20:51:241636 // Unsupported requirements overrides the management policy.
[email protected]4a1d9c0d2014-06-13 12:50:111637 if (install_flags & extensions::kInstallFlagHasRequirementErrors) {
[email protected]98270432012-09-11 20:51:241638 initial_enable = false;
1639 extension_prefs_->AddDisableReason(
1640 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
1641 // If the extension was disabled because of unsupported requirements but
1642 // now supports all requirements after an update and there are not other
1643 // disable reasons, enable it.
1644 } else if (extension_prefs_->GetDisableReasons(id) ==
1645 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
1646 initial_enable = true;
1647 extension_prefs_->ClearDisableReasons(id);
1648 }
1649
[email protected]4a1d9c0d2014-06-13 12:50:111650 if (install_flags & extensions::kInstallFlagIsBlacklistedForMalware) {
[email protected]9f3c8532013-07-31 19:52:071651 // Installation of a blacklisted extension can happen from sync, policy,
1652 // etc, where to maintain consistency we need to install it, just never
1653 // load it (see AddExtension). Usually it should be the job of callers to
1654 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
1655 // showing the install dialogue).
[email protected]7c82539c2014-02-19 06:09:171656 extension_prefs_->AcknowledgeBlacklistedExtension(id);
[email protected]9f3c8532013-07-31 19:52:071657 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
1658 extension->location(),
1659 Manifest::NUM_LOCATIONS);
1660 }
1661
[email protected]695b5712012-12-06 23:55:281662 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:551663 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
1664 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:301665 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:401666 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]4c86be582014-06-04 08:39:111667 RecordPermissionMessagesHistogram(extension,
1668 "Extensions.Permissions_Install2");
[email protected]69084dc2012-11-17 07:39:301669 } else {
1670 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
1671 extension->GetType(), 100);
1672 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:401673 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]8f959f522014-08-06 06:26:281674
1675 // A fully installed app cannot be demoted to an ephemeral app.
1676 if ((install_flags & extensions::kInstallFlagIsEphemeral) &&
1677 !extension_prefs_->IsEphemeralApp(id)) {
1678 install_flags &= ~static_cast<int>(extensions::kInstallFlagIsEphemeral);
1679 }
[email protected]07533022011-06-27 20:42:551680 }
1681
[email protected]399583b2012-12-11 09:33:421682 const Extension::State initial_state =
1683 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]4a1d9c0d2014-06-13 12:50:111684 if (ShouldDelayExtensionUpdate(
1685 id,
1686 !!(install_flags & extensions::kInstallFlagInstallImmediately))) {
[email protected]9f3c8532013-07-31 19:52:071687 extension_prefs_->SetDelayedInstallInfo(
1688 extension,
1689 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111690 install_flags,
[email protected]9f3c8532013-07-31 19:52:071691 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
[email protected]d8fd0fd2014-03-24 13:16:061692 page_ordinal,
1693 install_parameter);
[email protected]0db124b02012-11-07 04:55:051694
1695 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:191696 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:051697
[email protected]75bdcb872013-03-13 00:41:451698 // Notify observers that app update is available.
1699 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:061700 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:051701 return;
1702 }
1703
[email protected]90878c52014-04-04 18:21:021704 extensions::SharedModuleService::ImportStatus status =
1705 shared_module_service_->SatisfyImports(extension);
[email protected]e5f8fffe2014-04-02 00:30:441706 if (installs_delayed_for_gc_) {
[email protected]9f3c8532013-07-31 19:52:071707 extension_prefs_->SetDelayedInstallInfo(
1708 extension,
1709 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111710 install_flags,
[email protected]9f3c8532013-07-31 19:52:071711 extensions::ExtensionPrefs::DELAY_REASON_GC,
[email protected]d8fd0fd2014-03-24 13:16:061712 page_ordinal,
1713 install_parameter);
[email protected]399583b2012-12-11 09:33:421714 delayed_installs_.Insert(extension);
[email protected]90878c52014-04-04 18:21:021715 } else if (status != SharedModuleService::IMPORT_STATUS_OK) {
1716 if (status == SharedModuleService::IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:071717 extension_prefs_->SetDelayedInstallInfo(
1718 extension,
1719 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111720 install_flags,
[email protected]9f4e4f082013-06-21 07:11:191721 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
[email protected]d8fd0fd2014-03-24 13:16:061722 page_ordinal,
1723 install_parameter);
[email protected]9f4e4f082013-06-21 07:11:191724 delayed_installs_.Insert(extension);
1725 }
[email protected]399583b2012-12-11 09:33:421726 } else {
[email protected]9f3c8532013-07-31 19:52:071727 AddNewOrUpdatedExtension(extension,
1728 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111729 install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061730 page_ordinal,
1731 install_parameter);
[email protected]399583b2012-12-11 09:33:421732 }
[email protected]8c484b742012-11-29 06:05:361733}
1734
1735void ExtensionService::AddNewOrUpdatedExtension(
1736 const Extension* extension,
[email protected]399583b2012-12-11 09:33:421737 Extension::State initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111738 int install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061739 const syncer::StringOrdinal& page_ordinal,
1740 const std::string& install_parameter) {
[email protected]8c484b742012-11-29 06:05:361741 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]760f743b2014-05-28 13:52:021742 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]4a1d9c0d2014-06-13 12:50:111743 extension_prefs_->OnExtensionInstalled(
1744 extension, initial_state, page_ordinal, install_flags, install_parameter);
[email protected]9e9c1d12013-07-31 01:58:121745 delayed_installs_.Remove(extension->id());
[email protected]913ffca92014-03-27 15:26:161746 if (InstallVerifier::NeedsVerification(*extension))
1747 system_->install_verifier()->VerifyExtension(extension->id());
[email protected]760f743b2014-05-28 13:52:021748 FinishInstallation(extension, was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481749}
1750
1751void ExtensionService::MaybeFinishDelayedInstallation(
1752 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:191753 // Check if the extension already got installed.
1754 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:481755 return;
[email protected]9f4e4f082013-06-21 07:11:191756 extensions::ExtensionPrefs::DelayReason reason =
1757 extension_prefs_->GetDelayedInstallReason(extension_id);
1758
1759 // Check if the extension is idle. DELAY_REASON_NONE is used for older
1760 // preferences files that will not have set this field but it was previously
1761 // only used for idle updates.
1762 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
1763 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]1d5cf4142014-01-24 18:25:221764 is_ready() && !extensions::util::IsExtensionIdle(extension_id, profile_))
[email protected]6f6101832012-11-27 22:10:481765 return;
1766
[email protected]9f4e4f082013-06-21 07:11:191767 const Extension* extension = delayed_installs_.GetByID(extension_id);
1768 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
[email protected]90878c52014-04-04 18:21:021769 extensions::SharedModuleService::ImportStatus status =
1770 shared_module_service_->SatisfyImports(extension);
1771 if (status != SharedModuleService::IMPORT_STATUS_OK) {
1772 if (status == SharedModuleService::IMPORT_STATUS_UNRECOVERABLE) {
[email protected]9f4e4f082013-06-21 07:11:191773 delayed_installs_.Remove(extension_id);
1774 // Make sure no version of the extension is actually installed, (i.e.,
1775 // that this delayed install was not an update).
1776 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
1777 extension_prefs_->DeleteExtensionPrefs(extension_id);
1778 }
1779 return;
1780 }
1781 }
1782
[email protected]6f6101832012-11-27 22:10:481783 FinishDelayedInstallation(extension_id);
1784}
1785
1786void ExtensionService::FinishDelayedInstallation(
1787 const std::string& extension_id) {
1788 scoped_refptr<const Extension> extension(
1789 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:091790 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:191791 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:481792
[email protected]760f743b2014-05-28 13:52:021793 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]399583b2012-12-11 09:33:421794 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:481795 NOTREACHED();
1796
[email protected]760f743b2014-05-28 13:52:021797 FinishInstallation(extension.get(), was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481798}
1799
[email protected]760f743b2014-05-28 13:52:021800void ExtensionService::FinishInstallation(
1801 const Extension* extension, bool was_ephemeral) {
[email protected]e7554c3f2013-05-29 00:36:561802 const extensions::Extension* existing_extension =
1803 GetInstalledExtension(extension->id());
1804 bool is_update = false;
1805 std::string old_name;
1806 if (existing_extension) {
1807 is_update = true;
1808 old_name = existing_extension->name();
1809 }
[email protected]760f743b2014-05-28 13:52:021810 bool from_ephemeral =
1811 was_ephemeral && !extension_prefs_->IsEphemeralApp(extension->id());
1812 extensions::InstalledExtensionInfo details(
1813 extension, is_update, from_ephemeral, old_name);
[email protected]fcb58a862012-05-01 01:03:151814 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061815 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]fcb58a862012-05-01 01:03:151816 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:021817 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:151818
[email protected]bc44b5da2014-06-12 14:20:001819 registry_->TriggerOnWillBeInstalled(
[email protected]760f743b2014-05-28 13:52:021820 extension, is_update, from_ephemeral, old_name);
[email protected]17f07822014-05-22 08:45:151821
[email protected]399583b2012-12-11 09:33:421822 // Unpacked extensions default to allowing file access, but if that has been
1823 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:401824 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:421825 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
1826 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1827 }
1828
[email protected]6f6101832012-11-27 22:10:481829 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:031830
[email protected]bc44b5da2014-06-12 14:20:001831 // Notify observers that need to know when an installation is complete.
[email protected]38e872532014-07-16 23:27:511832 registry_->TriggerOnInstalled(extension, is_update);
[email protected]bc44b5da2014-06-12 14:20:001833
[email protected]9f4e4f082013-06-21 07:11:191834 // Check extensions that may have been delayed only because this shared module
1835 // was not available.
[email protected]374ceb6f2014-07-02 19:25:341836 if (SharedModuleInfo::IsSharedModule(extension))
[email protected]9f4e4f082013-06-21 07:11:191837 MaybeFinishDelayedInstallations();
[email protected]4a190632009-05-09 01:07:421838}
1839
[email protected]760f743b2014-05-28 13:52:021840void ExtensionService::PromoteEphemeralApp(
1841 const extensions::Extension* extension, bool is_from_sync) {
1842 DCHECK(GetInstalledExtension(extension->id()) &&
1843 extension_prefs_->IsEphemeralApp(extension->id()));
1844
[email protected]db378322014-07-21 23:19:341845 if (extension->RequiresSortOrdinal()) {
1846 extension_prefs_->app_sorting()->SetExtensionVisible(
1847 extension->id(), extension->ShouldDisplayInNewTabPage());
1848
1849 if (!is_from_sync) {
[email protected]760f743b2014-05-28 13:52:021850 // Reset the sort ordinals of the app to ensure it is added to the default
1851 // position, like newly installed apps would.
1852 extension_prefs_->app_sorting()->ClearOrdinals(extension->id());
[email protected]760f743b2014-05-28 13:52:021853 }
1854
[email protected]db378322014-07-21 23:19:341855 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1856 extension->id(), syncer::StringOrdinal());
1857 }
1858
[email protected]760f743b2014-05-28 13:52:021859 // Remove the ephemeral flags from the preferences.
1860 extension_prefs_->OnEphemeralAppPromoted(extension->id());
1861
1862 // Fire install-related events to allow observers to handle the promotion
1863 // of the ephemeral app.
1864 extensions::InstalledExtensionInfo details(
1865 extension,
1866 true /* is update */,
1867 true /* from ephemeral */,
1868 extension->name() /* old name */);
1869 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061870 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:021871 content::Source<Profile>(profile_),
1872 content::Details<const extensions::InstalledExtensionInfo>(&details));
1873
1874 registry_->TriggerOnWillBeInstalled(
1875 extension,
1876 true /* is update */,
1877 true /* from ephemeral */,
1878 extension->name() /* old name */);
1879
1880 if (registry_->enabled_extensions().Contains(extension->id())) {
[email protected]8f959f522014-08-06 06:26:281881 // If the app is already enabled and loaded, fire the load events to allow
1882 // observers to handle the promotion of the ephemeral app.
[email protected]760f743b2014-05-28 13:52:021883 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061884 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:021885 content::Source<Profile>(profile_),
1886 content::Details<const Extension>(extension));
1887
1888 registry_->TriggerOnLoaded(extension);
[email protected]8f959f522014-08-06 06:26:281889 } else {
1890 // Cached ephemeral apps may be updated and disabled due to permissions
1891 // increase. The app can be enabled (as long as no other disable reasons
1892 // exist) as the install was user-acknowledged.
1893 int disable_mask = Extension::DISABLE_NONE;
1894 if (!is_from_sync)
1895 disable_mask |= Extension::DISABLE_PERMISSIONS_INCREASE;
1896
1897 int other_disable_reasons =
1898 extension_prefs_->GetDisableReasons(extension->id()) & ~disable_mask;
1899 if (!other_disable_reasons) {
1900 if (extension_prefs_->DidExtensionEscalatePermissions(extension->id()))
1901 GrantPermissionsAndEnableExtension(extension);
1902 else
1903 EnableExtension(extension->id());
1904 }
[email protected]760f743b2014-05-28 13:52:021905 }
1906
[email protected]38e872532014-07-16 23:27:511907 registry_->TriggerOnInstalled(extension, true);
[email protected]bc44b5da2014-06-12 14:20:001908
[email protected]760f743b2014-05-28 13:52:021909 if (!is_from_sync && extension_sync_service_)
1910 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1911}
1912
[email protected]76b65442012-11-17 14:11:481913const Extension* ExtensionService::GetPendingExtensionUpdate(
1914 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:191915 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:481916}
1917
[email protected]bb7f40952011-01-13 00:21:201918void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]bb1bc9b32013-12-21 03:09:141919 // No need to check for duplicates; inserting a duplicate is a no-op.
1920 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]3964e572014-02-13 21:57:411921 extensions_being_terminated_.erase(extension->id());
[email protected]b0af4792013-10-23 09:12:131922 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:201923}
1924
[email protected]e9d7496e2014-04-18 01:25:461925void ExtensionService::TerminateExtension(const std::string& extension_id) {
1926 const Extension* extension = GetInstalledExtension(extension_id);
1927 TrackTerminatedExtension(extension);
1928}
1929
[email protected]bb7f40952011-01-13 00:21:201930void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]cb1f4ac2014-08-07 16:55:421931 std::string lowercase_id = base::StringToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:141932 const Extension* extension =
1933 registry_->terminated_extensions().GetByID(lowercase_id);
1934 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:211935 if (extension) {
1936 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061937 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:211938 content::Source<Profile>(profile_),
1939 content::Details<const Extension>(extension));
1940 }
[email protected]bb7f40952011-01-13 00:21:201941}
1942
[email protected]8001df22011-04-28 19:59:471943const Extension* ExtensionService::GetInstalledExtension(
1944 const std::string& id) const {
[email protected]599539802014-01-07 23:06:001945 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:361946}
1947
[email protected]9060d8b02012-01-13 02:14:301948bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:321949 const std::string& id,
[email protected]c5e4a2222014-01-03 16:06:131950 const Version* version,
[email protected]650b2d52013-02-10 03:41:451951 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:401952 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:511953 int creation_flags,
1954 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:381955 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:261956 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:011957 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:301958 return false;
[email protected]a8af9fdb2010-10-28 21:52:201959
[email protected]7577a5c52009-07-30 06:21:581960 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491961 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581962 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:021963 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:351964
[email protected]7577a5c52009-07-30 06:21:581965 if (existing) {
[email protected]910f72ce2012-08-24 01:38:351966 // The default apps will have the location set as INTERNAL. Since older
1967 // default apps are installed as EXTERNAL, we override them. However, if the
1968 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:401969 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:351970 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:401971 (location == Manifest::INTERNAL &&
1972 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:351973
1974 if (!is_default_apps_migration) {
1975 DCHECK(version);
1976
1977 switch (existing->version()->CompareTo(*version)) {
1978 case -1: // existing version is older, we should upgrade
1979 break;
1980 case 0: // existing version is same, do nothing
1981 return false;
1982 case 1: // existing version is newer, uh-oh
1983 LOG(WARNING) << "Found external version of extension " << id
1984 << "that is older than current version. Current version "
1985 << "is: " << existing->VersionString() << ". New "
1986 << "version is: " << version->GetString()
1987 << ". Keeping current version.";
1988 return false;
1989 }
[email protected]7577a5c52009-07-30 06:21:581990 }
1991 }
1992
[email protected]9060d8b02012-01-13 02:14:301993 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:301994 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:481995 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:301996 return false;
[email protected]e3987852012-05-04 10:06:301997 }
[email protected]9c635f22010-12-02 09:36:361998
[email protected]14908b72011-04-20 06:54:361999 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372000 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162001 installer->set_install_source(location);
2002 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072003 installer->set_expected_version(*version);
2004 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312005 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182006#if defined(OS_CHROMEOS)
2007 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2008#else
[email protected]6dfbbf82010-03-12 23:09:162009 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182010#endif
[email protected]47fc70c2011-12-06 07:29:512011
2012 // Depending on the source, a new external extension might not need a user
2013 // notification on installation. For such extensions, mark them acknowledged
2014 // now to suppress the notification.
2015 if (mark_acknowledged)
[email protected]374ceb6f2014-07-02 19:25:342016 external_install_manager_->AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302017
2018 return true;
[email protected]7577a5c52009-07-30 06:21:582019}
2020
[email protected]eaa7dd182010-12-14 11:09:002021void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132022 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182023 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332024 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182025 if (iter == orphaned_dev_tools_.end())
2026 return;
2027
[email protected]b3f957e62014-08-08 10:09:022028 iter->second->ConnectWebContents(host->host_contents());
[email protected]406027c02010-09-27 08:03:182029 orphaned_dev_tools_.erase(iter);
2030}
2031
[email protected]432115822011-07-10 15:52:272032void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532033 const content::NotificationSource& source,
2034 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272035 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542036 case chrome::NOTIFICATION_APP_TERMINATING:
2037 // Shutdown has started. Don't start any more extension installs.
2038 // (We cannot use ExtensionService::Shutdown() for this because it
2039 // happens too late in browser teardown.)
2040 browser_terminating_ = true;
2041 break;
[email protected]adf5a102014-07-31 12:44:062042 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532043 if (profile_ !=
2044 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322045 break;
[email protected]6c2381d2011-10-19 02:52:532046 }
[email protected]a4ed6282009-12-14 20:51:162047
[email protected]3a1dc572012-07-31 22:25:132048 extensions::ExtensionHost* host =
2049 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262050
[email protected]3964e572014-02-13 21:57:412051 // If the extension is already being terminated, there is nothing left to
2052 // do.
2053 if (!extensions_being_terminated_.insert(host->extension_id()).second)
2054 break;
2055
[email protected]fa2416f2011-05-03 08:41:202056 // Mark the extension as terminated and Unload it. We want it to
2057 // be in a consistent state: either fully working or not loaded
2058 // at all, but never half-crashed. We do it in a PostTask so
2059 // that other handlers of this notification will still have
2060 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162061 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362062 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362063 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202064 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012065 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202066 host->extension()));
[email protected]31f77262009-12-02 20:48:532067 break;
2068 }
[email protected]432115822011-07-10 15:52:272069 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302070 content::RenderProcessHost* process =
2071 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192072 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302073 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192074 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2075 break;
2076
[email protected]fafdc842014-01-17 18:09:082077 extensions::ProcessMap* process_map =
2078 extensions::ProcessMap::Get(profile_);
2079 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:032080 // An extension process was terminated, this might have resulted in an
2081 // app or extension becoming idle.
2082 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:082083 process_map->GetExtensionsInProcess(process->GetID());
[email protected]52b76592013-11-02 17:59:032084 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2085 it != extension_ids.end(); ++it) {
2086 if (delayed_installs_.Contains(*it)) {
2087 base::MessageLoop::current()->PostDelayedTask(
2088 FROM_HERE,
2089 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2090 AsWeakPtr(), *it),
2091 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2092 }
2093 }
2094 }
2095
[email protected]fafdc842014-01-17 18:09:082096 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462097 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202098 BrowserThread::IO,
2099 FROM_HERE,
2100 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082101 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302102 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212103 break;
2104 }
[email protected]92dd8d92013-02-26 00:41:082105 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452106 // Notify observers that chrome update is available.
2107 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2108 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082109 break;
2110 }
[email protected]ebe07772014-05-22 04:16:062111 case chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED: {
2112 OnProfileDestructionStarted();
2113 break;
2114 }
[email protected]aa96d3a2010-08-21 08:45:252115
[email protected]4814b512009-11-07 00:12:292116 default:
2117 NOTREACHED() << "Unexpected notification type.";
2118 }
2119}
2120
[email protected]90bb38d2012-11-14 18:36:032121void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]373daf972014-04-10 01:50:442122 error_controller_->ShowErrorIfNeeded();
[email protected]a6a7ced2012-11-01 17:24:182123 CheckManagementPolicy();
2124}
2125
[email protected]612a1cb12012-10-17 13:18:032126bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2127 // Extensions installed by policy can't be disabled. So even if a previous
2128 // installation disabled the extension, make sure it is now enabled.
[email protected]ab894542014-07-03 12:35:392129 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
[email protected]612a1cb12012-10-17 13:18:032130 return true;
2131
2132 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2133 return false;
2134
[email protected]41070e8d2012-10-24 01:34:362135 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032136 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542137 // enabling them. Hosted apps are excepted because they are not dangerous
2138 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402139 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2140 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032141 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2142 return false;
2143 }
2144 }
[email protected]612a1cb12012-10-17 13:18:032145
2146 return true;
2147}
[email protected]0db124b02012-11-07 04:55:052148
[email protected]e7aa7b7e2012-11-27 04:51:222149bool ExtensionService::ShouldDelayExtensionUpdate(
2150 const std::string& extension_id,
[email protected]4a1d9c0d2014-06-13 12:50:112151 bool install_immediately) const {
[email protected]e7aa7b7e2012-11-27 04:51:222152 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2153
2154 // If delayed updates are globally disabled, or just for this extension,
2155 // don't delay.
[email protected]4a1d9c0d2014-06-13 12:50:112156 if (!install_updates_when_idle_ || install_immediately)
[email protected]e7aa7b7e2012-11-27 04:51:222157 return false;
2158
[email protected]695b5712012-12-06 23:55:282159 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222160 // If there is no old extension, this is not an update, so don't delay.
2161 if (!old)
2162 return false;
2163
[email protected]9367eabc2013-03-01 01:29:292164 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222165 // Delay installation if the extension listens for the onUpdateAvailable
2166 // event.
2167 return system_->event_router()->ExtensionHasEventListener(
2168 extension_id, kOnUpdateAvailableEvent);
2169 } else {
2170 // Delay installation if the extension is not idle.
[email protected]1d5cf4142014-01-24 18:25:222171 return !extensions::util::IsExtensionIdle(extension_id, profile_);
[email protected]e7aa7b7e2012-11-27 04:51:222172 }
[email protected]0db124b02012-11-07 04:55:052173}
[email protected]fdd679b2012-11-15 20:49:392174
[email protected]e5f8fffe2014-04-02 00:30:442175void ExtensionService::OnGarbageCollectIsolatedStorageStart() {
2176 DCHECK(!installs_delayed_for_gc_);
2177 installs_delayed_for_gc_ = true;
[email protected]399583b2012-12-11 09:33:422178}
2179
2180void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]e5f8fffe2014-04-02 00:30:442181 DCHECK(installs_delayed_for_gc_);
2182 installs_delayed_for_gc_ = false;
[email protected]9f4e4f082013-06-21 07:11:192183 MaybeFinishDelayedInstallations();
2184}
2185
2186void ExtensionService::MaybeFinishDelayedInstallations() {
2187 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422188 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2189 it != delayed_installs_.end();
2190 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192191 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422192 }
[email protected]9f4e4f082013-06-21 07:11:192193 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2194 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422195 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192196 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422197 }
[email protected]399583b2012-12-11 09:33:422198}
2199
[email protected]fdd679b2012-11-15 20:49:392200void ExtensionService::OnBlacklistUpdated() {
[email protected]2d19eb6e2014-01-27 17:30:002201 blacklist_->GetBlacklistedIDs(
[email protected]f47f7172014-03-19 19:27:102202 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252203 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282204}
2205
[email protected]2d19eb6e2014-01-27 17:30:002206void ExtensionService::ManageBlacklist(
2207 const extensions::Blacklist::BlacklistStateMap& state_map) {
[email protected]54ee8192014-03-29 17:37:242208 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]695b5712012-12-06 23:55:282209
[email protected]2d19eb6e2014-01-27 17:30:002210 std::set<std::string> blocked;
2211 ExtensionIdSet greylist;
2212 ExtensionIdSet unchanged;
2213 for (extensions::Blacklist::BlacklistStateMap::const_iterator it =
2214 state_map.begin();
2215 it != state_map.end();
2216 ++it) {
2217 switch (it->second) {
2218 case extensions::NOT_BLACKLISTED:
2219 break;
[email protected]695b5712012-12-06 23:55:282220
[email protected]2d19eb6e2014-01-27 17:30:002221 case extensions::BLACKLISTED_MALWARE:
2222 blocked.insert(it->first);
2223 break;
2224
2225 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2226 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2227 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
2228 greylist.insert(it->first);
2229 break;
2230
2231 case extensions::BLACKLISTED_UNKNOWN:
2232 unchanged.insert(it->first);
2233 break;
2234 }
2235 }
2236
2237 UpdateBlockedExtensions(blocked, unchanged);
2238 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2239
[email protected]373daf972014-04-10 01:50:442240 error_controller_->ShowErrorIfNeeded();
[email protected]2d19eb6e2014-01-27 17:30:002241}
2242
2243namespace {
2244void Partition(const ExtensionIdSet& before,
2245 const ExtensionIdSet& after,
2246 const ExtensionIdSet& unchanged,
2247 ExtensionIdSet* no_longer,
2248 ExtensionIdSet* not_yet) {
2249 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2250 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2251 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2252}
2253} // namespace
2254
2255void ExtensionService::UpdateBlockedExtensions(
2256 const ExtensionIdSet& blocked,
2257 const ExtensionIdSet& unchanged) {
2258 ExtensionIdSet not_yet_blocked, no_longer_blocked;
2259 Partition(registry_->blacklisted_extensions().GetIDs(),
2260 blocked, unchanged,
2261 &no_longer_blocked, &not_yet_blocked);
2262
2263 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2264 it != no_longer_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282265 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142266 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252267 if (!extension.get()) {
[email protected]2d19eb6e2014-01-27 17:30:002268 NOTREACHED() << "Extension " << *it << " no longer blocked, "
2269 << "but it was never blocked.";
[email protected]695b5712012-12-06 23:55:282270 continue;
[email protected]3f2a2fa2013-09-24 02:55:252271 }
[email protected]bb1bc9b32013-12-21 03:09:142272 registry_->RemoveBlacklisted(*it);
[email protected]3f2a2fa2013-09-24 02:55:252273 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092274 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092275 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092276 extension->location(),
2277 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282278 }
2279
[email protected]2d19eb6e2014-01-27 17:30:002280 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2281 it != not_yet_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282282 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252283 if (!extension.get()) {
2284 NOTREACHED() << "Extension " << *it << " needs to be "
2285 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282286 continue;
[email protected]3f2a2fa2013-09-24 02:55:252287 }
[email protected]bb1bc9b32013-12-21 03:09:142288 registry_->AddBlacklisted(extension);
[email protected]2d19eb6e2014-01-27 17:30:002289 extension_prefs_->SetExtensionBlacklistState(
2290 extension->id(), extensions::BLACKLISTED_MALWARE);
[email protected]b0af4792013-10-23 09:12:132291 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092292 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2293 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282294 }
[email protected]2d19eb6e2014-01-27 17:30:002295}
[email protected]695b5712012-12-06 23:55:282296
[email protected]2d19eb6e2014-01-27 17:30:002297// TODO(oleg): UMA logging
2298void ExtensionService::UpdateGreylistedExtensions(
2299 const ExtensionIdSet& greylist,
2300 const ExtensionIdSet& unchanged,
2301 const extensions::Blacklist::BlacklistStateMap& state_map) {
2302 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2303 Partition(greylist_.GetIDs(),
2304 greylist, unchanged,
2305 &no_longer_greylisted, &not_yet_greylisted);
2306
2307 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2308 it != no_longer_greylisted.end(); ++it) {
2309 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2310 if (!extension.get()) {
2311 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2312 << "but it was not marked as greylisted.";
2313 continue;
2314 }
2315
2316 greylist_.Remove(*it);
2317 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2318 extensions::NOT_BLACKLISTED);
2319 if (extension_prefs_->GetDisableReasons(extension->id()) &
2320 extensions::Extension::DISABLE_GREYLIST)
2321 EnableExtension(*it);
2322 }
2323
2324 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2325 it != not_yet_greylisted.end(); ++it) {
2326 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2327 if (!extension.get()) {
2328 NOTREACHED() << "Extension " << *it << " needs to be "
2329 << "disabled, but it's not installed.";
2330 continue;
2331 }
2332 greylist_.Insert(extension);
2333 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2334 state_map.find(*it)->second);
2335 if (registry_->enabled_extensions().Contains(extension->id()))
2336 DisableExtension(*it, extensions::Extension::DISABLE_GREYLIST);
2337 }
[email protected]fdd679b2012-11-15 20:49:392338}
[email protected]75bdcb872013-03-13 00:41:452339
2340void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2341 update_observers_.AddObserver(observer);
2342}
2343
2344void ExtensionService::RemoveUpdateObserver(
2345 extensions::UpdateObserver* observer) {
2346 update_observers_.RemoveObserver(observer);
2347}
[email protected]bb1bc9b32013-12-21 03:09:142348
2349// Used only by test code.
2350void ExtensionService::UnloadAllExtensionsInternal() {
2351 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2352
2353 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132354 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142355
2356 // TODO(erikkay) should there be a notification for this? We can't use
2357 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2358 // or uninstalled.
2359}
[email protected]ebe07772014-05-22 04:16:062360
2361void ExtensionService::OnProfileDestructionStarted() {
2362 ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
2363 for (ExtensionIdSet::iterator it = ids_to_unload.begin();
2364 it != ids_to_unload.end();
2365 ++it) {
2366 UnloadExtension(*it, UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN);
2367 }
2368}