blob: 59466bc208c6ac879d6e94717075776e634317a4 [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]24b538a2010-02-27 01:22:4411#include "base/basictypes.h"
[email protected]05aad2da2011-10-28 10:12:3712#include "base/bind.h"
[email protected]e5af875f2011-10-10 21:09:1413#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1214#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2515#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3616#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:5718#include "base/prefs/pref_service.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]3ea1b182013-02-08 22:38:4120#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1721#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1823#include "base/strings/utf_string_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1424#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread_restrictions.h"
[email protected]41a17c52013-06-28 00:27:5326#include "base/time/time.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]211a3f32013-05-28 21:48:1829#include "chrome/browser/app_mode/app_mode_utils.h"
[email protected]15730c42009-09-03 00:03:2030#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3631#include "chrome/browser/chrome_notification_types.h"
[email protected]32b36c62012-10-24 05:37:4132#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1233#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4434#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]c39df1a2013-01-08 18:22:0735#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3136#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]4636c832013-01-11 02:10:1137#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0138#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1739#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3840#include "chrome/browser/extensions/data_deleter.h"
[email protected]ebc8d932013-09-11 23:47:2941#include "chrome/browser/extensions/event_router.h"
[email protected]62f051c2012-03-29 17:04:4442#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2443#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1844#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5645#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2246#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2947#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2348#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4349#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5250#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]31d8f5f22012-04-02 15:22:0851#include "chrome/browser/extensions/extension_system.h"
[email protected]a7ff4b72013-10-17 20:56:0252#include "chrome/browser/extensions/extension_util.h"
[email protected]612a1cb12012-10-17 13:18:0353#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2754#include "chrome/browser/extensions/external_provider_impl.h"
55#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0156#include "chrome/browser/extensions/installed_loader.h"
[email protected]fdd679b2012-11-15 20:49:3957#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3758#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3959#include "chrome/browser/extensions/permissions_updater.h"
[email protected]d8c8f25f2011-11-02 18:18:0160#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]75bdcb872013-03-13 00:41:4561#include "chrome/browser/extensions/update_observer.h"
[email protected]42a08162012-03-16 18:09:1162#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3363#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3964#include "chrome/browser/profiles/profile_manager.h"
[email protected]c8d407e2011-04-28 21:27:1765#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3166#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1567#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5468#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0669#include "chrome/common/chrome_version_info.h"
[email protected]f6431be82013-09-07 02:53:4570#include "chrome/common/crash_keys.h"
[email protected]9367eabc2013-03-01 01:29:2971#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:5872#include "chrome/common/extensions/extension.h"
[email protected]9f4e4f082013-06-21 07:11:1973#include "chrome/common/extensions/extension_constants.h"
[email protected]7c927b62010-02-24 09:54:1374#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5675#include "chrome/common/extensions/extension_messages.h"
[email protected]612a1cb12012-10-17 13:18:0376#include "chrome/common/extensions/feature_switch.h"
[email protected]15d4d2d2013-08-09 06:49:0377#include "chrome/common/extensions/features/feature_channel.h"
[email protected]06492ed2013-03-24 22:13:1478#include "chrome/common/extensions/incognito_handler.h"
[email protected]561e33d52013-04-03 06:58:4379#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]6b414c232013-06-05 07:53:3480#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]9f4e4f082013-06-21 07:11:1981#include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
[email protected]65348062013-01-15 07:27:2282#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]2cb5e302013-05-09 05:00:0683#include "chrome/common/extensions/permissions/permissions_data.h"
[email protected]25b34332009-06-05 21:53:1984#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1485#include "chrome/common/url_constants.h"
[email protected]7a83dd072013-09-03 12:09:1786#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]c38831a12011-10-28 12:44:4987#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3688#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3789#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1690#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3091#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4292#include "content/public/browser/site_instance.h"
93#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:5394#include "content/public/browser/url_data_source.h"
[email protected]836e2982013-05-16 08:07:4295#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:3196#include "extensions/common/error_utils.h"
[email protected]c81f5d32013-10-26 10:38:4297#include "extensions/common/extensions_client.h"
[email protected]d42c11152013-08-22 19:36:3298#include "extensions/common/manifest.h"
[email protected]0c3c9732013-09-16 08:53:4199#include "extensions/common/manifest_constants.h"
[email protected]c41003472013-10-19 15:37:25100#include "extensions/common/permissions/permission_message_provider.h"
[email protected]65187152012-06-02 13:14:14101#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25102#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]cd67ed52013-10-15 01:22:13103#include "ui/base/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:00104#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38105#include "webkit/browser/database/database_tracker.h"
106#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18107
[email protected]eed367e2011-04-12 03:43:31108#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18109#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32110#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07111#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31112#endif
113
[email protected]55eb70e762012-02-20 17:38:39114using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39115using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36116using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27117using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34118using extensions::Extension;
119using extensions::ExtensionIdSet;
120using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42121using extensions::FeatureSwitch;
[email protected]0d54b682013-11-05 14:15:36122using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40123using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06124using extensions::PermissionMessage;
125using extensions::PermissionMessages;
126using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19127using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24128using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05129
[email protected]0c3c9732013-09-16 08:53:41130namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06131
[email protected]b6ab96d2009-08-20 18:58:19132namespace {
133
[email protected]d96eb512012-11-01 23:44:08134// Histogram values for logging events related to externally installed
135// extensions.
136enum ExternalExtensionEvent {
137 EXTERNAL_EXTENSION_INSTALLED = 0,
138 EXTERNAL_EXTENSION_IGNORED,
139 EXTERNAL_EXTENSION_REENABLED,
140 EXTERNAL_EXTENSION_UNINSTALLED,
141 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
142};
143
[email protected]612a1cb12012-10-17 13:18:03144// Prompt the user this many times before considering an extension acknowledged.
145static const int kMaxExtensionAcknowledgePromptCount = 3;
146
[email protected]0db124b02012-11-07 04:55:05147// Wait this many seconds after an extensions becomes idle before updating it.
148static const int kUpdateIdleDelay = 5;
149
[email protected]9bd9a6862012-11-29 09:24:22150// Wait this many seconds before trying to garbage collect extensions again.
151static const int kGarbageCollectRetryDelay = 30;
152
[email protected]36fc54f2013-06-14 02:49:34153// Wait this many seconds after startup to see if there are any extensions
154// which can be garbage collected.
155static const int kGarbageCollectStartupDelay = 30;
156
[email protected]9f4e4f082013-06-21 07:11:19157static bool IsSharedModule(const Extension* extension) {
158 return SharedModuleInfo::IsSharedModule(extension);
159}
160
[email protected]4c9201c42013-08-16 04:56:21161static bool IsCWSSharedModule(const Extension* extension) {
162 return extension->from_webstore() && IsSharedModule(extension);
163}
164
165class SharedModuleProvider : public extensions::ManagementPolicy::Provider {
166 public:
167 SharedModuleProvider() {}
168 virtual ~SharedModuleProvider() {}
169
170 virtual std::string GetDebugPolicyProviderName() const OVERRIDE {
171 return "SharedModuleProvider";
172 }
173
174 virtual bool UserMayModifySettings(const Extension* extension,
175 string16* error) const OVERRIDE {
176 return !IsCWSSharedModule(extension);
177 }
178
179 virtual bool MustRemainEnabled(const Extension* extension,
180 string16* error) const OVERRIDE {
181 return IsCWSSharedModule(extension);
182 }
183
184 private:
185 DISALLOW_COPY_AND_ASSIGN(SharedModuleProvider);
186};
187
188
[email protected]c6d474f82009-12-16 21:11:06189} // namespace
[email protected]b6ab96d2009-08-20 18:58:19190
[email protected]eaa7dd182010-12-14 11:09:00191ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49192 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57193 being_upgraded(false),
194 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49195}
196
[email protected]eaa7dd182010-12-14 11:09:00197ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49198}
199
[email protected]eaa7dd182010-12-14 11:09:00200// ExtensionService.
[email protected]6014d672008-12-05 00:38:25201
[email protected]8e4560b62011-01-14 10:09:14202void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12203 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20204
[email protected]7a4c6852010-09-16 03:44:22205 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27206 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21207 for (i = external_extension_providers_.begin();
208 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14209 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21210 if (i->get()->HasExtension(id))
211 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22212 }
213
[email protected]0f48fca2011-05-19 18:46:35214 // We get the list of external extensions to check from preferences.
215 // It is possible that an extension has preferences but is not loaded.
216 // For example, an extension that requires experimental permissions
217 // will not be loaded if the experimental command line flag is not used.
218 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40219 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35220 // We can't call UninstallExtension with an unloaded/invalid
221 // extension ID.
222 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
223 << "with id: " << id;
224 return;
225 }
[email protected]d6ebc9792011-04-07 18:18:33226 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22227}
228
[email protected]7f8f24f2012-11-15 19:40:14229void ExtensionService::SetFileTaskRunnerForTesting(
230 base::SequencedTaskRunner* task_runner) {
231 file_task_runner_ = task_runner;
232}
233
[email protected]8e4560b62011-01-14 10:09:14234void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22235 external_extension_providers_.clear();
236}
237
[email protected]8e4560b62011-01-14 10:09:14238void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27239 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12240 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21241 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27242 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22243}
244
[email protected]9060d8b02012-01-13 02:14:30245bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22246 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42247 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48248 Manifest::Location location,
249 int creation_flags,
250 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38251 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
252 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20253
[email protected]8a87a5332011-08-11 17:54:59254 const Extension* extension = GetExtensionById(id, true);
255 if (extension) {
256 // Already installed. Skip this install if the current location has
257 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40258 Manifest::Location current = extension->location();
259 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30260 return false;
[email protected]8a87a5332011-08-11 17:54:59261 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22262 }
[email protected]9060d8b02012-01-13 02:14:30263
264 // Add |id| to the set of pending extensions. If it can not be added,
265 // then there is already a pending record from a higher-priority install
266 // source. In this case, signal that this extension will not be
267 // installed by returning false.
268 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]464213a2013-10-15 01:06:48269 id, update_url, location, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30270 return false;
[email protected]31bb5ee62012-09-12 22:58:40271 }
[email protected]9060d8b02012-01-13 02:14:30272
[email protected]94fde232012-04-27 10:22:30273 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30274 return true;
[email protected]7a4c6852010-09-16 03:44:22275}
276
[email protected]dc9a74f72012-08-17 18:07:21277const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]be9915fb2013-07-18 09:28:55278 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]dc9a74f72012-08-17 18:07:21279 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09280}
281
[email protected]dc9a74f72012-08-17 18:07:21282bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09283 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27284}
285
[email protected]6aeac8342010-10-01 20:21:18286// static
[email protected]d6ebc9792011-04-07 18:18:33287// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30288// --uninstall-extension, and to uninstall an extension via sync. The LOG
289// statements within this function are used to inform the user if the uninstall
290// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00291bool ExtensionService::UninstallExtensionHelper(
292 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18293 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33294 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40295 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18296 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33297 << "id: " << extension_id;
298 return false;
[email protected]6aeac8342010-10-01 20:21:18299 }
300
[email protected]d6ebc9792011-04-07 18:18:33301 // The following call to UninstallExtension will not allow an uninstall of a
302 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14303 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33304 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
305 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
306 << ": " << error;
307 return false;
308 }
[email protected]95da88c42011-03-31 10:07:33309
[email protected]6aeac8342010-10-01 20:21:18310 return true;
311}
312
[email protected]eaa7dd182010-12-14 11:09:00313ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24314 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45315 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23316 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39317 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03318 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35319 bool extensions_enabled,
320 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39321 : extensions::Blacklist::Observer(blacklist),
322 profile_(profile),
[email protected]bd306722012-07-11 20:43:59323 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43324 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28325 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38326 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]f8aefb132013-10-30 09:29:52327 extension_sync_service_(NULL),
[email protected]9c009092013-05-01 03:14:09328 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23329 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03330 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13331 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33332 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35333 ready_(ready),
[email protected]9c009092013-05-01 03:14:09334 toolbar_model_(this),
[email protected]8b280302011-10-13 22:22:23335 menu_manager_(profile),
[email protected]dff1e042012-04-27 10:59:18336 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54337 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19338 installs_delayed_for_gc_(false),
[email protected]f8aefb132013-10-30 09:29:52339 is_first_run_(false) {
[email protected]c04fd3dd2013-09-05 08:20:10340#if defined(OS_CHROMEOS)
341 disable_garbage_collection_ = false;
342#endif
[email protected]a29a517a2011-01-21 21:11:12343 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20344
[email protected]36a784c2009-06-23 06:21:08345 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40346 if (command_line->HasSwitch(switches::kDisableExtensions) ||
347 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23348 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18349 }
[email protected]36a784c2009-06-23 06:21:08350
[email protected]3c4abc82012-10-22 22:25:54351 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
352 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27353 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07354 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27355 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07356 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27357 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07358 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08359 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
360 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28361 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03362 base::Closure callback =
363 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
364 base::Unretained(this));
365 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
366 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24367 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29368
[email protected]93fd78f42009-07-10 16:43:17369 // Set up the ExtensionUpdater
370 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02371 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17372 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25373 base::StringToInt(command_line->GetSwitchValueASCII(
374 switches::kExtensionsUpdateFrequency),
375 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17376 }
[email protected]42a08162012-03-16 18:09:11377 updater_.reset(new extensions::ExtensionUpdater(this,
378 extension_prefs,
379 profile->GetPrefs(),
380 profile,
381 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17382 }
383
[email protected]25ae0152011-11-18 14:40:02384 component_loader_.reset(
385 new extensions::ComponentLoader(this,
386 profile->GetPrefs(),
387 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14388
[email protected]0436b102011-04-15 18:30:03389 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25390 extensions::ExternalProviderImpl::CreateExternalProviders(
391 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05392 }
[email protected]b671760b2010-07-15 21:13:47393
[email protected]fb82dcd2012-03-21 14:15:46394 // Set this as the ExtensionService for extension sorting to ensure it
395 // cause syncs if required.
[email protected]b3aa7182013-04-25 04:45:23396 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
397
[email protected]9ce11d22012-08-08 23:20:13398#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58399 extension_action_storage_manager_.reset(
400 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13401#endif
[email protected]c77f2352012-08-08 22:07:58402
[email protected]4c9201c42013-08-16 04:56:21403 shared_module_policy_provider_.reset(new SharedModuleProvider);
404
[email protected]cb0e50312011-05-09 15:03:07405 // How long is the path to the Extensions directory?
406 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
407 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25408}
409
[email protected]84df8332011-12-06 18:22:46410const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45411 return &extensions_;
412}
413
[email protected]84df8332011-12-06 18:22:46414const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45415 return &disabled_extensions_;
416}
417
[email protected]84df8332011-12-06 18:22:46418const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20419 return &terminated_extensions_;
420}
421
[email protected]695b5712012-12-06 23:55:28422const ExtensionSet* ExtensionService::blacklisted_extensions() const {
423 return &blacklisted_extensions_;
424}
425
[email protected]9e9c1d12013-07-31 01:58:12426const ExtensionSet* ExtensionService::delayed_installs() const {
427 return &delayed_installs_;
428}
429
[email protected]695b5712012-12-06 23:55:28430scoped_ptr<const ExtensionSet>
431 ExtensionService::GenerateInstalledExtensionsSet() const {
432 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01433 installed_extensions->InsertAll(extensions_);
434 installed_extensions->InsertAll(disabled_extensions_);
435 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28436 installed_extensions->InsertAll(blacklisted_extensions_);
437 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01438}
439
[email protected]3f213ad2012-07-26 23:39:41440extensions::PendingExtensionManager*
441 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37442 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45443}
444
[email protected]eaa7dd182010-12-14 11:09:00445ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17446 // No need to unload extensions here because they are profile-scoped, and the
447 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17448
[email protected]5df038b2012-07-16 19:03:27449 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14450 for (i = external_extension_providers_.begin();
451 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27452 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14453 provider->ServiceShutdown();
454 }
[email protected]6014d672008-12-05 00:38:25455}
456
[email protected]037228a2012-10-05 01:36:16457void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21458 system_->management_policy()->UnregisterProvider(
459 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16460}
461
[email protected]b2907fd2011-03-25 16:43:37462const Extension* ExtensionService::GetExtensionById(
463 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47464 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28465 if (include_disabled) {
466 // Include blacklisted extensions here because there are hundreds of
467 // callers of this function, and many might assume that this includes those
468 // that have been disabled due to blacklisting.
469 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
470 }
471 return GetExtensionById(id, include_mask);
472}
473
[email protected]399583b2012-12-11 09:33:42474GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
475 return content::SiteInstance::GetSiteForURL(
476 profile_,
477 Extension::GetBaseURLFromExtensionId(extension_id));
478}
479
[email protected]695b5712012-12-06 23:55:28480const Extension* ExtensionService::GetExtensionById(
481 const std::string& id, int include_mask) const {
482 std::string lowercase_id = StringToLowerASCII(id);
483 if (include_mask & INCLUDE_ENABLED) {
484 const Extension* extension = extensions_.GetByID(lowercase_id);
485 if (extension)
486 return extension;
487 }
488 if (include_mask & INCLUDE_DISABLED) {
489 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
490 if (extension)
491 return extension;
492 }
493 if (include_mask & INCLUDE_TERMINATED) {
494 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
495 if (extension)
496 return extension;
497 }
498 if (include_mask & INCLUDE_BLACKLISTED) {
499 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
500 if (extension)
501 return extension;
502 }
503 return NULL;
[email protected]78994ab02010-12-08 18:06:44504}
505
[email protected]eaa7dd182010-12-14 11:09:00506void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12507 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20508
[email protected]4a10006a2013-05-17 23:18:35509 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17510 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32511
[email protected]820d9bd2013-04-03 03:46:03512 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03513 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
514 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
515 // The sole purpose of this launch is to install a new extension from CWS
516 // and immediately terminate: loading already installed extensions is
517 // unnecessary and may interfere with the inline install dialog (e.g. if an
518 // extension listens to onStartup and opens a window).
519 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25520 } else {
[email protected]47b60072013-06-20 10:35:56521 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
522 component_loader_->LoadAll();
523 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19524
[email protected]59ee99d2013-10-11 15:46:16525 ReconcileKnownDisabled();
526
[email protected]2edd57852013-08-14 22:07:40527 // Attempt to re-enable extensions whose only disable reason is reloading.
528 std::vector<std::string> extensions_to_enable;
529 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
530 iter != disabled_extensions_.end(); ++iter) {
531 const Extension* e = iter->get();
532 if (extension_prefs_->GetDisableReasons(e->id()) ==
533 Extension::DISABLE_RELOAD) {
534 extensions_to_enable.push_back(e->id());
535 }
536 }
537 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
538 it != extensions_to_enable.end(); ++it) {
539 EnableExtension(*it);
540 }
541
[email protected]9f4e4f082013-06-21 07:11:19542 // Finish install (if possible) of extensions that were still delayed while
543 // the browser was shut down.
544 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
545 extension_prefs_->GetAllDelayedInstallInfo());
546 for (size_t i = 0; i < delayed_info->size(); ++i) {
547 ExtensionInfo* info = delayed_info->at(i).get();
548 scoped_refptr<const Extension> extension(NULL);
549 if (info->extension_manifest) {
550 std::string error;
551 extension = Extension::Create(
552 info->extension_path,
553 info->extension_location,
554 *info->extension_manifest,
555 extension_prefs_->GetDelayedInstallCreationFlags(
556 info->extension_id),
557 info->extension_id,
558 &error);
559 if (extension.get())
560 delayed_installs_.Insert(extension);
561 }
562 }
563 MaybeFinishDelayedInstallations();
564
565 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
566 extension_prefs_->GetAllDelayedInstallInfo());
567 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
568 delayed_info2->size() - delayed_info->size());
569
[email protected]47b60072013-06-20 10:35:56570 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25571
[email protected]47b60072013-06-20 10:35:56572 // TODO(erikkay) this should probably be deferred to a future point
573 // rather than running immediately at startup.
574 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57575
[email protected]47b60072013-06-20 10:35:56576 base::MessageLoop::current()->PostDelayedTask(
577 FROM_HERE,
578 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
579 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42580
[email protected]820d9bd2013-04-03 03:46:03581 if (extension_prefs_->NeedsStorageGarbageCollection()) {
582 GarbageCollectIsolatedStorage();
583 extension_prefs_->SetNeedsStorageGarbageCollection(false);
584 }
[email protected]4c9201c42013-08-16 04:56:21585 system_->management_policy()->RegisterProvider(
586 shared_module_policy_provider_.get());
[email protected]399583b2012-12-11 09:33:42587 }
[email protected]6014d672008-12-05 00:38:25588}
589
[email protected]31bb5ee62012-09-12 22:58:40590bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45591 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40592 const GURL& download_url,
593 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12594 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54595 if (browser_terminating_) {
596 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
597 // Leak the temp file at extension_path. We don't want to add to the disk
598 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
599 // the file is in the OS temp directory which should be cleaned up for us.
600 return false;
601 }
[email protected]a8af9fdb2010-10-28 21:52:20602
[email protected]3f213ad2012-07-26 23:39:41603 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06604 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32605
[email protected]695b5712012-12-06 23:55:28606 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06607 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33608 LOG(WARNING) << "Will not update extension " << id
609 << " because it is not installed or pending";
610 // Delete extension_path since we're not creating a CrxInstaller
611 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14612 if (!GetFileTaskRunner()->PostTask(
613 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36614 base::Bind(
[email protected]7296f2762011-11-21 19:23:44615 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36616 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03617
618 return false;
[email protected]e957fe52009-06-23 16:51:05619 }
620
[email protected]aa142702010-03-26 01:26:33621 // We want a silent install only for non-pending extensions and
622 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37623 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05624 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37625 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33626
[email protected]f8636f92013-08-09 21:02:37627 scoped_refptr<CrxInstaller> installer(
628 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16629 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48630 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06631 if (pending_extension_info) {
632 installer->set_install_source(pending_extension_info->install_source());
633 if (pending_extension_info->install_silently())
634 installer->set_allow_silent_install(true);
[email protected]464213a2013-10-15 01:06:48635 creation_flags = pending_extension_info->creation_flags();
636 if (pending_extension_info->mark_acknowledged())
637 AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06638 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41639 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06640 }
[email protected]7d8b7072012-04-07 01:07:46641 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45642 // its auto-update URL is from the webstore, treat it as a webstore install.
643 // Note that we ignore some older extensions with blank auto-update URLs
644 // because we are mostly concerned with restrictions on NaCl extensions,
645 // which are newer.
[email protected]75764512011-12-19 19:54:28646 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38647 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35648 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06649 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53650 creation_flags |= Extension::FROM_WEBSTORE;
651 }
652
653 // Bookmark apps being updated is kind of a contradiction, but that's because
654 // we mark the default apps as bookmark apps, and they're hosted in the web
655 // store, thus they can get updated. See http://crbug.com/101605 for more
656 // details.
657 if (extension && extension->from_bookmark())
658 creation_flags |= Extension::FROM_BOOKMARK;
659
[email protected]e33bbc22012-08-27 22:05:46660 if (extension && extension->was_installed_by_default())
661 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
662
[email protected]a12ce8b22012-01-17 18:40:53663 installer->set_creation_flags(creation_flags);
664
[email protected]6dfbbf82010-03-12 23:09:16665 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06666 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07667 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16668 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03669
670 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09671 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03672
673 return true;
[email protected]e957fe52009-06-23 16:51:05674}
675
[email protected]e178ad92013-06-28 02:29:25676void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12677 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05678
679 // If the extension is already reloading, don't reload again.
680 if (extension_prefs_->GetDisableReasons(extension_id) &
681 Extension::DISABLE_RELOAD) {
682 return;
683 }
684
[email protected]650b2d52013-02-10 03:41:45685 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02686 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40687
[email protected]f17dbd42010-08-16 23:21:10688 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06689 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29690 // If the extension has an inspector open for its background page, detach
691 // the inspector and hang onto a cookie for it, so that we can reattach
692 // later.
[email protected]31d8f5f22012-04-02 15:22:08693 // TODO(yoz): this is not incognito-safe!
694 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13695 extensions::ExtensionHost* host =
696 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36697 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29698 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09699 scoped_refptr<DevToolsAgentHost> agent_host =
700 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
701 agent_host->DisconnectRenderViewHost();
702 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29703 }
704
[email protected]b65272f2009-08-31 15:47:06705 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50706 // BeingUpgraded is set back to false when the extension is added.
707 SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03708 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03709 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16710 } else {
711 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06712 }
713
[email protected]9f4e4f082013-06-21 07:11:19714 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48715 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05716 return;
717 }
718
[email protected]43ceb002012-02-10 23:19:15719 // If we're reloading a component extension, use the component extension
720 // loader's reloader.
721 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25722 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15723 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25724 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15725 return;
726 }
727
[email protected]e6090e42010-03-23 22:44:08728 // Check the installed extensions to see if what we're reloading was already
729 // installed.
[email protected]e178ad92013-06-28 02:29:25730 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08731 scoped_ptr<ExtensionInfo> installed_extension(
732 extension_prefs_->GetInstalledExtensionInfo(extension_id));
733 if (installed_extension.get() &&
734 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01735 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08736 } else {
[email protected]d8c8f25f2011-11-02 18:18:01737 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08738 // We should always be able to remember the extension's path. If it's not in
739 // the map, someone failed to update |unloaded_extension_paths_|.
740 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01741 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08742 }
[email protected]e178ad92013-06-28 02:29:25743 // When reloading is done, mark this extension as done reloading.
744 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40745}
746
[email protected]fa2416f2011-05-03 08:41:20747bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08748 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20749 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14750 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12751 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20752
[email protected]0d6ec3a72011-09-02 02:09:43753 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25754
[email protected]e7afe2452010-08-22 16:19:13755 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09756 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32757
[email protected]95da88c42011-03-31 10:07:33758 // Policy change which triggers an uninstall will always set
759 // |external_uninstall| to true so this is the only way to uninstall
760 // managed extensions.
[email protected]65187152012-06-02 13:14:14761 if (!external_uninstall &&
762 !system_->management_policy()->UserMayModifySettings(
763 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07764 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27765 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53766 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09767 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33768 return false;
769 }
[email protected]95da88c42011-03-31 10:07:33770
[email protected]65f173552012-06-28 22:43:58771 syncer::SyncChange sync_change;
[email protected]f8aefb132013-10-30 09:29:52772 if (extension_sync_service_) {
773 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
774 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30775 }
776
[email protected]dc24976f2013-06-02 21:15:09777 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08778 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
779 EXTERNAL_EXTENSION_UNINSTALLED,
780 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02781 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
782 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
783 EXTERNAL_EXTENSION_UNINSTALLED,
784 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
785 } else {
786 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
787 EXTERNAL_EXTENSION_UNINSTALLED,
788 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
789 }
[email protected]d96eb512012-11-01 23:44:08790 }
[email protected]9b217652010-10-08 22:04:23791 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08792 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09793 RecordPermissionMessagesHistogram(extension.get(),
794 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23795
[email protected]831aa212010-03-26 13:55:19796 // Unload before doing more cleanup to ensure that nothing is hanging on to
797 // any of these resources.
[email protected]b0af4792013-10-23 09:12:13798 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19799
[email protected]9f1087e2009-06-15 17:29:32800 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05801 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14802 if (!GetFileTaskRunner()->PostTask(
803 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36804 base::Bind(
[email protected]14908b72011-04-20 06:54:36805 &extension_file_util::UninstallExtension,
806 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20807 extension_id)))
[email protected]14908b72011-04-20 06:54:36808 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32809 }
810
[email protected]6b414c232013-06-05 07:53:34811 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36812 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43813 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09814 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43815
[email protected]399583b2012-12-11 09:33:42816 if (is_storage_isolated) {
817 BrowserContext::AsyncObliterateStoragePartition(
818 profile_,
819 GetSiteForExtensionId(extension_id),
820 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
821 AsWeakPtr()));
822 } else {
823 if (extension->is_hosted_app() &&
824 !profile_->GetExtensionSpecialStoragePolicy()->
825 IsStorageProtected(launch_web_url_origin)) {
826 extensions::DataDeleter::StartDeleting(
827 profile_, extension_id, launch_web_url_origin);
828 }
829 extensions::DataDeleter::StartDeleting(profile_, extension_id,
830 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43831 }
[email protected]0d6ec3a72011-09-02 02:09:43832
[email protected]0dfe05c2011-02-23 23:03:36833 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06834
835 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07836 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27837 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53838 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09839 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33840
[email protected]f8aefb132013-10-30 09:29:52841 if (extension_sync_service_) {
842 extension_sync_service_->ProcessSyncUninstallExtension(extension_id,
843 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30844 }
845
[email protected]399583b2012-12-11 09:33:42846 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05847
[email protected]198b5902013-06-27 10:36:11848 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19849
[email protected]d4eda592013-09-18 03:37:57850 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
851 external_uninstall);
852
[email protected]333b1de2011-09-12 18:28:50853 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18854 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
855
[email protected]d6ebc9792011-04-07 18:18:33856 return true;
[email protected]c10da4b02010-03-25 14:38:32857}
858
[email protected]c3cfb012011-04-06 22:07:35859bool ExtensionService::IsExtensionEnabled(
860 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46861 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21862 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18863 return true;
[email protected]dc9a74f72012-08-17 18:07:21864 }
[email protected]36429da2011-07-11 20:25:18865
[email protected]695b5712012-12-06 23:55:28866 if (disabled_extensions_.Contains(extension_id) ||
867 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25868 return false;
[email protected]695b5712012-12-06 23:55:28869 }
[email protected]ad83ca242011-07-29 01:32:25870
871 // If the extension hasn't been loaded yet, check the prefs for it. Assume
872 // enabled unless otherwise noted.
873 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31874 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35875}
876
877bool ExtensionService::IsExternalExtensionUninstalled(
878 const std::string& extension_id) const {
879 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
880}
881
[email protected]e2377032013-01-18 17:53:22882bool ExtensionService::IsExtensionEnabledForLauncher(
883 const std::string& extension_id) const {
884 return IsExtensionEnabled(extension_id) &&
885 !GetTerminatedExtension(extension_id);
886}
887
[email protected]eaa7dd182010-12-14 11:09:00888void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12889 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20890
[email protected]06f92562011-04-29 19:27:31891 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39892 return;
[email protected]0d54b682013-11-05 14:15:36893 const Extension* extension = disabled_extensions_.GetByID(extension_id);
894
895 ManagementPolicy* policy = system_->management_policy();
896 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
897 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
898 return;
899 }
[email protected]0c6da502009-08-14 22:32:39900
[email protected]b3317ad2011-04-28 23:46:00901 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28902 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52903
[email protected]06f92562011-04-29 19:27:31904 // This can happen if sync enables an extension that is not
905 // installed yet.
906 if (!extension)
907 return;
908
[email protected]d96eb512012-11-01 23:44:08909 if (IsUnacknowledgedExternalExtension(extension)) {
910 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
911 EXTERNAL_EXTENSION_REENABLED,
912 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02913 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
914 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
915 EXTERNAL_EXTENSION_REENABLED,
916 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
917 } else {
918 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
919 EXTERNAL_EXTENSION_REENABLED,
920 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
921 }
[email protected]612a1cb12012-10-17 13:18:03922 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08923 }
[email protected]612a1cb12012-10-17 13:18:03924
[email protected]0c6da502009-08-14 22:32:39925 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46926 extensions_.Insert(make_scoped_refptr(extension));
[email protected]195c1582013-10-29 18:42:23927 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39928
[email protected]62d30f42009-10-01 22:36:06929 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30930
[email protected]f74d7f32012-06-19 19:22:51931 // Notify listeners that the extension was enabled.
932 content::NotificationService::current()->Notify(
933 chrome::NOTIFICATION_EXTENSION_ENABLED,
934 content::Source<Profile>(profile_),
935 content::Details<const Extension>(extension));
936
[email protected]f8aefb132013-10-30 09:29:52937 if (extension_sync_service_)
938 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:39939}
940
[email protected]44d62b62012-04-11 00:06:03941void ExtensionService::DisableExtension(
942 const std::string& extension_id,
943 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12944 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20945
[email protected]b2ba9962009-12-10 20:10:15946 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31947 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52948 return;
[email protected]1784e83a2009-09-08 21:01:52949
[email protected]06f92562011-04-29 19:27:31950 const Extension* extension = GetInstalledExtension(extension_id);
951 // |extension| can be NULL if sync disables an extension that is not
952 // installed yet.
[email protected]65187152012-06-02 13:14:14953 if (extension &&
[email protected]505ccdc2013-06-20 20:15:35954 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:14955 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33956 return;
[email protected]65187152012-06-02 13:14:14957 }
[email protected]95da88c42011-03-31 10:07:33958
[email protected]b3317ad2011-04-28 23:46:00959 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28960 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52961
[email protected]695b5712012-12-06 23:55:28962 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
963 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31964 if (!extension)
965 return;
966
[email protected]64742de32013-04-22 08:44:34967 // Reset the background_page_ready flag
968 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
969 extension_runtime_data_[extension->id()].background_page_ready = false;
970
[email protected]5c094792012-09-07 19:44:53971 // Move it over to the disabled list. Don't send a second unload notification
972 // for terminated extensions being disabled.
[email protected]195c1582013-10-29 18:42:23973 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53974 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46975 extensions_.Remove(extension->id());
[email protected]b0af4792013-10-23 09:12:13976 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:53977 } else {
[email protected]84df8332011-12-06 18:22:46978 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53979 }
[email protected]3bdba0d2011-08-23 07:17:30980
[email protected]f8aefb132013-10-30 09:29:52981 if (extension_sync_service_)
982 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:52983}
984
[email protected]1abf05e2013-07-09 17:04:36985void ExtensionService::DisableUserExtensions(
986 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:14987 extensions::ManagementPolicy* management_policy =
988 system_->management_policy();
989 extensions::ExtensionList to_disable;
990
991 for (ExtensionSet::const_iterator extension = extensions_.begin();
992 extension != extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:11993 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14994 to_disable.push_back(*extension);
995 }
996 for (ExtensionSet::const_iterator extension = terminated_extensions_.begin();
997 extension != terminated_extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:11998 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:14999 to_disable.push_back(*extension);
1000 }
1001
1002 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1003 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381004 if ((*extension)->was_installed_by_default() &&
1005 extension_urls::IsWebstoreUpdateUrl(
1006 extensions::ManifestURL::GetUpdateURL(*extension)))
1007 continue;
[email protected]1abf05e2013-07-09 17:04:361008 const std::string& id = (*extension)->id();
1009 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1010 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141011 }
1012}
1013
[email protected]eaa7dd182010-12-14 11:09:001014void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281015 const Extension* extension) {
1016 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491017 RecordPermissionMessagesHistogram(
1018 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251019 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1020 EnableExtension(extension->id());
1021}
1022
[email protected]009633c2013-03-07 22:08:281023void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451024 CHECK(extension);
1025 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281026 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451027}
1028
[email protected]fe2dd7742011-04-19 22:52:491029// static
1030void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051031 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171032 // Since this is called from multiple sources, and since the histogram macros
1033 // use statics, we need to manually lookup the histogram ourselves.
1034 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491035 histogram,
1036 1,
[email protected]c2e66e12012-06-27 06:27:061037 PermissionMessage::kEnumBoundary,
1038 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171039 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491040
[email protected]13c68b62013-05-17 11:29:051041 PermissionMessages permissions =
1042 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491043 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061044 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491045 } else {
[email protected]c2e66e12012-06-27 06:27:061046 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521047 it != permissions.end(); ++it)
1048 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491049 }
1050}
1051
[email protected]eaa7dd182010-12-14 11:09:001052void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421053 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061054 // was loaded, otherwise a race can arise where a renderer that is created
1055 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421056 // that the request context doesn't yet know about. The profile is responsible
1057 // for ensuring its URLRequestContexts appropriately discover the loaded
1058 // extension.
[email protected]31d8f5f22012-04-02 15:22:081059 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061060
[email protected]d5949312012-12-03 22:13:301061 // Tell renderers about the new extension, unless it's a theme (renderers
1062 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191063 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491064 for (content::RenderProcessHost::iterator i(
1065 content::RenderProcessHost::AllHostsIterator());
1066 !i.IsAtEnd(); i.Advance()) {
1067 content::RenderProcessHost* host = i.GetCurrentValue();
1068 Profile* host_profile =
1069 Profile::FromBrowserContext(host->GetBrowserContext());
1070 if (host_profile->GetOriginalProfile() ==
1071 profile_->GetOriginalProfile()) {
1072 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1073 1, ExtensionMsg_Loaded_Params(extension));
1074 host->Send(
1075 new ExtensionMsg_Loaded(loaded_extensions));
1076 }
[email protected]c8d407e2011-04-28 21:27:171077 }
[email protected]77a6970c2011-04-23 16:58:561078 }
1079
[email protected]3442a662012-01-12 08:06:171080 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1081 // extension.
1082 //
1083 // NOTE: It is important that this happen after notifying the renderers about
1084 // the new extensions so that if we navigate to an extension URL in
1085 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1086 content::NotificationService::current()->Notify(
1087 chrome::NOTIFICATION_EXTENSION_LOADED,
1088 content::Source<Profile>(profile_),
1089 content::Details<const Extension>(extension));
1090
[email protected]c8d407e2011-04-28 21:27:171091 // Tell a random-ass collection of other subsystems about the new extension.
1092 // TODO(aa): What should we do with all this goop? Can it move into the
1093 // relevant objects via EXTENSION_LOADED?
1094
1095 profile_->GetExtensionSpecialStoragePolicy()->
1096 GrantRightsForExtension(extension);
1097
1098 UpdateActiveExtensionsInCrashReporter();
1099
[email protected]c8d407e2011-04-28 21:27:171100 // If the extension has permission to load chrome://favicon/ resources we need
1101 // to make sure that the FaviconSource is registered with the
1102 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051103 if (extensions::PermissionsData::HasHostPermission(
1104 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171105 FaviconSource* favicon_source = new FaviconSource(profile_,
1106 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531107 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171108 }
[email protected]b07e606e2012-09-15 20:16:151109
1110#if !defined(OS_ANDROID)
1111 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051112 if (extensions::PermissionsData::HasHostPermission(
1113 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151114 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531115 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151116 }
1117#endif
1118
[email protected]5eddc3e2011-10-26 04:33:311119 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051120 if (extensions::PermissionsData::HasHostPermission(
1121 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511122 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531123 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311124 }
[email protected]62d30f42009-10-01 22:36:061125}
1126
[email protected]a9f39a312010-12-23 22:14:271127void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591128 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131129 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271130 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071131 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271132 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531133 content::Source<Profile>(profile_),
1134 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061135
[email protected]f3b1a082011-11-18 00:34:301136 for (content::RenderProcessHost::iterator i(
1137 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561138 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301139 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081140 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301141 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081142 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171143 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561144 }
1145
[email protected]31d8f5f22012-04-02 15:22:081146 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171147 profile_->GetExtensionSpecialStoragePolicy()->
1148 RevokeRightsForExtension(extension);
1149
[email protected]b777b332011-04-16 04:01:081150#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231151 // Revoke external file access for the extension from its file system context.
1152 // It is safe to access the extension's storage partition at this point. The
1153 // storage partition may get destroyed only after the extension gets unloaded.
1154 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1155 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091156 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231157 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091158 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321159 if (filesystem_context && filesystem_context->external_backend()) {
1160 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171161 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061162 }
[email protected]c8d407e2011-04-28 21:27:171163#endif
1164
1165 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061166}
1167
[email protected]0a071a32011-02-08 00:18:241168Profile* ExtensionService::profile() {
1169 return profile_;
1170}
1171
[email protected]45759612012-07-10 17:21:231172extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451173 return extension_prefs_;
1174}
1175
[email protected]a7ff4b72013-10-17 20:56:021176const extensions::ExtensionPrefs* ExtensionService::extension_prefs() const {
1177 return extension_prefs_;
1178}
1179
[email protected]0d9a2202011-11-09 13:48:411180extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381181 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201182}
1183
[email protected]e2d720aa2012-05-17 00:11:351184extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441185 return extension_prefs()->content_settings_store();
1186}
1187
[email protected]25ae0152011-11-18 14:40:021188bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351189 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021190}
1191
[email protected]7f8f24f2012-11-15 19:40:141192base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091193 if (file_task_runner_.get())
1194 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141195
1196 // We should be able to interrupt any part of extension install process during
1197 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1198 // will be ignored/deleted while we will block on started tasks.
1199 std::string token("ext_install-");
1200 token.append(profile_->GetPath().AsUTF8Unsafe());
1201 file_task_runner_ = BrowserThread::GetBlockingPool()->
1202 GetSequencedTaskRunnerWithShutdownBehavior(
1203 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1204 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091205 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141206}
1207
[email protected]42a08162012-03-16 18:09:111208extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061209 return updater_.get();
1210}
1211
[email protected]4ee07c62012-08-21 12:40:421212void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361213 std::vector<std::string> to_unload;
1214 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281215
[email protected]0d54b682013-11-05 14:15:361216 // Loop through the extensions list, finding extensions we need to unload or
1217 // disable.
[email protected]84df8332011-12-06 18:22:461218 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251219 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361220 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421221 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361222 to_unload.push_back(extension->id());
1223 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1224 if (system_->management_policy()->MustRemainDisabled(
1225 extension, &disable_reason, NULL))
1226 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251227 }
1228
[email protected]0d54b682013-11-05 14:15:361229 for (size_t i = 0; i < to_unload.size(); ++i)
1230 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1231
1232 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1233 to_disable.begin(); i != to_disable.end(); ++i)
1234 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251235}
1236
[email protected]31206602011-04-13 23:07:321237void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351238 if (updater()) {
[email protected]94fde232012-04-27 10:22:301239 if (AreAllExternalProvidersReady()) {
1240 updater()->CheckSoon();
1241 } else {
1242 // Sync can start updating before all the external providers are ready
1243 // during startup. Start the update as soon as those providers are ready,
1244 // but not before.
1245 update_once_all_providers_are_ready_ = true;
1246 }
[email protected]c3cfb012011-04-06 22:07:351247 } else {
[email protected]31206602011-04-13 23:07:321248 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351249 }
1250}
1251
[email protected]168389f2011-12-20 17:12:481252void ExtensionService::OnExtensionMoved(
1253 const std::string& moved_extension_id,
1254 const std::string& predecessor_extension_id,
1255 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231256 extension_prefs_->extension_sorting()->OnExtensionMoved(
1257 moved_extension_id,
1258 predecessor_extension_id,
1259 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481260
1261 const Extension* extension = GetInstalledExtension(moved_extension_id);
[email protected]f8aefb132013-10-30 09:29:521262 if (extension_sync_service_ && extension) {
1263 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1264 }
[email protected]168389f2011-12-20 17:12:481265}
1266
[email protected]8e4560b62011-01-14 10:09:141267// Some extensions will autoupdate themselves externally from Chrome. These
1268// are typically part of some larger client application package. To support
1269// these, the extension will register its location in the the preferences file
1270// (and also, on Windows, in the registry) and this code will periodically
1271// check that location for a .crx file, which it will then install locally if
1272// a new version is available.
1273// Errors are reported through ExtensionErrorReporter. Succcess is not
1274// reported.
[email protected]eaa7dd182010-12-14 11:09:001275void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121276 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141277
1278 // Note that this installation is intentionally silent (since it didn't
1279 // go through the front-end). Extensions that are registered in this
1280 // way are effectively considered 'pre-bundled', and so implicitly
1281 // trusted. In general, if something has HKLM or filesystem access,
1282 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121283
[email protected]8e4560b62011-01-14 10:09:141284 // Ask each external extension provider to give us a call back for each
1285 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271286 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141287 for (i = external_extension_providers_.begin();
1288 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271289 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141290 provider->VisitRegisteredExtension();
1291 }
1292
[email protected]50067e52011-10-20 23:17:071293 // Do any required work that we would have done after completion of all
1294 // providers.
1295 if (external_extension_providers_.empty()) {
1296 OnAllExternalProvidersReady();
1297 }
[email protected]9f1087e2009-06-15 17:29:321298}
1299
[email protected]50067e52011-10-20 23:17:071300void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271301 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121302 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071303 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121304
1305 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141306 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301307 if (AreAllExternalProvidersReady())
1308 OnAllExternalProvidersReady();
1309}
1310
1311bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271312 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141313 for (i = external_extension_providers_.begin();
1314 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301315 if (!i->get()->IsReady())
1316 return false;
[email protected]8e4560b62011-01-14 10:09:141317 }
[email protected]94fde232012-04-27 10:22:301318 return true;
[email protected]50067e52011-10-20 23:17:071319}
1320
1321void ExtensionService::OnAllExternalProvidersReady() {
1322 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301323 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1324 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071325
1326 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301327 if (update_once_all_providers_are_ready_ && updater()) {
1328 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061329 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141330 }
1331
1332 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231333 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141334 extension_prefs_->GetInstalledExtensionsInfo());
1335 for (size_t i = 0; i < extensions_info->size(); ++i) {
1336 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401337 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141338 CheckExternalUninstall(info->extension_id);
1339 }
[email protected]e5af875f2011-10-10 21:09:141340 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141341}
1342
1343void ExtensionService::IdentifyAlertableExtensions() {
1344 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1345
[email protected]07c9f2f42012-02-29 18:45:221346 // Build up the lists of extensions that require acknowledgment. If this is
1347 // the first time, grandfather extensions that would have caused
1348 // notification.
[email protected]89226982012-07-16 20:09:181349 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141350
[email protected]a9aa5932012-01-25 08:27:401351 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181352 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011353 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401354 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181355 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141356 } else {
1357 // First run. Just acknowledge all the extensions, silently, by
1358 // shortcutting the display of the UI and going straight to the
1359 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401360 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141361 }
1362 }
[email protected]a9aa5932012-01-25 08:27:401363
[email protected]612a1cb12012-10-17 13:18:031364 UpdateExternalExtensionAlert();
1365
[email protected]a9aa5932012-01-25 08:27:401366 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181367 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141368}
1369
[email protected]89226982012-07-16 20:09:181370bool ExtensionService::PopulateExtensionErrorUI(
1371 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221372 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281373
1374 // Extensions that are blacklisted.
1375 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1376 it != blacklisted_extensions_.end(); ++it) {
1377 std::string id = (*it)->id();
1378 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1379 extension_error_ui->AddBlacklistedExtension(id);
1380 needs_alert = true;
1381 }
1382 }
1383
[email protected]07c9f2f42012-02-29 18:45:221384 for (ExtensionSet::const_iterator iter = extensions_.begin();
1385 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361386 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281387
1388 // Extensions disabled by policy. Note: this no longer includes blacklisted
1389 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391390 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221391 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181392 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221393 needs_alert = true;
1394 }
1395 }
[email protected]07c9f2f42012-02-29 18:45:221396 }
[email protected]695b5712012-12-06 23:55:281397
[email protected]07c9f2f42012-02-29 18:45:221398 return needs_alert;
1399}
1400
[email protected]a9aa5932012-01-25 08:27:401401void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031402 const ExtensionIdSet* extension_ids =
1403 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141404 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1405 iter != extension_ids->end(); ++iter) {
1406 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1407 }
[email protected]879a6092013-03-06 04:12:501408 extension_error_ui_.reset();
1409}
1410
1411void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301412 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141413}
1414
[email protected]47fc70c2011-12-06 07:29:511415void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1416 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031417 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511418}
1419
[email protected]d96eb512012-11-01 23:44:081420bool ExtensionService::IsUnacknowledgedExternalExtension(
1421 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321422 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1423 return false;
1424
[email protected]1d5e58b2013-01-31 08:41:401425 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081426 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1427 !(extension_prefs_->GetDisableReasons(extension->id()) &
1428 Extension::DISABLE_SIDELOAD_WIPEOUT));
1429}
1430
[email protected]59ee99d2013-10-11 15:46:161431void ExtensionService::ReconcileKnownDisabled() {
1432 ExtensionIdSet known_disabled_ids = extension_prefs_->GetKnownDisabled();
1433 if (known_disabled_ids.empty()) {
1434 if (!disabled_extensions_.is_empty()) {
1435 extension_prefs_->SetKnownDisabled(disabled_extensions_.GetIDs());
1436 UMA_HISTOGRAM_BOOLEAN("Extensions.KnownDisabledInitialized", true);
1437 }
1438 } else {
1439 // Both |known_disabled_ids| and |extensions_| are ordered (by definition
1440 // of std::map and std::set). Iterate forward over both sets in parallel
1441 // to find matching IDs and disable the corresponding extensions.
1442 ExtensionSet::const_iterator extensions_it = extensions_.begin();
1443 ExtensionIdSet::const_iterator known_disabled_ids_it =
1444 known_disabled_ids.begin();
1445 int known_disabled_count = 0;
1446 while (extensions_it != extensions_.end() &&
1447 known_disabled_ids_it != known_disabled_ids.end()) {
1448 const std::string& extension_id = extensions_it->get()->id();
1449 const int comparison = extension_id.compare(*known_disabled_ids_it);
1450 if (comparison < 0) {
1451 ++extensions_it;
1452 } else if (comparison > 0) {
1453 ++known_disabled_ids_it;
1454 } else {
1455 ++known_disabled_count;
1456 // Advance |extensions_it| immediately as it will be invalidated upon
1457 // disabling the extension it points to.
1458 ++extensions_it;
1459 ++known_disabled_ids_it;
1460 DisableExtension(extension_id, Extension::DISABLE_KNOWN_DISABLED);
1461 }
1462 }
1463 UMA_HISTOGRAM_COUNTS_100("Extensions.KnownDisabledReDisabled",
1464 known_disabled_count);
1465 }
[email protected]195c1582013-10-29 18:42:231466
1467 // Update the list of known disabled to reflect every change to
1468 // |disabled_extensions_| from this point forward.
1469 disabled_extensions_.set_modification_callback(
1470 base::Bind(&extensions::ExtensionPrefs::SetKnownDisabled,
1471 base::Unretained(extension_prefs_)));
[email protected]59ee99d2013-10-11 15:46:161472}
1473
[email protected]89226982012-07-16 20:09:181474void ExtensionService::HandleExtensionAlertDetails() {
1475 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301476 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1477 // causes a navigation.
1478 if (extension_error_ui_)
1479 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101480}
1481
[email protected]612a1cb12012-10-17 13:18:031482void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361483 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031484 return;
1485
1486 const Extension* extension = NULL;
1487 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1488 iter != disabled_extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361489 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081490 if (IsUnacknowledgedExternalExtension(e)) {
1491 extension = e;
1492 break;
[email protected]612a1cb12012-10-17 13:18:031493 }
1494 }
1495
1496 if (extension) {
[email protected]d96eb512012-11-01 23:44:081497 if (!extensions::HasExternalInstallError(this)) {
1498 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031499 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081500 // Stop prompting for this extension, and check if there's another
1501 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031502 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081503 UpdateExternalExtensionAlert();
1504 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1505 EXTERNAL_EXTENSION_IGNORED,
1506 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021507 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1508 UMA_HISTOGRAM_ENUMERATION(
1509 "Extensions.ExternalExtensionEventWebstore",
1510 EXTERNAL_EXTENSION_IGNORED,
1511 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1512 } else {
1513 UMA_HISTOGRAM_ENUMERATION(
1514 "Extensions.ExternalExtensionEventNonWebstore",
1515 EXTERNAL_EXTENSION_IGNORED,
1516 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1517 }
[email protected]d96eb512012-11-01 23:44:081518 return;
[email protected]612a1cb12012-10-17 13:18:031519 }
[email protected]b3aa7182013-04-25 04:45:231520 if (is_first_run_)
1521 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1522 // first_run is true if the extension was installed during a first run
1523 // (even if it's post-first run now).
1524 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1525 extension->id());
1526 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031527 }
1528 } else {
1529 extensions::RemoveExternalInstallError(this);
1530 }
[email protected]612a1cb12012-10-17 13:18:031531}
1532
[email protected]a9f39a312010-12-23 22:14:271533void ExtensionService::UnloadExtension(
1534 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131535 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091536 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281537 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021538 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281539 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251540
[email protected]fa0c96732010-11-17 00:14:231541 // This method can be called via PostTask, so the extension may have been
1542 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091543 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171544 // In case the extension may have crashed/uninstalled. Allow the profile to
1545 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081546 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231547 return;
[email protected]dd163fb02011-05-04 22:22:171548 }
[email protected]0c6da502009-08-14 22:32:391549
[email protected]20c8b942013-05-26 02:03:401550 // If uninstalling let RuntimeEventRouter know.
[email protected]b0af4792013-10-23 09:12:131551 if (reason == UnloadedExtensionInfo::REASON_UNINSTALL)
[email protected]20c8b942013-05-26 02:03:401552 extensions::RuntimeEventRouter::OnExtensionUninstalled(
1553 profile_, extension_id);
1554
[email protected]1eb175082010-02-10 09:26:161555 // Keep information about the extension so that we can reload it later
1556 // even if it's not permanently installed.
1557 unloaded_extension_paths_[extension->id()] = extension->path();
1558
[email protected]f17dbd42010-08-16 23:21:101559 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031560 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101561
[email protected]d7e9a862010-11-03 21:57:491562 // Clean up runtime data.
1563 extension_runtime_data_.erase(extension_id);
1564
[email protected]060d4972012-07-19 17:22:391565 if (disabled_extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461566 disabled_extensions_.Remove(extension->id());
[email protected]dd163fb02011-05-04 22:22:171567 // Make sure the profile cleans up its RequestContexts when an already
1568 // disabled extension is unloaded (since they are also tracking the disabled
1569 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081570 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]60a174a2013-08-02 20:29:281571 } else {
1572 // Remove the extension from our list.
1573 extensions_.Remove(extension->id());
1574 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391575 }
1576
[email protected]fb789532013-08-27 02:40:211577 content::NotificationService::current()->Notify(
1578 chrome::NOTIFICATION_EXTENSION_REMOVED,
1579 content::Source<Profile>(profile_),
1580 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251581}
1582
[email protected]8b1ec202013-09-05 02:09:501583void ExtensionService::RemoveComponentExtension(
1584 const std::string& extension_id) {
1585 scoped_refptr<const Extension> extension(
1586 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131587 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]8b1ec202013-09-05 02:09:501588 content::NotificationService::current()->Notify(
1589 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1590 content::Source<Profile>(profile_),
1591 content::Details<const Extension>(extension.get()));
1592}
1593
[email protected]eaa7dd182010-12-14 11:09:001594void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401595 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171596
[email protected]84df8332011-12-06 18:22:461597 extensions_.Clear();
1598 disabled_extensions_.Clear();
1599 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491600 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321601
[email protected]c6e4a3412009-06-24 15:45:291602 // TODO(erikkay) should there be a notification for this? We can't use
1603 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1604 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321605}
1606
[email protected]eaa7dd182010-12-14 11:09:001607void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321608 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451609 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011610 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351611 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1612 // times.
[email protected]9f1087e2009-06-15 17:29:321613}
1614
[email protected]eaa7dd182010-12-14 11:09:001615void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:101616#if defined(OS_CHROMEOS)
1617 if (disable_garbage_collection_)
1618 return;
1619#endif
1620
[email protected]2f8757c32012-06-19 19:17:471621 if (extension_prefs_->pref_service()->ReadOnly())
1622 return;
1623
[email protected]9bd9a6862012-11-29 09:24:221624 if (pending_extension_manager()->HasPendingExtensions()) {
1625 // Don't garbage collect while there are pending installations, which may
1626 // be using the temporary installation directory. Try to garbage collect
1627 // again later.
[email protected]b3a25092013-05-28 22:08:161628 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221629 FROM_HERE,
1630 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1631 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1632 return;
1633 }
1634
[email protected]45759612012-07-10 17:21:231635 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471636 extension_prefs_->GetInstalledExtensionsInfo());
1637
[email protected]650b2d52013-02-10 03:41:451638 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471639 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051640 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1641 info->at(i)->extension_path));
1642
[email protected]399583b2012-12-11 09:33:421643 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051644 for (size_t i = 0; i < info->size(); ++i)
1645 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1646 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471647
[email protected]7f8f24f2012-11-15 19:40:141648 if (!GetFileTaskRunner()->PostTask(
1649 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471650 base::Bind(
1651 &extension_file_util::GarbageCollectExtensions,
1652 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:401653 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:471654 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401655 }
[email protected]3cf4f0992009-02-03 23:00:301656}
1657
[email protected]820d9bd2013-04-03 03:46:031658void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351659 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031660 content::NotificationService::current()->Notify(
1661 chrome::NOTIFICATION_EXTENSIONS_READY,
1662 content::Source<Profile>(profile_),
1663 content::NotificationService::NoDetails());
1664}
1665
[email protected]eaa7dd182010-12-14 11:09:001666void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111667 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171668 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501669
[email protected]bc151cf92013-02-12 04:57:261670 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511671}
1672
[email protected]fcb58a862012-05-01 01:03:151673void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171674 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1675 // default extension state to DISABLED when the --disable-extensions flag
1676 // is set (http://crbug.com/29067).
1677 if (!extensions_enabled() &&
1678 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401679 extension->location() != Manifest::COMPONENT &&
1680 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151681 return;
[email protected]330840c2012-08-29 22:21:011682 }
[email protected]c8d407e2011-04-28 21:27:171683
[email protected]b914e2952013-04-26 07:10:031684 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171685 bool is_extension_installed = false;
1686 const Extension* old = GetInstalledExtension(extension->id());
1687 if (old) {
1688 is_extension_installed = true;
1689 int version_compare_result =
1690 extension->version()->CompareTo(*(old->version()));
1691 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031692 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1693 // that we aren't downgrading.
1694 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171695 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031696 }
1697 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171698
[email protected]1eb175082010-02-10 09:26:161699 // The extension is now loaded, remove its data from unloaded extension map.
1700 unloaded_extension_paths_.erase(extension->id());
1701
[email protected]bb7f40952011-01-13 00:21:201702 // If a terminated extension is loaded, remove it from the terminated list.
1703 UntrackTerminatedExtension(extension->id());
1704
[email protected]f17dbd42010-08-16 23:21:101705 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411706 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101707
[email protected]b914e2952013-04-26 07:10:031708 // Check if the extension's privileges have changed and mark the
1709 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171710 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031711
[email protected]116d40e2013-08-08 17:23:171712 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411713 // To upgrade an extension in place, unload the old one and then load the
1714 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131715 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031716 }
[email protected]0c6da502009-08-14 22:32:391717
[email protected]695b5712012-12-06 23:55:281718 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091719 // Only prefs is checked for the blacklist. We rely on callers to check the
1720 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071721 // installation then threads through the install and pending install flow
1722 // of this class, and we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:281723 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:411724 } else if (!reloading &&
1725 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:481726 disabled_extensions_.Insert(extension);
[email protected]f8aefb132013-10-30 09:29:521727 if (extension_sync_service_)
1728 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031729 content::NotificationService::current()->Notify(
1730 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1731 content::Source<Profile>(profile_),
1732 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441733
[email protected]7e9e2422012-12-13 19:54:191734 // Show the extension disabled error if a permissions increase was the
1735 // only reason it was disabled.
1736 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:031737 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441738 extensions::AddExtensionDisabledError(this, extension);
1739 }
[email protected]ca2e82022013-04-29 16:48:411740 } else if (reloading) {
1741 // Replace the old extension with the new version.
1742 CHECK(!disabled_extensions_.Insert(extension));
1743 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281744 } else {
1745 // All apps that are displayed in the launcher are ordered by their ordinals
1746 // so we must ensure they have valid ordinals.
1747 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:251748 if (!extension->ShouldDisplayInNewTabPage()) {
1749 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
1750 extension->id());
1751 }
[email protected]695b5712012-12-06 23:55:281752 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
1753 extension->id(), syncer::StringOrdinal());
1754 }
1755
1756 extensions_.Insert(extension);
[email protected]f8aefb132013-10-30 09:29:521757 if (extension_sync_service_)
1758 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281759 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311760 }
[email protected]b914e2952013-04-26 07:10:031761 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:351762}
1763
[email protected]8c484b742012-11-29 06:05:361764void ExtensionService::AddComponentExtension(const Extension* extension) {
1765 const std::string old_version_string(
1766 extension_prefs_->GetVersionString(extension->id()));
1767 const Version old_version(old_version_string);
1768
1769 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1770 VLOG(1) << "Component extension " << extension->name() << " ("
1771 << extension->id() << ") installing/upgrading from '"
1772 << old_version_string << "' to " << extension->version()->GetString();
1773
1774 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:421775 Extension::ENABLED_COMPONENT,
[email protected]9f3c8532013-07-31 19:52:071776 extensions::Blacklist::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:421777 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:361778 return;
1779 }
1780
1781 AddExtension(extension);
1782}
1783
[email protected]b914e2952013-04-26 07:10:031784void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:311785 // If the extension has used the optional permissions API, it will have a
1786 // custom set of active permissions defined in the extension prefs. Here,
1787 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:061788 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:311789 extension_prefs()->GetActivePermissions(extension->id());
1790
[email protected]dc24976f2013-06-02 21:15:091791 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:311792 // We restrict the active permissions to be within the bounds defined in the
1793 // extension's manifest.
1794 // a) active permissions must be a subset of optional + default permissions
1795 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:061796 scoped_refptr<PermissionSet> total_permissions =
1797 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061798 extensions::PermissionsData::GetRequiredPermissions(extension),
1799 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:311800
1801 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:061802 scoped_refptr<PermissionSet> adjusted_active =
1803 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:311804 total_permissions.get(), active_permissions.get());
1805
1806 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:061807 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061808 extensions::PermissionsData::GetRequiredPermissions(extension),
1809 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311810
[email protected]c333e792012-01-06 16:57:391811 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:091812 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311813 }
[email protected]b914e2952013-04-26 07:10:031814}
1815
1816void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171817 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:031818 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311819
[email protected]8d888c12010-11-30 00:00:251820 // We keep track of all permissions the user has granted each extension.
1821 // This allows extensions to gracefully support backwards compatibility
1822 // by including unknown permissions in their manifests. When the user
1823 // installs the extension, only the recognized permissions are recorded.
1824 // When the unknown permissions become recognized (e.g., through browser
1825 // upgrade), we can prompt the user to accept these new permissions.
1826 // Extensions can also silently upgrade to less permissions, and then
1827 // silently upgrade to a version that adds these permissions back.
1828 //
1829 // For example, pretend that Chrome 10 includes a permission "omnibox"
1830 // for an API that adds suggestions to the omnibox. An extension can
1831 // maintain backwards compatibility while still having "omnibox" in the
1832 // manifest. If a user installs the extension on Chrome 9, the browser
1833 // will record the permissions it recognized, not including "omnibox."
1834 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1835 // will disable the extension and prompt the user to approve the increase
1836 // in privileges. The extension could then release a new version that
1837 // removes the "omnibox" permission. When the user upgrades, Chrome will
1838 // still remember that "omnibox" had been granted, so that if the
1839 // extension once again includes "omnibox" in an upgrade, the extension
1840 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281841 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251842
[email protected]211a3f32013-05-28 21:48:181843 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361844 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]211a3f32013-05-28 21:48:181845 chrome::IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031846 // Silently grant all active permissions to default apps only on install.
1847 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181848 // Silently grant all active permissions to apps install in kiosk mode on both
1849 // install and update.
1850 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031851 GrantPermissions(extension);
1852
1853 bool is_privilege_increase = false;
1854 // We only need to compare the granted permissions to the current permissions
1855 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051856 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181857 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251858 // Add all the recognized permissions if the granted permissions list
1859 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061860 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311861 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521862 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251863
1864 // Here, we check if an extension's privileges have increased in a manner
1865 // that requires the user's approval. This could occur because the browser
1866 // upgraded and recognized additional privileges, or an extension upgrades
1867 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251868 is_privilege_increase =
1869 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
1870 granted_permissions,
1871 extension->GetActivePermissions().get(),
1872 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251873 }
1874
[email protected]116d40e2013-08-08 17:23:171875 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031876 // If the extension was already disabled, suppress any alerts for becoming
1877 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031878 bool previously_disabled =
1879 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191880 // Legacy disabled extensions do not have a disable reason. Infer that if
1881 // there was no permission increase, it was likely disabled by the user.
1882 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031883 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191884 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031885 }
[email protected]7e9e2422012-12-13 19:54:191886 // Extensions that came to us disabled from sync need a similar inference,
1887 // except based on the new version's permissions.
[email protected]60d8d382013-10-27 02:03:361888 // TODO(yoz): Since we no longer sync disabled state, simplify this.
[email protected]7e9e2422012-12-13 19:54:191889 if (previously_disabled &&
1890 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1891 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1892 extension_prefs_->ClearDisableReasons(extension->id());
1893 if (!is_privilege_increase)
1894 disable_reasons |= Extension::DISABLE_USER_ACTION;
1895 }
[email protected]b914e2952013-04-26 07:10:031896 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251897 }
1898
1899 // Extension has changed permissions significantly. Disable it. A
1900 // notification should be sent by the caller.
1901 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191902 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491903 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1904 RecordPermissionMessagesHistogram(
1905 extension, "Extensions.Permissions_AutoDisable");
1906 }
[email protected]195c1582013-10-29 18:42:231907 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251908 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031909 }
1910 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281911 extension_prefs_->AddDisableReason(
1912 extension->id(),
1913 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251914 }
1915}
1916
[email protected]eaa7dd182010-12-14 11:09:001917void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391918 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:461919 for (ExtensionSet::const_iterator iter = extensions_.begin();
1920 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361921 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401922 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461923 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351924 }
1925
[email protected]f6431be82013-09-07 02:53:451926 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251927}
1928
[email protected]669b2372013-10-17 15:04:581929ExtensionService::ImportStatus ExtensionService::CheckImports(
1930 const extensions::Extension* extension,
1931 std::list<SharedModuleInfo::ImportInfo>* missing_modules,
1932 std::list<SharedModuleInfo::ImportInfo>* outdated_modules) {
1933 DCHECK(extension);
1934 DCHECK(missing_modules && missing_modules->empty());
1935 DCHECK(outdated_modules && outdated_modules->empty());
[email protected]9f4e4f082013-06-21 07:11:191936 ImportStatus status = IMPORT_STATUS_OK;
[email protected]9f4e4f082013-06-21 07:11:191937 if (SharedModuleInfo::ImportsModules(extension)) {
1938 const std::vector<SharedModuleInfo::ImportInfo>& imports =
1939 SharedModuleInfo::GetImports(extension);
1940 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
1941 for (i = imports.begin(); i != imports.end(); ++i) {
1942 Version version_required(i->minimum_version);
1943 const Extension* imported_module =
1944 GetExtensionById(i->extension_id, true);
1945 if (!imported_module) {
1946 if (extension->from_webstore()) {
1947 status = IMPORT_STATUS_UNSATISFIED;
[email protected]669b2372013-10-17 15:04:581948 missing_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:191949 } else {
1950 return IMPORT_STATUS_UNRECOVERABLE;
1951 }
1952 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
1953 return IMPORT_STATUS_UNRECOVERABLE;
1954 } else if (version_required.IsValid() &&
1955 imported_module->version()->CompareTo(version_required) < 0) {
1956 if (imported_module->from_webstore()) {
[email protected]669b2372013-10-17 15:04:581957 outdated_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:191958 status = IMPORT_STATUS_UNSATISFIED;
1959 } else {
1960 return IMPORT_STATUS_UNRECOVERABLE;
1961 }
1962 }
1963 }
1964 }
[email protected]669b2372013-10-17 15:04:581965 return status;
1966}
1967
1968ExtensionService::ImportStatus ExtensionService::SatisfyImports(
1969 const Extension* extension) {
1970 std::list<SharedModuleInfo::ImportInfo> noinstalled;
1971 std::list<SharedModuleInfo::ImportInfo> outdated;
1972 ImportStatus status = CheckImports(extension, &noinstalled, &outdated);
1973 if (status == IMPORT_STATUS_UNRECOVERABLE)
1974 return status;
[email protected]9f4e4f082013-06-21 07:11:191975 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]669b2372013-10-17 15:04:581976 std::list<SharedModuleInfo::ImportInfo>::const_iterator iter;
1977 for (iter = noinstalled.begin(); iter != noinstalled.end(); ++iter) {
[email protected]9f4e4f082013-06-21 07:11:191978 pending_extension_manager()->AddFromExtensionImport(
[email protected]669b2372013-10-17 15:04:581979 iter->extension_id,
[email protected]9f4e4f082013-06-21 07:11:191980 extension_urls::GetWebstoreUpdateUrl(),
1981 IsSharedModule);
1982 }
1983 CheckForUpdatesSoon();
1984 }
1985 return status;
1986}
1987
1988scoped_ptr<const ExtensionSet>
1989 ExtensionService::GetDependentExtensions(const Extension* extension) {
1990 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
1991 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
1992 if (SharedModuleInfo::IsSharedModule(extension)) {
1993 set_to_check->InsertAll(disabled_extensions_);
1994 set_to_check->InsertAll(delayed_installs_);
1995 set_to_check->InsertAll(extensions_);
1996 for (ExtensionSet::const_iterator iter = set_to_check->begin();
1997 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:111998 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
1999 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192000 dependents->Insert(*iter);
2001 }
2002 }
2003 }
2004 return dependents.PassAs<const ExtensionSet>();
2005}
2006
2007void ExtensionService::PruneSharedModulesOnUninstall(
2008 const Extension* extension) {
2009 if (SharedModuleInfo::ImportsModules(extension)) {
2010 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2011 SharedModuleInfo::GetImports(extension);
2012 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2013 for (i = imports.begin(); i != imports.end(); ++i) {
2014 const Extension* imported_module =
2015 GetExtensionById(i->extension_id, true);
2016 if (imported_module && imported_module->from_webstore()) {
2017 scoped_ptr<const ExtensionSet> dependents =
2018 GetDependentExtensions(imported_module);
2019 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212020 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192021 }
2022 }
2023 }
2024 }
2025}
2026
[email protected]8266d662011-07-12 21:53:262027void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502028 const Extension* extension,
[email protected]98270432012-09-11 20:51:242029 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052030 bool has_requirement_errors,
[email protected]9f3c8532013-07-31 19:52:072031 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052032 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122033 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202034
[email protected]c3cfb012011-04-06 22:07:352035 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032036 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412037 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062038 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402039 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062040 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322041
[email protected]31bb5ee62012-09-12 22:58:402042 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2043 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222044 << " and update URL "
2045 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402046 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542047
[email protected]4416c5a2010-06-26 01:28:572048 // Delete the extension directory since we're not going to
2049 // load it.
[email protected]7f8f24f2012-11-15 19:40:142050 if (!GetFileTaskRunner()->PostTask(
2051 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362052 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402053 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362054 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402055 }
[email protected]4416c5a2010-06-26 01:28:572056 return;
2057 }
[email protected]51a3bf8b2012-06-08 22:53:062058
2059 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572060 } else {
[email protected]c3cfb012011-04-06 22:07:352061 // We explicitly want to re-enable an uninstalled external
2062 // extension; if we're here, that means the user is manually
2063 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332064 if (IsExternalExtensionUninstalled(id)) {
2065 initial_enable = true;
2066 }
[email protected]aa142702010-03-26 01:26:332067 }
2068
[email protected]98270432012-09-11 20:51:242069 // Unsupported requirements overrides the management policy.
2070 if (has_requirement_errors) {
2071 initial_enable = false;
2072 extension_prefs_->AddDisableReason(
2073 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2074 // If the extension was disabled because of unsupported requirements but
2075 // now supports all requirements after an update and there are not other
2076 // disable reasons, enable it.
2077 } else if (extension_prefs_->GetDisableReasons(id) ==
2078 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2079 initial_enable = true;
2080 extension_prefs_->ClearDisableReasons(id);
2081 }
2082
[email protected]48a359342013-10-30 00:22:002083 if (blacklist_state == extensions::Blacklist::BLACKLISTED_MALWARE) {
[email protected]9f3c8532013-07-31 19:52:072084 // Installation of a blacklisted extension can happen from sync, policy,
2085 // etc, where to maintain consistency we need to install it, just never
2086 // load it (see AddExtension). Usually it should be the job of callers to
2087 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2088 // showing the install dialogue).
2089 extension_prefs()->AcknowledgeBlacklistedExtension(id);
2090 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2091 extension->location(),
2092 Manifest::NUM_LOCATIONS);
2093 }
2094
[email protected]695b5712012-12-06 23:55:282095 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552096 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2097 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302098 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402099 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552100 RecordPermissionMessagesHistogram(
2101 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302102 } else {
2103 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2104 extension->GetType(), 100);
2105 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402106 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552107 }
2108
[email protected]a39921b42012-02-28 03:42:542109 // Certain extension locations are specific enough that we can
2110 // auto-acknowledge any extension that came from one of them.
[email protected]9bb691632013-09-26 18:50:122111 if (Manifest::IsPolicyLocation(extension->location()))
[email protected]a39921b42012-02-28 03:42:542112 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422113 const Extension::State initial_state =
2114 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222115 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072116 extension_prefs_->SetDelayedInstallInfo(
2117 extension,
2118 initial_state,
2119 blacklist_state,
2120 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2121 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052122
2123 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192124 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052125
2126 // Notify extension of available update.
2127 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2128 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452129
2130 // Notify observers that app update is available.
2131 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2132 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052133 return;
2134 }
2135
[email protected]9f4e4f082013-06-21 07:11:192136 ImportStatus status = SatisfyImports(extension);
2137 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072138 extension_prefs_->SetDelayedInstallInfo(
2139 extension,
2140 initial_state,
2141 blacklist_state,
2142 extensions::ExtensionPrefs::DELAY_REASON_GC,
2143 page_ordinal);
[email protected]399583b2012-12-11 09:33:422144 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192145 } else if (status != IMPORT_STATUS_OK) {
2146 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072147 extension_prefs_->SetDelayedInstallInfo(
2148 extension,
2149 initial_state,
2150 blacklist_state,
[email protected]9f4e4f082013-06-21 07:11:192151 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2152 page_ordinal);
2153 delayed_installs_.Insert(extension);
2154 }
[email protected]399583b2012-12-11 09:33:422155 } else {
[email protected]9f3c8532013-07-31 19:52:072156 AddNewOrUpdatedExtension(extension,
2157 initial_state,
2158 blacklist_state,
2159 page_ordinal);
[email protected]399583b2012-12-11 09:33:422160 }
[email protected]8c484b742012-11-29 06:05:362161}
2162
2163void ExtensionService::AddNewOrUpdatedExtension(
2164 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422165 Extension::State initial_state,
[email protected]9f3c8532013-07-31 19:52:072166 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422167 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362168 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9f3c8532013-07-31 19:52:072169 extension_prefs_->OnExtensionInstalled(extension,
2170 initial_state,
2171 blacklist_state,
2172 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122173 delayed_installs_.Remove(extension->id());
[email protected]6f6101832012-11-27 22:10:482174 FinishInstallation(extension);
2175}
2176
2177void ExtensionService::MaybeFinishDelayedInstallation(
2178 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192179 // Check if the extension already got installed.
2180 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482181 return;
[email protected]9f4e4f082013-06-21 07:11:192182 extensions::ExtensionPrefs::DelayReason reason =
2183 extension_prefs_->GetDelayedInstallReason(extension_id);
2184
2185 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2186 // preferences files that will not have set this field but it was previously
2187 // only used for idle updates.
2188 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2189 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
2190 is_ready() && !IsExtensionIdle(extension_id))
[email protected]6f6101832012-11-27 22:10:482191 return;
2192
[email protected]9f4e4f082013-06-21 07:11:192193 const Extension* extension = delayed_installs_.GetByID(extension_id);
2194 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2195 ImportStatus status = SatisfyImports(extension);
2196 if (status != IMPORT_STATUS_OK) {
2197 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2198 delayed_installs_.Remove(extension_id);
2199 // Make sure no version of the extension is actually installed, (i.e.,
2200 // that this delayed install was not an update).
2201 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2202 extension_prefs_->DeleteExtensionPrefs(extension_id);
2203 }
2204 return;
2205 }
2206 }
2207
[email protected]6f6101832012-11-27 22:10:482208 FinishDelayedInstallation(extension_id);
2209}
2210
2211void ExtensionService::FinishDelayedInstallation(
2212 const std::string& extension_id) {
2213 scoped_refptr<const Extension> extension(
2214 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092215 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192216 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482217
[email protected]399583b2012-12-11 09:33:422218 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482219 NOTREACHED();
2220
[email protected]dc24976f2013-06-02 21:15:092221 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482222}
2223
2224void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562225 const extensions::Extension* existing_extension =
2226 GetInstalledExtension(extension->id());
2227 bool is_update = false;
2228 std::string old_name;
2229 if (existing_extension) {
2230 is_update = true;
2231 old_name = existing_extension->name();
2232 }
2233 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152234 content::NotificationService::current()->Notify(
2235 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2236 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022237 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152238
[email protected]d96eb512012-11-01 23:44:082239 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362240
[email protected]399583b2012-12-11 09:33:422241 // Unpacked extensions default to allowing file access, but if that has been
2242 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402243 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422244 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2245 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2246 }
2247
[email protected]6f6101832012-11-27 22:10:482248 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032249
2250 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082251 // so he can reenable it. We do this last so that it has already been
2252 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162253 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032254 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082255 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2256 EXTERNAL_EXTENSION_INSTALLED,
2257 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022258 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2259 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2260 EXTERNAL_EXTENSION_INSTALLED,
2261 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2262 } else {
2263 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2264 EXTERNAL_EXTENSION_INSTALLED,
2265 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2266 }
[email protected]d96eb512012-11-01 23:44:082267 }
[email protected]9f4e4f082013-06-21 07:11:192268
2269 // Check extensions that may have been delayed only because this shared module
2270 // was not available.
2271 if (SharedModuleInfo::IsSharedModule(extension)) {
2272 MaybeFinishDelayedInstallations();
2273 }
[email protected]4a190632009-05-09 01:07:422274}
2275
[email protected]76b65442012-11-17 14:11:482276const Extension* ExtensionService::GetPendingExtensionUpdate(
2277 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192278 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482279}
2280
[email protected]bb7f40952011-01-13 00:21:202281void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462282 if (!terminated_extensions_.Contains(extension->id()))
2283 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202284
[email protected]b0af4792013-10-23 09:12:132285 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202286}
2287
2288void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202289 std::string lowercase_id = StringToLowerASCII(id);
[email protected]60a174a2013-08-02 20:29:282290 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
[email protected]84df8332011-12-06 18:22:462291 terminated_extensions_.Remove(lowercase_id);
[email protected]fb789532013-08-27 02:40:212292 if (extension) {
2293 content::NotificationService::current()->Notify(
2294 chrome::NOTIFICATION_EXTENSION_REMOVED,
2295 content::Source<Profile>(profile_),
2296 content::Details<const Extension>(extension));
2297 }
[email protected]bb7f40952011-01-13 00:21:202298}
2299
[email protected]0dfe05c2011-02-23 23:03:362300const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472301 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282302 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472303}
2304
2305const Extension* ExtensionService::GetInstalledExtension(
2306 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282307 int include_mask = INCLUDE_ENABLED |
2308 INCLUDE_DISABLED |
2309 INCLUDE_TERMINATED |
2310 INCLUDE_BLACKLISTED;
2311 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362312}
2313
[email protected]eaa7dd182010-12-14 11:09:002314bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442315 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]be9915fb2013-07-18 09:28:552316 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442317 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402318 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392319}
2320
[email protected]4d007b312012-10-17 03:00:482321bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]be9915fb2013-07-18 09:28:552322 const Extension* extension = extensions_.GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482323 if (extension && extension->is_platform_app()) {
2324 *url = GURL(chrome::kExtensionInvalidRequestURL);
2325 return true;
2326 }
2327
2328 return false;
2329}
2330
[email protected]9060d8b02012-01-13 02:14:302331bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322332 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142333 const Version* version,
[email protected]650b2d52013-02-10 03:41:452334 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402335 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512336 int creation_flags,
2337 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382338 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2339 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012340 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302341 return false;
[email protected]a8af9fdb2010-10-28 21:52:202342
[email protected]7577a5c52009-07-30 06:21:582343 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492344 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582345 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022346 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352347
[email protected]7577a5c52009-07-30 06:21:582348 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352349 // The default apps will have the location set as INTERNAL. Since older
2350 // default apps are installed as EXTERNAL, we override them. However, if the
2351 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402352 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352353 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402354 (location == Manifest::INTERNAL &&
2355 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352356
2357 if (!is_default_apps_migration) {
2358 DCHECK(version);
2359
2360 switch (existing->version()->CompareTo(*version)) {
2361 case -1: // existing version is older, we should upgrade
2362 break;
2363 case 0: // existing version is same, do nothing
2364 return false;
2365 case 1: // existing version is newer, uh-oh
2366 LOG(WARNING) << "Found external version of extension " << id
2367 << "that is older than current version. Current version "
2368 << "is: " << existing->VersionString() << ". New "
2369 << "version is: " << version->GetString()
2370 << ". Keeping current version.";
2371 return false;
2372 }
[email protected]7577a5c52009-07-30 06:21:582373 }
2374 }
2375
[email protected]9060d8b02012-01-13 02:14:302376 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302377 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482378 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302379 return false;
[email protected]e3987852012-05-04 10:06:302380 }
[email protected]9c635f22010-12-02 09:36:362381
[email protected]14908b72011-04-20 06:54:362382 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372383 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162384 installer->set_install_source(location);
2385 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072386 installer->set_expected_version(*version);
2387 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312388 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182389#if defined(OS_CHROMEOS)
2390 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2391#else
[email protected]6dfbbf82010-03-12 23:09:162392 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182393#endif
[email protected]47fc70c2011-12-06 07:29:512394
2395 // Depending on the source, a new external extension might not need a user
2396 // notification on installation. For such extensions, mark them acknowledged
2397 // now to suppress the notification.
2398 if (mark_acknowledged)
2399 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302400
2401 return true;
[email protected]7577a5c52009-07-30 06:21:582402}
2403
[email protected]eaa7dd182010-12-14 11:09:002404void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452405 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402406 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402407 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012408 content::NotificationService::current()->Notify(
2409 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2410 content::Source<Profile>(profile_),
2411 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402412
[email protected]8a205c02011-02-04 20:41:332413 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212414 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042415 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492416 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402417 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2418}
2419
[email protected]eaa7dd182010-12-14 11:09:002420void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132421 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182422 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332423 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182424 if (iter == orphaned_dev_tools_.end())
2425 return;
2426
[email protected]04ea1bb2013-07-10 09:26:092427 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182428 orphaned_dev_tools_.erase(iter);
2429}
2430
[email protected]432115822011-07-10 15:52:272431void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532432 const content::NotificationSource& source,
2433 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272434 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542435 case chrome::NOTIFICATION_APP_TERMINATING:
2436 // Shutdown has started. Don't start any more extension installs.
2437 // (We cannot use ExtensionService::Shutdown() for this because it
2438 // happens too late in browser teardown.)
2439 browser_terminating_ = true;
2440 break;
[email protected]432115822011-07-10 15:52:272441 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532442 if (profile_ !=
2443 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322444 break;
[email protected]6c2381d2011-10-19 02:52:532445 }
[email protected]a4ed6282009-12-14 20:51:162446
[email protected]3a1dc572012-07-31 22:25:132447 extensions::ExtensionHost* host =
2448 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262449
[email protected]fa2416f2011-05-03 08:41:202450 // Mark the extension as terminated and Unload it. We want it to
2451 // be in a consistent state: either fully working or not loaded
2452 // at all, but never half-crashed. We do it in a PostTask so
2453 // that other handlers of this notification will still have
2454 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162455 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362456 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362457 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202458 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012459 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202460 host->extension()));
[email protected]31f77262009-12-02 20:48:532461 break;
2462 }
[email protected]432115822011-07-10 15:52:272463 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302464 content::RenderProcessHost* process =
2465 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192466 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302467 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192468 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2469 break;
2470
[email protected]15397b42012-05-16 23:56:062471 // Extensions need to know the channel for API restrictions.
2472 process->Send(new ExtensionMsg_SetChannel(
[email protected]15d4d2d2013-08-09 06:49:032473 extensions::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062474
[email protected]4941fe92013-06-13 23:21:012475 // Platform apps need to know the system font.
2476 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2477 webui::SetFontAndTextDirection(fonts.get());
2478 std::string font_family, font_size;
2479 fonts->GetString("fontfamily", &font_family);
2480 fonts->GetString("fontsize", &font_size);
2481 process->Send(new ExtensionMsg_SetSystemFont(
2482 font_family, font_size));
2483
[email protected]77a6970c2011-04-23 16:58:562484 // Valid extension function names, used to setup bindings in renderer.
2485 std::vector<std::string> function_names;
2486 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2487 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532488
[email protected]77a6970c2011-04-23 16:58:562489 // Scripting whitelist. This is modified by tests and must be communicated
2490 // to renderers.
2491 process->Send(new ExtensionMsg_SetScriptingWhitelist(
[email protected]c81f5d32013-10-26 10:38:422492 extensions::ExtensionsClient::Get()->GetScriptingWhitelist()));
[email protected]77a6970c2011-04-23 16:58:562493
2494 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532495 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462496 for (ExtensionSet::const_iterator iter = extensions_.begin();
2497 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492498 // Renderers don't need to know about themes.
2499 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362500 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562501 }
[email protected]9776e82e2011-11-15 02:17:532502 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562503 break;
2504 }
[email protected]432115822011-07-10 15:52:272505 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302506 content::RenderProcessHost* process =
2507 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192508 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302509 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192510 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2511 break;
2512
[email protected]52b76592013-11-02 17:59:032513 if (process_map_.Contains(process->GetID())) {
2514 // An extension process was terminated, this might have resulted in an
2515 // app or extension becoming idle.
2516 std::set<std::string> extension_ids =
2517 process_map_.GetExtensionsInProcess(process->GetID());
2518 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2519 it != extension_ids.end(); ++it) {
2520 if (delayed_installs_.Contains(*it)) {
2521 base::MessageLoop::current()->PostDelayedTask(
2522 FROM_HERE,
2523 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2524 AsWeakPtr(), *it),
2525 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2526 }
2527 }
2528 }
2529
[email protected]6bc04fd82011-12-04 02:29:352530 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462531 BrowserThread::PostTask(
2532 BrowserThread::IO, FROM_HERE,
2533 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082534 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302535 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212536 break;
2537 }
[email protected]92dd8d92013-02-26 00:41:082538 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2539 // Notify extensions that chrome update is available.
2540 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2541 profile_);
[email protected]75bdcb872013-03-13 00:41:452542
2543 // Notify observers that chrome update is available.
2544 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2545 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082546 break;
2547 }
[email protected]aa96d3a2010-08-21 08:45:252548
[email protected]4814b512009-11-07 00:12:292549 default:
2550 NOTREACHED() << "Unexpected notification type.";
2551 }
2552}
2553
[email protected]90bb38d2012-11-14 18:36:032554void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182555 IdentifyAlertableExtensions();
2556 CheckManagementPolicy();
2557}
2558
[email protected]eaa7dd182010-12-14 11:09:002559bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182560 return !GetAppIds().empty();
2561}
[email protected]377011d2010-07-20 04:18:502562
[email protected]eaa7dd182010-12-14 11:09:002563ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182564 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462565 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502566 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402567 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182568 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502569 }
2570
[email protected]ec5b50d2010-10-09 16:35:182571 return result;
[email protected]377011d2010-07-20 04:18:502572}
[email protected]d7e9a862010-11-03 21:57:492573
[email protected]dc9a74f72012-08-17 18:07:212574bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292575 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212576 return true;
2577 ExtensionRuntimeDataMap::const_iterator it =
2578 extension_runtime_data_.find(extension->id());
2579 return it == extension_runtime_data_.end() ? false :
2580 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492581}
2582
[email protected]eaa7dd182010-12-14 11:09:002583void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292584 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492585 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072586 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272587 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532588 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072589 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492590}
2591
[email protected]dc9a74f72012-08-17 18:07:212592bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2593 ExtensionRuntimeDataMap::const_iterator it =
2594 extension_runtime_data_.find(extension->id());
2595 return it == extension_runtime_data_.end() ? false :
2596 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492597}
2598
[email protected]eaa7dd182010-12-14 11:09:002599void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032600 bool value) {
[email protected]d7e9a862010-11-03 21:57:492601 extension_runtime_data_[extension->id()].being_upgraded = value;
2602}
[email protected]1bead0712010-11-27 17:41:532603
[email protected]e178ad92013-06-28 02:29:252604bool ExtensionService::IsBeingReloaded(
2605 const std::string& extension_id) const {
2606 return ContainsKey(extensions_being_reloaded_, extension_id);
2607}
2608
2609void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432610 bool isBeingReloaded) {
2611 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252612 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432613 else
[email protected]e178ad92013-06-28 02:29:252614 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252615}
2616
[email protected]dc9a74f72012-08-17 18:07:212617bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2618 ExtensionRuntimeDataMap::const_iterator it =
2619 extension_runtime_data_.find(extension->id());
2620 return it == extension_runtime_data_.end() ? false :
2621 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072622}
2623
2624void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2625 bool value) {
2626 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2627}
2628
[email protected]612a1cb12012-10-17 13:18:032629bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2630 // Extensions installed by policy can't be disabled. So even if a previous
2631 // installation disabled the extension, make sure it is now enabled.
2632 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2633 return true;
2634
2635 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2636 return false;
2637
[email protected]41070e8d2012-10-24 01:34:362638 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032639 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542640 // enabling them. Hosted apps are excepted because they are not dangerous
2641 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402642 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2643 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032644 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2645 return false;
2646 }
2647 }
[email protected]612a1cb12012-10-17 13:18:032648
2649 return true;
2650}
[email protected]0db124b02012-11-07 04:55:052651
2652bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:472653 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:332654 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:052655 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:472656 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222657 if (host)
2658 return false;
[email protected]52b76592013-11-02 17:59:032659
2660 content::SiteInstance* site_instance = process_manager->GetSiteInstanceForURL(
2661 Extension::GetBaseURLFromExtensionId(extension_id));
2662 if (site_instance && site_instance->HasProcess()) {
2663 return false;
2664 }
2665
[email protected]e7aa7b7e2012-11-27 04:51:222666 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
2667}
2668
2669bool ExtensionService::ShouldDelayExtensionUpdate(
2670 const std::string& extension_id,
2671 bool wait_for_idle) const {
2672 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2673
2674 // If delayed updates are globally disabled, or just for this extension,
2675 // don't delay.
2676 if (!install_updates_when_idle_ || !wait_for_idle)
2677 return false;
2678
[email protected]695b5712012-12-06 23:55:282679 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222680 // If there is no old extension, this is not an update, so don't delay.
2681 if (!old)
2682 return false;
2683
[email protected]9367eabc2013-03-01 01:29:292684 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222685 // Delay installation if the extension listens for the onUpdateAvailable
2686 // event.
2687 return system_->event_router()->ExtensionHasEventListener(
2688 extension_id, kOnUpdateAvailableEvent);
2689 } else {
2690 // Delay installation if the extension is not idle.
2691 return !IsExtensionIdle(extension_id);
2692 }
[email protected]0db124b02012-11-07 04:55:052693}
[email protected]fdd679b2012-11-15 20:49:392694
[email protected]399583b2012-12-11 09:33:422695void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:452696 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
2697 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:422698 for (ExtensionSet::const_iterator it = extensions_.begin();
2699 it != extensions_.end(); ++it) {
[email protected]cadac622013-06-11 16:46:362700 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
2701 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
2702 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:422703 }
2704 }
2705
[email protected]9f4e4f082013-06-21 07:11:192706 DCHECK(!installs_delayed_for_gc());
2707 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:422708 BrowserContext::GarbageCollectStoragePartitions(
2709 profile_, active_paths.Pass(),
2710 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
2711 AsWeakPtr()));
2712}
2713
2714void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:192715 set_installs_delayed_for_gc(false);
2716 MaybeFinishDelayedInstallations();
2717}
2718
2719void ExtensionService::MaybeFinishDelayedInstallations() {
2720 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422721 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2722 it != delayed_installs_.end();
2723 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192724 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422725 }
[email protected]9f4e4f082013-06-21 07:11:192726 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2727 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422728 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192729 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422730 }
[email protected]399583b2012-12-11 09:33:422731}
2732
2733void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
2734 extension_prefs_->SetNeedsStorageGarbageCollection(true);
2735}
2736
[email protected]fdd679b2012-11-15 20:49:392737void ExtensionService::OnBlacklistUpdated() {
[email protected]48a359342013-10-30 00:22:002738 blacklist_->GetMalwareIDs(
[email protected]695b5712012-12-06 23:55:282739 GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252740 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282741}
2742
[email protected]3f2a2fa2013-09-24 02:55:252743void ExtensionService::ManageBlacklist(const std::set<std::string>& updated) {
[email protected]695b5712012-12-06 23:55:282744 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2745
[email protected]3f2a2fa2013-09-24 02:55:252746 std::set<std::string> before = blacklisted_extensions_.GetIDs();
[email protected]16bf7ba72013-08-23 11:52:542747 std::set<std::string> no_longer_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252748 base::STLSetDifference<std::set<std::string> >(before, updated);
[email protected]16bf7ba72013-08-23 11:52:542749 std::set<std::string> not_yet_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252750 base::STLSetDifference<std::set<std::string> >(updated, before);
[email protected]695b5712012-12-06 23:55:282751
2752 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
2753 it != no_longer_blacklisted.end(); ++it) {
2754 scoped_refptr<const Extension> extension =
2755 blacklisted_extensions_.GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252756 if (!extension.get()) {
2757 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2758 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282759 continue;
[email protected]3f2a2fa2013-09-24 02:55:252760 }
[email protected]695b5712012-12-06 23:55:282761 blacklisted_extensions_.Remove(*it);
[email protected]3f2a2fa2013-09-24 02:55:252762 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092763 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092764 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092765 extension->location(),
2766 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282767 }
2768
2769 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
2770 it != not_yet_blacklisted.end(); ++it) {
2771 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252772 if (!extension.get()) {
2773 NOTREACHED() << "Extension " << *it << " needs to be "
2774 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282775 continue;
[email protected]3f2a2fa2013-09-24 02:55:252776 }
[email protected]695b5712012-12-06 23:55:282777 blacklisted_extensions_.Insert(extension);
[email protected]3f2a2fa2013-09-24 02:55:252778 extension_prefs_->SetExtensionBlacklisted(extension->id(), true);
[email protected]b0af4792013-10-23 09:12:132779 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092780 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2781 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282782 }
2783
2784 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:392785}
[email protected]75bdcb872013-03-13 00:41:452786
2787void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2788 update_observers_.AddObserver(observer);
2789}
2790
2791void ExtensionService::RemoveUpdateObserver(
2792 extensions::UpdateObserver* observer) {
2793 update_observers_.RemoveObserver(observer);
2794}