blob: 56838a770a23cb6281f2cb998b9e7c3e149d636e [file] [log] [blame]
[email protected]098fa7a2013-03-08 22:11:171// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]695b5712012-12-06 23:55:288#include <iterator>
[email protected]97d2f1d2011-01-15 00:41:089#include <set>
[email protected]654512b2010-09-01 02:09:4210
[email protected]62433d32011-10-12 22:33:1211#include "base/command_line.h"
[email protected]835d7c82010-10-14 04:38:3812#include "base/metrics/histogram.h"
[email protected]3ea1b182013-02-08 22:38:4113#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1714#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1815#include "base/strings/utf_string_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1416#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0617#include "base/threading/thread_restrictions.h"
[email protected]41a17c52013-06-28 00:27:5318#include "base/time/time.h"
[email protected]15730c42009-09-03 00:03:2019#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3620#include "chrome/browser/chrome_notification_types.h"
mukai87a8d402014-09-15 20:15:0921#include "chrome/browser/content_settings/content_settings_custom_extension_provider.h"
22#include "chrome/browser/content_settings/content_settings_internal_extension_provider.h"
23#include "chrome/browser/content_settings/host_content_settings_map.h"
24#include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
[email protected]d8c8f25f2011-11-02 18:18:0125#include "chrome/browser/extensions/component_loader.h"
[email protected]13e062e2014-08-09 10:21:5526#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3827#include "chrome/browser/extensions/data_deleter.h"
rdevlin.cronin2813c1b2014-08-26 23:51:0828#include "chrome/browser/extensions/extension_action_storage_manager.h"
[email protected]5a145e82014-05-29 22:19:0729#include "chrome/browser/extensions/extension_assets_manager.h"
[email protected]62f051c2012-03-29 17:04:4430#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]373daf972014-04-10 01:50:4431#include "chrome/browser/extensions/extension_error_controller.h"
[email protected]00b38242012-07-18 18:43:2232#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]19eb80152011-02-26 00:28:4333#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5234#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]617342a42013-12-18 23:34:0335#include "chrome/browser/extensions/extension_util.h"
[email protected]2894a512014-06-26 19:03:5636#include "chrome/browser/extensions/external_install_manager.h"
[email protected]5df038b2012-07-16 19:03:2737#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4638#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0139#include "chrome/browser/extensions/installed_loader.h"
[email protected]f3d3b382014-03-14 21:19:2840#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]13e062e2014-08-09 10:21:5541#include "chrome/browser/extensions/permissions_updater.h"
[email protected]90878c52014-04-04 18:21:0242#include "chrome/browser/extensions/shared_module_service.h"
[email protected]13e062e2014-08-09 10:21:5543#include "chrome/browser/extensions/unpacked_installer.h"
rockotdb5192992014-09-16 21:27:0244#include "chrome/browser/extensions/updater/chrome_extension_downloader_factory.h"
[email protected]02c967fe2014-08-12 14:48:4345#include "chrome/browser/extensions/updater/extension_downloader.h"
[email protected]13e062e2014-08-09 10:21:5546#include "chrome/browser/extensions/updater/extension_updater.h"
rockotdb5192992014-09-16 21:27:0247#include "chrome/browser/google/google_brand.h"
[email protected]8ecad5e2010-12-02 21:18:3348#include "chrome/browser/profiles/profile.h"
[email protected]b5a507b22013-11-08 20:41:5749#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1750#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3151#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1552#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5453#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4554#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1955#include "chrome/common/extensions/extension_constants.h"
[email protected]15d4d2d2013-08-09 06:49:0356#include "chrome/common/extensions/features/feature_channel.h"
[email protected]a57209872009-05-04 22:53:1457#include "chrome/common/url_constants.h"
[email protected]fdd28372014-08-21 02:27:2658#include "components/crx_file/id_util.h"
[email protected]7a83dd072013-09-03 12:09:1759#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]dccba4f82014-05-29 00:52:5660#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3761#include "content/public/browser/notification_service.h"
[email protected]f3b1a082011-11-18 00:34:3062#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4263#include "content/public/browser/storage_partition.h"
[email protected]34423532013-11-21 18:13:1064#include "extensions/browser/event_router.h"
[email protected]22401dc2014-03-21 01:38:5765#include "extensions/browser/extension_host.h"
[email protected]dccba4f82014-05-29 00:52:5666#include "extensions/browser/extension_prefs.h"
[email protected]bb1bc9b32013-12-21 03:09:1467#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2468#include "extensions/browser/extension_system.h"
rockotffa65b12014-10-14 17:19:0669#include "extensions/browser/extensions_browser_client.h"
[email protected]4a1d9c0d2014-06-13 12:50:1170#include "extensions/browser/install_flag.h"
[email protected]45f5b7d2014-01-22 23:47:1371#include "extensions/browser/runtime_data.h"
[email protected]e43c61f2014-07-20 21:46:3472#include "extensions/browser/uninstall_reason.h"
[email protected]a9aa62b312013-11-29 05:35:0673#include "extensions/browser/update_observer.h"
rockotffa65b12014-10-14 17:19:0674#include "extensions/browser/updater/extension_cache.h"
[email protected]fb820c02014-03-13 15:07:0875#include "extensions/common/extension_messages.h"
rockot90659852014-09-18 19:31:5276#include "extensions/common/extension_urls.h"
[email protected]5ef835a2013-11-08 20:42:5777#include "extensions/common/feature_switch.h"
[email protected]85df9d12014-04-15 17:02:1478#include "extensions/common/file_util.h"
[email protected]0c3c9732013-09-16 08:53:4179#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5180#include "extensions/common/manifest_handlers/background_info.h"
rockotd5546142014-10-15 00:29:0881#include "extensions/common/manifest_url_handlers.h"
[email protected]dccba4f82014-05-29 00:52:5682#include "extensions/common/one_shot_event.h"
[email protected]c41003472013-10-19 15:37:2583#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4184#include "extensions/common/permissions/permissions_data.h"
[email protected]79a60642012-10-20 21:03:1885
[email protected]eed367e2011-04-12 03:43:3186#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:1887#include "chrome/browser/chromeos/extensions/install_limiter.h"
pilgrime92c5fcd2014-09-10 23:31:2388#include "storage/browser/fileapi/file_system_backend.h"
89#include "storage/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:3190#endif
91
[email protected]55eb70e762012-02-20 17:38:3992using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:3993using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:3694using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:2795using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:3496using extensions::Extension;
97using extensions::ExtensionIdSet;
98using extensions::ExtensionInfo;
[email protected]599539802014-01-07 23:06:0099using extensions::ExtensionRegistry;
[email protected]289c44b2013-12-17 03:26:57100using extensions::ExtensionSet;
[email protected]215a7be2012-10-22 19:53:42101using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46102using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36103using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40104using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06105using extensions::PermissionMessage;
106using extensions::PermissionMessages;
107using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19108using extensions::SharedModuleInfo;
[email protected]90878c52014-04-04 18:21:02109using extensions::SharedModuleService;
[email protected]e410b5f2012-12-14 14:02:24110using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05111
[email protected]0c3c9732013-09-16 08:53:41112namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06113
[email protected]b6ab96d2009-08-20 18:58:19114namespace {
115
[email protected]0db124b02012-11-07 04:55:05116// Wait this many seconds after an extensions becomes idle before updating it.
[email protected]ddc6a122014-06-27 04:52:32117const int kUpdateIdleDelay = 5;
[email protected]0db124b02012-11-07 04:55:05118
[email protected]c6d474f82009-12-16 21:11:06119} // namespace
[email protected]b6ab96d2009-08-20 18:58:19120
[email protected]eaa7dd182010-12-14 11:09:00121// ExtensionService.
[email protected]6014d672008-12-05 00:38:25122
[email protected]8e4560b62011-01-14 10:09:14123void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12124 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20125
[email protected]7a4c6852010-09-16 03:44:22126 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27127 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21128 for (i = external_extension_providers_.begin();
129 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14130 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21131 if (i->get()->HasExtension(id))
132 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22133 }
134
[email protected]0f48fca2011-05-19 18:46:35135 // We get the list of external extensions to check from preferences.
136 // It is possible that an extension has preferences but is not loaded.
137 // For example, an extension that requires experimental permissions
138 // will not be loaded if the experimental command line flag is not used.
139 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40140 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35141 // We can't call UninstallExtension with an unloaded/invalid
142 // extension ID.
143 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
144 << "with id: " << id;
145 return;
146 }
[email protected]42d58f62014-07-31 01:32:45147 UninstallExtension(id,
148 extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION,
149 base::Bind(&base::DoNothing),
150 NULL);
[email protected]7a4c6852010-09-16 03:44:22151}
152
[email protected]7f8f24f2012-11-15 19:40:14153void ExtensionService::SetFileTaskRunnerForTesting(
dcheng605ef8d2014-08-28 18:29:44154 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
[email protected]7f8f24f2012-11-15 19:40:14155 file_task_runner_ = task_runner;
156}
157
[email protected]8e4560b62011-01-14 10:09:14158void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22159 external_extension_providers_.clear();
160}
161
[email protected]8e4560b62011-01-14 10:09:14162void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27163 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12164 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21165 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27166 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22167}
168
[email protected]8f959f522014-08-06 06:26:28169void ExtensionService::BlacklistExtensionForTest(
170 const std::string& extension_id) {
171 ExtensionIdSet blocked;
172 ExtensionIdSet unchanged;
173 blocked.insert(extension_id);
174 UpdateBlockedExtensions(blocked, unchanged);
175}
176
[email protected]9060d8b02012-01-13 02:14:30177bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22178 const std::string& id,
[email protected]d8fd0fd2014-03-24 13:16:06179 const std::string& install_parameter,
[email protected]21a5a672010-11-04 10:47:42180 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48181 Manifest::Location location,
182 int creation_flags,
183 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38184 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:26185 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20186
[email protected]044e86992014-01-24 22:59:11187 if (Manifest::IsExternalLocation(location)) {
188 // All extensions that are not user specific can be cached.
rockotffa65b12014-10-14 17:19:06189 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache()
190 ->AllowCaching(id);
[email protected]044e86992014-01-24 22:59:11191 }
192
[email protected]8a87a5332011-08-11 17:54:59193 const Extension* extension = GetExtensionById(id, true);
194 if (extension) {
195 // Already installed. Skip this install if the current location has
196 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40197 Manifest::Location current = extension->location();
198 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30199 return false;
[email protected]8a87a5332011-08-11 17:54:59200 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22201 }
[email protected]9060d8b02012-01-13 02:14:30202
203 // Add |id| to the set of pending extensions. If it can not be added,
204 // then there is already a pending record from a higher-priority install
205 // source. In this case, signal that this extension will not be
206 // installed by returning false.
207 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]d8fd0fd2014-03-24 13:16:06208 id,
209 install_parameter,
210 update_url,
211 location,
212 creation_flags,
213 mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30214 return false;
[email protected]31bb5ee62012-09-12 22:58:40215 }
[email protected]9060d8b02012-01-13 02:14:30216
[email protected]94fde232012-04-27 10:22:30217 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30218 return true;
[email protected]7a4c6852010-09-16 03:44:22219}
220
[email protected]6aeac8342010-10-01 20:21:18221// static
[email protected]cc2f55c2014-07-08 02:19:04222// This function is used to uninstall an extension via sync. The LOG statements
223// within this function are used to inform the user if the uninstall cannot be
224// done.
[email protected]eaa7dd182010-12-14 11:09:00225bool ExtensionService::UninstallExtensionHelper(
226 ExtensionService* extensions_service,
[email protected]cc2f55c2014-07-08 02:19:04227 const std::string& extension_id,
[email protected]e43c61f2014-07-20 21:46:34228 extensions::UninstallReason reason) {
[email protected]d6ebc9792011-04-07 18:18:33229 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40230 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18231 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33232 << "id: " << extension_id;
233 return false;
[email protected]6aeac8342010-10-01 20:21:18234 }
235
[email protected]d6ebc9792011-04-07 18:18:33236 // The following call to UninstallExtension will not allow an uninstall of a
237 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09238 base::string16 error;
[email protected]42d58f62014-07-31 01:32:45239 if (!extensions_service->UninstallExtension(
240 extension_id, reason, base::Bind(&base::DoNothing), &error)) {
[email protected]d6ebc9792011-04-07 18:18:33241 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
242 << ": " << error;
243 return false;
244 }
[email protected]95da88c42011-03-31 10:07:33245
[email protected]6aeac8342010-10-01 20:21:18246 return true;
247}
248
[email protected]eaa7dd182010-12-14 11:09:00249ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24250 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45251 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23252 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39253 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03254 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35255 bool extensions_enabled,
256 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39257 : extensions::Blacklist::Observer(blacklist),
258 profile_(profile),
[email protected]bd306722012-07-11 20:43:59259 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43260 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28261 blacklist_(blacklist),
[email protected]f8aefb132013-10-30 09:29:52262 extension_sync_service_(NULL),
[email protected]5fdfa562013-12-27 17:43:59263 registry_(extensions::ExtensionRegistry::Get(profile)),
[email protected]6c9bedf2014-05-21 03:55:51264 pending_extension_manager_(profile),
[email protected]a9b00ac2009-06-25 21:03:23265 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03266 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13267 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33268 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35269 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18270 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54271 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19272 installs_delayed_for_gc_(false),
[email protected]e5f8fffe2014-04-02 00:30:44273 is_first_run_(false),
[email protected]90878c52014-04-04 18:21:02274 shared_module_service_(new extensions::SharedModuleService(profile_)) {
[email protected]a29a517a2011-01-21 21:11:12275 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20276
[email protected]36a784c2009-06-23 06:21:08277 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02278 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
279 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23280 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08281
[email protected]3c4abc82012-10-22 22:25:54282 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
283 content::NotificationService::AllBrowserContextsAndSources());
[email protected]adf5a102014-07-31 12:44:06284 registrar_.Add(this,
285 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07286 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27287 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07288 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08289 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
290 content::NotificationService::AllBrowserContextsAndSources());
[email protected]ebe07772014-05-22 04:16:06291 registrar_.Add(this,
292 chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
293 content::Source<Profile>(profile_));
binjin1569c9b2014-09-05 13:33:18294
295 extensions::ExtensionManagementFactory::GetForBrowserContext(profile_)
296 ->AddObserver(this);
[email protected]4814b512009-11-07 00:12:29297
[email protected]93fd78f42009-07-10 16:43:17298 // Set up the ExtensionUpdater
299 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02300 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17301 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25302 base::StringToInt(command_line->GetSwitchValueASCII(
303 switches::kExtensionsUpdateFrequency),
304 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17305 }
[email protected]044e86992014-01-24 22:59:11306 updater_.reset(new extensions::ExtensionUpdater(
307 this,
308 extension_prefs,
309 profile->GetPrefs(),
310 profile,
311 update_frequency,
rockotffa65b12014-10-14 17:19:06312 extensions::ExtensionsBrowserClient::Get()->GetExtensionCache(),
rockotdb5192992014-09-16 21:27:02313 base::Bind(ChromeExtensionDownloaderFactory::CreateForProfile,
314 profile)));
[email protected]93fd78f42009-07-10 16:43:17315 }
316
[email protected]25ae0152011-11-18 14:40:02317 component_loader_.reset(
318 new extensions::ComponentLoader(this,
319 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08320 g_browser_process->local_state(),
321 profile));
[email protected]8e4560b62011-01-14 10:09:14322
[email protected]0436b102011-04-15 18:30:03323 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25324 extensions::ExternalProviderImpl::CreateExternalProviders(
325 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05326 }
[email protected]b671760b2010-07-15 21:13:47327
[email protected]74474042013-11-21 12:03:54328 // Set this as the ExtensionService for app sorting to ensure it causes syncs
329 // if required.
[email protected]b3aa7182013-04-25 04:45:23330 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
331
[email protected]373daf972014-04-10 01:50:44332 error_controller_.reset(
333 new extensions::ExtensionErrorController(profile_, is_first_run_));
[email protected]374ceb6f2014-07-02 19:25:34334 external_install_manager_.reset(
335 new extensions::ExternalInstallManager(profile_, is_first_run_));
[email protected]373daf972014-04-10 01:50:44336
[email protected]c77f2352012-08-08 22:07:58337 extension_action_storage_manager_.reset(
338 new extensions::ExtensionActionStorageManager(profile_));
339
[email protected]cb0e50312011-05-09 15:03:07340 // How long is the path to the Extensions directory?
341 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
342 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25343}
344
[email protected]84df8332011-12-06 18:22:46345const ExtensionSet* ExtensionService::extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14346 return &registry_->enabled_extensions();
[email protected]ddb1e5a2010-12-13 20:10:45347}
348
[email protected]3f213ad2012-07-26 23:39:41349extensions::PendingExtensionManager*
350 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37351 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45352}
353
[email protected]eaa7dd182010-12-14 11:09:00354ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17355 // No need to unload extensions here because they are profile-scoped, and the
356 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17357
[email protected]5df038b2012-07-16 19:03:27358 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14359 for (i = external_extension_providers_.begin();
360 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27361 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14362 provider->ServiceShutdown();
363 }
[email protected]6014d672008-12-05 00:38:25364}
365
[email protected]037228a2012-10-05 01:36:16366void ExtensionService::Shutdown() {
binjin1569c9b2014-09-05 13:33:18367 extensions::ExtensionManagementFactory::GetInstance()
368 ->GetForBrowserContext(profile())
369 ->RemoveObserver(this);
[email protected]4c9201c42013-08-16 04:56:21370 system_->management_policy()->UnregisterProvider(
371 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16372}
373
[email protected]b2907fd2011-03-25 16:43:37374const Extension* ExtensionService::GetExtensionById(
375 const std::string& id, bool include_disabled) const {
[email protected]599539802014-01-07 23:06:00376 int include_mask = ExtensionRegistry::ENABLED;
[email protected]695b5712012-12-06 23:55:28377 if (include_disabled) {
378 // Include blacklisted extensions here because there are hundreds of
379 // callers of this function, and many might assume that this includes those
380 // that have been disabled due to blacklisting.
[email protected]599539802014-01-07 23:06:00381 include_mask |= ExtensionRegistry::DISABLED |
382 ExtensionRegistry::BLACKLISTED;
[email protected]695b5712012-12-06 23:55:28383 }
[email protected]599539802014-01-07 23:06:00384 return registry_->GetExtensionById(id, include_mask);
[email protected]695b5712012-12-06 23:55:28385}
386
[email protected]eaa7dd182010-12-14 11:09:00387void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12388 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20389
[email protected]3350d712013-11-18 09:32:24390 base::Time begin_time = base::Time::Now();
391
[email protected]4a10006a2013-05-17 23:18:35392 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14393 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32394
[email protected]820d9bd2013-04-03 03:46:03395 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
rdevlin.cronin462b2fb2014-10-02 18:32:52396 if (cmd_line->HasSwitch(switches::kInstallEphemeralAppFromWebstore)) {
[email protected]820d9bd2013-04-03 03:46:03397 // The sole purpose of this launch is to install a new extension from CWS
398 // and immediately terminate: loading already installed extensions is
399 // unnecessary and may interfere with the inline install dialog (e.g. if an
400 // extension listens to onStartup and opens a window).
401 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25402 } else {
[email protected]47b60072013-06-20 10:35:56403 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
404 component_loader_->LoadAll();
405 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19406
[email protected]2edd57852013-08-14 22:07:40407 // Attempt to re-enable extensions whose only disable reason is reloading.
408 std::vector<std::string> extensions_to_enable;
[email protected]bb1bc9b32013-12-21 03:09:14409 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
410 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
411 iter != disabled_extensions.end(); ++iter) {
[email protected]2edd57852013-08-14 22:07:40412 const Extension* e = iter->get();
413 if (extension_prefs_->GetDisableReasons(e->id()) ==
414 Extension::DISABLE_RELOAD) {
415 extensions_to_enable.push_back(e->id());
416 }
417 }
418 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
419 it != extensions_to_enable.end(); ++it) {
420 EnableExtension(*it);
421 }
422
[email protected]9f4e4f082013-06-21 07:11:19423 // Finish install (if possible) of extensions that were still delayed while
424 // the browser was shut down.
425 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
426 extension_prefs_->GetAllDelayedInstallInfo());
427 for (size_t i = 0; i < delayed_info->size(); ++i) {
428 ExtensionInfo* info = delayed_info->at(i).get();
429 scoped_refptr<const Extension> extension(NULL);
430 if (info->extension_manifest) {
431 std::string error;
432 extension = Extension::Create(
433 info->extension_path,
434 info->extension_location,
435 *info->extension_manifest,
436 extension_prefs_->GetDelayedInstallCreationFlags(
437 info->extension_id),
438 info->extension_id,
439 &error);
440 if (extension.get())
441 delayed_installs_.Insert(extension);
442 }
443 }
444 MaybeFinishDelayedInstallations();
445
446 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
447 extension_prefs_->GetAllDelayedInstallInfo());
448 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
449 delayed_info2->size() - delayed_info->size());
450
[email protected]47b60072013-06-20 10:35:56451 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25452
[email protected]47b60072013-06-20 10:35:56453 // TODO(erikkay) this should probably be deferred to a future point
454 // rather than running immediately at startup.
455 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57456
[email protected]2d19eb6e2014-01-27 17:30:00457 LoadGreylistFromPrefs();
[email protected]399583b2012-12-11 09:33:42458 }
[email protected]3350d712013-11-18 09:32:24459
460 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
461 base::Time::Now() - begin_time);
[email protected]6014d672008-12-05 00:38:25462}
463
[email protected]2d19eb6e2014-01-27 17:30:00464void ExtensionService::LoadGreylistFromPrefs() {
[email protected]f47f7172014-03-19 19:27:10465 scoped_ptr<ExtensionSet> all_extensions =
466 registry_->GenerateInstalledExtensionsSet();
[email protected]2d19eb6e2014-01-27 17:30:00467
468 for (ExtensionSet::const_iterator it = all_extensions->begin();
469 it != all_extensions->end(); ++it) {
470 extensions::BlacklistState state =
471 extension_prefs_->GetExtensionBlacklistState((*it)->id());
472 if (state == extensions::BLACKLISTED_SECURITY_VULNERABILITY ||
473 state == extensions::BLACKLISTED_POTENTIALLY_UNWANTED ||
474 state == extensions::BLACKLISTED_CWS_POLICY_VIOLATION)
475 greylist_.Insert(*it);
476 }
477}
478
[email protected]31bb5ee62012-09-12 22:58:40479bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45480 const base::FilePath& extension_path,
[email protected]044e86992014-01-24 22:59:11481 bool file_ownership_passed,
[email protected]31bb5ee62012-09-12 22:58:40482 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12483 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54484 if (browser_terminating_) {
485 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
486 // Leak the temp file at extension_path. We don't want to add to the disk
487 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
488 // the file is in the OS temp directory which should be cleaned up for us.
489 return false;
490 }
[email protected]a8af9fdb2010-10-28 21:52:20491
[email protected]3f213ad2012-07-26 23:39:41492 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06493 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32494
[email protected]695b5712012-12-06 23:55:28495 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06496 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33497 LOG(WARNING) << "Will not update extension " << id
498 << " because it is not installed or pending";
499 // Delete extension_path since we're not creating a CrxInstaller
500 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14501 if (!GetFileTaskRunner()->PostTask(
502 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36503 base::Bind(
[email protected]85df9d12014-04-15 17:02:14504 &extensions::file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36505 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03506
507 return false;
[email protected]e957fe52009-06-23 16:51:05508 }
509
[email protected]f8636f92013-08-09 21:02:37510 scoped_refptr<CrxInstaller> installer(
pkotwicz31eb1e8ec2014-10-08 15:26:46511 CrxInstaller::Create(this, scoped_ptr<ExtensionInstallPrompt>()));
[email protected]6dfbbf82010-03-12 23:09:16512 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48513 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06514 if (pending_extension_info) {
515 installer->set_install_source(pending_extension_info->install_source());
pkotwicz31eb1e8ec2014-10-08 15:26:46516 installer->set_allow_silent_install(true);
[email protected]21db9ef2014-05-16 02:06:27517 if (pending_extension_info->remote_install())
518 installer->set_grant_permissions(false);
[email protected]464213a2013-10-15 01:06:48519 creation_flags = pending_extension_info->creation_flags();
520 if (pending_extension_info->mark_acknowledged())
[email protected]374ceb6f2014-07-02 19:25:34521 external_install_manager_->AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06522 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41523 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06524 }
[email protected]7d8b7072012-04-07 01:07:46525 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45526 // its auto-update URL is from the webstore, treat it as a webstore install.
527 // Note that we ignore some older extensions with blank auto-update URLs
528 // because we are mostly concerned with restrictions on NaCl extensions,
529 // which are newer.
[email protected]75764512011-12-19 19:54:28530 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38531 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35532 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06533 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53534 creation_flags |= Extension::FROM_WEBSTORE;
535 }
536
537 // Bookmark apps being updated is kind of a contradiction, but that's because
538 // we mark the default apps as bookmark apps, and they're hosted in the web
539 // store, thus they can get updated. See http://crbug.com/101605 for more
540 // details.
541 if (extension && extension->from_bookmark())
542 creation_flags |= Extension::FROM_BOOKMARK;
543
[email protected]e33bbc22012-08-27 22:05:46544 if (extension && extension->was_installed_by_default())
545 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
546
[email protected]acc3c7c22014-03-19 06:23:39547 if (extension && extension->was_installed_by_oem())
548 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
549
[email protected]6338fa32014-07-16 21:41:59550 if (extension && extension->was_installed_by_custodian())
551 creation_flags |= Extension::WAS_INSTALLED_BY_CUSTODIAN;
552
[email protected]658eae52014-06-14 20:28:05553 if (extension) {
[email protected]411f8ae2014-05-22 11:12:23554 installer->set_is_ephemeral(extension_prefs_->IsEphemeralApp(id));
[email protected]c30bda262014-06-19 04:10:13555 installer->set_do_not_sync(extension_prefs_->DoNotSync(id));
[email protected]658eae52014-06-14 20:28:05556 }
[email protected]ab6c7be42014-01-16 02:05:54557
[email protected]a12ce8b22012-01-17 18:40:53558 installer->set_creation_flags(creation_flags);
559
[email protected]044e86992014-01-24 22:59:11560 installer->set_delete_source(file_ownership_passed);
[email protected]cb0e50312011-05-09 15:03:07561 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16562 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03563
564 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09565 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03566
567 return true;
[email protected]e957fe52009-06-23 16:51:05568}
569
[email protected]bca4b832014-07-17 20:22:34570void ExtensionService::ReloadExtensionImpl(
[email protected]757d60a2014-05-23 00:11:44571 // "transient" because the process of reloading may cause the reference
572 // to become invalid. Instead, use |extension_id|, a copy.
[email protected]bca4b832014-07-17 20:22:34573 const std::string& transient_extension_id,
574 bool be_noisy) {
[email protected]a29a517a2011-01-21 21:11:12575 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05576
577 // If the extension is already reloading, don't reload again.
[email protected]757d60a2014-05-23 00:11:44578 if (extension_prefs_->GetDisableReasons(transient_extension_id) &
[email protected]2a947202013-03-06 04:58:05579 Extension::DISABLE_RELOAD) {
580 return;
581 }
582
[email protected]757d60a2014-05-23 00:11:44583 // Ignore attempts to reload a blacklisted extension. Sometimes this can
584 // happen in a convoluted reload sequence triggered by the termination of a
585 // blacklisted extension and a naive attempt to reload it. For an example see
586 // http://crbug.com/373842.
587 if (registry_->blacklisted_extensions().Contains(transient_extension_id))
588 return;
589
[email protected]650b2d52013-02-10 03:41:45590 base::FilePath path;
[email protected]757d60a2014-05-23 00:11:44591
592 std::string extension_id = transient_extension_id;
593 const Extension* transient_current_extension =
594 GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40595
[email protected]f17dbd42010-08-16 23:21:10596 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]757d60a2014-05-23 00:11:44597 if (transient_current_extension) {
[email protected]4814b512009-11-07 00:12:29598 // If the extension has an inspector open for its background page, detach
599 // the inspector and hang onto a cookie for it, so that we can reattach
600 // later.
[email protected]31d8f5f22012-04-02 15:22:08601 // TODO(yoz): this is not incognito-safe!
[email protected]98b6d942013-11-10 00:34:07602 extensions::ProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13603 extensions::ExtensionHost* host =
604 manager->GetBackgroundHostForExtension(extension_id);
[email protected]b3f957e62014-08-08 10:09:02605 if (host && DevToolsAgentHost::HasFor(host->host_contents())) {
[email protected]4814b512009-11-07 00:12:29606 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09607 scoped_refptr<DevToolsAgentHost> agent_host =
[email protected]b3f957e62014-08-08 10:09:02608 DevToolsAgentHost::GetOrCreateFor(host->host_contents());
609 agent_host->DisconnectWebContents();
[email protected]04ea1bb2013-07-10 09:26:09610 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29611 }
612
[email protected]757d60a2014-05-23 00:11:44613 path = transient_current_extension->path();
[email protected]58076192013-07-19 19:43:50614 // BeingUpgraded is set back to false when the extension is added.
[email protected]757d60a2014-05-23 00:11:44615 system_->runtime_data()->SetBeingUpgraded(transient_current_extension,
616 true);
[email protected]44d62b62012-04-11 00:06:03617 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03618 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16619 } else {
[email protected]d32c02a2014-06-16 22:58:36620 std::map<std::string, base::FilePath>::const_iterator iter =
621 unloaded_extension_paths_.find(extension_id);
622 if (iter == unloaded_extension_paths_.end()) {
623 return;
624 }
[email protected]1eb175082010-02-10 09:26:16625 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06626 }
627
[email protected]757d60a2014-05-23 00:11:44628 transient_current_extension = NULL;
629
[email protected]9f4e4f082013-06-21 07:11:19630 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48631 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05632 return;
633 }
634
[email protected]43ceb002012-02-10 23:19:15635 // If we're reloading a component extension, use the component extension
636 // loader's reloader.
637 if (component_loader_->Exists(extension_id)) {
638 component_loader_->Reload(extension_id);
639 return;
640 }
641
[email protected]e6090e42010-03-23 22:44:08642 // Check the installed extensions to see if what we're reloading was already
643 // installed.
644 scoped_ptr<ExtensionInfo> installed_extension(
645 extension_prefs_->GetInstalledExtensionInfo(extension_id));
646 if (installed_extension.get() &&
647 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01648 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08649 } else {
[email protected]d8c8f25f2011-11-02 18:18:01650 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08651 // We should always be able to remember the extension's path. If it's not in
652 // the map, someone failed to update |unloaded_extension_paths_|.
653 CHECK(!path.empty());
[email protected]bca4b832014-07-17 20:22:34654 scoped_refptr<extensions::UnpackedInstaller> unpacked_installer =
655 extensions::UnpackedInstaller::Create(this);
656 unpacked_installer->set_be_noisy_on_failure(be_noisy);
657 unpacked_installer->Load(path);
[email protected]e6090e42010-03-23 22:44:08658 }
[email protected]9cddd4702009-07-27 22:09:40659}
660
[email protected]bca4b832014-07-17 20:22:34661void ExtensionService::ReloadExtension(const std::string& extension_id) {
662 ReloadExtensionImpl(extension_id, true); // be_noisy
663}
664
665void ExtensionService::ReloadExtensionWithQuietFailure(
666 const std::string& extension_id) {
667 ReloadExtensionImpl(extension_id, false); // be_noisy
668}
669
[email protected]757d60a2014-05-23 00:11:44670bool ExtensionService::UninstallExtension(
671 // "transient" because the process of uninstalling may cause the reference
672 // to become invalid. Instead, use |extenson->id()|.
673 const std::string& transient_extension_id,
[email protected]e43c61f2014-07-20 21:46:34674 extensions::UninstallReason reason,
[email protected]42d58f62014-07-31 01:32:45675 const base::Closure& deletion_done_callback,
[email protected]757d60a2014-05-23 00:11:44676 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12677 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20678
[email protected]757d60a2014-05-23 00:11:44679 scoped_refptr<const Extension> extension =
680 GetInstalledExtension(transient_extension_id);
[email protected]631cf822009-05-15 07:01:25681
[email protected]e7afe2452010-08-22 16:19:13682 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09683 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32684
binjincccacef2014-10-13 19:00:20685 ManagementPolicy* by_policy = system_->management_policy();
[email protected]95da88c42011-03-31 10:07:33686 // Policy change which triggers an uninstall will always set
687 // |external_uninstall| to true so this is the only way to uninstall
688 // managed extensions.
[email protected]90878c52014-04-04 18:21:02689 // Shared modules being uninstalled will also set |external_uninstall| to true
690 // so that we can guarantee users don't uninstall a shared module.
691 // (crbug.com/273300)
692 // TODO(rdevlin.cronin): This is probably not right. We should do something
693 // else, like include an enum IS_INTERNAL_UNINSTALL or IS_USER_UNINSTALL so
694 // we don't do this.
[email protected]cc2f55c2014-07-08 02:19:04695 bool external_uninstall =
[email protected]e43c61f2014-07-20 21:46:34696 (reason == extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT) ||
rdevlin.cronin5f6b69d2014-09-20 01:23:35697 (reason == extensions::UNINSTALL_REASON_REINSTALL) ||
[email protected]e43c61f2014-07-20 21:46:34698 (reason == extensions::UNINSTALL_REASON_ORPHANED_EXTERNAL_EXTENSION) ||
[email protected]6ef60012014-07-22 19:08:12699 (reason == extensions::UNINSTALL_REASON_ORPHANED_SHARED_MODULE) ||
700 (reason == extensions::UNINSTALL_REASON_SYNC &&
701 extension->was_installed_by_custodian());
[email protected]65187152012-06-02 13:14:14702 if (!external_uninstall &&
binjincccacef2014-10-13 19:00:20703 (!by_policy->UserMayModifySettings(extension.get(), error) ||
704 by_policy->MustRemainInstalled(extension.get(), error))) {
[email protected]ad50def52011-10-19 23:17:07705 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06706 extensions::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53707 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09708 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33709 return false;
710 }
[email protected]95da88c42011-03-31 10:07:33711
[email protected]65f173552012-06-28 22:43:58712 syncer::SyncChange sync_change;
rdevlin.cronin5f6b69d2014-09-20 01:23:35713 // Don't sync the uninstall if we're going to reinstall the extension
714 // momentarily.
715 if (extension_sync_service_ &&
716 reason != extensions::UNINSTALL_REASON_REINSTALL) {
[email protected]f8aefb132013-10-30 09:29:52717 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
718 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30719 }
720
[email protected]d820bc52014-03-13 05:35:23721 system_->install_verifier()->Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:46722
[email protected]9b217652010-10-08 22:04:23723 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08724 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09725 RecordPermissionMessagesHistogram(extension.get(),
[email protected]4c86be582014-06-04 08:39:11726 "Extensions.Permissions_Uninstall2");
[email protected]9b217652010-10-08 22:04:23727
[email protected]831aa212010-03-26 13:55:19728 // Unload before doing more cleanup to ensure that nothing is hanging on to
729 // any of these resources.
[email protected]757d60a2014-05-23 00:11:44730 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19731
[email protected]9f1087e2009-06-15 17:29:32732 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05733 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14734 if (!GetFileTaskRunner()->PostTask(
735 FROM_HERE,
[email protected]5a145e82014-05-29 22:19:07736 base::Bind(&ExtensionService::UninstallExtensionOnFileThread,
737 extension->id(),
738 profile_,
[email protected]85df9d12014-04-15 17:02:14739 install_directory_,
[email protected]5a145e82014-05-29 22:19:07740 extension->path())))
[email protected]14908b72011-04-20 06:54:36741 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32742 }
743
[email protected]42d58f62014-07-31 01:32:45744 extensions::DataDeleter::StartDeleting(
745 profile_, extension.get(), deletion_done_callback);
[email protected]0d6ec3a72011-09-02 02:09:43746
[email protected]757d60a2014-05-23 00:11:44747 UntrackTerminatedExtension(extension->id());
[email protected]211030342010-09-30 18:41:06748
749 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07750 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06751 extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:53752 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09753 content::Details<const Extension>(extension.get()));
[email protected]e43c61f2014-07-20 21:46:34754 ExtensionRegistry::Get(profile_)
755 ->TriggerOnUninstalled(extension.get(), reason);
[email protected]d6ebc9792011-04-07 18:18:33756
rdevlin.cronin5f6b69d2014-09-20 01:23:35757 if (sync_change.IsValid()) {
[email protected]757d60a2014-05-23 00:11:44758 extension_sync_service_->ProcessSyncUninstallExtension(extension->id(),
[email protected]f8aefb132013-10-30 09:29:52759 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30760 }
761
[email protected]757d60a2014-05-23 00:11:44762 delayed_installs_.Remove(extension->id());
[email protected]0db124b02012-11-07 04:55:05763
[email protected]757d60a2014-05-23 00:11:44764 extension_prefs_->OnExtensionUninstalled(
765 extension->id(), extension->location(), external_uninstall);
[email protected]d4eda592013-09-18 03:37:57766
[email protected]333b1de2011-09-12 18:28:50767 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18768 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
769
[email protected]d6ebc9792011-04-07 18:18:33770 return true;
[email protected]c10da4b02010-03-25 14:38:32771}
772
[email protected]5a145e82014-05-29 22:19:07773// static
774void ExtensionService::UninstallExtensionOnFileThread(
775 const std::string& id,
776 Profile* profile,
777 const base::FilePath& install_dir,
778 const base::FilePath& extension_path) {
779 extensions::ExtensionAssetsManager* assets_manager =
780 extensions::ExtensionAssetsManager::GetInstance();
781 assets_manager->UninstallExtension(id, profile, install_dir, extension_path);
782}
783
[email protected]c3cfb012011-04-06 22:07:35784bool ExtensionService::IsExtensionEnabled(
785 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14786 if (registry_->enabled_extensions().Contains(extension_id) ||
787 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18788 return true;
[email protected]dc9a74f72012-08-17 18:07:21789 }
[email protected]36429da2011-07-11 20:25:18790
[email protected]bb1bc9b32013-12-21 03:09:14791 if (registry_->disabled_extensions().Contains(extension_id) ||
792 registry_->blacklisted_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25793 return false;
[email protected]695b5712012-12-06 23:55:28794 }
[email protected]ad83ca242011-07-29 01:32:25795
796 // If the extension hasn't been loaded yet, check the prefs for it. Assume
797 // enabled unless otherwise noted.
798 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]27e528322014-05-27 20:54:30799 !extension_prefs_->IsExtensionBlacklisted(extension_id) &&
[email protected]f574c402012-12-04 23:20:31800 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35801}
802
[email protected]eaa7dd182010-12-14 11:09:00803void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12804 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20805
[email protected]06f92562011-04-29 19:27:31806 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39807 return;
[email protected]bb1bc9b32013-12-21 03:09:14808 const Extension* extension =
809 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36810
811 ManagementPolicy* policy = system_->management_policy();
812 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
813 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
814 return;
815 }
[email protected]0c6da502009-08-14 22:32:39816
[email protected]b3317ad2011-04-28 23:46:00817 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28818 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52819
[email protected]06f92562011-04-29 19:27:31820 // This can happen if sync enables an extension that is not
821 // installed yet.
822 if (!extension)
823 return;
824
[email protected]0c6da502009-08-14 22:32:39825 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14826 registry_->AddEnabled(make_scoped_refptr(extension));
827 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39828
[email protected]62d30f42009-10-01 22:36:06829 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30830
[email protected]f74d7f32012-06-19 19:22:51831 // Notify listeners that the extension was enabled.
832 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06833 extensions::NOTIFICATION_EXTENSION_ENABLED,
[email protected]f74d7f32012-06-19 19:22:51834 content::Source<Profile>(profile_),
835 content::Details<const Extension>(extension));
836
[email protected]f8aefb132013-10-30 09:29:52837 if (extension_sync_service_)
838 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:39839}
840
[email protected]44d62b62012-04-11 00:06:03841void ExtensionService::DisableExtension(
842 const std::string& extension_id,
843 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12844 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20845
[email protected]8f959f522014-08-06 06:26:28846 // The extension may have been disabled already. Just add a disable reason.
847 if (!IsExtensionEnabled(extension_id)) {
848 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52849 return;
[email protected]8f959f522014-08-06 06:26:28850 }
[email protected]1784e83a2009-09-08 21:01:52851
[email protected]06f92562011-04-29 19:27:31852 const Extension* extension = GetInstalledExtension(extension_id);
853 // |extension| can be NULL if sync disables an extension that is not
854 // installed yet.
[email protected]ab894542014-07-03 12:35:39855 // EXTERNAL_COMPONENT extensions are not generally modifiable by users, but
856 // can be uninstalled by the browser if the user sets extension-specific
857 // preferences.
[email protected]65187152012-06-02 13:14:14858 if (extension &&
[email protected]505ccdc2013-06-20 20:15:35859 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]ab894542014-07-03 12:35:39860 !system_->management_policy()->UserMayModifySettings(extension, NULL) &&
861 extension->location() != Manifest::EXTERNAL_COMPONENT) {
[email protected]95da88c42011-03-31 10:07:33862 return;
[email protected]65187152012-06-02 13:14:14863 }
[email protected]95da88c42011-03-31 10:07:33864
[email protected]b3317ad2011-04-28 23:46:00865 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28866 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52867
[email protected]599539802014-01-07 23:06:00868 int include_mask =
869 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::DISABLED;
870 extension = registry_->GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31871 if (!extension)
872 return;
873
[email protected]45f5b7d2014-01-22 23:47:13874 // The extension is either enabled or terminated.
875 DCHECK(registry_->enabled_extensions().Contains(extension->id()) ||
876 registry_->terminated_extensions().Contains(extension->id()));
[email protected]64742de32013-04-22 08:44:34877
[email protected]5c094792012-09-07 19:44:53878 // Move it over to the disabled list. Don't send a second unload notification
879 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:14880 registry_->AddDisabled(make_scoped_refptr(extension));
881 if (registry_->enabled_extensions().Contains(extension->id())) {
882 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:13883 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:53884 } else {
[email protected]bb1bc9b32013-12-21 03:09:14885 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:53886 }
[email protected]3bdba0d2011-08-23 07:17:30887
[email protected]f8aefb132013-10-30 09:29:52888 if (extension_sync_service_)
889 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:52890}
891
[email protected]1abf05e2013-07-09 17:04:36892void ExtensionService::DisableUserExtensions(
893 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14894 extensions::ManagementPolicy* management_policy =
895 system_->management_policy();
896 extensions::ExtensionList to_disable;
897
[email protected]bb1bc9b32013-12-21 03:09:14898 const ExtensionSet& enabled_set = registry_->enabled_extensions();
899 for (ExtensionSet::const_iterator extension = enabled_set.begin();
900 extension != enabled_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39901 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14902 to_disable.push_back(*extension);
903 }
[email protected]bb1bc9b32013-12-21 03:09:14904 const ExtensionSet& terminated_set = registry_->terminated_extensions();
905 for (ExtensionSet::const_iterator extension = terminated_set.begin();
906 extension != terminated_set.end(); ++extension) {
[email protected]ab894542014-07-03 12:35:39907 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14908 to_disable.push_back(*extension);
909 }
910
911 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
912 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:38913 if ((*extension)->was_installed_by_default() &&
914 extension_urls::IsWebstoreUpdateUrl(
dchengc7047942014-08-26 05:05:31915 extensions::ManifestURL::GetUpdateURL(extension->get())))
[email protected]3082fe32013-08-06 11:12:38916 continue;
[email protected]1abf05e2013-07-09 17:04:36917 const std::string& id = (*extension)->id();
918 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
919 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:14920 }
921}
922
[email protected]eaa7dd182010-12-14 11:09:00923void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:28924 const Extension* extension) {
925 GrantPermissions(extension);
[email protected]4c86be582014-06-04 08:39:11926 RecordPermissionMessagesHistogram(extension,
927 "Extensions.Permissions_ReEnable2");
[email protected]8d888c12010-11-30 00:00:25928 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
929 EnableExtension(extension->id());
930}
931
[email protected]009633c2013-03-07 22:08:28932void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:45933 CHECK(extension);
[email protected]8d42db52014-06-20 21:50:50934 extensions::PermissionsUpdater(profile()).GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:45935}
936
[email protected]fe2dd7742011-04-19 22:52:49937// static
938void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:05939 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:17940 // Since this is called from multiple sources, and since the histogram macros
941 // use statics, we need to manually lookup the histogram ourselves.
942 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:49943 histogram,
944 1,
[email protected]c2e66e12012-06-27 06:27:06945 PermissionMessage::kEnumBoundary,
946 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:17947 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:49948
[email protected]a6910e72014-06-06 05:04:36949 PermissionMessages permissions =
950 extension->permissions_data()->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49951 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:06952 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49953 } else {
[email protected]c2e66e12012-06-27 06:27:06954 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:52955 it != permissions.end(); ++it)
956 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49957 }
958}
959
[email protected]eaa7dd182010-12-14 11:09:00960void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]f9c8c7c72014-07-31 16:42:31961 // The URLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06962 // was loaded, otherwise a race can arise where a renderer that is created
963 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42964 // that the request context doesn't yet know about. The profile is responsible
965 // for ensuring its URLRequestContexts appropriately discover the loaded
966 // extension.
[email protected]31d8f5f22012-04-02 15:22:08967 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06968
[email protected]d5949312012-12-03 22:13:30969 // Tell renderers about the new extension, unless it's a theme (renderers
970 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:19971 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:49972 for (content::RenderProcessHost::iterator i(
973 content::RenderProcessHost::AllHostsIterator());
974 !i.IsAtEnd(); i.Advance()) {
975 content::RenderProcessHost* host = i.GetCurrentValue();
976 Profile* host_profile =
977 Profile::FromBrowserContext(host->GetBrowserContext());
978 if (host_profile->GetOriginalProfile() ==
979 profile_->GetOriginalProfile()) {
980 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
981 1, ExtensionMsg_Loaded_Params(extension));
982 host->Send(
983 new ExtensionMsg_Loaded(loaded_extensions));
984 }
[email protected]c8d407e2011-04-28 21:27:17985 }
[email protected]77a6970c2011-04-23 16:58:56986 }
987
[email protected]3442a662012-01-12 08:06:17988 // Tell subsystems that use the EXTENSION_LOADED notification about the new
989 // extension.
990 //
991 // NOTE: It is important that this happen after notifying the renderers about
992 // the new extensions so that if we navigate to an extension URL in
[email protected]fdc76192014-04-20 21:54:41993 // ExtensionRegistryObserver::OnLoaded or
994 // NOTIFICATION_EXTENSION_LOADED_DEPRECATED, the
[email protected]dcc47642014-03-26 22:03:49995 // renderer is guaranteed to know about it.
996 registry_->TriggerOnLoaded(extension);
997
[email protected]3442a662012-01-12 08:06:17998 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:06999 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]3442a662012-01-12 08:06:171000 content::Source<Profile>(profile_),
1001 content::Details<const Extension>(extension));
1002
[email protected]dcc47642014-03-26 22:03:491003 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1004 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171005 profile_->GetExtensionSpecialStoragePolicy()->
rdevlin.croninbc8979a2014-09-30 01:08:501006 GrantRightsForExtension(extension, profile_);
[email protected]c8d407e2011-04-28 21:27:171007
[email protected]dcc47642014-03-26 22:03:491008 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1009 // work properly multi-profile. Besides which, it should be using
1010 // ExtensionRegistryObserver. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171011 UpdateActiveExtensionsInCrashReporter();
1012
[email protected]d695bb12014-06-05 16:16:301013 const extensions::PermissionsData* permissions_data =
[email protected]a6910e72014-06-06 05:04:361014 extension->permissions_data();
[email protected]d695bb12014-06-05 16:16:301015
[email protected]c8d407e2011-04-28 21:27:171016 // If the extension has permission to load chrome://favicon/ resources we need
1017 // to make sure that the FaviconSource is registered with the
1018 // ChromeURLDataManager.
[email protected]d695bb12014-06-05 16:16:301019 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171020 FaviconSource* favicon_source = new FaviconSource(profile_,
1021 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531022 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171023 }
[email protected]b07e606e2012-09-15 20:16:151024
[email protected]b07e606e2012-09-15 20:16:151025 // Same for chrome://theme/ resources.
[email protected]d695bb12014-06-05 16:16:301026 if (permissions_data->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151027 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531028 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151029 }
[email protected]b07e606e2012-09-15 20:16:151030
[email protected]5eddc3e2011-10-26 04:33:311031 // Same for chrome://thumb/ resources.
[email protected]d695bb12014-06-05 16:16:301032 if (permissions_data->HasHostPermission(
1033 GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511034 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531035 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311036 }
[email protected]62d30f42009-10-01 22:36:061037}
1038
[email protected]a9f39a312010-12-23 22:14:271039void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591040 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131041 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271042 UnloadedExtensionInfo details(extension, reason);
[email protected]e51232f32014-04-18 20:05:361043
1044 registry_->TriggerOnUnloaded(extension, reason);
1045
[email protected]ad50def52011-10-19 23:17:071046 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061047 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
[email protected]6c2381d2011-10-19 02:52:531048 content::Source<Profile>(profile_),
1049 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061050
[email protected]f3b1a082011-11-18 00:34:301051 for (content::RenderProcessHost::iterator i(
1052 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561053 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301054 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081055 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301056 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081057 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171058 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561059 }
1060
[email protected]31d8f5f22012-04-02 15:22:081061 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]dcc47642014-03-26 22:03:491062
1063 // TODO(kalman): Convert ExtensionSpecialStoragePolicy to a
1064 // BrowserContextKeyedService and use ExtensionRegistryObserver.
[email protected]c8d407e2011-04-28 21:27:171065 profile_->GetExtensionSpecialStoragePolicy()->
1066 RevokeRightsForExtension(extension);
1067
[email protected]b777b332011-04-16 04:01:081068#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231069 // Revoke external file access for the extension from its file system context.
1070 // It is safe to access the extension's storage partition at this point. The
1071 // storage partition may get destroyed only after the extension gets unloaded.
[email protected]3a746ec2014-03-15 05:30:561072 GURL site =
1073 extensions::util::GetSiteForExtensionId(extension->id(), profile_);
[email protected]cd501a72014-08-22 19:58:311074 storage::FileSystemContext* filesystem_context =
1075 BrowserContext::GetStoragePartitionForSite(profile_, site)
1076 ->GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321077 if (filesystem_context && filesystem_context->external_backend()) {
1078 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171079 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061080 }
[email protected]c8d407e2011-04-28 21:27:171081#endif
1082
[email protected]dcc47642014-03-26 22:03:491083 // TODO(kalman): This is broken. The crash reporter is process-wide so doesn't
1084 // work properly multi-profile. Besides which, it should be using
1085 // ExtensionRegistryObserver::OnExtensionLoaded. See http://crbug.com/355029.
[email protected]c8d407e2011-04-28 21:27:171086 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061087}
1088
[email protected]599539802014-01-07 23:06:001089content::BrowserContext* ExtensionService::GetBrowserContext() const {
1090 // Implemented in the .cc file to avoid adding a profile.h dependency to
1091 // extension_service.h.
1092 return profile_;
1093}
1094
[email protected]25ae0152011-11-18 14:40:021095bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351096 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021097}
1098
[email protected]7f8f24f2012-11-15 19:40:141099base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091100 if (file_task_runner_.get())
1101 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141102
1103 // We should be able to interrupt any part of extension install process during
1104 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1105 // will be ignored/deleted while we will block on started tasks.
1106 std::string token("ext_install-");
1107 token.append(profile_->GetPath().AsUTF8Unsafe());
1108 file_task_runner_ = BrowserThread::GetBlockingPool()->
1109 GetSequencedTaskRunnerWithShutdownBehavior(
1110 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1111 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091112 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141113}
1114
[email protected]4ee07c62012-08-21 12:40:421115void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361116 std::vector<std::string> to_unload;
1117 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281118
[email protected]0d54b682013-11-05 14:15:361119 // Loop through the extensions list, finding extensions we need to unload or
1120 // disable.
[email protected]bb1bc9b32013-12-21 03:09:141121 const ExtensionSet& extensions = registry_->enabled_extensions();
1122 for (ExtensionSet::const_iterator iter = extensions.begin();
1123 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361124 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421125 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361126 to_unload.push_back(extension->id());
1127 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1128 if (system_->management_policy()->MustRemainDisabled(
1129 extension, &disable_reason, NULL))
1130 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251131 }
1132
[email protected]0d54b682013-11-05 14:15:361133 for (size_t i = 0; i < to_unload.size(); ++i)
1134 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1135
1136 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1137 to_disable.begin(); i != to_disable.end(); ++i)
1138 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251139}
1140
[email protected]31206602011-04-13 23:07:321141void ExtensionService::CheckForUpdatesSoon() {
[email protected]90878c52014-04-04 18:21:021142 // This can legitimately happen in unit tests.
1143 if (!updater_.get())
1144 return;
1145
1146 if (AreAllExternalProvidersReady()) {
1147 updater_->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351148 } else {
[email protected]90878c52014-04-04 18:21:021149 // Sync can start updating before all the external providers are ready
1150 // during startup. Start the update as soon as those providers are ready,
1151 // but not before.
1152 update_once_all_providers_are_ready_ = true;
[email protected]c3cfb012011-04-06 22:07:351153 }
1154}
1155
[email protected]8e4560b62011-01-14 10:09:141156// Some extensions will autoupdate themselves externally from Chrome. These
1157// are typically part of some larger client application package. To support
1158// these, the extension will register its location in the the preferences file
1159// (and also, on Windows, in the registry) and this code will periodically
1160// check that location for a .crx file, which it will then install locally if
1161// a new version is available.
1162// Errors are reported through ExtensionErrorReporter. Succcess is not
1163// reported.
[email protected]eaa7dd182010-12-14 11:09:001164void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121165 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141166
1167 // Note that this installation is intentionally silent (since it didn't
1168 // go through the front-end). Extensions that are registered in this
1169 // way are effectively considered 'pre-bundled', and so implicitly
1170 // trusted. In general, if something has HKLM or filesystem access,
1171 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121172
[email protected]8e4560b62011-01-14 10:09:141173 // Ask each external extension provider to give us a call back for each
1174 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271175 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141176 for (i = external_extension_providers_.begin();
1177 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271178 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141179 provider->VisitRegisteredExtension();
1180 }
1181
[email protected]50067e52011-10-20 23:17:071182 // Do any required work that we would have done after completion of all
1183 // providers.
[email protected]373daf972014-04-10 01:50:441184 if (external_extension_providers_.empty())
[email protected]50067e52011-10-20 23:17:071185 OnAllExternalProvidersReady();
[email protected]9f1087e2009-06-15 17:29:321186}
1187
[email protected]50067e52011-10-20 23:17:071188void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271189 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121190 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071191 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121192
1193 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141194 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301195 if (AreAllExternalProvidersReady())
1196 OnAllExternalProvidersReady();
1197}
1198
1199bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271200 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141201 for (i = external_extension_providers_.begin();
1202 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301203 if (!i->get()->IsReady())
1204 return false;
[email protected]8e4560b62011-01-14 10:09:141205 }
[email protected]94fde232012-04-27 10:22:301206 return true;
[email protected]50067e52011-10-20 23:17:071207}
1208
1209void ExtensionService::OnAllExternalProvidersReady() {
1210 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301211 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1212 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071213
1214 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301215 if (update_once_all_providers_are_ready_ && updater()) {
1216 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141217 extensions::ExtensionUpdater::CheckParams params;
1218 params.callback = external_updates_finished_callback_;
1219 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141220 }
1221
1222 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231223 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141224 extension_prefs_->GetInstalledExtensionsInfo());
1225 for (size_t i = 0; i < extensions_info->size(); ++i) {
1226 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401227 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141228 CheckExternalUninstall(info->extension_id);
1229 }
[email protected]e5af875f2011-10-10 21:09:141230
[email protected]373daf972014-04-10 01:50:441231 error_controller_->ShowErrorIfNeeded();
[email protected]a9aa5932012-01-25 08:27:401232
[email protected]374ceb6f2014-07-02 19:25:341233 external_install_manager_->UpdateExternalExtensionAlert();
[email protected]e5af875f2011-10-10 21:09:141234}
1235
[email protected]a9f39a312010-12-23 22:14:271236void ExtensionService::UnloadExtension(
1237 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131238 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091239 // Make sure the extension gets deleted after we return from this function.
[email protected]599539802014-01-07 23:06:001240 int include_mask =
1241 ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED;
[email protected]9adb9692010-10-29 23:14:021242 scoped_refptr<const Extension> extension(
[email protected]599539802014-01-07 23:06:001243 registry_->GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251244
[email protected]fa0c96732010-11-17 00:14:231245 // This method can be called via PostTask, so the extension may have been
1246 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091247 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171248 // In case the extension may have crashed/uninstalled. Allow the profile to
1249 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081250 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231251 return;
[email protected]dd163fb02011-05-04 22:22:171252 }
[email protected]0c6da502009-08-14 22:32:391253
[email protected]1eb175082010-02-10 09:26:161254 // Keep information about the extension so that we can reload it later
1255 // even if it's not permanently installed.
1256 unloaded_extension_paths_[extension->id()] = extension->path();
1257
[email protected]f17dbd42010-08-16 23:21:101258 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031259 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101260
[email protected]bb1bc9b32013-12-21 03:09:141261 if (registry_->disabled_extensions().Contains(extension->id())) {
1262 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171263 // Make sure the profile cleans up its RequestContexts when an already
1264 // disabled extension is unloaded (since they are also tracking the disabled
1265 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081266 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]45f5b7d2014-01-22 23:47:131267 // Don't send the unloaded notification. It was sent when the extension
1268 // was disabled.
[email protected]60a174a2013-08-02 20:29:281269 } else {
[email protected]bb1bc9b32013-12-21 03:09:141270 // Remove the extension from the enabled list.
1271 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281272 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391273 }
1274
[email protected]fb789532013-08-27 02:40:211275 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061276 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:211277 content::Source<Profile>(profile_),
1278 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251279}
1280
[email protected]8b1ec202013-09-05 02:09:501281void ExtensionService::RemoveComponentExtension(
1282 const std::string& extension_id) {
1283 scoped_refptr<const Extension> extension(
1284 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131285 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]ebe07772014-05-22 04:16:061286 if (extension.get()) {
1287 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061288 extensions::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
[email protected]ebe07772014-05-22 04:16:061289 content::Source<Profile>(profile_),
1290 content::Details<const Extension>(extension.get()));
[email protected]e43c61f2014-07-20 21:46:341291 ExtensionRegistry::Get(profile_)->TriggerOnUninstalled(
1292 extension.get(), extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT);
[email protected]ebe07772014-05-22 04:16:061293 }
[email protected]8b1ec202013-09-05 02:09:501294}
1295
[email protected]bb1bc9b32013-12-21 03:09:141296void ExtensionService::UnloadAllExtensionsForTest() {
1297 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321298}
1299
[email protected]bb1bc9b32013-12-21 03:09:141300void ExtensionService::ReloadExtensionsForTest() {
1301 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1302 // warning about calling test code in production.
1303 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451304 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011305 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351306 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1307 // times.
[email protected]9f1087e2009-06-15 17:29:321308}
1309
[email protected]820d9bd2013-04-03 03:46:031310void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351311 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031312 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061313 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED,
[email protected]820d9bd2013-04-03 03:46:031314 content::Source<Profile>(profile_),
1315 content::NotificationService::NoDetails());
1316}
1317
[email protected]eaa7dd182010-12-14 11:09:001318void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111319 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171320 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501321
[email protected]bc151cf92013-02-12 04:57:261322 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511323}
1324
[email protected]fcb58a862012-05-01 01:03:151325void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171326 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1327 // default extension state to DISABLED when the --disable-extensions flag
1328 // is set (http://crbug.com/29067).
1329 if (!extensions_enabled() &&
1330 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401331 extension->location() != Manifest::COMPONENT &&
1332 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151333 return;
[email protected]330840c2012-08-29 22:21:011334 }
[email protected]c8d407e2011-04-28 21:27:171335
[email protected]b914e2952013-04-26 07:10:031336 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171337 bool is_extension_installed = false;
1338 const Extension* old = GetInstalledExtension(extension->id());
1339 if (old) {
1340 is_extension_installed = true;
1341 int version_compare_result =
1342 extension->version()->CompareTo(*(old->version()));
1343 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031344 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1345 // that we aren't downgrading.
1346 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171347 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031348 }
[email protected]45f5b7d2014-01-22 23:47:131349 system_->runtime_data()->SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171350
[email protected]1eb175082010-02-10 09:26:161351 // The extension is now loaded, remove its data from unloaded extension map.
1352 unloaded_extension_paths_.erase(extension->id());
1353
[email protected]bb7f40952011-01-13 00:21:201354 // If a terminated extension is loaded, remove it from the terminated list.
1355 UntrackTerminatedExtension(extension->id());
1356
[email protected]f17dbd42010-08-16 23:21:101357 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411358 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101359
[email protected]b914e2952013-04-26 07:10:031360 // Check if the extension's privileges have changed and mark the
1361 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171362 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031363
[email protected]116d40e2013-08-08 17:23:171364 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411365 // To upgrade an extension in place, unload the old one and then load the
1366 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131367 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031368 }
[email protected]0c6da502009-08-14 22:32:391369
[email protected]695b5712012-12-06 23:55:281370 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091371 // Only prefs is checked for the blacklist. We rely on callers to check the
1372 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071373 // installation then threads through the install and pending install flow
1374 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141375 registry_->AddBlacklisted(extension);
[email protected]ca2e82022013-04-29 16:48:411376 } else if (!reloading &&
1377 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141378 registry_->AddDisabled(extension);
[email protected]f8aefb132013-10-30 09:29:521379 if (extension_sync_service_)
1380 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031381 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061382 extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]44d62b62012-04-11 00:06:031383 content::Source<Profile>(profile_),
1384 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441385
[email protected]21db9ef2014-05-16 02:06:271386 // Show the extension disabled error if a permissions increase or a remote
1387 // installation is the reason it was disabled, and no other reasons exist.
1388 int reasons = extension_prefs_->GetDisableReasons(extension->id());
1389 const int kReasonMask = Extension::DISABLE_PERMISSIONS_INCREASE |
1390 Extension::DISABLE_REMOTE_INSTALL;
1391 if (reasons & kReasonMask && !(reasons & ~kReasonMask)) {
1392 extensions::AddExtensionDisabledError(
1393 this,
1394 extension,
1395 extension_prefs_->HasDisableReason(
1396 extension->id(), Extension::DISABLE_REMOTE_INSTALL));
[email protected]62f051c2012-03-29 17:04:441397 }
[email protected]ca2e82022013-04-29 16:48:411398 } else if (reloading) {
1399 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141400 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411401 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281402 } else {
1403 // All apps that are displayed in the launcher are ordered by their ordinals
1404 // so we must ensure they have valid ordinals.
1405 if (extension->RequiresSortOrdinal()) {
[email protected]db378322014-07-21 23:19:341406 extension_prefs_->app_sorting()->SetExtensionVisible(
1407 extension->id(),
1408 extension->ShouldDisplayInNewTabPage() &&
1409 !extension_prefs_->IsEphemeralApp(extension->id()));
1410 if (!extension_prefs_->IsEphemeralApp(extension->id())) {
1411 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1412 extension->id(), syncer::StringOrdinal());
[email protected]bd36b652013-02-27 02:13:251413 }
[email protected]695b5712012-12-06 23:55:281414 }
1415
[email protected]bb1bc9b32013-12-21 03:09:141416 registry_->AddEnabled(extension);
[email protected]f8aefb132013-10-30 09:29:521417 if (extension_sync_service_)
1418 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281419 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311420 }
[email protected]45f5b7d2014-01-22 23:47:131421 system_->runtime_data()->SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:351422}
1423
[email protected]8c484b742012-11-29 06:05:361424void ExtensionService::AddComponentExtension(const Extension* extension) {
1425 const std::string old_version_string(
1426 extension_prefs_->GetVersionString(extension->id()));
[email protected]c5e4a2222014-01-03 16:06:131427 const Version old_version(old_version_string);
[email protected]8c484b742012-11-29 06:05:361428
[email protected]0181a7d2013-11-12 01:08:421429 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361430 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1431 VLOG(1) << "Component extension " << extension->name() << " ("
1432 << extension->id() << ") installing/upgrading from '"
1433 << old_version_string << "' to " << extension->version()->GetString();
1434
1435 AddNewOrUpdatedExtension(extension,
[email protected]9ae73962014-08-14 16:53:411436 Extension::ENABLED,
[email protected]4a1d9c0d2014-06-13 12:50:111437 extensions::kInstallFlagNone,
[email protected]d8fd0fd2014-03-24 13:16:061438 syncer::StringOrdinal(),
1439 std::string());
[email protected]8c484b742012-11-29 06:05:361440 return;
1441 }
1442
1443 AddExtension(extension);
1444}
1445
[email protected]b914e2952013-04-26 07:10:031446void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171447 bool is_extension_installed) {
[email protected]23a85362014-07-07 23:26:191448 extensions::PermissionsUpdater(profile_).InitializePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311449
[email protected]8d888c12010-11-30 00:00:251450 // We keep track of all permissions the user has granted each extension.
1451 // This allows extensions to gracefully support backwards compatibility
1452 // by including unknown permissions in their manifests. When the user
1453 // installs the extension, only the recognized permissions are recorded.
1454 // When the unknown permissions become recognized (e.g., through browser
1455 // upgrade), we can prompt the user to accept these new permissions.
1456 // Extensions can also silently upgrade to less permissions, and then
1457 // silently upgrade to a version that adds these permissions back.
1458 //
1459 // For example, pretend that Chrome 10 includes a permission "omnibox"
1460 // for an API that adds suggestions to the omnibox. An extension can
1461 // maintain backwards compatibility while still having "omnibox" in the
1462 // manifest. If a user installs the extension on Chrome 9, the browser
1463 // will record the permissions it recognized, not including "omnibox."
1464 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1465 // will disable the extension and prompt the user to approve the increase
1466 // in privileges. The extension could then release a new version that
1467 // removes the "omnibox" permission. When the user upgrades, Chrome will
1468 // still remember that "omnibox" had been granted, so that if the
1469 // extension once again includes "omnibox" in an upgrade, the extension
1470 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281471 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251472
[email protected]211a3f32013-05-28 21:48:181473 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361474 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]45928b682013-11-28 08:20:271475 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031476 // Silently grant all active permissions to default apps only on install.
1477 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181478 // Silently grant all active permissions to apps install in kiosk mode on both
1479 // install and update.
1480 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031481 GrantPermissions(extension);
1482
1483 bool is_privilege_increase = false;
1484 // We only need to compare the granted permissions to the current permissions
1485 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051486 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181487 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251488 // Add all the recognized permissions if the granted permissions list
1489 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061490 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311491 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521492 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251493
1494 // Here, we check if an extension's privileges have increased in a manner
1495 // that requires the user's approval. This could occur because the browser
1496 // upgraded and recognized additional privileges, or an extension upgrades
1497 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251498 is_privilege_increase =
1499 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
dchengc7047942014-08-26 05:05:311500 granted_permissions.get(),
[email protected]076ebeda2014-06-06 21:47:261501 extension->permissions_data()->active_permissions().get(),
1502 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251503 }
1504
[email protected]116d40e2013-08-08 17:23:171505 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031506 // If the extension was already disabled, suppress any alerts for becoming
1507 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031508 bool previously_disabled =
1509 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191510 // Legacy disabled extensions do not have a disable reason. Infer that if
1511 // there was no permission increase, it was likely disabled by the user.
1512 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031513 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191514 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031515 }
[email protected]7e9e2422012-12-13 19:54:191516 // Extensions that came to us disabled from sync need a similar inference,
1517 // except based on the new version's permissions.
1518 if (previously_disabled &&
1519 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1520 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1521 extension_prefs_->ClearDisableReasons(extension->id());
1522 if (!is_privilege_increase)
1523 disable_reasons |= Extension::DISABLE_USER_ACTION;
1524 }
[email protected]b914e2952013-04-26 07:10:031525 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251526 }
1527
1528 // Extension has changed permissions significantly. Disable it. A
[email protected]21db9ef2014-05-16 02:06:271529 // notification should be sent by the caller. If the extension is already
1530 // disabled because it was installed remotely, don't add another disable
1531 // reason, but instead always set the "did escalate permissions" flag, to
1532 // ensure enabling it will always show a warning.
1533 if (disable_reasons == Extension::DISABLE_REMOTE_INSTALL) {
1534 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1535 } else if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191536 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491537 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]4c86be582014-06-04 08:39:111538 RecordPermissionMessagesHistogram(extension,
1539 "Extensions.Permissions_AutoDisable2");
[email protected]fe2dd7742011-04-19 22:52:491540 }
[email protected]195c1582013-10-29 18:42:231541 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251542 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031543 }
1544 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281545 extension_prefs_->AddDisableReason(
1546 extension->id(),
1547 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251548 }
1549}
1550
[email protected]eaa7dd182010-12-14 11:09:001551void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391552 std::set<std::string> extension_ids;
[email protected]bb1bc9b32013-12-21 03:09:141553 const ExtensionSet& extensions = registry_->enabled_extensions();
1554 for (ExtensionSet::const_iterator iter = extensions.begin();
1555 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361556 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401557 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461558 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351559 }
1560
[email protected]dcc47642014-03-26 22:03:491561 // TODO(kalman): This is broken. ExtensionService is per-profile.
1562 // crash_keys::SetActiveExtensions is per-process. See
1563 // http://crbug.com/355029.
[email protected]f6431be82013-09-07 02:53:451564 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251565}
1566
[email protected]8266d662011-07-12 21:53:261567void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:501568 const Extension* extension,
[email protected]98270432012-09-11 20:51:241569 const syncer::StringOrdinal& page_ordinal,
[email protected]4a1d9c0d2014-06-13 12:50:111570 int install_flags) {
[email protected]a29a517a2011-01-21 21:11:121571 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201572
[email protected]c3cfb012011-04-06 22:07:351573 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:031574 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]d8fd0fd2014-03-24 13:16:061575 std::string install_parameter;
[email protected]96aebe22014-07-16 04:07:511576 const extensions::PendingExtensionInfo* pending_extension_info =
1577 pending_extension_manager()->GetById(id);
1578 if (pending_extension_info) {
[email protected]8f3bcbd2013-06-05 08:42:401579 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:061580 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321581
[email protected]31bb5ee62012-09-12 22:58:401582 LOG(WARNING) << "ShouldAllowInstall() returned false for "
1583 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:221584 << " and update URL "
1585 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:401586 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541587
[email protected]4416c5a2010-06-26 01:28:571588 // Delete the extension directory since we're not going to
1589 // load it.
[email protected]7f8f24f2012-11-15 19:40:141590 if (!GetFileTaskRunner()->PostTask(
1591 FROM_HERE,
[email protected]85df9d12014-04-15 17:02:141592 base::Bind(&extensions::file_util::DeleteFile,
1593 extension->path(),
1594 true))) {
[email protected]14908b72011-04-20 06:54:361595 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401596 }
[email protected]4416c5a2010-06-26 01:28:571597 return;
1598 }
[email protected]51a3bf8b2012-06-08 22:53:061599
[email protected]d8fd0fd2014-03-24 13:16:061600 install_parameter = pending_extension_info->install_parameter();
[email protected]51a3bf8b2012-06-08 22:53:061601 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:571602 } else {
[email protected]c3cfb012011-04-06 22:07:351603 // We explicitly want to re-enable an uninstalled external
1604 // extension; if we're here, that means the user is manually
1605 // installing the extension.
[email protected]46f3e102014-03-25 01:22:451606 if (extension_prefs_->IsExternalExtensionUninstalled(id)) {
[email protected]6cc7dbae2011-04-29 21:18:331607 initial_enable = true;
1608 }
[email protected]aa142702010-03-26 01:26:331609 }
1610
[email protected]98270432012-09-11 20:51:241611 // Unsupported requirements overrides the management policy.
[email protected]4a1d9c0d2014-06-13 12:50:111612 if (install_flags & extensions::kInstallFlagHasRequirementErrors) {
[email protected]98270432012-09-11 20:51:241613 initial_enable = false;
1614 extension_prefs_->AddDisableReason(
1615 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
1616 // If the extension was disabled because of unsupported requirements but
1617 // now supports all requirements after an update and there are not other
1618 // disable reasons, enable it.
1619 } else if (extension_prefs_->GetDisableReasons(id) ==
1620 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
1621 initial_enable = true;
1622 extension_prefs_->ClearDisableReasons(id);
1623 }
1624
[email protected]4a1d9c0d2014-06-13 12:50:111625 if (install_flags & extensions::kInstallFlagIsBlacklistedForMalware) {
[email protected]9f3c8532013-07-31 19:52:071626 // Installation of a blacklisted extension can happen from sync, policy,
1627 // etc, where to maintain consistency we need to install it, just never
1628 // load it (see AddExtension). Usually it should be the job of callers to
1629 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
1630 // showing the install dialogue).
[email protected]7c82539c2014-02-19 06:09:171631 extension_prefs_->AcknowledgeBlacklistedExtension(id);
[email protected]9f3c8532013-07-31 19:52:071632 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
1633 extension->location(),
1634 Manifest::NUM_LOCATIONS);
1635 }
1636
[email protected]695b5712012-12-06 23:55:281637 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:551638 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
1639 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:301640 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:401641 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]4c86be582014-06-04 08:39:111642 RecordPermissionMessagesHistogram(extension,
1643 "Extensions.Permissions_Install2");
[email protected]69084dc2012-11-17 07:39:301644 } else {
1645 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
1646 extension->GetType(), 100);
1647 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:401648 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]8f959f522014-08-06 06:26:281649
1650 // A fully installed app cannot be demoted to an ephemeral app.
1651 if ((install_flags & extensions::kInstallFlagIsEphemeral) &&
1652 !extension_prefs_->IsEphemeralApp(id)) {
1653 install_flags &= ~static_cast<int>(extensions::kInstallFlagIsEphemeral);
1654 }
[email protected]07533022011-06-27 20:42:551655 }
1656
[email protected]399583b2012-12-11 09:33:421657 const Extension::State initial_state =
1658 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]4a1d9c0d2014-06-13 12:50:111659 if (ShouldDelayExtensionUpdate(
1660 id,
1661 !!(install_flags & extensions::kInstallFlagInstallImmediately))) {
[email protected]9f3c8532013-07-31 19:52:071662 extension_prefs_->SetDelayedInstallInfo(
1663 extension,
1664 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111665 install_flags,
[email protected]9f3c8532013-07-31 19:52:071666 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
[email protected]d8fd0fd2014-03-24 13:16:061667 page_ordinal,
1668 install_parameter);
[email protected]0db124b02012-11-07 04:55:051669
1670 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:191671 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:051672
[email protected]75bdcb872013-03-13 00:41:451673 // Notify observers that app update is available.
1674 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:061675 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:051676 return;
1677 }
1678
[email protected]90878c52014-04-04 18:21:021679 extensions::SharedModuleService::ImportStatus status =
1680 shared_module_service_->SatisfyImports(extension);
[email protected]e5f8fffe2014-04-02 00:30:441681 if (installs_delayed_for_gc_) {
[email protected]9f3c8532013-07-31 19:52:071682 extension_prefs_->SetDelayedInstallInfo(
1683 extension,
1684 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111685 install_flags,
[email protected]9f3c8532013-07-31 19:52:071686 extensions::ExtensionPrefs::DELAY_REASON_GC,
[email protected]d8fd0fd2014-03-24 13:16:061687 page_ordinal,
1688 install_parameter);
[email protected]399583b2012-12-11 09:33:421689 delayed_installs_.Insert(extension);
[email protected]90878c52014-04-04 18:21:021690 } else if (status != SharedModuleService::IMPORT_STATUS_OK) {
1691 if (status == SharedModuleService::IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:071692 extension_prefs_->SetDelayedInstallInfo(
1693 extension,
1694 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111695 install_flags,
[email protected]9f4e4f082013-06-21 07:11:191696 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
[email protected]d8fd0fd2014-03-24 13:16:061697 page_ordinal,
1698 install_parameter);
[email protected]9f4e4f082013-06-21 07:11:191699 delayed_installs_.Insert(extension);
1700 }
[email protected]399583b2012-12-11 09:33:421701 } else {
[email protected]9f3c8532013-07-31 19:52:071702 AddNewOrUpdatedExtension(extension,
1703 initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111704 install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061705 page_ordinal,
1706 install_parameter);
[email protected]399583b2012-12-11 09:33:421707 }
[email protected]8c484b742012-11-29 06:05:361708}
1709
binjin1569c9b2014-09-05 13:33:181710void ExtensionService::OnExtensionManagementSettingsChanged() {
1711 error_controller_->ShowErrorIfNeeded();
1712 CheckManagementPolicy();
1713}
1714
[email protected]8c484b742012-11-29 06:05:361715void ExtensionService::AddNewOrUpdatedExtension(
1716 const Extension* extension,
[email protected]399583b2012-12-11 09:33:421717 Extension::State initial_state,
[email protected]4a1d9c0d2014-06-13 12:50:111718 int install_flags,
[email protected]d8fd0fd2014-03-24 13:16:061719 const syncer::StringOrdinal& page_ordinal,
1720 const std::string& install_parameter) {
[email protected]8c484b742012-11-29 06:05:361721 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]760f743b2014-05-28 13:52:021722 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]4a1d9c0d2014-06-13 12:50:111723 extension_prefs_->OnExtensionInstalled(
1724 extension, initial_state, page_ordinal, install_flags, install_parameter);
[email protected]9e9c1d12013-07-31 01:58:121725 delayed_installs_.Remove(extension->id());
[email protected]913ffca92014-03-27 15:26:161726 if (InstallVerifier::NeedsVerification(*extension))
1727 system_->install_verifier()->VerifyExtension(extension->id());
[email protected]760f743b2014-05-28 13:52:021728 FinishInstallation(extension, was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481729}
1730
1731void ExtensionService::MaybeFinishDelayedInstallation(
1732 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:191733 // Check if the extension already got installed.
1734 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:481735 return;
[email protected]9f4e4f082013-06-21 07:11:191736 extensions::ExtensionPrefs::DelayReason reason =
1737 extension_prefs_->GetDelayedInstallReason(extension_id);
1738
1739 // Check if the extension is idle. DELAY_REASON_NONE is used for older
1740 // preferences files that will not have set this field but it was previously
1741 // only used for idle updates.
1742 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
1743 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]1d5cf4142014-01-24 18:25:221744 is_ready() && !extensions::util::IsExtensionIdle(extension_id, profile_))
[email protected]6f6101832012-11-27 22:10:481745 return;
1746
[email protected]9f4e4f082013-06-21 07:11:191747 const Extension* extension = delayed_installs_.GetByID(extension_id);
1748 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
[email protected]90878c52014-04-04 18:21:021749 extensions::SharedModuleService::ImportStatus status =
1750 shared_module_service_->SatisfyImports(extension);
1751 if (status != SharedModuleService::IMPORT_STATUS_OK) {
1752 if (status == SharedModuleService::IMPORT_STATUS_UNRECOVERABLE) {
[email protected]9f4e4f082013-06-21 07:11:191753 delayed_installs_.Remove(extension_id);
1754 // Make sure no version of the extension is actually installed, (i.e.,
1755 // that this delayed install was not an update).
1756 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
1757 extension_prefs_->DeleteExtensionPrefs(extension_id);
1758 }
1759 return;
1760 }
1761 }
1762
[email protected]6f6101832012-11-27 22:10:481763 FinishDelayedInstallation(extension_id);
1764}
1765
1766void ExtensionService::FinishDelayedInstallation(
1767 const std::string& extension_id) {
1768 scoped_refptr<const Extension> extension(
1769 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:091770 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:191771 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:481772
[email protected]760f743b2014-05-28 13:52:021773 bool was_ephemeral = extension_prefs_->IsEphemeralApp(extension->id());
[email protected]399583b2012-12-11 09:33:421774 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:481775 NOTREACHED();
1776
[email protected]760f743b2014-05-28 13:52:021777 FinishInstallation(extension.get(), was_ephemeral);
[email protected]6f6101832012-11-27 22:10:481778}
1779
[email protected]760f743b2014-05-28 13:52:021780void ExtensionService::FinishInstallation(
1781 const Extension* extension, bool was_ephemeral) {
[email protected]e7554c3f2013-05-29 00:36:561782 const extensions::Extension* existing_extension =
1783 GetInstalledExtension(extension->id());
1784 bool is_update = false;
1785 std::string old_name;
1786 if (existing_extension) {
1787 is_update = true;
1788 old_name = existing_extension->name();
1789 }
[email protected]760f743b2014-05-28 13:52:021790 bool from_ephemeral =
1791 was_ephemeral && !extension_prefs_->IsEphemeralApp(extension->id());
1792 extensions::InstalledExtensionInfo details(
1793 extension, is_update, from_ephemeral, old_name);
[email protected]fcb58a862012-05-01 01:03:151794 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061795 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]fcb58a862012-05-01 01:03:151796 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:021797 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:151798
[email protected]bc44b5da2014-06-12 14:20:001799 registry_->TriggerOnWillBeInstalled(
[email protected]760f743b2014-05-28 13:52:021800 extension, is_update, from_ephemeral, old_name);
[email protected]17f07822014-05-22 08:45:151801
[email protected]399583b2012-12-11 09:33:421802 // Unpacked extensions default to allowing file access, but if that has been
1803 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:401804 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:421805 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
1806 extension_prefs_->SetAllowFileAccess(extension->id(), true);
1807 }
1808
[email protected]6f6101832012-11-27 22:10:481809 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:031810
[email protected]bc44b5da2014-06-12 14:20:001811 // Notify observers that need to know when an installation is complete.
[email protected]38e872532014-07-16 23:27:511812 registry_->TriggerOnInstalled(extension, is_update);
[email protected]bc44b5da2014-06-12 14:20:001813
[email protected]9f4e4f082013-06-21 07:11:191814 // Check extensions that may have been delayed only because this shared module
1815 // was not available.
[email protected]374ceb6f2014-07-02 19:25:341816 if (SharedModuleInfo::IsSharedModule(extension))
[email protected]9f4e4f082013-06-21 07:11:191817 MaybeFinishDelayedInstallations();
[email protected]4a190632009-05-09 01:07:421818}
1819
[email protected]760f743b2014-05-28 13:52:021820void ExtensionService::PromoteEphemeralApp(
1821 const extensions::Extension* extension, bool is_from_sync) {
1822 DCHECK(GetInstalledExtension(extension->id()) &&
1823 extension_prefs_->IsEphemeralApp(extension->id()));
1824
[email protected]db378322014-07-21 23:19:341825 if (extension->RequiresSortOrdinal()) {
1826 extension_prefs_->app_sorting()->SetExtensionVisible(
1827 extension->id(), extension->ShouldDisplayInNewTabPage());
1828
1829 if (!is_from_sync) {
[email protected]760f743b2014-05-28 13:52:021830 // Reset the sort ordinals of the app to ensure it is added to the default
1831 // position, like newly installed apps would.
1832 extension_prefs_->app_sorting()->ClearOrdinals(extension->id());
[email protected]760f743b2014-05-28 13:52:021833 }
1834
[email protected]db378322014-07-21 23:19:341835 extension_prefs_->app_sorting()->EnsureValidOrdinals(
1836 extension->id(), syncer::StringOrdinal());
1837 }
1838
[email protected]760f743b2014-05-28 13:52:021839 // Remove the ephemeral flags from the preferences.
1840 extension_prefs_->OnEphemeralAppPromoted(extension->id());
1841
1842 // Fire install-related events to allow observers to handle the promotion
1843 // of the ephemeral app.
1844 extensions::InstalledExtensionInfo details(
1845 extension,
1846 true /* is update */,
1847 true /* from ephemeral */,
1848 extension->name() /* old name */);
1849 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061850 extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:021851 content::Source<Profile>(profile_),
1852 content::Details<const extensions::InstalledExtensionInfo>(&details));
1853
1854 registry_->TriggerOnWillBeInstalled(
1855 extension,
1856 true /* is update */,
1857 true /* from ephemeral */,
1858 extension->name() /* old name */);
1859
1860 if (registry_->enabled_extensions().Contains(extension->id())) {
[email protected]8f959f522014-08-06 06:26:281861 // If the app is already enabled and loaded, fire the load events to allow
1862 // observers to handle the promotion of the ephemeral app.
[email protected]760f743b2014-05-28 13:52:021863 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061864 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
[email protected]760f743b2014-05-28 13:52:021865 content::Source<Profile>(profile_),
1866 content::Details<const Extension>(extension));
1867
1868 registry_->TriggerOnLoaded(extension);
[email protected]8f959f522014-08-06 06:26:281869 } else {
1870 // Cached ephemeral apps may be updated and disabled due to permissions
1871 // increase. The app can be enabled (as long as no other disable reasons
1872 // exist) as the install was user-acknowledged.
1873 int disable_mask = Extension::DISABLE_NONE;
1874 if (!is_from_sync)
1875 disable_mask |= Extension::DISABLE_PERMISSIONS_INCREASE;
1876
1877 int other_disable_reasons =
1878 extension_prefs_->GetDisableReasons(extension->id()) & ~disable_mask;
1879 if (!other_disable_reasons) {
1880 if (extension_prefs_->DidExtensionEscalatePermissions(extension->id()))
1881 GrantPermissionsAndEnableExtension(extension);
1882 else
1883 EnableExtension(extension->id());
1884 }
[email protected]760f743b2014-05-28 13:52:021885 }
1886
[email protected]38e872532014-07-16 23:27:511887 registry_->TriggerOnInstalled(extension, true);
[email protected]bc44b5da2014-06-12 14:20:001888
[email protected]760f743b2014-05-28 13:52:021889 if (!is_from_sync && extension_sync_service_)
1890 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1891}
1892
[email protected]76b65442012-11-17 14:11:481893const Extension* ExtensionService::GetPendingExtensionUpdate(
1894 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:191895 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:481896}
1897
mukai87a8d402014-09-15 20:15:091898void ExtensionService::RegisterContentSettings(
1899 HostContentSettingsMap* host_content_settings_map) {
1900 host_content_settings_map->RegisterProvider(
1901 HostContentSettingsMap::INTERNAL_EXTENSION_PROVIDER,
1902 scoped_ptr<content_settings::ObservableProvider>(
1903 new content_settings::InternalExtensionProvider(this)));
1904
1905 host_content_settings_map->RegisterProvider(
1906 HostContentSettingsMap::CUSTOM_EXTENSION_PROVIDER,
1907 scoped_ptr<content_settings::ObservableProvider>(
1908 new content_settings::CustomExtensionProvider(
1909 extensions::ContentSettingsService::Get(
1910 profile_)->content_settings_store(),
1911 profile_->GetOriginalProfile() != profile_)));
1912}
1913
[email protected]bb7f40952011-01-13 00:21:201914void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]bb1bc9b32013-12-21 03:09:141915 // No need to check for duplicates; inserting a duplicate is a no-op.
1916 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]3964e572014-02-13 21:57:411917 extensions_being_terminated_.erase(extension->id());
[email protected]b0af4792013-10-23 09:12:131918 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:201919}
1920
[email protected]e9d7496e2014-04-18 01:25:461921void ExtensionService::TerminateExtension(const std::string& extension_id) {
1922 const Extension* extension = GetInstalledExtension(extension_id);
1923 TrackTerminatedExtension(extension);
1924}
1925
[email protected]bb7f40952011-01-13 00:21:201926void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]cb1f4ac2014-08-07 16:55:421927 std::string lowercase_id = base::StringToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:141928 const Extension* extension =
1929 registry_->terminated_extensions().GetByID(lowercase_id);
1930 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:211931 if (extension) {
1932 content::NotificationService::current()->Notify(
[email protected]adf5a102014-07-31 12:44:061933 extensions::NOTIFICATION_EXTENSION_REMOVED,
[email protected]fb789532013-08-27 02:40:211934 content::Source<Profile>(profile_),
1935 content::Details<const Extension>(extension));
1936 }
[email protected]bb7f40952011-01-13 00:21:201937}
1938
[email protected]8001df22011-04-28 19:59:471939const Extension* ExtensionService::GetInstalledExtension(
1940 const std::string& id) const {
[email protected]599539802014-01-07 23:06:001941 return registry_->GetExtensionById(id, ExtensionRegistry::EVERYTHING);
[email protected]0dfe05c2011-02-23 23:03:361942}
1943
[email protected]9060d8b02012-01-13 02:14:301944bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:321945 const std::string& id,
[email protected]c5e4a2222014-01-03 16:06:131946 const Version* version,
[email protected]650b2d52013-02-10 03:41:451947 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:401948 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:511949 int creation_flags,
1950 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:381951 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fdd28372014-08-21 02:27:261952 CHECK(crx_file::id_util::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:011953 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:301954 return false;
[email protected]a8af9fdb2010-10-28 21:52:201955
[email protected]7577a5c52009-07-30 06:21:581956 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491957 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581958 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:021959 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:351960
[email protected]7577a5c52009-07-30 06:21:581961 if (existing) {
[email protected]910f72ce2012-08-24 01:38:351962 // The default apps will have the location set as INTERNAL. Since older
1963 // default apps are installed as EXTERNAL, we override them. However, if the
1964 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:401965 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:351966 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:401967 (location == Manifest::INTERNAL &&
1968 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:351969
1970 if (!is_default_apps_migration) {
1971 DCHECK(version);
1972
1973 switch (existing->version()->CompareTo(*version)) {
1974 case -1: // existing version is older, we should upgrade
1975 break;
1976 case 0: // existing version is same, do nothing
1977 return false;
1978 case 1: // existing version is newer, uh-oh
1979 LOG(WARNING) << "Found external version of extension " << id
1980 << "that is older than current version. Current version "
1981 << "is: " << existing->VersionString() << ". New "
1982 << "version is: " << version->GetString()
1983 << ". Keeping current version.";
1984 return false;
1985 }
[email protected]7577a5c52009-07-30 06:21:581986 }
1987 }
1988
[email protected]9060d8b02012-01-13 02:14:301989 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:301990 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:481991 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:301992 return false;
[email protected]e3987852012-05-04 10:06:301993 }
[email protected]9c635f22010-12-02 09:36:361994
[email protected]14908b72011-04-20 06:54:361995 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:371996 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:161997 installer->set_install_source(location);
1998 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:071999 installer->set_expected_version(*version);
2000 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312001 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182002#if defined(OS_CHROMEOS)
2003 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2004#else
[email protected]6dfbbf82010-03-12 23:09:162005 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182006#endif
[email protected]47fc70c2011-12-06 07:29:512007
2008 // Depending on the source, a new external extension might not need a user
2009 // notification on installation. For such extensions, mark them acknowledged
2010 // now to suppress the notification.
2011 if (mark_acknowledged)
[email protected]374ceb6f2014-07-02 19:25:342012 external_install_manager_->AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302013
2014 return true;
[email protected]7577a5c52009-07-30 06:21:582015}
2016
[email protected]eaa7dd182010-12-14 11:09:002017void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132018 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182019 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332020 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182021 if (iter == orphaned_dev_tools_.end())
2022 return;
2023
[email protected]b3f957e62014-08-08 10:09:022024 iter->second->ConnectWebContents(host->host_contents());
[email protected]406027c02010-09-27 08:03:182025 orphaned_dev_tools_.erase(iter);
2026}
2027
[email protected]432115822011-07-10 15:52:272028void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532029 const content::NotificationSource& source,
2030 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272031 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542032 case chrome::NOTIFICATION_APP_TERMINATING:
2033 // Shutdown has started. Don't start any more extension installs.
2034 // (We cannot use ExtensionService::Shutdown() for this because it
2035 // happens too late in browser teardown.)
2036 browser_terminating_ = true;
2037 break;
[email protected]adf5a102014-07-31 12:44:062038 case extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532039 if (profile_ !=
2040 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322041 break;
[email protected]6c2381d2011-10-19 02:52:532042 }
[email protected]a4ed6282009-12-14 20:51:162043
[email protected]3a1dc572012-07-31 22:25:132044 extensions::ExtensionHost* host =
2045 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262046
[email protected]3964e572014-02-13 21:57:412047 // If the extension is already being terminated, there is nothing left to
2048 // do.
2049 if (!extensions_being_terminated_.insert(host->extension_id()).second)
2050 break;
2051
[email protected]fa2416f2011-05-03 08:41:202052 // Mark the extension as terminated and Unload it. We want it to
2053 // be in a consistent state: either fully working or not loaded
2054 // at all, but never half-crashed. We do it in a PostTask so
2055 // that other handlers of this notification will still have
2056 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162057 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362058 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362059 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202060 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012061 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202062 host->extension()));
[email protected]31f77262009-12-02 20:48:532063 break;
2064 }
[email protected]432115822011-07-10 15:52:272065 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302066 content::RenderProcessHost* process =
2067 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192068 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302069 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192070 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2071 break;
2072
[email protected]fafdc842014-01-17 18:09:082073 extensions::ProcessMap* process_map =
2074 extensions::ProcessMap::Get(profile_);
2075 if (process_map->Contains(process->GetID())) {
[email protected]52b76592013-11-02 17:59:032076 // An extension process was terminated, this might have resulted in an
2077 // app or extension becoming idle.
2078 std::set<std::string> extension_ids =
[email protected]fafdc842014-01-17 18:09:082079 process_map->GetExtensionsInProcess(process->GetID());
elijahtaylor0def4432014-10-06 18:15:112080 // In addition to the extensions listed in the process map, one of those
2081 // extensions could be referencing a shared module which is waiting for
2082 // idle to update. Check all imports of these extensions, too.
2083 std::set<std::string> import_ids;
2084 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2085 it != extension_ids.end();
2086 ++it) {
2087 const Extension* extension = GetExtensionById(*it, true);
2088 if (!extension)
2089 continue;
2090 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2091 SharedModuleInfo::GetImports(extension);
2092 std::vector<SharedModuleInfo::ImportInfo>::const_iterator import_it;
2093 for (import_it = imports.begin(); import_it != imports.end();
2094 import_it++) {
2095 import_ids.insert((*import_it).extension_id);
2096 }
2097 }
2098 extension_ids.insert(import_ids.begin(), import_ids.end());
2099
[email protected]52b76592013-11-02 17:59:032100 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2101 it != extension_ids.end(); ++it) {
2102 if (delayed_installs_.Contains(*it)) {
2103 base::MessageLoop::current()->PostDelayedTask(
2104 FROM_HERE,
2105 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2106 AsWeakPtr(), *it),
2107 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2108 }
2109 }
2110 }
2111
[email protected]fafdc842014-01-17 18:09:082112 process_map->RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462113 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202114 BrowserThread::IO,
2115 FROM_HERE,
2116 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082117 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302118 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212119 break;
2120 }
[email protected]92dd8d92013-02-26 00:41:082121 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452122 // Notify observers that chrome update is available.
2123 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2124 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082125 break;
2126 }
[email protected]ebe07772014-05-22 04:16:062127 case chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED: {
2128 OnProfileDestructionStarted();
2129 break;
2130 }
[email protected]aa96d3a2010-08-21 08:45:252131
[email protected]4814b512009-11-07 00:12:292132 default:
2133 NOTREACHED() << "Unexpected notification type.";
2134 }
2135}
2136
[email protected]612a1cb12012-10-17 13:18:032137bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2138 // Extensions installed by policy can't be disabled. So even if a previous
2139 // installation disabled the extension, make sure it is now enabled.
[email protected]ab894542014-07-03 12:35:392140 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
[email protected]612a1cb12012-10-17 13:18:032141 return true;
2142
2143 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2144 return false;
2145
[email protected]41070e8d2012-10-24 01:34:362146 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032147 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542148 // enabling them. Hosted apps are excepted because they are not dangerous
2149 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402150 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2151 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032152 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2153 return false;
2154 }
2155 }
[email protected]612a1cb12012-10-17 13:18:032156
2157 return true;
2158}
[email protected]0db124b02012-11-07 04:55:052159
[email protected]e7aa7b7e2012-11-27 04:51:222160bool ExtensionService::ShouldDelayExtensionUpdate(
2161 const std::string& extension_id,
[email protected]4a1d9c0d2014-06-13 12:50:112162 bool install_immediately) const {
[email protected]e7aa7b7e2012-11-27 04:51:222163 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2164
2165 // If delayed updates are globally disabled, or just for this extension,
2166 // don't delay.
[email protected]4a1d9c0d2014-06-13 12:50:112167 if (!install_updates_when_idle_ || install_immediately)
[email protected]e7aa7b7e2012-11-27 04:51:222168 return false;
2169
[email protected]695b5712012-12-06 23:55:282170 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222171 // If there is no old extension, this is not an update, so don't delay.
2172 if (!old)
2173 return false;
2174
[email protected]9367eabc2013-03-01 01:29:292175 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222176 // Delay installation if the extension listens for the onUpdateAvailable
2177 // event.
2178 return system_->event_router()->ExtensionHasEventListener(
2179 extension_id, kOnUpdateAvailableEvent);
2180 } else {
2181 // Delay installation if the extension is not idle.
[email protected]1d5cf4142014-01-24 18:25:222182 return !extensions::util::IsExtensionIdle(extension_id, profile_);
[email protected]e7aa7b7e2012-11-27 04:51:222183 }
[email protected]0db124b02012-11-07 04:55:052184}
[email protected]fdd679b2012-11-15 20:49:392185
[email protected]e5f8fffe2014-04-02 00:30:442186void ExtensionService::OnGarbageCollectIsolatedStorageStart() {
2187 DCHECK(!installs_delayed_for_gc_);
2188 installs_delayed_for_gc_ = true;
[email protected]399583b2012-12-11 09:33:422189}
2190
2191void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]e5f8fffe2014-04-02 00:30:442192 DCHECK(installs_delayed_for_gc_);
2193 installs_delayed_for_gc_ = false;
[email protected]9f4e4f082013-06-21 07:11:192194 MaybeFinishDelayedInstallations();
2195}
2196
2197void ExtensionService::MaybeFinishDelayedInstallations() {
2198 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422199 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2200 it != delayed_installs_.end();
2201 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192202 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422203 }
[email protected]9f4e4f082013-06-21 07:11:192204 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2205 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422206 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192207 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422208 }
[email protected]399583b2012-12-11 09:33:422209}
2210
[email protected]fdd679b2012-11-15 20:49:392211void ExtensionService::OnBlacklistUpdated() {
[email protected]2d19eb6e2014-01-27 17:30:002212 blacklist_->GetBlacklistedIDs(
[email protected]f47f7172014-03-19 19:27:102213 registry_->GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252214 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282215}
2216
[email protected]2d19eb6e2014-01-27 17:30:002217void ExtensionService::ManageBlacklist(
2218 const extensions::Blacklist::BlacklistStateMap& state_map) {
[email protected]54ee8192014-03-29 17:37:242219 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]695b5712012-12-06 23:55:282220
[email protected]2d19eb6e2014-01-27 17:30:002221 std::set<std::string> blocked;
2222 ExtensionIdSet greylist;
2223 ExtensionIdSet unchanged;
2224 for (extensions::Blacklist::BlacklistStateMap::const_iterator it =
2225 state_map.begin();
2226 it != state_map.end();
2227 ++it) {
2228 switch (it->second) {
2229 case extensions::NOT_BLACKLISTED:
2230 break;
[email protected]695b5712012-12-06 23:55:282231
[email protected]2d19eb6e2014-01-27 17:30:002232 case extensions::BLACKLISTED_MALWARE:
2233 blocked.insert(it->first);
2234 break;
2235
2236 case extensions::BLACKLISTED_SECURITY_VULNERABILITY:
2237 case extensions::BLACKLISTED_CWS_POLICY_VIOLATION:
2238 case extensions::BLACKLISTED_POTENTIALLY_UNWANTED:
2239 greylist.insert(it->first);
2240 break;
2241
2242 case extensions::BLACKLISTED_UNKNOWN:
2243 unchanged.insert(it->first);
2244 break;
2245 }
2246 }
2247
2248 UpdateBlockedExtensions(blocked, unchanged);
2249 UpdateGreylistedExtensions(greylist, unchanged, state_map);
2250
[email protected]373daf972014-04-10 01:50:442251 error_controller_->ShowErrorIfNeeded();
[email protected]2d19eb6e2014-01-27 17:30:002252}
2253
2254namespace {
2255void Partition(const ExtensionIdSet& before,
2256 const ExtensionIdSet& after,
2257 const ExtensionIdSet& unchanged,
2258 ExtensionIdSet* no_longer,
2259 ExtensionIdSet* not_yet) {
2260 *not_yet = base::STLSetDifference<ExtensionIdSet>(after, before);
2261 *no_longer = base::STLSetDifference<ExtensionIdSet>(before, after);
2262 *no_longer = base::STLSetDifference<ExtensionIdSet>(*no_longer, unchanged);
2263}
2264} // namespace
2265
2266void ExtensionService::UpdateBlockedExtensions(
2267 const ExtensionIdSet& blocked,
2268 const ExtensionIdSet& unchanged) {
2269 ExtensionIdSet not_yet_blocked, no_longer_blocked;
2270 Partition(registry_->blacklisted_extensions().GetIDs(),
2271 blocked, unchanged,
2272 &no_longer_blocked, &not_yet_blocked);
2273
2274 for (ExtensionIdSet::iterator it = no_longer_blocked.begin();
2275 it != no_longer_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282276 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142277 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252278 if (!extension.get()) {
[email protected]2d19eb6e2014-01-27 17:30:002279 NOTREACHED() << "Extension " << *it << " no longer blocked, "
2280 << "but it was never blocked.";
[email protected]695b5712012-12-06 23:55:282281 continue;
[email protected]3f2a2fa2013-09-24 02:55:252282 }
[email protected]bb1bc9b32013-12-21 03:09:142283 registry_->RemoveBlacklisted(*it);
[email protected]3f2a2fa2013-09-24 02:55:252284 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092285 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092286 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092287 extension->location(),
2288 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282289 }
2290
[email protected]2d19eb6e2014-01-27 17:30:002291 for (ExtensionIdSet::iterator it = not_yet_blocked.begin();
2292 it != not_yet_blocked.end(); ++it) {
[email protected]695b5712012-12-06 23:55:282293 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252294 if (!extension.get()) {
2295 NOTREACHED() << "Extension " << *it << " needs to be "
2296 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282297 continue;
[email protected]3f2a2fa2013-09-24 02:55:252298 }
[email protected]bb1bc9b32013-12-21 03:09:142299 registry_->AddBlacklisted(extension);
[email protected]2d19eb6e2014-01-27 17:30:002300 extension_prefs_->SetExtensionBlacklistState(
2301 extension->id(), extensions::BLACKLISTED_MALWARE);
[email protected]b0af4792013-10-23 09:12:132302 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092303 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2304 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282305 }
[email protected]2d19eb6e2014-01-27 17:30:002306}
[email protected]695b5712012-12-06 23:55:282307
[email protected]2d19eb6e2014-01-27 17:30:002308// TODO(oleg): UMA logging
2309void ExtensionService::UpdateGreylistedExtensions(
2310 const ExtensionIdSet& greylist,
2311 const ExtensionIdSet& unchanged,
2312 const extensions::Blacklist::BlacklistStateMap& state_map) {
2313 ExtensionIdSet not_yet_greylisted, no_longer_greylisted;
2314 Partition(greylist_.GetIDs(),
2315 greylist, unchanged,
2316 &no_longer_greylisted, &not_yet_greylisted);
2317
2318 for (ExtensionIdSet::iterator it = no_longer_greylisted.begin();
2319 it != no_longer_greylisted.end(); ++it) {
2320 scoped_refptr<const Extension> extension = greylist_.GetByID(*it);
2321 if (!extension.get()) {
2322 NOTREACHED() << "Extension " << *it << " no longer greylisted, "
2323 << "but it was not marked as greylisted.";
2324 continue;
2325 }
2326
2327 greylist_.Remove(*it);
2328 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2329 extensions::NOT_BLACKLISTED);
2330 if (extension_prefs_->GetDisableReasons(extension->id()) &
2331 extensions::Extension::DISABLE_GREYLIST)
2332 EnableExtension(*it);
2333 }
2334
2335 for (ExtensionIdSet::iterator it = not_yet_greylisted.begin();
2336 it != not_yet_greylisted.end(); ++it) {
2337 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
2338 if (!extension.get()) {
2339 NOTREACHED() << "Extension " << *it << " needs to be "
2340 << "disabled, but it's not installed.";
2341 continue;
2342 }
2343 greylist_.Insert(extension);
2344 extension_prefs_->SetExtensionBlacklistState(extension->id(),
2345 state_map.find(*it)->second);
2346 if (registry_->enabled_extensions().Contains(extension->id()))
2347 DisableExtension(*it, extensions::Extension::DISABLE_GREYLIST);
2348 }
[email protected]fdd679b2012-11-15 20:49:392349}
[email protected]75bdcb872013-03-13 00:41:452350
2351void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2352 update_observers_.AddObserver(observer);
2353}
2354
2355void ExtensionService::RemoveUpdateObserver(
2356 extensions::UpdateObserver* observer) {
2357 update_observers_.RemoveObserver(observer);
2358}
[email protected]bb1bc9b32013-12-21 03:09:142359
2360// Used only by test code.
2361void ExtensionService::UnloadAllExtensionsInternal() {
2362 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2363
2364 registry_->ClearAll();
[email protected]45f5b7d2014-01-22 23:47:132365 system_->runtime_data()->ClearAll();
[email protected]bb1bc9b32013-12-21 03:09:142366
2367 // TODO(erikkay) should there be a notification for this? We can't use
2368 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2369 // or uninstalled.
2370}
[email protected]ebe07772014-05-22 04:16:062371
2372void ExtensionService::OnProfileDestructionStarted() {
2373 ExtensionIdSet ids_to_unload = registry_->enabled_extensions().GetIDs();
2374 for (ExtensionIdSet::iterator it = ids_to_unload.begin();
2375 it != ids_to_unload.end();
2376 ++it) {
2377 UnloadExtension(*it, UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN);
2378 }
2379}