blob: 8b931f1321e2929a77c63726f13dd94a40c43c65 [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]bb1bc9b32013-12-21 03:09:1479#include "extensions/browser/extension_registry.h"
[email protected]45928b682013-11-28 08:20:2780#include "extensions/browser/extensions_browser_client.h"
[email protected]301116c62013-11-26 10:37:4581#include "extensions/browser/external_provider_interface.h"
82#include "extensions/browser/management_policy.h"
[email protected]b52f8ca2013-11-28 08:25:2283#include "extensions/browser/pending_extension_manager.h"
[email protected]98b6d942013-11-10 00:34:0784#include "extensions/browser/process_manager.h"
[email protected]a9aa62b312013-11-29 05:35:0685#include "extensions/browser/update_observer.h"
[email protected]836e2982013-05-16 08:07:4286#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:3187#include "extensions/common/error_utils.h"
[email protected]e4452d32013-11-15 23:07:4188#include "extensions/common/extension.h"
[email protected]c81f5d32013-10-26 10:38:4289#include "extensions/common/extensions_client.h"
[email protected]5ef835a2013-11-08 20:42:5790#include "extensions/common/feature_switch.h"
[email protected]d42c11152013-08-22 19:36:3291#include "extensions/common/manifest.h"
[email protected]0c3c9732013-09-16 08:53:4192#include "extensions/common/manifest_constants.h"
[email protected]558878cc82013-11-09 01:25:5193#include "extensions/common/manifest_handlers/background_info.h"
[email protected]1f7de252013-11-06 22:02:0094#include "extensions/common/manifest_handlers/incognito_info.h"
[email protected]301116c62013-11-26 10:37:4595#include "extensions/common/manifest_handlers/shared_module_info.h"
[email protected]c41003472013-10-19 15:37:2596#include "extensions/common/permissions/permission_message_provider.h"
[email protected]e4452d32013-11-15 23:07:4197#include "extensions/common/permissions/permissions_data.h"
[email protected]65187152012-06-02 13:14:1498#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:2599#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]cd67ed52013-10-15 01:22:13100#include "ui/base/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:00101#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38102#include "webkit/browser/database/database_tracker.h"
103#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18104
[email protected]eed367e2011-04-12 03:43:31105#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18106#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32107#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07108#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31109#endif
110
[email protected]55eb70e762012-02-20 17:38:39111using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39112using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36113using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27114using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34115using extensions::Extension;
116using extensions::ExtensionIdSet;
117using extensions::ExtensionInfo;
[email protected]289c44b2013-12-17 03:26:57118using extensions::ExtensionSet;
[email protected]215a7be2012-10-22 19:53:42119using extensions::FeatureSwitch;
[email protected]ffd2f79e2013-11-14 00:11:46120using extensions::InstallVerifier;
[email protected]0d54b682013-11-05 14:15:36121using extensions::ManagementPolicy;
[email protected]1d5e58b2013-01-31 08:41:40122using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06123using extensions::PermissionMessage;
124using extensions::PermissionMessages;
125using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19126using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24127using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05128
[email protected]0c3c9732013-09-16 08:53:41129namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06130
[email protected]b6ab96d2009-08-20 18:58:19131namespace {
132
[email protected]d96eb512012-11-01 23:44:08133// Histogram values for logging events related to externally installed
134// extensions.
135enum ExternalExtensionEvent {
136 EXTERNAL_EXTENSION_INSTALLED = 0,
137 EXTERNAL_EXTENSION_IGNORED,
138 EXTERNAL_EXTENSION_REENABLED,
139 EXTERNAL_EXTENSION_UNINSTALLED,
140 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
141};
142
[email protected]612a1cb12012-10-17 13:18:03143// Prompt the user this many times before considering an extension acknowledged.
144static const int kMaxExtensionAcknowledgePromptCount = 3;
145
[email protected]0db124b02012-11-07 04:55:05146// Wait this many seconds after an extensions becomes idle before updating it.
147static const int kUpdateIdleDelay = 5;
148
[email protected]9bd9a6862012-11-29 09:24:22149// Wait this many seconds before trying to garbage collect extensions again.
150static const int kGarbageCollectRetryDelay = 30;
151
[email protected]36fc54f2013-06-14 02:49:34152// Wait this many seconds after startup to see if there are any extensions
153// which can be garbage collected.
154static const int kGarbageCollectStartupDelay = 30;
155
[email protected]9f4e4f082013-06-21 07:11:19156static bool IsSharedModule(const Extension* extension) {
157 return SharedModuleInfo::IsSharedModule(extension);
158}
159
[email protected]4c9201c42013-08-16 04:56:21160static bool IsCWSSharedModule(const Extension* extension) {
161 return extension->from_webstore() && IsSharedModule(extension);
162}
163
164class SharedModuleProvider : public extensions::ManagementPolicy::Provider {
165 public:
166 SharedModuleProvider() {}
167 virtual ~SharedModuleProvider() {}
168
169 virtual std::string GetDebugPolicyProviderName() const OVERRIDE {
170 return "SharedModuleProvider";
171 }
172
173 virtual bool UserMayModifySettings(const Extension* extension,
[email protected]439f1e32013-12-09 20:09:09174 base::string16* error) const OVERRIDE {
[email protected]4c9201c42013-08-16 04:56:21175 return !IsCWSSharedModule(extension);
176 }
177
178 virtual bool MustRemainEnabled(const Extension* extension,
[email protected]439f1e32013-12-09 20:09:09179 base::string16* error) const OVERRIDE {
[email protected]4c9201c42013-08-16 04:56:21180 return IsCWSSharedModule(extension);
181 }
182
183 private:
184 DISALLOW_COPY_AND_ASSIGN(SharedModuleProvider);
185};
186
187
[email protected]c6d474f82009-12-16 21:11:06188} // namespace
[email protected]b6ab96d2009-08-20 18:58:19189
[email protected]eaa7dd182010-12-14 11:09:00190ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49191 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57192 being_upgraded(false),
193 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49194}
195
[email protected]eaa7dd182010-12-14 11:09:00196ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49197}
198
[email protected]eaa7dd182010-12-14 11:09:00199// ExtensionService.
[email protected]6014d672008-12-05 00:38:25200
[email protected]8e4560b62011-01-14 10:09:14201void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12202 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20203
[email protected]7a4c6852010-09-16 03:44:22204 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27205 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21206 for (i = external_extension_providers_.begin();
207 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14208 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21209 if (i->get()->HasExtension(id))
210 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22211 }
212
[email protected]0f48fca2011-05-19 18:46:35213 // We get the list of external extensions to check from preferences.
214 // It is possible that an extension has preferences but is not loaded.
215 // For example, an extension that requires experimental permissions
216 // will not be loaded if the experimental command line flag is not used.
217 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40218 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35219 // We can't call UninstallExtension with an unloaded/invalid
220 // extension ID.
221 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
222 << "with id: " << id;
223 return;
224 }
[email protected]d6ebc9792011-04-07 18:18:33225 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22226}
227
[email protected]7f8f24f2012-11-15 19:40:14228void ExtensionService::SetFileTaskRunnerForTesting(
229 base::SequencedTaskRunner* task_runner) {
230 file_task_runner_ = task_runner;
231}
232
[email protected]8e4560b62011-01-14 10:09:14233void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22234 external_extension_providers_.clear();
235}
236
[email protected]8e4560b62011-01-14 10:09:14237void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27238 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12239 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21240 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27241 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22242}
243
[email protected]9060d8b02012-01-13 02:14:30244bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22245 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42246 const GURL& update_url,
[email protected]464213a2013-10-15 01:06:48247 Manifest::Location location,
248 int creation_flags,
249 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:38250 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
251 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20252
[email protected]8a87a5332011-08-11 17:54:59253 const Extension* extension = GetExtensionById(id, true);
254 if (extension) {
255 // Already installed. Skip this install if the current location has
256 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40257 Manifest::Location current = extension->location();
258 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30259 return false;
[email protected]8a87a5332011-08-11 17:54:59260 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22261 }
[email protected]9060d8b02012-01-13 02:14:30262
263 // Add |id| to the set of pending extensions. If it can not be added,
264 // then there is already a pending record from a higher-priority install
265 // source. In this case, signal that this extension will not be
266 // installed by returning false.
267 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]464213a2013-10-15 01:06:48268 id, update_url, location, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:30269 return false;
[email protected]31bb5ee62012-09-12 22:58:40270 }
[email protected]9060d8b02012-01-13 02:14:30271
[email protected]94fde232012-04-27 10:22:30272 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30273 return true;
[email protected]7a4c6852010-09-16 03:44:22274}
275
[email protected]400c4392013-12-13 14:41:58276const Extension* ExtensionService::GetInstalledExtensionByUrl(
277 const GURL& url) const {
[email protected]bb1bc9b32013-12-21 03:09:14278 return registry_->enabled_extensions().GetExtensionOrAppByURL(url);
[email protected]400c4392013-12-13 14:41:58279}
280
[email protected]dc9a74f72012-08-17 18:07:21281const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]400c4392013-12-13 14:41:58282 const Extension* extension = GetInstalledExtensionByUrl(url);
[email protected]dc9a74f72012-08-17 18:07:21283 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09284}
285
[email protected]dc9a74f72012-08-17 18:07:21286bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09287 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27288}
289
[email protected]6aeac8342010-10-01 20:21:18290// static
[email protected]d6ebc9792011-04-07 18:18:33291// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30292// --uninstall-extension, and to uninstall an extension via sync. The LOG
293// statements within this function are used to inform the user if the uninstall
294// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00295bool ExtensionService::UninstallExtensionHelper(
296 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18297 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33298 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40299 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18300 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33301 << "id: " << extension_id;
302 return false;
[email protected]6aeac8342010-10-01 20:21:18303 }
304
[email protected]d6ebc9792011-04-07 18:18:33305 // The following call to UninstallExtension will not allow an uninstall of a
306 // policy-controlled extension.
[email protected]439f1e32013-12-09 20:09:09307 base::string16 error;
[email protected]d6ebc9792011-04-07 18:18:33308 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
309 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
310 << ": " << error;
311 return false;
312 }
[email protected]95da88c42011-03-31 10:07:33313
[email protected]6aeac8342010-10-01 20:21:18314 return true;
315}
316
[email protected]eaa7dd182010-12-14 11:09:00317ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24318 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45319 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23320 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39321 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03322 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35323 bool extensions_enabled,
324 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39325 : extensions::Blacklist::Observer(blacklist),
326 profile_(profile),
[email protected]bd306722012-07-11 20:43:59327 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43328 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28329 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38330 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]f8aefb132013-10-30 09:29:52331 extension_sync_service_(NULL),
[email protected]bb1bc9b32013-12-21 03:09:14332 registry_(new extensions::ExtensionRegistry),
[email protected]9c009092013-05-01 03:14:09333 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23334 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03335 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13336 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33337 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35338 ready_(ready),
[email protected]dff1e042012-04-27 10:59:18339 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54340 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19341 installs_delayed_for_gc_(false),
[email protected]f8aefb132013-10-30 09:29:52342 is_first_run_(false) {
[email protected]c04fd3dd2013-09-05 08:20:10343#if defined(OS_CHROMEOS)
344 disable_garbage_collection_ = false;
345#endif
[email protected]a29a517a2011-01-21 21:11:12346 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20347
[email protected]36a784c2009-06-23 06:21:08348 // Figure out if extension installation should be enabled.
[email protected]367d9b172013-12-03 00:31:02349 if (extensions::ExtensionsBrowserClient::Get()->AreExtensionsDisabled(
350 *command_line, profile))
[email protected]6d60703b2009-08-29 01:29:23351 extensions_enabled_ = false;
[email protected]36a784c2009-06-23 06:21:08352
[email protected]3c4abc82012-10-22 22:25:54353 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
354 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27355 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[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_CREATED,
[email protected]ad50def52011-10-19 23:17:07358 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27359 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07360 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08361 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
362 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28363 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03364 base::Closure callback =
365 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
366 base::Unretained(this));
367 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
368 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24369 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29370
[email protected]93fd78f42009-07-10 16:43:17371 // Set up the ExtensionUpdater
372 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02373 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17374 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25375 base::StringToInt(command_line->GetSwitchValueASCII(
376 switches::kExtensionsUpdateFrequency),
377 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17378 }
[email protected]42a08162012-03-16 18:09:11379 updater_.reset(new extensions::ExtensionUpdater(this,
380 extension_prefs,
381 profile->GetPrefs(),
382 profile,
383 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17384 }
385
[email protected]25ae0152011-11-18 14:40:02386 component_loader_.reset(
387 new extensions::ComponentLoader(this,
388 profile->GetPrefs(),
[email protected]55e16cd2013-12-18 04:36:08389 g_browser_process->local_state(),
390 profile));
[email protected]8e4560b62011-01-14 10:09:14391
[email protected]0436b102011-04-15 18:30:03392 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25393 extensions::ExternalProviderImpl::CreateExternalProviders(
394 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05395 }
[email protected]b671760b2010-07-15 21:13:47396
[email protected]74474042013-11-21 12:03:54397 // Set this as the ExtensionService for app sorting to ensure it causes syncs
398 // if required.
[email protected]b3aa7182013-04-25 04:45:23399 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
400
[email protected]9ce11d22012-08-08 23:20:13401#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58402 extension_action_storage_manager_.reset(
403 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13404#endif
[email protected]c77f2352012-08-08 22:07:58405
[email protected]4c9201c42013-08-16 04:56:21406 shared_module_policy_provider_.reset(new SharedModuleProvider);
407
[email protected]cb0e50312011-05-09 15:03:07408 // How long is the path to the Extensions directory?
409 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
410 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25411}
412
[email protected]84df8332011-12-06 18:22:46413const ExtensionSet* ExtensionService::extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14414 return &registry_->enabled_extensions();
[email protected]ddb1e5a2010-12-13 20:10:45415}
416
[email protected]84df8332011-12-06 18:22:46417const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14418 return &registry_->disabled_extensions();
[email protected]ddb1e5a2010-12-13 20:10:45419}
420
[email protected]84df8332011-12-06 18:22:46421const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14422 return &registry_->terminated_extensions();
[email protected]bb7f40952011-01-13 00:21:20423}
424
[email protected]695b5712012-12-06 23:55:28425const ExtensionSet* ExtensionService::blacklisted_extensions() const {
[email protected]bb1bc9b32013-12-21 03:09:14426 return &registry_->blacklisted_extensions();
[email protected]695b5712012-12-06 23:55:28427}
428
[email protected]9e9c1d12013-07-31 01:58:12429const ExtensionSet* ExtensionService::delayed_installs() const {
430 return &delayed_installs_;
431}
432
[email protected]620b5a62013-11-18 04:21:08433scoped_ptr<ExtensionSet>
[email protected]695b5712012-12-06 23:55:28434 ExtensionService::GenerateInstalledExtensionsSet() const {
435 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]bb1bc9b32013-12-21 03:09:14436 installed_extensions->InsertAll(registry_->enabled_extensions());
437 installed_extensions->InsertAll(registry_->disabled_extensions());
438 installed_extensions->InsertAll(registry_->terminated_extensions());
439 installed_extensions->InsertAll(registry_->blacklisted_extensions());
440 return installed_extensions.Pass();
[email protected]7f4308d2012-01-18 07:43:01441}
442
[email protected]3f213ad2012-07-26 23:39:41443extensions::PendingExtensionManager*
444 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37445 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45446}
447
[email protected]eaa7dd182010-12-14 11:09:00448ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17449 // No need to unload extensions here because they are profile-scoped, and the
450 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17451
[email protected]5df038b2012-07-16 19:03:27452 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14453 for (i = external_extension_providers_.begin();
454 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27455 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14456 provider->ServiceShutdown();
457 }
[email protected]6014d672008-12-05 00:38:25458}
459
[email protected]037228a2012-10-05 01:36:16460void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21461 system_->management_policy()->UnregisterProvider(
462 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16463}
464
[email protected]b2907fd2011-03-25 16:43:37465const Extension* ExtensionService::GetExtensionById(
466 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47467 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28468 if (include_disabled) {
469 // Include blacklisted extensions here because there are hundreds of
470 // callers of this function, and many might assume that this includes those
471 // that have been disabled due to blacklisting.
[email protected]d2fff5d2013-12-11 02:49:19472 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
[email protected]695b5712012-12-06 23:55:28473 }
474 return GetExtensionById(id, include_mask);
475}
476
[email protected]399583b2012-12-11 09:33:42477GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
478 return content::SiteInstance::GetSiteForURL(
479 profile_,
480 Extension::GetBaseURLFromExtensionId(extension_id));
481}
482
[email protected]695b5712012-12-06 23:55:28483const Extension* ExtensionService::GetExtensionById(
484 const std::string& id, int include_mask) const {
485 std::string lowercase_id = StringToLowerASCII(id);
486 if (include_mask & INCLUDE_ENABLED) {
[email protected]bb1bc9b32013-12-21 03:09:14487 const Extension* extension =
488 registry_->enabled_extensions().GetByID(lowercase_id);
[email protected]695b5712012-12-06 23:55:28489 if (extension)
490 return extension;
491 }
492 if (include_mask & INCLUDE_DISABLED) {
[email protected]bb1bc9b32013-12-21 03:09:14493 const Extension* extension =
494 registry_->disabled_extensions().GetByID(lowercase_id);
[email protected]695b5712012-12-06 23:55:28495 if (extension)
496 return extension;
497 }
498 if (include_mask & INCLUDE_TERMINATED) {
[email protected]bb1bc9b32013-12-21 03:09:14499 const Extension* extension =
500 registry_->terminated_extensions().GetByID(lowercase_id);
[email protected]695b5712012-12-06 23:55:28501 if (extension)
502 return extension;
503 }
504 if (include_mask & INCLUDE_BLACKLISTED) {
[email protected]bb1bc9b32013-12-21 03:09:14505 const Extension* extension =
506 registry_->blacklisted_extensions().GetByID(lowercase_id);
[email protected]695b5712012-12-06 23:55:28507 if (extension)
508 return extension;
509 }
510 return NULL;
[email protected]78994ab02010-12-08 18:06:44511}
512
[email protected]eaa7dd182010-12-14 11:09:00513void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12514 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20515
[email protected]3350d712013-11-18 09:32:24516 base::Time begin_time = base::Time::Now();
517
[email protected]4a10006a2013-05-17 23:18:35518 DCHECK(!is_ready()); // Can't redo init.
[email protected]bb1bc9b32013-12-21 03:09:14519 DCHECK_EQ(registry_->enabled_extensions().size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32520
[email protected]820d9bd2013-04-03 03:46:03521 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03522 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
523 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
524 // The sole purpose of this launch is to install a new extension from CWS
525 // and immediately terminate: loading already installed extensions is
526 // unnecessary and may interfere with the inline install dialog (e.g. if an
527 // extension listens to onStartup and opens a window).
528 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25529 } else {
[email protected]47b60072013-06-20 10:35:56530 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
531 component_loader_->LoadAll();
532 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19533
[email protected]59ee99d2013-10-11 15:46:16534 ReconcileKnownDisabled();
535
[email protected]2edd57852013-08-14 22:07:40536 // Attempt to re-enable extensions whose only disable reason is reloading.
537 std::vector<std::string> extensions_to_enable;
[email protected]bb1bc9b32013-12-21 03:09:14538 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
539 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
540 iter != disabled_extensions.end(); ++iter) {
[email protected]2edd57852013-08-14 22:07:40541 const Extension* e = iter->get();
542 if (extension_prefs_->GetDisableReasons(e->id()) ==
543 Extension::DISABLE_RELOAD) {
544 extensions_to_enable.push_back(e->id());
545 }
546 }
547 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
548 it != extensions_to_enable.end(); ++it) {
549 EnableExtension(*it);
550 }
551
[email protected]9f4e4f082013-06-21 07:11:19552 // Finish install (if possible) of extensions that were still delayed while
553 // the browser was shut down.
554 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
555 extension_prefs_->GetAllDelayedInstallInfo());
556 for (size_t i = 0; i < delayed_info->size(); ++i) {
557 ExtensionInfo* info = delayed_info->at(i).get();
558 scoped_refptr<const Extension> extension(NULL);
559 if (info->extension_manifest) {
560 std::string error;
561 extension = Extension::Create(
562 info->extension_path,
563 info->extension_location,
564 *info->extension_manifest,
565 extension_prefs_->GetDelayedInstallCreationFlags(
566 info->extension_id),
567 info->extension_id,
568 &error);
569 if (extension.get())
570 delayed_installs_.Insert(extension);
571 }
572 }
573 MaybeFinishDelayedInstallations();
574
575 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
576 extension_prefs_->GetAllDelayedInstallInfo());
577 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
578 delayed_info2->size() - delayed_info->size());
579
[email protected]47b60072013-06-20 10:35:56580 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25581
[email protected]47b60072013-06-20 10:35:56582 // TODO(erikkay) this should probably be deferred to a future point
583 // rather than running immediately at startup.
584 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57585
[email protected]68e5cc72013-12-17 02:41:53586 InstallVerifier* verifier =
587 extensions::ExtensionSystem::Get(profile_)->install_verifier();
588 if (verifier->NeedsBootstrap())
589 VerifyAllExtensions();
590
[email protected]47b60072013-06-20 10:35:56591 base::MessageLoop::current()->PostDelayedTask(
592 FROM_HERE,
593 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
594 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42595
[email protected]820d9bd2013-04-03 03:46:03596 if (extension_prefs_->NeedsStorageGarbageCollection()) {
597 GarbageCollectIsolatedStorage();
598 extension_prefs_->SetNeedsStorageGarbageCollection(false);
599 }
[email protected]4c9201c42013-08-16 04:56:21600 system_->management_policy()->RegisterProvider(
601 shared_module_policy_provider_.get());
[email protected]399583b2012-12-11 09:33:42602 }
[email protected]3350d712013-11-18 09:32:24603
604 UMA_HISTOGRAM_TIMES("Extensions.ExtensionServiceInitTime",
605 base::Time::Now() - begin_time);
[email protected]6014d672008-12-05 00:38:25606}
607
[email protected]68e5cc72013-12-17 02:41:53608void ExtensionService::VerifyAllExtensions() {
609 ExtensionIdSet to_add;
610 scoped_ptr<ExtensionSet> all_extensions = GenerateInstalledExtensionsSet();
611
612 for (ExtensionSet::const_iterator i = all_extensions->begin();
613 i != all_extensions->end(); ++i) {
614 const Extension& extension = **i;
615
616 if (extensions::ManifestURL::UpdatesFromGallery(&extension) &&
617 extension.is_extension())
618 to_add.insert(extension.id());
619 }
620 extensions::ExtensionSystem::Get(profile_)->install_verifier()->AddMany(
621 to_add, base::Bind(&ExtensionService::FinishVerifyAllExtensions,
622 AsWeakPtr()));
623}
624
625void ExtensionService::FinishVerifyAllExtensions(bool success) {
626 if (success) {
627 // Check to see if any currently unverified extensions became verified.
628 InstallVerifier* verifier =
629 extensions::ExtensionSystem::Get(profile_)->install_verifier();
[email protected]bb1bc9b32013-12-21 03:09:14630 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
631 for (ExtensionSet::const_iterator i = disabled_extensions.begin();
632 i != disabled_extensions.end(); ++i) {
[email protected]68e5cc72013-12-17 02:41:53633 const Extension& extension = **i;
634 int disable_reasons = extension_prefs_->GetDisableReasons(extension.id());
635 if (disable_reasons & Extension::DISABLE_NOT_VERIFIED &&
636 !verifier->MustRemainDisabled(&extension, NULL, NULL)) {
637 extension_prefs_->RemoveDisableReason(extension.id(),
638 Extension::DISABLE_NOT_VERIFIED);
639 // Notify interested observers (eg the extensions settings page) by
640 // sending an UNLOADED notification.
641 //
642 // TODO(asargent) - this is a slight hack because it's already
643 // disabled; the right solution might be to add a separate listener
644 // interface for DisableReason's changing. http://crbug.com/328916
645 UnloadedExtensionInfo details(&extension,
646 UnloadedExtensionInfo::REASON_DISABLE);
647 content::NotificationService::current()->Notify(
648 chrome::NOTIFICATION_EXTENSION_UNLOADED,
649 content::Source<Profile>(profile_),
650 content::Details<UnloadedExtensionInfo>(&details));
651 }
652 }
653 // Might disable some extensions.
654 CheckManagementPolicy();
655 }
656}
657
[email protected]31bb5ee62012-09-12 22:58:40658bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45659 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40660 const GURL& download_url,
661 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12662 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54663 if (browser_terminating_) {
664 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
665 // Leak the temp file at extension_path. We don't want to add to the disk
666 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
667 // the file is in the OS temp directory which should be cleaned up for us.
668 return false;
669 }
[email protected]a8af9fdb2010-10-28 21:52:20670
[email protected]3f213ad2012-07-26 23:39:41671 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06672 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32673
[email protected]695b5712012-12-06 23:55:28674 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06675 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33676 LOG(WARNING) << "Will not update extension " << id
677 << " because it is not installed or pending";
678 // Delete extension_path since we're not creating a CrxInstaller
679 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14680 if (!GetFileTaskRunner()->PostTask(
681 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36682 base::Bind(
[email protected]7296f2762011-11-21 19:23:44683 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36684 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03685
686 return false;
[email protected]e957fe52009-06-23 16:51:05687 }
688
[email protected]aa142702010-03-26 01:26:33689 // We want a silent install only for non-pending extensions and
690 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37691 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05692 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37693 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33694
[email protected]f8636f92013-08-09 21:02:37695 scoped_refptr<CrxInstaller> installer(
696 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16697 installer->set_expected_id(id);
[email protected]464213a2013-10-15 01:06:48698 int creation_flags = Extension::NO_FLAGS;
[email protected]51a3bf8b2012-06-08 22:53:06699 if (pending_extension_info) {
700 installer->set_install_source(pending_extension_info->install_source());
701 if (pending_extension_info->install_silently())
702 installer->set_allow_silent_install(true);
[email protected]464213a2013-10-15 01:06:48703 creation_flags = pending_extension_info->creation_flags();
704 if (pending_extension_info->mark_acknowledged())
705 AcknowledgeExternalExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06706 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41707 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06708 }
[email protected]7d8b7072012-04-07 01:07:46709 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45710 // its auto-update URL is from the webstore, treat it as a webstore install.
711 // Note that we ignore some older extensions with blank auto-update URLs
712 // because we are mostly concerned with restrictions on NaCl extensions,
713 // which are newer.
[email protected]75764512011-12-19 19:54:28714 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38715 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35716 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06717 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53718 creation_flags |= Extension::FROM_WEBSTORE;
719 }
720
721 // Bookmark apps being updated is kind of a contradiction, but that's because
722 // we mark the default apps as bookmark apps, and they're hosted in the web
723 // store, thus they can get updated. See http://crbug.com/101605 for more
724 // details.
725 if (extension && extension->from_bookmark())
726 creation_flags |= Extension::FROM_BOOKMARK;
727
[email protected]e33bbc22012-08-27 22:05:46728 if (extension && extension->was_installed_by_default())
729 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
730
[email protected]a12ce8b22012-01-17 18:40:53731 installer->set_creation_flags(creation_flags);
732
[email protected]6dfbbf82010-03-12 23:09:16733 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06734 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07735 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16736 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03737
738 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09739 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03740
741 return true;
[email protected]e957fe52009-06-23 16:51:05742}
743
[email protected]e178ad92013-06-28 02:29:25744void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12745 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05746
747 // If the extension is already reloading, don't reload again.
748 if (extension_prefs_->GetDisableReasons(extension_id) &
749 Extension::DISABLE_RELOAD) {
750 return;
751 }
752
[email protected]650b2d52013-02-10 03:41:45753 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02754 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40755
[email protected]f17dbd42010-08-16 23:21:10756 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06757 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29758 // If the extension has an inspector open for its background page, detach
759 // the inspector and hang onto a cookie for it, so that we can reattach
760 // later.
[email protected]31d8f5f22012-04-02 15:22:08761 // TODO(yoz): this is not incognito-safe!
[email protected]98b6d942013-11-10 00:34:07762 extensions::ProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13763 extensions::ExtensionHost* host =
764 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36765 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29766 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09767 scoped_refptr<DevToolsAgentHost> agent_host =
768 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
769 agent_host->DisconnectRenderViewHost();
770 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29771 }
772
[email protected]b65272f2009-08-31 15:47:06773 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50774 // BeingUpgraded is set back to false when the extension is added.
775 SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03776 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03777 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16778 } else {
779 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06780 }
781
[email protected]9f4e4f082013-06-21 07:11:19782 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48783 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05784 return;
785 }
786
[email protected]43ceb002012-02-10 23:19:15787 // If we're reloading a component extension, use the component extension
788 // loader's reloader.
789 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25790 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15791 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25792 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15793 return;
794 }
795
[email protected]e6090e42010-03-23 22:44:08796 // Check the installed extensions to see if what we're reloading was already
797 // installed.
[email protected]e178ad92013-06-28 02:29:25798 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08799 scoped_ptr<ExtensionInfo> installed_extension(
800 extension_prefs_->GetInstalledExtensionInfo(extension_id));
801 if (installed_extension.get() &&
802 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01803 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08804 } else {
[email protected]d8c8f25f2011-11-02 18:18:01805 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08806 // We should always be able to remember the extension's path. If it's not in
807 // the map, someone failed to update |unloaded_extension_paths_|.
808 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01809 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08810 }
[email protected]e178ad92013-06-28 02:29:25811 // When reloading is done, mark this extension as done reloading.
812 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40813}
814
[email protected]fa2416f2011-05-03 08:41:20815bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08816 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20817 bool external_uninstall,
[email protected]439f1e32013-12-09 20:09:09818 base::string16* error) {
[email protected]a29a517a2011-01-21 21:11:12819 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20820
[email protected]0d6ec3a72011-09-02 02:09:43821 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25822
[email protected]e7afe2452010-08-22 16:19:13823 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09824 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32825
[email protected]95da88c42011-03-31 10:07:33826 // Policy change which triggers an uninstall will always set
827 // |external_uninstall| to true so this is the only way to uninstall
828 // managed extensions.
[email protected]65187152012-06-02 13:14:14829 if (!external_uninstall &&
830 !system_->management_policy()->UserMayModifySettings(
831 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07832 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27833 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53834 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09835 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33836 return false;
837 }
[email protected]95da88c42011-03-31 10:07:33838
[email protected]65f173552012-06-28 22:43:58839 syncer::SyncChange sync_change;
[email protected]f8aefb132013-10-30 09:29:52840 if (extension_sync_service_) {
841 sync_change = extension_sync_service_->PrepareToSyncUninstallExtension(
842 extension.get(), is_ready());
[email protected]3bdba0d2011-08-23 07:17:30843 }
844
[email protected]ffd2f79e2013-11-14 00:11:46845 extensions::ExtensionSystem::Get(profile_)->install_verifier()->Remove(
846 extension->id());
847
[email protected]dc24976f2013-06-02 21:15:09848 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08849 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
850 EXTERNAL_EXTENSION_UNINSTALLED,
851 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02852 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
853 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
854 EXTERNAL_EXTENSION_UNINSTALLED,
855 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
856 } else {
857 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
858 EXTERNAL_EXTENSION_UNINSTALLED,
859 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
860 }
[email protected]d96eb512012-11-01 23:44:08861 }
[email protected]9b217652010-10-08 22:04:23862 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08863 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09864 RecordPermissionMessagesHistogram(extension.get(),
865 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23866
[email protected]831aa212010-03-26 13:55:19867 // Unload before doing more cleanup to ensure that nothing is hanging on to
868 // any of these resources.
[email protected]b0af4792013-10-23 09:12:13869 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19870
[email protected]9f1087e2009-06-15 17:29:32871 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05872 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14873 if (!GetFileTaskRunner()->PostTask(
874 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36875 base::Bind(
[email protected]14908b72011-04-20 06:54:36876 &extension_file_util::UninstallExtension,
877 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20878 extension_id)))
[email protected]14908b72011-04-20 06:54:36879 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32880 }
881
[email protected]6b414c232013-06-05 07:53:34882 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36883 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43884 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09885 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43886
[email protected]399583b2012-12-11 09:33:42887 if (is_storage_isolated) {
888 BrowserContext::AsyncObliterateStoragePartition(
889 profile_,
890 GetSiteForExtensionId(extension_id),
891 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
892 AsWeakPtr()));
893 } else {
894 if (extension->is_hosted_app() &&
895 !profile_->GetExtensionSpecialStoragePolicy()->
896 IsStorageProtected(launch_web_url_origin)) {
897 extensions::DataDeleter::StartDeleting(
898 profile_, extension_id, launch_web_url_origin);
899 }
900 extensions::DataDeleter::StartDeleting(profile_, extension_id,
901 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43902 }
[email protected]0d6ec3a72011-09-02 02:09:43903
[email protected]0dfe05c2011-02-23 23:03:36904 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06905
906 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07907 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27908 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53909 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09910 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33911
[email protected]f8aefb132013-10-30 09:29:52912 if (extension_sync_service_) {
913 extension_sync_service_->ProcessSyncUninstallExtension(extension_id,
914 sync_change);
[email protected]3bdba0d2011-08-23 07:17:30915 }
916
[email protected]399583b2012-12-11 09:33:42917 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05918
[email protected]198b5902013-06-27 10:36:11919 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19920
[email protected]d4eda592013-09-18 03:37:57921 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
922 external_uninstall);
923
[email protected]333b1de2011-09-12 18:28:50924 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18925 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
926
[email protected]d6ebc9792011-04-07 18:18:33927 return true;
[email protected]c10da4b02010-03-25 14:38:32928}
929
[email protected]c3cfb012011-04-06 22:07:35930bool ExtensionService::IsExtensionEnabled(
931 const std::string& extension_id) const {
[email protected]bb1bc9b32013-12-21 03:09:14932 if (registry_->enabled_extensions().Contains(extension_id) ||
933 registry_->terminated_extensions().Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18934 return true;
[email protected]dc9a74f72012-08-17 18:07:21935 }
[email protected]36429da2011-07-11 20:25:18936
[email protected]bb1bc9b32013-12-21 03:09:14937 if (registry_->disabled_extensions().Contains(extension_id) ||
938 registry_->blacklisted_extensions().Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25939 return false;
[email protected]695b5712012-12-06 23:55:28940 }
[email protected]ad83ca242011-07-29 01:32:25941
942 // If the extension hasn't been loaded yet, check the prefs for it. Assume
943 // enabled unless otherwise noted.
944 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31945 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35946}
947
948bool ExtensionService::IsExternalExtensionUninstalled(
949 const std::string& extension_id) const {
950 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
951}
952
[email protected]eaa7dd182010-12-14 11:09:00953void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12954 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20955
[email protected]06f92562011-04-29 19:27:31956 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39957 return;
[email protected]bb1bc9b32013-12-21 03:09:14958 const Extension* extension =
959 registry_->disabled_extensions().GetByID(extension_id);
[email protected]0d54b682013-11-05 14:15:36960
961 ManagementPolicy* policy = system_->management_policy();
962 if (extension && policy->MustRemainDisabled(extension, NULL, NULL)) {
963 UMA_HISTOGRAM_COUNTS_100("Extensions.EnableDeniedByPolicy", 1);
964 return;
965 }
[email protected]0c6da502009-08-14 22:32:39966
[email protected]b3317ad2011-04-28 23:46:00967 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28968 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52969
[email protected]06f92562011-04-29 19:27:31970 // This can happen if sync enables an extension that is not
971 // installed yet.
972 if (!extension)
973 return;
974
[email protected]d96eb512012-11-01 23:44:08975 if (IsUnacknowledgedExternalExtension(extension)) {
976 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
977 EXTERNAL_EXTENSION_REENABLED,
978 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02979 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
980 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
981 EXTERNAL_EXTENSION_REENABLED,
982 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
983 } else {
984 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
985 EXTERNAL_EXTENSION_REENABLED,
986 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
987 }
[email protected]612a1cb12012-10-17 13:18:03988 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08989 }
[email protected]612a1cb12012-10-17 13:18:03990
[email protected]0c6da502009-08-14 22:32:39991 // Move it over to the enabled list.
[email protected]bb1bc9b32013-12-21 03:09:14992 registry_->AddEnabled(make_scoped_refptr(extension));
993 registry_->RemoveDisabled(extension->id());
[email protected]0c6da502009-08-14 22:32:39994
[email protected]62d30f42009-10-01 22:36:06995 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30996
[email protected]f74d7f32012-06-19 19:22:51997 // Notify listeners that the extension was enabled.
998 content::NotificationService::current()->Notify(
999 chrome::NOTIFICATION_EXTENSION_ENABLED,
1000 content::Source<Profile>(profile_),
1001 content::Details<const Extension>(extension));
1002
[email protected]f8aefb132013-10-30 09:29:521003 if (extension_sync_service_)
1004 extension_sync_service_->SyncEnableExtension(*extension);
[email protected]0c6da502009-08-14 22:32:391005}
1006
[email protected]44d62b62012-04-11 00:06:031007void ExtensionService::DisableExtension(
1008 const std::string& extension_id,
1009 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:121010 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201011
[email protected]b2ba9962009-12-10 20:10:151012 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:311013 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:521014 return;
[email protected]1784e83a2009-09-08 21:01:521015
[email protected]06f92562011-04-29 19:27:311016 const Extension* extension = GetInstalledExtension(extension_id);
1017 // |extension| can be NULL if sync disables an extension that is not
1018 // installed yet.
[email protected]65187152012-06-02 13:14:141019 if (extension &&
[email protected]505ccdc2013-06-20 20:15:351020 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:141021 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:331022 return;
[email protected]65187152012-06-02 13:14:141023 }
[email protected]95da88c42011-03-31 10:07:331024
[email protected]b3317ad2011-04-28 23:46:001025 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:281026 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:521027
[email protected]695b5712012-12-06 23:55:281028 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
1029 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:311030 if (!extension)
1031 return;
1032
[email protected]64742de32013-04-22 08:44:341033 // Reset the background_page_ready flag
1034 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
1035 extension_runtime_data_[extension->id()].background_page_ready = false;
1036
[email protected]5c094792012-09-07 19:44:531037 // Move it over to the disabled list. Don't send a second unload notification
1038 // for terminated extensions being disabled.
[email protected]bb1bc9b32013-12-21 03:09:141039 registry_->AddDisabled(make_scoped_refptr(extension));
1040 if (registry_->enabled_extensions().Contains(extension->id())) {
1041 registry_->RemoveEnabled(extension->id());
[email protected]b0af4792013-10-23 09:12:131042 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::REASON_DISABLE);
[email protected]5c094792012-09-07 19:44:531043 } else {
[email protected]bb1bc9b32013-12-21 03:09:141044 registry_->RemoveTerminated(extension->id());
[email protected]5c094792012-09-07 19:44:531045 }
[email protected]3bdba0d2011-08-23 07:17:301046
[email protected]f8aefb132013-10-30 09:29:521047 if (extension_sync_service_)
1048 extension_sync_service_->SyncDisableExtension(*extension);
[email protected]1784e83a2009-09-08 21:01:521049}
1050
[email protected]1abf05e2013-07-09 17:04:361051void ExtensionService::DisableUserExtensions(
1052 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:141053 extensions::ManagementPolicy* management_policy =
1054 system_->management_policy();
1055 extensions::ExtensionList to_disable;
1056
[email protected]bb1bc9b32013-12-21 03:09:141057 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1058 for (ExtensionSet::const_iterator extension = enabled_set.begin();
1059 extension != enabled_set.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111060 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141061 to_disable.push_back(*extension);
1062 }
[email protected]bb1bc9b32013-12-21 03:09:141063 const ExtensionSet& terminated_set = registry_->terminated_extensions();
1064 for (ExtensionSet::const_iterator extension = terminated_set.begin();
1065 extension != terminated_set.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111066 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141067 to_disable.push_back(*extension);
1068 }
1069
1070 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1071 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381072 if ((*extension)->was_installed_by_default() &&
1073 extension_urls::IsWebstoreUpdateUrl(
1074 extensions::ManifestURL::GetUpdateURL(*extension)))
1075 continue;
[email protected]1abf05e2013-07-09 17:04:361076 const std::string& id = (*extension)->id();
1077 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1078 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141079 }
1080}
1081
[email protected]eaa7dd182010-12-14 11:09:001082void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281083 const Extension* extension) {
1084 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491085 RecordPermissionMessagesHistogram(
1086 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251087 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1088 EnableExtension(extension->id());
1089}
1090
[email protected]009633c2013-03-07 22:08:281091void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451092 CHECK(extension);
1093 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281094 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451095}
1096
[email protected]fe2dd7742011-04-19 22:52:491097// static
1098void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051099 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171100 // Since this is called from multiple sources, and since the histogram macros
1101 // use statics, we need to manually lookup the histogram ourselves.
1102 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491103 histogram,
1104 1,
[email protected]c2e66e12012-06-27 06:27:061105 PermissionMessage::kEnumBoundary,
1106 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171107 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491108
[email protected]13c68b62013-05-17 11:29:051109 PermissionMessages permissions =
1110 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491111 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061112 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491113 } else {
[email protected]c2e66e12012-06-27 06:27:061114 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521115 it != permissions.end(); ++it)
1116 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491117 }
1118}
1119
[email protected]eaa7dd182010-12-14 11:09:001120void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421121 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061122 // was loaded, otherwise a race can arise where a renderer that is created
1123 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421124 // that the request context doesn't yet know about. The profile is responsible
1125 // for ensuring its URLRequestContexts appropriately discover the loaded
1126 // extension.
[email protected]31d8f5f22012-04-02 15:22:081127 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061128
[email protected]d5949312012-12-03 22:13:301129 // Tell renderers about the new extension, unless it's a theme (renderers
1130 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191131 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491132 for (content::RenderProcessHost::iterator i(
1133 content::RenderProcessHost::AllHostsIterator());
1134 !i.IsAtEnd(); i.Advance()) {
1135 content::RenderProcessHost* host = i.GetCurrentValue();
1136 Profile* host_profile =
1137 Profile::FromBrowserContext(host->GetBrowserContext());
1138 if (host_profile->GetOriginalProfile() ==
1139 profile_->GetOriginalProfile()) {
1140 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1141 1, ExtensionMsg_Loaded_Params(extension));
1142 host->Send(
1143 new ExtensionMsg_Loaded(loaded_extensions));
1144 }
[email protected]c8d407e2011-04-28 21:27:171145 }
[email protected]77a6970c2011-04-23 16:58:561146 }
1147
[email protected]3442a662012-01-12 08:06:171148 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1149 // extension.
1150 //
1151 // NOTE: It is important that this happen after notifying the renderers about
1152 // the new extensions so that if we navigate to an extension URL in
1153 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1154 content::NotificationService::current()->Notify(
1155 chrome::NOTIFICATION_EXTENSION_LOADED,
1156 content::Source<Profile>(profile_),
1157 content::Details<const Extension>(extension));
1158
[email protected]c8d407e2011-04-28 21:27:171159 // Tell a random-ass collection of other subsystems about the new extension.
1160 // TODO(aa): What should we do with all this goop? Can it move into the
1161 // relevant objects via EXTENSION_LOADED?
1162
1163 profile_->GetExtensionSpecialStoragePolicy()->
1164 GrantRightsForExtension(extension);
1165
1166 UpdateActiveExtensionsInCrashReporter();
1167
[email protected]c8d407e2011-04-28 21:27:171168 // If the extension has permission to load chrome://favicon/ resources we need
1169 // to make sure that the FaviconSource is registered with the
1170 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051171 if (extensions::PermissionsData::HasHostPermission(
1172 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171173 FaviconSource* favicon_source = new FaviconSource(profile_,
1174 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531175 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171176 }
[email protected]b07e606e2012-09-15 20:16:151177
1178#if !defined(OS_ANDROID)
1179 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051180 if (extensions::PermissionsData::HasHostPermission(
1181 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151182 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531183 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151184 }
1185#endif
1186
[email protected]5eddc3e2011-10-26 04:33:311187 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051188 if (extensions::PermissionsData::HasHostPermission(
1189 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511190 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531191 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311192 }
[email protected]62d30f42009-10-01 22:36:061193}
1194
[email protected]a9f39a312010-12-23 22:14:271195void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591196 const Extension* extension,
[email protected]b0af4792013-10-23 09:12:131197 UnloadedExtensionInfo::Reason reason) {
[email protected]a9f39a312010-12-23 22:14:271198 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071199 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271200 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531201 content::Source<Profile>(profile_),
1202 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061203
[email protected]f3b1a082011-11-18 00:34:301204 for (content::RenderProcessHost::iterator i(
1205 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561206 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301207 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081208 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301209 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081210 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171211 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561212 }
1213
[email protected]31d8f5f22012-04-02 15:22:081214 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171215 profile_->GetExtensionSpecialStoragePolicy()->
1216 RevokeRightsForExtension(extension);
1217
[email protected]b777b332011-04-16 04:01:081218#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231219 // Revoke external file access for the extension from its file system context.
1220 // It is safe to access the extension's storage partition at this point. The
1221 // storage partition may get destroyed only after the extension gets unloaded.
1222 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1223 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091224 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231225 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091226 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321227 if (filesystem_context && filesystem_context->external_backend()) {
1228 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171229 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061230 }
[email protected]c8d407e2011-04-28 21:27:171231#endif
1232
1233 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061234}
1235
[email protected]0a071a32011-02-08 00:18:241236Profile* ExtensionService::profile() {
1237 return profile_;
1238}
1239
[email protected]45759612012-07-10 17:21:231240extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451241 return extension_prefs_;
1242}
1243
[email protected]a7ff4b72013-10-17 20:56:021244const extensions::ExtensionPrefs* ExtensionService::extension_prefs() const {
1245 return extension_prefs_;
1246}
1247
[email protected]0d9a2202011-11-09 13:48:411248extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381249 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201250}
1251
[email protected]e2d720aa2012-05-17 00:11:351252extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441253 return extension_prefs()->content_settings_store();
1254}
1255
[email protected]25ae0152011-11-18 14:40:021256bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351257 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021258}
1259
[email protected]7f8f24f2012-11-15 19:40:141260base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091261 if (file_task_runner_.get())
1262 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141263
1264 // We should be able to interrupt any part of extension install process during
1265 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1266 // will be ignored/deleted while we will block on started tasks.
1267 std::string token("ext_install-");
1268 token.append(profile_->GetPath().AsUTF8Unsafe());
1269 file_task_runner_ = BrowserThread::GetBlockingPool()->
1270 GetSequencedTaskRunnerWithShutdownBehavior(
1271 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1272 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091273 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141274}
1275
[email protected]42a08162012-03-16 18:09:111276extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061277 return updater_.get();
1278}
1279
[email protected]4ee07c62012-08-21 12:40:421280void ExtensionService::CheckManagementPolicy() {
[email protected]0d54b682013-11-05 14:15:361281 std::vector<std::string> to_unload;
1282 std::map<std::string, Extension::DisableReason> to_disable;
[email protected]695b5712012-12-06 23:55:281283
[email protected]0d54b682013-11-05 14:15:361284 // Loop through the extensions list, finding extensions we need to unload or
1285 // disable.
[email protected]bb1bc9b32013-12-21 03:09:141286 const ExtensionSet& extensions = registry_->enabled_extensions();
1287 for (ExtensionSet::const_iterator iter = extensions.begin();
1288 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361289 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421290 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]0d54b682013-11-05 14:15:361291 to_unload.push_back(extension->id());
1292 Extension::DisableReason disable_reason = Extension::DISABLE_NONE;
1293 if (system_->management_policy()->MustRemainDisabled(
1294 extension, &disable_reason, NULL))
1295 to_disable[extension->id()] = disable_reason;
[email protected]aa96d3a2010-08-21 08:45:251296 }
1297
[email protected]0d54b682013-11-05 14:15:361298 for (size_t i = 0; i < to_unload.size(); ++i)
1299 UnloadExtension(to_unload[i], UnloadedExtensionInfo::REASON_DISABLE);
1300
1301 for (std::map<std::string, Extension::DisableReason>::const_iterator i =
1302 to_disable.begin(); i != to_disable.end(); ++i)
1303 DisableExtension(i->first, i->second);
[email protected]aa96d3a2010-08-21 08:45:251304}
1305
[email protected]31206602011-04-13 23:07:321306void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351307 if (updater()) {
[email protected]94fde232012-04-27 10:22:301308 if (AreAllExternalProvidersReady()) {
1309 updater()->CheckSoon();
1310 } else {
1311 // Sync can start updating before all the external providers are ready
1312 // during startup. Start the update as soon as those providers are ready,
1313 // but not before.
1314 update_once_all_providers_are_ready_ = true;
1315 }
[email protected]c3cfb012011-04-06 22:07:351316 } else {
[email protected]31206602011-04-13 23:07:321317 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351318 }
1319}
1320
[email protected]168389f2011-12-20 17:12:481321void ExtensionService::OnExtensionMoved(
1322 const std::string& moved_extension_id,
1323 const std::string& predecessor_extension_id,
1324 const std::string& successor_extension_id) {
[email protected]74474042013-11-21 12:03:541325 extension_prefs_->app_sorting()->OnExtensionMoved(
[email protected]3569b502012-01-12 20:08:231326 moved_extension_id,
1327 predecessor_extension_id,
1328 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481329
1330 const Extension* extension = GetInstalledExtension(moved_extension_id);
[email protected]f8aefb132013-10-30 09:29:521331 if (extension_sync_service_ && extension) {
1332 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
1333 }
[email protected]168389f2011-12-20 17:12:481334}
1335
[email protected]8e4560b62011-01-14 10:09:141336// Some extensions will autoupdate themselves externally from Chrome. These
1337// are typically part of some larger client application package. To support
1338// these, the extension will register its location in the the preferences file
1339// (and also, on Windows, in the registry) and this code will periodically
1340// check that location for a .crx file, which it will then install locally if
1341// a new version is available.
1342// Errors are reported through ExtensionErrorReporter. Succcess is not
1343// reported.
[email protected]eaa7dd182010-12-14 11:09:001344void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121345 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141346
1347 // Note that this installation is intentionally silent (since it didn't
1348 // go through the front-end). Extensions that are registered in this
1349 // way are effectively considered 'pre-bundled', and so implicitly
1350 // trusted. In general, if something has HKLM or filesystem access,
1351 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121352
[email protected]8e4560b62011-01-14 10:09:141353 // Ask each external extension provider to give us a call back for each
1354 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271355 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141356 for (i = external_extension_providers_.begin();
1357 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271358 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141359 provider->VisitRegisteredExtension();
1360 }
1361
[email protected]50067e52011-10-20 23:17:071362 // Do any required work that we would have done after completion of all
1363 // providers.
1364 if (external_extension_providers_.empty()) {
1365 OnAllExternalProvidersReady();
1366 }
[email protected]9f1087e2009-06-15 17:29:321367}
1368
[email protected]50067e52011-10-20 23:17:071369void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271370 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121371 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071372 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121373
1374 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141375 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301376 if (AreAllExternalProvidersReady())
1377 OnAllExternalProvidersReady();
1378}
1379
1380bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271381 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141382 for (i = external_extension_providers_.begin();
1383 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301384 if (!i->get()->IsReady())
1385 return false;
[email protected]8e4560b62011-01-14 10:09:141386 }
[email protected]94fde232012-04-27 10:22:301387 return true;
[email protected]50067e52011-10-20 23:17:071388}
1389
1390void ExtensionService::OnAllExternalProvidersReady() {
1391 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301392 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1393 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071394
1395 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301396 if (update_once_all_providers_are_ready_ && updater()) {
1397 update_once_all_providers_are_ready_ = false;
[email protected]4e9b59d2013-12-18 06:47:141398 extensions::ExtensionUpdater::CheckParams params;
1399 params.callback = external_updates_finished_callback_;
1400 updater()->CheckNow(params);
[email protected]8e4560b62011-01-14 10:09:141401 }
1402
1403 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231404 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141405 extension_prefs_->GetInstalledExtensionsInfo());
1406 for (size_t i = 0; i < extensions_info->size(); ++i) {
1407 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401408 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141409 CheckExternalUninstall(info->extension_id);
1410 }
[email protected]e5af875f2011-10-10 21:09:141411 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141412}
1413
1414void ExtensionService::IdentifyAlertableExtensions() {
1415 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1416
[email protected]07c9f2f42012-02-29 18:45:221417 // Build up the lists of extensions that require acknowledgment. If this is
1418 // the first time, grandfather extensions that would have caused
1419 // notification.
[email protected]89226982012-07-16 20:09:181420 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141421
[email protected]a9aa5932012-01-25 08:27:401422 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181423 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011424 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401425 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181426 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141427 } else {
1428 // First run. Just acknowledge all the extensions, silently, by
1429 // shortcutting the display of the UI and going straight to the
1430 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401431 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141432 }
1433 }
[email protected]a9aa5932012-01-25 08:27:401434
[email protected]612a1cb12012-10-17 13:18:031435 UpdateExternalExtensionAlert();
1436
[email protected]a9aa5932012-01-25 08:27:401437 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181438 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141439}
1440
[email protected]89226982012-07-16 20:09:181441bool ExtensionService::PopulateExtensionErrorUI(
1442 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221443 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281444
1445 // Extensions that are blacklisted.
[email protected]bb1bc9b32013-12-21 03:09:141446 const ExtensionSet& blacklisted_set = registry_->blacklisted_extensions();
1447 for (ExtensionSet::const_iterator it = blacklisted_set.begin();
1448 it != blacklisted_set.end(); ++it) {
[email protected]695b5712012-12-06 23:55:281449 std::string id = (*it)->id();
1450 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1451 extension_error_ui->AddBlacklistedExtension(id);
1452 needs_alert = true;
1453 }
1454 }
1455
[email protected]bb1bc9b32013-12-21 03:09:141456 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1457 for (ExtensionSet::const_iterator iter = enabled_set.begin();
1458 iter != enabled_set.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361459 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281460
[email protected]870f5572013-12-19 12:30:151461 // Skip for extensions that have pending updates. They will be checked again
1462 // once the pending update is finished.
1463 if (pending_extension_manager()->IsIdPending(e->id()))
1464 continue;
1465
[email protected]695b5712012-12-06 23:55:281466 // Extensions disabled by policy. Note: this no longer includes blacklisted
1467 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391468 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221469 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181470 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221471 needs_alert = true;
1472 }
1473 }
[email protected]07c9f2f42012-02-29 18:45:221474 }
[email protected]695b5712012-12-06 23:55:281475
[email protected]07c9f2f42012-02-29 18:45:221476 return needs_alert;
1477}
1478
[email protected]a9aa5932012-01-25 08:27:401479void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031480 const ExtensionIdSet* extension_ids =
1481 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141482 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1483 iter != extension_ids->end(); ++iter) {
1484 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1485 }
[email protected]879a6092013-03-06 04:12:501486 extension_error_ui_.reset();
1487}
1488
1489void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301490 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141491}
1492
[email protected]47fc70c2011-12-06 07:29:511493void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1494 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031495 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511496}
1497
[email protected]d96eb512012-11-01 23:44:081498bool ExtensionService::IsUnacknowledgedExternalExtension(
1499 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321500 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1501 return false;
1502
[email protected]1d5e58b2013-01-31 08:41:401503 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081504 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1505 !(extension_prefs_->GetDisableReasons(extension->id()) &
1506 Extension::DISABLE_SIDELOAD_WIPEOUT));
1507}
1508
[email protected]59ee99d2013-10-11 15:46:161509void ExtensionService::ReconcileKnownDisabled() {
[email protected]60a8bc962013-12-03 03:17:271510 ExtensionIdSet known_disabled_ids;
1511 if (!extension_prefs_->GetKnownDisabled(&known_disabled_ids)) {
[email protected]bb1bc9b32013-12-21 03:09:141512 extension_prefs_->SetKnownDisabled(
1513 registry_->disabled_extensions().GetIDs());
[email protected]018a5682013-11-20 02:41:211514 UMA_HISTOGRAM_BOOLEAN("Extensions.KnownDisabledInitialized", true);
1515 return;
[email protected]59ee99d2013-10-11 15:46:161516 }
[email protected]195c1582013-10-29 18:42:231517
[email protected]bb1bc9b32013-12-21 03:09:141518 // Both |known_disabled_ids| and |extensions| are ordered (by definition
[email protected]018a5682013-11-20 02:41:211519 // of std::map and std::set). Iterate forward over both sets in parallel
1520 // to find matching IDs and disable the corresponding extensions.
[email protected]bb1bc9b32013-12-21 03:09:141521 const ExtensionSet& enabled_set = registry_->enabled_extensions();
1522 ExtensionSet::const_iterator extensions_it = enabled_set.begin();
[email protected]018a5682013-11-20 02:41:211523 ExtensionIdSet::const_iterator known_disabled_ids_it =
1524 known_disabled_ids.begin();
1525 int known_disabled_count = 0;
[email protected]bb1bc9b32013-12-21 03:09:141526 while (extensions_it != enabled_set.end() &&
[email protected]018a5682013-11-20 02:41:211527 known_disabled_ids_it != known_disabled_ids.end()) {
1528 const std::string& extension_id = extensions_it->get()->id();
1529 const int comparison = extension_id.compare(*known_disabled_ids_it);
1530 if (comparison < 0) {
1531 ++extensions_it;
1532 } else if (comparison > 0) {
1533 ++known_disabled_ids_it;
1534 } else {
1535 ++known_disabled_count;
1536 // Advance |extensions_it| immediately as it will be invalidated upon
1537 // disabling the extension it points to.
1538 ++extensions_it;
1539 ++known_disabled_ids_it;
1540 DisableExtension(extension_id, Extension::DISABLE_KNOWN_DISABLED);
1541 }
1542 }
1543 UMA_HISTOGRAM_COUNTS_100("Extensions.KnownDisabledReDisabled",
1544 known_disabled_count);
1545
[email protected]195c1582013-10-29 18:42:231546 // Update the list of known disabled to reflect every change to
1547 // |disabled_extensions_| from this point forward.
[email protected]bb1bc9b32013-12-21 03:09:141548 registry_->SetDisabledModificationCallback(
[email protected]195c1582013-10-29 18:42:231549 base::Bind(&extensions::ExtensionPrefs::SetKnownDisabled,
1550 base::Unretained(extension_prefs_)));
[email protected]59ee99d2013-10-11 15:46:161551}
1552
[email protected]89226982012-07-16 20:09:181553void ExtensionService::HandleExtensionAlertDetails() {
1554 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301555 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1556 // causes a navigation.
1557 if (extension_error_ui_)
1558 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101559}
1560
[email protected]612a1cb12012-10-17 13:18:031561void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361562 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031563 return;
1564
1565 const Extension* extension = NULL;
[email protected]bb1bc9b32013-12-21 03:09:141566 const ExtensionSet& disabled_extensions = registry_->disabled_extensions();
1567 for (ExtensionSet::const_iterator iter = disabled_extensions.begin();
1568 iter != disabled_extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361569 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081570 if (IsUnacknowledgedExternalExtension(e)) {
1571 extension = e;
1572 break;
[email protected]612a1cb12012-10-17 13:18:031573 }
1574 }
1575
1576 if (extension) {
[email protected]d96eb512012-11-01 23:44:081577 if (!extensions::HasExternalInstallError(this)) {
1578 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031579 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081580 // Stop prompting for this extension, and check if there's another
1581 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031582 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081583 UpdateExternalExtensionAlert();
1584 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1585 EXTERNAL_EXTENSION_IGNORED,
1586 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021587 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1588 UMA_HISTOGRAM_ENUMERATION(
1589 "Extensions.ExternalExtensionEventWebstore",
1590 EXTERNAL_EXTENSION_IGNORED,
1591 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1592 } else {
1593 UMA_HISTOGRAM_ENUMERATION(
1594 "Extensions.ExternalExtensionEventNonWebstore",
1595 EXTERNAL_EXTENSION_IGNORED,
1596 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1597 }
[email protected]d96eb512012-11-01 23:44:081598 return;
[email protected]612a1cb12012-10-17 13:18:031599 }
[email protected]b3aa7182013-04-25 04:45:231600 if (is_first_run_)
1601 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1602 // first_run is true if the extension was installed during a first run
1603 // (even if it's post-first run now).
1604 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1605 extension->id());
1606 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031607 }
1608 } else {
1609 extensions::RemoveExternalInstallError(this);
1610 }
[email protected]612a1cb12012-10-17 13:18:031611}
1612
[email protected]a9f39a312010-12-23 22:14:271613void ExtensionService::UnloadExtension(
1614 const std::string& extension_id,
[email protected]b0af4792013-10-23 09:12:131615 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091616 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281617 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021618 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281619 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251620
[email protected]fa0c96732010-11-17 00:14:231621 // This method can be called via PostTask, so the extension may have been
1622 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091623 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171624 // In case the extension may have crashed/uninstalled. Allow the profile to
1625 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081626 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231627 return;
[email protected]dd163fb02011-05-04 22:22:171628 }
[email protected]0c6da502009-08-14 22:32:391629
[email protected]1eb175082010-02-10 09:26:161630 // Keep information about the extension so that we can reload it later
1631 // even if it's not permanently installed.
1632 unloaded_extension_paths_[extension->id()] = extension->path();
1633
[email protected]f17dbd42010-08-16 23:21:101634 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031635 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101636
[email protected]d7e9a862010-11-03 21:57:491637 // Clean up runtime data.
1638 extension_runtime_data_.erase(extension_id);
1639
[email protected]bb1bc9b32013-12-21 03:09:141640 if (registry_->disabled_extensions().Contains(extension->id())) {
1641 registry_->RemoveDisabled(extension->id());
[email protected]dd163fb02011-05-04 22:22:171642 // Make sure the profile cleans up its RequestContexts when an already
1643 // disabled extension is unloaded (since they are also tracking the disabled
1644 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081645 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]60a174a2013-08-02 20:29:281646 } else {
[email protected]bb1bc9b32013-12-21 03:09:141647 // Remove the extension from the enabled list.
1648 registry_->RemoveEnabled(extension->id());
[email protected]60a174a2013-08-02 20:29:281649 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391650 }
1651
[email protected]fb789532013-08-27 02:40:211652 content::NotificationService::current()->Notify(
1653 chrome::NOTIFICATION_EXTENSION_REMOVED,
1654 content::Source<Profile>(profile_),
1655 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251656}
1657
[email protected]8b1ec202013-09-05 02:09:501658void ExtensionService::RemoveComponentExtension(
1659 const std::string& extension_id) {
1660 scoped_refptr<const Extension> extension(
1661 GetExtensionById(extension_id, false));
[email protected]b0af4792013-10-23 09:12:131662 UnloadExtension(extension_id, UnloadedExtensionInfo::REASON_UNINSTALL);
[email protected]8b1ec202013-09-05 02:09:501663 content::NotificationService::current()->Notify(
1664 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1665 content::Source<Profile>(profile_),
1666 content::Details<const Extension>(extension.get()));
1667}
1668
[email protected]bb1bc9b32013-12-21 03:09:141669void ExtensionService::UnloadAllExtensionsForTest() {
1670 UnloadAllExtensionsInternal();
[email protected]9f1087e2009-06-15 17:29:321671}
1672
[email protected]bb1bc9b32013-12-21 03:09:141673void ExtensionService::ReloadExtensionsForTest() {
1674 // Calling UnloadAllExtensionsForTest here triggers a false-positive presubmit
1675 // warning about calling test code in production.
1676 UnloadAllExtensionsInternal();
[email protected]eac88332012-12-26 17:57:451677 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011678 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351679 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1680 // times.
[email protected]9f1087e2009-06-15 17:29:321681}
1682
[email protected]eaa7dd182010-12-14 11:09:001683void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:101684#if defined(OS_CHROMEOS)
1685 if (disable_garbage_collection_)
1686 return;
1687#endif
1688
[email protected]2f8757c32012-06-19 19:17:471689 if (extension_prefs_->pref_service()->ReadOnly())
1690 return;
1691
[email protected]9bd9a6862012-11-29 09:24:221692 if (pending_extension_manager()->HasPendingExtensions()) {
1693 // Don't garbage collect while there are pending installations, which may
1694 // be using the temporary installation directory. Try to garbage collect
1695 // again later.
[email protected]b3a25092013-05-28 22:08:161696 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221697 FROM_HERE,
1698 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1699 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1700 return;
1701 }
1702
[email protected]45759612012-07-10 17:21:231703 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471704 extension_prefs_->GetInstalledExtensionsInfo());
1705
[email protected]650b2d52013-02-10 03:41:451706 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471707 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051708 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1709 info->at(i)->extension_path));
1710
[email protected]399583b2012-12-11 09:33:421711 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:051712 for (size_t i = 0; i < info->size(); ++i)
1713 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
1714 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:471715
[email protected]7f8f24f2012-11-15 19:40:141716 if (!GetFileTaskRunner()->PostTask(
1717 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:471718 base::Bind(
1719 &extension_file_util::GarbageCollectExtensions,
1720 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:401721 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:471722 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:401723 }
[email protected]3cf4f0992009-02-03 23:00:301724}
1725
[email protected]820d9bd2013-04-03 03:46:031726void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:351727 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:031728 content::NotificationService::current()->Notify(
1729 chrome::NOTIFICATION_EXTENSIONS_READY,
1730 content::Source<Profile>(profile_),
1731 content::NotificationService::NoDetails());
1732}
1733
[email protected]eaa7dd182010-12-14 11:09:001734void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:111735 if (updater_)
[email protected]93fd78f42009-07-10 16:43:171736 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:501737
[email protected]bc151cf92013-02-12 04:57:261738 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:511739}
1740
[email protected]fcb58a862012-05-01 01:03:151741void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:171742 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1743 // default extension state to DISABLED when the --disable-extensions flag
1744 // is set (http://crbug.com/29067).
1745 if (!extensions_enabled() &&
1746 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:401747 extension->location() != Manifest::COMPONENT &&
1748 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:151749 return;
[email protected]330840c2012-08-29 22:21:011750 }
[email protected]c8d407e2011-04-28 21:27:171751
[email protected]b914e2952013-04-26 07:10:031752 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:171753 bool is_extension_installed = false;
1754 const Extension* old = GetInstalledExtension(extension->id());
1755 if (old) {
1756 is_extension_installed = true;
1757 int version_compare_result =
1758 extension->version()->CompareTo(*(old->version()));
1759 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:031760 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1761 // that we aren't downgrading.
1762 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:171763 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:031764 }
1765 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:171766
[email protected]1eb175082010-02-10 09:26:161767 // The extension is now loaded, remove its data from unloaded extension map.
1768 unloaded_extension_paths_.erase(extension->id());
1769
[email protected]bb7f40952011-01-13 00:21:201770 // If a terminated extension is loaded, remove it from the terminated list.
1771 UntrackTerminatedExtension(extension->id());
1772
[email protected]f17dbd42010-08-16 23:21:101773 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:411774 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:101775
[email protected]b914e2952013-04-26 07:10:031776 // Check if the extension's privileges have changed and mark the
1777 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:171778 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:031779
[email protected]116d40e2013-08-08 17:23:171780 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:411781 // To upgrade an extension in place, unload the old one and then load the
1782 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b0af4792013-10-23 09:12:131783 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_UPDATE);
[email protected]b914e2952013-04-26 07:10:031784 }
[email protected]0c6da502009-08-14 22:32:391785
[email protected]695b5712012-12-06 23:55:281786 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:091787 // Only prefs is checked for the blacklist. We rely on callers to check the
1788 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:071789 // installation then threads through the install and pending install flow
1790 // of this class, and we check when loading installed extensions.
[email protected]bb1bc9b32013-12-21 03:09:141791 registry_->AddBlacklisted(extension);
[email protected]ca2e82022013-04-29 16:48:411792 } else if (!reloading &&
1793 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]bb1bc9b32013-12-21 03:09:141794 registry_->AddDisabled(extension);
[email protected]f8aefb132013-10-30 09:29:521795 if (extension_sync_service_)
1796 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:031797 content::NotificationService::current()->Notify(
1798 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
1799 content::Source<Profile>(profile_),
1800 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:441801
[email protected]7e9e2422012-12-13 19:54:191802 // Show the extension disabled error if a permissions increase was the
1803 // only reason it was disabled.
1804 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:031805 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:441806 extensions::AddExtensionDisabledError(this, extension);
1807 }
[email protected]ca2e82022013-04-29 16:48:411808 } else if (reloading) {
1809 // Replace the old extension with the new version.
[email protected]bb1bc9b32013-12-21 03:09:141810 CHECK(!registry_->AddDisabled(extension));
[email protected]ca2e82022013-04-29 16:48:411811 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:281812 } else {
1813 // All apps that are displayed in the launcher are ordered by their ordinals
1814 // so we must ensure they have valid ordinals.
1815 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:251816 if (!extension->ShouldDisplayInNewTabPage()) {
[email protected]74474042013-11-21 12:03:541817 extension_prefs_->app_sorting()->MarkExtensionAsHidden(extension->id());
[email protected]bd36b652013-02-27 02:13:251818 }
[email protected]74474042013-11-21 12:03:541819 extension_prefs_->app_sorting()->EnsureValidOrdinals(
[email protected]695b5712012-12-06 23:55:281820 extension->id(), syncer::StringOrdinal());
1821 }
1822
[email protected]bb1bc9b32013-12-21 03:09:141823 registry_->AddEnabled(extension);
[email protected]f8aefb132013-10-30 09:29:521824 if (extension_sync_service_)
1825 extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
[email protected]695b5712012-12-06 23:55:281826 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:311827 }
[email protected]b914e2952013-04-26 07:10:031828 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:351829}
1830
[email protected]8c484b742012-11-29 06:05:361831void ExtensionService::AddComponentExtension(const Extension* extension) {
1832 const std::string old_version_string(
1833 extension_prefs_->GetVersionString(extension->id()));
1834 const Version old_version(old_version_string);
1835
[email protected]0181a7d2013-11-12 01:08:421836 VLOG(1) << "AddComponentExtension " << extension->name();
[email protected]8c484b742012-11-29 06:05:361837 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
1838 VLOG(1) << "Component extension " << extension->name() << " ("
1839 << extension->id() << ") installing/upgrading from '"
1840 << old_version_string << "' to " << extension->version()->GetString();
1841
1842 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:421843 Extension::ENABLED_COMPONENT,
[email protected]8e289f0b2013-12-17 17:49:071844 extensions::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:421845 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:361846 return;
1847 }
1848
1849 AddExtension(extension);
1850}
1851
[email protected]b914e2952013-04-26 07:10:031852void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:311853 // If the extension has used the optional permissions API, it will have a
1854 // custom set of active permissions defined in the extension prefs. Here,
1855 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:061856 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:311857 extension_prefs()->GetActivePermissions(extension->id());
1858
[email protected]dc24976f2013-06-02 21:15:091859 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:311860 // We restrict the active permissions to be within the bounds defined in the
1861 // extension's manifest.
1862 // a) active permissions must be a subset of optional + default permissions
1863 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:061864 scoped_refptr<PermissionSet> total_permissions =
1865 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061866 extensions::PermissionsData::GetRequiredPermissions(extension),
1867 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:311868
1869 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:061870 scoped_refptr<PermissionSet> adjusted_active =
1871 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:311872 total_permissions.get(), active_permissions.get());
1873
1874 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:061875 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:061876 extensions::PermissionsData::GetRequiredPermissions(extension),
1877 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311878
[email protected]c333e792012-01-06 16:57:391879 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:091880 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:311881 }
[email protected]b914e2952013-04-26 07:10:031882}
1883
1884void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:171885 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:031886 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:311887
[email protected]8d888c12010-11-30 00:00:251888 // We keep track of all permissions the user has granted each extension.
1889 // This allows extensions to gracefully support backwards compatibility
1890 // by including unknown permissions in their manifests. When the user
1891 // installs the extension, only the recognized permissions are recorded.
1892 // When the unknown permissions become recognized (e.g., through browser
1893 // upgrade), we can prompt the user to accept these new permissions.
1894 // Extensions can also silently upgrade to less permissions, and then
1895 // silently upgrade to a version that adds these permissions back.
1896 //
1897 // For example, pretend that Chrome 10 includes a permission "omnibox"
1898 // for an API that adds suggestions to the omnibox. An extension can
1899 // maintain backwards compatibility while still having "omnibox" in the
1900 // manifest. If a user installs the extension on Chrome 9, the browser
1901 // will record the permissions it recognized, not including "omnibox."
1902 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1903 // will disable the extension and prompt the user to approve the increase
1904 // in privileges. The extension could then release a new version that
1905 // removes the "omnibox" permission. When the user upgrades, Chrome will
1906 // still remember that "omnibox" had been granted, so that if the
1907 // extension once again includes "omnibox" in an upgrade, the extension
1908 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:281909 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:251910
[email protected]211a3f32013-05-28 21:48:181911 bool auto_grant_permission =
[email protected]1b563df2013-10-30 16:49:361912 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]45928b682013-11-28 08:20:271913 extensions::ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:031914 // Silently grant all active permissions to default apps only on install.
1915 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:181916 // Silently grant all active permissions to apps install in kiosk mode on both
1917 // install and update.
1918 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:031919 GrantPermissions(extension);
1920
1921 bool is_privilege_increase = false;
1922 // We only need to compare the granted permissions to the current permissions
1923 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:051924 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:181925 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:251926 // Add all the recognized permissions if the granted permissions list
1927 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:061928 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:311929 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521930 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251931
1932 // Here, we check if an extension's privileges have increased in a manner
1933 // that requires the user's approval. This could occur because the browser
1934 // upgraded and recognized additional privileges, or an extension upgrades
1935 // to a version that requires additional privileges.
[email protected]c41003472013-10-19 15:37:251936 is_privilege_increase =
1937 extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease(
1938 granted_permissions,
1939 extension->GetActivePermissions().get(),
1940 extension->GetType());
[email protected]8d888c12010-11-30 00:00:251941 }
1942
[email protected]116d40e2013-08-08 17:23:171943 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:031944 // If the extension was already disabled, suppress any alerts for becoming
1945 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:031946 bool previously_disabled =
1947 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:191948 // Legacy disabled extensions do not have a disable reason. Infer that if
1949 // there was no permission increase, it was likely disabled by the user.
1950 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:031951 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:191952 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:031953 }
[email protected]7e9e2422012-12-13 19:54:191954 // Extensions that came to us disabled from sync need a similar inference,
1955 // except based on the new version's permissions.
1956 if (previously_disabled &&
1957 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
1958 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
1959 extension_prefs_->ClearDisableReasons(extension->id());
1960 if (!is_privilege_increase)
1961 disable_reasons |= Extension::DISABLE_USER_ACTION;
1962 }
[email protected]b914e2952013-04-26 07:10:031963 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:251964 }
1965
1966 // Extension has changed permissions significantly. Disable it. A
1967 // notification should be sent by the caller.
1968 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:191969 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:491970 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1971 RecordPermissionMessagesHistogram(
1972 extension, "Extensions.Permissions_AutoDisable");
1973 }
[email protected]195c1582013-10-29 18:42:231974 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251975 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:031976 }
1977 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:281978 extension_prefs_->AddDisableReason(
1979 extension->id(),
1980 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:251981 }
1982}
1983
[email protected]eaa7dd182010-12-14 11:09:001984void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391985 std::set<std::string> extension_ids;
[email protected]bb1bc9b32013-12-21 03:09:141986 const ExtensionSet& extensions = registry_->enabled_extensions();
1987 for (ExtensionSet::const_iterator iter = extensions.begin();
1988 iter != extensions.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361989 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:401990 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:461991 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:351992 }
1993
[email protected]f6431be82013-09-07 02:53:451994 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251995}
1996
[email protected]669b2372013-10-17 15:04:581997ExtensionService::ImportStatus ExtensionService::CheckImports(
1998 const extensions::Extension* extension,
1999 std::list<SharedModuleInfo::ImportInfo>* missing_modules,
2000 std::list<SharedModuleInfo::ImportInfo>* outdated_modules) {
2001 DCHECK(extension);
2002 DCHECK(missing_modules && missing_modules->empty());
2003 DCHECK(outdated_modules && outdated_modules->empty());
[email protected]9f4e4f082013-06-21 07:11:192004 ImportStatus status = IMPORT_STATUS_OK;
[email protected]9f4e4f082013-06-21 07:11:192005 if (SharedModuleInfo::ImportsModules(extension)) {
2006 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2007 SharedModuleInfo::GetImports(extension);
2008 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2009 for (i = imports.begin(); i != imports.end(); ++i) {
2010 Version version_required(i->minimum_version);
2011 const Extension* imported_module =
2012 GetExtensionById(i->extension_id, true);
2013 if (!imported_module) {
2014 if (extension->from_webstore()) {
2015 status = IMPORT_STATUS_UNSATISFIED;
[email protected]669b2372013-10-17 15:04:582016 missing_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:192017 } else {
2018 return IMPORT_STATUS_UNRECOVERABLE;
2019 }
2020 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
2021 return IMPORT_STATUS_UNRECOVERABLE;
2022 } else if (version_required.IsValid() &&
2023 imported_module->version()->CompareTo(version_required) < 0) {
2024 if (imported_module->from_webstore()) {
[email protected]669b2372013-10-17 15:04:582025 outdated_modules->push_back(*i);
[email protected]9f4e4f082013-06-21 07:11:192026 status = IMPORT_STATUS_UNSATISFIED;
2027 } else {
2028 return IMPORT_STATUS_UNRECOVERABLE;
2029 }
2030 }
2031 }
2032 }
[email protected]669b2372013-10-17 15:04:582033 return status;
2034}
2035
2036ExtensionService::ImportStatus ExtensionService::SatisfyImports(
2037 const Extension* extension) {
2038 std::list<SharedModuleInfo::ImportInfo> noinstalled;
2039 std::list<SharedModuleInfo::ImportInfo> outdated;
2040 ImportStatus status = CheckImports(extension, &noinstalled, &outdated);
2041 if (status == IMPORT_STATUS_UNRECOVERABLE)
2042 return status;
[email protected]9f4e4f082013-06-21 07:11:192043 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]669b2372013-10-17 15:04:582044 std::list<SharedModuleInfo::ImportInfo>::const_iterator iter;
2045 for (iter = noinstalled.begin(); iter != noinstalled.end(); ++iter) {
[email protected]9f4e4f082013-06-21 07:11:192046 pending_extension_manager()->AddFromExtensionImport(
[email protected]669b2372013-10-17 15:04:582047 iter->extension_id,
[email protected]9f4e4f082013-06-21 07:11:192048 extension_urls::GetWebstoreUpdateUrl(),
2049 IsSharedModule);
2050 }
2051 CheckForUpdatesSoon();
2052 }
2053 return status;
2054}
2055
2056scoped_ptr<const ExtensionSet>
2057 ExtensionService::GetDependentExtensions(const Extension* extension) {
2058 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2059 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2060 if (SharedModuleInfo::IsSharedModule(extension)) {
[email protected]bb1bc9b32013-12-21 03:09:142061 set_to_check->InsertAll(registry_->disabled_extensions());
[email protected]9f4e4f082013-06-21 07:11:192062 set_to_check->InsertAll(delayed_installs_);
[email protected]bb1bc9b32013-12-21 03:09:142063 set_to_check->InsertAll(registry_->enabled_extensions());
[email protected]9f4e4f082013-06-21 07:11:192064 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2065 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112066 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2067 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192068 dependents->Insert(*iter);
2069 }
2070 }
2071 }
2072 return dependents.PassAs<const ExtensionSet>();
2073}
2074
2075void ExtensionService::PruneSharedModulesOnUninstall(
2076 const Extension* extension) {
2077 if (SharedModuleInfo::ImportsModules(extension)) {
2078 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2079 SharedModuleInfo::GetImports(extension);
2080 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2081 for (i = imports.begin(); i != imports.end(); ++i) {
2082 const Extension* imported_module =
2083 GetExtensionById(i->extension_id, true);
2084 if (imported_module && imported_module->from_webstore()) {
2085 scoped_ptr<const ExtensionSet> dependents =
2086 GetDependentExtensions(imported_module);
2087 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212088 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192089 }
2090 }
2091 }
2092 }
2093}
2094
[email protected]8266d662011-07-12 21:53:262095void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502096 const Extension* extension,
[email protected]98270432012-09-11 20:51:242097 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052098 bool has_requirement_errors,
[email protected]8e289f0b2013-12-17 17:49:072099 extensions::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052100 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122101 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202102
[email protected]c3cfb012011-04-06 22:07:352103 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032104 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412105 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062106 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402107 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062108 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322109
[email protected]31bb5ee62012-09-12 22:58:402110 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2111 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222112 << " and update URL "
2113 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402114 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542115
[email protected]4416c5a2010-06-26 01:28:572116 // Delete the extension directory since we're not going to
2117 // load it.
[email protected]7f8f24f2012-11-15 19:40:142118 if (!GetFileTaskRunner()->PostTask(
2119 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362120 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402121 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362122 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402123 }
[email protected]4416c5a2010-06-26 01:28:572124 return;
2125 }
[email protected]51a3bf8b2012-06-08 22:53:062126
2127 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572128 } else {
[email protected]c3cfb012011-04-06 22:07:352129 // We explicitly want to re-enable an uninstalled external
2130 // extension; if we're here, that means the user is manually
2131 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332132 if (IsExternalExtensionUninstalled(id)) {
2133 initial_enable = true;
2134 }
[email protected]aa142702010-03-26 01:26:332135 }
2136
[email protected]98270432012-09-11 20:51:242137 // Unsupported requirements overrides the management policy.
2138 if (has_requirement_errors) {
2139 initial_enable = false;
2140 extension_prefs_->AddDisableReason(
2141 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2142 // If the extension was disabled because of unsupported requirements but
2143 // now supports all requirements after an update and there are not other
2144 // disable reasons, enable it.
2145 } else if (extension_prefs_->GetDisableReasons(id) ==
2146 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2147 initial_enable = true;
2148 extension_prefs_->ClearDisableReasons(id);
2149 }
2150
[email protected]8e289f0b2013-12-17 17:49:072151 if (blacklist_state == extensions::BLACKLISTED_MALWARE) {
[email protected]9f3c8532013-07-31 19:52:072152 // Installation of a blacklisted extension can happen from sync, policy,
2153 // etc, where to maintain consistency we need to install it, just never
2154 // load it (see AddExtension). Usually it should be the job of callers to
2155 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2156 // showing the install dialogue).
2157 extension_prefs()->AcknowledgeBlacklistedExtension(id);
2158 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2159 extension->location(),
2160 Manifest::NUM_LOCATIONS);
2161 }
2162
[email protected]695b5712012-12-06 23:55:282163 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552164 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2165 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302166 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402167 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552168 RecordPermissionMessagesHistogram(
2169 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302170 } else {
2171 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2172 extension->GetType(), 100);
2173 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402174 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552175 }
2176
[email protected]a39921b42012-02-28 03:42:542177 // Certain extension locations are specific enough that we can
2178 // auto-acknowledge any extension that came from one of them.
[email protected]9bb691632013-09-26 18:50:122179 if (Manifest::IsPolicyLocation(extension->location()))
[email protected]a39921b42012-02-28 03:42:542180 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422181 const Extension::State initial_state =
2182 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]0f9e6262013-11-05 23:49:192183 const bool blacklisted_for_malware =
[email protected]8e289f0b2013-12-17 17:49:072184 blacklist_state == extensions::BLACKLISTED_MALWARE;
[email protected]e7aa7b7e2012-11-27 04:51:222185 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072186 extension_prefs_->SetDelayedInstallInfo(
2187 extension,
2188 initial_state,
[email protected]0f9e6262013-11-05 23:49:192189 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072190 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2191 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052192
2193 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192194 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052195
[email protected]75bdcb872013-03-13 00:41:452196 // Notify observers that app update is available.
2197 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
[email protected]a9aa62b312013-11-29 05:35:062198 OnAppUpdateAvailable(extension));
[email protected]0db124b02012-11-07 04:55:052199 return;
2200 }
2201
[email protected]9f4e4f082013-06-21 07:11:192202 ImportStatus status = SatisfyImports(extension);
2203 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072204 extension_prefs_->SetDelayedInstallInfo(
2205 extension,
2206 initial_state,
[email protected]0f9e6262013-11-05 23:49:192207 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072208 extensions::ExtensionPrefs::DELAY_REASON_GC,
2209 page_ordinal);
[email protected]399583b2012-12-11 09:33:422210 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192211 } else if (status != IMPORT_STATUS_OK) {
2212 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072213 extension_prefs_->SetDelayedInstallInfo(
2214 extension,
2215 initial_state,
[email protected]0f9e6262013-11-05 23:49:192216 blacklisted_for_malware,
[email protected]9f4e4f082013-06-21 07:11:192217 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2218 page_ordinal);
2219 delayed_installs_.Insert(extension);
2220 }
[email protected]399583b2012-12-11 09:33:422221 } else {
[email protected]9f3c8532013-07-31 19:52:072222 AddNewOrUpdatedExtension(extension,
2223 initial_state,
2224 blacklist_state,
2225 page_ordinal);
[email protected]399583b2012-12-11 09:33:422226 }
[email protected]8c484b742012-11-29 06:05:362227}
2228
2229void ExtensionService::AddNewOrUpdatedExtension(
2230 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422231 Extension::State initial_state,
[email protected]8e289f0b2013-12-17 17:49:072232 extensions::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422233 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362234 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0f9e6262013-11-05 23:49:192235 const bool blacklisted_for_malware =
[email protected]8e289f0b2013-12-17 17:49:072236 blacklist_state == extensions::BLACKLISTED_MALWARE;
[email protected]9f3c8532013-07-31 19:52:072237 extension_prefs_->OnExtensionInstalled(extension,
2238 initial_state,
[email protected]0f9e6262013-11-05 23:49:192239 blacklisted_for_malware,
[email protected]9f3c8532013-07-31 19:52:072240 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122241 delayed_installs_.Remove(extension->id());
[email protected]ffd2f79e2013-11-14 00:11:462242 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2243 extensions::ExtensionSystem::Get(profile_)->install_verifier()->Add(
2244 extension->id(), InstallVerifier::AddResultCallback());
2245 }
[email protected]6f6101832012-11-27 22:10:482246 FinishInstallation(extension);
2247}
2248
2249void ExtensionService::MaybeFinishDelayedInstallation(
2250 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192251 // Check if the extension already got installed.
2252 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482253 return;
[email protected]9f4e4f082013-06-21 07:11:192254 extensions::ExtensionPrefs::DelayReason reason =
2255 extension_prefs_->GetDelayedInstallReason(extension_id);
2256
2257 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2258 // preferences files that will not have set this field but it was previously
2259 // only used for idle updates.
2260 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2261 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
[email protected]617342a42013-12-18 23:34:032262 is_ready() && !extension_util::IsExtensionIdle(extension_id, system_))
[email protected]6f6101832012-11-27 22:10:482263 return;
2264
[email protected]9f4e4f082013-06-21 07:11:192265 const Extension* extension = delayed_installs_.GetByID(extension_id);
2266 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2267 ImportStatus status = SatisfyImports(extension);
2268 if (status != IMPORT_STATUS_OK) {
2269 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2270 delayed_installs_.Remove(extension_id);
2271 // Make sure no version of the extension is actually installed, (i.e.,
2272 // that this delayed install was not an update).
2273 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2274 extension_prefs_->DeleteExtensionPrefs(extension_id);
2275 }
2276 return;
2277 }
2278 }
2279
[email protected]6f6101832012-11-27 22:10:482280 FinishDelayedInstallation(extension_id);
2281}
2282
2283void ExtensionService::FinishDelayedInstallation(
2284 const std::string& extension_id) {
2285 scoped_refptr<const Extension> extension(
2286 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092287 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192288 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482289
[email protected]399583b2012-12-11 09:33:422290 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482291 NOTREACHED();
2292
[email protected]dc24976f2013-06-02 21:15:092293 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482294}
2295
2296void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562297 const extensions::Extension* existing_extension =
2298 GetInstalledExtension(extension->id());
2299 bool is_update = false;
2300 std::string old_name;
2301 if (existing_extension) {
2302 is_update = true;
2303 old_name = existing_extension->name();
2304 }
2305 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152306 content::NotificationService::current()->Notify(
2307 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2308 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022309 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152310
[email protected]d96eb512012-11-01 23:44:082311 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362312
[email protected]399583b2012-12-11 09:33:422313 // Unpacked extensions default to allowing file access, but if that has been
2314 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402315 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422316 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2317 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2318 }
2319
[email protected]6f6101832012-11-27 22:10:482320 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032321
2322 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082323 // so he can reenable it. We do this last so that it has already been
2324 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162325 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032326 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082327 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2328 EXTERNAL_EXTENSION_INSTALLED,
2329 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022330 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2331 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2332 EXTERNAL_EXTENSION_INSTALLED,
2333 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2334 } else {
2335 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2336 EXTERNAL_EXTENSION_INSTALLED,
2337 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2338 }
[email protected]d96eb512012-11-01 23:44:082339 }
[email protected]9f4e4f082013-06-21 07:11:192340
2341 // Check extensions that may have been delayed only because this shared module
2342 // was not available.
2343 if (SharedModuleInfo::IsSharedModule(extension)) {
2344 MaybeFinishDelayedInstallations();
2345 }
[email protected]4a190632009-05-09 01:07:422346}
2347
[email protected]76b65442012-11-17 14:11:482348const Extension* ExtensionService::GetPendingExtensionUpdate(
2349 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192350 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482351}
2352
[email protected]bb7f40952011-01-13 00:21:202353void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]bb1bc9b32013-12-21 03:09:142354 // No need to check for duplicates; inserting a duplicate is a no-op.
2355 registry_->AddTerminated(make_scoped_refptr(extension));
[email protected]b0af4792013-10-23 09:12:132356 UnloadExtension(extension->id(), UnloadedExtensionInfo::REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202357}
2358
2359void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202360 std::string lowercase_id = StringToLowerASCII(id);
[email protected]bb1bc9b32013-12-21 03:09:142361 const Extension* extension =
2362 registry_->terminated_extensions().GetByID(lowercase_id);
2363 registry_->RemoveTerminated(lowercase_id);
[email protected]fb789532013-08-27 02:40:212364 if (extension) {
2365 content::NotificationService::current()->Notify(
2366 chrome::NOTIFICATION_EXTENSION_REMOVED,
2367 content::Source<Profile>(profile_),
2368 content::Details<const Extension>(extension));
2369 }
[email protected]bb7f40952011-01-13 00:21:202370}
2371
[email protected]0dfe05c2011-02-23 23:03:362372const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472373 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282374 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472375}
2376
2377const Extension* ExtensionService::GetInstalledExtension(
2378 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282379 int include_mask = INCLUDE_ENABLED |
2380 INCLUDE_DISABLED |
2381 INCLUDE_TERMINATED |
2382 INCLUDE_BLACKLISTED;
2383 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362384}
2385
[email protected]eaa7dd182010-12-14 11:09:002386bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442387 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]bb1bc9b32013-12-21 03:09:142388 const Extension* extension =
2389 registry_->enabled_extensions().GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442390 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402391 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392392}
2393
[email protected]4d007b312012-10-17 03:00:482394bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]bb1bc9b32013-12-21 03:09:142395 const Extension* extension =
2396 registry_->enabled_extensions().GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482397 if (extension && extension->is_platform_app()) {
2398 *url = GURL(chrome::kExtensionInvalidRequestURL);
2399 return true;
2400 }
2401
2402 return false;
2403}
2404
[email protected]9060d8b02012-01-13 02:14:302405bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322406 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142407 const Version* version,
[email protected]650b2d52013-02-10 03:41:452408 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402409 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512410 int creation_flags,
2411 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382412 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2413 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012414 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302415 return false;
[email protected]a8af9fdb2010-10-28 21:52:202416
[email protected]7577a5c52009-07-30 06:21:582417 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492418 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582419 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022420 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352421
[email protected]7577a5c52009-07-30 06:21:582422 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352423 // The default apps will have the location set as INTERNAL. Since older
2424 // default apps are installed as EXTERNAL, we override them. However, if the
2425 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402426 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352427 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402428 (location == Manifest::INTERNAL &&
2429 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352430
2431 if (!is_default_apps_migration) {
2432 DCHECK(version);
2433
2434 switch (existing->version()->CompareTo(*version)) {
2435 case -1: // existing version is older, we should upgrade
2436 break;
2437 case 0: // existing version is same, do nothing
2438 return false;
2439 case 1: // existing version is newer, uh-oh
2440 LOG(WARNING) << "Found external version of extension " << id
2441 << "that is older than current version. Current version "
2442 << "is: " << existing->VersionString() << ". New "
2443 << "version is: " << version->GetString()
2444 << ". Keeping current version.";
2445 return false;
2446 }
[email protected]7577a5c52009-07-30 06:21:582447 }
2448 }
2449
[email protected]9060d8b02012-01-13 02:14:302450 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302451 if (!pending_extension_manager()->AddFromExternalFile(
[email protected]464213a2013-10-15 01:06:482452 id, location, *version, creation_flags, mark_acknowledged)) {
[email protected]9060d8b02012-01-13 02:14:302453 return false;
[email protected]e3987852012-05-04 10:06:302454 }
[email protected]9c635f22010-12-02 09:36:362455
[email protected]14908b72011-04-20 06:54:362456 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372457 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162458 installer->set_install_source(location);
2459 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072460 installer->set_expected_version(*version);
2461 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312462 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182463#if defined(OS_CHROMEOS)
2464 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2465#else
[email protected]6dfbbf82010-03-12 23:09:162466 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182467#endif
[email protected]47fc70c2011-12-06 07:29:512468
2469 // Depending on the source, a new external extension might not need a user
2470 // notification on installation. For such extensions, mark them acknowledged
2471 // now to suppress the notification.
2472 if (mark_acknowledged)
2473 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302474
2475 return true;
[email protected]7577a5c52009-07-30 06:21:582476}
2477
[email protected]b5a507b22013-11-08 20:41:572478scoped_ptr<DictionaryValue> ExtensionService::GetExtensionInfo(
2479 const std::string& extension_id) const {
2480 scoped_ptr<DictionaryValue> dictionary(new DictionaryValue);
[email protected]bb1bc9b32013-12-21 03:09:142481 const extensions::Extension* extension =
2482 registry_->enabled_extensions().GetByID(extension_id);
[email protected]b5a507b22013-11-08 20:41:572483 if (extension) {
2484 GURL icon = extensions::ExtensionIconSource::GetIconURL(
2485 extension, extension_misc::EXTENSION_ICON_SMALLISH,
2486 ExtensionIconSet::MATCH_BIGGER, false, NULL);
2487 dictionary->SetString("id", extension_id);
2488 dictionary->SetString("name", extension->name());
2489 dictionary->SetString("icon", icon.spec());
2490 }
2491 return dictionary.Pass();
2492}
2493
[email protected]eaa7dd182010-12-14 11:09:002494void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452495 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402496 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402497 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012498 content::NotificationService::current()->Notify(
2499 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2500 content::Source<Profile>(profile_),
2501 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402502
[email protected]8a205c02011-02-04 20:41:332503 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]439f1e32013-12-09 20:09:092504 base::string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042505 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492506 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402507 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2508}
2509
[email protected]eaa7dd182010-12-14 11:09:002510void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132511 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182512 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332513 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182514 if (iter == orphaned_dev_tools_.end())
2515 return;
2516
[email protected]04ea1bb2013-07-10 09:26:092517 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182518 orphaned_dev_tools_.erase(iter);
2519}
2520
[email protected]432115822011-07-10 15:52:272521void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532522 const content::NotificationSource& source,
2523 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272524 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542525 case chrome::NOTIFICATION_APP_TERMINATING:
2526 // Shutdown has started. Don't start any more extension installs.
2527 // (We cannot use ExtensionService::Shutdown() for this because it
2528 // happens too late in browser teardown.)
2529 browser_terminating_ = true;
2530 break;
[email protected]432115822011-07-10 15:52:272531 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532532 if (profile_ !=
2533 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322534 break;
[email protected]6c2381d2011-10-19 02:52:532535 }
[email protected]a4ed6282009-12-14 20:51:162536
[email protected]3a1dc572012-07-31 22:25:132537 extensions::ExtensionHost* host =
2538 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262539
[email protected]fa2416f2011-05-03 08:41:202540 // Mark the extension as terminated and Unload it. We want it to
2541 // be in a consistent state: either fully working or not loaded
2542 // at all, but never half-crashed. We do it in a PostTask so
2543 // that other handlers of this notification will still have
2544 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162545 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362546 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362547 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202548 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012549 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202550 host->extension()));
[email protected]31f77262009-12-02 20:48:532551 break;
2552 }
[email protected]432115822011-07-10 15:52:272553 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302554 content::RenderProcessHost* process =
2555 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192556 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302557 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192558 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2559 break;
2560
[email protected]15397b42012-05-16 23:56:062561 // Extensions need to know the channel for API restrictions.
2562 process->Send(new ExtensionMsg_SetChannel(
[email protected]15d4d2d2013-08-09 06:49:032563 extensions::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062564
[email protected]4941fe92013-06-13 23:21:012565 // Platform apps need to know the system font.
2566 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2567 webui::SetFontAndTextDirection(fonts.get());
2568 std::string font_family, font_size;
2569 fonts->GetString("fontfamily", &font_family);
2570 fonts->GetString("fontsize", &font_size);
2571 process->Send(new ExtensionMsg_SetSystemFont(
2572 font_family, font_size));
2573
[email protected]77a6970c2011-04-23 16:58:562574 // Valid extension function names, used to setup bindings in renderer.
2575 std::vector<std::string> function_names;
2576 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2577 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532578
[email protected]77a6970c2011-04-23 16:58:562579 // Scripting whitelist. This is modified by tests and must be communicated
2580 // to renderers.
2581 process->Send(new ExtensionMsg_SetScriptingWhitelist(
[email protected]c81f5d32013-10-26 10:38:422582 extensions::ExtensionsClient::Get()->GetScriptingWhitelist()));
[email protected]77a6970c2011-04-23 16:58:562583
2584 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532585 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]bb1bc9b32013-12-21 03:09:142586 const ExtensionSet& extensions = registry_->enabled_extensions();
2587 for (ExtensionSet::const_iterator iter = extensions.begin();
2588 iter != extensions.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492589 // Renderers don't need to know about themes.
2590 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362591 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562592 }
[email protected]9776e82e2011-11-15 02:17:532593 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562594 break;
2595 }
[email protected]432115822011-07-10 15:52:272596 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302597 content::RenderProcessHost* process =
2598 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192599 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302600 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192601 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2602 break;
2603
[email protected]52b76592013-11-02 17:59:032604 if (process_map_.Contains(process->GetID())) {
2605 // An extension process was terminated, this might have resulted in an
2606 // app or extension becoming idle.
2607 std::set<std::string> extension_ids =
2608 process_map_.GetExtensionsInProcess(process->GetID());
2609 for (std::set<std::string>::const_iterator it = extension_ids.begin();
2610 it != extension_ids.end(); ++it) {
2611 if (delayed_installs_.Contains(*it)) {
2612 base::MessageLoop::current()->PostDelayedTask(
2613 FROM_HERE,
2614 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
2615 AsWeakPtr(), *it),
2616 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
2617 }
2618 }
2619 }
2620
[email protected]6bc04fd82011-12-04 02:29:352621 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462622 BrowserThread::PostTask(
[email protected]38427a12013-11-09 17:34:202623 BrowserThread::IO,
2624 FROM_HERE,
2625 base::Bind(&extensions::InfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082626 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302627 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212628 break;
2629 }
[email protected]92dd8d92013-02-26 00:41:082630 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
[email protected]75bdcb872013-03-13 00:41:452631 // Notify observers that chrome update is available.
2632 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2633 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082634 break;
2635 }
[email protected]aa96d3a2010-08-21 08:45:252636
[email protected]4814b512009-11-07 00:12:292637 default:
2638 NOTREACHED() << "Unexpected notification type.";
2639 }
2640}
2641
[email protected]90bb38d2012-11-14 18:36:032642void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182643 IdentifyAlertableExtensions();
2644 CheckManagementPolicy();
2645}
2646
[email protected]eaa7dd182010-12-14 11:09:002647bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182648 return !GetAppIds().empty();
2649}
[email protected]377011d2010-07-20 04:18:502650
[email protected]eaa7dd182010-12-14 11:09:002651ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182652 ExtensionIdSet result;
[email protected]bb1bc9b32013-12-21 03:09:142653 const ExtensionSet& extensions = registry_->enabled_extensions();
2654 for (ExtensionSet::const_iterator it = extensions.begin();
2655 it != extensions.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402656 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182657 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502658 }
2659
[email protected]ec5b50d2010-10-09 16:35:182660 return result;
[email protected]377011d2010-07-20 04:18:502661}
[email protected]d7e9a862010-11-03 21:57:492662
[email protected]dc9a74f72012-08-17 18:07:212663bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292664 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212665 return true;
2666 ExtensionRuntimeDataMap::const_iterator it =
2667 extension_runtime_data_.find(extension->id());
2668 return it == extension_runtime_data_.end() ? false :
2669 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492670}
2671
[email protected]eaa7dd182010-12-14 11:09:002672void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292673 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492674 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072675 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272676 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532677 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072678 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492679}
2680
[email protected]dc9a74f72012-08-17 18:07:212681bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2682 ExtensionRuntimeDataMap::const_iterator it =
2683 extension_runtime_data_.find(extension->id());
2684 return it == extension_runtime_data_.end() ? false :
2685 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492686}
2687
[email protected]eaa7dd182010-12-14 11:09:002688void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032689 bool value) {
[email protected]d7e9a862010-11-03 21:57:492690 extension_runtime_data_[extension->id()].being_upgraded = value;
2691}
[email protected]1bead0712010-11-27 17:41:532692
[email protected]e178ad92013-06-28 02:29:252693bool ExtensionService::IsBeingReloaded(
2694 const std::string& extension_id) const {
2695 return ContainsKey(extensions_being_reloaded_, extension_id);
2696}
2697
2698void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432699 bool isBeingReloaded) {
2700 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252701 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432702 else
[email protected]e178ad92013-06-28 02:29:252703 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252704}
2705
[email protected]dc9a74f72012-08-17 18:07:212706bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2707 ExtensionRuntimeDataMap::const_iterator it =
2708 extension_runtime_data_.find(extension->id());
2709 return it == extension_runtime_data_.end() ? false :
2710 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072711}
2712
2713void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2714 bool value) {
2715 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2716}
2717
[email protected]612a1cb12012-10-17 13:18:032718bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2719 // Extensions installed by policy can't be disabled. So even if a previous
2720 // installation disabled the extension, make sure it is now enabled.
2721 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2722 return true;
2723
2724 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2725 return false;
2726
[email protected]41070e8d2012-10-24 01:34:362727 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032728 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542729 // enabling them. Hosted apps are excepted because they are not dangerous
2730 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402731 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2732 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032733 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2734 return false;
2735 }
2736 }
[email protected]612a1cb12012-10-17 13:18:032737
2738 return true;
2739}
[email protected]0db124b02012-11-07 04:55:052740
[email protected]e7aa7b7e2012-11-27 04:51:222741bool ExtensionService::ShouldDelayExtensionUpdate(
2742 const std::string& extension_id,
2743 bool wait_for_idle) const {
2744 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
2745
2746 // If delayed updates are globally disabled, or just for this extension,
2747 // don't delay.
2748 if (!install_updates_when_idle_ || !wait_for_idle)
2749 return false;
2750
[email protected]695b5712012-12-06 23:55:282751 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:222752 // If there is no old extension, this is not an update, so don't delay.
2753 if (!old)
2754 return false;
2755
[email protected]9367eabc2013-03-01 01:29:292756 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:222757 // Delay installation if the extension listens for the onUpdateAvailable
2758 // event.
2759 return system_->event_router()->ExtensionHasEventListener(
2760 extension_id, kOnUpdateAvailableEvent);
2761 } else {
2762 // Delay installation if the extension is not idle.
[email protected]617342a42013-12-18 23:34:032763 return !extension_util::IsExtensionIdle(extension_id, system_);
[email protected]e7aa7b7e2012-11-27 04:51:222764 }
[email protected]0db124b02012-11-07 04:55:052765}
[email protected]fdd679b2012-11-15 20:49:392766
[email protected]399583b2012-12-11 09:33:422767void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:452768 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
2769 new base::hash_set<base::FilePath>());
[email protected]bb1bc9b32013-12-21 03:09:142770 const ExtensionSet& extensions = registry_->enabled_extensions();
2771 for (ExtensionSet::const_iterator it = extensions.begin();
2772 it != extensions.end(); ++it) {
[email protected]cadac622013-06-11 16:46:362773 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
2774 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
2775 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:422776 }
2777 }
2778
[email protected]9f4e4f082013-06-21 07:11:192779 DCHECK(!installs_delayed_for_gc());
2780 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:422781 BrowserContext::GarbageCollectStoragePartitions(
2782 profile_, active_paths.Pass(),
2783 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
2784 AsWeakPtr()));
2785}
2786
2787void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:192788 set_installs_delayed_for_gc(false);
2789 MaybeFinishDelayedInstallations();
2790}
2791
2792void ExtensionService::MaybeFinishDelayedInstallations() {
2793 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:422794 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
2795 it != delayed_installs_.end();
2796 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192797 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:422798 }
[email protected]9f4e4f082013-06-21 07:11:192799 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
2800 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:422801 ++it) {
[email protected]9f4e4f082013-06-21 07:11:192802 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:422803 }
[email protected]399583b2012-12-11 09:33:422804}
2805
2806void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
2807 extension_prefs_->SetNeedsStorageGarbageCollection(true);
2808}
2809
[email protected]fdd679b2012-11-15 20:49:392810void ExtensionService::OnBlacklistUpdated() {
[email protected]48a359342013-10-30 00:22:002811 blacklist_->GetMalwareIDs(
[email protected]695b5712012-12-06 23:55:282812 GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:252813 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:282814}
2815
[email protected]3f2a2fa2013-09-24 02:55:252816void ExtensionService::ManageBlacklist(const std::set<std::string>& updated) {
[email protected]695b5712012-12-06 23:55:282817 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2818
[email protected]bb1bc9b32013-12-21 03:09:142819 std::set<std::string> before = registry_->blacklisted_extensions().GetIDs();
[email protected]16bf7ba72013-08-23 11:52:542820 std::set<std::string> no_longer_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252821 base::STLSetDifference<std::set<std::string> >(before, updated);
[email protected]16bf7ba72013-08-23 11:52:542822 std::set<std::string> not_yet_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:252823 base::STLSetDifference<std::set<std::string> >(updated, before);
[email protected]695b5712012-12-06 23:55:282824
2825 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
2826 it != no_longer_blacklisted.end(); ++it) {
2827 scoped_refptr<const Extension> extension =
[email protected]bb1bc9b32013-12-21 03:09:142828 registry_->blacklisted_extensions().GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:252829 if (!extension.get()) {
2830 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
2831 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:282832 continue;
[email protected]3f2a2fa2013-09-24 02:55:252833 }
[email protected]bb1bc9b32013-12-21 03:09:142834 registry_->RemoveBlacklisted(*it);
[email protected]3f2a2fa2013-09-24 02:55:252835 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:092836 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:092837 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:092838 extension->location(),
2839 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282840 }
2841
2842 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
2843 it != not_yet_blacklisted.end(); ++it) {
2844 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:252845 if (!extension.get()) {
2846 NOTREACHED() << "Extension " << *it << " needs to be "
2847 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:282848 continue;
[email protected]3f2a2fa2013-09-24 02:55:252849 }
[email protected]bb1bc9b32013-12-21 03:09:142850 registry_->AddBlacklisted(extension);
[email protected]3f2a2fa2013-09-24 02:55:252851 extension_prefs_->SetExtensionBlacklisted(extension->id(), true);
[email protected]b0af4792013-10-23 09:12:132852 UnloadExtension(*it, UnloadedExtensionInfo::REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:092853 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
2854 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:282855 }
2856
2857 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:392858}
[email protected]75bdcb872013-03-13 00:41:452859
2860void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
2861 update_observers_.AddObserver(observer);
2862}
2863
2864void ExtensionService::RemoveUpdateObserver(
2865 extensions::UpdateObserver* observer) {
2866 update_observers_.RemoveObserver(observer);
2867}
[email protected]bb1bc9b32013-12-21 03:09:142868
2869// Used only by test code.
2870void ExtensionService::UnloadAllExtensionsInternal() {
2871 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
2872
2873 registry_->ClearAll();
2874 extension_runtime_data_.clear();
2875
2876 // TODO(erikkay) should there be a notification for this? We can't use
2877 // EXTENSION_UNLOADED since that implies that the extension has been disabled
2878 // or uninstalled.
2879}