blob: 1e7ae32cd7f73c264bae8a8f2e07ad74ac7b9f22 [file] [log] [blame]
[email protected]098fa7a2013-03-08 22:11:171// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]695b5712012-12-06 23:55:288#include <iterator>
[email protected]97d2f1d2011-01-15 00:41:089#include <set>
[email protected]654512b2010-09-01 02:09:4210
[email protected]24b538a2010-02-27 01:22:4411#include "base/basictypes.h"
[email protected]05aad2da2011-10-28 10:12:3712#include "base/bind.h"
[email protected]e5af875f2011-10-10 21:09:1413#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1214#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2515#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3616#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:5718#include "base/prefs/pref_service.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]3ea1b182013-02-08 22:38:4120#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1721#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1823#include "base/strings/utf_string_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1424#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread_restrictions.h"
[email protected]41a17c52013-06-28 00:27:5326#include "base/time/time.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]211a3f32013-05-28 21:48:1829#include "chrome/browser/app_mode/app_mode_utils.h"
[email protected]15730c42009-09-03 00:03:2030#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3631#include "chrome/browser/chrome_notification_types.h"
[email protected]32b36c62012-10-24 05:37:4132#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1233#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4434#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]c39df1a2013-01-08 18:22:0735#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3136#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]4636c832013-01-11 02:10:1137#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]5db9ada2012-04-11 13:48:2038#include "chrome/browser/extensions/app_sync_data.h"
[email protected]d8c8f25f2011-11-02 18:18:0139#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1740#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3841#include "chrome/browser/extensions/data_deleter.h"
[email protected]ebc8d932013-09-11 23:47:2942#include "chrome/browser/extensions/event_router.h"
[email protected]62f051c2012-03-29 17:04:4443#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2444#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1845#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5646#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2247#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2948#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2349#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4350#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0451#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0852#include "chrome/browser/extensions/extension_system.h"
[email protected]612a1cb12012-10-17 13:18:0353#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2754#include "chrome/browser/extensions/external_provider_impl.h"
55#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0156#include "chrome/browser/extensions/installed_loader.h"
[email protected]fdd679b2012-11-15 20:49:3957#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3758#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3959#include "chrome/browser/extensions/permissions_updater.h"
[email protected]d8c8f25f2011-11-02 18:18:0160#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]75bdcb872013-03-13 00:41:4561#include "chrome/browser/extensions/update_observer.h"
[email protected]42a08162012-03-16 18:09:1162#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3363#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3964#include "chrome/browser/profiles/profile_manager.h"
[email protected]c8d407e2011-04-28 21:27:1765#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3166#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1567#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5468#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0669#include "chrome/common/chrome_version_info.h"
[email protected]f6431be82013-09-07 02:53:4570#include "chrome/common/crash_keys.h"
[email protected]9367eabc2013-03-01 01:29:2971#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:5872#include "chrome/common/extensions/extension.h"
[email protected]9f4e4f082013-06-21 07:11:1973#include "chrome/common/extensions/extension_constants.h"
[email protected]7c927b62010-02-24 09:54:1374#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5675#include "chrome/common/extensions/extension_messages.h"
[email protected]612a1cb12012-10-17 13:18:0376#include "chrome/common/extensions/feature_switch.h"
[email protected]15d4d2d2013-08-09 06:49:0377#include "chrome/common/extensions/features/feature_channel.h"
[email protected]06492ed2013-03-24 22:13:1478#include "chrome/common/extensions/incognito_handler.h"
[email protected]561e33d52013-04-03 06:58:4379#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]6b414c232013-06-05 07:53:3480#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]9f4e4f082013-06-21 07:11:1981#include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
[email protected]65348062013-01-15 07:27:2282#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]2cb5e302013-05-09 05:00:0683#include "chrome/common/extensions/permissions/permissions_data.h"
[email protected]8f3bcbd2013-06-05 08:42:4084#include "chrome/common/extensions/sync_helper.h"
[email protected]25b34332009-06-05 21:53:1985#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1486#include "chrome/common/url_constants.h"
[email protected]7a83dd072013-09-03 12:09:1787#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]c38831a12011-10-28 12:44:4988#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3689#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3790#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1691#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3092#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4293#include "content/public/browser/site_instance.h"
94#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:5395#include "content/public/browser/url_data_source.h"
[email protected]836e2982013-05-16 08:07:4296#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:3197#include "extensions/common/error_utils.h"
[email protected]d42c11152013-08-22 19:36:3298#include "extensions/common/manifest.h"
[email protected]0c3c9732013-09-16 08:53:4199#include "extensions/common/manifest_constants.h"
[email protected]65187152012-06-02 13:14:14100#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25101#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12102#include "sync/api/sync_change.h"
103#include "sync/api/sync_error_factory.h"
[email protected]4941fe92013-06-13 23:21:01104#include "ui/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:00105#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38106#include "webkit/browser/database/database_tracker.h"
107#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18108
[email protected]eed367e2011-04-12 03:43:31109#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18110#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32111#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07112#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31113#endif
114
[email protected]55eb70e762012-02-20 17:38:39115using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39116using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36117using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27118using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34119using extensions::Extension;
120using extensions::ExtensionIdSet;
121using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42122using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40123using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06124using extensions::PermissionMessage;
125using extensions::PermissionMessages;
126using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19127using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24128using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05129
[email protected]0c3c9732013-09-16 08:53:41130namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06131
[email protected]b6ab96d2009-08-20 18:58:19132namespace {
133
[email protected]d96eb512012-11-01 23:44:08134// Histogram values for logging events related to externally installed
135// extensions.
136enum ExternalExtensionEvent {
137 EXTERNAL_EXTENSION_INSTALLED = 0,
138 EXTERNAL_EXTENSION_IGNORED,
139 EXTERNAL_EXTENSION_REENABLED,
140 EXTERNAL_EXTENSION_UNINSTALLED,
141 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
142};
143
[email protected]612a1cb12012-10-17 13:18:03144// Prompt the user this many times before considering an extension acknowledged.
145static const int kMaxExtensionAcknowledgePromptCount = 3;
146
[email protected]0db124b02012-11-07 04:55:05147// Wait this many seconds after an extensions becomes idle before updating it.
148static const int kUpdateIdleDelay = 5;
149
[email protected]9bd9a6862012-11-29 09:24:22150// Wait this many seconds before trying to garbage collect extensions again.
151static const int kGarbageCollectRetryDelay = 30;
152
[email protected]36fc54f2013-06-14 02:49:34153// Wait this many seconds after startup to see if there are any extensions
154// which can be garbage collected.
155static const int kGarbageCollectStartupDelay = 30;
156
[email protected]9f4e4f082013-06-21 07:11:19157static bool IsSharedModule(const Extension* extension) {
158 return SharedModuleInfo::IsSharedModule(extension);
159}
160
[email protected]4c9201c42013-08-16 04:56:21161static bool IsCWSSharedModule(const Extension* extension) {
162 return extension->from_webstore() && IsSharedModule(extension);
163}
164
165class SharedModuleProvider : public extensions::ManagementPolicy::Provider {
166 public:
167 SharedModuleProvider() {}
168 virtual ~SharedModuleProvider() {}
169
170 virtual std::string GetDebugPolicyProviderName() const OVERRIDE {
171 return "SharedModuleProvider";
172 }
173
174 virtual bool UserMayModifySettings(const Extension* extension,
175 string16* error) const OVERRIDE {
176 return !IsCWSSharedModule(extension);
177 }
178
179 virtual bool MustRemainEnabled(const Extension* extension,
180 string16* error) const OVERRIDE {
181 return IsCWSSharedModule(extension);
182 }
183
184 private:
185 DISALLOW_COPY_AND_ASSIGN(SharedModuleProvider);
186};
187
188
[email protected]c6d474f82009-12-16 21:11:06189} // namespace
[email protected]b6ab96d2009-08-20 18:58:19190
[email protected]eaa7dd182010-12-14 11:09:00191ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49192 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57193 being_upgraded(false),
194 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49195}
196
[email protected]eaa7dd182010-12-14 11:09:00197ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49198}
199
[email protected]eaa7dd182010-12-14 11:09:00200// ExtensionService.
[email protected]6014d672008-12-05 00:38:25201
[email protected]31bb5ee62012-09-12 22:58:40202const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24203 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40204const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24205 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40206const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24207 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40208const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24209 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29210const char ExtensionService::kManagedSettingsDirectoryName[] =
211 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40212const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]a690e292012-12-19 19:22:49213const char ExtensionService::kRulesStoreName[] = "Extension Rules";
[email protected]494c06e2009-07-25 01:06:42214
[email protected]8e4560b62011-01-14 10:09:14215void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12216 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20217
[email protected]7a4c6852010-09-16 03:44:22218 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27219 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21220 for (i = external_extension_providers_.begin();
221 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14222 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21223 if (i->get()->HasExtension(id))
224 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22225 }
226
[email protected]0f48fca2011-05-19 18:46:35227 // We get the list of external extensions to check from preferences.
228 // It is possible that an extension has preferences but is not loaded.
229 // For example, an extension that requires experimental permissions
230 // will not be loaded if the experimental command line flag is not used.
231 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40232 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35233 // We can't call UninstallExtension with an unloaded/invalid
234 // extension ID.
235 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
236 << "with id: " << id;
237 return;
238 }
[email protected]d6ebc9792011-04-07 18:18:33239 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22240}
241
[email protected]7f8f24f2012-11-15 19:40:14242void ExtensionService::SetFileTaskRunnerForTesting(
243 base::SequencedTaskRunner* task_runner) {
244 file_task_runner_ = task_runner;
245}
246
[email protected]8e4560b62011-01-14 10:09:14247void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22248 external_extension_providers_.clear();
249}
250
[email protected]8e4560b62011-01-14 10:09:14251void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27252 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12253 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21254 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27255 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22256}
257
[email protected]9060d8b02012-01-13 02:14:30258bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22259 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42260 const GURL& update_url,
[email protected]1d5e58b2013-01-31 08:41:40261 Manifest::Location location) {
[email protected]ab22ba42011-01-14 16:36:38262 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
263 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20264
[email protected]8a87a5332011-08-11 17:54:59265 const Extension* extension = GetExtensionById(id, true);
266 if (extension) {
267 // Already installed. Skip this install if the current location has
268 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40269 Manifest::Location current = extension->location();
270 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30271 return false;
[email protected]8a87a5332011-08-11 17:54:59272 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22273 }
[email protected]9060d8b02012-01-13 02:14:30274
275 // Add |id| to the set of pending extensions. If it can not be added,
276 // then there is already a pending record from a higher-priority install
277 // source. In this case, signal that this extension will not be
278 // installed by returning false.
279 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40280 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30281 return false;
[email protected]31bb5ee62012-09-12 22:58:40282 }
[email protected]9060d8b02012-01-13 02:14:30283
[email protected]94fde232012-04-27 10:22:30284 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30285 return true;
[email protected]7a4c6852010-09-16 03:44:22286}
287
[email protected]dc9a74f72012-08-17 18:07:21288const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]be9915fb2013-07-18 09:28:55289 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]dc9a74f72012-08-17 18:07:21290 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09291}
292
[email protected]dc9a74f72012-08-17 18:07:21293bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09294 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27295}
296
[email protected]404fb002012-10-02 21:06:17297const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21298 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17299 std::set<std::string> extension_ids =
300 process_map_.GetExtensionsInProcess(renderer_child_id);
301 // All apps in one process share the same partition.
302 // It is only possible for the app to have isolated storage
303 // if there is only 1 app in the process.
304 if (extension_ids.size() != 1)
305 return NULL;
306
307 const extensions::Extension* extension =
308 extensions_.GetByID(*(extension_ids.begin()));
[email protected]561e33d52013-04-03 06:58:43309 // We still need to check if the extension has isolated storage,
[email protected]404fb002012-10-02 21:06:17310 // because it's common for there to be one extension in a process
[email protected]561e33d52013-04-03 06:58:43311 // without isolated storage.
312 if (extension &&
313 extensions::AppIsolationInfo::HasIsolatedStorage(extension))
[email protected]404fb002012-10-02 21:06:17314 return extension;
315
316 return NULL;
[email protected]da5683db2011-04-23 17:12:21317}
318
[email protected]6aeac8342010-10-01 20:21:18319// static
[email protected]d6ebc9792011-04-07 18:18:33320// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30321// --uninstall-extension, and to uninstall an extension via sync. The LOG
322// statements within this function are used to inform the user if the uninstall
323// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00324bool ExtensionService::UninstallExtensionHelper(
325 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18326 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33327 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40328 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18329 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33330 << "id: " << extension_id;
331 return false;
[email protected]6aeac8342010-10-01 20:21:18332 }
333
[email protected]d6ebc9792011-04-07 18:18:33334 // The following call to UninstallExtension will not allow an uninstall of a
335 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14336 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33337 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
338 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
339 << ": " << error;
340 return false;
341 }
[email protected]95da88c42011-03-31 10:07:33342
[email protected]6aeac8342010-10-01 20:21:18343 return true;
344}
345
[email protected]eaa7dd182010-12-14 11:09:00346ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24347 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45348 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23349 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39350 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03351 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35352 bool extensions_enabled,
353 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39354 : extensions::Blacklist::Observer(blacklist),
355 profile_(profile),
[email protected]bd306722012-07-11 20:43:59356 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43357 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28358 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38359 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]9c009092013-05-01 03:14:09360 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23361 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03362 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13363 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33364 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35365 ready_(ready),
[email protected]9c009092013-05-01 03:14:09366 toolbar_model_(this),
[email protected]8b280302011-10-13 22:22:23367 menu_manager_(profile),
[email protected]dff1e042012-04-27 10:59:18368 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54369 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19370 installs_delayed_for_gc_(false),
[email protected]460c6712013-04-24 07:20:01371 is_first_run_(false),
[email protected]9c009092013-05-01 03:14:09372 app_sync_bundle_(this),
373 extension_sync_bundle_(this) {
[email protected]c04fd3dd2013-09-05 08:20:10374#if defined(OS_CHROMEOS)
375 disable_garbage_collection_ = false;
376#endif
[email protected]a29a517a2011-01-21 21:11:12377 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20378
[email protected]36a784c2009-06-23 06:21:08379 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40380 if (command_line->HasSwitch(switches::kDisableExtensions) ||
381 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23382 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18383 }
[email protected]36a784c2009-06-23 06:21:08384
[email protected]3c4abc82012-10-22 22:25:54385 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
386 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27387 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07388 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27389 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07390 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27391 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07392 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05393 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
394 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08395 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
396 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28397 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03398 base::Closure callback =
399 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
400 base::Unretained(this));
401 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
402 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24403 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29404
[email protected]93fd78f42009-07-10 16:43:17405 // Set up the ExtensionUpdater
406 if (autoupdate_enabled) {
407 int update_frequency = kDefaultUpdateFrequencySeconds;
408 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25409 base::StringToInt(command_line->GetSwitchValueASCII(
410 switches::kExtensionsUpdateFrequency),
411 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17412 }
[email protected]42a08162012-03-16 18:09:11413 updater_.reset(new extensions::ExtensionUpdater(this,
414 extension_prefs,
415 profile->GetPrefs(),
416 profile,
[email protected]42a08162012-03-16 18:09:11417 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17418 }
419
[email protected]25ae0152011-11-18 14:40:02420 component_loader_.reset(
421 new extensions::ComponentLoader(this,
422 profile->GetPrefs(),
423 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14424
[email protected]0436b102011-04-15 18:30:03425 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25426 extensions::ExternalProviderImpl::CreateExternalProviders(
427 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05428 }
[email protected]b671760b2010-07-15 21:13:47429
[email protected]fb82dcd2012-03-21 14:15:46430 // Set this as the ExtensionService for extension sorting to ensure it
431 // cause syncs if required.
432 extension_prefs_->extension_sorting()->SetExtensionService(this);
433
[email protected]b3aa7182013-04-25 04:45:23434 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
435
[email protected]9ce11d22012-08-08 23:20:13436#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58437 extension_action_storage_manager_.reset(
438 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13439#endif
[email protected]c77f2352012-08-08 22:07:58440
[email protected]4c9201c42013-08-16 04:56:21441 shared_module_policy_provider_.reset(new SharedModuleProvider);
442
[email protected]cb0e50312011-05-09 15:03:07443 // How long is the path to the Extensions directory?
444 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
445 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25446}
447
[email protected]84df8332011-12-06 18:22:46448const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45449 return &extensions_;
450}
451
[email protected]84df8332011-12-06 18:22:46452const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45453 return &disabled_extensions_;
454}
455
[email protected]84df8332011-12-06 18:22:46456const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20457 return &terminated_extensions_;
458}
459
[email protected]695b5712012-12-06 23:55:28460const ExtensionSet* ExtensionService::blacklisted_extensions() const {
461 return &blacklisted_extensions_;
462}
463
[email protected]9e9c1d12013-07-31 01:58:12464const ExtensionSet* ExtensionService::delayed_installs() const {
465 return &delayed_installs_;
466}
467
[email protected]695b5712012-12-06 23:55:28468scoped_ptr<const ExtensionSet>
469 ExtensionService::GenerateInstalledExtensionsSet() const {
470 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01471 installed_extensions->InsertAll(extensions_);
472 installed_extensions->InsertAll(disabled_extensions_);
473 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28474 installed_extensions->InsertAll(blacklisted_extensions_);
475 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01476}
477
[email protected]3f213ad2012-07-26 23:39:41478extensions::PendingExtensionManager*
479 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37480 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45481}
482
[email protected]eaa7dd182010-12-14 11:09:00483ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17484 // No need to unload extensions here because they are profile-scoped, and the
485 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17486
[email protected]5df038b2012-07-16 19:03:27487 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14488 for (i = external_extension_providers_.begin();
489 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27490 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14491 provider->ServiceShutdown();
492 }
[email protected]6014d672008-12-05 00:38:25493}
494
[email protected]6d147912013-07-09 08:02:14495void ExtensionService::SetSyncStartFlare(
496 const syncer::SyncableService::StartSyncFlare& flare) {
497 flare_ = flare;
498}
499
[email protected]037228a2012-10-05 01:36:16500void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21501 system_->management_policy()->UnregisterProvider(
502 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16503}
504
[email protected]b2907fd2011-03-25 16:43:37505const Extension* ExtensionService::GetExtensionById(
506 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47507 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28508 if (include_disabled) {
509 // Include blacklisted extensions here because there are hundreds of
510 // callers of this function, and many might assume that this includes those
511 // that have been disabled due to blacklisting.
512 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
513 }
514 return GetExtensionById(id, include_mask);
515}
516
[email protected]399583b2012-12-11 09:33:42517GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
518 return content::SiteInstance::GetSiteForURL(
519 profile_,
520 Extension::GetBaseURLFromExtensionId(extension_id));
521}
522
[email protected]695b5712012-12-06 23:55:28523const Extension* ExtensionService::GetExtensionById(
524 const std::string& id, int include_mask) const {
525 std::string lowercase_id = StringToLowerASCII(id);
526 if (include_mask & INCLUDE_ENABLED) {
527 const Extension* extension = extensions_.GetByID(lowercase_id);
528 if (extension)
529 return extension;
530 }
531 if (include_mask & INCLUDE_DISABLED) {
532 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
533 if (extension)
534 return extension;
535 }
536 if (include_mask & INCLUDE_TERMINATED) {
537 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
538 if (extension)
539 return extension;
540 }
541 if (include_mask & INCLUDE_BLACKLISTED) {
542 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
543 if (extension)
544 return extension;
545 }
546 return NULL;
[email protected]78994ab02010-12-08 18:06:44547}
548
[email protected]eaa7dd182010-12-14 11:09:00549void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12550 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20551
[email protected]4a10006a2013-05-17 23:18:35552 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17553 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32554
[email protected]820d9bd2013-04-03 03:46:03555 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03556 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
557 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
558 // The sole purpose of this launch is to install a new extension from CWS
559 // and immediately terminate: loading already installed extensions is
560 // unnecessary and may interfere with the inline install dialog (e.g. if an
561 // extension listens to onStartup and opens a window).
562 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25563 } else {
[email protected]47b60072013-06-20 10:35:56564 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
565 component_loader_->LoadAll();
566 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19567
[email protected]2edd57852013-08-14 22:07:40568 // Attempt to re-enable extensions whose only disable reason is reloading.
569 std::vector<std::string> extensions_to_enable;
570 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
571 iter != disabled_extensions_.end(); ++iter) {
572 const Extension* e = iter->get();
573 if (extension_prefs_->GetDisableReasons(e->id()) ==
574 Extension::DISABLE_RELOAD) {
575 extensions_to_enable.push_back(e->id());
576 }
577 }
578 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
579 it != extensions_to_enable.end(); ++it) {
580 EnableExtension(*it);
581 }
582
[email protected]9f4e4f082013-06-21 07:11:19583 // Finish install (if possible) of extensions that were still delayed while
584 // the browser was shut down.
585 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
586 extension_prefs_->GetAllDelayedInstallInfo());
587 for (size_t i = 0; i < delayed_info->size(); ++i) {
588 ExtensionInfo* info = delayed_info->at(i).get();
589 scoped_refptr<const Extension> extension(NULL);
590 if (info->extension_manifest) {
591 std::string error;
592 extension = Extension::Create(
593 info->extension_path,
594 info->extension_location,
595 *info->extension_manifest,
596 extension_prefs_->GetDelayedInstallCreationFlags(
597 info->extension_id),
598 info->extension_id,
599 &error);
600 if (extension.get())
601 delayed_installs_.Insert(extension);
602 }
603 }
604 MaybeFinishDelayedInstallations();
605
606 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
607 extension_prefs_->GetAllDelayedInstallInfo());
608 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
609 delayed_info2->size() - delayed_info->size());
610
[email protected]47b60072013-06-20 10:35:56611 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25612
[email protected]47b60072013-06-20 10:35:56613 // TODO(erikkay) this should probably be deferred to a future point
614 // rather than running immediately at startup.
615 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57616
[email protected]47b60072013-06-20 10:35:56617 base::MessageLoop::current()->PostDelayedTask(
618 FROM_HERE,
619 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
620 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42621
[email protected]820d9bd2013-04-03 03:46:03622 if (extension_prefs_->NeedsStorageGarbageCollection()) {
623 GarbageCollectIsolatedStorage();
624 extension_prefs_->SetNeedsStorageGarbageCollection(false);
625 }
[email protected]4c9201c42013-08-16 04:56:21626 system_->management_policy()->RegisterProvider(
627 shared_module_policy_provider_.get());
[email protected]399583b2012-12-11 09:33:42628 }
[email protected]6014d672008-12-05 00:38:25629}
630
[email protected]31bb5ee62012-09-12 22:58:40631bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45632 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40633 const GURL& download_url,
634 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12635 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54636 if (browser_terminating_) {
637 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
638 // Leak the temp file at extension_path. We don't want to add to the disk
639 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
640 // the file is in the OS temp directory which should be cleaned up for us.
641 return false;
642 }
[email protected]a8af9fdb2010-10-28 21:52:20643
[email protected]3f213ad2012-07-26 23:39:41644 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06645 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32646
[email protected]695b5712012-12-06 23:55:28647 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06648 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33649 LOG(WARNING) << "Will not update extension " << id
650 << " because it is not installed or pending";
651 // Delete extension_path since we're not creating a CrxInstaller
652 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14653 if (!GetFileTaskRunner()->PostTask(
654 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36655 base::Bind(
[email protected]7296f2762011-11-21 19:23:44656 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36657 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03658
659 return false;
[email protected]e957fe52009-06-23 16:51:05660 }
661
[email protected]aa142702010-03-26 01:26:33662 // We want a silent install only for non-pending extensions and
663 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37664 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05665 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37666 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33667
[email protected]f8636f92013-08-09 21:02:37668 scoped_refptr<CrxInstaller> installer(
669 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16670 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06671 if (pending_extension_info) {
672 installer->set_install_source(pending_extension_info->install_source());
673 if (pending_extension_info->install_silently())
674 installer->set_allow_silent_install(true);
675 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41676 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06677 }
[email protected]7d8b7072012-04-07 01:07:46678 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45679 // its auto-update URL is from the webstore, treat it as a webstore install.
680 // Note that we ignore some older extensions with blank auto-update URLs
681 // because we are mostly concerned with restrictions on NaCl extensions,
682 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53683 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28684 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38685 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35686 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06687 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53688 creation_flags |= Extension::FROM_WEBSTORE;
689 }
690
691 // Bookmark apps being updated is kind of a contradiction, but that's because
692 // we mark the default apps as bookmark apps, and they're hosted in the web
693 // store, thus they can get updated. See http://crbug.com/101605 for more
694 // details.
695 if (extension && extension->from_bookmark())
696 creation_flags |= Extension::FROM_BOOKMARK;
697
[email protected]e33bbc22012-08-27 22:05:46698 if (extension && extension->was_installed_by_default())
699 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
700
[email protected]a12ce8b22012-01-17 18:40:53701 installer->set_creation_flags(creation_flags);
702
[email protected]6dfbbf82010-03-12 23:09:16703 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06704 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07705 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16706 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03707
708 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09709 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03710
711 return true;
[email protected]e957fe52009-06-23 16:51:05712}
713
[email protected]e178ad92013-06-28 02:29:25714void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12715 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05716
717 // If the extension is already reloading, don't reload again.
718 if (extension_prefs_->GetDisableReasons(extension_id) &
719 Extension::DISABLE_RELOAD) {
720 return;
721 }
722
[email protected]650b2d52013-02-10 03:41:45723 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02724 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40725
[email protected]f17dbd42010-08-16 23:21:10726 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06727 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29728 // If the extension has an inspector open for its background page, detach
729 // the inspector and hang onto a cookie for it, so that we can reattach
730 // later.
[email protected]31d8f5f22012-04-02 15:22:08731 // TODO(yoz): this is not incognito-safe!
732 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13733 extensions::ExtensionHost* host =
734 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36735 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29736 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09737 scoped_refptr<DevToolsAgentHost> agent_host =
738 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
739 agent_host->DisconnectRenderViewHost();
740 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29741 }
742
[email protected]b65272f2009-08-31 15:47:06743 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50744 // BeingUpgraded is set back to false when the extension is added.
745 SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03746 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03747 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16748 } else {
749 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06750 }
751
[email protected]9f4e4f082013-06-21 07:11:19752 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48753 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05754 return;
755 }
756
[email protected]43ceb002012-02-10 23:19:15757 // If we're reloading a component extension, use the component extension
758 // loader's reloader.
759 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25760 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15761 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25762 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15763 return;
764 }
765
[email protected]e6090e42010-03-23 22:44:08766 // Check the installed extensions to see if what we're reloading was already
767 // installed.
[email protected]e178ad92013-06-28 02:29:25768 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08769 scoped_ptr<ExtensionInfo> installed_extension(
770 extension_prefs_->GetInstalledExtensionInfo(extension_id));
771 if (installed_extension.get() &&
772 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01773 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08774 } else {
[email protected]d8c8f25f2011-11-02 18:18:01775 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08776 // We should always be able to remember the extension's path. If it's not in
777 // the map, someone failed to update |unloaded_extension_paths_|.
778 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01779 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08780 }
[email protected]e178ad92013-06-28 02:29:25781 // When reloading is done, mark this extension as done reloading.
782 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40783}
784
[email protected]fa2416f2011-05-03 08:41:20785bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08786 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20787 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14788 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12789 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20790
[email protected]0d6ec3a72011-09-02 02:09:43791 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25792
[email protected]e7afe2452010-08-22 16:19:13793 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09794 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32795
[email protected]95da88c42011-03-31 10:07:33796 // Policy change which triggers an uninstall will always set
797 // |external_uninstall| to true so this is the only way to uninstall
798 // managed extensions.
[email protected]65187152012-06-02 13:14:14799 if (!external_uninstall &&
800 !system_->management_policy()->UserMayModifySettings(
801 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07802 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27803 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53804 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09805 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33806 return false;
807 }
[email protected]95da88c42011-03-31 10:07:33808
[email protected]3bdba0d2011-08-23 07:17:30809 // Extract the data we need for sync now, but don't actually sync until we've
810 // completed the uninstallation.
[email protected]6d147912013-07-09 08:02:14811 // TODO(tim): If we get here and IsSyncing is false, this will cause
812 // "back from the dead" style bugs, because sync will add-back the extension
813 // that was uninstalled here when MergeDataAndStartSyncing is called.
814 // See crbug.com/256795.
[email protected]65f173552012-06-28 22:43:58815 syncer::SyncChange sync_change;
[email protected]6d147912013-07-09 08:02:14816 if (extensions::sync_helper::IsSyncableApp(extension.get())) {
817 if (app_sync_bundle_.IsSyncing())
818 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension.get());
819 else if (is_ready() && !flare_.is_null())
820 flare_.Run(syncer::APPS); // Tell sync to start ASAP.
821 } else if (extensions::sync_helper::IsSyncableExtension(extension.get())) {
822 if (extension_sync_bundle_.IsSyncing())
823 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
824 else if (is_ready() && !flare_.is_null())
825 flare_.Run(syncer::EXTENSIONS); // Tell sync to start ASAP.
[email protected]3bdba0d2011-08-23 07:17:30826 }
827
[email protected]dc24976f2013-06-02 21:15:09828 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08829 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
830 EXTERNAL_EXTENSION_UNINSTALLED,
831 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02832 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
833 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
834 EXTERNAL_EXTENSION_UNINSTALLED,
835 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
836 } else {
837 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
838 EXTERNAL_EXTENSION_UNINSTALLED,
839 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
840 }
[email protected]d96eb512012-11-01 23:44:08841 }
[email protected]9b217652010-10-08 22:04:23842 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08843 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09844 RecordPermissionMessagesHistogram(extension.get(),
845 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23846
[email protected]831aa212010-03-26 13:55:19847 // Unload before doing more cleanup to ensure that nothing is hanging on to
848 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59849 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19850
[email protected]9f1087e2009-06-15 17:29:32851 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05852 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14853 if (!GetFileTaskRunner()->PostTask(
854 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36855 base::Bind(
[email protected]14908b72011-04-20 06:54:36856 &extension_file_util::UninstallExtension,
857 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20858 extension_id)))
[email protected]14908b72011-04-20 06:54:36859 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32860 }
861
[email protected]6b414c232013-06-05 07:53:34862 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36863 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43864 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09865 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43866
[email protected]399583b2012-12-11 09:33:42867 if (is_storage_isolated) {
868 BrowserContext::AsyncObliterateStoragePartition(
869 profile_,
870 GetSiteForExtensionId(extension_id),
871 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
872 AsWeakPtr()));
873 } else {
874 if (extension->is_hosted_app() &&
875 !profile_->GetExtensionSpecialStoragePolicy()->
876 IsStorageProtected(launch_web_url_origin)) {
877 extensions::DataDeleter::StartDeleting(
878 profile_, extension_id, launch_web_url_origin);
879 }
880 extensions::DataDeleter::StartDeleting(profile_, extension_id,
881 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43882 }
[email protected]0d6ec3a72011-09-02 02:09:43883
[email protected]0dfe05c2011-02-23 23:03:36884 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06885
886 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07887 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27888 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53889 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09890 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33891
[email protected]5db9ada2012-04-11 13:48:20892 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32893 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20894 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
895 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32896 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20897 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30898 }
899
[email protected]399583b2012-12-11 09:33:42900 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05901
[email protected]198b5902013-06-27 10:36:11902 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19903
[email protected]d4eda592013-09-18 03:37:57904 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
905 external_uninstall);
906
[email protected]333b1de2011-09-12 18:28:50907 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18908 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
909
[email protected]d6ebc9792011-04-07 18:18:33910 return true;
[email protected]c10da4b02010-03-25 14:38:32911}
912
[email protected]c3cfb012011-04-06 22:07:35913bool ExtensionService::IsExtensionEnabled(
914 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46915 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21916 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18917 return true;
[email protected]dc9a74f72012-08-17 18:07:21918 }
[email protected]36429da2011-07-11 20:25:18919
[email protected]695b5712012-12-06 23:55:28920 if (disabled_extensions_.Contains(extension_id) ||
921 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25922 return false;
[email protected]695b5712012-12-06 23:55:28923 }
[email protected]ad83ca242011-07-29 01:32:25924
925 // If the extension hasn't been loaded yet, check the prefs for it. Assume
926 // enabled unless otherwise noted.
927 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31928 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35929}
930
931bool ExtensionService::IsExternalExtensionUninstalled(
932 const std::string& extension_id) const {
933 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
934}
935
[email protected]e2377032013-01-18 17:53:22936bool ExtensionService::IsExtensionEnabledForLauncher(
937 const std::string& extension_id) const {
938 return IsExtensionEnabled(extension_id) &&
939 !GetTerminatedExtension(extension_id);
940}
941
[email protected]eaa7dd182010-12-14 11:09:00942void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12943 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20944
[email protected]06f92562011-04-29 19:27:31945 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39946 return;
[email protected]0c6da502009-08-14 22:32:39947
[email protected]b3317ad2011-04-28 23:46:00948 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28949 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52950
[email protected]695b5712012-12-06 23:55:28951 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31952 // This can happen if sync enables an extension that is not
953 // installed yet.
954 if (!extension)
955 return;
956
[email protected]d96eb512012-11-01 23:44:08957 if (IsUnacknowledgedExternalExtension(extension)) {
958 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
959 EXTERNAL_EXTENSION_REENABLED,
960 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02961 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
962 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
963 EXTERNAL_EXTENSION_REENABLED,
964 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
965 } else {
966 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
967 EXTERNAL_EXTENSION_REENABLED,
968 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
969 }
[email protected]612a1cb12012-10-17 13:18:03970 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08971 }
[email protected]612a1cb12012-10-17 13:18:03972
[email protected]0c6da502009-08-14 22:32:39973 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46974 extensions_.Insert(make_scoped_refptr(extension));
975 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39976
[email protected]62d30f42009-10-01 22:36:06977 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30978
[email protected]f74d7f32012-06-19 19:22:51979 // Notify listeners that the extension was enabled.
980 content::NotificationService::current()->Notify(
981 chrome::NOTIFICATION_EXTENSION_ENABLED,
982 content::Source<Profile>(profile_),
983 content::Details<const Extension>(extension));
984
[email protected]3bdba0d2011-08-23 07:17:30985 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39986}
987
[email protected]44d62b62012-04-11 00:06:03988void ExtensionService::DisableExtension(
989 const std::string& extension_id,
990 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12991 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20992
[email protected]b2ba9962009-12-10 20:10:15993 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31994 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52995 return;
[email protected]1784e83a2009-09-08 21:01:52996
[email protected]06f92562011-04-29 19:27:31997 const Extension* extension = GetInstalledExtension(extension_id);
998 // |extension| can be NULL if sync disables an extension that is not
999 // installed yet.
[email protected]65187152012-06-02 13:14:141000 if (extension &&
[email protected]505ccdc2013-06-20 20:15:351001 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:141002 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:331003 return;
[email protected]65187152012-06-02 13:14:141004 }
[email protected]95da88c42011-03-31 10:07:331005
[email protected]b3317ad2011-04-28 23:46:001006 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:281007 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:521008
[email protected]695b5712012-12-06 23:55:281009 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
1010 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:311011 if (!extension)
1012 return;
1013
[email protected]64742de32013-04-22 08:44:341014 // Reset the background_page_ready flag
1015 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
1016 extension_runtime_data_[extension->id()].background_page_ready = false;
1017
[email protected]5c094792012-09-07 19:44:531018 // Move it over to the disabled list. Don't send a second unload notification
1019 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:461020 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:531021 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461022 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:531023 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
1024 } else {
[email protected]84df8332011-12-06 18:22:461025 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:531026 }
[email protected]3bdba0d2011-08-23 07:17:301027
1028 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:521029}
1030
[email protected]1abf05e2013-07-09 17:04:361031void ExtensionService::DisableUserExtensions(
1032 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:141033 extensions::ManagementPolicy* management_policy =
1034 system_->management_policy();
1035 extensions::ExtensionList to_disable;
1036
1037 for (ExtensionSet::const_iterator extension = extensions_.begin();
1038 extension != extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111039 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141040 to_disable.push_back(*extension);
1041 }
1042 for (ExtensionSet::const_iterator extension = terminated_extensions_.begin();
1043 extension != terminated_extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111044 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141045 to_disable.push_back(*extension);
1046 }
1047
1048 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1049 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381050 if ((*extension)->was_installed_by_default() &&
1051 extension_urls::IsWebstoreUpdateUrl(
1052 extensions::ManifestURL::GetUpdateURL(*extension)))
1053 continue;
[email protected]1abf05e2013-07-09 17:04:361054 const std::string& id = (*extension)->id();
1055 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1056 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141057 }
1058}
1059
[email protected]eaa7dd182010-12-14 11:09:001060void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281061 const Extension* extension) {
1062 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491063 RecordPermissionMessagesHistogram(
1064 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251065 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1066 EnableExtension(extension->id());
1067}
1068
[email protected]009633c2013-03-07 22:08:281069void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451070 CHECK(extension);
1071 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281072 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451073}
1074
[email protected]fe2dd7742011-04-19 22:52:491075// static
1076void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051077 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171078 // Since this is called from multiple sources, and since the histogram macros
1079 // use statics, we need to manually lookup the histogram ourselves.
1080 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491081 histogram,
1082 1,
[email protected]c2e66e12012-06-27 06:27:061083 PermissionMessage::kEnumBoundary,
1084 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171085 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491086
[email protected]13c68b62013-05-17 11:29:051087 PermissionMessages permissions =
1088 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491089 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061090 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491091 } else {
[email protected]c2e66e12012-06-27 06:27:061092 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521093 it != permissions.end(); ++it)
1094 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491095 }
1096}
1097
[email protected]eaa7dd182010-12-14 11:09:001098void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421099 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061100 // was loaded, otherwise a race can arise where a renderer that is created
1101 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421102 // that the request context doesn't yet know about. The profile is responsible
1103 // for ensuring its URLRequestContexts appropriately discover the loaded
1104 // extension.
[email protected]31d8f5f22012-04-02 15:22:081105 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061106
[email protected]d5949312012-12-03 22:13:301107 // Tell renderers about the new extension, unless it's a theme (renderers
1108 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191109 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491110 for (content::RenderProcessHost::iterator i(
1111 content::RenderProcessHost::AllHostsIterator());
1112 !i.IsAtEnd(); i.Advance()) {
1113 content::RenderProcessHost* host = i.GetCurrentValue();
1114 Profile* host_profile =
1115 Profile::FromBrowserContext(host->GetBrowserContext());
1116 if (host_profile->GetOriginalProfile() ==
1117 profile_->GetOriginalProfile()) {
1118 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1119 1, ExtensionMsg_Loaded_Params(extension));
1120 host->Send(
1121 new ExtensionMsg_Loaded(loaded_extensions));
1122 }
[email protected]c8d407e2011-04-28 21:27:171123 }
[email protected]77a6970c2011-04-23 16:58:561124 }
1125
[email protected]3442a662012-01-12 08:06:171126 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1127 // extension.
1128 //
1129 // NOTE: It is important that this happen after notifying the renderers about
1130 // the new extensions so that if we navigate to an extension URL in
1131 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1132 content::NotificationService::current()->Notify(
1133 chrome::NOTIFICATION_EXTENSION_LOADED,
1134 content::Source<Profile>(profile_),
1135 content::Details<const Extension>(extension));
1136
[email protected]c8d407e2011-04-28 21:27:171137 // Tell a random-ass collection of other subsystems about the new extension.
1138 // TODO(aa): What should we do with all this goop? Can it move into the
1139 // relevant objects via EXTENSION_LOADED?
1140
1141 profile_->GetExtensionSpecialStoragePolicy()->
1142 GrantRightsForExtension(extension);
1143
1144 UpdateActiveExtensionsInCrashReporter();
1145
[email protected]c8d407e2011-04-28 21:27:171146 // If the extension has permission to load chrome://favicon/ resources we need
1147 // to make sure that the FaviconSource is registered with the
1148 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051149 if (extensions::PermissionsData::HasHostPermission(
1150 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171151 FaviconSource* favicon_source = new FaviconSource(profile_,
1152 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531153 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171154 }
[email protected]b07e606e2012-09-15 20:16:151155
1156#if !defined(OS_ANDROID)
1157 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051158 if (extensions::PermissionsData::HasHostPermission(
1159 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151160 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531161 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151162 }
1163#endif
1164
[email protected]5eddc3e2011-10-26 04:33:311165 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051166 if (extensions::PermissionsData::HasHostPermission(
1167 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511168 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531169 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311170 }
[email protected]62d30f42009-10-01 22:36:061171}
1172
[email protected]a9f39a312010-12-23 22:14:271173void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591174 const Extension* extension,
1175 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271176 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071177 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271178 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531179 content::Source<Profile>(profile_),
1180 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061181
[email protected]f3b1a082011-11-18 00:34:301182 for (content::RenderProcessHost::iterator i(
1183 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561184 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301185 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081186 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301187 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081188 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171189 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561190 }
1191
[email protected]31d8f5f22012-04-02 15:22:081192 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171193 profile_->GetExtensionSpecialStoragePolicy()->
1194 RevokeRightsForExtension(extension);
1195
[email protected]b777b332011-04-16 04:01:081196#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231197 // Revoke external file access for the extension from its file system context.
1198 // It is safe to access the extension's storage partition at this point. The
1199 // storage partition may get destroyed only after the extension gets unloaded.
1200 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1201 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091202 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231203 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091204 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321205 if (filesystem_context && filesystem_context->external_backend()) {
1206 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171207 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061208 }
[email protected]c8d407e2011-04-28 21:27:171209#endif
1210
1211 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061212}
1213
[email protected]0a071a32011-02-08 00:18:241214Profile* ExtensionService::profile() {
1215 return profile_;
1216}
1217
[email protected]45759612012-07-10 17:21:231218extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451219 return extension_prefs_;
1220}
1221
[email protected]0d9a2202011-11-09 13:48:411222extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381223 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201224}
1225
[email protected]e2d720aa2012-05-17 00:11:351226extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441227 return extension_prefs()->content_settings_store();
1228}
1229
[email protected]25ae0152011-11-18 14:40:021230bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351231 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021232}
1233
[email protected]7f8f24f2012-11-15 19:40:141234base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091235 if (file_task_runner_.get())
1236 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141237
1238 // We should be able to interrupt any part of extension install process during
1239 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1240 // will be ignored/deleted while we will block on started tasks.
1241 std::string token("ext_install-");
1242 token.append(profile_->GetPath().AsUTF8Unsafe());
1243 file_task_runner_ = BrowserThread::GetBlockingPool()->
1244 GetSequencedTaskRunnerWithShutdownBehavior(
1245 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1246 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091247 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141248}
1249
[email protected]42a08162012-03-16 18:09:111250extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061251 return updater_.get();
1252}
1253
[email protected]4ee07c62012-08-21 12:40:421254void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251255 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281256
[email protected]aa96d3a2010-08-21 08:45:251257 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461258 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251259 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361260 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421261 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251262 to_be_removed.push_back(extension->id());
1263 }
1264
1265 // UnloadExtension will change the extensions_ list. So, we should
1266 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401267 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591268 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251269}
1270
[email protected]31206602011-04-13 23:07:321271void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351272 if (updater()) {
[email protected]94fde232012-04-27 10:22:301273 if (AreAllExternalProvidersReady()) {
1274 updater()->CheckSoon();
1275 } else {
1276 // Sync can start updating before all the external providers are ready
1277 // during startup. Start the update as soon as those providers are ready,
1278 // but not before.
1279 update_once_all_providers_are_ready_ = true;
1280 }
[email protected]c3cfb012011-04-06 22:07:351281 } else {
[email protected]31206602011-04-13 23:07:321282 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351283 }
1284}
1285
[email protected]3f98fd92012-11-17 15:54:291286syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321287 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581288 const syncer::SyncDataList& initial_sync_data,
1289 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1290 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201291 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281292 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301293
1294 switch (type) {
[email protected]a4a147652012-07-03 23:41:321295 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201296 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281297 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201298 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301299 break;
1300
[email protected]a4a147652012-07-03 23:41:321301 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281302 app_sync_bundle_.SetupSync(sync_processor.release(),
1303 sync_error_factory.release(),
1304 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301305 break;
1306
1307 default:
1308 LOG(FATAL) << "Got " << type << " ModelType";
1309 }
[email protected]3bdba0d2011-08-23 07:17:301310
[email protected]b1417ab2011-12-15 17:09:111311 // Process local extensions.
1312 // TODO(yoz): Determine whether pending extensions should be considered too.
1313 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581314 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1315 syncer::SyncChangeList sync_change_list;
1316 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301317 i != sync_data_list.end();
1318 ++i) {
[email protected]5db9ada2012-04-11 13:48:201319 switch (type) {
[email protected]a4a147652012-07-03 23:41:321320 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201321 sync_change_list.push_back(
1322 extension_sync_bundle_.CreateSyncChange(*i));
1323 break;
[email protected]a4a147652012-07-03 23:41:321324 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201325 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1326 break;
1327 default:
1328 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401329 }
[email protected]3bdba0d2011-08-23 07:17:301330 }
[email protected]3bdba0d2011-08-23 07:17:301331
[email protected]5db9ada2012-04-11 13:48:201332
[email protected]a4a147652012-07-03 23:41:321333 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201334 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321335 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201336 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1337 }
[email protected]0ffd9e52012-01-26 21:44:341338
[email protected]3f98fd92012-11-17 15:54:291339 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301340}
1341
[email protected]a4a147652012-07-03 23:41:321342void ExtensionService::StopSyncing(syncer::ModelType type) {
1343 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201344 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321345 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201346 extension_sync_bundle_.Reset();
1347 }
[email protected]3bdba0d2011-08-23 07:17:301348}
1349
[email protected]65f173552012-06-28 22:43:581350syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321351 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401352 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201353 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401354 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201355 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201356
1357 // We should only get sync data for extensions and apps.
1358 NOTREACHED();
1359
[email protected]65f173552012-06-28 22:43:581360 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301361}
1362
[email protected]65f173552012-06-28 22:43:581363syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301364 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581365 const syncer::SyncChangeList& change_list) {
1366 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301367 i != change_list.end();
1368 ++i) {
[email protected]a4a147652012-07-03 23:41:321369 syncer::ModelType type = i->sync_data().GetDataType();
1370 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201371 extension_sync_bundle_.ProcessSyncChange(
1372 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321373 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201374 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1375 }
[email protected]3bdba0d2011-08-23 07:17:301376 }
1377
[email protected]0ffd9e52012-01-26 21:44:341378 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1379
[email protected]65f173552012-06-28 22:43:581380 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561381}
1382
[email protected]5db9ada2012-04-11 13:48:201383extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1384 const Extension& extension) const {
1385 return extensions::ExtensionSyncData(extension,
1386 IsExtensionEnabled(extension.id()),
1387 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561388}
1389
[email protected]5db9ada2012-04-11 13:48:201390extensions::AppSyncData ExtensionService::GetAppSyncData(
1391 const Extension& extension) const {
1392 return extensions::AppSyncData(
1393 extension,
1394 IsExtensionEnabled(extension.id()),
1395 IsIncognitoEnabled(extension.id()),
[email protected]5db9ada2012-04-11 13:48:201396 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1397 extension.id()),
1398 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1399}
[email protected]3bdba0d2011-08-23 07:17:301400
[email protected]5db9ada2012-04-11 13:48:201401std::vector<extensions::ExtensionSyncData>
1402 ExtensionService::GetExtensionSyncDataList() const {
1403 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1404 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1405 &extension_sync_list);
1406 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1407 &extension_sync_list);
1408 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1409 &extension_sync_list);
1410
1411 std::vector<extensions::ExtensionSyncData> pending_extensions =
1412 extension_sync_bundle_.GetPendingData();
1413 extension_sync_list.insert(extension_sync_list.begin(),
1414 pending_extensions.begin(),
1415 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301416
1417 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561418}
1419
[email protected]5db9ada2012-04-11 13:48:201420std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1421 const {
1422 std::vector<extensions::AppSyncData> app_sync_list;
1423 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1424 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1425 &app_sync_list);
1426 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1427 &app_sync_list);
1428
1429 std::vector<extensions::AppSyncData> pending_apps =
1430 app_sync_bundle_.GetPendingData();
1431 app_sync_list.insert(app_sync_list.begin(),
1432 pending_apps.begin(),
1433 pending_apps.end());
1434
1435 return app_sync_list;
1436}
1437
1438bool ExtensionService::ProcessExtensionSyncData(
1439 const extensions::ExtensionSyncData& extension_sync_data) {
1440 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321441 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201442 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1443 extension_sync_data);
1444 CheckForUpdatesSoon();
1445 return false;
1446 }
1447
1448 return true;
1449}
1450
1451bool ExtensionService::ProcessAppSyncData(
1452 const extensions::AppSyncData& app_sync_data) {
1453 const std::string& id = app_sync_data.id();
[email protected]5db9ada2012-04-11 13:48:201454
1455 if (app_sync_data.app_launch_ordinal().IsValid() &&
1456 app_sync_data.page_ordinal().IsValid()) {
1457 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1458 id,
1459 app_sync_data.app_launch_ordinal());
1460 extension_prefs_->extension_sorting()->SetPageOrdinal(
1461 id,
1462 app_sync_data.page_ordinal());
1463 }
1464
[email protected]5db9ada2012-04-11 13:48:201465 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321466 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201467 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1468 CheckForUpdatesSoon();
1469 return false;
1470 }
1471
1472 return true;
1473}
1474
1475bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321476 syncer::ModelType type) const {
1477 if (type == syncer::EXTENSIONS &&
[email protected]8f3bcbd2013-06-05 08:42:401478 extensions::sync_helper::IsSyncableExtension(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201479 return true;
[email protected]31bb5ee62012-09-12 22:58:401480 }
[email protected]5db9ada2012-04-11 13:48:201481
[email protected]a4a147652012-07-03 23:41:321482 if (type == syncer::APPS &&
[email protected]8f3bcbd2013-06-05 08:42:401483 extensions::sync_helper::IsSyncableApp(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201484 return true;
[email protected]31bb5ee62012-09-12 22:58:401485 }
[email protected]5db9ada2012-04-11 13:48:201486
1487 return false;
1488}
1489
1490bool ExtensionService::ProcessExtensionSyncDataHelper(
1491 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321492 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301493 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061494 const Extension* extension = GetInstalledExtension(id);
1495
1496 // TODO(bolms): we should really handle this better. The particularly bad
1497 // case is where an app becomes an extension or vice versa, and we end up with
1498 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201499 if (extension && !IsCorrectSyncType(*extension, type))
1500 return true;
[email protected]90310d92011-04-17 07:35:041501
1502 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301503 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041504 if (!UninstallExtensionHelper(this, id)) {
1505 LOG(WARNING) << "Could not uninstall extension " << id
1506 << " for sync";
1507 }
[email protected]5db9ada2012-04-11 13:48:201508 return true;
[email protected]90310d92011-04-17 07:35:041509 }
1510
[email protected]caf2d442012-05-04 10:00:501511 // Extension from sync was uninstalled by the user as external extensions.
1512 // Honor user choice and skip installation/enabling.
1513 if (IsExternalExtensionUninstalled(id)) {
1514 LOG(WARNING) << "Extension with id " << id
1515 << " from sync was uninstalled as external extension";
1516 return true;
1517 }
1518
[email protected]06f92562011-04-29 19:27:311519 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191520 // If the extension has been disabled from sync, it may not have
1521 // been installed yet, so we don't know if the disable reason was a
[email protected]b914e2952013-04-26 07:10:031522 // permissions increase. That will be updated once CheckPermissionsIncrease
[email protected]7e9e2422012-12-13 19:54:191523 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401524 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311525 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401526 else
[email protected]7e9e2422012-12-13 19:54:191527 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371528
[email protected]406b5a92011-11-08 11:58:261529 // We need to cache some version information here because setting the
1530 // incognito flag invalidates the |extension| pointer (it reloads the
1531 // extension).
[email protected]9060d8b02012-01-13 02:14:301532 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261533 int result = extension ?
1534 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1535 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1536 extension = NULL; // No longer safe to use.
1537
1538 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311539 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041540 if (result < 0) {
1541 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201542 return false;
[email protected]b020dc12011-12-06 04:35:421543 }
[email protected]06f92562011-04-29 19:27:311544 } else {
[email protected]06f92562011-04-29 19:27:311545 // TODO(akalin): Replace silent update with a list of enabled
1546 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331547 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201548
[email protected]a4a147652012-07-03 23:41:321549 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]8f3bcbd2013-06-05 08:42:401550 extensions::PendingExtensionInfo::ShouldAllowInstallPredicate filter =
1551 (type == syncer::APPS) ? extensions::sync_helper::IsSyncableApp :
1552 extensions::sync_helper::IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201553
[email protected]06f92562011-04-29 19:27:311554 if (!pending_extension_manager()->AddFromSync(
1555 id,
[email protected]3bdba0d2011-08-23 07:17:301556 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201557 filter,
[email protected]6cc7dbae2011-04-29 21:18:331558 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311559 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301560 // This means that the extension is already pending installation, with a
1561 // non-INTERNAL location. Add to pending_sync_data, even though it will
1562 // never be removed (we'll never install a syncable version of the
1563 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311564 }
[email protected]3bdba0d2011-08-23 07:17:301565 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201566 return false;
[email protected]90310d92011-04-17 07:35:041567 }
[email protected]5db9ada2012-04-11 13:48:201568
1569 return true;
[email protected]90310d92011-04-17 07:35:041570}
1571
[email protected]c3cfb012011-04-06 22:07:351572bool ExtensionService::IsIncognitoEnabled(
1573 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481574 const Extension* extension = GetInstalledExtension(extension_id);
1575 if (extension && !extension->can_be_incognito_enabled())
1576 return false;
[email protected]c3cfb012011-04-06 22:07:351577 // If this is an existing component extension we always allow it to
1578 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401579 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411580 return true;
[email protected]fc05749a2013-07-10 06:59:111581 if (extension && extension->force_incognito_enabled())
1582 return true;
[email protected]cb0ce1e022010-03-10 19:54:411583
1584 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351585 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501586}
[email protected]55a35692010-02-11 23:25:211587
[email protected]c3cfb012011-04-06 22:07:351588void ExtensionService::SetIsIncognitoEnabled(
1589 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371590 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481591 if (extension && !extension->can_be_incognito_enabled())
1592 return;
[email protected]1d5e58b2013-01-31 08:41:401593 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481594 // This shouldn't be called for component extensions unless they are
1595 // syncable.
[email protected]8f3bcbd2013-06-05 08:42:401596 DCHECK(extensions::sync_helper::IsSyncable(extension));
[email protected]b873cd92012-02-09 21:51:481597
1598 // If we are here, make sure the we aren't trying to change the value.
1599 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1600
[email protected]c3cfb012011-04-06 22:07:351601 return;
1602 }
1603
[email protected]8c6c8c22011-03-09 12:52:341604 // Broadcast unloaded and loaded events to update browser state. Only bother
1605 // if the value changed and the extension is actually enabled, since there is
1606 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351607 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341608 if (enabled == old_enabled)
1609 return;
1610
[email protected]c3cfb012011-04-06 22:07:351611 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371612
[email protected]ffbf3f12011-12-09 22:10:331613 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341614
1615 // When we reload the extension the ID may be invalidated if we've passed it
1616 // by const ref everywhere. Make a copy to be safe.
1617 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261618 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331619 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301620
[email protected]406b5a92011-11-08 11:58:261621 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341622 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301623 if (extension)
1624 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211625}
1626
[email protected]dc9a74f72012-08-17 18:07:211627bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051628 // We allow the extension to see events and data from another profile iff it
1629 // uses "spanning" behavior and it has incognito access. "split" mode
1630 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221631 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351632 return IsIncognitoEnabled(extension->id()) &&
[email protected]06492ed2013-03-24 22:13:141633 !extensions::IncognitoInfo::IsSplitMode(extension);
[email protected]2a8f24e2010-11-03 21:37:051634}
1635
[email protected]5ef7b002011-05-12 23:16:201636bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1637 if (extension->is_hosted_app())
1638 return true;
1639 // Packaged apps and regular extensions need to be enabled specifically for
1640 // incognito (and split mode should be set).
[email protected]06492ed2013-03-24 22:13:141641 return extensions::IncognitoInfo::IsSplitMode(extension) &&
[email protected]5ef7b002011-05-12 23:16:201642 IsIncognitoEnabled(extension->id());
1643}
1644
[email protected]168389f2011-12-20 17:12:481645void ExtensionService::OnExtensionMoved(
1646 const std::string& moved_extension_id,
1647 const std::string& predecessor_extension_id,
1648 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231649 extension_prefs_->extension_sorting()->OnExtensionMoved(
1650 moved_extension_id,
1651 predecessor_extension_id,
1652 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481653
1654 const Extension* extension = GetInstalledExtension(moved_extension_id);
1655 if (extension)
1656 SyncExtensionChangeIfNeeded(*extension);
1657}
1658
[email protected]dc9a74f72012-08-17 18:07:211659bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081660 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441661 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081662 extension_prefs_->AllowFileAccess(extension->id()));
1663}
1664
[email protected]eaa7dd182010-12-14 11:09:001665void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481666 bool allow) {
1667 // Reload to update browser state. Only bother if the value changed and the
1668 // extension is actually enabled, since there is no UI otherwise.
1669 bool old_allow = AllowFileAccess(extension);
1670 if (allow == old_allow)
1671 return;
1672
[email protected]05c82182010-06-24 17:49:081673 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481674
[email protected]84df8332011-12-06 18:22:461675 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481676 if (extension_is_enabled)
1677 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081678}
1679
[email protected]8e4560b62011-01-14 10:09:141680// Some extensions will autoupdate themselves externally from Chrome. These
1681// are typically part of some larger client application package. To support
1682// these, the extension will register its location in the the preferences file
1683// (and also, on Windows, in the registry) and this code will periodically
1684// check that location for a .crx file, which it will then install locally if
1685// a new version is available.
1686// Errors are reported through ExtensionErrorReporter. Succcess is not
1687// reported.
[email protected]eaa7dd182010-12-14 11:09:001688void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121689 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141690
1691 // Note that this installation is intentionally silent (since it didn't
1692 // go through the front-end). Extensions that are registered in this
1693 // way are effectively considered 'pre-bundled', and so implicitly
1694 // trusted. In general, if something has HKLM or filesystem access,
1695 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121696
[email protected]8e4560b62011-01-14 10:09:141697 // Ask each external extension provider to give us a call back for each
1698 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271699 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141700 for (i = external_extension_providers_.begin();
1701 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271702 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141703 provider->VisitRegisteredExtension();
1704 }
1705
[email protected]50067e52011-10-20 23:17:071706 // Do any required work that we would have done after completion of all
1707 // providers.
1708 if (external_extension_providers_.empty()) {
1709 OnAllExternalProvidersReady();
1710 }
[email protected]9f1087e2009-06-15 17:29:321711}
1712
[email protected]50067e52011-10-20 23:17:071713void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271714 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121715 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071716 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121717
1718 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141719 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301720 if (AreAllExternalProvidersReady())
1721 OnAllExternalProvidersReady();
1722}
1723
1724bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271725 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141726 for (i = external_extension_providers_.begin();
1727 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301728 if (!i->get()->IsReady())
1729 return false;
[email protected]8e4560b62011-01-14 10:09:141730 }
[email protected]94fde232012-04-27 10:22:301731 return true;
[email protected]50067e52011-10-20 23:17:071732}
1733
1734void ExtensionService::OnAllExternalProvidersReady() {
1735 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301736 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1737 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071738
1739 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301740 if (update_once_all_providers_are_ready_ && updater()) {
1741 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061742 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141743 }
1744
1745 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231746 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141747 extension_prefs_->GetInstalledExtensionsInfo());
1748 for (size_t i = 0; i < extensions_info->size(); ++i) {
1749 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401750 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141751 CheckExternalUninstall(info->extension_id);
1752 }
[email protected]e5af875f2011-10-10 21:09:141753 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141754}
1755
1756void ExtensionService::IdentifyAlertableExtensions() {
1757 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1758
[email protected]07c9f2f42012-02-29 18:45:221759 // Build up the lists of extensions that require acknowledgment. If this is
1760 // the first time, grandfather extensions that would have caused
1761 // notification.
[email protected]89226982012-07-16 20:09:181762 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141763
[email protected]a9aa5932012-01-25 08:27:401764 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181765 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011766 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401767 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181768 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141769 } else {
1770 // First run. Just acknowledge all the extensions, silently, by
1771 // shortcutting the display of the UI and going straight to the
1772 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401773 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141774 }
1775 }
[email protected]a9aa5932012-01-25 08:27:401776
[email protected]612a1cb12012-10-17 13:18:031777 UpdateExternalExtensionAlert();
1778
[email protected]a9aa5932012-01-25 08:27:401779 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181780 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141781}
1782
[email protected]89226982012-07-16 20:09:181783bool ExtensionService::PopulateExtensionErrorUI(
1784 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221785 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281786
1787 // Extensions that are blacklisted.
1788 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1789 it != blacklisted_extensions_.end(); ++it) {
1790 std::string id = (*it)->id();
1791 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1792 extension_error_ui->AddBlacklistedExtension(id);
1793 needs_alert = true;
1794 }
1795 }
1796
[email protected]07c9f2f42012-02-29 18:45:221797 for (ExtensionSet::const_iterator iter = extensions_.begin();
1798 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361799 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281800
1801 // Extensions disabled by policy. Note: this no longer includes blacklisted
1802 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391803 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221804 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181805 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221806 needs_alert = true;
1807 }
1808 }
[email protected]07c9f2f42012-02-29 18:45:221809 }
[email protected]695b5712012-12-06 23:55:281810
[email protected]07c9f2f42012-02-29 18:45:221811 return needs_alert;
1812}
1813
[email protected]a9aa5932012-01-25 08:27:401814void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031815 const ExtensionIdSet* extension_ids =
1816 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141817 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1818 iter != extension_ids->end(); ++iter) {
1819 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1820 }
[email protected]879a6092013-03-06 04:12:501821 extension_error_ui_.reset();
1822}
1823
1824void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301825 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141826}
1827
[email protected]47fc70c2011-12-06 07:29:511828void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1829 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031830 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511831}
1832
[email protected]d96eb512012-11-01 23:44:081833bool ExtensionService::IsUnacknowledgedExternalExtension(
1834 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321835 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1836 return false;
1837
[email protected]1d5e58b2013-01-31 08:41:401838 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081839 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1840 !(extension_prefs_->GetDisableReasons(extension->id()) &
1841 Extension::DISABLE_SIDELOAD_WIPEOUT));
1842}
1843
[email protected]89226982012-07-16 20:09:181844void ExtensionService::HandleExtensionAlertDetails() {
1845 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301846 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1847 // causes a navigation.
1848 if (extension_error_ui_)
1849 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101850}
1851
[email protected]612a1cb12012-10-17 13:18:031852void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361853 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031854 return;
1855
1856 const Extension* extension = NULL;
1857 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1858 iter != disabled_extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361859 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081860 if (IsUnacknowledgedExternalExtension(e)) {
1861 extension = e;
1862 break;
[email protected]612a1cb12012-10-17 13:18:031863 }
1864 }
1865
1866 if (extension) {
[email protected]d96eb512012-11-01 23:44:081867 if (!extensions::HasExternalInstallError(this)) {
1868 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031869 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081870 // Stop prompting for this extension, and check if there's another
1871 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031872 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081873 UpdateExternalExtensionAlert();
1874 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1875 EXTERNAL_EXTENSION_IGNORED,
1876 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021877 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1878 UMA_HISTOGRAM_ENUMERATION(
1879 "Extensions.ExternalExtensionEventWebstore",
1880 EXTERNAL_EXTENSION_IGNORED,
1881 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1882 } else {
1883 UMA_HISTOGRAM_ENUMERATION(
1884 "Extensions.ExternalExtensionEventNonWebstore",
1885 EXTERNAL_EXTENSION_IGNORED,
1886 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1887 }
[email protected]d96eb512012-11-01 23:44:081888 return;
[email protected]612a1cb12012-10-17 13:18:031889 }
[email protected]b3aa7182013-04-25 04:45:231890 if (is_first_run_)
1891 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1892 // first_run is true if the extension was installed during a first run
1893 // (even if it's post-first run now).
1894 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1895 extension->id());
1896 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031897 }
1898 } else {
1899 extensions::RemoveExternalInstallError(this);
1900 }
[email protected]612a1cb12012-10-17 13:18:031901}
1902
[email protected]a9f39a312010-12-23 22:14:271903void ExtensionService::UnloadExtension(
1904 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591905 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091906 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281907 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021908 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281909 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251910
[email protected]fa0c96732010-11-17 00:14:231911 // This method can be called via PostTask, so the extension may have been
1912 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091913 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171914 // In case the extension may have crashed/uninstalled. Allow the profile to
1915 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081916 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231917 return;
[email protected]dd163fb02011-05-04 22:22:171918 }
[email protected]0c6da502009-08-14 22:32:391919
[email protected]20c8b942013-05-26 02:03:401920 // If uninstalling let RuntimeEventRouter know.
1921 if (reason == extension_misc::UNLOAD_REASON_UNINSTALL)
1922 extensions::RuntimeEventRouter::OnExtensionUninstalled(
1923 profile_, extension_id);
1924
[email protected]1eb175082010-02-10 09:26:161925 // Keep information about the extension so that we can reload it later
1926 // even if it's not permanently installed.
1927 unloaded_extension_paths_[extension->id()] = extension->path();
1928
[email protected]f17dbd42010-08-16 23:21:101929 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031930 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101931
[email protected]d7e9a862010-11-03 21:57:491932 // Clean up runtime data.
1933 extension_runtime_data_.erase(extension_id);
1934
[email protected]060d4972012-07-19 17:22:391935 if (disabled_extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461936 disabled_extensions_.Remove(extension->id());
[email protected]dd163fb02011-05-04 22:22:171937 // Make sure the profile cleans up its RequestContexts when an already
1938 // disabled extension is unloaded (since they are also tracking the disabled
1939 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081940 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]60a174a2013-08-02 20:29:281941 } else {
1942 // Remove the extension from our list.
1943 extensions_.Remove(extension->id());
1944 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391945 }
1946
[email protected]fb789532013-08-27 02:40:211947 content::NotificationService::current()->Notify(
1948 chrome::NOTIFICATION_EXTENSION_REMOVED,
1949 content::Source<Profile>(profile_),
1950 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251951}
1952
[email protected]8b1ec202013-09-05 02:09:501953void ExtensionService::RemoveComponentExtension(
1954 const std::string& extension_id) {
1955 scoped_refptr<const Extension> extension(
1956 GetExtensionById(extension_id, false));
1957 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
1958 content::NotificationService::current()->Notify(
1959 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1960 content::Source<Profile>(profile_),
1961 content::Details<const Extension>(extension.get()));
1962}
1963
[email protected]eaa7dd182010-12-14 11:09:001964void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401965 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171966
[email protected]84df8332011-12-06 18:22:461967 extensions_.Clear();
1968 disabled_extensions_.Clear();
1969 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491970 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321971
[email protected]c6e4a3412009-06-24 15:45:291972 // TODO(erikkay) should there be a notification for this? We can't use
1973 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1974 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321975}
1976
[email protected]eaa7dd182010-12-14 11:09:001977void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321978 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451979 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011980 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351981 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1982 // times.
[email protected]9f1087e2009-06-15 17:29:321983}
1984
[email protected]eaa7dd182010-12-14 11:09:001985void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:101986#if defined(OS_CHROMEOS)
1987 if (disable_garbage_collection_)
1988 return;
1989#endif
1990
[email protected]2f8757c32012-06-19 19:17:471991 if (extension_prefs_->pref_service()->ReadOnly())
1992 return;
1993
[email protected]9bd9a6862012-11-29 09:24:221994 if (pending_extension_manager()->HasPendingExtensions()) {
1995 // Don't garbage collect while there are pending installations, which may
1996 // be using the temporary installation directory. Try to garbage collect
1997 // again later.
[email protected]b3a25092013-05-28 22:08:161998 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:221999 FROM_HERE,
2000 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
2001 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
2002 return;
2003 }
2004
[email protected]45759612012-07-10 17:21:232005 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:472006 extension_prefs_->GetInstalledExtensionsInfo());
2007
[email protected]650b2d52013-02-10 03:41:452008 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:472009 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:052010 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2011 info->at(i)->extension_path));
2012
[email protected]399583b2012-12-11 09:33:422013 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:052014 for (size_t i = 0; i < info->size(); ++i)
2015 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2016 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:472017
[email protected]7f8f24f2012-11-15 19:40:142018 if (!GetFileTaskRunner()->PostTask(
2019 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:472020 base::Bind(
2021 &extension_file_util::GarbageCollectExtensions,
2022 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:402023 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:472024 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402025 }
[email protected]3cf4f0992009-02-03 23:00:302026}
2027
[email protected]fb82dcd2012-03-21 14:15:462028void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]6d147912013-07-09 08:02:142029 if (extensions::sync_helper::IsSyncableApp(&extension)) {
2030 if (app_sync_bundle_.IsSyncing())
2031 app_sync_bundle_.SyncChangeIfNeeded(extension);
2032 else if (is_ready() && !flare_.is_null())
2033 flare_.Run(syncer::APPS);
2034 } else if (extensions::sync_helper::IsSyncableExtension(&extension)) {
2035 if (extension_sync_bundle_.IsSyncing())
2036 extension_sync_bundle_.SyncChangeIfNeeded(extension);
2037 else if (is_ready() && !flare_.is_null())
2038 flare_.Run(syncer::EXTENSIONS);
[email protected]fb82dcd2012-03-21 14:15:462039 }
2040}
2041
[email protected]820d9bd2013-04-03 03:46:032042void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:352043 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:032044 content::NotificationService::current()->Notify(
2045 chrome::NOTIFICATION_EXTENSIONS_READY,
2046 content::Source<Profile>(profile_),
2047 content::NotificationService::NoDetails());
2048}
2049
[email protected]eaa7dd182010-12-14 11:09:002050void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:112051 if (updater_)
[email protected]93fd78f42009-07-10 16:43:172052 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502053
[email protected]bc151cf92013-02-12 04:57:262054 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:512055}
2056
[email protected]fcb58a862012-05-01 01:03:152057void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172058 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2059 // default extension state to DISABLED when the --disable-extensions flag
2060 // is set (http://crbug.com/29067).
2061 if (!extensions_enabled() &&
2062 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:402063 extension->location() != Manifest::COMPONENT &&
2064 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152065 return;
[email protected]330840c2012-08-29 22:21:012066 }
[email protected]c8d407e2011-04-28 21:27:172067
[email protected]b914e2952013-04-26 07:10:032068 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:172069 bool is_extension_installed = false;
2070 const Extension* old = GetInstalledExtension(extension->id());
2071 if (old) {
2072 is_extension_installed = true;
2073 int version_compare_result =
2074 extension->version()->CompareTo(*(old->version()));
2075 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:032076 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2077 // that we aren't downgrading.
2078 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:172079 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:032080 }
2081 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:172082
[email protected]1eb175082010-02-10 09:26:162083 // The extension is now loaded, remove its data from unloaded extension map.
2084 unloaded_extension_paths_.erase(extension->id());
2085
[email protected]bb7f40952011-01-13 00:21:202086 // If a terminated extension is loaded, remove it from the terminated list.
2087 UntrackTerminatedExtension(extension->id());
2088
[email protected]f17dbd42010-08-16 23:21:102089 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:412090 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:102091
[email protected]b914e2952013-04-26 07:10:032092 // Check if the extension's privileges have changed and mark the
2093 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:172094 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:032095
[email protected]116d40e2013-08-08 17:23:172096 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:412097 // To upgrade an extension in place, unload the old one and then load the
2098 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b914e2952013-04-26 07:10:032099 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_UPDATE);
2100 }
[email protected]0c6da502009-08-14 22:32:392101
[email protected]695b5712012-12-06 23:55:282102 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:092103 // Only prefs is checked for the blacklist. We rely on callers to check the
2104 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:072105 // installation then threads through the install and pending install flow
2106 // of this class, and we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:282107 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:412108 } else if (!reloading &&
2109 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482110 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302111 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032112 content::NotificationService::current()->Notify(
2113 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2114 content::Source<Profile>(profile_),
2115 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442116
[email protected]7e9e2422012-12-13 19:54:192117 // Show the extension disabled error if a permissions increase was the
2118 // only reason it was disabled.
2119 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032120 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442121 extensions::AddExtensionDisabledError(this, extension);
2122 }
[email protected]ca2e82022013-04-29 16:48:412123 } else if (reloading) {
2124 // Replace the old extension with the new version.
2125 CHECK(!disabled_extensions_.Insert(extension));
2126 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:282127 } else {
2128 // All apps that are displayed in the launcher are ordered by their ordinals
2129 // so we must ensure they have valid ordinals.
2130 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252131 if (!extension->ShouldDisplayInNewTabPage()) {
2132 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2133 extension->id());
2134 }
[email protected]695b5712012-12-06 23:55:282135 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2136 extension->id(), syncer::StringOrdinal());
2137 }
2138
2139 extensions_.Insert(extension);
2140 SyncExtensionChangeIfNeeded(*extension);
2141 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:312142 }
[email protected]b914e2952013-04-26 07:10:032143 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:352144}
2145
[email protected]8c484b742012-11-29 06:05:362146void ExtensionService::AddComponentExtension(const Extension* extension) {
2147 const std::string old_version_string(
2148 extension_prefs_->GetVersionString(extension->id()));
2149 const Version old_version(old_version_string);
2150
2151 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2152 VLOG(1) << "Component extension " << extension->name() << " ("
2153 << extension->id() << ") installing/upgrading from '"
2154 << old_version_string << "' to " << extension->version()->GetString();
2155
2156 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422157 Extension::ENABLED_COMPONENT,
[email protected]9f3c8532013-07-31 19:52:072158 extensions::Blacklist::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:422159 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362160 return;
2161 }
2162
2163 AddExtension(extension);
2164}
2165
[email protected]b914e2952013-04-26 07:10:032166void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:312167 // If the extension has used the optional permissions API, it will have a
2168 // custom set of active permissions defined in the extension prefs. Here,
2169 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062170 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312171 extension_prefs()->GetActivePermissions(extension->id());
2172
[email protected]dc24976f2013-06-02 21:15:092173 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:312174 // We restrict the active permissions to be within the bounds defined in the
2175 // extension's manifest.
2176 // a) active permissions must be a subset of optional + default permissions
2177 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062178 scoped_refptr<PermissionSet> total_permissions =
2179 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062180 extensions::PermissionsData::GetRequiredPermissions(extension),
2181 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:312182
2183 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062184 scoped_refptr<PermissionSet> adjusted_active =
2185 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312186 total_permissions.get(), active_permissions.get());
2187
2188 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062189 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062190 extensions::PermissionsData::GetRequiredPermissions(extension),
2191 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312192
[email protected]c333e792012-01-06 16:57:392193 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:092194 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312195 }
[email protected]b914e2952013-04-26 07:10:032196}
2197
2198void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:172199 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:032200 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:312201
[email protected]8d888c12010-11-30 00:00:252202 // We keep track of all permissions the user has granted each extension.
2203 // This allows extensions to gracefully support backwards compatibility
2204 // by including unknown permissions in their manifests. When the user
2205 // installs the extension, only the recognized permissions are recorded.
2206 // When the unknown permissions become recognized (e.g., through browser
2207 // upgrade), we can prompt the user to accept these new permissions.
2208 // Extensions can also silently upgrade to less permissions, and then
2209 // silently upgrade to a version that adds these permissions back.
2210 //
2211 // For example, pretend that Chrome 10 includes a permission "omnibox"
2212 // for an API that adds suggestions to the omnibox. An extension can
2213 // maintain backwards compatibility while still having "omnibox" in the
2214 // manifest. If a user installs the extension on Chrome 9, the browser
2215 // will record the permissions it recognized, not including "omnibox."
2216 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2217 // will disable the extension and prompt the user to approve the increase
2218 // in privileges. The extension could then release a new version that
2219 // removes the "omnibox" permission. When the user upgrades, Chrome will
2220 // still remember that "omnibox" had been granted, so that if the
2221 // extension once again includes "omnibox" in an upgrade, the extension
2222 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:282223 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252224
[email protected]211a3f32013-05-28 21:48:182225 bool auto_grant_permission =
[email protected]116d40e2013-08-08 17:23:172226 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]211a3f32013-05-28 21:48:182227 chrome::IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:032228 // Silently grant all active permissions to default apps only on install.
2229 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:182230 // Silently grant all active permissions to apps install in kiosk mode on both
2231 // install and update.
2232 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:032233 GrantPermissions(extension);
2234
2235 bool is_privilege_increase = false;
2236 // We only need to compare the granted permissions to the current permissions
2237 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:052238 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:182239 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:252240 // Add all the recognized permissions if the granted permissions list
2241 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062242 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312243 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522244 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252245
2246 // Here, we check if an extension's privileges have increased in a manner
2247 // that requires the user's approval. This could occur because the browser
2248 // upgraded and recognized additional privileges, or an extension upgrades
2249 // to a version that requires additional privileges.
[email protected]cadac622013-06-11 16:46:362250 is_privilege_increase = granted_permissions->HasLessPrivilegesThan(
[email protected]a0a73052013-07-10 01:42:432251 extension->GetActivePermissions().get(), extension->GetType());
[email protected]8d888c12010-11-30 00:00:252252 }
2253
[email protected]116d40e2013-08-08 17:23:172254 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:032255 // If the extension was already disabled, suppress any alerts for becoming
2256 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:032257 bool previously_disabled =
2258 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:192259 // Legacy disabled extensions do not have a disable reason. Infer that if
2260 // there was no permission increase, it was likely disabled by the user.
2261 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:032262 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:192263 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032264 }
[email protected]7e9e2422012-12-13 19:54:192265 // Extensions that came to us disabled from sync need a similar inference,
2266 // except based on the new version's permissions.
2267 if (previously_disabled &&
2268 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2269 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2270 extension_prefs_->ClearDisableReasons(extension->id());
2271 if (!is_privilege_increase)
2272 disable_reasons |= Extension::DISABLE_USER_ACTION;
2273 }
[email protected]b914e2952013-04-26 07:10:032274 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:252275 }
2276
2277 // Extension has changed permissions significantly. Disable it. A
2278 // notification should be sent by the caller.
2279 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192280 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492281 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2282 RecordPermissionMessagesHistogram(
2283 extension, "Extensions.Permissions_AutoDisable");
2284 }
[email protected]b3317ad2011-04-28 23:46:002285 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252286 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:032287 }
2288 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:282289 extension_prefs_->AddDisableReason(
2290 extension->id(),
2291 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252292 }
2293}
2294
[email protected]eaa7dd182010-12-14 11:09:002295void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392296 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462297 for (ExtensionSet::const_iterator iter = extensions_.begin();
2298 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:362299 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:402300 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462301 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352302 }
2303
[email protected]f6431be82013-09-07 02:53:452304 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252305}
2306
[email protected]9f4e4f082013-06-21 07:11:192307ExtensionService::ImportStatus ExtensionService::SatisfyImports(
2308 const Extension* extension) {
2309 ImportStatus status = IMPORT_STATUS_OK;
2310 std::vector<std::string> pending;
2311 // TODO(elijahtaylor): Message the user if there is a failure that is
2312 // unrecoverable.
2313 if (SharedModuleInfo::ImportsModules(extension)) {
2314 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2315 SharedModuleInfo::GetImports(extension);
2316 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2317 for (i = imports.begin(); i != imports.end(); ++i) {
2318 Version version_required(i->minimum_version);
2319 const Extension* imported_module =
2320 GetExtensionById(i->extension_id, true);
2321 if (!imported_module) {
2322 if (extension->from_webstore()) {
2323 status = IMPORT_STATUS_UNSATISFIED;
2324 pending.push_back(i->extension_id);
2325 } else {
2326 return IMPORT_STATUS_UNRECOVERABLE;
2327 }
2328 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
2329 return IMPORT_STATUS_UNRECOVERABLE;
2330 } else if (version_required.IsValid() &&
2331 imported_module->version()->CompareTo(version_required) < 0) {
2332 if (imported_module->from_webstore()) {
2333 status = IMPORT_STATUS_UNSATISFIED;
2334 } else {
2335 return IMPORT_STATUS_UNRECOVERABLE;
2336 }
2337 }
2338 }
2339 }
2340 if (status == IMPORT_STATUS_UNSATISFIED) {
2341 for (std::vector<std::string>::const_iterator iter = pending.begin();
2342 iter != pending.end();
2343 ++iter) {
2344 pending_extension_manager()->AddFromExtensionImport(
2345 *iter,
2346 extension_urls::GetWebstoreUpdateUrl(),
2347 IsSharedModule);
2348 }
2349 CheckForUpdatesSoon();
2350 }
2351 return status;
2352}
2353
2354scoped_ptr<const ExtensionSet>
2355 ExtensionService::GetDependentExtensions(const Extension* extension) {
2356 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2357 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2358 if (SharedModuleInfo::IsSharedModule(extension)) {
2359 set_to_check->InsertAll(disabled_extensions_);
2360 set_to_check->InsertAll(delayed_installs_);
2361 set_to_check->InsertAll(extensions_);
2362 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2363 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112364 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2365 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192366 dependents->Insert(*iter);
2367 }
2368 }
2369 }
2370 return dependents.PassAs<const ExtensionSet>();
2371}
2372
2373void ExtensionService::PruneSharedModulesOnUninstall(
2374 const Extension* extension) {
2375 if (SharedModuleInfo::ImportsModules(extension)) {
2376 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2377 SharedModuleInfo::GetImports(extension);
2378 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2379 for (i = imports.begin(); i != imports.end(); ++i) {
2380 const Extension* imported_module =
2381 GetExtensionById(i->extension_id, true);
2382 if (imported_module && imported_module->from_webstore()) {
2383 scoped_ptr<const ExtensionSet> dependents =
2384 GetDependentExtensions(imported_module);
2385 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212386 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192387 }
2388 }
2389 }
2390 }
2391}
2392
[email protected]8266d662011-07-12 21:53:262393void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502394 const Extension* extension,
[email protected]98270432012-09-11 20:51:242395 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052396 bool has_requirement_errors,
[email protected]9f3c8532013-07-31 19:52:072397 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052398 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122399 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202400
[email protected]c3cfb012011-04-06 22:07:352401 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032402 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412403 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062404 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402405 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062406 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322407
[email protected]31bb5ee62012-09-12 22:58:402408 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2409 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222410 << " and update URL "
2411 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402412 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542413
[email protected]4416c5a2010-06-26 01:28:572414 // Delete the extension directory since we're not going to
2415 // load it.
[email protected]7f8f24f2012-11-15 19:40:142416 if (!GetFileTaskRunner()->PostTask(
2417 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362418 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402419 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362420 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402421 }
[email protected]4416c5a2010-06-26 01:28:572422 return;
2423 }
[email protected]51a3bf8b2012-06-08 22:53:062424
2425 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572426 } else {
[email protected]c3cfb012011-04-06 22:07:352427 // We explicitly want to re-enable an uninstalled external
2428 // extension; if we're here, that means the user is manually
2429 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332430 if (IsExternalExtensionUninstalled(id)) {
2431 initial_enable = true;
2432 }
[email protected]aa142702010-03-26 01:26:332433 }
2434
[email protected]98270432012-09-11 20:51:242435 // Unsupported requirements overrides the management policy.
2436 if (has_requirement_errors) {
2437 initial_enable = false;
2438 extension_prefs_->AddDisableReason(
2439 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2440 // If the extension was disabled because of unsupported requirements but
2441 // now supports all requirements after an update and there are not other
2442 // disable reasons, enable it.
2443 } else if (extension_prefs_->GetDisableReasons(id) ==
2444 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2445 initial_enable = true;
2446 extension_prefs_->ClearDisableReasons(id);
2447 }
2448
[email protected]9f3c8532013-07-31 19:52:072449 if (blacklist_state == extensions::Blacklist::BLACKLISTED) {
2450 // Installation of a blacklisted extension can happen from sync, policy,
2451 // etc, where to maintain consistency we need to install it, just never
2452 // load it (see AddExtension). Usually it should be the job of callers to
2453 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2454 // showing the install dialogue).
2455 extension_prefs()->AcknowledgeBlacklistedExtension(id);
2456 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2457 extension->location(),
2458 Manifest::NUM_LOCATIONS);
2459 }
2460
[email protected]695b5712012-12-06 23:55:282461 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552462 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2463 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302464 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402465 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552466 RecordPermissionMessagesHistogram(
2467 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302468 } else {
2469 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2470 extension->GetType(), 100);
2471 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402472 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552473 }
2474
[email protected]a39921b42012-02-28 03:42:542475 // Certain extension locations are specific enough that we can
2476 // auto-acknowledge any extension that came from one of them.
[email protected]1d5e58b2013-01-31 08:41:402477 if (extension->location() == Manifest::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542478 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422479 const Extension::State initial_state =
2480 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222481 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072482 extension_prefs_->SetDelayedInstallInfo(
2483 extension,
2484 initial_state,
2485 blacklist_state,
2486 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2487 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052488
2489 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192490 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052491
2492 // Notify extension of available update.
2493 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2494 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452495
2496 // Notify observers that app update is available.
2497 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2498 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052499 return;
2500 }
2501
[email protected]9f4e4f082013-06-21 07:11:192502 ImportStatus status = SatisfyImports(extension);
2503 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072504 extension_prefs_->SetDelayedInstallInfo(
2505 extension,
2506 initial_state,
2507 blacklist_state,
2508 extensions::ExtensionPrefs::DELAY_REASON_GC,
2509 page_ordinal);
[email protected]399583b2012-12-11 09:33:422510 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192511 } else if (status != IMPORT_STATUS_OK) {
2512 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072513 extension_prefs_->SetDelayedInstallInfo(
2514 extension,
2515 initial_state,
2516 blacklist_state,
[email protected]9f4e4f082013-06-21 07:11:192517 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2518 page_ordinal);
2519 delayed_installs_.Insert(extension);
2520 }
[email protected]399583b2012-12-11 09:33:422521 } else {
[email protected]9f3c8532013-07-31 19:52:072522 AddNewOrUpdatedExtension(extension,
2523 initial_state,
2524 blacklist_state,
2525 page_ordinal);
[email protected]399583b2012-12-11 09:33:422526 }
[email protected]8c484b742012-11-29 06:05:362527}
2528
2529void ExtensionService::AddNewOrUpdatedExtension(
2530 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422531 Extension::State initial_state,
[email protected]9f3c8532013-07-31 19:52:072532 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422533 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362534 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9f3c8532013-07-31 19:52:072535 extension_prefs_->OnExtensionInstalled(extension,
2536 initial_state,
2537 blacklist_state,
2538 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122539 delayed_installs_.Remove(extension->id());
[email protected]6f6101832012-11-27 22:10:482540 FinishInstallation(extension);
2541}
2542
2543void ExtensionService::MaybeFinishDelayedInstallation(
2544 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192545 // Check if the extension already got installed.
2546 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482547 return;
[email protected]9f4e4f082013-06-21 07:11:192548 extensions::ExtensionPrefs::DelayReason reason =
2549 extension_prefs_->GetDelayedInstallReason(extension_id);
2550
2551 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2552 // preferences files that will not have set this field but it was previously
2553 // only used for idle updates.
2554 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2555 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
2556 is_ready() && !IsExtensionIdle(extension_id))
[email protected]6f6101832012-11-27 22:10:482557 return;
2558
[email protected]9f4e4f082013-06-21 07:11:192559 const Extension* extension = delayed_installs_.GetByID(extension_id);
2560 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2561 ImportStatus status = SatisfyImports(extension);
2562 if (status != IMPORT_STATUS_OK) {
2563 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2564 delayed_installs_.Remove(extension_id);
2565 // Make sure no version of the extension is actually installed, (i.e.,
2566 // that this delayed install was not an update).
2567 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2568 extension_prefs_->DeleteExtensionPrefs(extension_id);
2569 }
2570 return;
2571 }
2572 }
2573
[email protected]6f6101832012-11-27 22:10:482574 FinishDelayedInstallation(extension_id);
2575}
2576
2577void ExtensionService::FinishDelayedInstallation(
2578 const std::string& extension_id) {
2579 scoped_refptr<const Extension> extension(
2580 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092581 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192582 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482583
[email protected]399583b2012-12-11 09:33:422584 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482585 NOTREACHED();
2586
[email protected]dc24976f2013-06-02 21:15:092587 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482588}
2589
2590void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562591 const extensions::Extension* existing_extension =
2592 GetInstalledExtension(extension->id());
2593 bool is_update = false;
2594 std::string old_name;
2595 if (existing_extension) {
2596 is_update = true;
2597 old_name = existing_extension->name();
2598 }
2599 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152600 content::NotificationService::current()->Notify(
2601 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2602 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022603 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152604
[email protected]d96eb512012-11-01 23:44:082605 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362606
[email protected]399583b2012-12-11 09:33:422607 // Unpacked extensions default to allowing file access, but if that has been
2608 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402609 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422610 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2611 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2612 }
2613
[email protected]6f6101832012-11-27 22:10:482614 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032615
2616 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082617 // so he can reenable it. We do this last so that it has already been
2618 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162619 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032620 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082621 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2622 EXTERNAL_EXTENSION_INSTALLED,
2623 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022624 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2625 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2626 EXTERNAL_EXTENSION_INSTALLED,
2627 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2628 } else {
2629 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2630 EXTERNAL_EXTENSION_INSTALLED,
2631 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2632 }
[email protected]d96eb512012-11-01 23:44:082633 }
[email protected]9f4e4f082013-06-21 07:11:192634
2635 // Check extensions that may have been delayed only because this shared module
2636 // was not available.
2637 if (SharedModuleInfo::IsSharedModule(extension)) {
2638 MaybeFinishDelayedInstallations();
2639 }
[email protected]4a190632009-05-09 01:07:422640}
2641
[email protected]76b65442012-11-17 14:11:482642const Extension* ExtensionService::GetPendingExtensionUpdate(
2643 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192644 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482645}
2646
[email protected]bb7f40952011-01-13 00:21:202647void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462648 if (!terminated_extensions_.Contains(extension->id()))
2649 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202650
[email protected]b3f7fe22011-11-11 19:27:562651 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202652}
2653
2654void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202655 std::string lowercase_id = StringToLowerASCII(id);
[email protected]60a174a2013-08-02 20:29:282656 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
[email protected]84df8332011-12-06 18:22:462657 terminated_extensions_.Remove(lowercase_id);
[email protected]fb789532013-08-27 02:40:212658 if (extension) {
2659 content::NotificationService::current()->Notify(
2660 chrome::NOTIFICATION_EXTENSION_REMOVED,
2661 content::Source<Profile>(profile_),
2662 content::Details<const Extension>(extension));
2663 }
[email protected]bb7f40952011-01-13 00:21:202664}
2665
[email protected]0dfe05c2011-02-23 23:03:362666const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472667 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282668 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472669}
2670
2671const Extension* ExtensionService::GetInstalledExtension(
2672 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282673 int include_mask = INCLUDE_ENABLED |
2674 INCLUDE_DISABLED |
2675 INCLUDE_TERMINATED |
2676 INCLUDE_BLACKLISTED;
2677 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362678}
2679
[email protected]eaa7dd182010-12-14 11:09:002680bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442681 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]be9915fb2013-07-18 09:28:552682 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442683 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402684 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392685}
2686
[email protected]4d007b312012-10-17 03:00:482687bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]be9915fb2013-07-18 09:28:552688 const Extension* extension = extensions_.GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482689 if (extension && extension->is_platform_app()) {
2690 *url = GURL(chrome::kExtensionInvalidRequestURL);
2691 return true;
2692 }
2693
2694 return false;
2695}
2696
[email protected]9060d8b02012-01-13 02:14:302697bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322698 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142699 const Version* version,
[email protected]650b2d52013-02-10 03:41:452700 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402701 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512702 int creation_flags,
2703 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382704 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2705 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012706 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302707 return false;
[email protected]a8af9fdb2010-10-28 21:52:202708
[email protected]7577a5c52009-07-30 06:21:582709 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492710 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582711 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022712 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352713
[email protected]7577a5c52009-07-30 06:21:582714 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352715 // The default apps will have the location set as INTERNAL. Since older
2716 // default apps are installed as EXTERNAL, we override them. However, if the
2717 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402718 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352719 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402720 (location == Manifest::INTERNAL &&
2721 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352722
2723 if (!is_default_apps_migration) {
2724 DCHECK(version);
2725
2726 switch (existing->version()->CompareTo(*version)) {
2727 case -1: // existing version is older, we should upgrade
2728 break;
2729 case 0: // existing version is same, do nothing
2730 return false;
2731 case 1: // existing version is newer, uh-oh
2732 LOG(WARNING) << "Found external version of extension " << id
2733 << "that is older than current version. Current version "
2734 << "is: " << existing->VersionString() << ". New "
2735 << "version is: " << version->GetString()
2736 << ". Keeping current version.";
2737 return false;
2738 }
[email protected]7577a5c52009-07-30 06:21:582739 }
2740 }
2741
[email protected]9060d8b02012-01-13 02:14:302742 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302743 if (!pending_extension_manager()->AddFromExternalFile(
2744 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302745 return false;
[email protected]e3987852012-05-04 10:06:302746 }
[email protected]9c635f22010-12-02 09:36:362747
[email protected]14908b72011-04-20 06:54:362748 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372749 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162750 installer->set_install_source(location);
2751 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072752 installer->set_expected_version(*version);
2753 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312754 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182755#if defined(OS_CHROMEOS)
2756 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2757#else
[email protected]6dfbbf82010-03-12 23:09:162758 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182759#endif
[email protected]47fc70c2011-12-06 07:29:512760
2761 // Depending on the source, a new external extension might not need a user
2762 // notification on installation. For such extensions, mark them acknowledged
2763 // now to suppress the notification.
2764 if (mark_acknowledged)
2765 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302766
2767 return true;
[email protected]7577a5c52009-07-30 06:21:582768}
2769
[email protected]eaa7dd182010-12-14 11:09:002770void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452771 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402772 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402773 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012774 content::NotificationService::current()->Notify(
2775 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2776 content::Source<Profile>(profile_),
2777 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402778
[email protected]8a205c02011-02-04 20:41:332779 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212780 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042781 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492782 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402783 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2784}
2785
[email protected]eaa7dd182010-12-14 11:09:002786void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132787 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182788 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332789 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182790 if (iter == orphaned_dev_tools_.end())
2791 return;
2792
[email protected]04ea1bb2013-07-10 09:26:092793 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182794 orphaned_dev_tools_.erase(iter);
2795}
2796
[email protected]432115822011-07-10 15:52:272797void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532798 const content::NotificationSource& source,
2799 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272800 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542801 case chrome::NOTIFICATION_APP_TERMINATING:
2802 // Shutdown has started. Don't start any more extension installs.
2803 // (We cannot use ExtensionService::Shutdown() for this because it
2804 // happens too late in browser teardown.)
2805 browser_terminating_ = true;
2806 break;
[email protected]432115822011-07-10 15:52:272807 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532808 if (profile_ !=
2809 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322810 break;
[email protected]6c2381d2011-10-19 02:52:532811 }
[email protected]a4ed6282009-12-14 20:51:162812
[email protected]3a1dc572012-07-31 22:25:132813 extensions::ExtensionHost* host =
2814 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262815
[email protected]fa2416f2011-05-03 08:41:202816 // Mark the extension as terminated and Unload it. We want it to
2817 // be in a consistent state: either fully working or not loaded
2818 // at all, but never half-crashed. We do it in a PostTask so
2819 // that other handlers of this notification will still have
2820 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162821 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362822 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362823 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202824 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012825 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202826 host->extension()));
[email protected]31f77262009-12-02 20:48:532827 break;
2828 }
[email protected]432115822011-07-10 15:52:272829 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302830 content::RenderProcessHost* process =
2831 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192832 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302833 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192834 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2835 break;
2836
[email protected]15397b42012-05-16 23:56:062837 // Extensions need to know the channel for API restrictions.
2838 process->Send(new ExtensionMsg_SetChannel(
[email protected]15d4d2d2013-08-09 06:49:032839 extensions::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062840
[email protected]4941fe92013-06-13 23:21:012841 // Platform apps need to know the system font.
2842 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2843 webui::SetFontAndTextDirection(fonts.get());
2844 std::string font_family, font_size;
2845 fonts->GetString("fontfamily", &font_family);
2846 fonts->GetString("fontsize", &font_size);
2847 process->Send(new ExtensionMsg_SetSystemFont(
2848 font_family, font_size));
2849
[email protected]77a6970c2011-04-23 16:58:562850 // Valid extension function names, used to setup bindings in renderer.
2851 std::vector<std::string> function_names;
2852 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2853 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532854
[email protected]77a6970c2011-04-23 16:58:562855 // Scripting whitelist. This is modified by tests and must be communicated
2856 // to renderers.
2857 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2858 *Extension::GetScriptingWhitelist()));
2859
2860 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532861 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462862 for (ExtensionSet::const_iterator iter = extensions_.begin();
2863 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492864 // Renderers don't need to know about themes.
2865 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362866 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562867 }
[email protected]9776e82e2011-11-15 02:17:532868 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562869 break;
2870 }
[email protected]432115822011-07-10 15:52:272871 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302872 content::RenderProcessHost* process =
2873 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192874 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302875 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192876 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2877 break;
2878
[email protected]6bc04fd82011-12-04 02:29:352879 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462880 BrowserThread::PostTask(
2881 BrowserThread::IO, FROM_HERE,
2882 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082883 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302884 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212885 break;
2886 }
[email protected]0db124b02012-11-07 04:55:052887 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2888 extensions::ExtensionHost* host =
2889 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222890 std::string extension_id = host->extension_id();
[email protected]9f4e4f082013-06-21 07:11:192891 if (delayed_installs_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222892 // We were waiting for this extension to become idle, it now might have,
2893 // so maybe finish installation.
[email protected]b3a25092013-05-28 22:08:162894 base::MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052895 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482896 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052897 AsWeakPtr(), extension_id),
2898 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052899 }
2900 break;
2901 }
[email protected]92dd8d92013-02-26 00:41:082902 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2903 // Notify extensions that chrome update is available.
2904 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2905 profile_);
[email protected]75bdcb872013-03-13 00:41:452906
2907 // Notify observers that chrome update is available.
2908 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2909 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082910 break;
2911 }
[email protected]aa96d3a2010-08-21 08:45:252912
[email protected]4814b512009-11-07 00:12:292913 default:
2914 NOTREACHED() << "Unexpected notification type.";
2915 }
2916}
2917
[email protected]90bb38d2012-11-14 18:36:032918void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182919 IdentifyAlertableExtensions();
2920 CheckManagementPolicy();
2921}
2922
[email protected]eaa7dd182010-12-14 11:09:002923bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182924 return !GetAppIds().empty();
2925}
[email protected]377011d2010-07-20 04:18:502926
[email protected]eaa7dd182010-12-14 11:09:002927ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182928 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462929 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502930 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402931 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182932 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502933 }
2934
[email protected]ec5b50d2010-10-09 16:35:182935 return result;
[email protected]377011d2010-07-20 04:18:502936}
[email protected]d7e9a862010-11-03 21:57:492937
[email protected]dc9a74f72012-08-17 18:07:212938bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292939 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212940 return true;
2941 ExtensionRuntimeDataMap::const_iterator it =
2942 extension_runtime_data_.find(extension->id());
2943 return it == extension_runtime_data_.end() ? false :
2944 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492945}
2946
[email protected]eaa7dd182010-12-14 11:09:002947void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292948 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492949 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072950 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272951 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532952 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072953 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492954}
2955
[email protected]dc9a74f72012-08-17 18:07:212956bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2957 ExtensionRuntimeDataMap::const_iterator it =
2958 extension_runtime_data_.find(extension->id());
2959 return it == extension_runtime_data_.end() ? false :
2960 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492961}
2962
[email protected]eaa7dd182010-12-14 11:09:002963void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032964 bool value) {
[email protected]d7e9a862010-11-03 21:57:492965 extension_runtime_data_[extension->id()].being_upgraded = value;
2966}
[email protected]1bead0712010-11-27 17:41:532967
[email protected]e178ad92013-06-28 02:29:252968bool ExtensionService::IsBeingReloaded(
2969 const std::string& extension_id) const {
2970 return ContainsKey(extensions_being_reloaded_, extension_id);
2971}
2972
2973void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432974 bool isBeingReloaded) {
2975 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252976 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432977 else
[email protected]e178ad92013-06-28 02:29:252978 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252979}
2980
[email protected]dc9a74f72012-08-17 18:07:212981bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2982 ExtensionRuntimeDataMap::const_iterator it =
2983 extension_runtime_data_.find(extension->id());
2984 return it == extension_runtime_data_.end() ? false :
2985 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072986}
2987
2988void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2989 bool value) {
2990 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2991}
2992
[email protected]612a1cb12012-10-17 13:18:032993bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2994 // Extensions installed by policy can't be disabled. So even if a previous
2995 // installation disabled the extension, make sure it is now enabled.
2996 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2997 return true;
2998
2999 if (extension_prefs_->IsExtensionDisabled(extension->id()))
3000 return false;
3001
[email protected]41070e8d2012-10-24 01:34:363002 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:033003 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:543004 // enabling them. Hosted apps are excepted because they are not dangerous
3005 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:403006 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
3007 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:033008 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
3009 return false;
3010 }
3011 }
[email protected]612a1cb12012-10-17 13:18:033012
3013 return true;
3014}
[email protected]0db124b02012-11-07 04:55:053015
3016bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:473017 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:333018 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:053019 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:473020 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223021 if (host)
3022 return false;
3023 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
3024}
3025
3026bool ExtensionService::ShouldDelayExtensionUpdate(
3027 const std::string& extension_id,
3028 bool wait_for_idle) const {
3029 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
3030
3031 // If delayed updates are globally disabled, or just for this extension,
3032 // don't delay.
3033 if (!install_updates_when_idle_ || !wait_for_idle)
3034 return false;
3035
[email protected]695b5712012-12-06 23:55:283036 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223037 // If there is no old extension, this is not an update, so don't delay.
3038 if (!old)
3039 return false;
3040
[email protected]9367eabc2013-03-01 01:29:293041 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:223042 // Delay installation if the extension listens for the onUpdateAvailable
3043 // event.
3044 return system_->event_router()->ExtensionHasEventListener(
3045 extension_id, kOnUpdateAvailableEvent);
3046 } else {
3047 // Delay installation if the extension is not idle.
3048 return !IsExtensionIdle(extension_id);
3049 }
[email protected]0db124b02012-11-07 04:55:053050}
[email protected]fdd679b2012-11-15 20:49:393051
[email protected]399583b2012-12-11 09:33:423052void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:453053 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
3054 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:423055 for (ExtensionSet::const_iterator it = extensions_.begin();
3056 it != extensions_.end(); ++it) {
[email protected]cadac622013-06-11 16:46:363057 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
3058 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
3059 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:423060 }
3061 }
3062
[email protected]9f4e4f082013-06-21 07:11:193063 DCHECK(!installs_delayed_for_gc());
3064 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:423065 BrowserContext::GarbageCollectStoragePartitions(
3066 profile_, active_paths.Pass(),
3067 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3068 AsWeakPtr()));
3069}
3070
3071void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:193072 set_installs_delayed_for_gc(false);
3073 MaybeFinishDelayedInstallations();
3074}
3075
3076void ExtensionService::MaybeFinishDelayedInstallations() {
3077 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:423078 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3079 it != delayed_installs_.end();
3080 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193081 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:423082 }
[email protected]9f4e4f082013-06-21 07:11:193083 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
3084 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:423085 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193086 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:423087 }
[email protected]399583b2012-12-11 09:33:423088}
3089
3090void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3091 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3092}
3093
[email protected]fdd679b2012-11-15 20:49:393094void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283095 blacklist_->GetBlacklistedIDs(
3096 GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:253097 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:283098}
3099
[email protected]3f2a2fa2013-09-24 02:55:253100void ExtensionService::ManageBlacklist(const std::set<std::string>& updated) {
[email protected]695b5712012-12-06 23:55:283101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3102
[email protected]3f2a2fa2013-09-24 02:55:253103 std::set<std::string> before = blacklisted_extensions_.GetIDs();
[email protected]16bf7ba72013-08-23 11:52:543104 std::set<std::string> no_longer_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:253105 base::STLSetDifference<std::set<std::string> >(before, updated);
[email protected]16bf7ba72013-08-23 11:52:543106 std::set<std::string> not_yet_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:253107 base::STLSetDifference<std::set<std::string> >(updated, before);
[email protected]695b5712012-12-06 23:55:283108
3109 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3110 it != no_longer_blacklisted.end(); ++it) {
3111 scoped_refptr<const Extension> extension =
3112 blacklisted_extensions_.GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:253113 if (!extension.get()) {
3114 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
3115 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:283116 continue;
[email protected]3f2a2fa2013-09-24 02:55:253117 }
[email protected]695b5712012-12-06 23:55:283118 blacklisted_extensions_.Remove(*it);
[email protected]3f2a2fa2013-09-24 02:55:253119 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:093120 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:093121 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:093122 extension->location(),
3123 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283124 }
3125
3126 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3127 it != not_yet_blacklisted.end(); ++it) {
3128 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:253129 if (!extension.get()) {
3130 NOTREACHED() << "Extension " << *it << " needs to be "
3131 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:283132 continue;
[email protected]3f2a2fa2013-09-24 02:55:253133 }
[email protected]695b5712012-12-06 23:55:283134 blacklisted_extensions_.Insert(extension);
[email protected]3f2a2fa2013-09-24 02:55:253135 extension_prefs_->SetExtensionBlacklisted(extension->id(), true);
[email protected]695b5712012-12-06 23:55:283136 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:093137 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
3138 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283139 }
3140
3141 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393142}
[email protected]75bdcb872013-03-13 00:41:453143
3144void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
3145 update_observers_.AddObserver(observer);
3146}
3147
3148void ExtensionService::RemoveUpdateObserver(
3149 extensions::UpdateObserver* observer) {
3150 update_observers_.RemoveObserver(observer);
3151}