blob: 89c8b2938efb31bf4fc78fdbf29d87bf51c10ee6 [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]15730c42009-09-03 00:03:2029#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3630#include "chrome/browser/chrome_notification_types.h"
[email protected]ad445762013-01-23 00:47:4431#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]4636c832013-01-11 02:10:1132#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0133#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1734#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3835#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4436#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2437#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1838#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5639#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2240#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]19eb80152011-02-26 00:28:4341#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]f8aefb132013-10-30 09:29:5242#include "chrome/browser/extensions/extension_sync_service.h"
[email protected]31d8f5f22012-04-02 15:22:0843#include "chrome/browser/extensions/extension_system.h"
[email protected]617342a42013-12-18 23:34:0344#include "chrome/browser/extensions/extension_util.h"
[email protected]612a1cb12012-10-17 13:18:0345#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2746#include "chrome/browser/extensions/external_provider_impl.h"
[email protected]ffd2f79e2013-11-14 00:11:4647#include "chrome/browser/extensions/install_verifier.h"
[email protected]d8c8f25f2011-11-02 18:18:0148#include "chrome/browser/extensions/installed_loader.h"
[email protected]c333e792012-01-06 16:57:3949#include "chrome/browser/extensions/permissions_updater.h"
[email protected]d8c8f25f2011-11-02 18:18:0150#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1151#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3352#include "chrome/browser/profiles/profile.h"
[email protected]b5a507b22013-11-08 20:41:5753#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1754#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3155#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1556#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5457#include "chrome/common/chrome_switches.h"
[email protected]f6431be82013-09-07 02:53:4558#include "chrome/common/crash_keys.h"
[email protected]9f4e4f082013-06-21 07:11:1959#include "chrome/common/extensions/extension_constants.h"
[email protected]7c927b62010-02-24 09:54:1360#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5661#include "chrome/common/extensions/extension_messages.h"
[email protected]15d4d2d2013-08-09 06:49:0362#include "chrome/common/extensions/features/feature_channel.h"
[email protected]561e33d52013-04-03 06:58:4363#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]6b414c232013-06-05 07:53:3464#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]65348062013-01-15 07:27:2265#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]25b34332009-06-05 21:53:1966#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1467#include "chrome/common/url_constants.h"
[email protected]7a83dd072013-09-03 12:09:1768#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]c38831a12011-10-28 12:44:4969#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3670#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3771#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1672#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3073#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4274#include "content/public/browser/site_instance.h"
75#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:5376#include "content/public/browser/url_data_source.h"
[email protected]74474042013-11-21 12:03:5477#include "extensions/browser/app_sorting.h"
[email protected]34423532013-11-21 18:13:1078#include "extensions/browser/event_router.h"
[email protected]45928b682013-11-28 08:20:2779#include "extensions/browser/extensions_browser_client.h"
[email protected]301116c62013-11-26 10:37:4580#include "extensions/browser/external_provider_interface.h"
81#include "extensions/browser/management_policy.h"
[email protected]b52f8ca2013-11-28 08:25:2282#include "extensions/browser/pending_extension_manager.h"
[email protected]98b6d942013-11-10 00:34:0783#include "extensions/browser/process_manager.h"
[email protected]a9aa62b312013-11-29 05:35:0684#include "extensions/browser/update_observer.h"
[email protected]836e2982013-05-16 08:07:4285#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:3186#include "extensions/common/error_utils.h"
[email protected]e4452d32013-11-15 23:07:4187#include "extensions/common/extension.h"
[email protected]c81f5d32013-10-26 10:38:4288#include "extensions/common/extensions_client.h"
[email protected]5ef835a2013-11-08 20:42:5789#include "extensions/common/feature_switch.h"
[email protected]d42c11152013-08-22 19:36:3290#include "extensions/common/manifest.h"
[email protected]0c3c9732013-09-16 08:53:4191#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5192#include "extensions/common/manifest_handlers/background_info.h"
[email protected]1f7de252013-11-06 22:02:0093#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]301116c62013-11-26 10:37:4594#include "extensions/common/manifest_handlers/shared_module_info.h"
[email protected]c41003472013-10-19 15:37:2595#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4196#include "extensions/common/permissions/permissions_data.h"
[email protected]65187152012-06-02 13:14:1497#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:2598#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]cd67ed52013-10-15 01:22:1399#include "ui/base/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:00100#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38101#include "webkit/browser/database/database_tracker.h"
102#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18103
[email protected]eed367e2011-04-12 03:43:31104#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18105#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32106#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07107#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31108#endif
109
[email protected]55eb70e762012-02-20 17:38:39110using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39111using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36112using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27113using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34114using extensions::Extension;
115using extensions::ExtensionIdSet;
116using extensions::ExtensionInfo;
[email protected]289c44b2013-12-17 03:26:57117using extensions::ExtensionSet;
[email protected]215a7be2012-10-22 19:53:42118using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46119using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36120using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40121using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06122using extensions::PermissionMessage;
123using extensions::PermissionMessages;
124using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19125using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24126using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05127
[email protected]0c3c9732013-09-16 08:53:41128namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06129
[email protected]b6ab96d2009-08-20 18:58:19130namespace {
131
[email protected]d96eb512012-11-01 23:44:08132// Histogram values for logging events related to externally installed
133// extensions.
134enum ExternalExtensionEvent {
135 EXTERNAL_EXTENSION_INSTALLED = 0,
136 EXTERNAL_EXTENSION_IGNORED,
137 EXTERNAL_EXTENSION_REENABLED,
138 EXTERNAL_EXTENSION_UNINSTALLED,
139 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
140};
141
[email protected]612a1cb12012-10-17 13:18:03142// Prompt the user this many times before considering an extension acknowledged.
143static const int kMaxExtensionAcknowledgePromptCount = 3;
144
[email protected]0db124b02012-11-07 04:55:05145// Wait this many seconds after an extensions becomes idle before updating it.
146static const int kUpdateIdleDelay = 5;
147
[email protected]9bd9a6862012-11-29 09:24:22148// Wait this many seconds before trying to garbage collect extensions again.
149static const int kGarbageCollectRetryDelay = 30;
150
[email protected]36fc54f2013-06-14 02:49:34151// Wait this many seconds after startup to see if there are any extensions
152// which can be garbage collected.
153static const int kGarbageCollectStartupDelay = 30;
154
[email protected]9f4e4f082013-06-21 07:11:19155static bool IsSharedModule(const Extension* extension) {
156 return SharedModuleInfo::IsSharedModule(extension);
157}
158
[email protected]4c9201c42013-08-16 04:56:21159static bool IsCWSSharedModule(const Extension* extension) {
160 return extension->from_webstore() && IsSharedModule(extension);
161}
162
163class SharedModuleProvider : public extensions::ManagementPolicy::Provider {
164 public:
165 SharedModuleProvider() {}
166 virtual ~SharedModuleProvider() {}
167
168 virtual std::string GetDebugPolicyProviderName() const OVERRIDE {
169 return "SharedModuleProvider";
170 }
171
172 virtual bool UserMayModifySettings(const Extension* extension,
[email protected]439f1e32013-12-09 20:09:09173 base::string16* error) const OVERRIDE {
[email protected]4c9201c42013-08-16 04:56:21174 return !IsCWSSharedModule(extension);
175 }
176
177 virtual bool MustRemainEnabled(const Extension* extension,
[email protected]439f1e32013-12-09 20:09:09178 base::string16* error) const OVERRIDE {
[email protected]4c9201c42013-08-16 04:56:21179 return IsCWSSharedModule(extension);
180 }
181
182 private:
183 DISALLOW_COPY_AND_ASSIGN(SharedModuleProvider);
184};
185
186
[email protected]c6d474f82009-12-16 21:11:06187} // namespace
[email protected]b6ab96d2009-08-20 18:58:19188
[email protected]eaa7dd182010-12-14 11:09:00189ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49190 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57191 being_upgraded(false),
192 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49193}
194
[email protected]eaa7dd182010-12-14 11:09:00195ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49196}
197
[email protected]eaa7dd182010-12-14 11:09:00198// ExtensionService.
[email protected]6014d672008-12-05 00:38:25199
[email protected]8e4560b62011-01-14 10:09:14200void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12201 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20202
[email protected]7a4c6852010-09-16 03:44:22203 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27204 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21205 for (i = external_extension_providers_.begin();
206 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14207 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21208 if (i->get()->HasExtension(id))
209 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22210 }
211
[email protected]0f48fca2011-05-19 18:46:35212 // We get the list of external extensions to check from preferences.
213 // It is possible that an extension has preferences but is not loaded.
214 // For example, an extension that requires experimental permissions
215 // will not be loaded if the experimental command line flag is not used.
216 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40217 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35218 // We can't call UninstallExtension with an unloaded/invalid
219 // extension ID.
220 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
221 << "with id: " << id;
222 return;
223 }
[email protected]d6ebc9792011-04-07 18:18:33224 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22225}
226
[email protected]7f8f24f2012-11-15 19:40:14227void ExtensionService::SetFileTaskRunnerForTesting(
228 base::SequencedTaskRunner* task_runner) {
229 file_task_runner_ = task_runner;
230}
231
[email protected]8e4560b62011-01-14 10:09:14232void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22233 external_extension_providers_.clear();
234}
235
[email protected]8e4560b62011-01-14 10:09:14236void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27237 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12238 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21239 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27240 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22241}
242
[email protected]9060d8b02012-01-13 02:14:30243bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22244 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42245 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48246 Manifest::Location location,
247 int creation_flags,
248 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38249 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
250 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20251
[email protected]8a87a5332011-08-11 17:54:59252 const Extension* extension = GetExtensionById(id, true);
253 if (extension) {
254 // Already installed. Skip this install if the current location has
255 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40256 Manifest::Location current = extension->location();
257 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30258 return false;
[email protected]8a87a5332011-08-11 17:54:59259 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22260 }
[email protected]9060d8b02012-01-13 02:14:30261
262 // Add |id| to the set of pending extensions. If it can not be added,
263 // then there is already a pending record from a higher-priority install
264 // source. In this case, signal that this extension will not be
265 // installed by returning false.
266 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]464213a2013-10-15 01:06:48267 id, update_url, location, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30268 return false;
[email protected]31bb5ee62012-09-12 22:58:40269 }
[email protected]9060d8b02012-01-13 02:14:30270
[email protected]94fde232012-04-27 10:22:30271 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30272 return true;
[email protected]7a4c6852010-09-16 03:44:22273}
274
[email protected]400c4392013-12-13 14:41:58275const Extension* ExtensionService::GetInstalledExtensionByUrl(
276 const GURL& url) const {
277 return extensions_.GetExtensionOrAppByURL(url);
278}
279
[email protected]dc9a74f72012-08-17 18:07:21280const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]400c4392013-12-13 14:41:58281 const Extension* extension = GetInstalledExtensionByUrl(url);
[email protected]dc9a74f72012-08-17 18:07:21282 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09283}
284
[email protected]dc9a74f72012-08-17 18:07:21285bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09286 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27287}
288
[email protected]6aeac8342010-10-01 20:21:18289// static
[email protected]d6ebc9792011-04-07 18:18:33290// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30291// --uninstall-extension, and to uninstall an extension via sync. The LOG
292// statements within this function are used to inform the user if the uninstall
293// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00294bool ExtensionService::UninstallExtensionHelper(
295 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18296 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33297 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40298 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18299 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33300 << "id: " << extension_id;
301 return false;
[email protected]6aeac8342010-10-01 20:21:18302 }
303
[email protected]d6ebc9792011-04-07 18:18:33304 // The following call to UninstallExtension will not allow an uninstall of a
305 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09306 base::string16 error;
[email protected]d6ebc9792011-04-07 18:18:33307 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
308 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
309 << ": " << error;
310 return false;
311 }
[email protected]95da88c42011-03-31 10:07:33312
[email protected]6aeac8342010-10-01 20:21:18313 return true;
314}
315
[email protected]eaa7dd182010-12-14 11:09:00316ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24317 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45318 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23319 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39320 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03321 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35322 bool extensions_enabled,
323 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39324 : extensions::Blacklist::Observer(blacklist),
325 profile_(profile),
[email protected]bd306722012-07-11 20:43:59326 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43327 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28328 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38329 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]f8aefb132013-10-30 09:29:52330 extension_sync_service_(NULL),
[email protected]9c009092013-05-01 03:14:09331 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23332 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03333 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13334 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33335 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35336 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18337 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54338 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19339 installs_delayed_for_gc_(false),
[email protected]f8aefb132013-10-30 09:29:52340 is_first_run_(false) {
[email protected]c04fd3dd2013-09-05 08:20:10341#if defined(OS_CHROMEOS)
342 disable_garbage_collection_ = false;
343#endif
[email protected]a29a517a2011-01-21 21:11:12344 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20345
[email protected]36a784c2009-06-23 06:21:08346 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02347 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
348 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23349 extensions_enabled_ = false;
[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(),
[email protected]55e16cd2013-12-18 04:36:08387 g_browser_process->local_state(),
388 profile));
[email protected]8e4560b62011-01-14 10:09:14389
[email protected]0436b102011-04-15 18:30:03390 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25391 extensions::ExternalProviderImpl::CreateExternalProviders(
392 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05393 }
[email protected]b671760b2010-07-15 21:13:47394
[email protected]74474042013-11-21 12:03:54395 // Set this as the ExtensionService for app sorting to ensure it causes syncs
396 // if required.
[email protected]b3aa7182013-04-25 04:45:23397 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
398
[email protected]9ce11d22012-08-08 23:20:13399#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58400 extension_action_storage_manager_.reset(
401 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13402#endif
[email protected]c77f2352012-08-08 22:07:58403
[email protected]4c9201c42013-08-16 04:56:21404 shared_module_policy_provider_.reset(new SharedModuleProvider);
405
[email protected]cb0e50312011-05-09 15:03:07406 // How long is the path to the Extensions directory?
407 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
408 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25409}
410
[email protected]84df8332011-12-06 18:22:46411const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45412 return &extensions_;
413}
414
[email protected]84df8332011-12-06 18:22:46415const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45416 return &disabled_extensions_;
417}
418
[email protected]84df8332011-12-06 18:22:46419const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20420 return &terminated_extensions_;
421}
422
[email protected]695b5712012-12-06 23:55:28423const ExtensionSet* ExtensionService::blacklisted_extensions() const {
424 return &blacklisted_extensions_;
425}
426
[email protected]9e9c1d12013-07-31 01:58:12427const ExtensionSet* ExtensionService::delayed_installs() const {
428 return &delayed_installs_;
429}
430
[email protected]620b5a62013-11-18 04:21:08431scoped_ptr<ExtensionSet>
[email protected]695b5712012-12-06 23:55:28432 ExtensionService::GenerateInstalledExtensionsSet() const {
433 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01434 installed_extensions->InsertAll(extensions_);
435 installed_extensions->InsertAll(disabled_extensions_);
436 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28437 installed_extensions->InsertAll(blacklisted_extensions_);
[email protected]620b5a62013-11-18 04:21:08438 return installed_extensions.PassAs<ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01439}
440
[email protected]3f213ad2012-07-26 23:39:41441extensions::PendingExtensionManager*
442 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37443 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45444}
445
[email protected]eaa7dd182010-12-14 11:09:00446ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17447 // No need to unload extensions here because they are profile-scoped, and the
448 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17449
[email protected]5df038b2012-07-16 19:03:27450 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14451 for (i = external_extension_providers_.begin();
452 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27453 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14454 provider->ServiceShutdown();
455 }
[email protected]6014d672008-12-05 00:38:25456}
457
[email protected]037228a2012-10-05 01:36:16458void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21459 system_->management_policy()->UnregisterProvider(
460 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16461}
462
[email protected]b2907fd2011-03-25 16:43:37463const Extension* ExtensionService::GetExtensionById(
464 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47465 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28466 if (include_disabled) {
467 // Include blacklisted extensions here because there are hundreds of
468 // callers of this function, and many might assume that this includes those
469 // that have been disabled due to blacklisting.
[email protected]d2fff5d2013-12-11 02:49:19470 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
[email protected]695b5712012-12-06 23:55:28471 }
472 return GetExtensionById(id, include_mask);
473}
474
[email protected]399583b2012-12-11 09:33:42475GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
476 return content::SiteInstance::GetSiteForURL(
477 profile_,
478 Extension::GetBaseURLFromExtensionId(extension_id));
479}
480
[email protected]695b5712012-12-06 23:55:28481const Extension* ExtensionService::GetExtensionById(
482 const std::string& id, int include_mask) const {
483 std::string lowercase_id = StringToLowerASCII(id);
484 if (include_mask & INCLUDE_ENABLED) {
485 const Extension* extension = extensions_.GetByID(lowercase_id);
486 if (extension)
487 return extension;
488 }
489 if (include_mask & INCLUDE_DISABLED) {
490 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
491 if (extension)
492 return extension;
493 }
494 if (include_mask & INCLUDE_TERMINATED) {
495 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
496 if (extension)
497 return extension;
498 }
499 if (include_mask & INCLUDE_BLACKLISTED) {
500 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
501 if (extension)
502 return extension;
503 }
504 return NULL;
[email protected]78994ab02010-12-08 18:06:44505}
506
[email protected]eaa7dd182010-12-14 11:09:00507void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12508 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20509
[email protected]3350d712013-11-18 09:32:24510 base::Time begin_time = base::Time::Now();
511
[email protected]4a10006a2013-05-17 23:18:35512 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17513 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32514
[email protected]820d9bd2013-04-03 03:46:03515 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03516 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
517 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
518 // The sole purpose of this launch is to install a new extension from CWS
519 // and immediately terminate: loading already installed extensions is
520 // unnecessary and may interfere with the inline install dialog (e.g. if an
521 // extension listens to onStartup and opens a window).
522 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25523 } else {
[email protected]47b60072013-06-20 10:35:56524 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
525 component_loader_->LoadAll();
526 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19527
[email protected]59ee99d2013-10-11 15:46:16528 ReconcileKnownDisabled();
529
[email protected]2edd57852013-08-14 22:07:40530 // Attempt to re-enable extensions whose only disable reason is reloading.
531 std::vector<std::string> extensions_to_enable;
532 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
533 iter != disabled_extensions_.end(); ++iter) {
534 const Extension* e = iter->get();
535 if (extension_prefs_->GetDisableReasons(e->id()) ==
536 Extension::DISABLE_RELOAD) {
537 extensions_to_enable.push_back(e->id());
538 }
539 }
540 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
541 it != extensions_to_enable.end(); ++it) {
542 EnableExtension(*it);
543 }
544
[email protected]9f4e4f082013-06-21 07:11:19545 // Finish install (if possible) of extensions that were still delayed while
546 // the browser was shut down.
547 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
548 extension_prefs_->GetAllDelayedInstallInfo());
549 for (size_t i = 0; i < delayed_info->size(); ++i) {
550 ExtensionInfo* info = delayed_info->at(i).get();
551 scoped_refptr<const Extension> extension(NULL);
552 if (info->extension_manifest) {
553 std::string error;
554 extension = Extension::Create(
555 info->extension_path,
556 info->extension_location,
557 *info->extension_manifest,
558 extension_prefs_->GetDelayedInstallCreationFlags(
559 info->extension_id),
560 info->extension_id,
561 &error);
562 if (extension.get())
563 delayed_installs_.Insert(extension);
564 }
565 }
566 MaybeFinishDelayedInstallations();
567
568 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
569 extension_prefs_->GetAllDelayedInstallInfo());
570 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
571 delayed_info2->size() - delayed_info->size());
572
[email protected]47b60072013-06-20 10:35:56573 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25574
[email protected]47b60072013-06-20 10:35:56575 // TODO(erikkay) this should probably be deferred to a future point
576 // rather than running immediately at startup.
577 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57578
[email protected]68e5cc72013-12-17 02:41:53579 InstallVerifier* verifier =
580 extensions::ExtensionSystem::Get(profile_)->install_verifier();
581 if (verifier->NeedsBootstrap())
582 VerifyAllExtensions();
583
[email protected]47b60072013-06-20 10:35:56584 base::MessageLoop::current()->PostDelayedTask(
585 FROM_HERE,
586 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
587 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42588
[email protected]820d9bd2013-04-03 03:46:03589 if (extension_prefs_->NeedsStorageGarbageCollection()) {
590 GarbageCollectIsolatedStorage();
591 extension_prefs_->SetNeedsStorageGarbageCollection(false);
592 }
[email protected]4c9201c42013-08-16 04:56:21593 system_->management_policy()->RegisterProvider(
594 shared_module_policy_provider_.get());
[email protected]399583b2012-12-11 09:33:42595 }
[email protected]3350d712013-11-18 09:32:24596
597 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
598 base::Time::Now() - begin_time);
[email protected]6014d672008-12-05 00:38:25599}
600
[email protected]68e5cc72013-12-17 02:41:53601void ExtensionService::VerifyAllExtensions() {
602 ExtensionIdSet to_add;
603 scoped_ptr<ExtensionSet> all_extensions = GenerateInstalledExtensionsSet();
604
605 for (ExtensionSet::const_iterator i = all_extensions->begin();
606 i != all_extensions->end(); ++i) {
607 const Extension& extension = **i;
608
609 if (extensions::ManifestURL::UpdatesFromGallery(&extension) &&
610 extension.is_extension())
611 to_add.insert(extension.id());
612 }
613 extensions::ExtensionSystem::Get(profile_)->install_verifier()->AddMany(
614 to_add, base::Bind(&ExtensionService::FinishVerifyAllExtensions,
615 AsWeakPtr()));
616}
617
618void ExtensionService::FinishVerifyAllExtensions(bool success) {
619 if (success) {
620 // Check to see if any currently unverified extensions became verified.
621 InstallVerifier* verifier =
622 extensions::ExtensionSystem::Get(profile_)->install_verifier();
623 for (ExtensionSet::const_iterator i = disabled_extensions_.begin();
624 i != disabled_extensions_.end(); ++i) {
625 const Extension& extension = **i;
626 int disable_reasons = extension_prefs_->GetDisableReasons(extension.id());
627 if (disable_reasons & Extension::DISABLE_NOT_VERIFIED &&
628 !verifier->MustRemainDisabled(&extension, NULL, NULL)) {
629 extension_prefs_->RemoveDisableReason(extension.id(),
630 Extension::DISABLE_NOT_VERIFIED);
631 // Notify interested observers (eg the extensions settings page) by
632 // sending an UNLOADED notification.
633 //
634 // TODO(asargent) - this is a slight hack because it's already
635 // disabled; the right solution might be to add a separate listener
636 // interface for DisableReason's changing. http://crbug.com/328916
637 UnloadedExtensionInfo details(&extension,
638 UnloadedExtensionInfo::REASON_DISABLE);
639 content::NotificationService::current()->Notify(
640 chrome::NOTIFICATION_EXTENSION_UNLOADED,
641 content::Source<Profile>(profile_),
642 content::Details<UnloadedExtensionInfo>(&details));
643 }
644 }
645 // Might disable some extensions.
646 CheckManagementPolicy();
647 }
648}
649
[email protected]31bb5ee62012-09-12 22:58:40650bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45651 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40652 const GURL& download_url,
653 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12654 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54655 if (browser_terminating_) {
656 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
657 // Leak the temp file at extension_path. We don't want to add to the disk
658 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
659 // the file is in the OS temp directory which should be cleaned up for us.
660 return false;
661 }
[email protected]a8af9fdb2010-10-28 21:52:20662
[email protected]3f213ad2012-07-26 23:39:41663 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06664 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32665
[email protected]695b5712012-12-06 23:55:28666 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06667 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33668 LOG(WARNING) << "Will not update extension " << id
669 << " because it is not installed or pending";
670 // Delete extension_path since we're not creating a CrxInstaller
671 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14672 if (!GetFileTaskRunner()->PostTask(
673 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36674 base::Bind(
[email protected]7296f2762011-11-21 19:23:44675 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36676 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03677
678 return false;
[email protected]e957fe52009-06-23 16:51:05679 }
680
[email protected]aa142702010-03-26 01:26:33681 // We want a silent install only for non-pending extensions and
682 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37683 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05684 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37685 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33686
[email protected]f8636f92013-08-09 21:02:37687 scoped_refptr<CrxInstaller> installer(
688 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16689 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48690 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06691 if (pending_extension_info) {
692 installer->set_install_source(pending_extension_info->install_source());
693 if (pending_extension_info->install_silently())
694 installer->set_allow_silent_install(true);
[email protected]464213a2013-10-15 01:06:48695 creation_flags = pending_extension_info->creation_flags();
696 if (pending_extension_info->mark_acknowledged())
697 AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06698 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41699 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06700 }
[email protected]7d8b7072012-04-07 01:07:46701 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45702 // its auto-update URL is from the webstore, treat it as a webstore install.
703 // Note that we ignore some older extensions with blank auto-update URLs
704 // because we are mostly concerned with restrictions on NaCl extensions,
705 // which are newer.
[email protected]75764512011-12-19 19:54:28706 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38707 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35708 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06709 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53710 creation_flags |= Extension::FROM_WEBSTORE;
711 }
712
713 // Bookmark apps being updated is kind of a contradiction, but that's because
714 // we mark the default apps as bookmark apps, and they're hosted in the web
715 // store, thus they can get updated. See http://crbug.com/101605 for more
716 // details.
717 if (extension && extension->from_bookmark())
718 creation_flags |= Extension::FROM_BOOKMARK;
719
[email protected]e33bbc22012-08-27 22:05:46720 if (extension && extension->was_installed_by_default())
721 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
722
[email protected]a12ce8b22012-01-17 18:40:53723 installer->set_creation_flags(creation_flags);
724
[email protected]6dfbbf82010-03-12 23:09:16725 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06726 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07727 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16728 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03729
730 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09731 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03732
733 return true;
[email protected]e957fe52009-06-23 16:51:05734}
735
[email protected]e178ad92013-06-28 02:29:25736void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12737 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05738
739 // If the extension is already reloading, don't reload again.
740 if (extension_prefs_->GetDisableReasons(extension_id) &
741 Extension::DISABLE_RELOAD) {
742 return;
743 }
744
[email protected]650b2d52013-02-10 03:41:45745 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02746 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40747
[email protected]f17dbd42010-08-16 23:21:10748 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06749 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29750 // If the extension has an inspector open for its background page, detach
751 // the inspector and hang onto a cookie for it, so that we can reattach
752 // later.
[email protected]31d8f5f22012-04-02 15:22:08753 // TODO(yoz): this is not incognito-safe!
[email protected]98b6d942013-11-10 00:34:07754 extensions::ProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13755 extensions::ExtensionHost* host =
756 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36757 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29758 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09759 scoped_refptr<DevToolsAgentHost> agent_host =
760 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
761 agent_host->DisconnectRenderViewHost();
762 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29763 }
764
[email protected]b65272f2009-08-31 15:47:06765 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50766 // BeingUpgraded is set back to false when the extension is added.
767 SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03768 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03769 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16770 } else {
771 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06772 }
773
[email protected]9f4e4f082013-06-21 07:11:19774 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48775 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05776 return;
777 }
778
[email protected]43ceb002012-02-10 23:19:15779 // If we're reloading a component extension, use the component extension
780 // loader's reloader.
781 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25782 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15783 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25784 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15785 return;
786 }
787
[email protected]e6090e42010-03-23 22:44:08788 // Check the installed extensions to see if what we're reloading was already
789 // installed.
[email protected]e178ad92013-06-28 02:29:25790 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08791 scoped_ptr<ExtensionInfo> installed_extension(
792 extension_prefs_->GetInstalledExtensionInfo(extension_id));
793 if (installed_extension.get() &&
794 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01795 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08796 } else {
[email protected]d8c8f25f2011-11-02 18:18:01797 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08798 // We should always be able to remember the extension's path. If it's not in
799 // the map, someone failed to update |unloaded_extension_paths_|.
800 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01801 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08802 }
[email protected]e178ad92013-06-28 02:29:25803 // When reloading is done, mark this extension as done reloading.
804 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40805}
806
[email protected]fa2416f2011-05-03 08:41:20807bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08808 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20809 bool external_uninstall,
[email protected]439f1e32013-12-09 20:09:09810 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12811 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20812
[email protected]0d6ec3a72011-09-02 02:09:43813 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25814
[email protected]e7afe2452010-08-22 16:19:13815 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09816 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32817
[email protected]95da88c42011-03-31 10:07:33818 // Policy change which triggers an uninstall will always set
819 // |external_uninstall| to true so this is the only way to uninstall
820 // managed extensions.
[email protected]65187152012-06-02 13:14:14821 if (!external_uninstall &&
822 !system_->management_policy()->UserMayModifySettings(
823 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07824 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27825 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53826 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09827 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33828 return false;
829 }
[email protected]95da88c42011-03-31 10:07:33830
[email protected]65f173552012-06-28 22:43:58831 syncer::SyncChange sync_change;
[email protected]f8aefb132013-10-30 09:29:52832 if (extension_sync_service_) {
833 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
834 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30835 }
836
[email protected]ffd2f79e2013-11-14 00:11:46837 extensions::ExtensionSystem::Get(profile_)->install_verifier()->Remove(
838 extension->id());
839
[email protected]dc24976f2013-06-02 21:15:09840 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08841 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
842 EXTERNAL_EXTENSION_UNINSTALLED,
843 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02844 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
845 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
846 EXTERNAL_EXTENSION_UNINSTALLED,
847 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
848 } else {
849 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
850 EXTERNAL_EXTENSION_UNINSTALLED,
851 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
852 }
[email protected]d96eb512012-11-01 23:44:08853 }
[email protected]9b217652010-10-08 22:04:23854 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08855 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09856 RecordPermissionMessagesHistogram(extension.get(),
857 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23858
[email protected]831aa212010-03-26 13:55:19859 // Unload before doing more cleanup to ensure that nothing is hanging on to
860 // any of these resources.
[email protected]b0af4792013-10-23 09:12:13861 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19862
[email protected]9f1087e2009-06-15 17:29:32863 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05864 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14865 if (!GetFileTaskRunner()->PostTask(
866 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36867 base::Bind(
[email protected]14908b72011-04-20 06:54:36868 &extension_file_util::UninstallExtension,
869 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20870 extension_id)))
[email protected]14908b72011-04-20 06:54:36871 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32872 }
873
[email protected]6b414c232013-06-05 07:53:34874 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36875 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43876 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09877 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43878
[email protected]399583b2012-12-11 09:33:42879 if (is_storage_isolated) {
880 BrowserContext::AsyncObliterateStoragePartition(
881 profile_,
882 GetSiteForExtensionId(extension_id),
883 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
884 AsWeakPtr()));
885 } else {
886 if (extension->is_hosted_app() &&
887 !profile_->GetExtensionSpecialStoragePolicy()->
888 IsStorageProtected(launch_web_url_origin)) {
889 extensions::DataDeleter::StartDeleting(
890 profile_, extension_id, launch_web_url_origin);
891 }
892 extensions::DataDeleter::StartDeleting(profile_, extension_id,
893 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43894 }
[email protected]0d6ec3a72011-09-02 02:09:43895
[email protected]0dfe05c2011-02-23 23:03:36896 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06897
898 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07899 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27900 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53901 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09902 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33903
[email protected]f8aefb132013-10-30 09:29:52904 if (extension_sync_service_) {
905 extension_sync_service_->ProcessSyncUninstallExtension(extension_id,
906 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30907 }
908
[email protected]399583b2012-12-11 09:33:42909 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05910
[email protected]198b5902013-06-27 10:36:11911 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19912
[email protected]d4eda592013-09-18 03:37:57913 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
914 external_uninstall);
915
[email protected]333b1de2011-09-12 18:28:50916 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18917 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
918
[email protected]d6ebc9792011-04-07 18:18:33919 return true;
[email protected]c10da4b02010-03-25 14:38:32920}
921
[email protected]c3cfb012011-04-06 22:07:35922bool ExtensionService::IsExtensionEnabled(
923 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46924 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21925 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18926 return true;
[email protected]dc9a74f72012-08-17 18:07:21927 }
[email protected]36429da2011-07-11 20:25:18928
[email protected]695b5712012-12-06 23:55:28929 if (disabled_extensions_.Contains(extension_id) ||
930 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25931 return false;
[email protected]695b5712012-12-06 23:55:28932 }
[email protected]ad83ca242011-07-29 01:32:25933
934 // If the extension hasn't been loaded yet, check the prefs for it. Assume
935 // enabled unless otherwise noted.
936 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31937 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35938}
939
940bool ExtensionService::IsExternalExtensionUninstalled(
941 const std::string& extension_id) const {
942 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
943}
944
[email protected]eaa7dd182010-12-14 11:09:00945void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12946 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20947
[email protected]06f92562011-04-29 19:27:31948 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39949 return;
[email protected]0d54b682013-11-05 14:15:36950 const Extension* extension = disabled_extensions_.GetByID(extension_id);
951
952 ManagementPolicy* policy = system_->management_policy();
953 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
954 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
955 return;
956 }
[email protected]0c6da502009-08-14 22:32:39957
[email protected]b3317ad2011-04-28 23:46:00958 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28959 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52960
[email protected]06f92562011-04-29 19:27:31961 // This can happen if sync enables an extension that is not
962 // installed yet.
963 if (!extension)
964 return;
965
[email protected]d96eb512012-11-01 23:44:08966 if (IsUnacknowledgedExternalExtension(extension)) {
967 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
968 EXTERNAL_EXTENSION_REENABLED,
969 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02970 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
971 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
972 EXTERNAL_EXTENSION_REENABLED,
973 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
974 } else {
975 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
976 EXTERNAL_EXTENSION_REENABLED,
977 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
978 }
[email protected]612a1cb12012-10-17 13:18:03979 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08980 }
[email protected]612a1cb12012-10-17 13:18:03981
[email protected]0c6da502009-08-14 22:32:39982 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46983 extensions_.Insert(make_scoped_refptr(extension));
[email protected]195c1582013-10-29 18:42:23984 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39985
[email protected]62d30f42009-10-01 22:36:06986 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30987
[email protected]f74d7f32012-06-19 19:22:51988 // Notify listeners that the extension was enabled.
989 content::NotificationService::current()->Notify(
990 chrome::NOTIFICATION_EXTENSION_ENABLED,
991 content::Source<Profile>(profile_),
992 content::Details<const Extension>(extension));
993
[email protected]f8aefb132013-10-30 09:29:52994 if (extension_sync_service_)
995 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:39996}
997
[email protected]44d62b62012-04-11 00:06:03998void ExtensionService::DisableExtension(
999 const std::string& extension_id,
1000 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:121001 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201002
[email protected]b2ba9962009-12-10 20:10:151003 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:311004 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:521005 return;
[email protected]1784e83a2009-09-08 21:01:521006
[email protected]06f92562011-04-29 19:27:311007 const Extension* extension = GetInstalledExtension(extension_id);
1008 // |extension| can be NULL if sync disables an extension that is not
1009 // installed yet.
[email protected]65187152012-06-02 13:14:141010 if (extension &&
[email protected]505ccdc2013-06-20 20:15:351011 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:141012 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:331013 return;
[email protected]65187152012-06-02 13:14:141014 }
[email protected]95da88c42011-03-31 10:07:331015
[email protected]b3317ad2011-04-28 23:46:001016 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:281017 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:521018
[email protected]695b5712012-12-06 23:55:281019 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
1020 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:311021 if (!extension)
1022 return;
1023
[email protected]64742de32013-04-22 08:44:341024 // Reset the background_page_ready flag
1025 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
1026 extension_runtime_data_[extension->id()].background_page_ready = false;
1027
[email protected]5c094792012-09-07 19:44:531028 // Move it over to the disabled list. Don't send a second unload notification
1029 // for terminated extensions being disabled.
[email protected]195c1582013-10-29 18:42:231030 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:531031 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461032 extensions_.Remove(extension->id());
[email protected]b0af4792013-10-23 09:12:131033 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:531034 } else {
[email protected]84df8332011-12-06 18:22:461035 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:531036 }
[email protected]3bdba0d2011-08-23 07:17:301037
[email protected]f8aefb132013-10-30 09:29:521038 if (extension_sync_service_)
1039 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:521040}
1041
[email protected]1abf05e2013-07-09 17:04:361042void ExtensionService::DisableUserExtensions(
1043 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:141044 extensions::ManagementPolicy* management_policy =
1045 system_->management_policy();
1046 extensions::ExtensionList to_disable;
1047
1048 for (ExtensionSet::const_iterator extension = extensions_.begin();
1049 extension != extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111050 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141051 to_disable.push_back(*extension);
1052 }
1053 for (ExtensionSet::const_iterator extension = terminated_extensions_.begin();
1054 extension != terminated_extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111055 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141056 to_disable.push_back(*extension);
1057 }
1058
1059 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1060 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381061 if ((*extension)->was_installed_by_default() &&
1062 extension_urls::IsWebstoreUpdateUrl(
1063 extensions::ManifestURL::GetUpdateURL(*extension)))
1064 continue;
[email protected]1abf05e2013-07-09 17:04:361065 const std::string& id = (*extension)->id();
1066 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1067 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141068 }
1069}
1070
[email protected]eaa7dd182010-12-14 11:09:001071void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281072 const Extension* extension) {
1073 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491074 RecordPermissionMessagesHistogram(
1075 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251076 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1077 EnableExtension(extension->id());
1078}
1079
[email protected]009633c2013-03-07 22:08:281080void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451081 CHECK(extension);
1082 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281083 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451084}
1085
[email protected]fe2dd7742011-04-19 22:52:491086// static
1087void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051088 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171089 // Since this is called from multiple sources, and since the histogram macros
1090 // use statics, we need to manually lookup the histogram ourselves.
1091 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491092 histogram,
1093 1,
[email protected]c2e66e12012-06-27 06:27:061094 PermissionMessage::kEnumBoundary,
1095 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171096 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491097
[email protected]13c68b62013-05-17 11:29:051098 PermissionMessages permissions =
1099 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491100 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061101 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491102 } else {
[email protected]c2e66e12012-06-27 06:27:061103 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521104 it != permissions.end(); ++it)
1105 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491106 }
1107}
1108
[email protected]eaa7dd182010-12-14 11:09:001109void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421110 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061111 // was loaded, otherwise a race can arise where a renderer that is created
1112 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421113 // that the request context doesn't yet know about. The profile is responsible
1114 // for ensuring its URLRequestContexts appropriately discover the loaded
1115 // extension.
[email protected]31d8f5f22012-04-02 15:22:081116 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061117
[email protected]d5949312012-12-03 22:13:301118 // Tell renderers about the new extension, unless it's a theme (renderers
1119 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191120 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491121 for (content::RenderProcessHost::iterator i(
1122 content::RenderProcessHost::AllHostsIterator());
1123 !i.IsAtEnd(); i.Advance()) {
1124 content::RenderProcessHost* host = i.GetCurrentValue();
1125 Profile* host_profile =
1126 Profile::FromBrowserContext(host->GetBrowserContext());
1127 if (host_profile->GetOriginalProfile() ==
1128 profile_->GetOriginalProfile()) {
1129 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1130 1, ExtensionMsg_Loaded_Params(extension));
1131 host->Send(
1132 new ExtensionMsg_Loaded(loaded_extensions));
1133 }
[email protected]c8d407e2011-04-28 21:27:171134 }
[email protected]77a6970c2011-04-23 16:58:561135 }
1136
[email protected]3442a662012-01-12 08:06:171137 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1138 // extension.
1139 //
1140 // NOTE: It is important that this happen after notifying the renderers about
1141 // the new extensions so that if we navigate to an extension URL in
1142 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1143 content::NotificationService::current()->Notify(
1144 chrome::NOTIFICATION_EXTENSION_LOADED,
1145 content::Source<Profile>(profile_),
1146 content::Details<const Extension>(extension));
1147
[email protected]c8d407e2011-04-28 21:27:171148 // Tell a random-ass collection of other subsystems about the new extension.
1149 // TODO(aa): What should we do with all this goop? Can it move into the
1150 // relevant objects via EXTENSION_LOADED?
1151
1152 profile_->GetExtensionSpecialStoragePolicy()->
1153 GrantRightsForExtension(extension);
1154
1155 UpdateActiveExtensionsInCrashReporter();
1156
[email protected]c8d407e2011-04-28 21:27:171157 // If the extension has permission to load chrome://favicon/ resources we need
1158 // to make sure that the FaviconSource is registered with the
1159 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051160 if (extensions::PermissionsData::HasHostPermission(
1161 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171162 FaviconSource* favicon_source = new FaviconSource(profile_,
1163 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531164 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171165 }
[email protected]b07e606e2012-09-15 20:16:151166
1167#if !defined(OS_ANDROID)
1168 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051169 if (extensions::PermissionsData::HasHostPermission(
1170 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151171 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531172 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151173 }
1174#endif
1175
[email protected]5eddc3e2011-10-26 04:33:311176 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051177 if (extensions::PermissionsData::HasHostPermission(
1178 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511179 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531180 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311181 }
[email protected]62d30f42009-10-01 22:36:061182}
1183
[email protected]a9f39a312010-12-23 22:14:271184void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591185 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131186 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271187 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071188 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271189 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531190 content::Source<Profile>(profile_),
1191 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061192
[email protected]f3b1a082011-11-18 00:34:301193 for (content::RenderProcessHost::iterator i(
1194 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561195 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301196 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081197 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301198 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081199 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171200 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561201 }
1202
[email protected]31d8f5f22012-04-02 15:22:081203 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171204 profile_->GetExtensionSpecialStoragePolicy()->
1205 RevokeRightsForExtension(extension);
1206
[email protected]b777b332011-04-16 04:01:081207#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231208 // Revoke external file access for the extension from its file system context.
1209 // It is safe to access the extension's storage partition at this point. The
1210 // storage partition may get destroyed only after the extension gets unloaded.
1211 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1212 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091213 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231214 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091215 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321216 if (filesystem_context && filesystem_context->external_backend()) {
1217 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171218 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061219 }
[email protected]c8d407e2011-04-28 21:27:171220#endif
1221
1222 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061223}
1224
[email protected]0a071a32011-02-08 00:18:241225Profile* ExtensionService::profile() {
1226 return profile_;
1227}
1228
[email protected]45759612012-07-10 17:21:231229extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451230 return extension_prefs_;
1231}
1232
[email protected]a7ff4b72013-10-17 20:56:021233const extensions::ExtensionPrefs* ExtensionService::extension_prefs() const {
1234 return extension_prefs_;
1235}
1236
[email protected]0d9a2202011-11-09 13:48:411237extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381238 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201239}
1240
[email protected]e2d720aa2012-05-17 00:11:351241extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441242 return extension_prefs()->content_settings_store();
1243}
1244
[email protected]25ae0152011-11-18 14:40:021245bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351246 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021247}
1248
[email protected]7f8f24f2012-11-15 19:40:141249base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091250 if (file_task_runner_.get())
1251 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141252
1253 // We should be able to interrupt any part of extension install process during
1254 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1255 // will be ignored/deleted while we will block on started tasks.
1256 std::string token("ext_install-");
1257 token.append(profile_->GetPath().AsUTF8Unsafe());
1258 file_task_runner_ = BrowserThread::GetBlockingPool()->
1259 GetSequencedTaskRunnerWithShutdownBehavior(
1260 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1261 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091262 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141263}
1264
[email protected]42a08162012-03-16 18:09:111265extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061266 return updater_.get();
1267}
1268
[email protected]4ee07c62012-08-21 12:40:421269void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361270 std::vector<std::string> to_unload;
1271 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281272
[email protected]0d54b682013-11-05 14:15:361273 // Loop through the extensions list, finding extensions we need to unload or
1274 // disable.
[email protected]84df8332011-12-06 18:22:461275 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251276 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361277 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421278 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361279 to_unload.push_back(extension->id());
1280 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1281 if (system_->management_policy()->MustRemainDisabled(
1282 extension, &disable_reason, NULL))
1283 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251284 }
1285
[email protected]0d54b682013-11-05 14:15:361286 for (size_t i = 0; i < to_unload.size(); ++i)
1287 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1288
1289 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1290 to_disable.begin(); i != to_disable.end(); ++i)
1291 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251292}
1293
[email protected]31206602011-04-13 23:07:321294void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351295 if (updater()) {
[email protected]94fde232012-04-27 10:22:301296 if (AreAllExternalProvidersReady()) {
1297 updater()->CheckSoon();
1298 } else {
1299 // Sync can start updating before all the external providers are ready
1300 // during startup. Start the update as soon as those providers are ready,
1301 // but not before.
1302 update_once_all_providers_are_ready_ = true;
1303 }
[email protected]c3cfb012011-04-06 22:07:351304 } else {
[email protected]31206602011-04-13 23:07:321305 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351306 }
1307}
1308
[email protected]168389f2011-12-20 17:12:481309void ExtensionService::OnExtensionMoved(
1310 const std::string& moved_extension_id,
1311 const std::string& predecessor_extension_id,
1312 const std::string& successor_extension_id) {
[email protected]74474042013-11-21 12:03:541313 extension_prefs_->app_sorting()->OnExtensionMoved(
[email protected]3569b502012-01-12 20:08:231314 moved_extension_id,
1315 predecessor_extension_id,
1316 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481317
1318 const Extension* extension = GetInstalledExtension(moved_extension_id);
[email protected]f8aefb132013-10-30 09:29:521319 if (extension_sync_service_ && extension) {
1320 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1321 }
[email protected]168389f2011-12-20 17:12:481322}
1323
[email protected]8e4560b62011-01-14 10:09:141324// Some extensions will autoupdate themselves externally from Chrome. These
1325// are typically part of some larger client application package. To support
1326// these, the extension will register its location in the the preferences file
1327// (and also, on Windows, in the registry) and this code will periodically
1328// check that location for a .crx file, which it will then install locally if
1329// a new version is available.
1330// Errors are reported through ExtensionErrorReporter. Succcess is not
1331// reported.
[email protected]eaa7dd182010-12-14 11:09:001332void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121333 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141334
1335 // Note that this installation is intentionally silent (since it didn't
1336 // go through the front-end). Extensions that are registered in this
1337 // way are effectively considered 'pre-bundled', and so implicitly
1338 // trusted. In general, if something has HKLM or filesystem access,
1339 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121340
[email protected]8e4560b62011-01-14 10:09:141341 // Ask each external extension provider to give us a call back for each
1342 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271343 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141344 for (i = external_extension_providers_.begin();
1345 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271346 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141347 provider->VisitRegisteredExtension();
1348 }
1349
[email protected]50067e52011-10-20 23:17:071350 // Do any required work that we would have done after completion of all
1351 // providers.
1352 if (external_extension_providers_.empty()) {
1353 OnAllExternalProvidersReady();
1354 }
[email protected]9f1087e2009-06-15 17:29:321355}
1356
[email protected]50067e52011-10-20 23:17:071357void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271358 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121359 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071360 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121361
1362 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141363 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301364 if (AreAllExternalProvidersReady())
1365 OnAllExternalProvidersReady();
1366}
1367
1368bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271369 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141370 for (i = external_extension_providers_.begin();
1371 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301372 if (!i->get()->IsReady())
1373 return false;
[email protected]8e4560b62011-01-14 10:09:141374 }
[email protected]94fde232012-04-27 10:22:301375 return true;
[email protected]50067e52011-10-20 23:17:071376}
1377
1378void ExtensionService::OnAllExternalProvidersReady() {
1379 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301380 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1381 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071382
1383 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301384 if (update_once_all_providers_are_ready_ && updater()) {
1385 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141386 extensions::ExtensionUpdater::CheckParams params;
1387 params.callback = external_updates_finished_callback_;
1388 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141389 }
1390
1391 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231392 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141393 extension_prefs_->GetInstalledExtensionsInfo());
1394 for (size_t i = 0; i < extensions_info->size(); ++i) {
1395 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401396 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141397 CheckExternalUninstall(info->extension_id);
1398 }
[email protected]e5af875f2011-10-10 21:09:141399 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141400}
1401
1402void ExtensionService::IdentifyAlertableExtensions() {
1403 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1404
[email protected]07c9f2f42012-02-29 18:45:221405 // Build up the lists of extensions that require acknowledgment. If this is
1406 // the first time, grandfather extensions that would have caused
1407 // notification.
[email protected]89226982012-07-16 20:09:181408 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141409
[email protected]a9aa5932012-01-25 08:27:401410 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181411 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011412 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401413 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181414 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141415 } else {
1416 // First run. Just acknowledge all the extensions, silently, by
1417 // shortcutting the display of the UI and going straight to the
1418 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401419 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141420 }
1421 }
[email protected]a9aa5932012-01-25 08:27:401422
[email protected]612a1cb12012-10-17 13:18:031423 UpdateExternalExtensionAlert();
1424
[email protected]a9aa5932012-01-25 08:27:401425 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181426 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141427}
1428
[email protected]89226982012-07-16 20:09:181429bool ExtensionService::PopulateExtensionErrorUI(
1430 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221431 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281432
1433 // Extensions that are blacklisted.
1434 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1435 it != blacklisted_extensions_.end(); ++it) {
1436 std::string id = (*it)->id();
1437 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1438 extension_error_ui->AddBlacklistedExtension(id);
1439 needs_alert = true;
1440 }
1441 }
1442
[email protected]07c9f2f42012-02-29 18:45:221443 for (ExtensionSet::const_iterator iter = extensions_.begin();
1444 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361445 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281446
1447 // Extensions disabled by policy. Note: this no longer includes blacklisted
1448 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391449 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221450 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181451 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221452 needs_alert = true;
1453 }
1454 }
[email protected]07c9f2f42012-02-29 18:45:221455 }
[email protected]695b5712012-12-06 23:55:281456
[email protected]07c9f2f42012-02-29 18:45:221457 return needs_alert;
1458}
1459
[email protected]a9aa5932012-01-25 08:27:401460void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031461 const ExtensionIdSet* extension_ids =
1462 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141463 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1464 iter != extension_ids->end(); ++iter) {
1465 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1466 }
[email protected]879a6092013-03-06 04:12:501467 extension_error_ui_.reset();
1468}
1469
1470void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301471 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141472}
1473
[email protected]47fc70c2011-12-06 07:29:511474void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1475 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031476 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511477}
1478
[email protected]d96eb512012-11-01 23:44:081479bool ExtensionService::IsUnacknowledgedExternalExtension(
1480 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321481 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1482 return false;
1483
[email protected]1d5e58b2013-01-31 08:41:401484 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081485 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1486 !(extension_prefs_->GetDisableReasons(extension->id()) &
1487 Extension::DISABLE_SIDELOAD_WIPEOUT));
1488}
1489
[email protected]59ee99d2013-10-11 15:46:161490void ExtensionService::ReconcileKnownDisabled() {
[email protected]60a8bc962013-12-03 03:17:271491 ExtensionIdSet known_disabled_ids;
1492 if (!extension_prefs_->GetKnownDisabled(&known_disabled_ids)) {
[email protected]018a5682013-11-20 02:41:211493 extension_prefs_->SetKnownDisabled(disabled_extensions_.GetIDs());
1494 UMA_HISTOGRAM_BOOLEAN("Extensions.KnownDisabledInitialized", true);
1495 return;
[email protected]59ee99d2013-10-11 15:46:161496 }
[email protected]195c1582013-10-29 18:42:231497
[email protected]018a5682013-11-20 02:41:211498 // Both |known_disabled_ids| and |extensions_| are ordered (by definition
1499 // of std::map and std::set). Iterate forward over both sets in parallel
1500 // to find matching IDs and disable the corresponding extensions.
1501 ExtensionSet::const_iterator extensions_it = extensions_.begin();
1502 ExtensionIdSet::const_iterator known_disabled_ids_it =
1503 known_disabled_ids.begin();
1504 int known_disabled_count = 0;
1505 while (extensions_it != extensions_.end() &&
1506 known_disabled_ids_it != known_disabled_ids.end()) {
1507 const std::string& extension_id = extensions_it->get()->id();
1508 const int comparison = extension_id.compare(*known_disabled_ids_it);
1509 if (comparison < 0) {
1510 ++extensions_it;
1511 } else if (comparison > 0) {
1512 ++known_disabled_ids_it;
1513 } else {
1514 ++known_disabled_count;
1515 // Advance |extensions_it| immediately as it will be invalidated upon
1516 // disabling the extension it points to.
1517 ++extensions_it;
1518 ++known_disabled_ids_it;
1519 DisableExtension(extension_id, Extension::DISABLE_KNOWN_DISABLED);
1520 }
1521 }
1522 UMA_HISTOGRAM_COUNTS_100("Extensions.KnownDisabledReDisabled",
1523 known_disabled_count);
1524
[email protected]195c1582013-10-29 18:42:231525 // Update the list of known disabled to reflect every change to
1526 // |disabled_extensions_| from this point forward.
1527 disabled_extensions_.set_modification_callback(
1528 base::Bind(&extensions::ExtensionPrefs::SetKnownDisabled,
1529 base::Unretained(extension_prefs_)));
[email protected]59ee99d2013-10-11 15:46:161530}
1531
[email protected]89226982012-07-16 20:09:181532void ExtensionService::HandleExtensionAlertDetails() {
1533 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301534 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1535 // causes a navigation.
1536 if (extension_error_ui_)
1537 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101538}
1539
[email protected]612a1cb12012-10-17 13:18:031540void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361541 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031542 return;
1543
1544 const Extension* extension = NULL;
1545 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1546 iter != disabled_extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361547 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081548 if (IsUnacknowledgedExternalExtension(e)) {
1549 extension = e;
1550 break;
[email protected]612a1cb12012-10-17 13:18:031551 }
1552 }
1553
1554 if (extension) {
[email protected]d96eb512012-11-01 23:44:081555 if (!extensions::HasExternalInstallError(this)) {
1556 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031557 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081558 // Stop prompting for this extension, and check if there's another
1559 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031560 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081561 UpdateExternalExtensionAlert();
1562 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1563 EXTERNAL_EXTENSION_IGNORED,
1564 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021565 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1566 UMA_HISTOGRAM_ENUMERATION(
1567 "Extensions.ExternalExtensionEventWebstore",
1568 EXTERNAL_EXTENSION_IGNORED,
1569 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1570 } else {
1571 UMA_HISTOGRAM_ENUMERATION(
1572 "Extensions.ExternalExtensionEventNonWebstore",
1573 EXTERNAL_EXTENSION_IGNORED,
1574 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1575 }
[email protected]d96eb512012-11-01 23:44:081576 return;
[email protected]612a1cb12012-10-17 13:18:031577 }
[email protected]b3aa7182013-04-25 04:45:231578 if (is_first_run_)
1579 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1580 // first_run is true if the extension was installed during a first run
1581 // (even if it's post-first run now).
1582 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1583 extension->id());
1584 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031585 }
1586 } else {
1587 extensions::RemoveExternalInstallError(this);
1588 }
[email protected]612a1cb12012-10-17 13:18:031589}
1590
[email protected]a9f39a312010-12-23 22:14:271591void ExtensionService::UnloadExtension(
1592 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131593 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091594 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281595 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021596 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281597 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251598
[email protected]fa0c96732010-11-17 00:14:231599 // This method can be called via PostTask, so the extension may have been
1600 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091601 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171602 // In case the extension may have crashed/uninstalled. Allow the profile to
1603 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081604 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231605 return;
[email protected]dd163fb02011-05-04 22:22:171606 }
[email protected]0c6da502009-08-14 22:32:391607
[email protected]1eb175082010-02-10 09:26:161608 // Keep information about the extension so that we can reload it later
1609 // even if it's not permanently installed.
1610 unloaded_extension_paths_[extension->id()] = extension->path();
1611
[email protected]f17dbd42010-08-16 23:21:101612 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031613 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101614
[email protected]d7e9a862010-11-03 21:57:491615 // Clean up runtime data.
1616 extension_runtime_data_.erase(extension_id);
1617
[email protected]060d4972012-07-19 17:22:391618 if (disabled_extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461619 disabled_extensions_.Remove(extension->id());
[email protected]dd163fb02011-05-04 22:22:171620 // Make sure the profile cleans up its RequestContexts when an already
1621 // disabled extension is unloaded (since they are also tracking the disabled
1622 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081623 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]60a174a2013-08-02 20:29:281624 } else {
1625 // Remove the extension from our list.
1626 extensions_.Remove(extension->id());
1627 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391628 }
1629
[email protected]fb789532013-08-27 02:40:211630 content::NotificationService::current()->Notify(
1631 chrome::NOTIFICATION_EXTENSION_REMOVED,
1632 content::Source<Profile>(profile_),
1633 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251634}
1635
[email protected]8b1ec202013-09-05 02:09:501636void ExtensionService::RemoveComponentExtension(
1637 const std::string& extension_id) {
1638 scoped_refptr<const Extension> extension(
1639 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131640 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]8b1ec202013-09-05 02:09:501641 content::NotificationService::current()->Notify(
1642 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1643 content::Source<Profile>(profile_),
1644 content::Details<const Extension>(extension.get()));
1645}
1646
[email protected]eaa7dd182010-12-14 11:09:001647void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401648 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171649
[email protected]84df8332011-12-06 18:22:461650 extensions_.Clear();
1651 disabled_extensions_.Clear();
1652 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491653 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321654
[email protected]c6e4a3412009-06-24 15:45:291655 // TODO(erikkay) should there be a notification for this? We can't use
1656 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1657 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321658}
1659
[email protected]eaa7dd182010-12-14 11:09:001660void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321661 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451662 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011663 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351664 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1665 // times.
[email protected]9f1087e2009-06-15 17:29:321666}
1667
[email protected]eaa7dd182010-12-14 11:09:001668void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:101669#if defined(OS_CHROMEOS)
1670 if (disable_garbage_collection_)
1671 return;
1672#endif
1673
[email protected]2f8757c32012-06-19 19:17:471674 if (extension_prefs_->pref_service()->ReadOnly())
1675 return;
1676
[email protected]9bd9a6862012-11-29 09:24:221677 if (pending_extension_manager()->HasPendingExtensions()) {
1678 // Don't garbage collect while there are pending installations, which may
1679 // be using the temporary installation directory. Try to garbage collect
1680 // again later.
[email protected]b3a25092013-05-28 22:08:161681 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221682 FROM_HERE,
1683 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1684 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1685 return;
1686 }
1687
[email protected]45759612012-07-10 17:21:231688 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471689 extension_prefs_->GetInstalledExtensionsInfo());
1690
[email protected]650b2d52013-02-10 03:41:451691 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471692 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051693 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1694 info->at(i)->extension_path));
1695
[email protected]399583b2012-12-11 09:33:421696 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051697 for (size_t i = 0; i < info->size(); ++i)
1698 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1699 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471700
[email protected]7f8f24f2012-11-15 19:40:141701 if (!GetFileTaskRunner()->PostTask(
1702 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471703 base::Bind(
1704 &extension_file_util::GarbageCollectExtensions,
1705 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:401706 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:471707 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401708 }
[email protected]3cf4f0992009-02-03 23:00:301709}
1710
[email protected]820d9bd2013-04-03 03:46:031711void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351712 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031713 content::NotificationService::current()->Notify(
1714 chrome::NOTIFICATION_EXTENSIONS_READY,
1715 content::Source<Profile>(profile_),
1716 content::NotificationService::NoDetails());
1717}
1718
[email protected]eaa7dd182010-12-14 11:09:001719void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111720 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171721 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501722
[email protected]bc151cf92013-02-12 04:57:261723 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511724}
1725
[email protected]fcb58a862012-05-01 01:03:151726void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171727 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1728 // default extension state to DISABLED when the --disable-extensions flag
1729 // is set (http://crbug.com/29067).
1730 if (!extensions_enabled() &&
1731 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401732 extension->location() != Manifest::COMPONENT &&
1733 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151734 return;
[email protected]330840c2012-08-29 22:21:011735 }
[email protected]c8d407e2011-04-28 21:27:171736
[email protected]b914e2952013-04-26 07:10:031737 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171738 bool is_extension_installed = false;
1739 const Extension* old = GetInstalledExtension(extension->id());
1740 if (old) {
1741 is_extension_installed = true;
1742 int version_compare_result =
1743 extension->version()->CompareTo(*(old->version()));
1744 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031745 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1746 // that we aren't downgrading.
1747 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171748 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031749 }
1750 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171751
[email protected]1eb175082010-02-10 09:26:161752 // The extension is now loaded, remove its data from unloaded extension map.
1753 unloaded_extension_paths_.erase(extension->id());
1754
[email protected]bb7f40952011-01-13 00:21:201755 // If a terminated extension is loaded, remove it from the terminated list.
1756 UntrackTerminatedExtension(extension->id());
1757
[email protected]f17dbd42010-08-16 23:21:101758 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411759 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101760
[email protected]b914e2952013-04-26 07:10:031761 // Check if the extension's privileges have changed and mark the
1762 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171763 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031764
[email protected]116d40e2013-08-08 17:23:171765 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411766 // To upgrade an extension in place, unload the old one and then load the
1767 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131768 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031769 }
[email protected]0c6da502009-08-14 22:32:391770
[email protected]695b5712012-12-06 23:55:281771 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091772 // Only prefs is checked for the blacklist. We rely on callers to check the
1773 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071774 // installation then threads through the install and pending install flow
1775 // of this class, and we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:281776 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:411777 } else if (!reloading &&
1778 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:481779 disabled_extensions_.Insert(extension);
[email protected]f8aefb132013-10-30 09:29:521780 if (extension_sync_service_)
1781 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031782 content::NotificationService::current()->Notify(
1783 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1784 content::Source<Profile>(profile_),
1785 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441786
[email protected]7e9e2422012-12-13 19:54:191787 // Show the extension disabled error if a permissions increase was the
1788 // only reason it was disabled.
1789 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:031790 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441791 extensions::AddExtensionDisabledError(this, extension);
1792 }
[email protected]ca2e82022013-04-29 16:48:411793 } else if (reloading) {
1794 // Replace the old extension with the new version.
1795 CHECK(!disabled_extensions_.Insert(extension));
1796 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281797 } else {
1798 // All apps that are displayed in the launcher are ordered by their ordinals
1799 // so we must ensure they have valid ordinals.
1800 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:251801 if (!extension->ShouldDisplayInNewTabPage()) {
[email protected]74474042013-11-21 12:03:541802 extension_prefs_->app_sorting()->MarkExtensionAsHidden(extension->id());
[email protected]bd36b652013-02-27 02:13:251803 }
[email protected]74474042013-11-21 12:03:541804 extension_prefs_->app_sorting()->EnsureValidOrdinals(
[email protected]695b5712012-12-06 23:55:281805 extension->id(), syncer::StringOrdinal());
1806 }
1807
1808 extensions_.Insert(extension);
[email protected]f8aefb132013-10-30 09:29:521809 if (extension_sync_service_)
1810 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281811 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311812 }
[email protected]b914e2952013-04-26 07:10:031813 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:351814}
1815
[email protected]8c484b742012-11-29 06:05:361816void ExtensionService::AddComponentExtension(const Extension* extension) {
1817 const std::string old_version_string(
1818 extension_prefs_->GetVersionString(extension->id()));
1819 const Version old_version(old_version_string);
1820
[email protected]0181a7d2013-11-12 01:08:421821 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361822 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1823 VLOG(1) << "Component extension " << extension->name() << " ("
1824 << extension->id() << ") installing/upgrading from '"
1825 << old_version_string << "' to " << extension->version()->GetString();
1826
1827 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:421828 Extension::ENABLED_COMPONENT,
[email protected]8e289f0b2013-12-17 17:49:071829 extensions::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:421830 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:361831 return;
1832 }
1833
1834 AddExtension(extension);
1835}
1836
[email protected]b914e2952013-04-26 07:10:031837void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:311838 // If the extension has used the optional permissions API, it will have a
1839 // custom set of active permissions defined in the extension prefs. Here,
1840 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:061841 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:311842 extension_prefs()->GetActivePermissions(extension->id());
1843
[email protected]dc24976f2013-06-02 21:15:091844 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:311845 // We restrict the active permissions to be within the bounds defined in the
1846 // extension's manifest.
1847 // a) active permissions must be a subset of optional + default permissions
1848 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:061849 scoped_refptr<PermissionSet> total_permissions =
1850 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061851 extensions::PermissionsData::GetRequiredPermissions(extension),
1852 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:311853
1854 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:061855 scoped_refptr<PermissionSet> adjusted_active =
1856 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:311857 total_permissions.get(), active_permissions.get());
1858
1859 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:061860 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061861 extensions::PermissionsData::GetRequiredPermissions(extension),
1862 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311863
[email protected]c333e792012-01-06 16:57:391864 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:091865 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311866 }
[email protected]b914e2952013-04-26 07:10:031867}
1868
1869void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171870 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:031871 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311872
[email protected]8d888c12010-11-30 00:00:251873 // We keep track of all permissions the user has granted each extension.
1874 // This allows extensions to gracefully support backwards compatibility
1875 // by including unknown permissions in their manifests. When the user
1876 // installs the extension, only the recognized permissions are recorded.
1877 // When the unknown permissions become recognized (e.g., through browser
1878 // upgrade), we can prompt the user to accept these new permissions.
1879 // Extensions can also silently upgrade to less permissions, and then
1880 // silently upgrade to a version that adds these permissions back.
1881 //
1882 // For example, pretend that Chrome 10 includes a permission "omnibox"
1883 // for an API that adds suggestions to the omnibox. An extension can
1884 // maintain backwards compatibility while still having "omnibox" in the
1885 // manifest. If a user installs the extension on Chrome 9, the browser
1886 // will record the permissions it recognized, not including "omnibox."
1887 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1888 // will disable the extension and prompt the user to approve the increase
1889 // in privileges. The extension could then release a new version that
1890 // removes the "omnibox" permission. When the user upgrades, Chrome will
1891 // still remember that "omnibox" had been granted, so that if the
1892 // extension once again includes "omnibox" in an upgrade, the extension
1893 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281894 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251895
[email protected]211a3f32013-05-28 21:48:181896 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361897 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]45928b682013-11-28 08:20:271898 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031899 // Silently grant all active permissions to default apps only on install.
1900 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181901 // Silently grant all active permissions to apps install in kiosk mode on both
1902 // install and update.
1903 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031904 GrantPermissions(extension);
1905
1906 bool is_privilege_increase = false;
1907 // We only need to compare the granted permissions to the current permissions
1908 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051909 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181910 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251911 // Add all the recognized permissions if the granted permissions list
1912 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061913 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311914 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521915 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251916
1917 // Here, we check if an extension's privileges have increased in a manner
1918 // that requires the user's approval. This could occur because the browser
1919 // upgraded and recognized additional privileges, or an extension upgrades
1920 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251921 is_privilege_increase =
1922 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
1923 granted_permissions,
1924 extension->GetActivePermissions().get(),
1925 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251926 }
1927
[email protected]116d40e2013-08-08 17:23:171928 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031929 // If the extension was already disabled, suppress any alerts for becoming
1930 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031931 bool previously_disabled =
1932 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191933 // Legacy disabled extensions do not have a disable reason. Infer that if
1934 // there was no permission increase, it was likely disabled by the user.
1935 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031936 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191937 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031938 }
[email protected]7e9e2422012-12-13 19:54:191939 // Extensions that came to us disabled from sync need a similar inference,
1940 // except based on the new version's permissions.
1941 if (previously_disabled &&
1942 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1943 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1944 extension_prefs_->ClearDisableReasons(extension->id());
1945 if (!is_privilege_increase)
1946 disable_reasons |= Extension::DISABLE_USER_ACTION;
1947 }
[email protected]b914e2952013-04-26 07:10:031948 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251949 }
1950
1951 // Extension has changed permissions significantly. Disable it. A
1952 // notification should be sent by the caller.
1953 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191954 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491955 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1956 RecordPermissionMessagesHistogram(
1957 extension, "Extensions.Permissions_AutoDisable");
1958 }
[email protected]195c1582013-10-29 18:42:231959 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251960 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031961 }
1962 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281963 extension_prefs_->AddDisableReason(
1964 extension->id(),
1965 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251966 }
1967}
1968
[email protected]eaa7dd182010-12-14 11:09:001969void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391970 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:461971 for (ExtensionSet::const_iterator iter = extensions_.begin();
1972 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361973 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401974 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461975 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351976 }
1977
[email protected]f6431be82013-09-07 02:53:451978 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251979}
1980
[email protected]669b2372013-10-17 15:04:581981ExtensionService::ImportStatus ExtensionService::CheckImports(
1982 const extensions::Extension* extension,
1983 std::list<SharedModuleInfo::ImportInfo>* missing_modules,
1984 std::list<SharedModuleInfo::ImportInfo>* outdated_modules) {
1985 DCHECK(extension);
1986 DCHECK(missing_modules && missing_modules->empty());
1987 DCHECK(outdated_modules && outdated_modules->empty());
[email protected]9f4e4f082013-06-21 07:11:191988 ImportStatus status = IMPORT_STATUS_OK;
[email protected]9f4e4f082013-06-21 07:11:191989 if (SharedModuleInfo::ImportsModules(extension)) {
1990 const std::vector<SharedModuleInfo::ImportInfo>& imports =
1991 SharedModuleInfo::GetImports(extension);
1992 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
1993 for (i = imports.begin(); i != imports.end(); ++i) {
1994 Version version_required(i->minimum_version);
1995 const Extension* imported_module =
1996 GetExtensionById(i->extension_id, true);
1997 if (!imported_module) {
1998 if (extension->from_webstore()) {
1999 status = IMPORT_STATUS_UNSATISFIED;
[email protected]669b2372013-10-17 15:04:582000 missing_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:192001 } else {
2002 return IMPORT_STATUS_UNRECOVERABLE;
2003 }
2004 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
2005 return IMPORT_STATUS_UNRECOVERABLE;
2006 } else if (version_required.IsValid() &&
2007 imported_module->version()->CompareTo(version_required) < 0) {
2008 if (imported_module->from_webstore()) {
[email protected]669b2372013-10-17 15:04:582009 outdated_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:192010 status = IMPORT_STATUS_UNSATISFIED;
2011 } else {
2012 return IMPORT_STATUS_UNRECOVERABLE;
2013 }
2014 }
2015 }
2016 }
[email protected]669b2372013-10-17 15:04:582017 return status;
2018}
2019
2020ExtensionService::ImportStatus ExtensionService::SatisfyImports(
2021 const Extension* extension) {
2022 std::list<SharedModuleInfo::ImportInfo> noinstalled;
2023 std::list<SharedModuleInfo::ImportInfo> outdated;
2024 ImportStatus status = CheckImports(extension, &noinstalled, &outdated);
2025 if (status == IMPORT_STATUS_UNRECOVERABLE)
2026 return status;
[email protected]9f4e4f082013-06-21 07:11:192027 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]669b2372013-10-17 15:04:582028 std::list<SharedModuleInfo::ImportInfo>::const_iterator iter;
2029 for (iter = noinstalled.begin(); iter != noinstalled.end(); ++iter) {
[email protected]9f4e4f082013-06-21 07:11:192030 pending_extension_manager()->AddFromExtensionImport(
[email protected]669b2372013-10-17 15:04:582031 iter->extension_id,
[email protected]9f4e4f082013-06-21 07:11:192032 extension_urls::GetWebstoreUpdateUrl(),
2033 IsSharedModule);
2034 }
2035 CheckForUpdatesSoon();
2036 }
2037 return status;
2038}
2039
2040scoped_ptr<const ExtensionSet>
2041 ExtensionService::GetDependentExtensions(const Extension* extension) {
2042 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2043 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2044 if (SharedModuleInfo::IsSharedModule(extension)) {
2045 set_to_check->InsertAll(disabled_extensions_);
2046 set_to_check->InsertAll(delayed_installs_);
2047 set_to_check->InsertAll(extensions_);
2048 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2049 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112050 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2051 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192052 dependents->Insert(*iter);
2053 }
2054 }
2055 }
2056 return dependents.PassAs<const ExtensionSet>();
2057}
2058
2059void ExtensionService::PruneSharedModulesOnUninstall(
2060 const Extension* extension) {
2061 if (SharedModuleInfo::ImportsModules(extension)) {
2062 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2063 SharedModuleInfo::GetImports(extension);
2064 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2065 for (i = imports.begin(); i != imports.end(); ++i) {
2066 const Extension* imported_module =
2067 GetExtensionById(i->extension_id, true);
2068 if (imported_module && imported_module->from_webstore()) {
2069 scoped_ptr<const ExtensionSet> dependents =
2070 GetDependentExtensions(imported_module);
2071 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212072 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192073 }
2074 }
2075 }
2076 }
2077}
2078
[email protected]8266d662011-07-12 21:53:262079void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502080 const Extension* extension,
[email protected]98270432012-09-11 20:51:242081 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052082 bool has_requirement_errors,
[email protected]8e289f0b2013-12-17 17:49:072083 extensions::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052084 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122085 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202086
[email protected]c3cfb012011-04-06 22:07:352087 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032088 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412089 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062090 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402091 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062092 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322093
[email protected]31bb5ee62012-09-12 22:58:402094 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2095 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222096 << " and update URL "
2097 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402098 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542099
[email protected]4416c5a2010-06-26 01:28:572100 // Delete the extension directory since we're not going to
2101 // load it.
[email protected]7f8f24f2012-11-15 19:40:142102 if (!GetFileTaskRunner()->PostTask(
2103 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362104 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402105 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362106 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402107 }
[email protected]4416c5a2010-06-26 01:28:572108 return;
2109 }
[email protected]51a3bf8b2012-06-08 22:53:062110
2111 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572112 } else {
[email protected]c3cfb012011-04-06 22:07:352113 // We explicitly want to re-enable an uninstalled external
2114 // extension; if we're here, that means the user is manually
2115 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332116 if (IsExternalExtensionUninstalled(id)) {
2117 initial_enable = true;
2118 }
[email protected]aa142702010-03-26 01:26:332119 }
2120
[email protected]98270432012-09-11 20:51:242121 // Unsupported requirements overrides the management policy.
2122 if (has_requirement_errors) {
2123 initial_enable = false;
2124 extension_prefs_->AddDisableReason(
2125 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2126 // If the extension was disabled because of unsupported requirements but
2127 // now supports all requirements after an update and there are not other
2128 // disable reasons, enable it.
2129 } else if (extension_prefs_->GetDisableReasons(id) ==
2130 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2131 initial_enable = true;
2132 extension_prefs_->ClearDisableReasons(id);
2133 }
2134
[email protected]8e289f0b2013-12-17 17:49:072135 if (blacklist_state == extensions::BLACKLISTED_MALWARE) {
[email protected]9f3c8532013-07-31 19:52:072136 // Installation of a blacklisted extension can happen from sync, policy,
2137 // etc, where to maintain consistency we need to install it, just never
2138 // load it (see AddExtension). Usually it should be the job of callers to
2139 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2140 // showing the install dialogue).
2141 extension_prefs()->AcknowledgeBlacklistedExtension(id);
2142 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2143 extension->location(),
2144 Manifest::NUM_LOCATIONS);
2145 }
2146
[email protected]695b5712012-12-06 23:55:282147 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552148 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2149 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302150 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402151 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552152 RecordPermissionMessagesHistogram(
2153 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302154 } else {
2155 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2156 extension->GetType(), 100);
2157 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402158 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552159 }
2160
[email protected]a39921b42012-02-28 03:42:542161 // Certain extension locations are specific enough that we can
2162 // auto-acknowledge any extension that came from one of them.
[email protected]9bb691632013-09-26 18:50:122163 if (Manifest::IsPolicyLocation(extension->location()))
[email protected]a39921b42012-02-28 03:42:542164 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422165 const Extension::State initial_state =
2166 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]0f9e6262013-11-05 23:49:192167 const bool blacklisted_for_malware =
[email protected]8e289f0b2013-12-17 17:49:072168 blacklist_state == extensions::BLACKLISTED_MALWARE;
[email protected]e7aa7b7e2012-11-27 04:51:222169 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072170 extension_prefs_->SetDelayedInstallInfo(
2171 extension,
2172 initial_state,
[email protected]0f9e6262013-11-05 23:49:192173 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072174 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2175 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052176
2177 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192178 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052179
[email protected]75bdcb872013-03-13 00:41:452180 // Notify observers that app update is available.
2181 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:062182 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:052183 return;
2184 }
2185
[email protected]9f4e4f082013-06-21 07:11:192186 ImportStatus status = SatisfyImports(extension);
2187 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072188 extension_prefs_->SetDelayedInstallInfo(
2189 extension,
2190 initial_state,
[email protected]0f9e6262013-11-05 23:49:192191 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072192 extensions::ExtensionPrefs::DELAY_REASON_GC,
2193 page_ordinal);
[email protected]399583b2012-12-11 09:33:422194 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192195 } else if (status != IMPORT_STATUS_OK) {
2196 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072197 extension_prefs_->SetDelayedInstallInfo(
2198 extension,
2199 initial_state,
[email protected]0f9e6262013-11-05 23:49:192200 blacklisted_for_malware,
[email protected]9f4e4f082013-06-21 07:11:192201 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2202 page_ordinal);
2203 delayed_installs_.Insert(extension);
2204 }
[email protected]399583b2012-12-11 09:33:422205 } else {
[email protected]9f3c8532013-07-31 19:52:072206 AddNewOrUpdatedExtension(extension,
2207 initial_state,
2208 blacklist_state,
2209 page_ordinal);
[email protected]399583b2012-12-11 09:33:422210 }
[email protected]8c484b742012-11-29 06:05:362211}
2212
2213void ExtensionService::AddNewOrUpdatedExtension(
2214 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422215 Extension::State initial_state,
[email protected]8e289f0b2013-12-17 17:49:072216 extensions::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422217 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362218 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0f9e6262013-11-05 23:49:192219 const bool blacklisted_for_malware =
[email protected]8e289f0b2013-12-17 17:49:072220 blacklist_state == extensions::BLACKLISTED_MALWARE;
[email protected]9f3c8532013-07-31 19:52:072221 extension_prefs_->OnExtensionInstalled(extension,
2222 initial_state,
[email protected]0f9e6262013-11-05 23:49:192223 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072224 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122225 delayed_installs_.Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:462226 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2227 extensions::ExtensionSystem::Get(profile_)->install_verifier()->Add(
2228 extension->id(), InstallVerifier::AddResultCallback());
2229 }
[email protected]6f6101832012-11-27 22:10:482230 FinishInstallation(extension);
2231}
2232
2233void ExtensionService::MaybeFinishDelayedInstallation(
2234 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192235 // Check if the extension already got installed.
2236 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482237 return;
[email protected]9f4e4f082013-06-21 07:11:192238 extensions::ExtensionPrefs::DelayReason reason =
2239 extension_prefs_->GetDelayedInstallReason(extension_id);
2240
2241 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2242 // preferences files that will not have set this field but it was previously
2243 // only used for idle updates.
2244 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2245 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]617342a42013-12-18 23:34:032246 is_ready() && !extension_util::IsExtensionIdle(extension_id, system_))
[email protected]6f6101832012-11-27 22:10:482247 return;
2248
[email protected]9f4e4f082013-06-21 07:11:192249 const Extension* extension = delayed_installs_.GetByID(extension_id);
2250 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2251 ImportStatus status = SatisfyImports(extension);
2252 if (status != IMPORT_STATUS_OK) {
2253 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2254 delayed_installs_.Remove(extension_id);
2255 // Make sure no version of the extension is actually installed, (i.e.,
2256 // that this delayed install was not an update).
2257 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2258 extension_prefs_->DeleteExtensionPrefs(extension_id);
2259 }
2260 return;
2261 }
2262 }
2263
[email protected]6f6101832012-11-27 22:10:482264 FinishDelayedInstallation(extension_id);
2265}
2266
2267void ExtensionService::FinishDelayedInstallation(
2268 const std::string& extension_id) {
2269 scoped_refptr<const Extension> extension(
2270 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092271 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192272 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482273
[email protected]399583b2012-12-11 09:33:422274 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482275 NOTREACHED();
2276
[email protected]dc24976f2013-06-02 21:15:092277 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482278}
2279
2280void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562281 const extensions::Extension* existing_extension =
2282 GetInstalledExtension(extension->id());
2283 bool is_update = false;
2284 std::string old_name;
2285 if (existing_extension) {
2286 is_update = true;
2287 old_name = existing_extension->name();
2288 }
2289 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152290 content::NotificationService::current()->Notify(
2291 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2292 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022293 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152294
[email protected]d96eb512012-11-01 23:44:082295 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362296
[email protected]399583b2012-12-11 09:33:422297 // Unpacked extensions default to allowing file access, but if that has been
2298 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402299 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422300 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2301 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2302 }
2303
[email protected]6f6101832012-11-27 22:10:482304 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032305
2306 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082307 // so he can reenable it. We do this last so that it has already been
2308 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162309 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032310 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082311 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2312 EXTERNAL_EXTENSION_INSTALLED,
2313 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022314 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2315 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2316 EXTERNAL_EXTENSION_INSTALLED,
2317 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2318 } else {
2319 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2320 EXTERNAL_EXTENSION_INSTALLED,
2321 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2322 }
[email protected]d96eb512012-11-01 23:44:082323 }
[email protected]9f4e4f082013-06-21 07:11:192324
2325 // Check extensions that may have been delayed only because this shared module
2326 // was not available.
2327 if (SharedModuleInfo::IsSharedModule(extension)) {
2328 MaybeFinishDelayedInstallations();
2329 }
[email protected]4a190632009-05-09 01:07:422330}
2331
[email protected]76b65442012-11-17 14:11:482332const Extension* ExtensionService::GetPendingExtensionUpdate(
2333 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192334 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482335}
2336
[email protected]bb7f40952011-01-13 00:21:202337void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462338 if (!terminated_extensions_.Contains(extension->id()))
2339 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202340
[email protected]b0af4792013-10-23 09:12:132341 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202342}
2343
2344void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202345 std::string lowercase_id = StringToLowerASCII(id);
[email protected]60a174a2013-08-02 20:29:282346 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
[email protected]84df8332011-12-06 18:22:462347 terminated_extensions_.Remove(lowercase_id);
[email protected]fb789532013-08-27 02:40:212348 if (extension) {
2349 content::NotificationService::current()->Notify(
2350 chrome::NOTIFICATION_EXTENSION_REMOVED,
2351 content::Source<Profile>(profile_),
2352 content::Details<const Extension>(extension));
2353 }
[email protected]bb7f40952011-01-13 00:21:202354}
2355
[email protected]0dfe05c2011-02-23 23:03:362356const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472357 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282358 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472359}
2360
2361const Extension* ExtensionService::GetInstalledExtension(
2362 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282363 int include_mask = INCLUDE_ENABLED |
2364 INCLUDE_DISABLED |
2365 INCLUDE_TERMINATED |
2366 INCLUDE_BLACKLISTED;
2367 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362368}
2369
[email protected]eaa7dd182010-12-14 11:09:002370bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442371 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]be9915fb2013-07-18 09:28:552372 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442373 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402374 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392375}
2376
[email protected]4d007b312012-10-17 03:00:482377bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]be9915fb2013-07-18 09:28:552378 const Extension* extension = extensions_.GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482379 if (extension && extension->is_platform_app()) {
2380 *url = GURL(chrome::kExtensionInvalidRequestURL);
2381 return true;
2382 }
2383
2384 return false;
2385}
2386
[email protected]9060d8b02012-01-13 02:14:302387bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322388 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142389 const Version* version,
[email protected]650b2d52013-02-10 03:41:452390 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402391 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512392 int creation_flags,
2393 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382394 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2395 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012396 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302397 return false;
[email protected]a8af9fdb2010-10-28 21:52:202398
[email protected]7577a5c52009-07-30 06:21:582399 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492400 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582401 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022402 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352403
[email protected]7577a5c52009-07-30 06:21:582404 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352405 // The default apps will have the location set as INTERNAL. Since older
2406 // default apps are installed as EXTERNAL, we override them. However, if the
2407 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402408 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352409 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402410 (location == Manifest::INTERNAL &&
2411 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352412
2413 if (!is_default_apps_migration) {
2414 DCHECK(version);
2415
2416 switch (existing->version()->CompareTo(*version)) {
2417 case -1: // existing version is older, we should upgrade
2418 break;
2419 case 0: // existing version is same, do nothing
2420 return false;
2421 case 1: // existing version is newer, uh-oh
2422 LOG(WARNING) << "Found external version of extension " << id
2423 << "that is older than current version. Current version "
2424 << "is: " << existing->VersionString() << ". New "
2425 << "version is: " << version->GetString()
2426 << ". Keeping current version.";
2427 return false;
2428 }
[email protected]7577a5c52009-07-30 06:21:582429 }
2430 }
2431
[email protected]9060d8b02012-01-13 02:14:302432 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302433 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482434 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302435 return false;
[email protected]e3987852012-05-04 10:06:302436 }
[email protected]9c635f22010-12-02 09:36:362437
[email protected]14908b72011-04-20 06:54:362438 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372439 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162440 installer->set_install_source(location);
2441 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072442 installer->set_expected_version(*version);
2443 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312444 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182445#if defined(OS_CHROMEOS)
2446 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2447#else
[email protected]6dfbbf82010-03-12 23:09:162448 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182449#endif
[email protected]47fc70c2011-12-06 07:29:512450
2451 // Depending on the source, a new external extension might not need a user
2452 // notification on installation. For such extensions, mark them acknowledged
2453 // now to suppress the notification.
2454 if (mark_acknowledged)
2455 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302456
2457 return true;
[email protected]7577a5c52009-07-30 06:21:582458}
2459
[email protected]b5a507b22013-11-08 20:41:572460scoped_ptr<DictionaryValue> ExtensionService::GetExtensionInfo(
2461 const std::string& extension_id) const {
2462 scoped_ptr<DictionaryValue> dictionary(new DictionaryValue);
2463 const extensions::Extension* extension = extensions_.GetByID(extension_id);
2464 if (extension) {
2465 GURL icon = extensions::ExtensionIconSource::GetIconURL(
2466 extension, extension_misc::EXTENSION_ICON_SMALLISH,
2467 ExtensionIconSet::MATCH_BIGGER, false, NULL);
2468 dictionary->SetString("id", extension_id);
2469 dictionary->SetString("name", extension->name());
2470 dictionary->SetString("icon", icon.spec());
2471 }
2472 return dictionary.Pass();
2473}
2474
[email protected]eaa7dd182010-12-14 11:09:002475void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452476 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402477 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402478 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012479 content::NotificationService::current()->Notify(
2480 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2481 content::Source<Profile>(profile_),
2482 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402483
[email protected]8a205c02011-02-04 20:41:332484 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]439f1e32013-12-09 20:09:092485 base::string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042486 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492487 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402488 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2489}
2490
[email protected]eaa7dd182010-12-14 11:09:002491void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132492 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182493 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332494 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182495 if (iter == orphaned_dev_tools_.end())
2496 return;
2497
[email protected]04ea1bb2013-07-10 09:26:092498 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182499 orphaned_dev_tools_.erase(iter);
2500}
2501
[email protected]432115822011-07-10 15:52:272502void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532503 const content::NotificationSource& source,
2504 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272505 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542506 case chrome::NOTIFICATION_APP_TERMINATING:
2507 // Shutdown has started. Don't start any more extension installs.
2508 // (We cannot use ExtensionService::Shutdown() for this because it
2509 // happens too late in browser teardown.)
2510 browser_terminating_ = true;
2511 break;
[email protected]432115822011-07-10 15:52:272512 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532513 if (profile_ !=
2514 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322515 break;
[email protected]6c2381d2011-10-19 02:52:532516 }
[email protected]a4ed6282009-12-14 20:51:162517
[email protected]3a1dc572012-07-31 22:25:132518 extensions::ExtensionHost* host =
2519 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262520
[email protected]fa2416f2011-05-03 08:41:202521 // Mark the extension as terminated and Unload it. We want it to
2522 // be in a consistent state: either fully working or not loaded
2523 // at all, but never half-crashed. We do it in a PostTask so
2524 // that other handlers of this notification will still have
2525 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162526 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362527 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362528 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202529 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012530 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202531 host->extension()));
[email protected]31f77262009-12-02 20:48:532532 break;
2533 }
[email protected]432115822011-07-10 15:52:272534 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302535 content::RenderProcessHost* process =
2536 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192537 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302538 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192539 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2540 break;
2541
[email protected]15397b42012-05-16 23:56:062542 // Extensions need to know the channel for API restrictions.
2543 process->Send(new ExtensionMsg_SetChannel(
[email protected]15d4d2d2013-08-09 06:49:032544 extensions::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062545
[email protected]4941fe92013-06-13 23:21:012546 // Platform apps need to know the system font.
2547 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2548 webui::SetFontAndTextDirection(fonts.get());
2549 std::string font_family, font_size;
2550 fonts->GetString("fontfamily", &font_family);
2551 fonts->GetString("fontsize", &font_size);
2552 process->Send(new ExtensionMsg_SetSystemFont(
2553 font_family, font_size));
2554
[email protected]77a6970c2011-04-23 16:58:562555 // Valid extension function names, used to setup bindings in renderer.
2556 std::vector<std::string> function_names;
2557 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2558 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532559
[email protected]77a6970c2011-04-23 16:58:562560 // Scripting whitelist. This is modified by tests and must be communicated
2561 // to renderers.
2562 process->Send(new ExtensionMsg_SetScriptingWhitelist(
[email protected]c81f5d32013-10-26 10:38:422563 extensions::ExtensionsClient::Get()->GetScriptingWhitelist()));
[email protected]77a6970c2011-04-23 16:58:562564
2565 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532566 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462567 for (ExtensionSet::const_iterator iter = extensions_.begin();
2568 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492569 // Renderers don't need to know about themes.
2570 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362571 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562572 }
[email protected]9776e82e2011-11-15 02:17:532573 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562574 break;
2575 }
[email protected]432115822011-07-10 15:52:272576 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302577 content::RenderProcessHost* process =
2578 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192579 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302580 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192581 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2582 break;
2583
[email protected]52b76592013-11-02 17:59:032584 if (process_map_.Contains(process->GetID())) {
2585 // An extension process was terminated, this might have resulted in an
2586 // app or extension becoming idle.
2587 std::set<std::string> extension_ids =
2588 process_map_.GetExtensionsInProcess(process->GetID());
2589 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2590 it != extension_ids.end(); ++it) {
2591 if (delayed_installs_.Contains(*it)) {
2592 base::MessageLoop::current()->PostDelayedTask(
2593 FROM_HERE,
2594 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2595 AsWeakPtr(), *it),
2596 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2597 }
2598 }
2599 }
2600
[email protected]6bc04fd82011-12-04 02:29:352601 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462602 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202603 BrowserThread::IO,
2604 FROM_HERE,
2605 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082606 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302607 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212608 break;
2609 }
[email protected]92dd8d92013-02-26 00:41:082610 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452611 // Notify observers that chrome update is available.
2612 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2613 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082614 break;
2615 }
[email protected]aa96d3a2010-08-21 08:45:252616
[email protected]4814b512009-11-07 00:12:292617 default:
2618 NOTREACHED() << "Unexpected notification type.";
2619 }
2620}
2621
[email protected]90bb38d2012-11-14 18:36:032622void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182623 IdentifyAlertableExtensions();
2624 CheckManagementPolicy();
2625}
2626
[email protected]eaa7dd182010-12-14 11:09:002627bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182628 return !GetAppIds().empty();
2629}
[email protected]377011d2010-07-20 04:18:502630
[email protected]eaa7dd182010-12-14 11:09:002631ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182632 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462633 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502634 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402635 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182636 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502637 }
2638
[email protected]ec5b50d2010-10-09 16:35:182639 return result;
[email protected]377011d2010-07-20 04:18:502640}
[email protected]d7e9a862010-11-03 21:57:492641
[email protected]dc9a74f72012-08-17 18:07:212642bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292643 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212644 return true;
2645 ExtensionRuntimeDataMap::const_iterator it =
2646 extension_runtime_data_.find(extension->id());
2647 return it == extension_runtime_data_.end() ? false :
2648 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492649}
2650
[email protected]eaa7dd182010-12-14 11:09:002651void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292652 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492653 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072654 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272655 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532656 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072657 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492658}
2659
[email protected]dc9a74f72012-08-17 18:07:212660bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2661 ExtensionRuntimeDataMap::const_iterator it =
2662 extension_runtime_data_.find(extension->id());
2663 return it == extension_runtime_data_.end() ? false :
2664 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492665}
2666
[email protected]eaa7dd182010-12-14 11:09:002667void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032668 bool value) {
[email protected]d7e9a862010-11-03 21:57:492669 extension_runtime_data_[extension->id()].being_upgraded = value;
2670}
[email protected]1bead0712010-11-27 17:41:532671
[email protected]e178ad92013-06-28 02:29:252672bool ExtensionService::IsBeingReloaded(
2673 const std::string& extension_id) const {
2674 return ContainsKey(extensions_being_reloaded_, extension_id);
2675}
2676
2677void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432678 bool isBeingReloaded) {
2679 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252680 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432681 else
[email protected]e178ad92013-06-28 02:29:252682 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252683}
2684
[email protected]dc9a74f72012-08-17 18:07:212685bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2686 ExtensionRuntimeDataMap::const_iterator it =
2687 extension_runtime_data_.find(extension->id());
2688 return it == extension_runtime_data_.end() ? false :
2689 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072690}
2691
2692void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2693 bool value) {
2694 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2695}
2696
[email protected]612a1cb12012-10-17 13:18:032697bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2698 // Extensions installed by policy can't be disabled. So even if a previous
2699 // installation disabled the extension, make sure it is now enabled.
2700 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2701 return true;
2702
2703 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2704 return false;
2705
[email protected]41070e8d2012-10-24 01:34:362706 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032707 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542708 // enabling them. Hosted apps are excepted because they are not dangerous
2709 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402710 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2711 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032712 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2713 return false;
2714 }
2715 }
[email protected]612a1cb12012-10-17 13:18:032716
2717 return true;
2718}
[email protected]0db124b02012-11-07 04:55:052719
[email protected]e7aa7b7e2012-11-27 04:51:222720bool ExtensionService::ShouldDelayExtensionUpdate(
2721 const std::string& extension_id,
2722 bool wait_for_idle) const {
2723 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2724
2725 // If delayed updates are globally disabled, or just for this extension,
2726 // don't delay.
2727 if (!install_updates_when_idle_ || !wait_for_idle)
2728 return false;
2729
[email protected]695b5712012-12-06 23:55:282730 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222731 // If there is no old extension, this is not an update, so don't delay.
2732 if (!old)
2733 return false;
2734
[email protected]9367eabc2013-03-01 01:29:292735 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222736 // Delay installation if the extension listens for the onUpdateAvailable
2737 // event.
2738 return system_->event_router()->ExtensionHasEventListener(
2739 extension_id, kOnUpdateAvailableEvent);
2740 } else {
2741 // Delay installation if the extension is not idle.
[email protected]617342a42013-12-18 23:34:032742 return !extension_util::IsExtensionIdle(extension_id, system_);
[email protected]e7aa7b7e2012-11-27 04:51:222743 }
[email protected]0db124b02012-11-07 04:55:052744}
[email protected]fdd679b2012-11-15 20:49:392745
[email protected]399583b2012-12-11 09:33:422746void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:452747 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
2748 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:422749 for (ExtensionSet::const_iterator it = extensions_.begin();
2750 it != extensions_.end(); ++it) {
[email protected]cadac622013-06-11 16:46:362751 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
2752 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
2753 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:422754 }
2755 }
2756
[email protected]9f4e4f082013-06-21 07:11:192757 DCHECK(!installs_delayed_for_gc());
2758 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:422759 BrowserContext::GarbageCollectStoragePartitions(
2760 profile_, active_paths.Pass(),
2761 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
2762 AsWeakPtr()));
2763}
2764
2765void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:192766 set_installs_delayed_for_gc(false);
2767 MaybeFinishDelayedInstallations();
2768}
2769
2770void ExtensionService::MaybeFinishDelayedInstallations() {
2771 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422772 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2773 it != delayed_installs_.end();
2774 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192775 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422776 }
[email protected]9f4e4f082013-06-21 07:11:192777 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2778 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422779 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192780 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422781 }
[email protected]399583b2012-12-11 09:33:422782}
2783
2784void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
2785 extension_prefs_->SetNeedsStorageGarbageCollection(true);
2786}
2787
[email protected]fdd679b2012-11-15 20:49:392788void ExtensionService::OnBlacklistUpdated() {
[email protected]48a359342013-10-30 00:22:002789 blacklist_->GetMalwareIDs(
[email protected]695b5712012-12-06 23:55:282790 GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252791 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282792}
2793
[email protected]3f2a2fa2013-09-24 02:55:252794void ExtensionService::ManageBlacklist(const std::set<std::string>& updated) {
[email protected]695b5712012-12-06 23:55:282795 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2796
[email protected]3f2a2fa2013-09-24 02:55:252797 std::set<std::string> before = blacklisted_extensions_.GetIDs();
[email protected]16bf7ba72013-08-23 11:52:542798 std::set<std::string> no_longer_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252799 base::STLSetDifference<std::set<std::string> >(before, updated);
[email protected]16bf7ba72013-08-23 11:52:542800 std::set<std::string> not_yet_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252801 base::STLSetDifference<std::set<std::string> >(updated, before);
[email protected]695b5712012-12-06 23:55:282802
2803 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
2804 it != no_longer_blacklisted.end(); ++it) {
2805 scoped_refptr<const Extension> extension =
2806 blacklisted_extensions_.GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252807 if (!extension.get()) {
2808 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2809 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282810 continue;
[email protected]3f2a2fa2013-09-24 02:55:252811 }
[email protected]695b5712012-12-06 23:55:282812 blacklisted_extensions_.Remove(*it);
[email protected]3f2a2fa2013-09-24 02:55:252813 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092814 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092815 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092816 extension->location(),
2817 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282818 }
2819
2820 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
2821 it != not_yet_blacklisted.end(); ++it) {
2822 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252823 if (!extension.get()) {
2824 NOTREACHED() << "Extension " << *it << " needs to be "
2825 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282826 continue;
[email protected]3f2a2fa2013-09-24 02:55:252827 }
[email protected]695b5712012-12-06 23:55:282828 blacklisted_extensions_.Insert(extension);
[email protected]3f2a2fa2013-09-24 02:55:252829 extension_prefs_->SetExtensionBlacklisted(extension->id(), true);
[email protected]b0af4792013-10-23 09:12:132830 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092831 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2832 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282833 }
2834
2835 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:392836}
[email protected]75bdcb872013-03-13 00:41:452837
2838void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2839 update_observers_.AddObserver(observer);
2840}
2841
2842void ExtensionService::RemoveUpdateObserver(
2843 extensions::UpdateObserver* observer) {
2844 update_observers_.RemoveObserver(observer);
2845}