blob: b4e4820bc8256d80a84831ad155e99f36d2d1e15 [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"
mukai87a8d402014-09-15 20:15:0921#include "chrome/browser/content_settings/content_settings_internal_extension_provider.h"
sunil.ratnu21b896c2014-11-24 10:09:2422#include "chrome/browser/extensions/api/content_settings/content_settings_custom_extension_provider.h"
mukai87a8d402014-09-15 20:15:0923#include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
[email protected]d8c8f25f2011-11-02 18:18:0124#include "chrome/browser/extensions/component_loader.h"
[email protected]13e062e2014-08-09 10:21:5525#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3826#include "chrome/browser/extensions/data_deleter.h"
rdevlin.cronin2813c1b2014-08-26 23:51:0827#include "chrome/browser/extensions/extension_action_storage_manager.h"
[email protected]5a145e82014-05-29 22:19:0728#include "chrome/browser/extensions/extension_assets_manager.h"
[email protected]62f051c2012-03-29 17:04:4429#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]373daf972014-04-10 01:50:4430#include "chrome/browser/extensions/extension_error_controller.h"
[email protected]19eb80152011-02-26 00:28:4331#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5232#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]617342a42013-12-18 23:34:0333#include "chrome/browser/extensions/extension_util.h"
[email protected]2894a512014-06-26 19:03:5634#include "chrome/browser/extensions/external_install_manager.h"
[email protected]5df038b2012-07-16 19:03:2735#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4636#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0137#include "chrome/browser/extensions/installed_loader.h"
[email protected]f3d3b382014-03-14 21:19:2838#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]13e062e2014-08-09 10:21:5539#include "chrome/browser/extensions/permissions_updater.h"
[email protected]90878c52014-04-04 18:21:0240#include "chrome/browser/extensions/shared_module_service.h"
[email protected]13e062e2014-08-09 10:21:5541#include "chrome/browser/extensions/unpacked_installer.h"
rockotdb5192992014-09-16 21:27:0242#include "chrome/browser/extensions/updater/chrome_extension_downloader_factory.h"
[email protected]13e062e2014-08-09 10:21:5543#include "chrome/browser/extensions/updater/extension_updater.h"
rockotdb5192992014-09-16 21:27:0244#include "chrome/browser/google/google_brand.h"
[email protected]8ecad5e2010-12-02 21:18:3345#include "chrome/browser/profiles/profile.h"
[email protected]b5a507b22013-11-08 20:41:5746#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1747#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3148#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1549#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5450#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4551#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1952#include "chrome/common/extensions/extension_constants.h"
[email protected]15d4d2d2013-08-09 06:49:0353#include "chrome/common/extensions/features/feature_channel.h"
[email protected]a57209872009-05-04 22:53:1454#include "chrome/common/url_constants.h"
mukai8eaec822014-10-25 17:53:1655#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]fdd28372014-08-21 02:27:2656#include "components/crx_file/id_util.h"
[email protected]7a83dd072013-09-03 12:09:1757#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]dccba4f82014-05-29 00:52:5658#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3759#include "content/public/browser/notification_service.h"
[email protected]f3b1a082011-11-18 00:34:3060#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4261#include "content/public/browser/storage_partition.h"
[email protected]34423532013-11-21 18:13:1062#include "extensions/browser/event_router.h"
[email protected]22401dc2014-03-21 01:38:5763#include "extensions/browser/extension_host.h"
[email protected]dccba4f82014-05-29 00:52:5664#include "extensions/browser/extension_prefs.h"
[email protected]bb1bc9b32013-12-21 03:09:1465#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2466#include "extensions/browser/extension_system.h"
rockotffa65b12014-10-14 17:19:0667#include "extensions/browser/extensions_browser_client.h"
[email protected]4a1d9c0d2014-06-13 12:50:1168#include "extensions/browser/install_flag.h"
[email protected]45f5b7d2014-01-22 23:47:1369#include "extensions/browser/runtime_data.h"
[email protected]e43c61f2014-07-20 21:46:3470#include "extensions/browser/uninstall_reason.h"
[email protected]a9aa62b312013-11-29 05:35:0671#include "extensions/browser/update_observer.h"
rockotffa65b12014-10-14 17:19:0672#include "extensions/browser/updater/extension_cache.h"
rockot05f40a922014-10-16 19:40:2573#include "extensions/browser/updater/extension_downloader.h"
[email protected]fb820c02014-03-13 15:07:0874#include "extensions/common/extension_messages.h"
rockot90659852014-09-18 19:31:5275#include "extensions/common/extension_urls.h"
[email protected]5ef835a2013-11-08 20:42:5776#include "extensions/common/feature_switch.h"
[email protected]85df9d12014-04-15 17:02:1477#include "extensions/common/file_util.h"
[email protected]0c3c9732013-09-16 08:53:4178#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5179#include "extensions/common/manifest_handlers/background_info.h"
rockotd5546142014-10-15 00:29:0880#include "extensions/common/manifest_url_handlers.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"
pilgrime92c5fcd2014-09-10 23:31:2387#include "storage/browser/fileapi/file_system_backend.h"
88#include "storage/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;
96using extensions::ExtensionIdSet;
97using extensions::ExtensionInfo;
[email protected]599539802014-01-07 23:06:0098using extensions::ExtensionRegistry;
[email protected]289c44b2013-12-17 03:26:5799using extensions::ExtensionSet;
[email protected]215a7be2012-10-22 19:53:42100using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46101using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36102using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40103using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06104using extensions::PermissionMessage;
105using extensions::PermissionMessages;
106using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19107using extensions::SharedModuleInfo;
[email protected]90878c52014-04-04 18:21:02108using extensions::SharedModuleService;
[email protected]e410b5f2012-12-14 14:02:24109using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05110
[email protected]0c3c9732013-09-16 08:53:41111namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06112
[email protected]b6ab96d2009-08-20 18:58:19113namespace {
114
[email protected]0db124b02012-11-07 04:55:05115// Wait this many seconds after an extensions becomes idle before updating it.
[email protected]ddc6a122014-06-27 04:52:32116const int kUpdateIdleDelay = 5;
[email protected]0db124b02012-11-07 04:55:05117
[email protected]c6d474f82009-12-16 21:11:06118} // namespace
[email protected]b6ab96d2009-08-20 18:58:19119
[email protected]eaa7dd182010-12-14 11:09:00120// ExtensionService.
[email protected]6014d672008-12-05 00:38:25121
[email protected]8e4560b62011-01-14 10:09:14122void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12123 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20124
[email protected]7a4c6852010-09-16 03:44:22125 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27126 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21127 for (i = external_extension_providers_.begin();
128 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14129 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21130 if (i->get()->HasExtension(id))
131 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22132 }
133
[email protected]0f48fca2011-05-19 18:46:35134 // We get the list of external extensions to check from preferences.
135 // It is possible that an extension has preferences but is not loaded.
136 // For example, an extension that requires experimental permissions
137 // will not be loaded if the experimental command line flag is not used.
138 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40139 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35140 // We can't call UninstallExtension with an unloaded/invalid
141 // extension ID.
142 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
143 << "with id: " << id;
144 return;
145 }
[email protected]42d58f62014-07-31 01:32:45146 UninstallExtension(id,
147 extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION,
148 base::Bind(&base::DoNothing),
149 NULL);
[email protected]7a4c6852010-09-16 03:44:22150}
151
[email protected]7f8f24f2012-11-15 19:40:14152void ExtensionService::SetFileTaskRunnerForTesting(
dcheng605ef8d2014-08-28 18:29:44153 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
[email protected]7f8f24f2012-11-15 19:40:14154 file_task_runner_ = task_runner;
155}
156
[email protected]8e4560b62011-01-14 10:09:14157void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22158 external_extension_providers_.clear();
159}
160
[email protected]8e4560b62011-01-14 10:09:14161void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27162 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12163 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21164 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27165 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22166}
167
[email protected]8f959f522014-08-06 06:26:28168void ExtensionService::BlacklistExtensionForTest(
169 const std::string& extension_id) {
mlerman6a37b6a42014-11-26 22:10:53170 ExtensionIdSet blacklisted;
[email protected]8f959f522014-08-06 06:26:28171 ExtensionIdSet unchanged;
mlerman6a37b6a42014-11-26 22:10:53172 blacklisted.insert(extension_id);
173 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]8f959f522014-08-06 06:26:28174}
175
[email protected]9060d8b02012-01-13 02:14:30176bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22177 const std::string& id,
[email protected]d8fd0fd2014-03-24 13:16:06178 const std::string& install_parameter,
[email protected]21a5a672010-11-04 10:47:42179 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48180 Manifest::Location location,
181 int creation_flags,
182 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38183 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:26184 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20185
[email protected]044e86992014-01-24 22:59:11186 if (Manifest::IsExternalLocation(location)) {
187 // All extensions that are not user specific can be cached.
rockotffa65b12014-10-14 17:19:06188 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache()
189 ->AllowCaching(id);
[email protected]044e86992014-01-24 22:59:11190 }
191
[email protected]8a87a5332011-08-11 17:54:59192 const Extension* extension = GetExtensionById(id, true);
193 if (extension) {
194 // Already installed. Skip this install if the current location has
195 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40196 Manifest::Location current = extension->location();
197 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30198 return false;
[email protected]8a87a5332011-08-11 17:54:59199 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22200 }
[email protected]9060d8b02012-01-13 02:14:30201
202 // Add |id| to the set of pending extensions. If it can not be added,
203 // then there is already a pending record from a higher-priority install
204 // source. In this case, signal that this extension will not be
205 // installed by returning false.
206 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]d8fd0fd2014-03-24 13:16:06207 id,
208 install_parameter,
209 update_url,
210 location,
211 creation_flags,
212 mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30213 return false;
[email protected]31bb5ee62012-09-12 22:58:40214 }
[email protected]9060d8b02012-01-13 02:14:30215
[email protected]94fde232012-04-27 10:22:30216 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30217 return true;
[email protected]7a4c6852010-09-16 03:44:22218}
219
[email protected]6aeac8342010-10-01 20:21:18220// static
[email protected]cc2f55c2014-07-08 02:19:04221// This function is used to uninstall an extension via sync. The LOG statements
222// within this function are used to inform the user if the uninstall cannot be
223// done.
[email protected]eaa7dd182010-12-14 11:09:00224bool ExtensionService::UninstallExtensionHelper(
225 ExtensionService* extensions_service,
[email protected]cc2f55c2014-07-08 02:19:04226 const std::string& extension_id,
[email protected]e43c61f2014-07-20 21:46:34227 extensions::UninstallReason reason) {
[email protected]d6ebc9792011-04-07 18:18:33228 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40229 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18230 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33231 << "id: " << extension_id;
232 return false;
[email protected]6aeac8342010-10-01 20:21:18233 }
234
[email protected]d6ebc9792011-04-07 18:18:33235 // The following call to UninstallExtension will not allow an uninstall of a
236 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09237 base::string16 error;
[email protected]42d58f62014-07-31 01:32:45238 if (!extensions_service->UninstallExtension(
239 extension_id, reason, base::Bind(&base::DoNothing), &error)) {
[email protected]d6ebc9792011-04-07 18:18:33240 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
241 << ": " << error;
242 return false;
243 }
[email protected]95da88c42011-03-31 10:07:33244
[email protected]6aeac8342010-10-01 20:21:18245 return true;
246}
247
[email protected]eaa7dd182010-12-14 11:09:00248ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24249 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45250 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23251 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39252 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03253 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35254 bool extensions_enabled,
255 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39256 : extensions::Blacklist::Observer(blacklist),
257 profile_(profile),
[email protected]bd306722012-07-11 20:43:59258 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43259 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28260 blacklist_(blacklist),
[email protected]f8aefb132013-10-30 09:29:52261 extension_sync_service_(NULL),
[email protected]5fdfa562013-12-27 17:43:59262 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]6c9bedf2014-05-21 03:55:51263 pending_extension_manager_(profile),
[email protected]a9b00ac2009-06-25 21:03:23264 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03265 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13266 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33267 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35268 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18269 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54270 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19271 installs_delayed_for_gc_(false),
[email protected]e5f8fffe2014-04-02 00:30:44272 is_first_run_(false),
mlerman6a37b6a42014-11-26 22:10:53273 block_extensions_(false),
[email protected]90878c52014-04-04 18:21:02274 shared_module_service_(new extensions::SharedModuleService(profile_)) {
[email protected]a29a517a2011-01-21 21:11:12275 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20276
[email protected]36a784c2009-06-23 06:21:08277 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02278 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
279 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23280 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08281
[email protected]3c4abc82012-10-22 22:25:54282 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
283 content::NotificationService::AllBrowserContextsAndSources());
[email protected]adf5a102014-07-31 12:44:06284 registrar_.Add(this,
285 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07286 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27287 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07288 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08289 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
290 content::NotificationService::AllBrowserContextsAndSources());
[email protected]ebe07772014-05-22 04:16:06291 registrar_.Add(this,
292 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
293 content::Source<Profile>(profile_));
binjin1569c9b2014-09-05 13:33:18294
295 extensions::ExtensionManagementFactory::GetForBrowserContext(profile_)
296 ->AddObserver(this);
[email protected]4814b512009-11-07 00:12:29297
[email protected]93fd78f42009-07-10 16:43:17298 // Set up the ExtensionUpdater
299 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02300 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17301 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25302 base::StringToInt(command_line->GetSwitchValueASCII(
303 switches::kExtensionsUpdateFrequency),
304 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17305 }
[email protected]044e86992014-01-24 22:59:11306 updater_.reset(new extensions::ExtensionUpdater(
307 this,
308 extension_prefs,
309 profile->GetPrefs(),
310 profile,
311 update_frequency,
rockotffa65b12014-10-14 17:19:06312 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache(),
rockotdb5192992014-09-16 21:27:02313 base::Bind(ChromeExtensionDownloaderFactory::CreateForProfile,
314 profile)));
[email protected]93fd78f42009-07-10 16:43:17315 }
316
[email protected]25ae0152011-11-18 14:40:02317 component_loader_.reset(
318 new extensions::ComponentLoader(this,
319 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08320 g_browser_process->local_state(),
321 profile));
[email protected]8e4560b62011-01-14 10:09:14322
[email protected]0436b102011-04-15 18:30:03323 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25324 extensions::ExternalProviderImpl::CreateExternalProviders(
325 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05326 }
[email protected]b671760b2010-07-15 21:13:47327
[email protected]74474042013-11-21 12:03:54328 // Set this as the ExtensionService for app sorting to ensure it causes syncs
329 // if required.
[email protected]b3aa7182013-04-25 04:45:23330 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
331
[email protected]373daf972014-04-10 01:50:44332 error_controller_.reset(
333 new extensions::ExtensionErrorController(profile_, is_first_run_));
[email protected]374ceb6f2014-07-02 19:25:34334 external_install_manager_.reset(
335 new extensions::ExternalInstallManager(profile_, is_first_run_));
[email protected]373daf972014-04-10 01:50:44336
[email protected]c77f2352012-08-08 22:07:58337 extension_action_storage_manager_.reset(
338 new extensions::ExtensionActionStorageManager(profile_));
339
[email protected]cb0e50312011-05-09 15:03:07340 // How long is the path to the Extensions directory?
341 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
342 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25343}
344
[email protected]3f213ad2012-07-26 23:39:41345extensions::PendingExtensionManager*
346 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37347 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45348}
349
[email protected]eaa7dd182010-12-14 11:09:00350ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17351 // No need to unload extensions here because they are profile-scoped, and the
352 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17353
[email protected]5df038b2012-07-16 19:03:27354 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14355 for (i = external_extension_providers_.begin();
356 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27357 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14358 provider->ServiceShutdown();
359 }
[email protected]6014d672008-12-05 00:38:25360}
361
[email protected]037228a2012-10-05 01:36:16362void ExtensionService::Shutdown() {
binjin1569c9b2014-09-05 13:33:18363 extensions::ExtensionManagementFactory::GetInstance()
364 ->GetForBrowserContext(profile())
365 ->RemoveObserver(this);
[email protected]4c9201c42013-08-16 04:56:21366 system_->management_policy()->UnregisterProvider(
367 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16368}
369
[email protected]b2907fd2011-03-25 16:43:37370const Extension* ExtensionService::GetExtensionById(
371 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00372 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28373 if (include_disabled) {
mlerman6a37b6a42014-11-26 22:10:53374 // Include blacklisted and blocked extensions here because there are
375 // hundreds of callers of this function, and many might assume that this
376 // includes those that have been disabled due to blacklisting or blocking.
[email protected]599539802014-01-07 23:06:00377 include_mask |= ExtensionRegistry::DISABLED |
mlerman6a37b6a42014-11-26 22:10:53378 ExtensionRegistry::BLACKLISTED | ExtensionRegistry::BLOCKED;
[email protected]695b5712012-12-06 23:55:28379 }
[email protected]599539802014-01-07 23:06:00380 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28381}
382
[email protected]eaa7dd182010-12-14 11:09:00383void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12384 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20385
[email protected]3350d712013-11-18 09:32:24386 base::Time begin_time = base::Time::Now();
387
[email protected]4a10006a2013-05-17 23:18:35388 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14389 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32390
[email protected]820d9bd2013-04-03 03:46:03391 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
rdevlin.cronin462b2fb2014-10-02 18:32:52392 if (cmd_line->HasSwitch(switches::kInstallEphemeralAppFromWebstore)) {
[email protected]820d9bd2013-04-03 03:46:03393 // The sole purpose of this launch is to install a new extension from CWS
394 // and immediately terminate: loading already installed extensions is
395 // unnecessary and may interfere with the inline install dialog (e.g. if an
396 // extension listens to onStartup and opens a window).
397 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25398 } else {
[email protected]47b60072013-06-20 10:35:56399 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
400 component_loader_->LoadAll();
401 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19402
[email protected]2edd57852013-08-14 22:07:40403 // Attempt to re-enable extensions whose only disable reason is reloading.
404 std::vector<std::string> extensions_to_enable;
[email protected]bb1bc9b32013-12-21 03:09:14405 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
406 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
407 iter != disabled_extensions.end(); ++iter) {
[email protected]2edd57852013-08-14 22:07:40408 const Extension* e = iter->get();
409 if (extension_prefs_->GetDisableReasons(e->id()) ==
410 Extension::DISABLE_RELOAD) {
411 extensions_to_enable.push_back(e->id());
412 }
413 }
414 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
415 it != extensions_to_enable.end(); ++it) {
416 EnableExtension(*it);
417 }
418
[email protected]9f4e4f082013-06-21 07:11:19419 // Finish install (if possible) of extensions that were still delayed while
420 // the browser was shut down.
421 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
422 extension_prefs_->GetAllDelayedInstallInfo());
423 for (size_t i = 0; i < delayed_info->size(); ++i) {
424 ExtensionInfo* info = delayed_info->at(i).get();
425 scoped_refptr<const Extension> extension(NULL);
426 if (info->extension_manifest) {
427 std::string error;
428 extension = Extension::Create(
429 info->extension_path,
430 info->extension_location,
431 *info->extension_manifest,
432 extension_prefs_->GetDelayedInstallCreationFlags(
433 info->extension_id),
434 info->extension_id,
435 &error);
436 if (extension.get())
437 delayed_installs_.Insert(extension);
438 }
439 }
440 MaybeFinishDelayedInstallations();
441
442 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
443 extension_prefs_->GetAllDelayedInstallInfo());
444 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
445 delayed_info2->size() - delayed_info->size());
446
[email protected]47b60072013-06-20 10:35:56447 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25448
[email protected]47b60072013-06-20 10:35:56449 // TODO(erikkay) this should probably be deferred to a future point
450 // rather than running immediately at startup.
451 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57452
[email protected]2d19eb6e2014-01-27 17:30:00453 LoadGreylistFromPrefs();
[email protected]399583b2012-12-11 09:33:42454 }
[email protected]3350d712013-11-18 09:32:24455
456 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
457 base::Time::Now() - begin_time);
[email protected]6014d672008-12-05 00:38:25458}
459
[email protected]2d19eb6e2014-01-27 17:30:00460void ExtensionService::LoadGreylistFromPrefs() {
[email protected]f47f7172014-03-19 19:27:10461 scoped_ptr<ExtensionSet> all_extensions =
462 registry_->GenerateInstalledExtensionsSet();
[email protected]2d19eb6e2014-01-27 17:30:00463
464 for (ExtensionSet::const_iterator it = all_extensions->begin();
465 it != all_extensions->end(); ++it) {
466 extensions::BlacklistState state =
467 extension_prefs_->GetExtensionBlacklistState((*it)->id());
468 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
469 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
470 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
471 greylist_.Insert(*it);
472 }
473}
474
[email protected]31bb5ee62012-09-12 22:58:40475bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45476 const base::FilePath& extension_path,
[email protected]044e86992014-01-24 22:59:11477 bool file_ownership_passed,
[email protected]31bb5ee62012-09-12 22:58:40478 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12479 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54480 if (browser_terminating_) {
481 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
482 // Leak the temp file at extension_path. We don't want to add to the disk
483 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
484 // the file is in the OS temp directory which should be cleaned up for us.
485 return false;
486 }
[email protected]a8af9fdb2010-10-28 21:52:20487
[email protected]3f213ad2012-07-26 23:39:41488 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06489 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32490
[email protected]695b5712012-12-06 23:55:28491 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06492 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33493 LOG(WARNING) << "Will not update extension " << id
494 << " because it is not installed or pending";
495 // Delete extension_path since we're not creating a CrxInstaller
496 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14497 if (!GetFileTaskRunner()->PostTask(
498 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36499 base::Bind(
[email protected]85df9d12014-04-15 17:02:14500 &extensions::file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36501 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03502
503 return false;
[email protected]e957fe52009-06-23 16:51:05504 }
505
[email protected]f8636f92013-08-09 21:02:37506 scoped_refptr<CrxInstaller> installer(
pkotwicz31eb1e8ec2014-10-08 15:26:46507 CrxInstaller::Create(this, scoped_ptr<ExtensionInstallPrompt>()));
[email protected]6dfbbf82010-03-12 23:09:16508 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48509 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06510 if (pending_extension_info) {
511 installer->set_install_source(pending_extension_info->install_source());
pkotwicz31eb1e8ec2014-10-08 15:26:46512 installer->set_allow_silent_install(true);
[email protected]21db9ef2014-05-16 02:06:27513 if (pending_extension_info->remote_install())
514 installer->set_grant_permissions(false);
[email protected]464213a2013-10-15 01:06:48515 creation_flags = pending_extension_info->creation_flags();
516 if (pending_extension_info->mark_acknowledged())
[email protected]374ceb6f2014-07-02 19:25:34517 external_install_manager_->AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06518 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41519 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06520 }
[email protected]7d8b7072012-04-07 01:07:46521 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45522 // its auto-update URL is from the webstore, treat it as a webstore install.
523 // Note that we ignore some older extensions with blank auto-update URLs
524 // because we are mostly concerned with restrictions on NaCl extensions,
525 // which are newer.
[email protected]75764512011-12-19 19:54:28526 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38527 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35528 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06529 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53530 creation_flags |= Extension::FROM_WEBSTORE;
531 }
532
533 // Bookmark apps being updated is kind of a contradiction, but that's because
534 // we mark the default apps as bookmark apps, and they're hosted in the web
535 // store, thus they can get updated. See http://crbug.com/101605 for more
536 // details.
537 if (extension && extension->from_bookmark())
538 creation_flags |= Extension::FROM_BOOKMARK;
539
[email protected]e33bbc22012-08-27 22:05:46540 if (extension && extension->was_installed_by_default())
541 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
542
[email protected]acc3c7c22014-03-19 06:23:39543 if (extension && extension->was_installed_by_oem())
544 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
545
[email protected]6338fa32014-07-16 21:41:59546 if (extension && extension->was_installed_by_custodian())
547 creation_flags |= Extension::WAS_INSTALLED_BY_CUSTODIAN;
548
[email protected]658eae52014-06-14 20:28:05549 if (extension) {
[email protected]411f8ae2014-05-22 11:12:23550 installer->set_is_ephemeral(extension_prefs_->IsEphemeralApp(id));
[email protected]c30bda262014-06-19 04:10:13551 installer->set_do_not_sync(extension_prefs_->DoNotSync(id));
[email protected]658eae52014-06-14 20:28:05552 }
[email protected]ab6c7be42014-01-16 02:05:54553
[email protected]a12ce8b22012-01-17 18:40:53554 installer->set_creation_flags(creation_flags);
555
[email protected]044e86992014-01-24 22:59:11556 installer->set_delete_source(file_ownership_passed);
[email protected]cb0e50312011-05-09 15:03:07557 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16558 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03559
560 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09561 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03562
563 return true;
[email protected]e957fe52009-06-23 16:51:05564}
565
[email protected]bca4b832014-07-17 20:22:34566void ExtensionService::ReloadExtensionImpl(
[email protected]757d60a2014-05-23 00:11:44567 // "transient" because the process of reloading may cause the reference
568 // to become invalid. Instead, use |extension_id|, a copy.
[email protected]bca4b832014-07-17 20:22:34569 const std::string& transient_extension_id,
570 bool be_noisy) {
[email protected]a29a517a2011-01-21 21:11:12571 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05572
573 // If the extension is already reloading, don't reload again.
[email protected]757d60a2014-05-23 00:11:44574 if (extension_prefs_->GetDisableReasons(transient_extension_id) &
[email protected]2a947202013-03-06 04:58:05575 Extension::DISABLE_RELOAD) {
576 return;
577 }
578
mlerman6a37b6a42014-11-26 22:10:53579 // Ignore attempts to reload a blacklisted or blocked extension. Sometimes
580 // this can happen in a convoluted reload sequence triggered by the
581 // termination of a blacklisted or blocked extension and a naive attempt to
582 // reload it. For an example see http://crbug.com/373842.
583 if (registry_->blacklisted_extensions().Contains(transient_extension_id) ||
584 registry_->blocked_extensions().Contains(transient_extension_id)) {
[email protected]757d60a2014-05-23 00:11:44585 return;
mlerman6a37b6a42014-11-26 22:10:53586 }
[email protected]757d60a2014-05-23 00:11:44587
[email protected]650b2d52013-02-10 03:41:45588 base::FilePath path;
[email protected]757d60a2014-05-23 00:11:44589
590 std::string extension_id = transient_extension_id;
591 const Extension* transient_current_extension =
592 GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40593
[email protected]f17dbd42010-08-16 23:21:10594 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]757d60a2014-05-23 00:11:44595 if (transient_current_extension) {
[email protected]4814b512009-11-07 00:12:29596 // If the extension has an inspector open for its background page, detach
597 // the inspector and hang onto a cookie for it, so that we can reattach
598 // later.
[email protected]31d8f5f22012-04-02 15:22:08599 // TODO(yoz): this is not incognito-safe!
reillyg0ea3fa902014-10-28 15:30:23600 extensions::ProcessManager* manager =
601 extensions::ProcessManager::Get(profile_);
[email protected]3a1dc572012-07-31 22:25:13602 extensions::ExtensionHost* host =
603 manager->GetBackgroundHostForExtension(extension_id);
[email protected]b3f957e62014-08-08 10:09:02604 if (host && DevToolsAgentHost::HasFor(host->host_contents())) {
[email protected]4814b512009-11-07 00:12:29605 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09606 scoped_refptr<DevToolsAgentHost> agent_host =
[email protected]b3f957e62014-08-08 10:09:02607 DevToolsAgentHost::GetOrCreateFor(host->host_contents());
608 agent_host->DisconnectWebContents();
[email protected]04ea1bb2013-07-10 09:26:09609 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29610 }
611
[email protected]757d60a2014-05-23 00:11:44612 path = transient_current_extension->path();
[email protected]58076192013-07-19 19:43:50613 // BeingUpgraded is set back to false when the extension is added.
rdevlin.cronin2c16c602014-11-21 01:35:49614 system_->runtime_data()->SetBeingUpgraded(transient_current_extension->id(),
[email protected]757d60a2014-05-23 00:11:44615 true);
[email protected]44d62b62012-04-11 00:06:03616 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03617 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16618 } else {
[email protected]d32c02a2014-06-16 22:58:36619 std::map<std::string, base::FilePath>::const_iterator iter =
620 unloaded_extension_paths_.find(extension_id);
621 if (iter == unloaded_extension_paths_.end()) {
622 return;
623 }
[email protected]1eb175082010-02-10 09:26:16624 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06625 }
626
[email protected]757d60a2014-05-23 00:11:44627 transient_current_extension = NULL;
628
[email protected]9f4e4f082013-06-21 07:11:19629 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48630 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05631 return;
632 }
633
[email protected]43ceb002012-02-10 23:19:15634 // If we're reloading a component extension, use the component extension
635 // loader's reloader.
636 if (component_loader_->Exists(extension_id)) {
637 component_loader_->Reload(extension_id);
638 return;
639 }
640
[email protected]e6090e42010-03-23 22:44:08641 // Check the installed extensions to see if what we're reloading was already
642 // installed.
643 scoped_ptr<ExtensionInfo> installed_extension(
644 extension_prefs_->GetInstalledExtensionInfo(extension_id));
645 if (installed_extension.get() &&
646 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01647 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08648 } else {
[email protected]d8c8f25f2011-11-02 18:18:01649 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08650 // We should always be able to remember the extension's path. If it's not in
651 // the map, someone failed to update |unloaded_extension_paths_|.
652 CHECK(!path.empty());
[email protected]bca4b832014-07-17 20:22:34653 scoped_refptr<extensions::UnpackedInstaller> unpacked_installer =
654 extensions::UnpackedInstaller::Create(this);
655 unpacked_installer->set_be_noisy_on_failure(be_noisy);
656 unpacked_installer->Load(path);
[email protected]e6090e42010-03-23 22:44:08657 }
[email protected]9cddd4702009-07-27 22:09:40658}
659
[email protected]bca4b832014-07-17 20:22:34660void ExtensionService::ReloadExtension(const std::string& extension_id) {
661 ReloadExtensionImpl(extension_id, true); // be_noisy
662}
663
664void ExtensionService::ReloadExtensionWithQuietFailure(
665 const std::string& extension_id) {
666 ReloadExtensionImpl(extension_id, false); // be_noisy
667}
668
[email protected]757d60a2014-05-23 00:11:44669bool ExtensionService::UninstallExtension(
670 // "transient" because the process of uninstalling may cause the reference
671 // to become invalid. Instead, use |extenson->id()|.
672 const std::string& transient_extension_id,
[email protected]e43c61f2014-07-20 21:46:34673 extensions::UninstallReason reason,
[email protected]42d58f62014-07-31 01:32:45674 const base::Closure& deletion_done_callback,
[email protected]757d60a2014-05-23 00:11:44675 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12676 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20677
[email protected]757d60a2014-05-23 00:11:44678 scoped_refptr<const Extension> extension =
679 GetInstalledExtension(transient_extension_id);
[email protected]631cf822009-05-15 07:01:25680
[email protected]e7afe2452010-08-22 16:19:13681 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09682 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32683
binjincccacef2014-10-13 19:00:20684 ManagementPolicy* by_policy = system_->management_policy();
[email protected]95da88c42011-03-31 10:07:33685 // Policy change which triggers an uninstall will always set
686 // |external_uninstall| to true so this is the only way to uninstall
687 // managed extensions.
[email protected]90878c52014-04-04 18:21:02688 // Shared modules being uninstalled will also set |external_uninstall| to true
689 // so that we can guarantee users don't uninstall a shared module.
690 // (crbug.com/273300)
691 // TODO(rdevlin.cronin): This is probably not right. We should do something
692 // else, like include an enum IS_INTERNAL_UNINSTALL or IS_USER_UNINSTALL so
693 // we don't do this.
[email protected]cc2f55c2014-07-08 02:19:04694 bool external_uninstall =
[email protected]e43c61f2014-07-20 21:46:34695 (reason == extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT) ||
rdevlin.cronin5f6b69d2014-09-20 01:23:35696 (reason == extensions::UNINSTALL_REASON_REINSTALL) ||
[email protected]e43c61f2014-07-20 21:46:34697 (reason == extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION) ||
[email protected]6ef60012014-07-22 19:08:12698 (reason == extensions::UNINSTALL_REASON_ORPHANED_SHARED_MODULE) ||
699 (reason == extensions::UNINSTALL_REASON_SYNC &&
700 extension->was_installed_by_custodian());
[email protected]65187152012-06-02 13:14:14701 if (!external_uninstall &&
binjincccacef2014-10-13 19:00:20702 (!by_policy->UserMayModifySettings(extension.get(), error) ||
703 by_policy->MustRemainInstalled(extension.get(), error))) {
[email protected]ad50def52011-10-19 23:17:07704 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06705 extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53706 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09707 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33708 return false;
709 }
[email protected]95da88c42011-03-31 10:07:33710
[email protected]65f173552012-06-28 22:43:58711 syncer::SyncChange sync_change;
rdevlin.cronin5f6b69d2014-09-20 01:23:35712 // Don't sync the uninstall if we're going to reinstall the extension
713 // momentarily.
714 if (extension_sync_service_ &&
715 reason != extensions::UNINSTALL_REASON_REINSTALL) {
[email protected]f8aefb132013-10-30 09:29:52716 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
717 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30718 }
719
[email protected]d820bc52014-03-13 05:35:23720 system_->install_verifier()->Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:46721
[email protected]9b217652010-10-08 22:04:23722 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08723 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09724 RecordPermissionMessagesHistogram(extension.get(),
[email protected]4c86be582014-06-04 08:39:11725 "Extensions.Permissions_Uninstall2");
[email protected]9b217652010-10-08 22:04:23726
[email protected]831aa212010-03-26 13:55:19727 // Unload before doing more cleanup to ensure that nothing is hanging on to
728 // any of these resources.
[email protected]757d60a2014-05-23 00:11:44729 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19730
[email protected]9f1087e2009-06-15 17:29:32731 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05732 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14733 if (!GetFileTaskRunner()->PostTask(
734 FROM_HERE,
[email protected]5a145e82014-05-29 22:19:07735 base::Bind(&ExtensionService::UninstallExtensionOnFileThread,
736 extension->id(),
737 profile_,
[email protected]85df9d12014-04-15 17:02:14738 install_directory_,
[email protected]5a145e82014-05-29 22:19:07739 extension->path())))
[email protected]14908b72011-04-20 06:54:36740 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32741 }
742
[email protected]42d58f62014-07-31 01:32:45743 extensions::DataDeleter::StartDeleting(
744 profile_, extension.get(), deletion_done_callback);
[email protected]0d6ec3a72011-09-02 02:09:43745
[email protected]757d60a2014-05-23 00:11:44746 UntrackTerminatedExtension(extension->id());
[email protected]211030342010-09-30 18:41:06747
748 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07749 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06750 extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:53751 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09752 content::Details<const Extension>(extension.get()));
[email protected]e43c61f2014-07-20 21:46:34753 ExtensionRegistry::Get(profile_)
754 ->TriggerOnUninstalled(extension.get(), reason);
[email protected]d6ebc9792011-04-07 18:18:33755
rdevlin.cronin5f6b69d2014-09-20 01:23:35756 if (sync_change.IsValid()) {
[email protected]757d60a2014-05-23 00:11:44757 extension_sync_service_->ProcessSyncUninstallExtension(extension->id(),
[email protected]f8aefb132013-10-30 09:29:52758 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30759 }
760
[email protected]757d60a2014-05-23 00:11:44761 delayed_installs_.Remove(extension->id());
[email protected]0db124b02012-11-07 04:55:05762
[email protected]757d60a2014-05-23 00:11:44763 extension_prefs_->OnExtensionUninstalled(
764 extension->id(), extension->location(), external_uninstall);
[email protected]d4eda592013-09-18 03:37:57765
[email protected]333b1de2011-09-12 18:28:50766 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18767 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
768
[email protected]d6ebc9792011-04-07 18:18:33769 return true;
[email protected]c10da4b02010-03-25 14:38:32770}
771
[email protected]5a145e82014-05-29 22:19:07772// static
773void ExtensionService::UninstallExtensionOnFileThread(
774 const std::string& id,
775 Profile* profile,
776 const base::FilePath& install_dir,
777 const base::FilePath& extension_path) {
778 extensions::ExtensionAssetsManager* assets_manager =
779 extensions::ExtensionAssetsManager::GetInstance();
780 assets_manager->UninstallExtension(id, profile, install_dir, extension_path);
781}
782
[email protected]c3cfb012011-04-06 22:07:35783bool ExtensionService::IsExtensionEnabled(
784 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14785 if (registry_->enabled_extensions().Contains(extension_id) ||
786 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18787 return true;
[email protected]dc9a74f72012-08-17 18:07:21788 }
[email protected]36429da2011-07-11 20:25:18789
[email protected]bb1bc9b32013-12-21 03:09:14790 if (registry_->disabled_extensions().Contains(extension_id) ||
mlerman6a37b6a42014-11-26 22:10:53791 registry_->blacklisted_extensions().Contains(extension_id) ||
792 registry_->blocked_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25793 return false;
[email protected]695b5712012-12-06 23:55:28794 }
[email protected]ad83ca242011-07-29 01:32:25795
mlerman6a37b6a42014-11-26 22:10:53796 if (block_extensions_ &&
797 CanBlockExtension(GetInstalledExtension(extension_id)))
798 return false;
799
[email protected]ad83ca242011-07-29 01:32:25800 // If the extension hasn't been loaded yet, check the prefs for it. Assume
801 // enabled unless otherwise noted.
802 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]27e528322014-05-27 20:54:30803 !extension_prefs_->IsExtensionBlacklisted(extension_id) &&
[email protected]f574c402012-12-04 23:20:31804 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35805}
806
[email protected]eaa7dd182010-12-14 11:09:00807void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12808 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20809
[email protected]06f92562011-04-29 19:27:31810 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39811 return;
[email protected]bb1bc9b32013-12-21 03:09:14812 const Extension* extension =
813 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36814
815 ManagementPolicy* policy = system_->management_policy();
816 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
817 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
818 return;
819 }
[email protected]0c6da502009-08-14 22:32:39820
[email protected]b3317ad2011-04-28 23:46:00821 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28822 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52823
[email protected]06f92562011-04-29 19:27:31824 // This can happen if sync enables an extension that is not
825 // installed yet.
826 if (!extension)
827 return;
828
[email protected]0c6da502009-08-14 22:32:39829 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14830 registry_->AddEnabled(make_scoped_refptr(extension));
831 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39832
[email protected]62d30f42009-10-01 22:36:06833 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30834
[email protected]f74d7f32012-06-19 19:22:51835 // Notify listeners that the extension was enabled.
836 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06837 extensions::NOTIFICATION_EXTENSION_ENABLED,
[email protected]f74d7f32012-06-19 19:22:51838 content::Source<Profile>(profile_),
839 content::Details<const Extension>(extension));
840
[email protected]f8aefb132013-10-30 09:29:52841 if (extension_sync_service_)
842 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:39843}
844
[email protected]44d62b62012-04-11 00:06:03845void ExtensionService::DisableExtension(
846 const std::string& extension_id,
847 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12848 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20849
[email protected]8f959f522014-08-06 06:26:28850 // The extension may have been disabled already. Just add a disable reason.
851 if (!IsExtensionEnabled(extension_id)) {
852 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52853 return;
[email protected]8f959f522014-08-06 06:26:28854 }
[email protected]1784e83a2009-09-08 21:01:52855
[email protected]06f92562011-04-29 19:27:31856 const Extension* extension = GetInstalledExtension(extension_id);
857 // |extension| can be NULL if sync disables an extension that is not
858 // installed yet.
[email protected]ab894542014-07-03 12:35:39859 // EXTERNAL_COMPONENT extensions are not generally modifiable by users, but
860 // can be uninstalled by the browser if the user sets extension-specific
861 // preferences.
[email protected]65187152012-06-02 13:14:14862 if (extension &&
[email protected]505ccdc2013-06-20 20:15:35863 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]ab894542014-07-03 12:35:39864 !system_->management_policy()->UserMayModifySettings(extension, NULL) &&
865 extension->location() != Manifest::EXTERNAL_COMPONENT) {
[email protected]95da88c42011-03-31 10:07:33866 return;
[email protected]65187152012-06-02 13:14:14867 }
[email protected]95da88c42011-03-31 10:07:33868
[email protected]b3317ad2011-04-28 23:46:00869 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28870 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52871
[email protected]599539802014-01-07 23:06:00872 int include_mask =
873 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
874 extension = registry_->GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31875 if (!extension)
876 return;
877
[email protected]45f5b7d2014-01-22 23:47:13878 // The extension is either enabled or terminated.
879 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
880 registry_->terminated_extensions().Contains(extension->id()));
[email protected]64742de32013-04-22 08:44:34881
[email protected]5c094792012-09-07 19:44:53882 // Move it over to the disabled list. Don't send a second unload notification
883 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:14884 registry_->AddDisabled(make_scoped_refptr(extension));
885 if (registry_->enabled_extensions().Contains(extension->id())) {
886 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:13887 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:53888 } else {
[email protected]bb1bc9b32013-12-21 03:09:14889 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:53890 }
[email protected]3bdba0d2011-08-23 07:17:30891
[email protected]f8aefb132013-10-30 09:29:52892 if (extension_sync_service_)
893 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:52894}
895
[email protected]1abf05e2013-07-09 17:04:36896void ExtensionService::DisableUserExtensions(
897 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14898 extensions::ManagementPolicy* management_policy =
899 system_->management_policy();
900 extensions::ExtensionList to_disable;
901
[email protected]bb1bc9b32013-12-21 03:09:14902 const ExtensionSet& enabled_set = registry_->enabled_extensions();
903 for (ExtensionSet::const_iterator extension = enabled_set.begin();
904 extension != enabled_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39905 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14906 to_disable.push_back(*extension);
907 }
[email protected]bb1bc9b32013-12-21 03:09:14908 const ExtensionSet& terminated_set = registry_->terminated_extensions();
909 for (ExtensionSet::const_iterator extension = terminated_set.begin();
910 extension != terminated_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39911 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14912 to_disable.push_back(*extension);
913 }
914
915 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
916 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:38917 if ((*extension)->was_installed_by_default() &&
918 extension_urls::IsWebstoreUpdateUrl(
dchengc7047942014-08-26 05:05:31919 extensions::ManifestURL::GetUpdateURL(extension->get())))
[email protected]3082fe32013-08-06 11:12:38920 continue;
[email protected]1abf05e2013-07-09 17:04:36921 const std::string& id = (*extension)->id();
922 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
923 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:14924 }
925}
926
mlerman6a37b6a42014-11-26 22:10:53927// Extensions that are not locked, components or forced by policy should be
928// locked. Extensions are no longer considered enabled or disabled. Blacklisted
929// extensions are now considered both blacklisted and locked.
930void ExtensionService::BlockAllExtensions() {
931 if (block_extensions_)
932 return;
933 block_extensions_ = true;
934
935 // Blacklisted extensions are already unloaded, need not be blocked.
936 scoped_ptr<ExtensionSet> extensions =
937 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::ENABLED |
938 ExtensionRegistry::DISABLED |
939 ExtensionRegistry::TERMINATED);
940
941 for (const scoped_refptr<const Extension>& extension : *extensions) {
942 const std::string& id = extension->id();
943
944 if (!CanBlockExtension(extension.get()))
945 continue;
946
947 registry_->RemoveEnabled(id);
948 registry_->RemoveDisabled(id);
949 registry_->RemoveTerminated(id);
950
951 registry_->AddBlocked(extension.get());
952 UnloadExtension(id, extensions::UnloadedExtensionInfo::REASON_LOCK_ALL);
953 }
954}
955
956// All locked extensions should revert to being either enabled or disabled
957// as appropriate.
958void ExtensionService::UnblockAllExtensions() {
959 block_extensions_ = false;
960 scoped_ptr<ExtensionSet> to_unblock =
961 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::BLOCKED);
962
963 for (const scoped_refptr<const Extension>& extension : *to_unblock) {
964 registry_->RemoveBlocked(extension->id());
965 AddExtension(extension.get());
966 }
967}
968
[email protected]eaa7dd182010-12-14 11:09:00969void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:28970 const Extension* extension) {
971 GrantPermissions(extension);
[email protected]4c86be582014-06-04 08:39:11972 RecordPermissionMessagesHistogram(extension,
973 "Extensions.Permissions_ReEnable2");
[email protected]8d888c12010-11-30 00:00:25974 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
975 EnableExtension(extension->id());
976}
977
[email protected]009633c2013-03-07 22:08:28978void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:45979 CHECK(extension);
[email protected]8d42db52014-06-20 21:50:50980 extensions::PermissionsUpdater(profile()).GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:45981}
982
[email protected]fe2dd7742011-04-19 22:52:49983// static
984void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:05985 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:17986 // Since this is called from multiple sources, and since the histogram macros
987 // use statics, we need to manually lookup the histogram ourselves.
988 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:49989 histogram,
990 1,
[email protected]c2e66e12012-06-27 06:27:06991 PermissionMessage::kEnumBoundary,
992 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:17993 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:49994
[email protected]a6910e72014-06-06 05:04:36995 PermissionMessages permissions =
996 extension->permissions_data()->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49997 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:06998 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49999 } else {
[email protected]c2e66e12012-06-27 06:27:061000 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521001 it != permissions.end(); ++it)
1002 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491003 }
1004}
1005
[email protected]eaa7dd182010-12-14 11:09:001006void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]f9c8c7c72014-07-31 16:42:311007 // The URLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061008 // was loaded, otherwise a race can arise where a renderer that is created
1009 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421010 // that the request context doesn't yet know about. The profile is responsible
1011 // for ensuring its URLRequestContexts appropriately discover the loaded
1012 // extension.
[email protected]31d8f5f22012-04-02 15:22:081013 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061014
[email protected]d5949312012-12-03 22:13:301015 // Tell renderers about the new extension, unless it's a theme (renderers
1016 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191017 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491018 for (content::RenderProcessHost::iterator i(
1019 content::RenderProcessHost::AllHostsIterator());
1020 !i.IsAtEnd(); i.Advance()) {
1021 content::RenderProcessHost* host = i.GetCurrentValue();
1022 Profile* host_profile =
1023 Profile::FromBrowserContext(host->GetBrowserContext());
1024 if (host_profile->GetOriginalProfile() ==
1025 profile_->GetOriginalProfile()) {
1026 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1027 1, ExtensionMsg_Loaded_Params(extension));
1028 host->Send(
1029 new ExtensionMsg_Loaded(loaded_extensions));
1030 }
[email protected]c8d407e2011-04-28 21:27:171031 }
[email protected]77a6970c2011-04-23 16:58:561032 }
1033
[email protected]3442a662012-01-12 08:06:171034 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1035 // extension.
1036 //
1037 // NOTE: It is important that this happen after notifying the renderers about
1038 // the new extensions so that if we navigate to an extension URL in
[email protected]fdc76192014-04-20 21:54:411039 // ExtensionRegistryObserver::OnLoaded or
1040 // NOTIFICATION_EXTENSION_LOADED_DEPRECATED, the
[email protected]dcc47642014-03-26 22:03:491041 // renderer is guaranteed to know about it.
1042 registry_->TriggerOnLoaded(extension);
1043
[email protected]3442a662012-01-12 08:06:171044 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061045 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]3442a662012-01-12 08:06:171046 content::Source<Profile>(profile_),
1047 content::Details<const Extension>(extension));
1048
[email protected]dcc47642014-03-26 22:03:491049 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1050 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171051 profile_->GetExtensionSpecialStoragePolicy()->
rdevlin.croninbc8979a2014-09-30 01:08:501052 GrantRightsForExtension(extension, profile_);
[email protected]c8d407e2011-04-28 21:27:171053
[email protected]dcc47642014-03-26 22:03:491054 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1055 // work properly multi-profile. Besides which, it should be using
1056 // ExtensionRegistryObserver. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171057 UpdateActiveExtensionsInCrashReporter();
1058
[email protected]d695bb12014-06-05 16:16:301059 const extensions::PermissionsData* permissions_data =
[email protected]a6910e72014-06-06 05:04:361060 extension->permissions_data();
[email protected]d695bb12014-06-05 16:16:301061
[email protected]c8d407e2011-04-28 21:27:171062 // If the extension has permission to load chrome://favicon/ resources we need
1063 // to make sure that the FaviconSource is registered with the
1064 // ChromeURLDataManager.
[email protected]d695bb12014-06-05 16:16:301065 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171066 FaviconSource* favicon_source = new FaviconSource(profile_,
1067 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531068 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171069 }
[email protected]b07e606e2012-09-15 20:16:151070
[email protected]b07e606e2012-09-15 20:16:151071 // Same for chrome://theme/ resources.
[email protected]d695bb12014-06-05 16:16:301072 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151073 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531074 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151075 }
[email protected]b07e606e2012-09-15 20:16:151076
[email protected]5eddc3e2011-10-26 04:33:311077 // Same for chrome://thumb/ resources.
[email protected]d695bb12014-06-05 16:16:301078 if (permissions_data->HasHostPermission(
1079 GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511080 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531081 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311082 }
[email protected]62d30f42009-10-01 22:36:061083}
1084
[email protected]a9f39a312010-12-23 22:14:271085void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591086 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131087 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271088 UnloadedExtensionInfo details(extension, reason);
[email protected]e51232f32014-04-18 20:05:361089
1090 registry_->TriggerOnUnloaded(extension, reason);
1091
[email protected]ad50def52011-10-19 23:17:071092 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061093 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:531094 content::Source<Profile>(profile_),
1095 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061096
[email protected]f3b1a082011-11-18 00:34:301097 for (content::RenderProcessHost::iterator i(
1098 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561099 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301100 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081101 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301102 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081103 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171104 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561105 }
1106
[email protected]31d8f5f22012-04-02 15:22:081107 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]dcc47642014-03-26 22:03:491108
1109 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1110 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171111 profile_->GetExtensionSpecialStoragePolicy()->
1112 RevokeRightsForExtension(extension);
1113
[email protected]b777b332011-04-16 04:01:081114#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231115 // Revoke external file access for the extension from its file system context.
1116 // It is safe to access the extension's storage partition at this point. The
1117 // storage partition may get destroyed only after the extension gets unloaded.
[email protected]3a746ec2014-03-15 05:30:561118 GURL site =
1119 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
[email protected]cd501a72014-08-22 19:58:311120 storage::FileSystemContext* filesystem_context =
1121 BrowserContext::GetStoragePartitionForSite(profile_, site)
1122 ->GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321123 if (filesystem_context && filesystem_context->external_backend()) {
1124 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171125 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061126 }
[email protected]c8d407e2011-04-28 21:27:171127#endif
1128
[email protected]dcc47642014-03-26 22:03:491129 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1130 // work properly multi-profile. Besides which, it should be using
1131 // ExtensionRegistryObserver::OnExtensionLoaded. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171132 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061133}
1134
[email protected]599539802014-01-07 23:06:001135content::BrowserContext* ExtensionService::GetBrowserContext() const {
1136 // Implemented in the .cc file to avoid adding a profile.h dependency to
1137 // extension_service.h.
1138 return profile_;
1139}
1140
[email protected]25ae0152011-11-18 14:40:021141bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351142 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021143}
1144
[email protected]7f8f24f2012-11-15 19:40:141145base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091146 if (file_task_runner_.get())
1147 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141148
1149 // We should be able to interrupt any part of extension install process during
1150 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1151 // will be ignored/deleted while we will block on started tasks.
1152 std::string token("ext_install-");
1153 token.append(profile_->GetPath().AsUTF8Unsafe());
1154 file_task_runner_ = BrowserThread::GetBlockingPool()->
1155 GetSequencedTaskRunnerWithShutdownBehavior(
1156 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1157 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091158 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141159}
1160
[email protected]4ee07c62012-08-21 12:40:421161void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361162 std::vector<std::string> to_unload;
1163 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281164
[email protected]0d54b682013-11-05 14:15:361165 // Loop through the extensions list, finding extensions we need to unload or
1166 // disable.
[email protected]bb1bc9b32013-12-21 03:09:141167 const ExtensionSet& extensions = registry_->enabled_extensions();
1168 for (ExtensionSet::const_iterator iter = extensions.begin();
1169 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361170 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421171 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361172 to_unload.push_back(extension->id());
1173 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1174 if (system_->management_policy()->MustRemainDisabled(
1175 extension, &disable_reason, NULL))
1176 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251177 }
1178
[email protected]0d54b682013-11-05 14:15:361179 for (size_t i = 0; i < to_unload.size(); ++i)
1180 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1181
1182 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1183 to_disable.begin(); i != to_disable.end(); ++i)
1184 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251185}
1186
[email protected]31206602011-04-13 23:07:321187void ExtensionService::CheckForUpdatesSoon() {
[email protected]90878c52014-04-04 18:21:021188 // This can legitimately happen in unit tests.
1189 if (!updater_.get())
1190 return;
1191
1192 if (AreAllExternalProvidersReady()) {
1193 updater_->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351194 } else {
[email protected]90878c52014-04-04 18:21:021195 // Sync can start updating before all the external providers are ready
1196 // during startup. Start the update as soon as those providers are ready,
1197 // but not before.
1198 update_once_all_providers_are_ready_ = true;
[email protected]c3cfb012011-04-06 22:07:351199 }
1200}
1201
[email protected]8e4560b62011-01-14 10:09:141202// Some extensions will autoupdate themselves externally from Chrome. These
1203// are typically part of some larger client application package. To support
1204// these, the extension will register its location in the the preferences file
1205// (and also, on Windows, in the registry) and this code will periodically
1206// check that location for a .crx file, which it will then install locally if
1207// a new version is available.
1208// Errors are reported through ExtensionErrorReporter. Succcess is not
1209// reported.
[email protected]eaa7dd182010-12-14 11:09:001210void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121211 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141212
1213 // Note that this installation is intentionally silent (since it didn't
1214 // go through the front-end). Extensions that are registered in this
1215 // way are effectively considered 'pre-bundled', and so implicitly
1216 // trusted. In general, if something has HKLM or filesystem access,
1217 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121218
[email protected]8e4560b62011-01-14 10:09:141219 // Ask each external extension provider to give us a call back for each
1220 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271221 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141222 for (i = external_extension_providers_.begin();
1223 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271224 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141225 provider->VisitRegisteredExtension();
1226 }
1227
[email protected]50067e52011-10-20 23:17:071228 // Do any required work that we would have done after completion of all
1229 // providers.
[email protected]373daf972014-04-10 01:50:441230 if (external_extension_providers_.empty())
[email protected]50067e52011-10-20 23:17:071231 OnAllExternalProvidersReady();
[email protected]9f1087e2009-06-15 17:29:321232}
1233
[email protected]50067e52011-10-20 23:17:071234void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271235 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121236 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071237 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121238
1239 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141240 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301241 if (AreAllExternalProvidersReady())
1242 OnAllExternalProvidersReady();
1243}
1244
1245bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271246 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141247 for (i = external_extension_providers_.begin();
1248 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301249 if (!i->get()->IsReady())
1250 return false;
[email protected]8e4560b62011-01-14 10:09:141251 }
[email protected]94fde232012-04-27 10:22:301252 return true;
[email protected]50067e52011-10-20 23:17:071253}
1254
1255void ExtensionService::OnAllExternalProvidersReady() {
1256 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301257 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1258 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071259
1260 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301261 if (update_once_all_providers_are_ready_ && updater()) {
1262 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141263 extensions::ExtensionUpdater::CheckParams params;
1264 params.callback = external_updates_finished_callback_;
1265 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141266 }
1267
1268 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231269 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141270 extension_prefs_->GetInstalledExtensionsInfo());
1271 for (size_t i = 0; i < extensions_info->size(); ++i) {
1272 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401273 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141274 CheckExternalUninstall(info->extension_id);
1275 }
[email protected]e5af875f2011-10-10 21:09:141276
[email protected]373daf972014-04-10 01:50:441277 error_controller_->ShowErrorIfNeeded();
[email protected]a9aa5932012-01-25 08:27:401278
[email protected]374ceb6f2014-07-02 19:25:341279 external_install_manager_->UpdateExternalExtensionAlert();
[email protected]e5af875f2011-10-10 21:09:141280}
1281
[email protected]a9f39a312010-12-23 22:14:271282void ExtensionService::UnloadExtension(
1283 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131284 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091285 // Make sure the extension gets deleted after we return from this function.
[email protected]599539802014-01-07 23:06:001286 int include_mask =
1287 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
[email protected]9adb9692010-10-29 23:14:021288 scoped_refptr<const Extension> extension(
[email protected]599539802014-01-07 23:06:001289 registry_->GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251290
[email protected]fa0c96732010-11-17 00:14:231291 // This method can be called via PostTask, so the extension may have been
1292 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091293 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171294 // In case the extension may have crashed/uninstalled. Allow the profile to
1295 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081296 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231297 return;
[email protected]dd163fb02011-05-04 22:22:171298 }
[email protected]0c6da502009-08-14 22:32:391299
[email protected]1eb175082010-02-10 09:26:161300 // Keep information about the extension so that we can reload it later
1301 // even if it's not permanently installed.
1302 unloaded_extension_paths_[extension->id()] = extension->path();
1303
[email protected]f17dbd42010-08-16 23:21:101304 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031305 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101306
[email protected]bb1bc9b32013-12-21 03:09:141307 if (registry_->disabled_extensions().Contains(extension->id())) {
1308 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171309 // Make sure the profile cleans up its RequestContexts when an already
1310 // disabled extension is unloaded (since they are also tracking the disabled
1311 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081312 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]45f5b7d2014-01-22 23:47:131313 // Don't send the unloaded notification. It was sent when the extension
1314 // was disabled.
[email protected]60a174a2013-08-02 20:29:281315 } else {
[email protected]bb1bc9b32013-12-21 03:09:141316 // Remove the extension from the enabled list.
1317 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281318 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391319 }
1320
[email protected]fb789532013-08-27 02:40:211321 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061322 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:211323 content::Source<Profile>(profile_),
1324 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251325}
1326
[email protected]8b1ec202013-09-05 02:09:501327void ExtensionService::RemoveComponentExtension(
1328 const std::string& extension_id) {
1329 scoped_refptr<const Extension> extension(
1330 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131331 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]ebe07772014-05-22 04:16:061332 if (extension.get()) {
1333 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061334 extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
[email protected]ebe07772014-05-22 04:16:061335 content::Source<Profile>(profile_),
1336 content::Details<const Extension>(extension.get()));
[email protected]e43c61f2014-07-20 21:46:341337 ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(
1338 extension.get(), extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT);
[email protected]ebe07772014-05-22 04:16:061339 }
[email protected]8b1ec202013-09-05 02:09:501340}
1341
[email protected]bb1bc9b32013-12-21 03:09:141342void ExtensionService::UnloadAllExtensionsForTest() {
1343 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321344}
1345
[email protected]bb1bc9b32013-12-21 03:09:141346void ExtensionService::ReloadExtensionsForTest() {
1347 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1348 // warning about calling test code in production.
1349 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451350 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011351 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351352 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1353 // times.
[email protected]9f1087e2009-06-15 17:29:321354}
1355
[email protected]820d9bd2013-04-03 03:46:031356void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351357 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031358 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061359 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
[email protected]820d9bd2013-04-03 03:46:031360 content::Source<Profile>(profile_),
1361 content::NotificationService::NoDetails());
1362}
1363
[email protected]eaa7dd182010-12-14 11:09:001364void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111365 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171366 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501367
[email protected]bc151cf92013-02-12 04:57:261368 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511369}
1370
[email protected]fcb58a862012-05-01 01:03:151371void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171372 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1373 // default extension state to DISABLED when the --disable-extensions flag
1374 // is set (http://crbug.com/29067).
1375 if (!extensions_enabled() &&
1376 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401377 extension->location() != Manifest::COMPONENT &&
1378 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151379 return;
[email protected]330840c2012-08-29 22:21:011380 }
[email protected]c8d407e2011-04-28 21:27:171381
[email protected]b914e2952013-04-26 07:10:031382 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171383 bool is_extension_installed = false;
1384 const Extension* old = GetInstalledExtension(extension->id());
1385 if (old) {
1386 is_extension_installed = true;
1387 int version_compare_result =
1388 extension->version()->CompareTo(*(old->version()));
1389 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031390 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1391 // that we aren't downgrading.
1392 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171393 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031394 }
rdevlin.cronin2c16c602014-11-21 01:35:491395 // If the extension was disabled for a reload, then enable it.
1396 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
1397
1398 // Set the upgraded bit; we consider reloads upgrades.
1399 system_->runtime_data()->SetBeingUpgraded(extension->id(),
1400 is_extension_upgrade || reloading);
[email protected]c8d407e2011-04-28 21:27:171401
[email protected]1eb175082010-02-10 09:26:161402 // The extension is now loaded, remove its data from unloaded extension map.
1403 unloaded_extension_paths_.erase(extension->id());
1404
[email protected]bb7f40952011-01-13 00:21:201405 // If a terminated extension is loaded, remove it from the terminated list.
1406 UntrackTerminatedExtension(extension->id());
1407
[email protected]b914e2952013-04-26 07:10:031408 // Check if the extension's privileges have changed and mark the
1409 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171410 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031411
[email protected]116d40e2013-08-08 17:23:171412 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411413 // To upgrade an extension in place, unload the old one and then load the
1414 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131415 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031416 }
[email protected]0c6da502009-08-14 22:32:391417
[email protected]695b5712012-12-06 23:55:281418 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091419 // Only prefs is checked for the blacklist. We rely on callers to check the
1420 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071421 // installation then threads through the install and pending install flow
1422 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141423 registry_->AddBlacklisted(extension);
mlerman6a37b6a42014-11-26 22:10:531424 } else if (block_extensions_ && CanBlockExtension(extension)) {
1425 registry_->AddBlocked(extension);
[email protected]ca2e82022013-04-29 16:48:411426 } else if (!reloading &&
1427 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141428 registry_->AddDisabled(extension);
[email protected]f8aefb132013-10-30 09:29:521429 if (extension_sync_service_)
1430 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031431 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061432 extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]44d62b62012-04-11 00:06:031433 content::Source<Profile>(profile_),
1434 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441435
[email protected]21db9ef2014-05-16 02:06:271436 // Show the extension disabled error if a permissions increase or a remote
1437 // installation is the reason it was disabled, and no other reasons exist.
1438 int reasons = extension_prefs_->GetDisableReasons(extension->id());
1439 const int kReasonMask = Extension::DISABLE_PERMISSIONS_INCREASE |
1440 Extension::DISABLE_REMOTE_INSTALL;
1441 if (reasons & kReasonMask && !(reasons & ~kReasonMask)) {
1442 extensions::AddExtensionDisabledError(
1443 this,
1444 extension,
1445 extension_prefs_->HasDisableReason(
1446 extension->id(), Extension::DISABLE_REMOTE_INSTALL));
[email protected]62f051c2012-03-29 17:04:441447 }
[email protected]ca2e82022013-04-29 16:48:411448 } else if (reloading) {
1449 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141450 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411451 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281452 } else {
1453 // All apps that are displayed in the launcher are ordered by their ordinals
1454 // so we must ensure they have valid ordinals.
1455 if (extension->RequiresSortOrdinal()) {
[email protected]db378322014-07-21 23:19:341456 extension_prefs_->app_sorting()->SetExtensionVisible(
1457 extension->id(),
1458 extension->ShouldDisplayInNewTabPage() &&
1459 !extension_prefs_->IsEphemeralApp(extension->id()));
1460 if (!extension_prefs_->IsEphemeralApp(extension->id())) {
1461 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1462 extension->id(), syncer::StringOrdinal());
[email protected]bd36b652013-02-27 02:13:251463 }
[email protected]695b5712012-12-06 23:55:281464 }
1465
[email protected]bb1bc9b32013-12-21 03:09:141466 registry_->AddEnabled(extension);
[email protected]f8aefb132013-10-30 09:29:521467 if (extension_sync_service_)
1468 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281469 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311470 }
rdevlin.cronin2c16c602014-11-21 01:35:491471 system_->runtime_data()->SetBeingUpgraded(extension->id(), false);
[email protected]aab98a52009-12-02 03:22:351472}
1473
[email protected]8c484b742012-11-29 06:05:361474void ExtensionService::AddComponentExtension(const Extension* extension) {
1475 const std::string old_version_string(
1476 extension_prefs_->GetVersionString(extension->id()));
[email protected]c5e4a2222014-01-03 16:06:131477 const Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361478
[email protected]0181a7d2013-11-12 01:08:421479 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361480 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1481 VLOG(1) << "Component extension " << extension->name() << " ("
1482 << extension->id() << ") installing/upgrading from '"
1483 << old_version_string << "' to " << extension->version()->GetString();
1484
1485 AddNewOrUpdatedExtension(extension,
[email protected]9ae73962014-08-14 16:53:411486 Extension::ENABLED,
[email protected]4a1d9c0d2014-06-13 12:50:111487 extensions::kInstallFlagNone,
[email protected]d8fd0fd2014-03-24 13:16:061488 syncer::StringOrdinal(),
1489 std::string());
[email protected]8c484b742012-11-29 06:05:361490 return;
1491 }
1492
1493 AddExtension(extension);
1494}
1495
[email protected]b914e2952013-04-26 07:10:031496void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171497 bool is_extension_installed) {
[email protected]23a85362014-07-07 23:26:191498 extensions::PermissionsUpdater(profile_).InitializePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311499
[email protected]8d888c12010-11-30 00:00:251500 // We keep track of all permissions the user has granted each extension.
1501 // This allows extensions to gracefully support backwards compatibility
1502 // by including unknown permissions in their manifests. When the user
1503 // installs the extension, only the recognized permissions are recorded.
1504 // When the unknown permissions become recognized (e.g., through browser
1505 // upgrade), we can prompt the user to accept these new permissions.
1506 // Extensions can also silently upgrade to less permissions, and then
1507 // silently upgrade to a version that adds these permissions back.
1508 //
1509 // For example, pretend that Chrome 10 includes a permission "omnibox"
1510 // for an API that adds suggestions to the omnibox. An extension can
1511 // maintain backwards compatibility while still having "omnibox" in the
1512 // manifest. If a user installs the extension on Chrome 9, the browser
1513 // will record the permissions it recognized, not including "omnibox."
1514 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1515 // will disable the extension and prompt the user to approve the increase
1516 // in privileges. The extension could then release a new version that
1517 // removes the "omnibox" permission. When the user upgrades, Chrome will
1518 // still remember that "omnibox" had been granted, so that if the
1519 // extension once again includes "omnibox" in an upgrade, the extension
1520 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281521 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251522
[email protected]211a3f32013-05-28 21:48:181523 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361524 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]45928b682013-11-28 08:20:271525 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031526 // Silently grant all active permissions to default apps only on install.
1527 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181528 // Silently grant all active permissions to apps install in kiosk mode on both
1529 // install and update.
1530 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031531 GrantPermissions(extension);
1532
1533 bool is_privilege_increase = false;
1534 // We only need to compare the granted permissions to the current permissions
1535 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051536 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181537 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251538 // Add all the recognized permissions if the granted permissions list
1539 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061540 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311541 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521542 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251543
1544 // Here, we check if an extension's privileges have increased in a manner
1545 // that requires the user's approval. This could occur because the browser
1546 // upgraded and recognized additional privileges, or an extension upgrades
1547 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251548 is_privilege_increase =
1549 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
dchengc7047942014-08-26 05:05:311550 granted_permissions.get(),
[email protected]076ebeda2014-06-06 21:47:261551 extension->permissions_data()->active_permissions().get(),
1552 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251553 }
1554
[email protected]116d40e2013-08-08 17:23:171555 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031556 // If the extension was already disabled, suppress any alerts for becoming
1557 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031558 bool previously_disabled =
1559 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191560 // Legacy disabled extensions do not have a disable reason. Infer that if
1561 // there was no permission increase, it was likely disabled by the user.
1562 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031563 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191564 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031565 }
[email protected]7e9e2422012-12-13 19:54:191566 // Extensions that came to us disabled from sync need a similar inference,
1567 // except based on the new version's permissions.
1568 if (previously_disabled &&
1569 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1570 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1571 extension_prefs_->ClearDisableReasons(extension->id());
1572 if (!is_privilege_increase)
1573 disable_reasons |= Extension::DISABLE_USER_ACTION;
1574 }
[email protected]b914e2952013-04-26 07:10:031575 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251576 }
1577
1578 // Extension has changed permissions significantly. Disable it. A
[email protected]21db9ef2014-05-16 02:06:271579 // notification should be sent by the caller. If the extension is already
1580 // disabled because it was installed remotely, don't add another disable
1581 // reason, but instead always set the "did escalate permissions" flag, to
1582 // ensure enabling it will always show a warning.
1583 if (disable_reasons == Extension::DISABLE_REMOTE_INSTALL) {
1584 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1585 } else if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191586 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491587 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]4c86be582014-06-04 08:39:111588 RecordPermissionMessagesHistogram(extension,
1589 "Extensions.Permissions_AutoDisable2");
[email protected]fe2dd7742011-04-19 22:52:491590 }
[email protected]195c1582013-10-29 18:42:231591 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251592 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031593 }
1594 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281595 extension_prefs_->AddDisableReason(
1596 extension->id(),
1597 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251598 }
1599}
1600
[email protected]eaa7dd182010-12-14 11:09:001601void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391602 std::set<std::string> extension_ids;
[email protected]bb1bc9b32013-12-21 03:09:141603 const ExtensionSet& extensions = registry_->enabled_extensions();
1604 for (ExtensionSet::const_iterator iter = extensions.begin();
1605 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361606 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401607 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461608 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351609 }
1610
[email protected]dcc47642014-03-26 22:03:491611 // TODO(kalman): This is broken. ExtensionService is per-profile.
1612 // crash_keys::SetActiveExtensions is per-process. See
1613 // http://crbug.com/355029.
[email protected]f6431be82013-09-07 02:53:451614 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251615}
1616
[email protected]8266d662011-07-12 21:53:261617void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:501618 const Extension* extension,
[email protected]98270432012-09-11 20:51:241619 const syncer::StringOrdinal& page_ordinal,
[email protected]4a1d9c0d2014-06-13 12:50:111620 int install_flags) {
[email protected]a29a517a2011-01-21 21:11:121621 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201622
[email protected]c3cfb012011-04-06 22:07:351623 const std::string& id = extension->id();
binjin47947f842014-11-18 12:10:241624 int disable_reasons = GetDisableReasonsOnInstalled(extension);
[email protected]d8fd0fd2014-03-24 13:16:061625 std::string install_parameter;
[email protected]96aebe22014-07-16 04:07:511626 const extensions::PendingExtensionInfo* pending_extension_info =
1627 pending_extension_manager()->GetById(id);
1628 if (pending_extension_info) {
[email protected]8f3bcbd2013-06-05 08:42:401629 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:061630 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321631
[email protected]31bb5ee62012-09-12 22:58:401632 LOG(WARNING) << "ShouldAllowInstall() returned false for "
1633 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:221634 << " and update URL "
1635 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:401636 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541637
[email protected]4416c5a2010-06-26 01:28:571638 // Delete the extension directory since we're not going to
1639 // load it.
[email protected]7f8f24f2012-11-15 19:40:141640 if (!GetFileTaskRunner()->PostTask(
1641 FROM_HERE,
[email protected]85df9d12014-04-15 17:02:141642 base::Bind(&extensions::file_util::DeleteFile,
1643 extension->path(),
1644 true))) {
[email protected]14908b72011-04-20 06:54:361645 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401646 }
[email protected]4416c5a2010-06-26 01:28:571647 return;
1648 }
[email protected]51a3bf8b2012-06-08 22:53:061649
[email protected]d8fd0fd2014-03-24 13:16:061650 install_parameter = pending_extension_info->install_parameter();
[email protected]51a3bf8b2012-06-08 22:53:061651 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:571652 } else {
[email protected]c3cfb012011-04-06 22:07:351653 // We explicitly want to re-enable an uninstalled external
1654 // extension; if we're here, that means the user is manually
1655 // installing the extension.
[email protected]46f3e102014-03-25 01:22:451656 if (extension_prefs_->IsExternalExtensionUninstalled(id)) {
binjin47947f842014-11-18 12:10:241657 disable_reasons = Extension::DISABLE_NONE;
[email protected]6cc7dbae2011-04-29 21:18:331658 }
[email protected]aa142702010-03-26 01:26:331659 }
1660
[email protected]98270432012-09-11 20:51:241661 // Unsupported requirements overrides the management policy.
[email protected]4a1d9c0d2014-06-13 12:50:111662 if (install_flags & extensions::kInstallFlagHasRequirementErrors) {
binjin47947f842014-11-18 12:10:241663 disable_reasons |= Extension::DISABLE_UNSUPPORTED_REQUIREMENT;
[email protected]98270432012-09-11 20:51:241664 // If the extension was disabled because of unsupported requirements but
1665 // now supports all requirements after an update and there are not other
1666 // disable reasons, enable it.
1667 } else if (extension_prefs_->GetDisableReasons(id) ==
binjin47947f842014-11-18 12:10:241668 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
1669 disable_reasons = Extension::DISABLE_NONE;
[email protected]98270432012-09-11 20:51:241670 extension_prefs_->ClearDisableReasons(id);
1671 }
1672
[email protected]4a1d9c0d2014-06-13 12:50:111673 if (install_flags & extensions::kInstallFlagIsBlacklistedForMalware) {
[email protected]9f3c8532013-07-31 19:52:071674 // Installation of a blacklisted extension can happen from sync, policy,
1675 // etc, where to maintain consistency we need to install it, just never
1676 // load it (see AddExtension). Usually it should be the job of callers to
1677 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
1678 // showing the install dialogue).
[email protected]7c82539c2014-02-19 06:09:171679 extension_prefs_->AcknowledgeBlacklistedExtension(id);
[email protected]9f3c8532013-07-31 19:52:071680 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
1681 extension->location(),
1682 Manifest::NUM_LOCATIONS);
1683 }
1684
[email protected]695b5712012-12-06 23:55:281685 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:551686 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
1687 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:301688 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:401689 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]4c86be582014-06-04 08:39:111690 RecordPermissionMessagesHistogram(extension,
1691 "Extensions.Permissions_Install2");
[email protected]69084dc2012-11-17 07:39:301692 } else {
1693 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
1694 extension->GetType(), 100);
1695 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:401696 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]8f959f522014-08-06 06:26:281697
1698 // A fully installed app cannot be demoted to an ephemeral app.
1699 if ((install_flags & extensions::kInstallFlagIsEphemeral) &&
1700 !extension_prefs_->IsEphemeralApp(id)) {
1701 install_flags &= ~static_cast<int>(extensions::kInstallFlagIsEphemeral);
1702 }
[email protected]07533022011-06-27 20:42:551703 }
1704
binjin47947f842014-11-18 12:10:241705 if (disable_reasons)
1706 extension_prefs_->AddDisableReasons(id, disable_reasons);
1707
[email protected]399583b2012-12-11 09:33:421708 const Extension::State initial_state =
binjin47947f842014-11-18 12:10:241709 disable_reasons == Extension::DISABLE_NONE ? Extension::ENABLED
1710 : Extension::DISABLED;
1711
[email protected]4a1d9c0d2014-06-13 12:50:111712 if (ShouldDelayExtensionUpdate(
1713 id,
1714 !!(install_flags & extensions::kInstallFlagInstallImmediately))) {
[email protected]9f3c8532013-07-31 19:52:071715 extension_prefs_->SetDelayedInstallInfo(
1716 extension,
1717 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111718 install_flags,
[email protected]9f3c8532013-07-31 19:52:071719 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
[email protected]d8fd0fd2014-03-24 13:16:061720 page_ordinal,
1721 install_parameter);
[email protected]0db124b02012-11-07 04:55:051722
1723 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:191724 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:051725
[email protected]75bdcb872013-03-13 00:41:451726 // Notify observers that app update is available.
1727 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:061728 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:051729 return;
1730 }
1731
[email protected]90878c52014-04-04 18:21:021732 extensions::SharedModuleService::ImportStatus status =
1733 shared_module_service_->SatisfyImports(extension);
[email protected]e5f8fffe2014-04-02 00:30:441734 if (installs_delayed_for_gc_) {
[email protected]9f3c8532013-07-31 19:52:071735 extension_prefs_->SetDelayedInstallInfo(
1736 extension,
1737 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111738 install_flags,
[email protected]9f3c8532013-07-31 19:52:071739 extensions::ExtensionPrefs::DELAY_REASON_GC,
[email protected]d8fd0fd2014-03-24 13:16:061740 page_ordinal,
1741 install_parameter);
[email protected]399583b2012-12-11 09:33:421742 delayed_installs_.Insert(extension);
[email protected]90878c52014-04-04 18:21:021743 } else if (status != SharedModuleService::IMPORT_STATUS_OK) {
1744 if (status == SharedModuleService::IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:071745 extension_prefs_->SetDelayedInstallInfo(
1746 extension,
1747 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111748 install_flags,
[email protected]9f4e4f082013-06-21 07:11:191749 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
[email protected]d8fd0fd2014-03-24 13:16:061750 page_ordinal,
1751 install_parameter);
[email protected]9f4e4f082013-06-21 07:11:191752 delayed_installs_.Insert(extension);
1753 }
[email protected]399583b2012-12-11 09:33:421754 } else {
[email protected]9f3c8532013-07-31 19:52:071755 AddNewOrUpdatedExtension(extension,
1756 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111757 install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061758 page_ordinal,
1759 install_parameter);
[email protected]399583b2012-12-11 09:33:421760 }
[email protected]8c484b742012-11-29 06:05:361761}
1762
binjin1569c9b2014-09-05 13:33:181763void ExtensionService::OnExtensionManagementSettingsChanged() {
1764 error_controller_->ShowErrorIfNeeded();
binjine6b58b52014-10-31 01:55:571765
1766 // Revokes blocked permissions from active_permissions for all extensions.
1767 extensions::ExtensionManagement* settings =
1768 extensions::ExtensionManagementFactory::GetForBrowserContext(profile());
1769 CHECK(settings);
1770 scoped_ptr<ExtensionSet> all_extensions(
1771 registry_->GenerateInstalledExtensionsSet());
1772 for (const auto& extension : *all_extensions.get()) {
1773 if (!settings->IsPermissionSetAllowed(
binjin685ade82014-11-06 09:53:561774 extension.get(),
binjine6b58b52014-10-31 01:55:571775 extension->permissions_data()->active_permissions())) {
1776 extensions::PermissionsUpdater(profile()).RemovePermissions(
1777 extension.get(),
binjin685ade82014-11-06 09:53:561778 settings->GetBlockedPermissions(extension.get()).get());
binjine6b58b52014-10-31 01:55:571779 }
1780 }
1781
binjin1569c9b2014-09-05 13:33:181782 CheckManagementPolicy();
1783}
1784
[email protected]8c484b742012-11-29 06:05:361785void ExtensionService::AddNewOrUpdatedExtension(
1786 const Extension* extension,
[email protected]399583b2012-12-11 09:33:421787 Extension::State initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111788 int install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061789 const syncer::StringOrdinal& page_ordinal,
1790 const std::string& install_parameter) {
[email protected]8c484b742012-11-29 06:05:361791 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]760f743b2014-05-28 13:52:021792 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]4a1d9c0d2014-06-13 12:50:111793 extension_prefs_->OnExtensionInstalled(
1794 extension, initial_state, page_ordinal, install_flags, install_parameter);
[email protected]9e9c1d12013-07-31 01:58:121795 delayed_installs_.Remove(extension->id());
[email protected]913ffca92014-03-27 15:26:161796 if (InstallVerifier::NeedsVerification(*extension))
1797 system_->install_verifier()->VerifyExtension(extension->id());
[email protected]760f743b2014-05-28 13:52:021798 FinishInstallation(extension, was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481799}
1800
1801void ExtensionService::MaybeFinishDelayedInstallation(
1802 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:191803 // Check if the extension already got installed.
1804 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:481805 return;
[email protected]9f4e4f082013-06-21 07:11:191806 extensions::ExtensionPrefs::DelayReason reason =
1807 extension_prefs_->GetDelayedInstallReason(extension_id);
1808
1809 // Check if the extension is idle. DELAY_REASON_NONE is used for older
1810 // preferences files that will not have set this field but it was previously
1811 // only used for idle updates.
1812 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
1813 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]1d5cf4142014-01-24 18:25:221814 is_ready() && !extensions::util::IsExtensionIdle(extension_id, profile_))
[email protected]6f6101832012-11-27 22:10:481815 return;
1816
[email protected]9f4e4f082013-06-21 07:11:191817 const Extension* extension = delayed_installs_.GetByID(extension_id);
1818 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
[email protected]90878c52014-04-04 18:21:021819 extensions::SharedModuleService::ImportStatus status =
1820 shared_module_service_->SatisfyImports(extension);
1821 if (status != SharedModuleService::IMPORT_STATUS_OK) {
1822 if (status == SharedModuleService::IMPORT_STATUS_UNRECOVERABLE) {
[email protected]9f4e4f082013-06-21 07:11:191823 delayed_installs_.Remove(extension_id);
1824 // Make sure no version of the extension is actually installed, (i.e.,
1825 // that this delayed install was not an update).
1826 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
1827 extension_prefs_->DeleteExtensionPrefs(extension_id);
1828 }
1829 return;
1830 }
1831 }
1832
[email protected]6f6101832012-11-27 22:10:481833 FinishDelayedInstallation(extension_id);
1834}
1835
1836void ExtensionService::FinishDelayedInstallation(
1837 const std::string& extension_id) {
1838 scoped_refptr<const Extension> extension(
1839 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:091840 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:191841 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:481842
[email protected]760f743b2014-05-28 13:52:021843 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]399583b2012-12-11 09:33:421844 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:481845 NOTREACHED();
1846
[email protected]760f743b2014-05-28 13:52:021847 FinishInstallation(extension.get(), was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481848}
1849
[email protected]760f743b2014-05-28 13:52:021850void ExtensionService::FinishInstallation(
1851 const Extension* extension, bool was_ephemeral) {
[email protected]e7554c3f2013-05-29 00:36:561852 const extensions::Extension* existing_extension =
1853 GetInstalledExtension(extension->id());
1854 bool is_update = false;
1855 std::string old_name;
1856 if (existing_extension) {
1857 is_update = true;
1858 old_name = existing_extension->name();
1859 }
[email protected]760f743b2014-05-28 13:52:021860 bool from_ephemeral =
1861 was_ephemeral && !extension_prefs_->IsEphemeralApp(extension->id());
1862 extensions::InstalledExtensionInfo details(
1863 extension, is_update, from_ephemeral, old_name);
[email protected]fcb58a862012-05-01 01:03:151864 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061865 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]fcb58a862012-05-01 01:03:151866 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:021867 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:151868
[email protected]bc44b5da2014-06-12 14:20:001869 registry_->TriggerOnWillBeInstalled(
[email protected]760f743b2014-05-28 13:52:021870 extension, is_update, from_ephemeral, old_name);
[email protected]17f07822014-05-22 08:45:151871
[email protected]399583b2012-12-11 09:33:421872 // Unpacked extensions default to allowing file access, but if that has been
1873 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:401874 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:421875 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
1876 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1877 }
1878
[email protected]6f6101832012-11-27 22:10:481879 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:031880
[email protected]bc44b5da2014-06-12 14:20:001881 // Notify observers that need to know when an installation is complete.
[email protected]38e872532014-07-16 23:27:511882 registry_->TriggerOnInstalled(extension, is_update);
[email protected]bc44b5da2014-06-12 14:20:001883
[email protected]9f4e4f082013-06-21 07:11:191884 // Check extensions that may have been delayed only because this shared module
1885 // was not available.
[email protected]374ceb6f2014-07-02 19:25:341886 if (SharedModuleInfo::IsSharedModule(extension))
[email protected]9f4e4f082013-06-21 07:11:191887 MaybeFinishDelayedInstallations();
[email protected]4a190632009-05-09 01:07:421888}
1889
[email protected]760f743b2014-05-28 13:52:021890void ExtensionService::PromoteEphemeralApp(
1891 const extensions::Extension* extension, bool is_from_sync) {
1892 DCHECK(GetInstalledExtension(extension->id()) &&
1893 extension_prefs_->IsEphemeralApp(extension->id()));
1894
[email protected]db378322014-07-21 23:19:341895 if (extension->RequiresSortOrdinal()) {
1896 extension_prefs_->app_sorting()->SetExtensionVisible(
1897 extension->id(), extension->ShouldDisplayInNewTabPage());
1898
1899 if (!is_from_sync) {
[email protected]760f743b2014-05-28 13:52:021900 // Reset the sort ordinals of the app to ensure it is added to the default
1901 // position, like newly installed apps would.
1902 extension_prefs_->app_sorting()->ClearOrdinals(extension->id());
[email protected]760f743b2014-05-28 13:52:021903 }
1904
[email protected]db378322014-07-21 23:19:341905 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1906 extension->id(), syncer::StringOrdinal());
1907 }
1908
[email protected]760f743b2014-05-28 13:52:021909 // Remove the ephemeral flags from the preferences.
1910 extension_prefs_->OnEphemeralAppPromoted(extension->id());
1911
1912 // Fire install-related events to allow observers to handle the promotion
1913 // of the ephemeral app.
1914 extensions::InstalledExtensionInfo details(
1915 extension,
1916 true /* is update */,
1917 true /* from ephemeral */,
1918 extension->name() /* old name */);
1919 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061920 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:021921 content::Source<Profile>(profile_),
1922 content::Details<const extensions::InstalledExtensionInfo>(&details));
1923
1924 registry_->TriggerOnWillBeInstalled(
1925 extension,
1926 true /* is update */,
1927 true /* from ephemeral */,
1928 extension->name() /* old name */);
1929
1930 if (registry_->enabled_extensions().Contains(extension->id())) {
[email protected]8f959f522014-08-06 06:26:281931 // If the app is already enabled and loaded, fire the load events to allow
1932 // observers to handle the promotion of the ephemeral app.
[email protected]760f743b2014-05-28 13:52:021933 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061934 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:021935 content::Source<Profile>(profile_),
1936 content::Details<const Extension>(extension));
1937
1938 registry_->TriggerOnLoaded(extension);
[email protected]8f959f522014-08-06 06:26:281939 } else {
1940 // Cached ephemeral apps may be updated and disabled due to permissions
1941 // increase. The app can be enabled (as long as no other disable reasons
1942 // exist) as the install was user-acknowledged.
1943 int disable_mask = Extension::DISABLE_NONE;
1944 if (!is_from_sync)
1945 disable_mask |= Extension::DISABLE_PERMISSIONS_INCREASE;
1946
1947 int other_disable_reasons =
1948 extension_prefs_->GetDisableReasons(extension->id()) & ~disable_mask;
1949 if (!other_disable_reasons) {
1950 if (extension_prefs_->DidExtensionEscalatePermissions(extension->id()))
1951 GrantPermissionsAndEnableExtension(extension);
1952 else
1953 EnableExtension(extension->id());
1954 }
[email protected]760f743b2014-05-28 13:52:021955 }
1956
[email protected]38e872532014-07-16 23:27:511957 registry_->TriggerOnInstalled(extension, true);
[email protected]bc44b5da2014-06-12 14:20:001958
[email protected]760f743b2014-05-28 13:52:021959 if (!is_from_sync && extension_sync_service_)
1960 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1961}
1962
[email protected]76b65442012-11-17 14:11:481963const Extension* ExtensionService::GetPendingExtensionUpdate(
1964 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:191965 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:481966}
1967
mukai87a8d402014-09-15 20:15:091968void ExtensionService::RegisterContentSettings(
1969 HostContentSettingsMap* host_content_settings_map) {
mukaia1aa3512014-10-22 20:14:111970 DCHECK_CURRENTLY_ON(BrowserThread::UI);
mukai87a8d402014-09-15 20:15:091971 host_content_settings_map->RegisterProvider(
1972 HostContentSettingsMap::INTERNAL_EXTENSION_PROVIDER,
1973 scoped_ptr<content_settings::ObservableProvider>(
reillyga3acbc12014-11-11 23:17:121974 new content_settings::InternalExtensionProvider(profile_)));
mukai87a8d402014-09-15 20:15:091975
1976 host_content_settings_map->RegisterProvider(
1977 HostContentSettingsMap::CUSTOM_EXTENSION_PROVIDER,
1978 scoped_ptr<content_settings::ObservableProvider>(
1979 new content_settings::CustomExtensionProvider(
1980 extensions::ContentSettingsService::Get(
1981 profile_)->content_settings_store(),
1982 profile_->GetOriginalProfile() != profile_)));
1983}
1984
[email protected]bb7f40952011-01-13 00:21:201985void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]bb1bc9b32013-12-21 03:09:141986 // No need to check for duplicates; inserting a duplicate is a no-op.
1987 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]3964e572014-02-13 21:57:411988 extensions_being_terminated_.erase(extension->id());
[email protected]b0af4792013-10-23 09:12:131989 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:201990}
1991
[email protected]e9d7496e2014-04-18 01:25:461992void ExtensionService::TerminateExtension(const std::string& extension_id) {
1993 const Extension* extension = GetInstalledExtension(extension_id);
1994 TrackTerminatedExtension(extension);
1995}
1996
[email protected]bb7f40952011-01-13 00:21:201997void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]cb1f4ac2014-08-07 16:55:421998 std::string lowercase_id = base::StringToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:141999 const Extension* extension =
2000 registry_->terminated_extensions().GetByID(lowercase_id);
2001 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:212002 if (extension) {
2003 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:062004 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:212005 content::Source<Profile>(profile_),
2006 content::Details<const Extension>(extension));
2007 }
[email protected]bb7f40952011-01-13 00:21:202008}
2009
[email protected]8001df22011-04-28 19:59:472010const Extension* ExtensionService::GetInstalledExtension(
2011 const std::string& id) const {
[email protected]599539802014-01-07 23:06:002012 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:362013}
2014
[email protected]9060d8b02012-01-13 02:14:302015bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322016 const std::string& id,
[email protected]c5e4a2222014-01-03 16:06:132017 const Version* version,
[email protected]650b2d52013-02-10 03:41:452018 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402019 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512020 int creation_flags,
2021 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382022 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:262023 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012024 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302025 return false;
[email protected]a8af9fdb2010-10-28 21:52:202026
[email protected]7577a5c52009-07-30 06:21:582027 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492028 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582029 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022030 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352031
[email protected]7577a5c52009-07-30 06:21:582032 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352033 // The default apps will have the location set as INTERNAL. Since older
2034 // default apps are installed as EXTERNAL, we override them. However, if the
2035 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402036 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352037 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402038 (location == Manifest::INTERNAL &&
2039 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352040
2041 if (!is_default_apps_migration) {
2042 DCHECK(version);
2043
2044 switch (existing->version()->CompareTo(*version)) {
2045 case -1: // existing version is older, we should upgrade
2046 break;
2047 case 0: // existing version is same, do nothing
2048 return false;
2049 case 1: // existing version is newer, uh-oh
2050 LOG(WARNING) << "Found external version of extension " << id
2051 << "that is older than current version. Current version "
2052 << "is: " << existing->VersionString() << ". New "
2053 << "version is: " << version->GetString()
2054 << ". Keeping current version.";
2055 return false;
2056 }
[email protected]7577a5c52009-07-30 06:21:582057 }
2058 }
2059
[email protected]9060d8b02012-01-13 02:14:302060 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302061 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482062 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302063 return false;
[email protected]e3987852012-05-04 10:06:302064 }
[email protected]9c635f22010-12-02 09:36:362065
[email protected]14908b72011-04-20 06:54:362066 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372067 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162068 installer->set_install_source(location);
2069 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072070 installer->set_expected_version(*version);
2071 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312072 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182073#if defined(OS_CHROMEOS)
2074 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2075#else
[email protected]6dfbbf82010-03-12 23:09:162076 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182077#endif
[email protected]47fc70c2011-12-06 07:29:512078
2079 // Depending on the source, a new external extension might not need a user
2080 // notification on installation. For such extensions, mark them acknowledged
2081 // now to suppress the notification.
2082 if (mark_acknowledged)
[email protected]374ceb6f2014-07-02 19:25:342083 external_install_manager_->AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302084
2085 return true;
[email protected]7577a5c52009-07-30 06:21:582086}
2087
[email protected]eaa7dd182010-12-14 11:09:002088void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132089 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182090 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332091 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182092 if (iter == orphaned_dev_tools_.end())
2093 return;
2094
[email protected]b3f957e62014-08-08 10:09:022095 iter->second->ConnectWebContents(host->host_contents());
[email protected]406027c02010-09-27 08:03:182096 orphaned_dev_tools_.erase(iter);
2097}
2098
[email protected]432115822011-07-10 15:52:272099void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532100 const content::NotificationSource& source,
2101 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272102 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542103 case chrome::NOTIFICATION_APP_TERMINATING:
2104 // Shutdown has started. Don't start any more extension installs.
2105 // (We cannot use ExtensionService::Shutdown() for this because it
2106 // happens too late in browser teardown.)
2107 browser_terminating_ = true;
2108 break;
[email protected]adf5a102014-07-31 12:44:062109 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532110 if (profile_ !=
2111 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322112 break;
[email protected]6c2381d2011-10-19 02:52:532113 }
[email protected]a4ed6282009-12-14 20:51:162114
[email protected]3a1dc572012-07-31 22:25:132115 extensions::ExtensionHost* host =
2116 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262117
[email protected]3964e572014-02-13 21:57:412118 // If the extension is already being terminated, there is nothing left to
2119 // do.
2120 if (!extensions_being_terminated_.insert(host->extension_id()).second)
2121 break;
2122
[email protected]fa2416f2011-05-03 08:41:202123 // Mark the extension as terminated and Unload it. We want it to
2124 // be in a consistent state: either fully working or not loaded
2125 // at all, but never half-crashed. We do it in a PostTask so
2126 // that other handlers of this notification will still have
2127 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162128 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362129 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362130 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202131 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012132 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202133 host->extension()));
[email protected]31f77262009-12-02 20:48:532134 break;
2135 }
[email protected]432115822011-07-10 15:52:272136 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302137 content::RenderProcessHost* process =
2138 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192139 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302140 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192141 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2142 break;
2143
[email protected]fafdc842014-01-17 18:09:082144 extensions::ProcessMap* process_map =
2145 extensions::ProcessMap::Get(profile_);
2146 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:032147 // An extension process was terminated, this might have resulted in an
2148 // app or extension becoming idle.
2149 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:082150 process_map->GetExtensionsInProcess(process->GetID());
elijahtaylor0def4432014-10-06 18:15:112151 // In addition to the extensions listed in the process map, one of those
2152 // extensions could be referencing a shared module which is waiting for
2153 // idle to update. Check all imports of these extensions, too.
2154 std::set<std::string> import_ids;
2155 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2156 it != extension_ids.end();
2157 ++it) {
2158 const Extension* extension = GetExtensionById(*it, true);
2159 if (!extension)
2160 continue;
2161 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2162 SharedModuleInfo::GetImports(extension);
2163 std::vector<SharedModuleInfo::ImportInfo>::const_iterator import_it;
2164 for (import_it = imports.begin(); import_it != imports.end();
2165 import_it++) {
2166 import_ids.insert((*import_it).extension_id);
2167 }
2168 }
2169 extension_ids.insert(import_ids.begin(), import_ids.end());
2170
[email protected]52b76592013-11-02 17:59:032171 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2172 it != extension_ids.end(); ++it) {
2173 if (delayed_installs_.Contains(*it)) {
2174 base::MessageLoop::current()->PostDelayedTask(
2175 FROM_HERE,
2176 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2177 AsWeakPtr(), *it),
2178 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2179 }
2180 }
2181 }
2182
[email protected]fafdc842014-01-17 18:09:082183 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462184 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202185 BrowserThread::IO,
2186 FROM_HERE,
2187 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082188 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302189 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212190 break;
2191 }
[email protected]92dd8d92013-02-26 00:41:082192 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452193 // Notify observers that chrome update is available.
2194 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2195 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082196 break;
2197 }
[email protected]ebe07772014-05-22 04:16:062198 case chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED: {
2199 OnProfileDestructionStarted();
2200 break;
2201 }
[email protected]aa96d3a2010-08-21 08:45:252202
[email protected]4814b512009-11-07 00:12:292203 default:
2204 NOTREACHED() << "Unexpected notification type.";
2205 }
2206}
2207
binjin47947f842014-11-18 12:10:242208int ExtensionService::GetDisableReasonsOnInstalled(const Extension* extension) {
2209 Extension::DisableReason disable_reason;
2210 // Extensions disabled by management policy should always be disabled, even
2211 // if it's force-installed.
2212 if (system_->management_policy()->MustRemainDisabled(
2213 extension, &disable_reason, nullptr)) {
2214 // A specified reason is required to disable the extension.
2215 DCHECK(disable_reason != Extension::DISABLE_NONE);
2216 return disable_reason;
2217 }
2218
[email protected]612a1cb12012-10-17 13:18:032219 // Extensions installed by policy can't be disabled. So even if a previous
2220 // installation disabled the extension, make sure it is now enabled.
binjin47947f842014-11-18 12:10:242221 if (system_->management_policy()->MustRemainEnabled(extension, nullptr))
2222 return Extension::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032223
binjin47947f842014-11-18 12:10:242224 // An already disabled extension should inherit the disable reasons and
2225 // remain disabled.
2226 if (extension_prefs_->IsExtensionDisabled(extension->id())) {
2227 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
2228 // If an extension was disabled without specified reason, presume it's
2229 // disabled by user.
2230 return disable_reasons == Extension::DISABLE_NONE
2231 ? Extension::DISABLE_USER_ACTION
2232 : disable_reasons;
2233 }
[email protected]612a1cb12012-10-17 13:18:032234
[email protected]41070e8d2012-10-24 01:34:362235 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032236 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542237 // enabling them. Hosted apps are excepted because they are not dangerous
2238 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402239 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2240 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032241 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
binjin47947f842014-11-18 12:10:242242 return Extension::DISABLE_EXTERNAL_EXTENSION;
[email protected]612a1cb12012-10-17 13:18:032243 }
2244 }
[email protected]612a1cb12012-10-17 13:18:032245
binjin47947f842014-11-18 12:10:242246 return Extension::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032247}
[email protected]0db124b02012-11-07 04:55:052248
mlerman6a37b6a42014-11-26 22:10:532249// Helper method to determine if an extension can be blocked.
2250bool ExtensionService::CanBlockExtension(const Extension* extension) const {
2251 return extension->location() != Manifest::COMPONENT &&
2252 extension->location() != Manifest::EXTERNAL_COMPONENT &&
2253 !system_->management_policy()->MustRemainEnabled(extension, NULL);
2254}
2255
[email protected]e7aa7b7e2012-11-27 04:51:222256bool ExtensionService::ShouldDelayExtensionUpdate(
2257 const std::string& extension_id,
[email protected]4a1d9c0d2014-06-13 12:50:112258 bool install_immediately) const {
[email protected]e7aa7b7e2012-11-27 04:51:222259 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2260
2261 // If delayed updates are globally disabled, or just for this extension,
2262 // don't delay.
[email protected]4a1d9c0d2014-06-13 12:50:112263 if (!install_updates_when_idle_ || install_immediately)
[email protected]e7aa7b7e2012-11-27 04:51:222264 return false;
2265
[email protected]695b5712012-12-06 23:55:282266 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222267 // If there is no old extension, this is not an update, so don't delay.
2268 if (!old)
2269 return false;
2270
[email protected]9367eabc2013-03-01 01:29:292271 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222272 // Delay installation if the extension listens for the onUpdateAvailable
2273 // event.
2274 return system_->event_router()->ExtensionHasEventListener(
2275 extension_id, kOnUpdateAvailableEvent);
2276 } else {
2277 // Delay installation if the extension is not idle.
[email protected]1d5cf4142014-01-24 18:25:222278 return !extensions::util::IsExtensionIdle(extension_id, profile_);
[email protected]e7aa7b7e2012-11-27 04:51:222279 }
[email protected]0db124b02012-11-07 04:55:052280}
[email protected]fdd679b2012-11-15 20:49:392281
[email protected]e5f8fffe2014-04-02 00:30:442282void ExtensionService::OnGarbageCollectIsolatedStorageStart() {
2283 DCHECK(!installs_delayed_for_gc_);
2284 installs_delayed_for_gc_ = true;
[email protected]399583b2012-12-11 09:33:422285}
2286
2287void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]e5f8fffe2014-04-02 00:30:442288 DCHECK(installs_delayed_for_gc_);
2289 installs_delayed_for_gc_ = false;
[email protected]9f4e4f082013-06-21 07:11:192290 MaybeFinishDelayedInstallations();
2291}
2292
2293void ExtensionService::MaybeFinishDelayedInstallations() {
2294 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422295 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2296 it != delayed_installs_.end();
2297 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192298 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422299 }
[email protected]9f4e4f082013-06-21 07:11:192300 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2301 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422302 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192303 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422304 }
[email protected]399583b2012-12-11 09:33:422305}
2306
[email protected]fdd679b2012-11-15 20:49:392307void ExtensionService::OnBlacklistUpdated() {
[email protected]2d19eb6e2014-01-27 17:30:002308 blacklist_->GetBlacklistedIDs(
[email protected]f47f7172014-03-19 19:27:102309 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252310 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282311}
2312
[email protected]2d19eb6e2014-01-27 17:30:002313void ExtensionService::ManageBlacklist(
2314 const extensions::Blacklist::BlacklistStateMap& state_map) {
[email protected]54ee8192014-03-29 17:37:242315 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]695b5712012-12-06 23:55:282316
mlerman6a37b6a42014-11-26 22:10:532317 std::set<std::string> blacklisted;
[email protected]2d19eb6e2014-01-27 17:30:002318 ExtensionIdSet greylist;
2319 ExtensionIdSet unchanged;
2320 for (extensions::Blacklist::BlacklistStateMap::const_iterator it =
2321 state_map.begin();
2322 it != state_map.end();
2323 ++it) {
2324 switch (it->second) {
2325 case extensions::NOT_BLACKLISTED:
2326 break;
[email protected]695b5712012-12-06 23:55:282327
[email protected]2d19eb6e2014-01-27 17:30:002328 case extensions::BLACKLISTED_MALWARE:
mlerman6a37b6a42014-11-26 22:10:532329 blacklisted.insert(it->first);
[email protected]2d19eb6e2014-01-27 17:30:002330 break;
2331
2332 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2333 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2334 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
2335 greylist.insert(it->first);
2336 break;
2337
2338 case extensions::BLACKLISTED_UNKNOWN:
2339 unchanged.insert(it->first);
2340 break;
2341 }
2342 }
2343
mlerman6a37b6a42014-11-26 22:10:532344 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]2d19eb6e2014-01-27 17:30:002345 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2346
[email protected]373daf972014-04-10 01:50:442347 error_controller_->ShowErrorIfNeeded();
[email protected]2d19eb6e2014-01-27 17:30:002348}
2349
2350namespace {
2351void Partition(const ExtensionIdSet& before,
2352 const ExtensionIdSet& after,
2353 const ExtensionIdSet& unchanged,
2354 ExtensionIdSet* no_longer,
2355 ExtensionIdSet* not_yet) {
2356 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2357 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2358 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2359}
2360} // namespace
2361
mlerman6a37b6a42014-11-26 22:10:532362void ExtensionService::UpdateBlacklistedExtensions(
2363 const ExtensionIdSet& blacklisted,
[email protected]2d19eb6e2014-01-27 17:30:002364 const ExtensionIdSet& unchanged) {
2365 ExtensionIdSet not_yet_blocked, no_longer_blocked;
mlerman6a37b6a42014-11-26 22:10:532366 Partition(registry_->blacklisted_extensions().GetIDs(), blacklisted,
2367 unchanged, &no_longer_blocked, &not_yet_blocked);
[email protected]2d19eb6e2014-01-27 17:30:002368
2369 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2370 it != no_longer_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282371 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142372 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252373 if (!extension.get()) {
mlerman6a37b6a42014-11-26 22:10:532374 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2375 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282376 continue;
[email protected]3f2a2fa2013-09-24 02:55:252377 }
[email protected]bb1bc9b32013-12-21 03:09:142378 registry_->RemoveBlacklisted(*it);
[email protected]3f2a2fa2013-09-24 02:55:252379 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092380 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092381 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092382 extension->location(),
2383 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282384 }
2385
[email protected]2d19eb6e2014-01-27 17:30:002386 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2387 it != not_yet_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282388 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252389 if (!extension.get()) {
2390 NOTREACHED() << "Extension " << *it << " needs to be "
2391 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282392 continue;
[email protected]3f2a2fa2013-09-24 02:55:252393 }
[email protected]bb1bc9b32013-12-21 03:09:142394 registry_->AddBlacklisted(extension);
[email protected]2d19eb6e2014-01-27 17:30:002395 extension_prefs_->SetExtensionBlacklistState(
2396 extension->id(), extensions::BLACKLISTED_MALWARE);
[email protected]b0af4792013-10-23 09:12:132397 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092398 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2399 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282400 }
[email protected]2d19eb6e2014-01-27 17:30:002401}
[email protected]695b5712012-12-06 23:55:282402
[email protected]2d19eb6e2014-01-27 17:30:002403// TODO(oleg): UMA logging
2404void ExtensionService::UpdateGreylistedExtensions(
2405 const ExtensionIdSet& greylist,
2406 const ExtensionIdSet& unchanged,
2407 const extensions::Blacklist::BlacklistStateMap& state_map) {
2408 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2409 Partition(greylist_.GetIDs(),
2410 greylist, unchanged,
2411 &no_longer_greylisted, &not_yet_greylisted);
2412
2413 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2414 it != no_longer_greylisted.end(); ++it) {
2415 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2416 if (!extension.get()) {
2417 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2418 << "but it was not marked as greylisted.";
2419 continue;
2420 }
2421
2422 greylist_.Remove(*it);
2423 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2424 extensions::NOT_BLACKLISTED);
2425 if (extension_prefs_->GetDisableReasons(extension->id()) &
2426 extensions::Extension::DISABLE_GREYLIST)
2427 EnableExtension(*it);
2428 }
2429
2430 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2431 it != not_yet_greylisted.end(); ++it) {
2432 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2433 if (!extension.get()) {
2434 NOTREACHED() << "Extension " << *it << " needs to be "
2435 << "disabled, but it's not installed.";
2436 continue;
2437 }
2438 greylist_.Insert(extension);
2439 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2440 state_map.find(*it)->second);
2441 if (registry_->enabled_extensions().Contains(extension->id()))
2442 DisableExtension(*it, extensions::Extension::DISABLE_GREYLIST);
2443 }
[email protected]fdd679b2012-11-15 20:49:392444}
[email protected]75bdcb872013-03-13 00:41:452445
2446void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2447 update_observers_.AddObserver(observer);
2448}
2449
2450void ExtensionService::RemoveUpdateObserver(
2451 extensions::UpdateObserver* observer) {
2452 update_observers_.RemoveObserver(observer);
2453}
[email protected]bb1bc9b32013-12-21 03:09:142454
2455// Used only by test code.
2456void ExtensionService::UnloadAllExtensionsInternal() {
2457 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2458
2459 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132460 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142461
2462 // TODO(erikkay) should there be a notification for this? We can't use
2463 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2464 // or uninstalled.
2465}
[email protected]ebe07772014-05-22 04:16:062466
2467void ExtensionService::OnProfileDestructionStarted() {
2468 ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
2469 for (ExtensionIdSet::iterator it = ids_to_unload.begin();
2470 it != ids_to_unload.end();
2471 ++it) {
2472 UnloadExtension(*it, UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN);
2473 }
2474}