blob: e42397b3648dc7fd27d9220a9f8dccdd95204559 [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
avia2f4804a2015-12-24 23:11:137#include <stddef.h>
8
[email protected]695b5712012-12-06 23:55:289#include <iterator>
asargente4de9f92016-09-15 01:40:0610#include <memory>
[email protected]97d2f1d2011-01-15 00:41:0811#include <set>
asargente4de9f92016-09-15 01:40:0612#include <utility>
[email protected]654512b2010-09-01 02:09:4213
[email protected]62433d32011-10-12 22:33:1214#include "base/command_line.h"
rdevlin.cronin08ada0f2017-03-09 23:29:5215#include "base/debug/alias.h"
16#include "base/debug/dump_without_crashing.h"
skyostilf221b7de2015-06-11 20:36:3217#include "base/location.h"
Ilya Sherman982457e62017-12-13 02:19:3618#include "base/metrics/histogram_functions.h"
rkaplowfca0f4dc52015-03-14 21:40:5719#include "base/metrics/histogram_macros.h"
skyostilf221b7de2015-06-11 20:36:3220#include "base/single_thread_task_runner.h"
tripta.g0ac673a2017-07-07 05:45:0921#include "base/stl_util.h"
[email protected]3ea1b182013-02-08 22:38:4122#include "base/strings/string_number_conversions.h"
catmullings98cd1942016-08-30 22:31:5923#include "base/strings/string_tokenizer.h"
[email protected]00e7bef2013-06-10 20:35:1724#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1825#include "base/strings/utf_string_conversions.h"
fdoray71c48e722017-05-17 22:37:0926#include "base/task_scheduler/post_task.h"
[email protected]34b99632011-01-01 01:01:0627#include "base/threading/thread_restrictions.h"
gabb15e19072016-05-11 20:45:4128#include "base/threading/thread_task_runner_handle.h"
[email protected]41a17c52013-06-28 00:27:5329#include "base/time/time.h"
rkaplowe65c2ff2015-02-14 16:29:5430#include "base/trace_event/trace_event.h"
avia2f4804a2015-12-24 23:11:1331#include "build/build_config.h"
[email protected]15730c42009-09-03 00:03:2032#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3633#include "chrome/browser/chrome_notification_types.h"
sunil.ratnu21b896c2014-11-24 10:09:2434#include "chrome/browser/extensions/api/content_settings/content_settings_custom_extension_provider.h"
mukai87a8d402014-09-15 20:15:0935#include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
ryanackley48bedbd2015-01-27 23:12:1436#include "chrome/browser/extensions/app_data_migrator.h"
[email protected]d8c8f25f2011-11-02 18:18:0137#include "chrome/browser/extensions/component_loader.h"
[email protected]13e062e2014-08-09 10:21:5538#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3839#include "chrome/browser/extensions/data_deleter.h"
rdevlin.cronin2813c1b2014-08-26 23:51:0840#include "chrome/browser/extensions/extension_action_storage_manager.h"
[email protected]5a145e82014-05-29 22:19:0741#include "chrome/browser/extensions/extension_assets_manager.h"
[email protected]62f051c2012-03-29 17:04:4442#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]373daf972014-04-10 01:50:4443#include "chrome/browser/extensions/extension_error_controller.h"
[email protected]19eb80152011-02-26 00:28:4344#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5245#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]617342a42013-12-18 23:34:0346#include "chrome/browser/extensions/extension_util.h"
[email protected]2894a512014-06-26 19:03:5647#include "chrome/browser/extensions/external_install_manager.h"
[email protected]5df038b2012-07-16 19:03:2748#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4649#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0150#include "chrome/browser/extensions/installed_loader.h"
[email protected]f3d3b382014-03-14 21:19:2851#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]13e062e2014-08-09 10:21:5552#include "chrome/browser/extensions/permissions_updater.h"
[email protected]90878c52014-04-04 18:21:0253#include "chrome/browser/extensions/shared_module_service.h"
[email protected]13e062e2014-08-09 10:21:5554#include "chrome/browser/extensions/unpacked_installer.h"
rockotdb5192992014-09-16 21:27:0255#include "chrome/browser/extensions/updater/chrome_extension_downloader_factory.h"
[email protected]13e062e2014-08-09 10:21:5556#include "chrome/browser/extensions/updater/extension_updater.h"
rockotdb5192992014-09-16 21:27:0257#include "chrome/browser/google/google_brand.h"
[email protected]8ecad5e2010-12-02 21:18:3358#include "chrome/browser/profiles/profile.h"
dbeam8d57a012015-05-21 07:12:2059#include "chrome/browser/search/thumbnail_source.h"
[email protected]b5a507b22013-11-08 20:41:5760#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1761#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]b07e606e2012-09-15 20:16:1562#include "chrome/browser/ui/webui/theme_source.h"
weidongg04695232017-06-09 00:01:0563#include "chrome/browser/upgrade_detector.h"
[email protected]e2eb43112009-05-29 21:19:5464#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4565#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1966#include "chrome/common/extensions/extension_constants.h"
brettw9e85ef42016-11-01 21:01:2467#include "chrome/common/features.h"
[email protected]a57209872009-05-04 22:53:1468#include "chrome/common/url_constants.h"
mukai8eaec822014-10-25 17:53:1669#include "components/content_settings/core/browser/host_content_settings_map.h"
[email protected]fdd28372014-08-21 02:27:2670#include "components/crx_file/id_util.h"
[email protected]dccba4f82014-05-29 00:52:5671#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3772#include "content/public/browser/notification_service.h"
[email protected]f3b1a082011-11-18 00:34:3073#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4274#include "content/public/browser/storage_partition.h"
[email protected]34423532013-11-21 18:13:1075#include "extensions/browser/event_router.h"
Devlin Cronin40b4cbc2017-08-02 01:57:5976#include "extensions/browser/extension_file_task_runner.h"
[email protected]22401dc2014-03-21 01:38:5777#include "extensions/browser/extension_host.h"
[email protected]bb1bc9b32013-12-21 03:09:1478#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2479#include "extensions/browser/extension_system.h"
Michael Giuffrida7efeed142017-06-07 06:29:2180#include "extensions/browser/extension_util.h"
rockotffa65b12014-10-14 17:19:0681#include "extensions/browser/extensions_browser_client.h"
lazyboye8634172016-01-28 00:10:4882#include "extensions/browser/external_install_info.h"
[email protected]4a1d9c0d2014-06-13 12:50:1183#include "extensions/browser/install_flag.h"
lazyboy75b9def2017-06-06 18:56:5984#include "extensions/browser/lazy_background_task_queue.h"
Michael Giuffrida6397c592017-10-09 20:46:5385#include "extensions/browser/management_policy.h"
[email protected]45f5b7d2014-01-22 23:47:1386#include "extensions/browser/runtime_data.h"
[email protected]e43c61f2014-07-20 21:46:3487#include "extensions/browser/uninstall_reason.h"
[email protected]a9aa62b312013-11-29 05:35:0688#include "extensions/browser/update_observer.h"
rockotffa65b12014-10-14 17:19:0689#include "extensions/browser/updater/extension_cache.h"
rockot05f40a922014-10-16 19:40:2590#include "extensions/browser/updater/extension_downloader.h"
[email protected]fb820c02014-03-13 15:07:0891#include "extensions/common/extension_messages.h"
rockot90659852014-09-18 19:31:5292#include "extensions/common/extension_urls.h"
[email protected]5ef835a2013-11-08 20:42:5793#include "extensions/common/feature_switch.h"
rdevlin.cronin41227532016-07-13 21:24:3494#include "extensions/common/features/feature_channel.h"
[email protected]85df9d12014-04-15 17:02:1495#include "extensions/common/file_util.h"
[email protected]0c3c9732013-09-16 08:53:4196#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5197#include "extensions/common/manifest_handlers/background_info.h"
Michael Giuffrida6397c592017-10-09 20:46:5398#include "extensions/common/manifest_handlers/shared_module_info.h"
rockotd5546142014-10-15 00:29:0899#include "extensions/common/manifest_url_handlers.h"
[email protected]dccba4f82014-05-29 00:52:56100#include "extensions/common/one_shot_event.h"
treib2e0517f92015-05-07 23:15:36101#include "extensions/common/permissions/api_permission.h"
[email protected]c41003472013-10-19 15:37:25102#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:41103#include "extensions/common/permissions/permissions_data.h"
Michael Giuffridab158f3f2017-07-17 19:53:44104#include "extensions/common/switches.h"
[email protected]79a60642012-10-20 21:03:18105
brettw9e85ef42016-11-01 21:01:24106#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
treib8ecc1eb52015-03-04 18:29:06107#include "chrome/browser/supervised_user/supervised_user_service.h"
108#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
109#endif
110
[email protected]eed367e2011-04-12 03:43:31111#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18112#include "chrome/browser/chromeos/extensions/install_limiter.h"
achuithd3da4f02017-03-23 20:05:29113#include "chrome/browser/chromeos/profiles/profile_helper.h"
pilgrime92c5fcd2014-09-10 23:31:23114#include "storage/browser/fileapi/file_system_backend.h"
115#include "storage/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31116#endif
117
[email protected]55eb70e762012-02-20 17:38:39118using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39119using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36120using content::DevToolsAgentHost;
treib2e0517f92015-05-07 23:15:36121using extensions::APIPermission;
[email protected]bf3d9df2012-07-24 23:20:27122using extensions::CrxInstaller;
[email protected]1c321ee2012-05-21 03:02:34123using extensions::Extension;
124using extensions::ExtensionIdSet;
125using extensions::ExtensionInfo;
Michael Giuffrida6397c592017-10-09 20:46:53126using extensions::ExtensionRegistrar;
[email protected]599539802014-01-07 23:06:00127using extensions::ExtensionRegistry;
[email protected]289c44b2013-12-17 03:26:57128using extensions::ExtensionSet;
lazyboye8634172016-01-28 00:10:48129using extensions::ExternalInstallInfoFile;
130using extensions::ExternalInstallInfoUpdateUrl;
131using extensions::ExternalProviderInterface;
[email protected]215a7be2012-10-22 19:53:42132using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46133using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36134using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40135using extensions::Manifest;
treib2e0517f92015-05-07 23:15:36136using extensions::PermissionID;
137using extensions::PermissionIDSet;
[email protected]c2e66e12012-06-27 06:27:06138using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19139using extensions::SharedModuleInfo;
[email protected]90878c52014-04-04 18:21:02140using extensions::SharedModuleService;
limasdf0deef2042017-05-03 19:17:17141using extensions::UnloadedExtensionReason;
[email protected]5ef47ec2010-01-28 05:58:05142
[email protected]b6ab96d2009-08-20 18:58:19143namespace {
144
[email protected]0db124b02012-11-07 04:55:05145// Wait this many seconds after an extensions becomes idle before updating it.
[email protected]ddc6a122014-06-27 04:52:32146const int kUpdateIdleDelay = 5;
[email protected]0db124b02012-11-07 04:55:05147
Takumi Fujimoto43c8c00f2017-07-26 22:48:56148// IDs of extensions that have been replaced by component extensions and need to
149// be uninstalled.
150const char* kMigratedExtensionIds[] = {
151 "boadgeojelhgndaghljhdicfkmllpafd", // Google Cast
152 "dliochdbjfkdbacpmhlcpmleaejidimm" // Google Cast (Beta)
153};
154
lazyboy75b9def2017-06-06 18:56:59155void DoNothingWithExtensionHost(extensions::ExtensionHost* host) {}
156
[email protected]c6d474f82009-12-16 21:11:06157} // namespace
[email protected]b6ab96d2009-08-20 18:58:19158
[email protected]eaa7dd182010-12-14 11:09:00159// ExtensionService.
[email protected]6014d672008-12-05 00:38:25160
[email protected]8e4560b62011-01-14 10:09:14161void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12162 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20163
[email protected]7a4c6852010-09-16 03:44:22164 // Check if the providers know about this extension.
achuith2f5578b2016-02-26 21:57:13165 for (const auto& provider : external_extension_providers_) {
166 DCHECK(provider->IsReady());
167 if (provider->HasExtension(id))
[email protected]0a60a2e2010-10-25 16:15:21168 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22169 }
170
[email protected]0f48fca2011-05-19 18:46:35171 // We get the list of external extensions to check from preferences.
172 // It is possible that an extension has preferences but is not loaded.
173 // For example, an extension that requires experimental permissions
174 // will not be loaded if the experimental command line flag is not used.
175 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40176 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35177 // We can't call UninstallExtension with an unloaded/invalid
178 // extension ID.
179 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
180 << "with id: " << id;
181 return;
182 }
Devlin Cronin218df7f2017-11-21 21:41:31183 UninstallExtension(
184 id, extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION, nullptr);
[email protected]7a4c6852010-09-16 03:44:22185}
186
[email protected]8e4560b62011-01-14 10:09:14187void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22188 external_extension_providers_.clear();
189}
190
[email protected]8e4560b62011-01-14 10:09:14191void ExtensionService::AddProviderForTesting(
lazyboyf33109d2016-08-31 00:37:08192 std::unique_ptr<ExternalProviderInterface> test_provider) {
[email protected]a29a517a2011-01-21 21:11:12193 CHECK(test_provider);
lazyboyf33109d2016-08-31 00:37:08194 external_extension_providers_.push_back(std::move(test_provider));
[email protected]7a4c6852010-09-16 03:44:22195}
196
[email protected]8f959f522014-08-06 06:26:28197void ExtensionService::BlacklistExtensionForTest(
198 const std::string& extension_id) {
mlerman6a37b6a42014-11-26 22:10:53199 ExtensionIdSet blacklisted;
[email protected]8f959f522014-08-06 06:26:28200 ExtensionIdSet unchanged;
mlerman6a37b6a42014-11-26 22:10:53201 blacklisted.insert(extension_id);
202 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]8f959f522014-08-06 06:26:28203}
204
[email protected]9060d8b02012-01-13 02:14:30205bool ExtensionService::OnExternalExtensionUpdateUrlFound(
lazyboye8634172016-01-28 00:10:48206 const ExternalInstallInfoUpdateUrl& info,
207 bool is_initial_load) {
Istiaque Ahmedf6e72622017-09-08 23:14:17208 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lazyboye8634172016-01-28 00:10:48209 CHECK(crx_file::id_util::IdIsValid(info.extension_id));
[email protected]a8af9fdb2010-10-28 21:52:20210
lazyboye8634172016-01-28 00:10:48211 if (Manifest::IsExternalLocation(info.download_location)) {
[email protected]044e86992014-01-24 22:59:11212 // All extensions that are not user specific can be cached.
lazyboye8634172016-01-28 00:10:48213 extensions::ExtensionsBrowserClient::Get()
214 ->GetExtensionCache()
215 ->AllowCaching(info.extension_id);
[email protected]044e86992014-01-24 22:59:11216 }
217
lazyboye8634172016-01-28 00:10:48218 const Extension* extension = GetExtensionById(info.extension_id, true);
[email protected]8a87a5332011-08-11 17:54:59219 if (extension) {
asargent56282ab72016-09-09 16:58:03220 // Already installed. Skip this install if the current location has higher
221 // priority than |info.download_location|, and we aren't doing a
222 // reinstall of a corrupt policy force-installed extension.
[email protected]1d5e58b2013-01-31 08:41:40223 Manifest::Location current = extension->location();
asargent56282ab72016-09-09 16:58:03224 if (!pending_extension_manager_.IsPolicyReinstallForCorruptionExpected(
225 info.extension_id) &&
226 current == Manifest::GetHigherPriorityLocation(
227 current, info.download_location)) {
[email protected]9060d8b02012-01-13 02:14:30228 return false;
lazyboye8634172016-01-28 00:10:48229 }
[email protected]8a87a5332011-08-11 17:54:59230 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22231 }
[email protected]9060d8b02012-01-13 02:14:30232
lazyboye8634172016-01-28 00:10:48233 // Add |info.extension_id| to the set of pending extensions. If it can not
234 // be added, then there is already a pending record from a higher-priority
235 // install source. In this case, signal that this extension will not be
[email protected]9060d8b02012-01-13 02:14:30236 // installed by returning false.
237 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
Devlin Cronind4c2a8f32017-09-29 17:08:30238 info.extension_id, info.install_parameter, info.update_url,
lazyboye8634172016-01-28 00:10:48239 info.download_location, info.creation_flags,
240 info.mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30241 return false;
[email protected]31bb5ee62012-09-12 22:58:40242 }
[email protected]9060d8b02012-01-13 02:14:30243
lazyboye8634172016-01-28 00:10:48244 if (is_initial_load)
245 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30246 return true;
[email protected]7a4c6852010-09-16 03:44:22247}
248
lazyboye8634172016-01-28 00:10:48249void ExtensionService::OnExternalProviderUpdateComplete(
250 const ExternalProviderInterface* provider,
Devlin Cronin19f70b6a2017-10-01 04:14:05251 const std::vector<ExternalInstallInfoUpdateUrl>& update_url_extensions,
252 const std::vector<ExternalInstallInfoFile>& file_extensions,
lazyboye8634172016-01-28 00:10:48253 const std::set<std::string>& removed_extensions) {
254 // Update pending_extension_manager() with the new extensions first.
lazyboy4aeef202016-09-07 21:28:59255 for (const auto& extension : update_url_extensions)
Devlin Cronin19f70b6a2017-10-01 04:14:05256 OnExternalExtensionUpdateUrlFound(extension, false);
lazyboy4aeef202016-09-07 21:28:59257 for (const auto& extension : file_extensions)
Devlin Cronin19f70b6a2017-10-01 04:14:05258 OnExternalExtensionFileFound(extension);
lazyboye8634172016-01-28 00:10:48259
260#if DCHECK_IS_ON()
261 for (const std::string& id : removed_extensions) {
lazyboy4aeef202016-09-07 21:28:59262 for (const auto& extension : update_url_extensions)
Devlin Cronin19f70b6a2017-10-01 04:14:05263 DCHECK_NE(id, extension.extension_id);
lazyboy4aeef202016-09-07 21:28:59264 for (const auto& extension : file_extensions)
Devlin Cronin19f70b6a2017-10-01 04:14:05265 DCHECK_NE(id, extension.extension_id);
lazyboye8634172016-01-28 00:10:48266 }
267#endif
268
269 // Then uninstall before running |updater_|.
270 for (const std::string& id : removed_extensions)
271 CheckExternalUninstall(id);
272
273 if (!update_url_extensions.empty() && updater_) {
274 // Empty params will cause pending extensions to be updated.
275 extensions::ExtensionUpdater::CheckParams empty_params;
276 updater_->CheckNow(empty_params);
277 }
278
279 error_controller_->ShowErrorIfNeeded();
280 external_install_manager_->UpdateExternalExtensionAlert();
281}
282
[email protected]eaa7dd182010-12-14 11:09:00283ExtensionService::ExtensionService(Profile* profile,
avi3ef9ec9e2014-12-22 22:50:17284 const base::CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45285 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23286 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39287 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03288 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35289 bool extensions_enabled,
290 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39291 : extensions::Blacklist::Observer(blacklist),
catmullings98cd1942016-08-30 22:31:59292 command_line_(command_line),
[email protected]fdd679b2012-11-15 20:49:39293 profile_(profile),
[email protected]bd306722012-07-11 20:43:59294 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43295 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28296 blacklist_(blacklist),
[email protected]5fdfa562013-12-27 17:43:59297 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]6c9bedf2014-05-21 03:55:51298 pending_extension_manager_(profile),
[email protected]a9b00ac2009-06-25 21:03:23299 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03300 extensions_enabled_(extensions_enabled),
[email protected]4a10006a2013-05-17 23:18:35301 ready_(ready),
ryanackley48bedbd2015-01-27 23:12:14302 shared_module_service_(new extensions::SharedModuleService(profile_)),
Michael Giuffrida023e3532017-09-28 23:21:21303 app_data_migrator_(new extensions::AppDataMigrator(profile_, registry_)),
Michael Giuffrida6397c592017-10-09 20:46:53304 extension_registrar_(profile_, this) {
[email protected]a29a517a2011-01-21 21:11:12305 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowdd66a1342015-03-05 00:31:49306 TRACE_EVENT0("browser,startup", "ExtensionService::ExtensionService::ctor");
[email protected]a8af9fdb2010-10-28 21:52:20307
[email protected]36a784c2009-06-23 06:21:08308 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02309 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
310 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23311 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08312
[email protected]3c4abc82012-10-22 22:25:54313 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
314 content::NotificationService::AllBrowserContextsAndSources());
[email protected]adf5a102014-07-31 12:44:06315 registrar_.Add(this,
316 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07317 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27318 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07319 content::NotificationService::AllBrowserContextsAndSources());
[email protected]ebe07772014-05-22 04:16:06320 registrar_.Add(this,
321 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
322 content::Source<Profile>(profile_));
binjin1569c9b2014-09-05 13:33:18323
weidongg04695232017-06-09 00:01:05324 UpgradeDetector::GetInstance()->AddObserver(this);
325
binjin1569c9b2014-09-05 13:33:18326 extensions::ExtensionManagementFactory::GetForBrowserContext(profile_)
327 ->AddObserver(this);
[email protected]4814b512009-11-07 00:12:29328
rkaplowdd66a1342015-03-05 00:31:49329 // Set up the ExtensionUpdater.
[email protected]93fd78f42009-07-10 16:43:17330 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02331 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
Catherine Mullings625803452017-06-29 22:29:10332 bool is_extensions_update_frequency_switch_used =
333 command_line->HasSwitch(switches::kExtensionsUpdateFrequency);
334 if (is_extensions_update_frequency_switch_used) {
[email protected]e83326f2010-07-31 17:29:25335 base::StringToInt(command_line->GetSwitchValueASCII(
336 switches::kExtensionsUpdateFrequency),
337 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17338 }
Catherine Mullings625803452017-06-29 22:29:10339 UMA_HISTOGRAM_BOOLEAN("Extensions.UpdateFrequencyCommandLineFlagIsUsed",
340 is_extensions_update_frequency_switch_used);
[email protected]044e86992014-01-24 22:59:11341 updater_.reset(new extensions::ExtensionUpdater(
342 this,
343 extension_prefs,
344 profile->GetPrefs(),
345 profile,
346 update_frequency,
rockotffa65b12014-10-14 17:19:06347 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache(),
rockotdb5192992014-09-16 21:27:02348 base::Bind(ChromeExtensionDownloaderFactory::CreateForProfile,
349 profile)));
[email protected]93fd78f42009-07-10 16:43:17350 }
351
[email protected]25ae0152011-11-18 14:40:02352 component_loader_.reset(
353 new extensions::ComponentLoader(this,
354 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08355 g_browser_process->local_state(),
356 profile));
[email protected]8e4560b62011-01-14 10:09:14357
[email protected]0436b102011-04-15 18:30:03358 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25359 extensions::ExternalProviderImpl::CreateExternalProviders(
360 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05361 }
[email protected]b671760b2010-07-15 21:13:47362
[email protected]74474042013-11-21 12:03:54363 // Set this as the ExtensionService for app sorting to ensure it causes syncs
364 // if required.
[email protected]b3aa7182013-04-25 04:45:23365 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
366
[email protected]373daf972014-04-10 01:50:44367 error_controller_.reset(
368 new extensions::ExtensionErrorController(profile_, is_first_run_));
[email protected]374ceb6f2014-07-02 19:25:34369 external_install_manager_.reset(
370 new extensions::ExternalInstallManager(profile_, is_first_run_));
[email protected]373daf972014-04-10 01:50:44371
[email protected]c77f2352012-08-08 22:07:58372 extension_action_storage_manager_.reset(
373 new extensions::ExtensionActionStorageManager(profile_));
374
[email protected]cb0e50312011-05-09 15:03:07375 // How long is the path to the Extensions directory?
376 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
drbasicf0d1b262016-08-23 06:10:42377 install_directory_.value().length(), 1, 500, 100);
[email protected]6014d672008-12-05 00:38:25378}
379
[email protected]3f213ad2012-07-26 23:39:41380extensions::PendingExtensionManager*
381 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37382 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45383}
384
[email protected]eaa7dd182010-12-14 11:09:00385ExtensionService::~ExtensionService() {
weidongg04695232017-06-09 00:01:05386 UpgradeDetector::GetInstance()->RemoveObserver(this);
[email protected]c8d407e2011-04-28 21:27:17387 // No need to unload extensions here because they are profile-scoped, and the
388 // profile is in the process of being deleted.
achuith2f5578b2016-02-26 21:57:13389 for (const auto& provider : external_extension_providers_)
[email protected]8e4560b62011-01-14 10:09:14390 provider->ServiceShutdown();
[email protected]6014d672008-12-05 00:38:25391}
392
[email protected]037228a2012-10-05 01:36:16393void ExtensionService::Shutdown() {
binjin1569c9b2014-09-05 13:33:18394 extensions::ExtensionManagementFactory::GetInstance()
395 ->GetForBrowserContext(profile())
396 ->RemoveObserver(this);
[email protected]037228a2012-10-05 01:36:16397}
398
[email protected]b2907fd2011-03-25 16:43:37399const Extension* ExtensionService::GetExtensionById(
400 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00401 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28402 if (include_disabled) {
mlerman6a37b6a42014-11-26 22:10:53403 // Include blacklisted and blocked extensions here because there are
404 // hundreds of callers of this function, and many might assume that this
405 // includes those that have been disabled due to blacklisting or blocking.
[email protected]599539802014-01-07 23:06:00406 include_mask |= ExtensionRegistry::DISABLED |
mlerman6a37b6a42014-11-26 22:10:53407 ExtensionRegistry::BLACKLISTED | ExtensionRegistry::BLOCKED;
[email protected]695b5712012-12-06 23:55:28408 }
[email protected]599539802014-01-07 23:06:00409 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28410}
411
[email protected]eaa7dd182010-12-14 11:09:00412void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12413 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowe65c2ff2015-02-14 16:29:54414 TRACE_EVENT0("browser,startup", "ExtensionService::Init");
rkaplowa8fd8d32015-02-25 21:27:56415 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.ExtensionServiceInitTime");
[email protected]3350d712013-11-18 09:32:24416
[email protected]4a10006a2013-05-17 23:18:35417 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14418 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32419
benwells22ab8ee2015-08-21 02:55:08420 component_loader_->LoadAll();
achuithd3da4f02017-03-23 20:05:29421 bool load_saved_extensions = true;
emaxx35e8b822017-04-11 02:04:29422 bool load_command_line_extensions = extensions_enabled_;
achuithd3da4f02017-03-23 20:05:29423#if defined(OS_CHROMEOS)
tbarzicd2a741e2017-06-28 20:37:54424 if (chromeos::ProfileHelper::IsSigninProfile(profile_) ||
425 chromeos::ProfileHelper::IsLockScreenAppProfile(profile_)) {
achuithd3da4f02017-03-23 20:05:29426 load_saved_extensions = false;
emaxx35e8b822017-04-11 02:04:29427 load_command_line_extensions = false;
428 }
achuithd3da4f02017-03-23 20:05:29429#endif
rdevlin.croninf2e1cb012017-05-27 01:27:59430 if (load_saved_extensions)
achuithd3da4f02017-03-23 20:05:29431 extensions::InstalledLoader(this).LoadAllExtensions();
rdevlin.croninf2e1cb012017-05-27 01:27:59432
433 OnInstalledExtensionsLoaded();
434
catmullings98cd1942016-08-30 22:31:59435 LoadExtensionsFromCommandLineFlag(switches::kDisableExtensionsExcept);
emaxx35e8b822017-04-11 02:04:29436 if (load_command_line_extensions)
Michael Giuffridab158f3f2017-07-17 19:53:44437 LoadExtensionsFromCommandLineFlag(extensions::switches::kLoadExtension);
Tatsuhisa Yamaguchi5ed1aece32017-10-26 10:18:56438 EnableZipUnpackerExtension();
benwells22ab8ee2015-08-21 02:55:08439 EnabledReloadableExtensions();
440 MaybeFinishShutdownDelayed();
441 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25442
Takumi Fujimoto43c8c00f2017-07-26 22:48:56443 UninstallMigratedExtensions();
444
benwells22ab8ee2015-08-21 02:55:08445 // TODO(erikkay): this should probably be deferred to a future point
446 // rather than running immediately at startup.
447 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57448
benwells22ab8ee2015-08-21 02:55:08449 LoadGreylistFromPrefs();
rkaplowa8fd8d32015-02-25 21:27:56450}
[email protected]3350d712013-11-18 09:32:24451
Tatsuhisa Yamaguchi5ed1aece32017-10-26 10:18:56452void ExtensionService::EnableZipUnpackerExtensionForTest() {
453 EnableZipUnpackerExtension();
454}
455
456void ExtensionService::EnableZipUnpackerExtension() {
457 TRACE_EVENT0("browser,startup",
458 "ExtensionService::EnableZipUnpackerExtension");
459
460#if defined(OS_CHROMEOS)
461 // There were some cases where the Zip Unpacker was disabled in the profile,
462 // by some reason, and cannot re-enable it in any UI. crbug.com/643060
463 const std::string& id = extension_misc::kZIPUnpackerExtensionId;
464 const Extension* extension = registry_->disabled_extensions().GetByID(id);
Tatsuhisa Yamaguchi6f033fa2017-11-21 14:57:25465 if (extension && CanEnableExtension(extension)) {
Ilya Sherman982457e62017-12-13 02:19:36466 base::UmaHistogramSparse(
Tatsuhisa Yamaguchi6f033fa2017-11-21 14:57:25467 "ExtensionService.ZipUnpackerDisabledReason",
468 extension_prefs_->GetDisableReasons(extension->id()));
Tatsuhisa Yamaguchi5ed1aece32017-10-26 10:18:56469 EnableExtension(id);
Tatsuhisa Yamaguchi6f033fa2017-11-21 14:57:25470 }
Tatsuhisa Yamaguchi5ed1aece32017-10-26 10:18:56471#endif
472}
473
rkaplowa8fd8d32015-02-25 21:27:56474void ExtensionService::EnabledReloadableExtensions() {
475 TRACE_EVENT0("browser,startup",
476 "ExtensionService::EnabledReloadableExtensions");
477
478 std::vector<std::string> extensions_to_enable;
achuith2f5578b2016-02-26 21:57:13479 for (const auto& e : registry_->disabled_extensions()) {
rkaplowa8fd8d32015-02-25 21:27:56480 if (extension_prefs_->GetDisableReasons(e->id()) ==
Minh X. Nguyen45479012017-08-18 21:35:36481 extensions::disable_reason::DISABLE_RELOAD)
rkaplowa8fd8d32015-02-25 21:27:56482 extensions_to_enable.push_back(e->id());
rkaplowa8fd8d32015-02-25 21:27:56483 }
484 for (const std::string& extension : extensions_to_enable) {
485 EnableExtension(extension);
486 }
487}
488
489void ExtensionService::MaybeFinishShutdownDelayed() {
490 TRACE_EVENT0("browser,startup",
491 "ExtensionService::MaybeFinishShutdownDelayed");
492
dchengc963c7142016-04-08 03:55:22493 std::unique_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
rkaplowa8fd8d32015-02-25 21:27:56494 extension_prefs_->GetAllDelayedInstallInfo());
495 for (size_t i = 0; i < delayed_info->size(); ++i) {
496 ExtensionInfo* info = delayed_info->at(i).get();
achuith2f5578b2016-02-26 21:57:13497 scoped_refptr<const Extension> extension(nullptr);
rkaplowa8fd8d32015-02-25 21:27:56498 if (info->extension_manifest) {
499 std::string error;
500 extension = Extension::Create(
501 info->extension_path, info->extension_location,
502 *info->extension_manifest,
503 extension_prefs_->GetDelayedInstallCreationFlags(info->extension_id),
504 info->extension_id, &error);
505 if (extension.get())
506 delayed_installs_.Insert(extension);
507 }
508 }
509 MaybeFinishDelayedInstallations();
dchengc963c7142016-04-08 03:55:22510 std::unique_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
rkaplowa8fd8d32015-02-25 21:27:56511 extension_prefs_->GetAllDelayedInstallInfo());
512 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
513 delayed_info2->size() - delayed_info->size());
[email protected]6014d672008-12-05 00:38:25514}
515
[email protected]2d19eb6e2014-01-27 17:30:00516void ExtensionService::LoadGreylistFromPrefs() {
rkaplowa8fd8d32015-02-25 21:27:56517 TRACE_EVENT0("browser,startup", "ExtensionService::LoadGreylistFromPrefs");
518
dchengc963c7142016-04-08 03:55:22519 std::unique_ptr<ExtensionSet> all_extensions =
[email protected]f47f7172014-03-19 19:27:10520 registry_->GenerateInstalledExtensionsSet();
[email protected]2d19eb6e2014-01-27 17:30:00521
achuith2f5578b2016-02-26 21:57:13522 for (const auto& extension : *all_extensions) {
523 const extensions::BlacklistState state =
524 extension_prefs_->GetExtensionBlacklistState(extension->id());
[email protected]2d19eb6e2014-01-27 17:30:00525 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
526 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
527 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
achuith2f5578b2016-02-26 21:57:13528 greylist_.Insert(extension);
[email protected]2d19eb6e2014-01-27 17:30:00529 }
530}
531
ginkage553af3202015-02-04 12:39:09532bool ExtensionService::UpdateExtension(const extensions::CRXFileInfo& file,
[email protected]044e86992014-01-24 22:59:11533 bool file_ownership_passed,
[email protected]31bb5ee62012-09-12 22:58:40534 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12535 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54536 if (browser_terminating_) {
537 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
538 // Leak the temp file at extension_path. We don't want to add to the disk
539 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
540 // the file is in the OS temp directory which should be cleaned up for us.
541 return false;
542 }
[email protected]a8af9fdb2010-10-28 21:52:20543
ginkage553af3202015-02-04 12:39:09544 const std::string& id = file.extension_id;
545
[email protected]3f213ad2012-07-26 23:39:41546 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06547 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32548
[email protected]695b5712012-12-06 23:55:28549 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06550 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33551 LOG(WARNING) << "Will not update extension " << id
552 << " because it is not installed or pending";
553 // Delete extension_path since we're not creating a CrxInstaller
554 // that would do it for us.
asargentd50b18c2016-04-21 01:17:16555 if (file_ownership_passed &&
Devlin Croninad3c52b2017-08-10 01:49:37556 !extensions::GetExtensionFileTaskRunner()->PostTask(
tzik8d880ee2017-04-20 19:46:24557 FROM_HERE, base::BindOnce(&extensions::file_util::DeleteFile,
558 file.path, false)))
[email protected]14908b72011-04-20 06:54:36559 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03560
561 return false;
[email protected]e957fe52009-06-23 16:51:05562 }
Minh X. Nguyen30975342017-12-04 22:02:41563 // Either |pending_extension_info| or |extension| or both must not be null.
treibe960e282015-09-11 10:38:08564 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:16565 installer->set_expected_id(id);
ginkage47e603e2015-02-27 08:42:41566 installer->set_expected_hash(file.expected_hash);
[email protected]464213a2013-10-15 01:06:48567 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06568 if (pending_extension_info) {
569 installer->set_install_source(pending_extension_info->install_source());
pkotwicz31eb1e8ec2014-10-08 15:26:46570 installer->set_allow_silent_install(true);
treibe960e282015-09-11 10:38:08571 // If the extension came in disabled due to a permission increase, then
572 // don't grant it all the permissions. crbug.com/484214
573 bool has_permissions_increase =
574 extensions::ExtensionPrefs::Get(profile_)->HasDisableReason(
Minh X. Nguyen45479012017-08-18 21:35:36575 id, extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE);
treibe960e282015-09-11 10:38:08576 const base::Version& expected_version = pending_extension_info->version();
577 if (has_permissions_increase ||
578 pending_extension_info->remote_install() ||
579 !expected_version.IsValid()) {
[email protected]21db9ef2014-05-16 02:06:27580 installer->set_grant_permissions(false);
treibe960e282015-09-11 10:38:08581 } else {
582 installer->set_expected_version(expected_version,
583 false /* fail_install_if_unexpected */);
584 }
mamir0128d5a2016-07-15 20:55:48585 creation_flags = pending_extension_info->creation_flags();
[email protected]464213a2013-10-15 01:06:48586 if (pending_extension_info->mark_acknowledged())
[email protected]374ceb6f2014-07-02 19:25:34587 external_install_manager_->AcknowledgeExternalExtension(id);
Minh X. Nguyen30975342017-12-04 22:02:41588 // If the extension was installed from or has migrated to the webstore, or
589 // its auto-update URL is from the webstore, treat it as a webstore install.
590 // Note that we ignore some older extensions with blank auto-update URLs
591 // because we are mostly concerned with restrictions on NaCl extensions,
592 // which are newer.
593 if (!extension && extension_urls::IsWebstoreUpdateUrl(
594 pending_extension_info->update_url()))
595 creation_flags |= Extension::FROM_WEBSTORE;
596 } else {
597 // |extension| must not be null.
[email protected]5eb375e92010-11-26 07:50:41598 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06599 }
[email protected]a12ce8b22012-01-17 18:40:53600
Minh X. Nguyen30975342017-12-04 22:02:41601 if (extension) {
602 installer->InitializeCreationFlagsForUpdate(extension, creation_flags);
[email protected]c30bda262014-06-19 04:10:13603 installer->set_do_not_sync(extension_prefs_->DoNotSync(id));
Minh X. Nguyen30975342017-12-04 22:02:41604 } else {
605 installer->set_creation_flags(creation_flags);
606 }
[email protected]044e86992014-01-24 22:59:11607 installer->set_delete_source(file_ownership_passed);
[email protected]cb0e50312011-05-09 15:03:07608 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
ginkage553af3202015-02-04 12:39:09609 installer->InstallCrxFile(file);
[email protected]420a0ec2011-06-01 01:07:03610
611 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09612 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03613
614 return true;
[email protected]e957fe52009-06-23 16:51:05615}
616
catmullings98cd1942016-08-30 22:31:59617void ExtensionService::LoadExtensionsFromCommandLineFlag(
618 const char* switch_name) {
619 if (command_line_->HasSwitch(switch_name)) {
620 base::CommandLine::StringType path_list =
621 command_line_->GetSwitchValueNative(switch_name);
622 base::StringTokenizerT<base::CommandLine::StringType,
623 base::CommandLine::StringType::const_iterator>
624 t(path_list, FILE_PATH_LITERAL(","));
625 while (t.GetNext()) {
626 std::string extension_id;
627 extensions::UnpackedInstaller::Create(this)->LoadFromCommandLine(
628 base::FilePath(t.token()), &extension_id, false /*only-allow-apps*/);
629 // Extension id is added to whitelist after its extension is loaded
michaelpga8ea0372017-04-06 20:41:35630 // because code is executed asynchronously. TODO(michaelpg): Remove this
631 // assumption so loading extensions does not have to be asynchronous:
632 // crbug.com/708354.
catmullings98cd1942016-08-30 22:31:59633 if (switch_name == switches::kDisableExtensionsExcept)
634 disable_flag_exempted_extensions_.insert(extension_id);
635 }
636 }
637}
638
[email protected]bca4b832014-07-17 20:22:34639void ExtensionService::ReloadExtensionImpl(
[email protected]757d60a2014-05-23 00:11:44640 // "transient" because the process of reloading may cause the reference
641 // to become invalid. Instead, use |extension_id|, a copy.
[email protected]bca4b832014-07-17 20:22:34642 const std::string& transient_extension_id,
643 bool be_noisy) {
[email protected]a29a517a2011-01-21 21:11:12644 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05645
646 // If the extension is already reloading, don't reload again.
[email protected]757d60a2014-05-23 00:11:44647 if (extension_prefs_->GetDisableReasons(transient_extension_id) &
Minh X. Nguyen45479012017-08-18 21:35:36648 extensions::disable_reason::DISABLE_RELOAD) {
[email protected]2a947202013-03-06 04:58:05649 return;
650 }
651
mlerman6a37b6a42014-11-26 22:10:53652 // Ignore attempts to reload a blacklisted or blocked extension. Sometimes
653 // this can happen in a convoluted reload sequence triggered by the
654 // termination of a blacklisted or blocked extension and a naive attempt to
655 // reload it. For an example see http://crbug.com/373842.
656 if (registry_->blacklisted_extensions().Contains(transient_extension_id) ||
657 registry_->blocked_extensions().Contains(transient_extension_id)) {
[email protected]757d60a2014-05-23 00:11:44658 return;
mlerman6a37b6a42014-11-26 22:10:53659 }
[email protected]757d60a2014-05-23 00:11:44660
[email protected]650b2d52013-02-10 03:41:45661 base::FilePath path;
[email protected]757d60a2014-05-23 00:11:44662
663 std::string extension_id = transient_extension_id;
664 const Extension* transient_current_extension =
665 GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40666
[email protected]f17dbd42010-08-16 23:21:10667 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]757d60a2014-05-23 00:11:44668 if (transient_current_extension) {
[email protected]4814b512009-11-07 00:12:29669 // If the extension has an inspector open for its background page, detach
670 // the inspector and hang onto a cookie for it, so that we can reattach
671 // later.
[email protected]31d8f5f22012-04-02 15:22:08672 // TODO(yoz): this is not incognito-safe!
reillyg0ea3fa902014-10-28 15:30:23673 extensions::ProcessManager* manager =
674 extensions::ProcessManager::Get(profile_);
[email protected]3a1dc572012-07-31 22:25:13675 extensions::ExtensionHost* host =
676 manager->GetBackgroundHostForExtension(extension_id);
[email protected]b3f957e62014-08-08 10:09:02677 if (host && DevToolsAgentHost::HasFor(host->host_contents())) {
[email protected]4814b512009-11-07 00:12:29678 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09679 scoped_refptr<DevToolsAgentHost> agent_host =
[email protected]b3f957e62014-08-08 10:09:02680 DevToolsAgentHost::GetOrCreateFor(host->host_contents());
681 agent_host->DisconnectWebContents();
[email protected]04ea1bb2013-07-10 09:26:09682 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29683 }
684
[email protected]757d60a2014-05-23 00:11:44685 path = transient_current_extension->path();
[email protected]58076192013-07-19 19:43:50686 // BeingUpgraded is set back to false when the extension is added.
rdevlin.cronin2c16c602014-11-21 01:35:49687 system_->runtime_data()->SetBeingUpgraded(transient_current_extension->id(),
[email protected]757d60a2014-05-23 00:11:44688 true);
Minh X. Nguyen45479012017-08-18 21:35:36689 DisableExtension(extension_id, extensions::disable_reason::DISABLE_RELOAD);
rdevlin.cronin07f10c12017-02-08 19:37:58690 DCHECK(registry_->disabled_extensions().Contains(extension_id));
[email protected]b914e292013-04-26 07:10:03691 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16692 } else {
[email protected]d32c02a2014-06-16 22:58:36693 std::map<std::string, base::FilePath>::const_iterator iter =
694 unloaded_extension_paths_.find(extension_id);
695 if (iter == unloaded_extension_paths_.end()) {
696 return;
697 }
[email protected]1eb175082010-02-10 09:26:16698 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06699 }
700
achuith2f5578b2016-02-26 21:57:13701 transient_current_extension = nullptr;
[email protected]757d60a2014-05-23 00:11:44702
Toni Barzic667db0d32018-01-09 18:00:19703 if (delayed_installs_.Contains(extension_id) &&
704 FinishDelayedInstallationIfReady(extension_id,
705 true /*install_immediately*/)) {
[email protected]0db124b02012-11-07 04:55:05706 return;
707 }
708
[email protected]43ceb002012-02-10 23:19:15709 // If we're reloading a component extension, use the component extension
710 // loader's reloader.
711 if (component_loader_->Exists(extension_id)) {
712 component_loader_->Reload(extension_id);
713 return;
714 }
715
[email protected]e6090e42010-03-23 22:44:08716 // Check the installed extensions to see if what we're reloading was already
717 // installed.
dchengc963c7142016-04-08 03:55:22718 std::unique_ptr<ExtensionInfo> installed_extension(
[email protected]e6090e42010-03-23 22:44:08719 extension_prefs_->GetInstalledExtensionInfo(extension_id));
720 if (installed_extension.get() &&
721 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01722 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08723 } else {
[email protected]d8c8f25f2011-11-02 18:18:01724 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08725 // We should always be able to remember the extension's path. If it's not in
726 // the map, someone failed to update |unloaded_extension_paths_|.
727 CHECK(!path.empty());
[email protected]bca4b832014-07-17 20:22:34728 scoped_refptr<extensions::UnpackedInstaller> unpacked_installer =
729 extensions::UnpackedInstaller::Create(this);
730 unpacked_installer->set_be_noisy_on_failure(be_noisy);
731 unpacked_installer->Load(path);
[email protected]e6090e42010-03-23 22:44:08732 }
[email protected]9cddd4702009-07-27 22:09:40733}
734
[email protected]bca4b832014-07-17 20:22:34735void ExtensionService::ReloadExtension(const std::string& extension_id) {
asargentd50b18c2016-04-21 01:17:16736 ReloadExtensionImpl(extension_id, true); // be_noisy
[email protected]bca4b832014-07-17 20:22:34737}
738
739void ExtensionService::ReloadExtensionWithQuietFailure(
740 const std::string& extension_id) {
asargentd50b18c2016-04-21 01:17:16741 ReloadExtensionImpl(extension_id, false); // be_noisy
[email protected]bca4b832014-07-17 20:22:34742}
743
[email protected]757d60a2014-05-23 00:11:44744bool ExtensionService::UninstallExtension(
745 // "transient" because the process of uninstalling may cause the reference
746 // to become invalid. Instead, use |extenson->id()|.
747 const std::string& transient_extension_id,
[email protected]e43c61f2014-07-20 21:46:34748 extensions::UninstallReason reason,
[email protected]757d60a2014-05-23 00:11:44749 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12750 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20751
[email protected]757d60a2014-05-23 00:11:44752 scoped_refptr<const Extension> extension =
753 GetInstalledExtension(transient_extension_id);
[email protected]631cf822009-05-15 07:01:25754
[email protected]e7afe2452010-08-22 16:19:13755 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09756 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32757
binjincccacef2014-10-13 19:00:20758 ManagementPolicy* by_policy = system_->management_policy();
[email protected]95da88c42011-03-31 10:07:33759 // Policy change which triggers an uninstall will always set
760 // |external_uninstall| to true so this is the only way to uninstall
761 // managed extensions.
[email protected]90878c52014-04-04 18:21:02762 // Shared modules being uninstalled will also set |external_uninstall| to true
763 // so that we can guarantee users don't uninstall a shared module.
764 // (crbug.com/273300)
765 // TODO(rdevlin.cronin): This is probably not right. We should do something
766 // else, like include an enum IS_INTERNAL_UNINSTALL or IS_USER_UNINSTALL so
767 // we don't do this.
[email protected]cc2f55c2014-07-08 02:19:04768 bool external_uninstall =
[email protected]e43c61f2014-07-20 21:46:34769 (reason == extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT) ||
dtseng9fb3d5b2015-02-23 17:24:17770 (reason == extensions::UNINSTALL_REASON_COMPONENT_REMOVED) ||
rdevlin.cronin5f6b69d2014-09-20 01:23:35771 (reason == extensions::UNINSTALL_REASON_REINSTALL) ||
[email protected]e43c61f2014-07-20 21:46:34772 (reason == extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION) ||
[email protected]6ef60012014-07-22 19:08:12773 (reason == extensions::UNINSTALL_REASON_ORPHANED_SHARED_MODULE) ||
774 (reason == extensions::UNINSTALL_REASON_SYNC &&
mamir192d7882016-06-22 17:10:16775 extensions::util::WasInstalledByCustodian(extension->id(), profile_));
[email protected]65187152012-06-02 13:14:14776 if (!external_uninstall &&
binjincccacef2014-10-13 19:00:20777 (!by_policy->UserMayModifySettings(extension.get(), error) ||
778 by_policy->MustRemainInstalled(extension.get(), error))) {
[email protected]ad50def52011-10-19 23:17:07779 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06780 extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53781 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09782 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33783 return false;
784 }
[email protected]95da88c42011-03-31 10:07:33785
juncai33e462102015-05-18 20:48:44786 InstallVerifier::Get(GetBrowserContext())->Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:46787
[email protected]9b217652010-10-08 22:04:23788 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08789 extension->GetType(), 100);
treib2e0517f92015-05-07 23:15:36790 RecordPermissionMessagesHistogram(extension.get(), "Uninstall");
[email protected]9b217652010-10-08 22:04:23791
[email protected]831aa212010-03-26 13:55:19792 // Unload before doing more cleanup to ensure that nothing is hanging on to
793 // any of these resources.
limasdf0deef2042017-05-03 19:17:17794 UnloadExtension(extension->id(), UnloadedExtensionReason::UNINSTALL);
asargent96c7ec42016-05-27 02:45:47795 if (registry_->blacklisted_extensions().Contains(extension->id()))
796 registry_->RemoveBlacklisted(extension->id());
[email protected]831aa212010-03-26 13:55:19797
[email protected]9f1087e2009-06-15 17:29:32798 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05799 if (!Manifest::IsUnpackedLocation(extension->location())) {
Devlin Croninad3c52b2017-08-10 01:49:37800 if (!extensions::GetExtensionFileTaskRunner()->PostTask(
[email protected]7f8f24f2012-11-15 19:40:14801 FROM_HERE,
tzik8d880ee2017-04-20 19:46:24802 base::BindOnce(&ExtensionService::UninstallExtensionOnFileThread,
803 extension->id(), profile_, install_directory_,
804 extension->path())))
[email protected]14908b72011-04-20 06:54:36805 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32806 }
807
Devlin Cronina68c2a852017-11-29 19:11:26808 extensions::DataDeleter::StartDeleting(profile_, extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43809
Michael Giuffridafcb420362017-10-11 21:50:26810 extension_registrar_.UntrackTerminatedExtension(extension->id());
[email protected]211030342010-09-30 18:41:06811
812 // Notify interested parties that we've uninstalled this extension.
[email protected]e43c61f2014-07-20 21:46:34813 ExtensionRegistry::Get(profile_)
814 ->TriggerOnUninstalled(extension.get(), reason);
[email protected]d6ebc9792011-04-07 18:18:33815
[email protected]757d60a2014-05-23 00:11:44816 delayed_installs_.Remove(extension->id());
[email protected]0db124b02012-11-07 04:55:05817
[email protected]757d60a2014-05-23 00:11:44818 extension_prefs_->OnExtensionUninstalled(
819 extension->id(), extension->location(), external_uninstall);
[email protected]d4eda592013-09-18 03:37:57820
[email protected]333b1de2011-09-12 18:28:50821 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18822 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
823
[email protected]d6ebc9792011-04-07 18:18:33824 return true;
[email protected]c10da4b02010-03-25 14:38:32825}
826
[email protected]5a145e82014-05-29 22:19:07827// static
828void ExtensionService::UninstallExtensionOnFileThread(
829 const std::string& id,
830 Profile* profile,
831 const base::FilePath& install_dir,
832 const base::FilePath& extension_path) {
833 extensions::ExtensionAssetsManager* assets_manager =
834 extensions::ExtensionAssetsManager::GetInstance();
835 assets_manager->UninstallExtension(id, profile, install_dir, extension_path);
836}
837
[email protected]c3cfb012011-04-06 22:07:35838bool ExtensionService::IsExtensionEnabled(
839 const std::string& extension_id) const {
Michael Giuffrida6397c592017-10-09 20:46:53840 return extension_registrar_.IsExtensionEnabled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35841}
842
[email protected]eaa7dd182010-12-14 11:09:00843void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12844 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20845
rdevlin.croninf87d15f2017-01-26 22:57:19846 // If the extension is currently reloading, it will be enabled once the reload
847 // is complete.
848 if (reloading_extensions_.count(extension_id) > 0)
849 return;
850
Michael Giuffrida6397c592017-10-09 20:46:53851 extension_registrar_.EnableExtension(extension_id);
[email protected]0c6da502009-08-14 22:32:39852}
853
treibc1192322015-05-20 12:56:07854void ExtensionService::DisableExtension(const std::string& extension_id,
855 int disable_reasons) {
[email protected]a29a517a2011-01-21 21:11:12856 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
Michael Giuffrida6397c592017-10-09 20:46:53857 extension_registrar_.DisableExtension(extension_id, disable_reasons);
[email protected]1784e83a2009-09-08 21:01:52858}
859
mtomasz294bb3e2017-01-24 02:17:52860void ExtensionService::DisableUserExtensionsExcept(
[email protected]1abf05e2013-07-09 17:04:36861 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14862 extensions::ManagementPolicy* management_policy =
863 system_->management_policy();
864 extensions::ExtensionList to_disable;
865
achuith2f5578b2016-02-26 21:57:13866 for (const auto& extension : registry_->enabled_extensions()) {
867 if (management_policy->UserMayModifySettings(extension.get(), nullptr))
868 to_disable.push_back(extension);
[email protected]e516e4c2013-06-12 17:41:14869 }
870
achuith2f5578b2016-02-26 21:57:13871 for (const auto& extension : registry_->terminated_extensions()) {
872 if (management_policy->UserMayModifySettings(extension.get(), nullptr))
873 to_disable.push_back(extension);
874 }
875
876 for (const auto& extension : to_disable) {
877 if (extension->was_installed_by_default() &&
[email protected]3082fe32013-08-06 11:12:38878 extension_urls::IsWebstoreUpdateUrl(
achuith2f5578b2016-02-26 21:57:13879 extensions::ManifestURL::GetUpdateURL(extension.get())))
[email protected]3082fe32013-08-06 11:12:38880 continue;
achuith2f5578b2016-02-26 21:57:13881 const std::string& id = extension->id();
tripta.g0ac673a2017-07-07 05:45:09882 if (!base::ContainsValue(except_ids, id))
Minh X. Nguyen45479012017-08-18 21:35:36883 DisableExtension(id, extensions::disable_reason::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:14884 }
885}
886
mlerman6a37b6a42014-11-26 22:10:53887// Extensions that are not locked, components or forced by policy should be
888// locked. Extensions are no longer considered enabled or disabled. Blacklisted
889// extensions are now considered both blacklisted and locked.
890void ExtensionService::BlockAllExtensions() {
891 if (block_extensions_)
892 return;
893 block_extensions_ = true;
894
895 // Blacklisted extensions are already unloaded, need not be blocked.
dchengc963c7142016-04-08 03:55:22896 std::unique_ptr<ExtensionSet> extensions =
mlerman6a37b6a42014-11-26 22:10:53897 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::ENABLED |
898 ExtensionRegistry::DISABLED |
899 ExtensionRegistry::TERMINATED);
900
achuith2f5578b2016-02-26 21:57:13901 for (const auto& extension : *extensions) {
mlerman6a37b6a42014-11-26 22:10:53902 const std::string& id = extension->id();
903
904 if (!CanBlockExtension(extension.get()))
905 continue;
906
907 registry_->RemoveEnabled(id);
908 registry_->RemoveDisabled(id);
909 registry_->RemoveTerminated(id);
910
911 registry_->AddBlocked(extension.get());
limasdf0deef2042017-05-03 19:17:17912 UnloadExtension(id, extensions::UnloadedExtensionReason::LOCK_ALL);
mlerman6a37b6a42014-11-26 22:10:53913 }
914}
915
916// All locked extensions should revert to being either enabled or disabled
917// as appropriate.
918void ExtensionService::UnblockAllExtensions() {
919 block_extensions_ = false;
dchengc963c7142016-04-08 03:55:22920 std::unique_ptr<ExtensionSet> to_unblock =
mlerman6a37b6a42014-11-26 22:10:53921 registry_->GenerateInstalledExtensionsSet(ExtensionRegistry::BLOCKED);
922
achuith2f5578b2016-02-26 21:57:13923 for (const auto& extension : *to_unblock) {
mlerman6a37b6a42014-11-26 22:10:53924 registry_->RemoveBlocked(extension->id());
925 AddExtension(extension.get());
926 }
Owen Minb71016d2018-01-11 01:51:49927 // While extensions are blocked, we won't display any external install
928 // warnings. Now that they are unblocked, we should update the error.
929 external_install_manager_->UpdateExternalExtensionAlert();
mlerman6a37b6a42014-11-26 22:10:53930}
931
[email protected]eaa7dd182010-12-14 11:09:00932void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:28933 const Extension* extension) {
934 GrantPermissions(extension);
treib2e0517f92015-05-07 23:15:36935 RecordPermissionMessagesHistogram(extension, "ReEnable");
[email protected]8d888c12010-11-30 00:00:25936 EnableExtension(extension->id());
937}
938
[email protected]009633c2013-03-07 22:08:28939void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:45940 CHECK(extension);
[email protected]8d42db52014-06-20 21:50:50941 extensions::PermissionsUpdater(profile()).GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:45942}
943
[email protected]fe2dd7742011-04-19 22:52:49944// static
945void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:05946 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:17947 // Since this is called from multiple sources, and since the histogram macros
948 // use statics, we need to manually lookup the histogram ourselves.
treib2e0517f92015-05-07 23:15:36949 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
950 base::StringPrintf("Extensions.Permissions_%s3", histogram),
951 1,
952 APIPermission::kEnumBoundary,
953 APIPermission::kEnumBoundary + 1,
954 base::HistogramBase::kUmaTargetedHistogramFlag);
955
956 base::HistogramBase* counter_has_any = base::BooleanHistogram::FactoryGet(
957 base::StringPrintf("Extensions.HasPermissions_%s3", histogram),
958 base::HistogramBase::kUmaTargetedHistogramFlag);
959
960 PermissionIDSet permissions =
961 extensions::PermissionMessageProvider::Get()->GetAllPermissionIDs(
rdevlin.cronine2d0fd02015-09-24 22:35:49962 extension->permissions_data()->active_permissions(),
treib2e0517f92015-05-07 23:15:36963 extension->GetType());
964 counter_has_any->AddBoolean(!permissions.empty());
965 for (const PermissionID& id : permissions)
966 counter->Add(id.id());
[email protected]fe2dd7742011-04-19 22:52:49967}
968
Michael Giuffrida6397c592017-10-09 20:46:53969// TODO(michaelpg): Group with other ExtensionRegistrar::Delegate overrides
970// according to header file once diffs have settled down.
971void ExtensionService::PostActivateExtension(
972 scoped_refptr<const Extension> extension,
973 bool is_newly_added) {
[email protected]dcc47642014-03-26 22:03:49974 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
975 // BrowserContextKeyedService and use ExtensionRegistryObserver.
Michael Giuffrida6397c592017-10-09 20:46:53976 profile_->GetExtensionSpecialStoragePolicy()->GrantRightsForExtension(
977 extension.get(), profile_);
[email protected]c8d407e2011-04-28 21:27:17978
[email protected]dcc47642014-03-26 22:03:49979 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
980 // work properly multi-profile. Besides which, it should be using
981 // ExtensionRegistryObserver. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:17982 UpdateActiveExtensionsInCrashReporter();
983
[email protected]d695bb12014-06-05 16:16:30984 const extensions::PermissionsData* permissions_data =
[email protected]a6910e72014-06-06 05:04:36985 extension->permissions_data();
[email protected]d695bb12014-06-05 16:16:30986
[email protected]c8d407e2011-04-28 21:27:17987 // If the extension has permission to load chrome://favicon/ resources we need
988 // to make sure that the FaviconSource is registered with the
989 // ChromeURLDataManager.
[email protected]d695bb12014-06-05 16:16:30990 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
pkotwiczd95aa6592017-05-11 02:26:21991 FaviconSource* favicon_source = new FaviconSource(profile_);
[email protected]24ea7a12013-01-27 23:54:53992 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:17993 }
[email protected]b07e606e2012-09-15 20:16:15994
[email protected]b07e606e2012-09-15 20:16:15995 // Same for chrome://theme/ resources.
[email protected]d695bb12014-06-05 16:16:30996 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:15997 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:53998 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:15999 }
[email protected]b07e606e2012-09-15 20:16:151000
[email protected]5eddc3e2011-10-26 04:33:311001 // Same for chrome://thumb/ resources.
[email protected]d695bb12014-06-05 16:16:301002 if (permissions_data->HasHostPermission(
1003 GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511004 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531005 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311006 }
Michael Giuffrida6397c592017-10-09 20:46:531007
1008 // When an existing extension is re-enabled, it may be necessary to spin up
1009 // its lazy background page.
1010 if (!is_newly_added)
1011 MaybeSpinUpLazyBackgroundPage(extension.get());
[email protected]62d30f42009-10-01 22:36:061012}
1013
Michael Giuffrida6397c592017-10-09 20:46:531014// TODO(michaelpg): Group with other ExtensionRegistrar::Delegate overrides
1015// according to header file once diffs have settled down.
Michael Giuffrida023e3532017-09-28 23:21:211016void ExtensionService::PostDeactivateExtension(
1017 scoped_refptr<const Extension> extension) {
[email protected]dcc47642014-03-26 22:03:491018 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1019 // BrowserContextKeyedService and use ExtensionRegistryObserver.
Michael Giuffrida023e3532017-09-28 23:21:211020 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForExtension(
1021 extension.get());
[email protected]c8d407e2011-04-28 21:27:171022
[email protected]b777b332011-04-16 04:01:081023#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231024 // Revoke external file access for the extension from its file system context.
1025 // It is safe to access the extension's storage partition at this point. The
1026 // storage partition may get destroyed only after the extension gets unloaded.
[email protected]3a746ec2014-03-15 05:30:561027 GURL site =
1028 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
[email protected]cd501a72014-08-22 19:58:311029 storage::FileSystemContext* filesystem_context =
1030 BrowserContext::GetStoragePartitionForSite(profile_, site)
1031 ->GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321032 if (filesystem_context && filesystem_context->external_backend()) {
Michael Giuffrida6397c592017-10-09 20:46:531033 filesystem_context->external_backend()->RevokeAccessForExtension(
1034 extension->id());
[email protected]62d30f42009-10-01 22:36:061035 }
[email protected]c8d407e2011-04-28 21:27:171036#endif
1037
[email protected]dcc47642014-03-26 22:03:491038 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1039 // work properly multi-profile. Besides which, it should be using
1040 // ExtensionRegistryObserver::OnExtensionLoaded. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171041 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061042}
1043
[email protected]599539802014-01-07 23:06:001044content::BrowserContext* ExtensionService::GetBrowserContext() const {
1045 // Implemented in the .cc file to avoid adding a profile.h dependency to
1046 // extension_service.h.
1047 return profile_;
1048}
1049
[email protected]25ae0152011-11-18 14:40:021050bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351051 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021052}
1053
[email protected]4ee07c62012-08-21 12:40:421054void ExtensionService::CheckManagementPolicy() {
Minh X. Nguyen45479012017-08-18 21:35:361055 std::map<std::string, extensions::disable_reason::DisableReason> to_disable;
binjin8e3d0182014-12-04 16:44:281056 std::vector<std::string> to_enable;
[email protected]695b5712012-12-06 23:55:281057
Karan Bhatia2a117232017-08-23 00:24:561058 // Loop through the extensions list, finding extensions we need to disable.
achuith2f5578b2016-02-26 21:57:131059 for (const auto& extension : registry_->enabled_extensions()) {
Minh X. Nguyen45479012017-08-18 21:35:361060 extensions::disable_reason::DisableReason disable_reason =
1061 extensions::disable_reason::DISABLE_NONE;
[email protected]0d54b682013-11-05 14:15:361062 if (system_->management_policy()->MustRemainDisabled(
binjin8e3d0182014-12-04 16:44:281063 extension.get(), &disable_reason, nullptr))
[email protected]0d54b682013-11-05 14:15:361064 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251065 }
1066
binjin8e3d0182014-12-04 16:44:281067 extensions::ExtensionManagement* management =
1068 extensions::ExtensionManagementFactory::GetForBrowserContext(profile());
nrpetere33d2a5b2017-04-25 00:12:311069 extensions::PermissionsUpdater(profile()).SetDefaultPolicyHostRestrictions(
1070 management->GetDefaultRuntimeBlockedHosts(),
1071 management->GetDefaultRuntimeAllowedHosts());
1072 for (const auto& extension : registry_->enabled_extensions()) {
1073 bool uses_default =
1074 management->UsesDefaultRuntimeHostRestrictions(extension.get());
1075 if (uses_default) {
1076 extensions::PermissionsUpdater(profile()).SetUsesDefaultHostRestrictions(
1077 extension.get());
1078 } else {
1079 extensions::PermissionsUpdater(profile()).SetPolicyHostRestrictions(
1080 extension.get(), management->GetRuntimeBlockedHosts(extension.get()),
1081 management->GetRuntimeAllowedHosts(extension.get()));
1082 }
1083 }
binjin8e3d0182014-12-04 16:44:281084
1085 // Loop through the disabled extension list, find extensions to re-enable
Karan Bhatia2a117232017-08-23 00:24:561086 // automatically. These extensions are exclusive from the |to_disable| list
1087 // constructed above, since disabled_extensions() and enabled_extensions() are
1088 // supposed to be mutually exclusive.
achuith2f5578b2016-02-26 21:57:131089 for (const auto& extension : registry_->disabled_extensions()) {
Karan Bhatia2a117232017-08-23 00:24:561090 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
binjin8e3d0182014-12-04 16:44:281091
Karan Bhatia2a117232017-08-23 00:24:561092 // Find all extensions disabled due to minimum version requirement and
1093 // management policy but now satisfying it.
1094 if (management->CheckMinimumVersion(extension.get(), nullptr)) {
1095 disable_reasons &=
1096 (~extensions::disable_reason::DISABLE_UPDATE_REQUIRED_BY_POLICY);
1097 }
1098
1099 if (!system_->management_policy()->MustRemainDisabled(extension.get(),
1100 nullptr, nullptr)) {
1101 disable_reasons &=
1102 (~extensions::disable_reason::DISABLE_BLOCKED_BY_POLICY);
1103 }
1104
1105 extension_prefs_->ReplaceDisableReasons(extension->id(), disable_reasons);
1106 if (disable_reasons == extensions::disable_reason::DISABLE_NONE)
1107 to_enable.push_back(extension->id());
1108 }
[email protected]0d54b682013-11-05 14:15:361109
achuith2f5578b2016-02-26 21:57:131110 for (const auto& i : to_disable)
1111 DisableExtension(i.first, i.second);
binjin8e3d0182014-12-04 16:44:281112
Karan Bhatia2a117232017-08-23 00:24:561113 // No extension is getting re-enabled here after disabling because |to_enable|
1114 // is mutually exclusive to |to_disable|.
binjin8e3d0182014-12-04 16:44:281115 for (const std::string& id : to_enable)
1116 EnableExtension(id);
1117
1118 if (updater_.get()) {
1119 // Find all extensions disabled due to minimum version requirement from
1120 // policy (including the ones that got disabled just now), and check
1121 // for update.
1122 extensions::ExtensionUpdater::CheckParams to_recheck;
achuith2f5578b2016-02-26 21:57:131123 for (const auto& extension : registry_->disabled_extensions()) {
binjin8e3d0182014-12-04 16:44:281124 if (extension_prefs_->GetDisableReasons(extension->id()) ==
Minh X. Nguyen45479012017-08-18 21:35:361125 extensions::disable_reason::DISABLE_UPDATE_REQUIRED_BY_POLICY) {
binjin8e3d0182014-12-04 16:44:281126 // The minimum version check is the only thing holding this extension
1127 // back, so check if it can be updated to fix that.
1128 to_recheck.ids.push_back(extension->id());
1129 }
1130 }
1131 if (!to_recheck.ids.empty())
1132 updater_->CheckNow(to_recheck);
1133 }
[email protected]aa96d3a2010-08-21 08:45:251134}
1135
[email protected]31206602011-04-13 23:07:321136void ExtensionService::CheckForUpdatesSoon() {
[email protected]90878c52014-04-04 18:21:021137 // This can legitimately happen in unit tests.
1138 if (!updater_.get())
1139 return;
1140
raphael.kubo.da.costa8527f942016-11-15 17:03:511141 updater_->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351142}
1143
[email protected]8e4560b62011-01-14 10:09:141144// Some extensions will autoupdate themselves externally from Chrome. These
rkaplowa8fd8d32015-02-25 21:27:561145// are typically part of some larger client application package. To support
1146// these, the extension will register its location in the preferences file
[email protected]8e4560b62011-01-14 10:09:141147// (and also, on Windows, in the registry) and this code will periodically
1148// check that location for a .crx file, which it will then install locally if
1149// a new version is available.
Devlin Cronin9722a722017-12-16 03:35:101150// Errors are reported through LoadErrorReporter. Success is not reported.
[email protected]eaa7dd182010-12-14 11:09:001151void ExtensionService::CheckForExternalUpdates() {
lazyboy77214d3c2017-04-04 16:46:121152 if (external_updates_disabled_for_test_)
1153 return;
1154
[email protected]a29a517a2011-01-21 21:11:121155 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
rkaplowa8fd8d32015-02-25 21:27:561156 TRACE_EVENT0("browser,startup", "ExtensionService::CheckForExternalUpdates");
1157 SCOPED_UMA_HISTOGRAM_TIMER("Extensions.CheckForExternalUpdatesTime");
[email protected]8e4560b62011-01-14 10:09:141158
1159 // Note that this installation is intentionally silent (since it didn't
1160 // go through the front-end). Extensions that are registered in this
1161 // way are effectively considered 'pre-bundled', and so implicitly
1162 // trusted. In general, if something has HKLM or filesystem access,
1163 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121164
[email protected]8e4560b62011-01-14 10:09:141165 // Ask each external extension provider to give us a call back for each
1166 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
achuith2f5578b2016-02-26 21:57:131167 for (const auto& provider : external_extension_providers_)
[email protected]8e4560b62011-01-14 10:09:141168 provider->VisitRegisteredExtension();
[email protected]8e4560b62011-01-14 10:09:141169
[email protected]50067e52011-10-20 23:17:071170 // Do any required work that we would have done after completion of all
1171 // providers.
[email protected]373daf972014-04-10 01:50:441172 if (external_extension_providers_.empty())
[email protected]50067e52011-10-20 23:17:071173 OnAllExternalProvidersReady();
[email protected]9f1087e2009-06-15 17:29:321174}
1175
[email protected]50067e52011-10-20 23:17:071176void ExtensionService::OnExternalProviderReady(
lazyboye8634172016-01-28 00:10:481177 const ExternalProviderInterface* provider) {
Istiaque Ahmedf6e72622017-09-08 23:14:171178 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]50067e52011-10-20 23:17:071179 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121180
1181 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141182 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301183 if (AreAllExternalProvidersReady())
1184 OnAllExternalProvidersReady();
1185}
1186
1187bool ExtensionService::AreAllExternalProvidersReady() const {
achuith2f5578b2016-02-26 21:57:131188 for (const auto& provider : external_extension_providers_) {
1189 if (!provider->IsReady())
[email protected]94fde232012-04-27 10:22:301190 return false;
[email protected]8e4560b62011-01-14 10:09:141191 }
[email protected]94fde232012-04-27 10:22:301192 return true;
[email protected]50067e52011-10-20 23:17:071193}
1194
1195void ExtensionService::OnAllExternalProvidersReady() {
Istiaque Ahmedf6e72622017-09-08 23:14:171196 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]94fde232012-04-27 10:22:301197 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1198 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071199
1200 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301201 if (update_once_all_providers_are_ready_ && updater()) {
1202 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141203 extensions::ExtensionUpdater::CheckParams params;
1204 params.callback = external_updates_finished_callback_;
1205 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141206 }
1207
1208 // Uninstall all the unclaimed extensions.
dchengc963c7142016-04-08 03:55:221209 std::unique_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141210 extension_prefs_->GetInstalledExtensionsInfo());
1211 for (size_t i = 0; i < extensions_info->size(); ++i) {
1212 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401213 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141214 CheckExternalUninstall(info->extension_id);
1215 }
[email protected]e5af875f2011-10-10 21:09:141216
[email protected]373daf972014-04-10 01:50:441217 error_controller_->ShowErrorIfNeeded();
[email protected]a9aa5932012-01-25 08:27:401218
[email protected]374ceb6f2014-07-02 19:25:341219 external_install_manager_->UpdateExternalExtensionAlert();
[email protected]e5af875f2011-10-10 21:09:141220}
1221
limasdf0deef2042017-05-03 19:17:171222void ExtensionService::UnloadExtension(const std::string& extension_id,
1223 UnloadedExtensionReason reason) {
Michael Giuffridafcb420362017-10-11 21:50:261224 UpdateForUnloadingExtension(extension_id);
Devlin Cronin5b35a082017-10-05 20:44:361225 extension_registrar_.RemoveExtension(extension_id, reason);
[email protected]631cf822009-05-15 07:01:251226}
1227
[email protected]8b1ec202013-09-05 02:09:501228void ExtensionService::RemoveComponentExtension(
1229 const std::string& extension_id) {
1230 scoped_refptr<const Extension> extension(
1231 GetExtensionById(extension_id, false));
limasdf0deef2042017-05-03 19:17:171232 UnloadExtension(extension_id, UnloadedExtensionReason::UNINSTALL);
[email protected]ebe07772014-05-22 04:16:061233 if (extension.get()) {
[email protected]e43c61f2014-07-20 21:46:341234 ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(
dtseng9fb3d5b2015-02-23 17:24:171235 extension.get(), extensions::UNINSTALL_REASON_COMPONENT_REMOVED);
[email protected]ebe07772014-05-22 04:16:061236 }
[email protected]8b1ec202013-09-05 02:09:501237}
1238
[email protected]bb1bc9b32013-12-21 03:09:141239void ExtensionService::UnloadAllExtensionsForTest() {
1240 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321241}
1242
[email protected]bb1bc9b32013-12-21 03:09:141243void ExtensionService::ReloadExtensionsForTest() {
1244 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1245 // warning about calling test code in production.
1246 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451247 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011248 extensions::InstalledLoader(this).LoadAllExtensions();
rdevlin.croninf2e1cb012017-05-27 01:27:591249 OnInstalledExtensionsLoaded();
[email protected]4a10006a2013-05-17 23:18:351250 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1251 // times.
[email protected]9f1087e2009-06-15 17:29:321252}
1253
[email protected]820d9bd2013-04-03 03:46:031254void ExtensionService::SetReadyAndNotifyListeners() {
rkaplowa8fd8d32015-02-25 21:27:561255 TRACE_EVENT0("browser,startup",
1256 "ExtensionService::SetReadyAndNotifyListeners");
1257 SCOPED_UMA_HISTOGRAM_TIMER(
1258 "Extensions.ExtensionServiceNotifyReadyListenersTime");
1259
[email protected]4a10006a2013-05-17 23:18:351260 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031261 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061262 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
[email protected]820d9bd2013-04-03 03:46:031263 content::Source<Profile>(profile_),
1264 content::NotificationService::NoDetails());
1265}
1266
[email protected]fcb58a862012-05-01 01:03:151267void ExtensionService::AddExtension(const Extension* extension) {
rdevlin.cronin08ada0f2017-03-09 23:29:521268 if (!Manifest::IsValidLocation(extension->location())) {
1269 // TODO(devlin): We should *never* add an extension with an invalid
1270 // location, but some bugs (e.g. crbug.com/692069) seem to indicate we do.
1271 // Track down the cases when this can happen, and remove this
1272 // DumpWithoutCrashing() (possibly replacing it with a CHECK).
1273 NOTREACHED();
1274 char extension_id_copy[33];
1275 base::strlcpy(extension_id_copy, extension->id().c_str(),
1276 arraysize(extension_id_copy));
1277 Manifest::Location location = extension->location();
1278 int creation_flags = extension->creation_flags();
1279 Manifest::Type type = extension->manifest()->type();
1280 base::debug::Alias(extension_id_copy);
1281 base::debug::Alias(&location);
1282 base::debug::Alias(&creation_flags);
1283 base::debug::Alias(&type);
1284 base::debug::DumpWithoutCrashing();
1285 return;
1286 }
1287
[email protected]c8d407e2011-04-28 21:27:171288 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1289 // default extension state to DISABLED when the --disable-extensions flag
1290 // is set (http://crbug.com/29067).
catmullings98cd1942016-08-30 22:31:591291 if (!extensions_enabled() && !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401292 extension->location() != Manifest::COMPONENT &&
catmullings98cd1942016-08-30 22:31:591293 !Manifest::IsExternalLocation(extension->location()) &&
1294 disable_flag_exempted_extensions_.count(extension->id()) == 0) {
[email protected]fcb58a862012-05-01 01:03:151295 return;
[email protected]330840c2012-08-29 22:21:011296 }
[email protected]c8d407e2011-04-28 21:27:171297
[email protected]b914e292013-04-26 07:10:031298 bool is_extension_upgrade = false;
treib6e51bca2016-06-15 10:26:031299 bool is_extension_loaded = false;
[email protected]116d40e2013-08-08 17:23:171300 const Extension* old = GetInstalledExtension(extension->id());
1301 if (old) {
treib6e51bca2016-06-15 10:26:031302 is_extension_loaded = true;
Devlin Cronin03bf2d22017-12-20 08:21:051303 int version_compare_result = extension->version().CompareTo(old->version());
[email protected]116d40e2013-08-08 17:23:171304 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e292013-04-26 07:10:031305 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1306 // that we aren't downgrading.
1307 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171308 CHECK_GE(version_compare_result, 0);
[email protected]b914e292013-04-26 07:10:031309 }
Michael Giuffrida023e3532017-09-28 23:21:211310
1311 // If the extension was disabled for a reload, we will enable it.
rdevlin.cronin2c16c602014-11-21 01:35:491312 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
1313
1314 // Set the upgraded bit; we consider reloads upgrades.
1315 system_->runtime_data()->SetBeingUpgraded(extension->id(),
1316 is_extension_upgrade || reloading);
[email protected]c8d407e2011-04-28 21:27:171317
[email protected]1eb175082010-02-10 09:26:161318 // The extension is now loaded, remove its data from unloaded extension map.
1319 unloaded_extension_paths_.erase(extension->id());
1320
[email protected]bb7f40952011-01-13 00:21:201321 // If a terminated extension is loaded, remove it from the terminated list.
Michael Giuffridafcb420362017-10-11 21:50:261322 extension_registrar_.UntrackTerminatedExtension(extension->id());
[email protected]bb7f40952011-01-13 00:21:201323
[email protected]b914e292013-04-26 07:10:031324 // Check if the extension's privileges have changed and mark the
1325 // extension disabled if necessary.
treib6e51bca2016-06-15 10:26:031326 CheckPermissionsIncrease(extension, is_extension_loaded);
[email protected]b914e292013-04-26 07:10:031327
Michael Giuffrida6397c592017-10-09 20:46:531328 if (reloading) {
1329 extension_registrar_.ReplaceReloadedExtension(extension);
[email protected]695b5712012-12-06 23:55:281330 } else {
Michael Giuffrida6397c592017-10-09 20:46:531331 if (is_extension_loaded) {
1332 // To upgrade an extension in place, remove the old one and then activate
1333 // the new one. ReloadExtension disables the extension, which is
1334 // sufficient.
1335 UnloadExtension(extension->id(), UnloadedExtensionReason::UPDATE);
[email protected]695b5712012-12-06 23:55:281336 }
1337
Michael Giuffrida6397c592017-10-09 20:46:531338 extension_registrar_.AddExtension(extension);
1339 if (registry_->disabled_extensions().Contains(extension->id())) {
1340 // Show the extension disabled error if a permissions increase or a remote
1341 // installation is the reason it was disabled, and no other reasons exist.
1342 int reasons = extension_prefs_->GetDisableReasons(extension->id());
1343 const int kReasonMask =
1344 extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE |
1345 extensions::disable_reason::DISABLE_REMOTE_INSTALL;
1346 if (reasons & kReasonMask && !(reasons & ~kReasonMask)) {
1347 AddExtensionDisabledError(
1348 this, extension,
1349 extension_prefs_->HasDisableReason(
1350 extension->id(),
1351 extensions::disable_reason::DISABLE_REMOTE_INSTALL));
1352 }
1353 }
[email protected]f574c402012-12-04 23:20:311354 }
Michael Giuffrida6397c592017-10-09 20:46:531355
rdevlin.cronin2c16c602014-11-21 01:35:491356 system_->runtime_data()->SetBeingUpgraded(extension->id(), false);
[email protected]aab98a52009-12-02 03:22:351357}
1358
[email protected]8c484b742012-11-29 06:05:361359void ExtensionService::AddComponentExtension(const Extension* extension) {
1360 const std::string old_version_string(
1361 extension_prefs_->GetVersionString(extension->id()));
pwnallcbd73192016-08-22 18:59:171362 const base::Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361363
[email protected]0181a7d2013-11-12 01:08:421364 VLOG(1) << "AddComponentExtension " << extension->name();
Devlin Cronin03bf2d22017-12-20 08:21:051365 if (!old_version.IsValid() || old_version != extension->version()) {
[email protected]8c484b742012-11-29 06:05:361366 VLOG(1) << "Component extension " << extension->name() << " ("
Devlin Cronin03bf2d22017-12-20 08:21:051367 << extension->id() << ") installing/upgrading from '"
1368 << old_version_string << "' to "
1369 << extension->version().GetString();
[email protected]8c484b742012-11-29 06:05:361370
Karandeep Bhatiaa8930652017-10-11 17:41:121371 // TODO(crbug.com/696822): If needed, add support for Declarative Net
1372 // Request to component extensions and pass the ruleset checksum here.
1373 AddNewOrUpdatedExtension(
1374 extension, Extension::ENABLED, extensions::kInstallFlagNone,
1375 syncer::StringOrdinal(), std::string(), base::nullopt);
[email protected]8c484b742012-11-29 06:05:361376 return;
1377 }
1378
1379 AddExtension(extension);
1380}
1381
[email protected]b914e292013-04-26 07:10:031382void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
treib6e51bca2016-06-15 10:26:031383 bool is_extension_loaded) {
[email protected]23a85362014-07-07 23:26:191384 extensions::PermissionsUpdater(profile_).InitializePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311385
[email protected]8d888c12010-11-30 00:00:251386 // We keep track of all permissions the user has granted each extension.
1387 // This allows extensions to gracefully support backwards compatibility
1388 // by including unknown permissions in their manifests. When the user
1389 // installs the extension, only the recognized permissions are recorded.
1390 // When the unknown permissions become recognized (e.g., through browser
1391 // upgrade), we can prompt the user to accept these new permissions.
1392 // Extensions can also silently upgrade to less permissions, and then
1393 // silently upgrade to a version that adds these permissions back.
1394 //
1395 // For example, pretend that Chrome 10 includes a permission "omnibox"
1396 // for an API that adds suggestions to the omnibox. An extension can
1397 // maintain backwards compatibility while still having "omnibox" in the
1398 // manifest. If a user installs the extension on Chrome 9, the browser
1399 // will record the permissions it recognized, not including "omnibox."
1400 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1401 // will disable the extension and prompt the user to approve the increase
1402 // in privileges. The extension could then release a new version that
1403 // removes the "omnibox" permission. When the user upgrades, Chrome will
1404 // still remember that "omnibox" had been granted, so that if the
1405 // extension once again includes "omnibox" in an upgrade, the extension
1406 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281407 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251408
rockot1d4a06262015-02-17 19:39:281409 // Silently grant all active permissions to default apps and apps installed
1410 // in kiosk mode.
[email protected]211a3f32013-05-28 21:48:181411 bool auto_grant_permission =
rockot1d4a06262015-02-17 19:39:281412 extension->was_installed_by_default() ||
[email protected]45928b682013-11-28 08:20:271413 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]211a3f32013-05-28 21:48:181414 if (auto_grant_permission)
[email protected]b914e292013-04-26 07:10:031415 GrantPermissions(extension);
1416
1417 bool is_privilege_increase = false;
1418 // We only need to compare the granted permissions to the current permissions
rockot1d4a06262015-02-17 19:39:281419 // if the extension has not been auto-granted its permissions above and is
1420 // installed internally.
1421 if (extension->location() == Manifest::INTERNAL && !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251422 // Add all the recognized permissions if the granted permissions list
1423 // hasn't been initialized yet.
dchengc963c7142016-04-08 03:55:221424 std::unique_ptr<const PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311425 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521426 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251427
1428 // Here, we check if an extension's privileges have increased in a manner
1429 // that requires the user's approval. This could occur because the browser
1430 // upgraded and recognized additional privileges, or an extension upgrades
1431 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251432 is_privilege_increase =
1433 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
rdevlin.cronind630c302015-09-30 20:19:331434 *granted_permissions,
rdevlin.cronine2d0fd02015-09-24 22:35:491435 extension->permissions_data()->active_permissions(),
[email protected]076ebeda2014-06-06 21:47:261436 extension->GetType());
treib2c20f9442016-05-18 19:50:281437
1438 // If there was no privilege increase, the extension might still have new
1439 // permissions (which either don't generate a warning message, or whose
1440 // warning messages are suppressed by existing permissions). Grant the new
1441 // permissions.
1442 if (!is_privilege_increase)
1443 GrantPermissions(extension);
[email protected]8d888c12010-11-30 00:00:251444 }
1445
treib6e51bca2016-06-15 10:26:031446 bool previously_disabled =
1447 extension_prefs_->IsExtensionDisabled(extension->id());
1448 // TODO(treib): Is the |is_extension_loaded| check needed here?
1449 if (is_extension_loaded && previously_disabled) {
treib8a6d9892015-08-26 10:23:191450 // Legacy disabled extensions do not have a disable reason. Infer that it
1451 // was likely disabled by the user.
Minh X. Nguyen45479012017-08-18 21:35:361452 if (disable_reasons == extensions::disable_reason::DISABLE_NONE)
1453 disable_reasons |= extensions::disable_reason::DISABLE_USER_ACTION;
treib8a6d9892015-08-26 10:23:191454
[email protected]7e9e2422012-12-13 19:54:191455 // Extensions that came to us disabled from sync need a similar inference,
1456 // except based on the new version's permissions.
Minh X. Nguyen45479012017-08-18 21:35:361457 // TODO(treib,devlin): Since M48,
1458 // extensions::disable_reason::DISABLE_UNKNOWN_FROM_SYNC isn't used anymore;
1459 // this code is still here to migrate any existing old state. Remove it
1460 // after some grace period.
1461 if (disable_reasons &
1462 extensions::disable_reason::DEPRECATED_DISABLE_UNKNOWN_FROM_SYNC) {
1463 // Remove the extensions::disable_reason::DISABLE_UNKNOWN_FROM_SYNC
1464 // reason.
1465 disable_reasons &=
1466 ~extensions::disable_reason::DEPRECATED_DISABLE_UNKNOWN_FROM_SYNC;
treib8a6d9892015-08-26 10:23:191467 extension_prefs_->RemoveDisableReason(
Minh X. Nguyen45479012017-08-18 21:35:361468 extension->id(),
1469 extensions::disable_reason::DEPRECATED_DISABLE_UNKNOWN_FROM_SYNC);
treib8a6d9892015-08-26 10:23:191470 // If there was no privilege increase, it was likely disabled by the user.
[email protected]7e9e2422012-12-13 19:54:191471 if (!is_privilege_increase)
Minh X. Nguyen45479012017-08-18 21:35:361472 disable_reasons |= extensions::disable_reason::DISABLE_USER_ACTION;
[email protected]7e9e2422012-12-13 19:54:191473 }
[email protected]8d888c12010-11-30 00:00:251474 }
1475
treib6e51bca2016-06-15 10:26:031476 // If the extension is disabled due to a permissions increase, but does in
1477 // fact have all permissions, remove that disable reason.
1478 // TODO(devlin): This was added to fix crbug.com/616474, but it's unclear
1479 // if this behavior should stay forever.
Minh X. Nguyen45479012017-08-18 21:35:361480 if (disable_reasons &
1481 extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE) {
treib6e51bca2016-06-15 10:26:031482 bool reset_permissions_increase = false;
1483 if (!is_privilege_increase) {
1484 reset_permissions_increase = true;
Minh X. Nguyen45479012017-08-18 21:35:361485 disable_reasons &=
1486 ~extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE;
treib6e51bca2016-06-15 10:26:031487 extension_prefs_->RemoveDisableReason(
Minh X. Nguyen45479012017-08-18 21:35:361488 extension->id(),
1489 extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE);
treib6e51bca2016-06-15 10:26:031490 }
1491 UMA_HISTOGRAM_BOOLEAN("Extensions.ResetPermissionsIncrease",
1492 reset_permissions_increase);
1493 }
1494
[email protected]8d888c12010-11-30 00:00:251495 // Extension has changed permissions significantly. Disable it. A
[email protected]21db9ef2014-05-16 02:06:271496 // notification should be sent by the caller. If the extension is already
1497 // disabled because it was installed remotely, don't add another disable
treibaac30ec2015-06-10 09:18:091498 // reason.
1499 if (is_privilege_increase &&
Minh X. Nguyen45479012017-08-18 21:35:361500 !(disable_reasons & extensions::disable_reason::DISABLE_REMOTE_INSTALL)) {
1501 disable_reasons |= extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE;
treib8a6d9892015-08-26 10:23:191502 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id()))
treib2e0517f92015-05-07 23:15:361503 RecordPermissionMessagesHistogram(extension, "AutoDisable");
treib8ecc1eb52015-03-04 18:29:061504
brettw9e85ef42016-11-01 21:01:241505#if BUILDFLAG(ENABLE_SUPERVISED_USERS)
treib8ecc1eb52015-03-04 18:29:061506 // If a custodian-installed extension is disabled for a supervised user due
mamir51e05d02016-10-06 10:55:011507 // to a permissions increase, send a request to the custodian.
treib3202d592015-07-31 08:33:161508 if (extensions::util::IsExtensionSupervised(extension, profile_) &&
treib40d3ad92015-10-20 18:15:421509 !ExtensionSyncService::Get(profile_)->HasPendingReenable(
Devlin Cronin03bf2d22017-12-20 08:21:051510 extension->id(), extension->version())) {
treib8ecc1eb52015-03-04 18:29:061511 SupervisedUserService* supervised_user_service =
1512 SupervisedUserServiceFactory::GetForProfile(profile_);
treib40d3ad92015-10-20 18:15:421513 supervised_user_service->AddExtensionUpdateRequest(extension->id(),
Devlin Cronin03bf2d22017-12-20 08:21:051514 extension->version());
treib8ecc1eb52015-03-04 18:29:061515 }
1516#endif
[email protected]b914e292013-04-26 07:10:031517 }
treib6e51bca2016-06-15 10:26:031518
Minh X. Nguyen45479012017-08-18 21:35:361519 if (disable_reasons == extensions::disable_reason::DISABLE_NONE)
treib6e51bca2016-06-15 10:26:031520 extension_prefs_->SetExtensionEnabled(extension->id());
1521 else
treib8a6d9892015-08-26 10:23:191522 extension_prefs_->SetExtensionDisabled(extension->id(), disable_reasons);
[email protected]8d888c12010-11-30 00:00:251523}
1524
[email protected]eaa7dd182010-12-14 11:09:001525void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391526 std::set<std::string> extension_ids;
achuith2f5578b2016-02-26 21:57:131527 for (const auto& extension : registry_->enabled_extensions()) {
[email protected]1d5e58b2013-01-31 08:41:401528 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461529 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351530 }
1531
[email protected]dcc47642014-03-26 22:03:491532 // TODO(kalman): This is broken. ExtensionService is per-profile.
1533 // crash_keys::SetActiveExtensions is per-process. See
1534 // http://crbug.com/355029.
[email protected]f6431be82013-09-07 02:53:451535 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251536}
1537
[email protected]8266d662011-07-12 21:53:261538void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:501539 const Extension* extension,
[email protected]98270432012-09-11 20:51:241540 const syncer::StringOrdinal& page_ordinal,
Karandeep Bhatiaa8930652017-10-11 17:41:121541 int install_flags,
1542 const base::Optional<int>& dnr_ruleset_checksum) {
[email protected]a29a517a2011-01-21 21:11:121543 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201544
[email protected]c3cfb012011-04-06 22:07:351545 const std::string& id = extension->id();
binjin47947f842014-11-18 12:10:241546 int disable_reasons = GetDisableReasonsOnInstalled(extension);
[email protected]d8fd0fd2014-03-24 13:16:061547 std::string install_parameter;
[email protected]96aebe22014-07-16 04:07:511548 const extensions::PendingExtensionInfo* pending_extension_info =
1549 pending_extension_manager()->GetById(id);
1550 if (pending_extension_info) {
[email protected]8f3bcbd2013-06-05 08:42:401551 if (!pending_extension_info->ShouldAllowInstall(extension)) {
treib227b2582015-12-09 09:28:261552 // Hack for crbug.com/558299, see comment on DeleteThemeDoNotUse.
1553 if (extension->is_theme() && pending_extension_info->is_from_sync())
1554 ExtensionSyncService::Get(profile_)->DeleteThemeDoNotUse(*extension);
1555
[email protected]51a3bf8b2012-06-08 22:53:061556 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321557
[email protected]31bb5ee62012-09-12 22:58:401558 LOG(WARNING) << "ShouldAllowInstall() returned false for "
1559 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:221560 << " and update URL "
1561 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:401562 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541563
[email protected]4416c5a2010-06-26 01:28:571564 // Delete the extension directory since we're not going to
1565 // load it.
Devlin Croninad3c52b2017-08-10 01:49:371566 if (!extensions::GetExtensionFileTaskRunner()->PostTask(
tzik8d880ee2017-04-20 19:46:241567 FROM_HERE, base::BindOnce(&extensions::file_util::DeleteFile,
1568 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:361569 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401570 }
[email protected]4416c5a2010-06-26 01:28:571571 return;
1572 }
[email protected]51a3bf8b2012-06-08 22:53:061573
[email protected]d8fd0fd2014-03-24 13:16:061574 install_parameter = pending_extension_info->install_parameter();
[email protected]51a3bf8b2012-06-08 22:53:061575 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:571576 } else {
[email protected]c3cfb012011-04-06 22:07:351577 // We explicitly want to re-enable an uninstalled external
1578 // extension; if we're here, that means the user is manually
1579 // installing the extension.
[email protected]46f3e102014-03-25 01:22:451580 if (extension_prefs_->IsExternalExtensionUninstalled(id)) {
Minh X. Nguyen45479012017-08-18 21:35:361581 disable_reasons = extensions::disable_reason::DISABLE_NONE;
[email protected]6cc7dbae2011-04-29 21:18:331582 }
[email protected]aa142702010-03-26 01:26:331583 }
1584
asargente4de9f92016-09-15 01:40:061585 // If the old version of the extension was disabled due to corruption, this
1586 // new install may correct the problem.
Minh X. Nguyen45479012017-08-18 21:35:361587 disable_reasons &= ~extensions::disable_reason::DISABLE_CORRUPTED;
asargente4de9f92016-09-15 01:40:061588
[email protected]98270432012-09-11 20:51:241589 // Unsupported requirements overrides the management policy.
[email protected]4a1d9c0d2014-06-13 12:50:111590 if (install_flags & extensions::kInstallFlagHasRequirementErrors) {
Minh X. Nguyen45479012017-08-18 21:35:361591 disable_reasons |=
1592 extensions::disable_reason::DISABLE_UNSUPPORTED_REQUIREMENT;
binjin8e3d0182014-12-04 16:44:281593 } else {
1594 // Requirement is supported now, remove the corresponding disable reason
1595 // instead.
Minh X. Nguyen45479012017-08-18 21:35:361596 disable_reasons &=
1597 ~extensions::disable_reason::DISABLE_UNSUPPORTED_REQUIREMENT;
binjin8e3d0182014-12-04 16:44:281598 }
1599
1600 // Check if the extension was disabled because of the minimum version
1601 // requirements from enterprise policy, and satisfies it now.
1602 if (extensions::ExtensionManagementFactory::GetForBrowserContext(profile())
1603 ->CheckMinimumVersion(extension, nullptr)) {
1604 // And remove the corresponding disable reason.
Minh X. Nguyen45479012017-08-18 21:35:361605 disable_reasons &=
1606 ~extensions::disable_reason::DISABLE_UPDATE_REQUIRED_BY_POLICY;
[email protected]98270432012-09-11 20:51:241607 }
1608
[email protected]4a1d9c0d2014-06-13 12:50:111609 if (install_flags & extensions::kInstallFlagIsBlacklistedForMalware) {
[email protected]9f3c8532013-07-31 19:52:071610 // Installation of a blacklisted extension can happen from sync, policy,
1611 // etc, where to maintain consistency we need to install it, just never
1612 // load it (see AddExtension). Usually it should be the job of callers to
treibaac30ec2015-06-10 09:18:091613 // intercept blacklisted extensions earlier (e.g. CrxInstaller, before even
[email protected]9f3c8532013-07-31 19:52:071614 // showing the install dialogue).
[email protected]7c82539c2014-02-19 06:09:171615 extension_prefs_->AcknowledgeBlacklistedExtension(id);
[email protected]9f3c8532013-07-31 19:52:071616 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
1617 extension->location(),
1618 Manifest::NUM_LOCATIONS);
1619 }
1620
[email protected]695b5712012-12-06 23:55:281621 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:551622 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
1623 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:301624 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:401625 extension->location(), Manifest::NUM_LOCATIONS);
treib2e0517f92015-05-07 23:15:361626 RecordPermissionMessagesHistogram(extension, "Install");
[email protected]69084dc2012-11-17 07:39:301627 } else {
1628 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
1629 extension->GetType(), 100);
1630 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:401631 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:551632 }
1633
[email protected]399583b2012-12-11 09:33:421634 const Extension::State initial_state =
Minh X. Nguyen45479012017-08-18 21:35:361635 disable_reasons == extensions::disable_reason::DISABLE_NONE
1636 ? Extension::ENABLED
1637 : Extension::DISABLED;
treib8a6d9892015-08-26 10:23:191638 if (initial_state == Extension::ENABLED)
1639 extension_prefs_->SetExtensionEnabled(id);
1640 else
1641 extension_prefs_->SetExtensionDisabled(id, disable_reasons);
binjin47947f842014-11-18 12:10:241642
xiyuanf6a4c6a62016-04-19 18:14:541643 extensions::ExtensionPrefs::DelayReason delay_reason;
1644 extensions::InstallGate::Action action = ShouldDelayExtensionInstall(
1645 extension, !!(install_flags & extensions::kInstallFlagInstallImmediately),
1646 &delay_reason);
1647 switch (action) {
1648 case extensions::InstallGate::INSTALL:
1649 AddNewOrUpdatedExtension(extension, initial_state, install_flags,
Karandeep Bhatiaa8930652017-10-11 17:41:121650 page_ordinal, install_parameter,
1651 dnr_ruleset_checksum);
xiyuanf6a4c6a62016-04-19 18:14:541652 return;
1653 case extensions::InstallGate::DELAY:
Karandeep Bhatiaa8930652017-10-11 17:41:121654 extension_prefs_->SetDelayedInstallInfo(
1655 extension, initial_state, install_flags, delay_reason, page_ordinal,
1656 install_parameter, dnr_ruleset_checksum);
[email protected]0db124b02012-11-07 04:55:051657
xiyuanf6a4c6a62016-04-19 18:14:541658 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:191659 delayed_installs_.Insert(extension);
xiyuanf6a4c6a62016-04-19 18:14:541660
1661 if (delay_reason ==
1662 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE) {
1663 // Notify observers that app update is available.
ericwilligersb5f79de2016-10-19 04:15:101664 for (auto& observer : update_observers_)
1665 observer.OnAppUpdateAvailable(extension);
xiyuanf6a4c6a62016-04-19 18:14:541666 }
1667 return;
1668 case extensions::InstallGate::ABORT:
1669 // Do nothing to abort the install. One such case is the shared module
1670 // service gets IMPORT_STATUS_UNRECOVERABLE status for the pending
1671 // install.
1672 return;
[email protected]399583b2012-12-11 09:33:421673 }
xiyuanf6a4c6a62016-04-19 18:14:541674
1675 NOTREACHED() << "Unknown action for delayed install: " << action;
[email protected]8c484b742012-11-29 06:05:361676}
1677
binjin1569c9b2014-09-05 13:33:181678void ExtensionService::OnExtensionManagementSettingsChanged() {
1679 error_controller_->ShowErrorIfNeeded();
binjine6b58b52014-10-31 01:55:571680
1681 // Revokes blocked permissions from active_permissions for all extensions.
1682 extensions::ExtensionManagement* settings =
1683 extensions::ExtensionManagementFactory::GetForBrowserContext(profile());
1684 CHECK(settings);
dchengc963c7142016-04-08 03:55:221685 std::unique_ptr<ExtensionSet> all_extensions(
binjine6b58b52014-10-31 01:55:571686 registry_->GenerateInstalledExtensionsSet());
achuith2f5578b2016-02-26 21:57:131687 for (const auto& extension : *all_extensions) {
binjine6b58b52014-10-31 01:55:571688 if (!settings->IsPermissionSetAllowed(
binjin685ade82014-11-06 09:53:561689 extension.get(),
Nick Peterson0203be4a2017-07-19 00:25:141690 extension->permissions_data()->active_permissions()) &&
1691 CanBlockExtension(extension.get())) {
rdevlin.cronin77cb0ef2015-09-16 17:03:481692 extensions::PermissionsUpdater(profile()).RemovePermissionsUnsafe(
rdevlin.cronind630c302015-09-30 20:19:331693 extension.get(), *settings->GetBlockedPermissions(extension.get()));
binjine6b58b52014-10-31 01:55:571694 }
1695 }
1696
binjin1569c9b2014-09-05 13:33:181697 CheckManagementPolicy();
1698}
1699
[email protected]8c484b742012-11-29 06:05:361700void ExtensionService::AddNewOrUpdatedExtension(
1701 const Extension* extension,
[email protected]399583b2012-12-11 09:33:421702 Extension::State initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111703 int install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061704 const syncer::StringOrdinal& page_ordinal,
Karandeep Bhatiaa8930652017-10-11 17:41:121705 const std::string& install_parameter,
1706 const base::Optional<int>& dnr_ruleset_checksum) {
[email protected]8c484b742012-11-29 06:05:361707 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
Karandeep Bhatiaa8930652017-10-11 17:41:121708 extension_prefs_->OnExtensionInstalled(extension, initial_state, page_ordinal,
1709 install_flags, install_parameter,
1710 dnr_ruleset_checksum);
[email protected]9e9c1d12013-07-31 01:58:121711 delayed_installs_.Remove(extension->id());
[email protected]913ffca92014-03-27 15:26:161712 if (InstallVerifier::NeedsVerification(*extension))
juncai33e462102015-05-18 20:48:441713 InstallVerifier::Get(GetBrowserContext())->VerifyExtension(extension->id());
ryanackley48bedbd2015-01-27 23:12:141714
1715 const Extension* old = GetInstalledExtension(extension->id());
1716 if (extensions::AppDataMigrator::NeedsMigration(old, extension)) {
1717 app_data_migrator_->DoMigrationAndReply(
vmpstra34d11322016-03-21 20:28:471718 old, extension, base::Bind(&ExtensionService::FinishInstallation,
1719 AsWeakPtr(), base::RetainedRef(extension)));
ryanackley48bedbd2015-01-27 23:12:141720 return;
1721 }
1722
benwells1dd4acd2015-12-09 02:20:241723 FinishInstallation(extension);
[email protected]6f6101832012-11-27 22:10:481724}
1725
Toni Barzic667db0d32018-01-09 18:00:191726bool ExtensionService::FinishDelayedInstallationIfReady(
1727 const std::string& extension_id,
1728 bool install_immediately) {
[email protected]9f4e4f082013-06-21 07:11:191729 // Check if the extension already got installed.
[email protected]9f4e4f082013-06-21 07:11:191730 const Extension* extension = delayed_installs_.GetByID(extension_id);
xiyuanf6a4c6a62016-04-19 18:14:541731 if (!extension)
Toni Barzic667db0d32018-01-09 18:00:191732 return false;
xiyuanf6a4c6a62016-04-19 18:14:541733
1734 extensions::ExtensionPrefs::DelayReason reason;
Toni Barzic667db0d32018-01-09 18:00:191735 const extensions::InstallGate::Action action =
1736 ShouldDelayExtensionInstall(extension, install_immediately, &reason);
xiyuanf6a4c6a62016-04-19 18:14:541737 switch (action) {
1738 case extensions::InstallGate::INSTALL:
1739 break;
1740 case extensions::InstallGate::DELAY:
1741 // Bail out and continue to delay the install.
Toni Barzic667db0d32018-01-09 18:00:191742 return false;
xiyuanf6a4c6a62016-04-19 18:14:541743 case extensions::InstallGate::ABORT:
1744 delayed_installs_.Remove(extension_id);
1745 // Make sure no version of the extension is actually installed, (i.e.,
1746 // that this delayed install was not an update).
1747 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
1748 extension_prefs_->DeleteExtensionPrefs(extension_id);
Toni Barzic667db0d32018-01-09 18:00:191749 return false;
[email protected]9f4e4f082013-06-21 07:11:191750 }
1751
Toni Barzic667db0d32018-01-09 18:00:191752 scoped_refptr<const Extension> delayed_install =
1753 GetPendingExtensionUpdate(extension_id);
1754 CHECK(delayed_install.get());
[email protected]9f4e4f082013-06-21 07:11:191755 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:481756
[email protected]399583b2012-12-11 09:33:421757 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:481758 NOTREACHED();
1759
Toni Barzic667db0d32018-01-09 18:00:191760 FinishInstallation(delayed_install.get());
1761 return true;
[email protected]6f6101832012-11-27 22:10:481762}
1763
[email protected]760f743b2014-05-28 13:52:021764void ExtensionService::FinishInstallation(
benwells1dd4acd2015-12-09 02:20:241765 const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:561766 const extensions::Extension* existing_extension =
1767 GetInstalledExtension(extension->id());
1768 bool is_update = false;
1769 std::string old_name;
1770 if (existing_extension) {
1771 is_update = true;
1772 old_name = existing_extension->name();
1773 }
[email protected]bc44b5da2014-06-12 14:20:001774 registry_->TriggerOnWillBeInstalled(
benwells1dd4acd2015-12-09 02:20:241775 extension, is_update, old_name);
[email protected]17f07822014-05-22 08:45:151776
[email protected]399583b2012-12-11 09:33:421777 // Unpacked extensions default to allowing file access, but if that has been
1778 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:401779 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:421780 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
1781 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1782 }
1783
[email protected]6f6101832012-11-27 22:10:481784 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:031785
[email protected]bc44b5da2014-06-12 14:20:001786 // Notify observers that need to know when an installation is complete.
[email protected]38e872532014-07-16 23:27:511787 registry_->TriggerOnInstalled(extension, is_update);
[email protected]bc44b5da2014-06-12 14:20:001788
[email protected]9f4e4f082013-06-21 07:11:191789 // Check extensions that may have been delayed only because this shared module
1790 // was not available.
[email protected]374ceb6f2014-07-02 19:25:341791 if (SharedModuleInfo::IsSharedModule(extension))
[email protected]9f4e4f082013-06-21 07:11:191792 MaybeFinishDelayedInstallations();
[email protected]4a190632009-05-09 01:07:421793}
1794
[email protected]76b65442012-11-17 14:11:481795const Extension* ExtensionService::GetPendingExtensionUpdate(
1796 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:191797 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:481798}
1799
mukai87a8d402014-09-15 20:15:091800void ExtensionService::RegisterContentSettings(
mmenkead5094ab2017-05-15 19:01:141801 HostContentSettingsMap* host_content_settings_map,
1802 Profile* profile) {
1803 // Most extension services key off of the original profile.
1804 Profile* original_profile = profile->GetOriginalProfile();
1805
rkaplowe65c2ff2015-02-14 16:29:541806 TRACE_EVENT0("browser,startup", "ExtensionService::RegisterContentSettings");
mukaia1aa3512014-10-22 20:14:111807 DCHECK_CURRENTLY_ON(BrowserThread::UI);
mukai87a8d402014-09-15 20:15:091808 host_content_settings_map->RegisterProvider(
mukai87a8d402014-09-15 20:15:091809 HostContentSettingsMap::CUSTOM_EXTENSION_PROVIDER,
dchengc963c7142016-04-08 03:55:221810 std::unique_ptr<content_settings::ObservableProvider>(
mukai87a8d402014-09-15 20:15:091811 new content_settings::CustomExtensionProvider(
mmenkead5094ab2017-05-15 19:01:141812 extensions::ContentSettingsService::Get(original_profile)
dchengc963c7142016-04-08 03:55:221813 ->content_settings_store(),
mmenkead5094ab2017-05-15 19:01:141814 // TODO(mmenke): CustomExtensionProvider expects this to be true
1815 // for incognito profiles.
1816 false)));
mukai87a8d402014-09-15 20:15:091817}
1818
[email protected]e9d7496e2014-04-18 01:25:461819void ExtensionService::TerminateExtension(const std::string& extension_id) {
Michael Giuffridafcb420362017-10-11 21:50:261820 UpdateForUnloadingExtension(extension_id);
1821 extension_registrar_.TerminateExtension(extension_id);
[email protected]bb7f40952011-01-13 00:21:201822}
1823
[email protected]8001df22011-04-28 19:59:471824const Extension* ExtensionService::GetInstalledExtension(
1825 const std::string& id) const {
[email protected]599539802014-01-07 23:06:001826 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:361827}
1828
[email protected]9060d8b02012-01-13 02:14:301829bool ExtensionService::OnExternalExtensionFileFound(
lazyboye8634172016-01-28 00:10:481830 const ExternalInstallInfoFile& info) {
Istiaque Ahmedf6e72622017-09-08 23:14:171831 DCHECK_CURRENTLY_ON(BrowserThread::UI);
lazyboye8634172016-01-28 00:10:481832 CHECK(crx_file::id_util::IdIsValid(info.extension_id));
1833 if (extension_prefs_->IsExternalExtensionUninstalled(info.extension_id))
[email protected]9060d8b02012-01-13 02:14:301834 return false;
[email protected]a8af9fdb2010-10-28 21:52:201835
[email protected]7577a5c52009-07-30 06:21:581836 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491837 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581838 // installed on every startup.
lazyboye8634172016-01-28 00:10:481839 const Extension* existing = GetExtensionById(info.extension_id, true);
[email protected]910f72ce2012-08-24 01:38:351840
[email protected]7577a5c52009-07-30 06:21:581841 if (existing) {
[email protected]910f72ce2012-08-24 01:38:351842 // The default apps will have the location set as INTERNAL. Since older
1843 // default apps are installed as EXTERNAL, we override them. However, if the
1844 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:401845 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:351846 bool is_default_apps_migration =
lazyboye8634172016-01-28 00:10:481847 (info.crx_location == Manifest::INTERNAL &&
[email protected]1d5e58b2013-01-31 08:41:401848 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:351849
1850 if (!is_default_apps_migration) {
Devlin Cronin03bf2d22017-12-20 08:21:051851 switch (existing->version().CompareTo(info.version)) {
[email protected]910f72ce2012-08-24 01:38:351852 case -1: // existing version is older, we should upgrade
1853 break;
1854 case 0: // existing version is same, do nothing
1855 return false;
1856 case 1: // existing version is newer, uh-oh
lazyboye8634172016-01-28 00:10:481857 LOG(WARNING) << "Found external version of extension "
1858 << info.extension_id
[email protected]910f72ce2012-08-24 01:38:351859 << "that is older than current version. Current version "
1860 << "is: " << existing->VersionString() << ". New "
Devlin Cronind4c2a8f32017-09-29 17:08:301861 << "version is: " << info.version.GetString()
[email protected]910f72ce2012-08-24 01:38:351862 << ". Keeping current version.";
1863 return false;
1864 }
[email protected]7577a5c52009-07-30 06:21:581865 }
1866 }
1867
[email protected]9060d8b02012-01-13 02:14:301868 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:301869 if (!pending_extension_manager()->AddFromExternalFile(
Devlin Cronind4c2a8f32017-09-29 17:08:301870 info.extension_id, info.crx_location, info.version,
lazyboye8634172016-01-28 00:10:481871 info.creation_flags, info.mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:301872 return false;
[email protected]e3987852012-05-04 10:06:301873 }
[email protected]9c635f22010-12-02 09:36:361874
[email protected]14908b72011-04-20 06:54:361875 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:371876 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
lazyboye8634172016-01-28 00:10:481877 installer->set_install_source(info.crx_location);
1878 installer->set_expected_id(info.extension_id);
Devlin Cronind4c2a8f32017-09-29 17:08:301879 installer->set_expected_version(info.version,
treibe960e282015-09-11 10:38:081880 true /* fail_install_if_unexpected */);
[email protected]cb0e50312011-05-09 15:03:071881 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
lazyboye8634172016-01-28 00:10:481882 installer->set_install_immediately(info.install_immediately);
1883 installer->set_creation_flags(info.creation_flags);
[email protected]88e8ec9152013-01-17 04:05:181884#if defined(OS_CHROMEOS)
lazyboye8634172016-01-28 00:10:481885 extensions::InstallLimiter::Get(profile_)->Add(installer, info.path);
[email protected]88e8ec9152013-01-17 04:05:181886#else
lazyboye8634172016-01-28 00:10:481887 installer->InstallCrx(info.path);
[email protected]88e8ec9152013-01-17 04:05:181888#endif
[email protected]47fc70c2011-12-06 07:29:511889
1890 // Depending on the source, a new external extension might not need a user
1891 // notification on installation. For such extensions, mark them acknowledged
1892 // now to suppress the notification.
lazyboye8634172016-01-28 00:10:481893 if (info.mark_acknowledged)
1894 external_install_manager_->AcknowledgeExternalExtension(info.extension_id);
[email protected]9060d8b02012-01-13 02:14:301895
1896 return true;
[email protected]7577a5c52009-07-30 06:21:581897}
1898
[email protected]eaa7dd182010-12-14 11:09:001899void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:131900 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:181901 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:331902 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:181903 if (iter == orphaned_dev_tools_.end())
1904 return;
1905
rockota34fed222015-09-25 21:56:311906 // Keepalive count is reset on extension reload. This re-establishes the
1907 // keepalive that was added when the DevTools agent was initially attached.
1908 extensions::ProcessManager::Get(profile_)->IncrementLazyKeepaliveCount(
1909 host->extension());
[email protected]b3f957e62014-08-08 10:09:021910 iter->second->ConnectWebContents(host->host_contents());
[email protected]406027c02010-09-27 08:03:181911 orphaned_dev_tools_.erase(iter);
1912}
1913
[email protected]432115822011-07-10 15:52:271914void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:531915 const content::NotificationSource& source,
1916 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:271917 switch (type) {
[email protected]3c4abc82012-10-22 22:25:541918 case chrome::NOTIFICATION_APP_TERMINATING:
1919 // Shutdown has started. Don't start any more extension installs.
1920 // (We cannot use ExtensionService::Shutdown() for this because it
1921 // happens too late in browser teardown.)
1922 browser_terminating_ = true;
1923 break;
[email protected]adf5a102014-07-31 12:44:061924 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:531925 if (profile_ !=
1926 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:321927 break;
[email protected]6c2381d2011-10-19 02:52:531928 }
[email protected]a4ed6282009-12-14 20:51:161929
Michael Giuffridafcb420362017-10-11 21:50:261930 // Mark the extension as terminated and deactivated. We want it to
[email protected]fa2416f2011-05-03 08:41:201931 // be in a consistent state: either fully working or not loaded
1932 // at all, but never half-crashed. We do it in a PostTask so
1933 // that other handlers of this notification will still have
1934 // access to the Extension and ExtensionHost.
Michael Giuffridafcb420362017-10-11 21:50:261935 extensions::ExtensionHost* host =
1936 content::Details<extensions::ExtensionHost>(details).ptr();
skyostilf221b7de2015-06-11 20:36:321937 base::ThreadTaskRunnerHandle::Get()->PostTask(
Michael Giuffridafcb420362017-10-11 21:50:261938 FROM_HERE, base::BindOnce(&ExtensionService::TerminateExtension,
1939 AsWeakPtr(), host->extension_id()));
[email protected]31f77262009-12-02 20:48:531940 break;
1941 }
[email protected]432115822011-07-10 15:52:271942 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:301943 content::RenderProcessHost* process =
1944 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:191945 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301946 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:191947 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
1948 break;
1949
[email protected]fafdc842014-01-17 18:09:081950 extensions::ProcessMap* process_map =
1951 extensions::ProcessMap::Get(profile_);
1952 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:031953 // An extension process was terminated, this might have resulted in an
1954 // app or extension becoming idle.
1955 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:081956 process_map->GetExtensionsInProcess(process->GetID());
elijahtaylor0def4432014-10-06 18:15:111957 // In addition to the extensions listed in the process map, one of those
1958 // extensions could be referencing a shared module which is waiting for
1959 // idle to update. Check all imports of these extensions, too.
1960 std::set<std::string> import_ids;
1961 for (std::set<std::string>::const_iterator it = extension_ids.begin();
1962 it != extension_ids.end();
1963 ++it) {
1964 const Extension* extension = GetExtensionById(*it, true);
1965 if (!extension)
1966 continue;
1967 const std::vector<SharedModuleInfo::ImportInfo>& imports =
1968 SharedModuleInfo::GetImports(extension);
1969 std::vector<SharedModuleInfo::ImportInfo>::const_iterator import_it;
1970 for (import_it = imports.begin(); import_it != imports.end();
1971 import_it++) {
1972 import_ids.insert((*import_it).extension_id);
1973 }
1974 }
1975 extension_ids.insert(import_ids.begin(), import_ids.end());
1976
[email protected]52b76592013-11-02 17:59:031977 for (std::set<std::string>::const_iterator it = extension_ids.begin();
1978 it != extension_ids.end(); ++it) {
1979 if (delayed_installs_.Contains(*it)) {
skyostilf221b7de2015-06-11 20:36:321980 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
[email protected]52b76592013-11-02 17:59:031981 FROM_HERE,
tzik8d880ee2017-04-20 19:46:241982 base::BindOnce(
Toni Barzic667db0d32018-01-09 18:00:191983 base::IgnoreResult(
1984 &ExtensionService::FinishDelayedInstallationIfReady),
1985 AsWeakPtr(), *it, false /*install_immediately*/),
[email protected]52b76592013-11-02 17:59:031986 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
1987 }
1988 }
1989 }
1990
[email protected]fafdc842014-01-17 18:09:081991 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:461992 BrowserThread::PostTask(
tzik8d880ee2017-04-20 19:46:241993 BrowserThread::IO, FROM_HERE,
1994 base::BindOnce(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
1995 system_->info_map(), process->GetID()));
[email protected]da5683db2011-04-23 17:12:211996 break;
1997 }
[email protected]ebe07772014-05-22 04:16:061998 case chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED: {
1999 OnProfileDestructionStarted();
2000 break;
2001 }
[email protected]aa96d3a2010-08-21 08:45:252002
[email protected]4814b512009-11-07 00:12:292003 default:
2004 NOTREACHED() << "Unexpected notification type.";
2005 }
2006}
2007
binjin47947f842014-11-18 12:10:242008int ExtensionService::GetDisableReasonsOnInstalled(const Extension* extension) {
rdevlin.cronine1456712016-12-29 22:47:282009 bool is_update_from_same_type = false;
2010 {
2011 const Extension* existing_extension =
2012 GetInstalledExtension(extension->id());
2013 is_update_from_same_type =
2014 existing_extension &&
2015 existing_extension->manifest()->type() == extension->manifest()->type();
2016 }
Minh X. Nguyen45479012017-08-18 21:35:362017 extensions::disable_reason::DisableReason disable_reason =
2018 extensions::disable_reason::DISABLE_NONE;
binjin47947f842014-11-18 12:10:242019 // Extensions disabled by management policy should always be disabled, even
2020 // if it's force-installed.
2021 if (system_->management_policy()->MustRemainDisabled(
2022 extension, &disable_reason, nullptr)) {
2023 // A specified reason is required to disable the extension.
Minh X. Nguyen45479012017-08-18 21:35:362024 DCHECK(disable_reason != extensions::disable_reason::DISABLE_NONE);
binjin47947f842014-11-18 12:10:242025 return disable_reason;
2026 }
2027
[email protected]612a1cb12012-10-17 13:18:032028 // Extensions installed by policy can't be disabled. So even if a previous
2029 // installation disabled the extension, make sure it is now enabled.
binjin47947f842014-11-18 12:10:242030 if (system_->management_policy()->MustRemainEnabled(extension, nullptr))
Minh X. Nguyen45479012017-08-18 21:35:362031 return extensions::disable_reason::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032032
binjin47947f842014-11-18 12:10:242033 // An already disabled extension should inherit the disable reasons and
2034 // remain disabled.
2035 if (extension_prefs_->IsExtensionDisabled(extension->id())) {
2036 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
2037 // If an extension was disabled without specified reason, presume it's
2038 // disabled by user.
Minh X. Nguyen45479012017-08-18 21:35:362039 return disable_reasons == extensions::disable_reason::DISABLE_NONE
2040 ? extensions::disable_reason::DISABLE_USER_ACTION
binjin47947f842014-11-18 12:10:242041 : disable_reasons;
2042 }
[email protected]612a1cb12012-10-17 13:18:032043
rdevlin.cronin9d7ae4d2017-01-13 16:38:232044 if (extensions::ExternalInstallManager::IsPromptingEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032045 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542046 // enabling them. Hosted apps are excepted because they are not dangerous
rdevlin.cronine1456712016-12-29 22:47:282047 // (they need to be launched by the user anyway). We also don't prompt for
2048 // extensions updating; this is because the extension will be disabled from
2049 // the initial install if it is supposed to be, and this allows us to turn
2050 // this on for other platforms without disabling already-installed
2051 // extensions.
[email protected]1d5e58b2013-01-31 08:41:402052 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2053 Manifest::IsExternalLocation(extension->location()) &&
rdevlin.cronine1456712016-12-29 22:47:282054 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
2055 !is_update_from_same_type) {
Minh X. Nguyen45479012017-08-18 21:35:362056 return extensions::disable_reason::DISABLE_EXTERNAL_EXTENSION;
[email protected]612a1cb12012-10-17 13:18:032057 }
2058 }
[email protected]612a1cb12012-10-17 13:18:032059
Minh X. Nguyen45479012017-08-18 21:35:362060 return extensions::disable_reason::DISABLE_NONE;
[email protected]612a1cb12012-10-17 13:18:032061}
[email protected]0db124b02012-11-07 04:55:052062
mlerman6a37b6a42014-11-26 22:10:532063// Helper method to determine if an extension can be blocked.
2064bool ExtensionService::CanBlockExtension(const Extension* extension) const {
mlerman3690e5be2014-12-01 22:57:442065 DCHECK(extension);
mlerman6a37b6a42014-11-26 22:10:532066 return extension->location() != Manifest::COMPONENT &&
2067 extension->location() != Manifest::EXTERNAL_COMPONENT &&
achuith2f5578b2016-02-26 21:57:132068 !system_->management_policy()->MustRemainEnabled(extension, nullptr);
mlerman6a37b6a42014-11-26 22:10:532069}
2070
xiyuanf6a4c6a62016-04-19 18:14:542071extensions::InstallGate::Action ExtensionService::ShouldDelayExtensionInstall(
2072 const extensions::Extension* extension,
2073 bool install_immediately,
2074 extensions::ExtensionPrefs::DelayReason* reason) const {
2075 for (const auto& entry : install_delayer_registry_) {
2076 extensions::InstallGate* const delayer = entry.second;
2077 extensions::InstallGate::Action action =
2078 delayer->ShouldDelay(extension, install_immediately);
2079 if (action != extensions::InstallGate::INSTALL) {
2080 *reason = entry.first;
2081 return action;
2082 }
[email protected]e7aa7b7e2012-11-27 04:51:222083 }
[email protected]fdd679b2012-11-15 20:49:392084
xiyuanf6a4c6a62016-04-19 18:14:542085 return extensions::InstallGate::INSTALL;
[email protected]9f4e4f082013-06-21 07:11:192086}
2087
2088void ExtensionService::MaybeFinishDelayedInstallations() {
2089 std::vector<std::string> to_be_installed;
achuith2f5578b2016-02-26 21:57:132090 for (const auto& extension : delayed_installs_) {
2091 to_be_installed.push_back(extension->id());
[email protected]399583b2012-12-11 09:33:422092 }
achuith2f5578b2016-02-26 21:57:132093 for (const auto& extension_id : to_be_installed) {
Toni Barzic667db0d32018-01-09 18:00:192094 FinishDelayedInstallationIfReady(extension_id,
2095 false /*install_immediately*/);
[email protected]399583b2012-12-11 09:33:422096 }
[email protected]399583b2012-12-11 09:33:422097}
2098
[email protected]fdd679b2012-11-15 20:49:392099void ExtensionService::OnBlacklistUpdated() {
[email protected]2d19eb6e2014-01-27 17:30:002100 blacklist_->GetBlacklistedIDs(
[email protected]f47f7172014-03-19 19:27:102101 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252102 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282103}
2104
weidongg04695232017-06-09 00:01:052105void ExtensionService::OnUpgradeRecommended() {
2106 // Notify observers that chrome update is available.
2107 for (auto& observer : update_observers_)
2108 observer.OnChromeUpdateAvailable();
2109}
2110
Michael Giuffrida6397c592017-10-09 20:46:532111bool ExtensionService::CanEnableExtension(const Extension* extension) {
2112 return !system_->management_policy()->MustRemainDisabled(extension, nullptr,
2113 nullptr);
2114}
2115
2116bool ExtensionService::CanDisableExtension(const Extension* extension) {
2117 // Some extensions cannot be disabled by users:
2118 // - |extension| can be null if sync disables an extension that is not
2119 // installed yet; allow disablement in this case.
2120 if (!extension)
2121 return true;
2122
2123 // - Shared modules are just resources used by other extensions, and are not
2124 // user-controlled.
2125 if (SharedModuleInfo::IsSharedModule(extension))
2126 return false;
2127
2128 // - EXTERNAL_COMPONENT extensions are not generally modifiable by users, but
2129 // can be uninstalled by the browser if the user sets extension-specific
2130 // preferences.
2131 if (extension->location() == Manifest::EXTERNAL_COMPONENT)
2132 return true;
2133
2134 return system_->management_policy()->UserMayModifySettings(extension,
2135 nullptr);
2136}
2137
2138bool ExtensionService::ShouldBlockExtension(const Extension* extension) {
2139 if (!block_extensions_)
2140 return false;
2141
2142 // Blocked extensions aren't marked as such in prefs, thus if
2143 // |block_extensions_| is true then CanBlockExtension() must be called with an
2144 // Extension object. If |extension| is not loaded, assume it should be
2145 // blocked.
2146 return !extension || CanBlockExtension(extension);
2147}
2148
[email protected]2d19eb6e2014-01-27 17:30:002149void ExtensionService::ManageBlacklist(
2150 const extensions::Blacklist::BlacklistStateMap& state_map) {
[email protected]54ee8192014-03-29 17:37:242151 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]695b5712012-12-06 23:55:282152
mlerman6a37b6a42014-11-26 22:10:532153 std::set<std::string> blacklisted;
[email protected]2d19eb6e2014-01-27 17:30:002154 ExtensionIdSet greylist;
2155 ExtensionIdSet unchanged;
achuith2f5578b2016-02-26 21:57:132156 for (const auto& it : state_map) {
2157 switch (it.second) {
[email protected]2d19eb6e2014-01-27 17:30:002158 case extensions::NOT_BLACKLISTED:
2159 break;
[email protected]695b5712012-12-06 23:55:282160
[email protected]2d19eb6e2014-01-27 17:30:002161 case extensions::BLACKLISTED_MALWARE:
achuith2f5578b2016-02-26 21:57:132162 blacklisted.insert(it.first);
[email protected]2d19eb6e2014-01-27 17:30:002163 break;
2164
2165 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2166 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2167 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
achuith2f5578b2016-02-26 21:57:132168 greylist.insert(it.first);
[email protected]2d19eb6e2014-01-27 17:30:002169 break;
2170
2171 case extensions::BLACKLISTED_UNKNOWN:
achuith2f5578b2016-02-26 21:57:132172 unchanged.insert(it.first);
[email protected]2d19eb6e2014-01-27 17:30:002173 break;
2174 }
2175 }
2176
mlerman6a37b6a42014-11-26 22:10:532177 UpdateBlacklistedExtensions(blacklisted, unchanged);
[email protected]2d19eb6e2014-01-27 17:30:002178 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2179
[email protected]373daf972014-04-10 01:50:442180 error_controller_->ShowErrorIfNeeded();
[email protected]2d19eb6e2014-01-27 17:30:002181}
2182
2183namespace {
2184void Partition(const ExtensionIdSet& before,
2185 const ExtensionIdSet& after,
2186 const ExtensionIdSet& unchanged,
2187 ExtensionIdSet* no_longer,
2188 ExtensionIdSet* not_yet) {
2189 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2190 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2191 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2192}
2193} // namespace
2194
mlerman6a37b6a42014-11-26 22:10:532195void ExtensionService::UpdateBlacklistedExtensions(
2196 const ExtensionIdSet& blacklisted,
[email protected]2d19eb6e2014-01-27 17:30:002197 const ExtensionIdSet& unchanged) {
2198 ExtensionIdSet not_yet_blocked, no_longer_blocked;
mlerman6a37b6a42014-11-26 22:10:532199 Partition(registry_->blacklisted_extensions().GetIDs(), blacklisted,
2200 unchanged, &no_longer_blocked, &not_yet_blocked);
[email protected]2d19eb6e2014-01-27 17:30:002201
2202 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2203 it != no_longer_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282204 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142205 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252206 if (!extension.get()) {
mlerman6a37b6a42014-11-26 22:10:532207 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2208 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282209 continue;
[email protected]3f2a2fa2013-09-24 02:55:252210 }
[email protected]bb1bc9b32013-12-21 03:09:142211 registry_->RemoveBlacklisted(*it);
atuchin6dc7c442016-07-20 07:04:342212 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2213 extensions::NOT_BLACKLISTED);
[email protected]dc24976f2013-06-02 21:15:092214 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092215 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092216 extension->location(),
2217 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282218 }
2219
[email protected]2d19eb6e2014-01-27 17:30:002220 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2221 it != not_yet_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282222 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252223 if (!extension.get()) {
2224 NOTREACHED() << "Extension " << *it << " needs to be "
2225 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282226 continue;
[email protected]3f2a2fa2013-09-24 02:55:252227 }
[email protected]bb1bc9b32013-12-21 03:09:142228 registry_->AddBlacklisted(extension);
[email protected]2d19eb6e2014-01-27 17:30:002229 extension_prefs_->SetExtensionBlacklistState(
2230 extension->id(), extensions::BLACKLISTED_MALWARE);
limasdf0deef2042017-05-03 19:17:172231 UnloadExtension(*it, UnloadedExtensionReason::BLACKLIST);
[email protected]ac875372013-02-28 04:36:092232 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2233 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282234 }
[email protected]2d19eb6e2014-01-27 17:30:002235}
[email protected]695b5712012-12-06 23:55:282236
[email protected]2d19eb6e2014-01-27 17:30:002237// TODO(oleg): UMA logging
2238void ExtensionService::UpdateGreylistedExtensions(
2239 const ExtensionIdSet& greylist,
2240 const ExtensionIdSet& unchanged,
2241 const extensions::Blacklist::BlacklistStateMap& state_map) {
2242 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2243 Partition(greylist_.GetIDs(),
2244 greylist, unchanged,
2245 &no_longer_greylisted, &not_yet_greylisted);
2246
2247 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2248 it != no_longer_greylisted.end(); ++it) {
2249 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2250 if (!extension.get()) {
2251 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2252 << "but it was not marked as greylisted.";
2253 continue;
2254 }
2255
2256 greylist_.Remove(*it);
2257 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2258 extensions::NOT_BLACKLISTED);
2259 if (extension_prefs_->GetDisableReasons(extension->id()) &
Minh X. Nguyen45479012017-08-18 21:35:362260 extensions::disable_reason::DISABLE_GREYLIST)
[email protected]2d19eb6e2014-01-27 17:30:002261 EnableExtension(*it);
2262 }
2263
2264 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2265 it != not_yet_greylisted.end(); ++it) {
2266 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2267 if (!extension.get()) {
2268 NOTREACHED() << "Extension " << *it << " needs to be "
2269 << "disabled, but it's not installed.";
2270 continue;
2271 }
2272 greylist_.Insert(extension);
2273 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2274 state_map.find(*it)->second);
2275 if (registry_->enabled_extensions().Contains(extension->id()))
Minh X. Nguyen45479012017-08-18 21:35:362276 DisableExtension(*it, extensions::disable_reason::DISABLE_GREYLIST);
[email protected]2d19eb6e2014-01-27 17:30:002277 }
[email protected]fdd679b2012-11-15 20:49:392278}
[email protected]75bdcb872013-03-13 00:41:452279
2280void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2281 update_observers_.AddObserver(observer);
2282}
2283
2284void ExtensionService::RemoveUpdateObserver(
2285 extensions::UpdateObserver* observer) {
2286 update_observers_.RemoveObserver(observer);
2287}
[email protected]bb1bc9b32013-12-21 03:09:142288
xiyuanf6a4c6a62016-04-19 18:14:542289void ExtensionService::RegisterInstallGate(
2290 extensions::ExtensionPrefs::DelayReason reason,
2291 extensions::InstallGate* install_delayer) {
2292 DCHECK(install_delayer_registry_.end() ==
2293 install_delayer_registry_.find(reason));
2294 install_delayer_registry_[reason] = install_delayer;
2295}
2296
2297void ExtensionService::UnregisterInstallGate(
2298 extensions::InstallGate* install_delayer) {
2299 for (auto it = install_delayer_registry_.begin();
2300 it != install_delayer_registry_.end(); ++it) {
2301 if (it->second == install_delayer) {
2302 install_delayer_registry_.erase(it);
2303 return;
2304 }
2305 }
2306}
2307
[email protected]bb1bc9b32013-12-21 03:09:142308// Used only by test code.
2309void ExtensionService::UnloadAllExtensionsInternal() {
2310 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2311
2312 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132313 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142314
2315 // TODO(erikkay) should there be a notification for this? We can't use
Minh X. Nguyen45479012017-08-18 21:35:362316 // extensions::EXTENSION_UNLOADED since that implies that the extension has
2317 // been disabled or uninstalled.
[email protected]bb1bc9b32013-12-21 03:09:142318}
[email protected]ebe07772014-05-22 04:16:062319
2320void ExtensionService::OnProfileDestructionStarted() {
2321 ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
2322 for (ExtensionIdSet::iterator it = ids_to_unload.begin();
2323 it != ids_to_unload.end();
2324 ++it) {
limasdf0deef2042017-05-03 19:17:172325 UnloadExtension(*it, UnloadedExtensionReason::PROFILE_SHUTDOWN);
[email protected]ebe07772014-05-22 04:16:062326 }
2327}
rdevlin.croninf2e1cb012017-05-27 01:27:592328
2329void ExtensionService::OnInstalledExtensionsLoaded() {
2330 if (updater_)
2331 updater_->Start();
2332
2333 // Enable any Shared Modules that incorrectly got disabled previously.
2334 // This is temporary code to fix incorrect behavior from previous versions of
2335 // Chrome and can be removed after several releases (perhaps M60).
2336 extensions::ExtensionList to_enable;
2337 for (const auto& extension : registry_->disabled_extensions()) {
2338 if (SharedModuleInfo::IsSharedModule(extension.get()))
2339 to_enable.push_back(extension);
2340 }
2341 for (const auto& extension : to_enable) {
2342 EnableExtension(extension->id());
2343 }
2344
2345 OnBlacklistUpdated();
2346}
lazyboy75b9def2017-06-06 18:56:592347
2348void ExtensionService::MaybeSpinUpLazyBackgroundPage(
2349 const Extension* extension) {
2350 if (!extensions::BackgroundInfo::HasLazyBackgroundPage(extension))
2351 return;
2352
2353 // For orphaned devtools, we will reconnect devtools to it later in
2354 // DidCreateRenderViewForBackgroundPage().
2355 OrphanedDevTools::iterator iter = orphaned_dev_tools_.find(extension->id());
2356 bool has_orphaned_dev_tools = iter != orphaned_dev_tools_.end();
2357
2358 // Reloading component extension does not trigger install, so RuntimeAPI won't
2359 // be able to detect its loading. Therefore, we need to spin up its lazy
2360 // background page.
2361 bool is_component_extension =
2362 Manifest::IsComponentLocation(extension->location());
2363
2364 if (!has_orphaned_dev_tools && !is_component_extension)
2365 return;
2366
2367 // Wake up the event page by posting a dummy task.
2368 extensions::LazyBackgroundTaskQueue* queue =
2369 extensions::LazyBackgroundTaskQueue::Get(profile_);
2370 queue->AddPendingTask(profile_, extension->id(),
Istiaque Ahmedb7f0e2fd92017-10-11 17:37:422371 base::BindOnce(&DoNothingWithExtensionHost));
lazyboy75b9def2017-06-06 18:56:592372}
Takumi Fujimoto43c8c00f2017-07-26 22:48:562373
2374void ExtensionService::UninstallMigratedExtensions() {
2375 std::unique_ptr<ExtensionSet> installed_extensions =
2376 registry_->GenerateInstalledExtensionsSet();
2377
2378 for (const std::string& extension_id : kMigratedExtensionIds) {
2379 if (installed_extensions->Contains(extension_id)) {
2380 UninstallExtension(extension_id, extensions::UNINSTALL_REASON_MIGRATED,
Devlin Cronin218df7f2017-11-21 21:41:312381 nullptr);
Takumi Fujimoto43c8c00f2017-07-26 22:48:562382 }
2383 }
2384}
Michael Giuffridafcb420362017-10-11 21:50:262385
2386void ExtensionService::UpdateForUnloadingExtension(
2387 const extensions::ExtensionId& extension_id) {
2388 int include_mask =
2389 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
2390 const Extension* extension =
2391 registry_->GetExtensionById(extension_id, include_mask);
2392 // The extension may have been unloaded already.
2393 if (!extension)
2394 return;
2395
2396 // Keep information about the extension so that we can reload it later
2397 // even if it's not permanently installed.
2398 unloaded_extension_paths_[extension->id()] = extension->path();
2399
2400 // Clean up if the extension is meant to be enabled after a reload.
2401 reloading_extensions_.erase(extension->id());
2402}