blob: c6dd2e09d8eca4bbaf17fe979e09ba41fef8c69e [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]895a1e52012-05-15 02:50:1275#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5676#include "chrome/common/extensions/extension_messages.h"
[email protected]612a1cb12012-10-17 13:18:0377#include "chrome/common/extensions/feature_switch.h"
[email protected]15d4d2d2013-08-09 06:49:0378#include "chrome/common/extensions/features/feature_channel.h"
[email protected]06492ed2013-03-24 22:13:1479#include "chrome/common/extensions/incognito_handler.h"
[email protected]561e33d52013-04-03 06:58:4380#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]6b414c232013-06-05 07:53:3481#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]9f4e4f082013-06-21 07:11:1982#include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
[email protected]65348062013-01-15 07:27:2283#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]2cb5e302013-05-09 05:00:0684#include "chrome/common/extensions/permissions/permissions_data.h"
[email protected]8f3bcbd2013-06-05 08:42:4085#include "chrome/common/extensions/sync_helper.h"
[email protected]25b34332009-06-05 21:53:1986#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1487#include "chrome/common/url_constants.h"
[email protected]7a83dd072013-09-03 12:09:1788#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]c38831a12011-10-28 12:44:4989#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3690#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3791#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1692#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3093#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4294#include "content/public/browser/site_instance.h"
95#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:5396#include "content/public/browser/url_data_source.h"
[email protected]836e2982013-05-16 08:07:4297#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:3198#include "extensions/common/error_utils.h"
[email protected]d42c11152013-08-22 19:36:3299#include "extensions/common/manifest.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]c6d474f82009-12-16 21:11:06130namespace errors = extension_manifest_errors;
131
[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]fdd679b2012-11-15 20:49:39417 blacklist,
[email protected]42a08162012-03-16 18:09:11418 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17419 }
420
[email protected]25ae0152011-11-18 14:40:02421 component_loader_.reset(
422 new extensions::ComponentLoader(this,
423 profile->GetPrefs(),
424 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14425
[email protected]0436b102011-04-15 18:30:03426 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25427 extensions::ExternalProviderImpl::CreateExternalProviders(
428 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05429 }
[email protected]b671760b2010-07-15 21:13:47430
[email protected]fb82dcd2012-03-21 14:15:46431 // Set this as the ExtensionService for extension sorting to ensure it
432 // cause syncs if required.
433 extension_prefs_->extension_sorting()->SetExtensionService(this);
434
[email protected]b3aa7182013-04-25 04:45:23435 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
436
[email protected]9ce11d22012-08-08 23:20:13437#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58438 extension_action_storage_manager_.reset(
439 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13440#endif
[email protected]c77f2352012-08-08 22:07:58441
[email protected]4c9201c42013-08-16 04:56:21442 shared_module_policy_provider_.reset(new SharedModuleProvider);
443
[email protected]cb0e50312011-05-09 15:03:07444 // How long is the path to the Extensions directory?
445 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
446 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25447}
448
[email protected]84df8332011-12-06 18:22:46449const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45450 return &extensions_;
451}
452
[email protected]84df8332011-12-06 18:22:46453const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45454 return &disabled_extensions_;
455}
456
[email protected]84df8332011-12-06 18:22:46457const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20458 return &terminated_extensions_;
459}
460
[email protected]695b5712012-12-06 23:55:28461const ExtensionSet* ExtensionService::blacklisted_extensions() const {
462 return &blacklisted_extensions_;
463}
464
[email protected]9e9c1d12013-07-31 01:58:12465const ExtensionSet* ExtensionService::delayed_installs() const {
466 return &delayed_installs_;
467}
468
[email protected]695b5712012-12-06 23:55:28469scoped_ptr<const ExtensionSet>
470 ExtensionService::GenerateInstalledExtensionsSet() const {
471 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01472 installed_extensions->InsertAll(extensions_);
473 installed_extensions->InsertAll(disabled_extensions_);
474 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28475 installed_extensions->InsertAll(blacklisted_extensions_);
476 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01477}
478
[email protected]3f213ad2012-07-26 23:39:41479extensions::PendingExtensionManager*
480 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37481 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45482}
483
[email protected]eaa7dd182010-12-14 11:09:00484ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17485 // No need to unload extensions here because they are profile-scoped, and the
486 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17487
[email protected]5df038b2012-07-16 19:03:27488 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14489 for (i = external_extension_providers_.begin();
490 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27491 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14492 provider->ServiceShutdown();
493 }
[email protected]6014d672008-12-05 00:38:25494}
495
[email protected]6d147912013-07-09 08:02:14496void ExtensionService::SetSyncStartFlare(
497 const syncer::SyncableService::StartSyncFlare& flare) {
498 flare_ = flare;
499}
500
[email protected]037228a2012-10-05 01:36:16501void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21502 system_->management_policy()->UnregisterProvider(
503 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16504}
505
[email protected]b2907fd2011-03-25 16:43:37506const Extension* ExtensionService::GetExtensionById(
507 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47508 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28509 if (include_disabled) {
510 // Include blacklisted extensions here because there are hundreds of
511 // callers of this function, and many might assume that this includes those
512 // that have been disabled due to blacklisting.
513 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
514 }
515 return GetExtensionById(id, include_mask);
516}
517
[email protected]399583b2012-12-11 09:33:42518GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
519 return content::SiteInstance::GetSiteForURL(
520 profile_,
521 Extension::GetBaseURLFromExtensionId(extension_id));
522}
523
[email protected]695b5712012-12-06 23:55:28524const Extension* ExtensionService::GetExtensionById(
525 const std::string& id, int include_mask) const {
526 std::string lowercase_id = StringToLowerASCII(id);
527 if (include_mask & INCLUDE_ENABLED) {
528 const Extension* extension = extensions_.GetByID(lowercase_id);
529 if (extension)
530 return extension;
531 }
532 if (include_mask & INCLUDE_DISABLED) {
533 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
534 if (extension)
535 return extension;
536 }
537 if (include_mask & INCLUDE_TERMINATED) {
538 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
539 if (extension)
540 return extension;
541 }
542 if (include_mask & INCLUDE_BLACKLISTED) {
543 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
544 if (extension)
545 return extension;
546 }
547 return NULL;
[email protected]78994ab02010-12-08 18:06:44548}
549
[email protected]eaa7dd182010-12-14 11:09:00550void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12551 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20552
[email protected]4a10006a2013-05-17 23:18:35553 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17554 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32555
[email protected]820d9bd2013-04-03 03:46:03556 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03557 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
558 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
559 // The sole purpose of this launch is to install a new extension from CWS
560 // and immediately terminate: loading already installed extensions is
561 // unnecessary and may interfere with the inline install dialog (e.g. if an
562 // extension listens to onStartup and opens a window).
563 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25564 } else {
[email protected]47b60072013-06-20 10:35:56565 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
566 component_loader_->LoadAll();
567 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19568
[email protected]2edd57852013-08-14 22:07:40569 // Attempt to re-enable extensions whose only disable reason is reloading.
570 std::vector<std::string> extensions_to_enable;
571 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
572 iter != disabled_extensions_.end(); ++iter) {
573 const Extension* e = iter->get();
574 if (extension_prefs_->GetDisableReasons(e->id()) ==
575 Extension::DISABLE_RELOAD) {
576 extensions_to_enable.push_back(e->id());
577 }
578 }
579 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
580 it != extensions_to_enable.end(); ++it) {
581 EnableExtension(*it);
582 }
583
[email protected]9f4e4f082013-06-21 07:11:19584 // Finish install (if possible) of extensions that were still delayed while
585 // the browser was shut down.
586 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
587 extension_prefs_->GetAllDelayedInstallInfo());
588 for (size_t i = 0; i < delayed_info->size(); ++i) {
589 ExtensionInfo* info = delayed_info->at(i).get();
590 scoped_refptr<const Extension> extension(NULL);
591 if (info->extension_manifest) {
592 std::string error;
593 extension = Extension::Create(
594 info->extension_path,
595 info->extension_location,
596 *info->extension_manifest,
597 extension_prefs_->GetDelayedInstallCreationFlags(
598 info->extension_id),
599 info->extension_id,
600 &error);
601 if (extension.get())
602 delayed_installs_.Insert(extension);
603 }
604 }
605 MaybeFinishDelayedInstallations();
606
607 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
608 extension_prefs_->GetAllDelayedInstallInfo());
609 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
610 delayed_info2->size() - delayed_info->size());
611
[email protected]47b60072013-06-20 10:35:56612 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25613
[email protected]47b60072013-06-20 10:35:56614 // TODO(erikkay) this should probably be deferred to a future point
615 // rather than running immediately at startup.
616 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57617
[email protected]47b60072013-06-20 10:35:56618 base::MessageLoop::current()->PostDelayedTask(
619 FROM_HERE,
620 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
621 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42622
[email protected]820d9bd2013-04-03 03:46:03623 if (extension_prefs_->NeedsStorageGarbageCollection()) {
624 GarbageCollectIsolatedStorage();
625 extension_prefs_->SetNeedsStorageGarbageCollection(false);
626 }
[email protected]4c9201c42013-08-16 04:56:21627 system_->management_policy()->RegisterProvider(
628 shared_module_policy_provider_.get());
[email protected]399583b2012-12-11 09:33:42629 }
[email protected]6014d672008-12-05 00:38:25630}
631
[email protected]31bb5ee62012-09-12 22:58:40632bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45633 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40634 const GURL& download_url,
635 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12636 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54637 if (browser_terminating_) {
638 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
639 // Leak the temp file at extension_path. We don't want to add to the disk
640 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
641 // the file is in the OS temp directory which should be cleaned up for us.
642 return false;
643 }
[email protected]a8af9fdb2010-10-28 21:52:20644
[email protected]3f213ad2012-07-26 23:39:41645 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06646 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32647
[email protected]695b5712012-12-06 23:55:28648 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06649 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33650 LOG(WARNING) << "Will not update extension " << id
651 << " because it is not installed or pending";
652 // Delete extension_path since we're not creating a CrxInstaller
653 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14654 if (!GetFileTaskRunner()->PostTask(
655 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36656 base::Bind(
[email protected]7296f2762011-11-21 19:23:44657 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36658 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03659
660 return false;
[email protected]e957fe52009-06-23 16:51:05661 }
662
[email protected]aa142702010-03-26 01:26:33663 // We want a silent install only for non-pending extensions and
664 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37665 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05666 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37667 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33668
[email protected]f8636f92013-08-09 21:02:37669 scoped_refptr<CrxInstaller> installer(
670 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16671 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06672 if (pending_extension_info) {
673 installer->set_install_source(pending_extension_info->install_source());
674 if (pending_extension_info->install_silently())
675 installer->set_allow_silent_install(true);
676 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41677 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06678 }
[email protected]7d8b7072012-04-07 01:07:46679 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45680 // its auto-update URL is from the webstore, treat it as a webstore install.
681 // Note that we ignore some older extensions with blank auto-update URLs
682 // because we are mostly concerned with restrictions on NaCl extensions,
683 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53684 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28685 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38686 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35687 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06688 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53689 creation_flags |= Extension::FROM_WEBSTORE;
690 }
691
692 // Bookmark apps being updated is kind of a contradiction, but that's because
693 // we mark the default apps as bookmark apps, and they're hosted in the web
694 // store, thus they can get updated. See http://crbug.com/101605 for more
695 // details.
696 if (extension && extension->from_bookmark())
697 creation_flags |= Extension::FROM_BOOKMARK;
698
[email protected]e33bbc22012-08-27 22:05:46699 if (extension && extension->was_installed_by_default())
700 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
701
[email protected]a12ce8b22012-01-17 18:40:53702 installer->set_creation_flags(creation_flags);
703
[email protected]6dfbbf82010-03-12 23:09:16704 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06705 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07706 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16707 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03708
709 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09710 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03711
712 return true;
[email protected]e957fe52009-06-23 16:51:05713}
714
[email protected]e178ad92013-06-28 02:29:25715void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12716 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05717
718 // If the extension is already reloading, don't reload again.
719 if (extension_prefs_->GetDisableReasons(extension_id) &
720 Extension::DISABLE_RELOAD) {
721 return;
722 }
723
[email protected]650b2d52013-02-10 03:41:45724 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02725 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40726
[email protected]f17dbd42010-08-16 23:21:10727 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06728 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29729 // If the extension has an inspector open for its background page, detach
730 // the inspector and hang onto a cookie for it, so that we can reattach
731 // later.
[email protected]31d8f5f22012-04-02 15:22:08732 // TODO(yoz): this is not incognito-safe!
733 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13734 extensions::ExtensionHost* host =
735 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36736 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29737 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09738 scoped_refptr<DevToolsAgentHost> agent_host =
739 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
740 agent_host->DisconnectRenderViewHost();
741 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29742 }
743
[email protected]b65272f2009-08-31 15:47:06744 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50745 // BeingUpgraded is set back to false when the extension is added.
746 SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03747 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03748 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16749 } else {
750 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06751 }
752
[email protected]9f4e4f082013-06-21 07:11:19753 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48754 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05755 return;
756 }
757
[email protected]43ceb002012-02-10 23:19:15758 // If we're reloading a component extension, use the component extension
759 // loader's reloader.
760 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25761 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15762 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25763 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15764 return;
765 }
766
[email protected]e6090e42010-03-23 22:44:08767 // Check the installed extensions to see if what we're reloading was already
768 // installed.
[email protected]e178ad92013-06-28 02:29:25769 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08770 scoped_ptr<ExtensionInfo> installed_extension(
771 extension_prefs_->GetInstalledExtensionInfo(extension_id));
772 if (installed_extension.get() &&
773 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01774 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08775 } else {
[email protected]d8c8f25f2011-11-02 18:18:01776 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08777 // We should always be able to remember the extension's path. If it's not in
778 // the map, someone failed to update |unloaded_extension_paths_|.
779 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01780 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08781 }
[email protected]e178ad92013-06-28 02:29:25782 // When reloading is done, mark this extension as done reloading.
783 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40784}
785
[email protected]fa2416f2011-05-03 08:41:20786bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08787 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20788 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14789 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12790 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20791
[email protected]0d6ec3a72011-09-02 02:09:43792 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25793
[email protected]e7afe2452010-08-22 16:19:13794 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09795 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32796
[email protected]95da88c42011-03-31 10:07:33797 // Policy change which triggers an uninstall will always set
798 // |external_uninstall| to true so this is the only way to uninstall
799 // managed extensions.
[email protected]65187152012-06-02 13:14:14800 if (!external_uninstall &&
801 !system_->management_policy()->UserMayModifySettings(
802 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07803 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27804 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53805 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09806 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33807 return false;
808 }
[email protected]95da88c42011-03-31 10:07:33809
[email protected]3bdba0d2011-08-23 07:17:30810 // Extract the data we need for sync now, but don't actually sync until we've
811 // completed the uninstallation.
[email protected]6d147912013-07-09 08:02:14812 // TODO(tim): If we get here and IsSyncing is false, this will cause
813 // "back from the dead" style bugs, because sync will add-back the extension
814 // that was uninstalled here when MergeDataAndStartSyncing is called.
815 // See crbug.com/256795.
[email protected]65f173552012-06-28 22:43:58816 syncer::SyncChange sync_change;
[email protected]6d147912013-07-09 08:02:14817 if (extensions::sync_helper::IsSyncableApp(extension.get())) {
818 if (app_sync_bundle_.IsSyncing())
819 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension.get());
820 else if (is_ready() && !flare_.is_null())
821 flare_.Run(syncer::APPS); // Tell sync to start ASAP.
822 } else if (extensions::sync_helper::IsSyncableExtension(extension.get())) {
823 if (extension_sync_bundle_.IsSyncing())
824 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
825 else if (is_ready() && !flare_.is_null())
826 flare_.Run(syncer::EXTENSIONS); // Tell sync to start ASAP.
[email protected]3bdba0d2011-08-23 07:17:30827 }
828
[email protected]dc24976f2013-06-02 21:15:09829 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08830 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
831 EXTERNAL_EXTENSION_UNINSTALLED,
832 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02833 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
834 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
835 EXTERNAL_EXTENSION_UNINSTALLED,
836 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
837 } else {
838 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
839 EXTERNAL_EXTENSION_UNINSTALLED,
840 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
841 }
[email protected]d96eb512012-11-01 23:44:08842 }
[email protected]9b217652010-10-08 22:04:23843 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08844 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09845 RecordPermissionMessagesHistogram(extension.get(),
846 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23847
[email protected]831aa212010-03-26 13:55:19848 // Unload before doing more cleanup to ensure that nothing is hanging on to
849 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59850 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19851
[email protected]0d6ec3a72011-09-02 02:09:43852 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19853 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32854
855 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05856 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14857 if (!GetFileTaskRunner()->PostTask(
858 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36859 base::Bind(
[email protected]14908b72011-04-20 06:54:36860 &extension_file_util::UninstallExtension,
861 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20862 extension_id)))
[email protected]14908b72011-04-20 06:54:36863 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32864 }
865
[email protected]6b414c232013-06-05 07:53:34866 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36867 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43868 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09869 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43870
[email protected]399583b2012-12-11 09:33:42871 if (is_storage_isolated) {
872 BrowserContext::AsyncObliterateStoragePartition(
873 profile_,
874 GetSiteForExtensionId(extension_id),
875 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
876 AsWeakPtr()));
877 } else {
878 if (extension->is_hosted_app() &&
879 !profile_->GetExtensionSpecialStoragePolicy()->
880 IsStorageProtected(launch_web_url_origin)) {
881 extensions::DataDeleter::StartDeleting(
882 profile_, extension_id, launch_web_url_origin);
883 }
884 extensions::DataDeleter::StartDeleting(profile_, extension_id,
885 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43886 }
[email protected]0d6ec3a72011-09-02 02:09:43887
[email protected]0dfe05c2011-02-23 23:03:36888 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06889
890 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07891 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27892 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53893 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09894 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33895
[email protected]5db9ada2012-04-11 13:48:20896 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32897 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20898 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
899 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32900 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20901 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30902 }
903
[email protected]399583b2012-12-11 09:33:42904 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05905
[email protected]198b5902013-06-27 10:36:11906 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19907
[email protected]333b1de2011-09-12 18:28:50908 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18909 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
910
[email protected]d6ebc9792011-04-07 18:18:33911 return true;
[email protected]c10da4b02010-03-25 14:38:32912}
913
[email protected]c3cfb012011-04-06 22:07:35914bool ExtensionService::IsExtensionEnabled(
915 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46916 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21917 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18918 return true;
[email protected]dc9a74f72012-08-17 18:07:21919 }
[email protected]36429da2011-07-11 20:25:18920
[email protected]695b5712012-12-06 23:55:28921 if (disabled_extensions_.Contains(extension_id) ||
922 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25923 return false;
[email protected]695b5712012-12-06 23:55:28924 }
[email protected]ad83ca242011-07-29 01:32:25925
926 // If the extension hasn't been loaded yet, check the prefs for it. Assume
927 // enabled unless otherwise noted.
928 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31929 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35930}
931
932bool ExtensionService::IsExternalExtensionUninstalled(
933 const std::string& extension_id) const {
934 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
935}
936
[email protected]e2377032013-01-18 17:53:22937bool ExtensionService::IsExtensionEnabledForLauncher(
938 const std::string& extension_id) const {
939 return IsExtensionEnabled(extension_id) &&
940 !GetTerminatedExtension(extension_id);
941}
942
[email protected]eaa7dd182010-12-14 11:09:00943void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12944 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20945
[email protected]06f92562011-04-29 19:27:31946 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39947 return;
[email protected]0c6da502009-08-14 22:32:39948
[email protected]b3317ad2011-04-28 23:46:00949 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28950 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52951
[email protected]695b5712012-12-06 23:55:28952 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31953 // This can happen if sync enables an extension that is not
954 // installed yet.
955 if (!extension)
956 return;
957
[email protected]d96eb512012-11-01 23:44:08958 if (IsUnacknowledgedExternalExtension(extension)) {
959 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
960 EXTERNAL_EXTENSION_REENABLED,
961 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02962 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
963 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
964 EXTERNAL_EXTENSION_REENABLED,
965 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
966 } else {
967 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
968 EXTERNAL_EXTENSION_REENABLED,
969 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
970 }
[email protected]612a1cb12012-10-17 13:18:03971 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08972 }
[email protected]612a1cb12012-10-17 13:18:03973
[email protected]0c6da502009-08-14 22:32:39974 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46975 extensions_.Insert(make_scoped_refptr(extension));
976 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39977
[email protected]62d30f42009-10-01 22:36:06978 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30979
[email protected]f74d7f32012-06-19 19:22:51980 // Notify listeners that the extension was enabled.
981 content::NotificationService::current()->Notify(
982 chrome::NOTIFICATION_EXTENSION_ENABLED,
983 content::Source<Profile>(profile_),
984 content::Details<const Extension>(extension));
985
[email protected]3bdba0d2011-08-23 07:17:30986 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39987}
988
[email protected]44d62b62012-04-11 00:06:03989void ExtensionService::DisableExtension(
990 const std::string& extension_id,
991 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12992 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20993
[email protected]b2ba9962009-12-10 20:10:15994 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31995 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52996 return;
[email protected]1784e83a2009-09-08 21:01:52997
[email protected]06f92562011-04-29 19:27:31998 const Extension* extension = GetInstalledExtension(extension_id);
999 // |extension| can be NULL if sync disables an extension that is not
1000 // installed yet.
[email protected]65187152012-06-02 13:14:141001 if (extension &&
[email protected]505ccdc2013-06-20 20:15:351002 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:141003 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:331004 return;
[email protected]65187152012-06-02 13:14:141005 }
[email protected]95da88c42011-03-31 10:07:331006
[email protected]b3317ad2011-04-28 23:46:001007 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:281008 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:521009
[email protected]695b5712012-12-06 23:55:281010 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
1011 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:311012 if (!extension)
1013 return;
1014
[email protected]64742de32013-04-22 08:44:341015 // Reset the background_page_ready flag
1016 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
1017 extension_runtime_data_[extension->id()].background_page_ready = false;
1018
[email protected]5c094792012-09-07 19:44:531019 // Move it over to the disabled list. Don't send a second unload notification
1020 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:461021 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:531022 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461023 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:531024 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
1025 } else {
[email protected]84df8332011-12-06 18:22:461026 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:531027 }
[email protected]3bdba0d2011-08-23 07:17:301028
1029 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:521030}
1031
[email protected]1abf05e2013-07-09 17:04:361032void ExtensionService::DisableUserExtensions(
1033 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:141034 extensions::ManagementPolicy* management_policy =
1035 system_->management_policy();
1036 extensions::ExtensionList to_disable;
1037
1038 for (ExtensionSet::const_iterator extension = extensions_.begin();
1039 extension != extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111040 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141041 to_disable.push_back(*extension);
1042 }
1043 for (ExtensionSet::const_iterator extension = terminated_extensions_.begin();
1044 extension != terminated_extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111045 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141046 to_disable.push_back(*extension);
1047 }
1048
1049 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1050 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381051 if ((*extension)->was_installed_by_default() &&
1052 extension_urls::IsWebstoreUpdateUrl(
1053 extensions::ManifestURL::GetUpdateURL(*extension)))
1054 continue;
[email protected]1abf05e2013-07-09 17:04:361055 const std::string& id = (*extension)->id();
1056 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1057 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141058 }
1059}
1060
[email protected]eaa7dd182010-12-14 11:09:001061void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281062 const Extension* extension) {
1063 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491064 RecordPermissionMessagesHistogram(
1065 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251066 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1067 EnableExtension(extension->id());
1068}
1069
[email protected]009633c2013-03-07 22:08:281070void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451071 CHECK(extension);
1072 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281073 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451074}
1075
[email protected]fe2dd7742011-04-19 22:52:491076// static
1077void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051078 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171079 // Since this is called from multiple sources, and since the histogram macros
1080 // use statics, we need to manually lookup the histogram ourselves.
1081 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491082 histogram,
1083 1,
[email protected]c2e66e12012-06-27 06:27:061084 PermissionMessage::kEnumBoundary,
1085 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171086 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491087
[email protected]13c68b62013-05-17 11:29:051088 PermissionMessages permissions =
1089 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491090 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061091 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491092 } else {
[email protected]c2e66e12012-06-27 06:27:061093 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521094 it != permissions.end(); ++it)
1095 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491096 }
1097}
1098
[email protected]eaa7dd182010-12-14 11:09:001099void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421100 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061101 // was loaded, otherwise a race can arise where a renderer that is created
1102 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421103 // that the request context doesn't yet know about. The profile is responsible
1104 // for ensuring its URLRequestContexts appropriately discover the loaded
1105 // extension.
[email protected]31d8f5f22012-04-02 15:22:081106 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061107
[email protected]d5949312012-12-03 22:13:301108 // Tell renderers about the new extension, unless it's a theme (renderers
1109 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191110 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491111 for (content::RenderProcessHost::iterator i(
1112 content::RenderProcessHost::AllHostsIterator());
1113 !i.IsAtEnd(); i.Advance()) {
1114 content::RenderProcessHost* host = i.GetCurrentValue();
1115 Profile* host_profile =
1116 Profile::FromBrowserContext(host->GetBrowserContext());
1117 if (host_profile->GetOriginalProfile() ==
1118 profile_->GetOriginalProfile()) {
1119 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1120 1, ExtensionMsg_Loaded_Params(extension));
1121 host->Send(
1122 new ExtensionMsg_Loaded(loaded_extensions));
1123 }
[email protected]c8d407e2011-04-28 21:27:171124 }
[email protected]77a6970c2011-04-23 16:58:561125 }
1126
[email protected]3442a662012-01-12 08:06:171127 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1128 // extension.
1129 //
1130 // NOTE: It is important that this happen after notifying the renderers about
1131 // the new extensions so that if we navigate to an extension URL in
1132 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1133 content::NotificationService::current()->Notify(
1134 chrome::NOTIFICATION_EXTENSION_LOADED,
1135 content::Source<Profile>(profile_),
1136 content::Details<const Extension>(extension));
1137
[email protected]c8d407e2011-04-28 21:27:171138 // Tell a random-ass collection of other subsystems about the new extension.
1139 // TODO(aa): What should we do with all this goop? Can it move into the
1140 // relevant objects via EXTENSION_LOADED?
1141
1142 profile_->GetExtensionSpecialStoragePolicy()->
1143 GrantRightsForExtension(extension);
1144
1145 UpdateActiveExtensionsInCrashReporter();
1146
[email protected]c8d407e2011-04-28 21:27:171147 // If the extension has permission to load chrome://favicon/ resources we need
1148 // to make sure that the FaviconSource is registered with the
1149 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051150 if (extensions::PermissionsData::HasHostPermission(
1151 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171152 FaviconSource* favicon_source = new FaviconSource(profile_,
1153 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531154 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171155 }
[email protected]b07e606e2012-09-15 20:16:151156
1157#if !defined(OS_ANDROID)
1158 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051159 if (extensions::PermissionsData::HasHostPermission(
1160 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151161 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531162 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151163 }
1164#endif
1165
[email protected]5eddc3e2011-10-26 04:33:311166 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051167 if (extensions::PermissionsData::HasHostPermission(
1168 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]5eddc3e2011-10-26 04:33:311169 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531170 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311171 }
[email protected]62d30f42009-10-01 22:36:061172}
1173
[email protected]a9f39a312010-12-23 22:14:271174void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591175 const Extension* extension,
1176 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271177 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071178 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271179 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531180 content::Source<Profile>(profile_),
1181 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061182
[email protected]f3b1a082011-11-18 00:34:301183 for (content::RenderProcessHost::iterator i(
1184 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561185 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301186 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081187 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301188 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081189 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171190 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561191 }
1192
[email protected]31d8f5f22012-04-02 15:22:081193 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171194 profile_->GetExtensionSpecialStoragePolicy()->
1195 RevokeRightsForExtension(extension);
1196
[email protected]b777b332011-04-16 04:01:081197#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231198 // Revoke external file access for the extension from its file system context.
1199 // It is safe to access the extension's storage partition at this point. The
1200 // storage partition may get destroyed only after the extension gets unloaded.
1201 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1202 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091203 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231204 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091205 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321206 if (filesystem_context && filesystem_context->external_backend()) {
1207 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171208 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061209 }
[email protected]c8d407e2011-04-28 21:27:171210#endif
1211
1212 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061213}
1214
[email protected]0a071a32011-02-08 00:18:241215Profile* ExtensionService::profile() {
1216 return profile_;
1217}
1218
[email protected]45759612012-07-10 17:21:231219extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451220 return extension_prefs_;
1221}
1222
[email protected]0d9a2202011-11-09 13:48:411223extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381224 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201225}
1226
[email protected]e2d720aa2012-05-17 00:11:351227extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441228 return extension_prefs()->content_settings_store();
1229}
1230
[email protected]25ae0152011-11-18 14:40:021231bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351232 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021233}
1234
[email protected]7f8f24f2012-11-15 19:40:141235base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091236 if (file_task_runner_.get())
1237 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141238
1239 // We should be able to interrupt any part of extension install process during
1240 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1241 // will be ignored/deleted while we will block on started tasks.
1242 std::string token("ext_install-");
1243 token.append(profile_->GetPath().AsUTF8Unsafe());
1244 file_task_runner_ = BrowserThread::GetBlockingPool()->
1245 GetSequencedTaskRunnerWithShutdownBehavior(
1246 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1247 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091248 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141249}
1250
[email protected]42a08162012-03-16 18:09:111251extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061252 return updater_.get();
1253}
1254
[email protected]4ee07c62012-08-21 12:40:421255void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251256 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281257
[email protected]aa96d3a2010-08-21 08:45:251258 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461259 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251260 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361261 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421262 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251263 to_be_removed.push_back(extension->id());
1264 }
1265
1266 // UnloadExtension will change the extensions_ list. So, we should
1267 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401268 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591269 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251270}
1271
[email protected]31206602011-04-13 23:07:321272void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351273 if (updater()) {
[email protected]94fde232012-04-27 10:22:301274 if (AreAllExternalProvidersReady()) {
1275 updater()->CheckSoon();
1276 } else {
1277 // Sync can start updating before all the external providers are ready
1278 // during startup. Start the update as soon as those providers are ready,
1279 // but not before.
1280 update_once_all_providers_are_ready_ = true;
1281 }
[email protected]c3cfb012011-04-06 22:07:351282 } else {
[email protected]31206602011-04-13 23:07:321283 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351284 }
1285}
1286
[email protected]3f98fd92012-11-17 15:54:291287syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321288 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581289 const syncer::SyncDataList& initial_sync_data,
1290 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1291 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201292 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281293 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301294
1295 switch (type) {
[email protected]a4a147652012-07-03 23:41:321296 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201297 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281298 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201299 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301300 break;
1301
[email protected]a4a147652012-07-03 23:41:321302 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281303 app_sync_bundle_.SetupSync(sync_processor.release(),
1304 sync_error_factory.release(),
1305 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301306 break;
1307
1308 default:
1309 LOG(FATAL) << "Got " << type << " ModelType";
1310 }
[email protected]3bdba0d2011-08-23 07:17:301311
[email protected]b1417ab2011-12-15 17:09:111312 // Process local extensions.
1313 // TODO(yoz): Determine whether pending extensions should be considered too.
1314 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581315 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1316 syncer::SyncChangeList sync_change_list;
1317 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301318 i != sync_data_list.end();
1319 ++i) {
[email protected]5db9ada2012-04-11 13:48:201320 switch (type) {
[email protected]a4a147652012-07-03 23:41:321321 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201322 sync_change_list.push_back(
1323 extension_sync_bundle_.CreateSyncChange(*i));
1324 break;
[email protected]a4a147652012-07-03 23:41:321325 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201326 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1327 break;
1328 default:
1329 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401330 }
[email protected]3bdba0d2011-08-23 07:17:301331 }
[email protected]3bdba0d2011-08-23 07:17:301332
[email protected]5db9ada2012-04-11 13:48:201333
[email protected]a4a147652012-07-03 23:41:321334 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201335 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321336 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201337 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1338 }
[email protected]0ffd9e52012-01-26 21:44:341339
[email protected]3f98fd92012-11-17 15:54:291340 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301341}
1342
[email protected]a4a147652012-07-03 23:41:321343void ExtensionService::StopSyncing(syncer::ModelType type) {
1344 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201345 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321346 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201347 extension_sync_bundle_.Reset();
1348 }
[email protected]3bdba0d2011-08-23 07:17:301349}
1350
[email protected]65f173552012-06-28 22:43:581351syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321352 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401353 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201354 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401355 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201356 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201357
1358 // We should only get sync data for extensions and apps.
1359 NOTREACHED();
1360
[email protected]65f173552012-06-28 22:43:581361 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301362}
1363
[email protected]65f173552012-06-28 22:43:581364syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301365 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581366 const syncer::SyncChangeList& change_list) {
1367 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301368 i != change_list.end();
1369 ++i) {
[email protected]a4a147652012-07-03 23:41:321370 syncer::ModelType type = i->sync_data().GetDataType();
1371 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201372 extension_sync_bundle_.ProcessSyncChange(
1373 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321374 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201375 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1376 }
[email protected]3bdba0d2011-08-23 07:17:301377 }
1378
[email protected]0ffd9e52012-01-26 21:44:341379 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1380
[email protected]65f173552012-06-28 22:43:581381 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561382}
1383
[email protected]5db9ada2012-04-11 13:48:201384extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1385 const Extension& extension) const {
1386 return extensions::ExtensionSyncData(extension,
1387 IsExtensionEnabled(extension.id()),
1388 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561389}
1390
[email protected]5db9ada2012-04-11 13:48:201391extensions::AppSyncData ExtensionService::GetAppSyncData(
1392 const Extension& extension) const {
1393 return extensions::AppSyncData(
1394 extension,
1395 IsExtensionEnabled(extension.id()),
1396 IsIncognitoEnabled(extension.id()),
[email protected]5db9ada2012-04-11 13:48:201397 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1398 extension.id()),
1399 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1400}
[email protected]3bdba0d2011-08-23 07:17:301401
[email protected]5db9ada2012-04-11 13:48:201402std::vector<extensions::ExtensionSyncData>
1403 ExtensionService::GetExtensionSyncDataList() const {
1404 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1405 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1406 &extension_sync_list);
1407 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1408 &extension_sync_list);
1409 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1410 &extension_sync_list);
1411
1412 std::vector<extensions::ExtensionSyncData> pending_extensions =
1413 extension_sync_bundle_.GetPendingData();
1414 extension_sync_list.insert(extension_sync_list.begin(),
1415 pending_extensions.begin(),
1416 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301417
1418 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561419}
1420
[email protected]5db9ada2012-04-11 13:48:201421std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1422 const {
1423 std::vector<extensions::AppSyncData> app_sync_list;
1424 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1425 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1426 &app_sync_list);
1427 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1428 &app_sync_list);
1429
1430 std::vector<extensions::AppSyncData> pending_apps =
1431 app_sync_bundle_.GetPendingData();
1432 app_sync_list.insert(app_sync_list.begin(),
1433 pending_apps.begin(),
1434 pending_apps.end());
1435
1436 return app_sync_list;
1437}
1438
1439bool ExtensionService::ProcessExtensionSyncData(
1440 const extensions::ExtensionSyncData& extension_sync_data) {
1441 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321442 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201443 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1444 extension_sync_data);
1445 CheckForUpdatesSoon();
1446 return false;
1447 }
1448
1449 return true;
1450}
1451
1452bool ExtensionService::ProcessAppSyncData(
1453 const extensions::AppSyncData& app_sync_data) {
1454 const std::string& id = app_sync_data.id();
[email protected]5db9ada2012-04-11 13:48:201455
1456 if (app_sync_data.app_launch_ordinal().IsValid() &&
1457 app_sync_data.page_ordinal().IsValid()) {
1458 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1459 id,
1460 app_sync_data.app_launch_ordinal());
1461 extension_prefs_->extension_sorting()->SetPageOrdinal(
1462 id,
1463 app_sync_data.page_ordinal());
1464 }
1465
[email protected]5db9ada2012-04-11 13:48:201466 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321467 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201468 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1469 CheckForUpdatesSoon();
1470 return false;
1471 }
1472
1473 return true;
1474}
1475
1476bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321477 syncer::ModelType type) const {
1478 if (type == syncer::EXTENSIONS &&
[email protected]8f3bcbd2013-06-05 08:42:401479 extensions::sync_helper::IsSyncableExtension(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201480 return true;
[email protected]31bb5ee62012-09-12 22:58:401481 }
[email protected]5db9ada2012-04-11 13:48:201482
[email protected]a4a147652012-07-03 23:41:321483 if (type == syncer::APPS &&
[email protected]8f3bcbd2013-06-05 08:42:401484 extensions::sync_helper::IsSyncableApp(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201485 return true;
[email protected]31bb5ee62012-09-12 22:58:401486 }
[email protected]5db9ada2012-04-11 13:48:201487
1488 return false;
1489}
1490
1491bool ExtensionService::ProcessExtensionSyncDataHelper(
1492 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321493 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301494 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061495 const Extension* extension = GetInstalledExtension(id);
1496
1497 // TODO(bolms): we should really handle this better. The particularly bad
1498 // case is where an app becomes an extension or vice versa, and we end up with
1499 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201500 if (extension && !IsCorrectSyncType(*extension, type))
1501 return true;
[email protected]90310d92011-04-17 07:35:041502
1503 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301504 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041505 if (!UninstallExtensionHelper(this, id)) {
1506 LOG(WARNING) << "Could not uninstall extension " << id
1507 << " for sync";
1508 }
[email protected]5db9ada2012-04-11 13:48:201509 return true;
[email protected]90310d92011-04-17 07:35:041510 }
1511
[email protected]caf2d442012-05-04 10:00:501512 // Extension from sync was uninstalled by the user as external extensions.
1513 // Honor user choice and skip installation/enabling.
1514 if (IsExternalExtensionUninstalled(id)) {
1515 LOG(WARNING) << "Extension with id " << id
1516 << " from sync was uninstalled as external extension";
1517 return true;
1518 }
1519
[email protected]06f92562011-04-29 19:27:311520 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191521 // If the extension has been disabled from sync, it may not have
1522 // been installed yet, so we don't know if the disable reason was a
[email protected]b914e2952013-04-26 07:10:031523 // permissions increase. That will be updated once CheckPermissionsIncrease
[email protected]7e9e2422012-12-13 19:54:191524 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401525 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311526 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401527 else
[email protected]7e9e2422012-12-13 19:54:191528 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371529
[email protected]406b5a92011-11-08 11:58:261530 // We need to cache some version information here because setting the
1531 // incognito flag invalidates the |extension| pointer (it reloads the
1532 // extension).
[email protected]9060d8b02012-01-13 02:14:301533 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261534 int result = extension ?
1535 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1536 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1537 extension = NULL; // No longer safe to use.
1538
1539 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311540 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041541 if (result < 0) {
1542 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201543 return false;
[email protected]b020dc12011-12-06 04:35:421544 }
[email protected]06f92562011-04-29 19:27:311545 } else {
[email protected]06f92562011-04-29 19:27:311546 // TODO(akalin): Replace silent update with a list of enabled
1547 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331548 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201549
[email protected]a4a147652012-07-03 23:41:321550 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]8f3bcbd2013-06-05 08:42:401551 extensions::PendingExtensionInfo::ShouldAllowInstallPredicate filter =
1552 (type == syncer::APPS) ? extensions::sync_helper::IsSyncableApp :
1553 extensions::sync_helper::IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201554
[email protected]06f92562011-04-29 19:27:311555 if (!pending_extension_manager()->AddFromSync(
1556 id,
[email protected]3bdba0d2011-08-23 07:17:301557 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201558 filter,
[email protected]6cc7dbae2011-04-29 21:18:331559 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311560 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301561 // This means that the extension is already pending installation, with a
1562 // non-INTERNAL location. Add to pending_sync_data, even though it will
1563 // never be removed (we'll never install a syncable version of the
1564 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311565 }
[email protected]3bdba0d2011-08-23 07:17:301566 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201567 return false;
[email protected]90310d92011-04-17 07:35:041568 }
[email protected]5db9ada2012-04-11 13:48:201569
1570 return true;
[email protected]90310d92011-04-17 07:35:041571}
1572
[email protected]c3cfb012011-04-06 22:07:351573bool ExtensionService::IsIncognitoEnabled(
1574 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481575 const Extension* extension = GetInstalledExtension(extension_id);
1576 if (extension && !extension->can_be_incognito_enabled())
1577 return false;
[email protected]c3cfb012011-04-06 22:07:351578 // If this is an existing component extension we always allow it to
1579 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401580 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411581 return true;
[email protected]fc05749a2013-07-10 06:59:111582 if (extension && extension->force_incognito_enabled())
1583 return true;
[email protected]cb0ce1e022010-03-10 19:54:411584
1585 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351586 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501587}
[email protected]55a35692010-02-11 23:25:211588
[email protected]c3cfb012011-04-06 22:07:351589void ExtensionService::SetIsIncognitoEnabled(
1590 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371591 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481592 if (extension && !extension->can_be_incognito_enabled())
1593 return;
[email protected]1d5e58b2013-01-31 08:41:401594 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481595 // This shouldn't be called for component extensions unless they are
1596 // syncable.
[email protected]8f3bcbd2013-06-05 08:42:401597 DCHECK(extensions::sync_helper::IsSyncable(extension));
[email protected]b873cd92012-02-09 21:51:481598
1599 // If we are here, make sure the we aren't trying to change the value.
1600 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1601
[email protected]c3cfb012011-04-06 22:07:351602 return;
1603 }
1604
[email protected]8c6c8c22011-03-09 12:52:341605 // Broadcast unloaded and loaded events to update browser state. Only bother
1606 // if the value changed and the extension is actually enabled, since there is
1607 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351608 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341609 if (enabled == old_enabled)
1610 return;
1611
[email protected]c3cfb012011-04-06 22:07:351612 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371613
[email protected]ffbf3f12011-12-09 22:10:331614 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341615
1616 // When we reload the extension the ID may be invalidated if we've passed it
1617 // by const ref everywhere. Make a copy to be safe.
1618 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261619 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331620 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301621
[email protected]406b5a92011-11-08 11:58:261622 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341623 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301624 if (extension)
1625 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211626}
1627
[email protected]dc9a74f72012-08-17 18:07:211628bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051629 // We allow the extension to see events and data from another profile iff it
1630 // uses "spanning" behavior and it has incognito access. "split" mode
1631 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221632 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351633 return IsIncognitoEnabled(extension->id()) &&
[email protected]06492ed2013-03-24 22:13:141634 !extensions::IncognitoInfo::IsSplitMode(extension);
[email protected]2a8f24e2010-11-03 21:37:051635}
1636
[email protected]5ef7b002011-05-12 23:16:201637bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1638 if (extension->is_hosted_app())
1639 return true;
1640 // Packaged apps and regular extensions need to be enabled specifically for
1641 // incognito (and split mode should be set).
[email protected]06492ed2013-03-24 22:13:141642 return extensions::IncognitoInfo::IsSplitMode(extension) &&
[email protected]5ef7b002011-05-12 23:16:201643 IsIncognitoEnabled(extension->id());
1644}
1645
[email protected]168389f2011-12-20 17:12:481646void ExtensionService::OnExtensionMoved(
1647 const std::string& moved_extension_id,
1648 const std::string& predecessor_extension_id,
1649 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231650 extension_prefs_->extension_sorting()->OnExtensionMoved(
1651 moved_extension_id,
1652 predecessor_extension_id,
1653 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481654
1655 const Extension* extension = GetInstalledExtension(moved_extension_id);
1656 if (extension)
1657 SyncExtensionChangeIfNeeded(*extension);
1658}
1659
[email protected]dc9a74f72012-08-17 18:07:211660bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081661 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441662 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081663 extension_prefs_->AllowFileAccess(extension->id()));
1664}
1665
[email protected]eaa7dd182010-12-14 11:09:001666void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481667 bool allow) {
1668 // Reload to update browser state. Only bother if the value changed and the
1669 // extension is actually enabled, since there is no UI otherwise.
1670 bool old_allow = AllowFileAccess(extension);
1671 if (allow == old_allow)
1672 return;
1673
[email protected]05c82182010-06-24 17:49:081674 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481675
[email protected]84df8332011-12-06 18:22:461676 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481677 if (extension_is_enabled)
1678 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081679}
1680
[email protected]8e4560b62011-01-14 10:09:141681// Some extensions will autoupdate themselves externally from Chrome. These
1682// are typically part of some larger client application package. To support
1683// these, the extension will register its location in the the preferences file
1684// (and also, on Windows, in the registry) and this code will periodically
1685// check that location for a .crx file, which it will then install locally if
1686// a new version is available.
1687// Errors are reported through ExtensionErrorReporter. Succcess is not
1688// reported.
[email protected]eaa7dd182010-12-14 11:09:001689void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121690 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141691
1692 // Note that this installation is intentionally silent (since it didn't
1693 // go through the front-end). Extensions that are registered in this
1694 // way are effectively considered 'pre-bundled', and so implicitly
1695 // trusted. In general, if something has HKLM or filesystem access,
1696 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121697
[email protected]8e4560b62011-01-14 10:09:141698 // Ask each external extension provider to give us a call back for each
1699 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271700 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141701 for (i = external_extension_providers_.begin();
1702 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271703 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141704 provider->VisitRegisteredExtension();
1705 }
1706
[email protected]50067e52011-10-20 23:17:071707 // Do any required work that we would have done after completion of all
1708 // providers.
1709 if (external_extension_providers_.empty()) {
1710 OnAllExternalProvidersReady();
1711 }
[email protected]9f1087e2009-06-15 17:29:321712}
1713
[email protected]50067e52011-10-20 23:17:071714void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271715 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121716 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071717 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121718
1719 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141720 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301721 if (AreAllExternalProvidersReady())
1722 OnAllExternalProvidersReady();
1723}
1724
1725bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271726 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141727 for (i = external_extension_providers_.begin();
1728 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301729 if (!i->get()->IsReady())
1730 return false;
[email protected]8e4560b62011-01-14 10:09:141731 }
[email protected]94fde232012-04-27 10:22:301732 return true;
[email protected]50067e52011-10-20 23:17:071733}
1734
1735void ExtensionService::OnAllExternalProvidersReady() {
1736 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301737 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1738 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071739
1740 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301741 if (update_once_all_providers_are_ready_ && updater()) {
1742 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061743 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141744 }
1745
1746 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231747 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141748 extension_prefs_->GetInstalledExtensionsInfo());
1749 for (size_t i = 0; i < extensions_info->size(); ++i) {
1750 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401751 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141752 CheckExternalUninstall(info->extension_id);
1753 }
[email protected]e5af875f2011-10-10 21:09:141754 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141755}
1756
1757void ExtensionService::IdentifyAlertableExtensions() {
1758 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1759
[email protected]07c9f2f42012-02-29 18:45:221760 // Build up the lists of extensions that require acknowledgment. If this is
1761 // the first time, grandfather extensions that would have caused
1762 // notification.
[email protected]89226982012-07-16 20:09:181763 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141764
[email protected]a9aa5932012-01-25 08:27:401765 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181766 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011767 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401768 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181769 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141770 } else {
1771 // First run. Just acknowledge all the extensions, silently, by
1772 // shortcutting the display of the UI and going straight to the
1773 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401774 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141775 }
1776 }
[email protected]a9aa5932012-01-25 08:27:401777
[email protected]612a1cb12012-10-17 13:18:031778 UpdateExternalExtensionAlert();
1779
[email protected]a9aa5932012-01-25 08:27:401780 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181781 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141782}
1783
[email protected]89226982012-07-16 20:09:181784bool ExtensionService::PopulateExtensionErrorUI(
1785 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221786 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281787
1788 // Extensions that are blacklisted.
1789 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1790 it != blacklisted_extensions_.end(); ++it) {
1791 std::string id = (*it)->id();
1792 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1793 extension_error_ui->AddBlacklistedExtension(id);
1794 needs_alert = true;
1795 }
1796 }
1797
[email protected]07c9f2f42012-02-29 18:45:221798 for (ExtensionSet::const_iterator iter = extensions_.begin();
1799 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361800 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281801
1802 // Extensions disabled by policy. Note: this no longer includes blacklisted
1803 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391804 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221805 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181806 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221807 needs_alert = true;
1808 }
1809 }
[email protected]07c9f2f42012-02-29 18:45:221810 }
[email protected]695b5712012-12-06 23:55:281811
[email protected]07c9f2f42012-02-29 18:45:221812 return needs_alert;
1813}
1814
[email protected]a9aa5932012-01-25 08:27:401815void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031816 const ExtensionIdSet* extension_ids =
1817 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141818 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1819 iter != extension_ids->end(); ++iter) {
1820 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1821 }
[email protected]879a6092013-03-06 04:12:501822 extension_error_ui_.reset();
1823}
1824
1825void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301826 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141827}
1828
[email protected]47fc70c2011-12-06 07:29:511829void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1830 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031831 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511832}
1833
[email protected]d96eb512012-11-01 23:44:081834bool ExtensionService::IsUnacknowledgedExternalExtension(
1835 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321836 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1837 return false;
1838
[email protected]1d5e58b2013-01-31 08:41:401839 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081840 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1841 !(extension_prefs_->GetDisableReasons(extension->id()) &
1842 Extension::DISABLE_SIDELOAD_WIPEOUT));
1843}
1844
[email protected]89226982012-07-16 20:09:181845void ExtensionService::HandleExtensionAlertDetails() {
1846 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301847 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1848 // causes a navigation.
1849 if (extension_error_ui_)
1850 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101851}
1852
[email protected]612a1cb12012-10-17 13:18:031853void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361854 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031855 return;
1856
1857 const Extension* extension = NULL;
1858 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1859 iter != disabled_extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361860 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081861 if (IsUnacknowledgedExternalExtension(e)) {
1862 extension = e;
1863 break;
[email protected]612a1cb12012-10-17 13:18:031864 }
1865 }
1866
1867 if (extension) {
[email protected]d96eb512012-11-01 23:44:081868 if (!extensions::HasExternalInstallError(this)) {
1869 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031870 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081871 // Stop prompting for this extension, and check if there's another
1872 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031873 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081874 UpdateExternalExtensionAlert();
1875 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1876 EXTERNAL_EXTENSION_IGNORED,
1877 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021878 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1879 UMA_HISTOGRAM_ENUMERATION(
1880 "Extensions.ExternalExtensionEventWebstore",
1881 EXTERNAL_EXTENSION_IGNORED,
1882 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1883 } else {
1884 UMA_HISTOGRAM_ENUMERATION(
1885 "Extensions.ExternalExtensionEventNonWebstore",
1886 EXTERNAL_EXTENSION_IGNORED,
1887 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1888 }
[email protected]d96eb512012-11-01 23:44:081889 return;
[email protected]612a1cb12012-10-17 13:18:031890 }
[email protected]b3aa7182013-04-25 04:45:231891 if (is_first_run_)
1892 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1893 // first_run is true if the extension was installed during a first run
1894 // (even if it's post-first run now).
1895 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1896 extension->id());
1897 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031898 }
1899 } else {
1900 extensions::RemoveExternalInstallError(this);
1901 }
[email protected]612a1cb12012-10-17 13:18:031902}
1903
[email protected]a9f39a312010-12-23 22:14:271904void ExtensionService::UnloadExtension(
1905 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591906 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091907 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281908 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021909 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281910 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251911
[email protected]fa0c96732010-11-17 00:14:231912 // This method can be called via PostTask, so the extension may have been
1913 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091914 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171915 // In case the extension may have crashed/uninstalled. Allow the profile to
1916 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081917 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231918 return;
[email protected]dd163fb02011-05-04 22:22:171919 }
[email protected]0c6da502009-08-14 22:32:391920
[email protected]20c8b942013-05-26 02:03:401921 // If uninstalling let RuntimeEventRouter know.
1922 if (reason == extension_misc::UNLOAD_REASON_UNINSTALL)
1923 extensions::RuntimeEventRouter::OnExtensionUninstalled(
1924 profile_, extension_id);
1925
[email protected]1eb175082010-02-10 09:26:161926 // Keep information about the extension so that we can reload it later
1927 // even if it's not permanently installed.
1928 unloaded_extension_paths_[extension->id()] = extension->path();
1929
[email protected]f17dbd42010-08-16 23:21:101930 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031931 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101932
[email protected]d7e9a862010-11-03 21:57:491933 // Clean up runtime data.
1934 extension_runtime_data_.erase(extension_id);
1935
[email protected]060d4972012-07-19 17:22:391936 if (disabled_extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461937 disabled_extensions_.Remove(extension->id());
[email protected]dd163fb02011-05-04 22:22:171938 // Make sure the profile cleans up its RequestContexts when an already
1939 // disabled extension is unloaded (since they are also tracking the disabled
1940 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081941 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]60a174a2013-08-02 20:29:281942 } else {
1943 // Remove the extension from our list.
1944 extensions_.Remove(extension->id());
1945 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391946 }
1947
[email protected]fb789532013-08-27 02:40:211948 content::NotificationService::current()->Notify(
1949 chrome::NOTIFICATION_EXTENSION_REMOVED,
1950 content::Source<Profile>(profile_),
1951 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251952}
1953
[email protected]8b1ec202013-09-05 02:09:501954void ExtensionService::RemoveComponentExtension(
1955 const std::string& extension_id) {
1956 scoped_refptr<const Extension> extension(
1957 GetExtensionById(extension_id, false));
1958 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
1959 content::NotificationService::current()->Notify(
1960 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1961 content::Source<Profile>(profile_),
1962 content::Details<const Extension>(extension.get()));
1963}
1964
[email protected]eaa7dd182010-12-14 11:09:001965void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401966 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171967
[email protected]84df8332011-12-06 18:22:461968 extensions_.Clear();
1969 disabled_extensions_.Clear();
1970 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491971 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321972
[email protected]c6e4a3412009-06-24 15:45:291973 // TODO(erikkay) should there be a notification for this? We can't use
1974 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1975 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321976}
1977
[email protected]eaa7dd182010-12-14 11:09:001978void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321979 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451980 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011981 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351982 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1983 // times.
[email protected]9f1087e2009-06-15 17:29:321984}
1985
[email protected]eaa7dd182010-12-14 11:09:001986void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:101987#if defined(OS_CHROMEOS)
1988 if (disable_garbage_collection_)
1989 return;
1990#endif
1991
[email protected]2f8757c32012-06-19 19:17:471992 if (extension_prefs_->pref_service()->ReadOnly())
1993 return;
1994
[email protected]9bd9a6862012-11-29 09:24:221995 if (pending_extension_manager()->HasPendingExtensions()) {
1996 // Don't garbage collect while there are pending installations, which may
1997 // be using the temporary installation directory. Try to garbage collect
1998 // again later.
[email protected]b3a25092013-05-28 22:08:161999 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:222000 FROM_HERE,
2001 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
2002 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
2003 return;
2004 }
2005
[email protected]45759612012-07-10 17:21:232006 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:472007 extension_prefs_->GetInstalledExtensionsInfo());
2008
[email protected]650b2d52013-02-10 03:41:452009 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:472010 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:052011 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2012 info->at(i)->extension_path));
2013
[email protected]399583b2012-12-11 09:33:422014 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:052015 for (size_t i = 0; i < info->size(); ++i)
2016 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2017 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:472018
[email protected]7f8f24f2012-11-15 19:40:142019 if (!GetFileTaskRunner()->PostTask(
2020 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:472021 base::Bind(
2022 &extension_file_util::GarbageCollectExtensions,
2023 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:402024 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:472025 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402026 }
[email protected]3cf4f0992009-02-03 23:00:302027}
2028
[email protected]fb82dcd2012-03-21 14:15:462029void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]6d147912013-07-09 08:02:142030 if (extensions::sync_helper::IsSyncableApp(&extension)) {
2031 if (app_sync_bundle_.IsSyncing())
2032 app_sync_bundle_.SyncChangeIfNeeded(extension);
2033 else if (is_ready() && !flare_.is_null())
2034 flare_.Run(syncer::APPS);
2035 } else if (extensions::sync_helper::IsSyncableExtension(&extension)) {
2036 if (extension_sync_bundle_.IsSyncing())
2037 extension_sync_bundle_.SyncChangeIfNeeded(extension);
2038 else if (is_ready() && !flare_.is_null())
2039 flare_.Run(syncer::EXTENSIONS);
[email protected]fb82dcd2012-03-21 14:15:462040 }
2041}
2042
[email protected]820d9bd2013-04-03 03:46:032043void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:352044 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:032045 content::NotificationService::current()->Notify(
2046 chrome::NOTIFICATION_EXTENSIONS_READY,
2047 content::Source<Profile>(profile_),
2048 content::NotificationService::NoDetails());
2049}
2050
[email protected]eaa7dd182010-12-14 11:09:002051void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:112052 if (updater_)
[email protected]93fd78f42009-07-10 16:43:172053 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502054
[email protected]bc151cf92013-02-12 04:57:262055 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:512056}
2057
[email protected]fcb58a862012-05-01 01:03:152058void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172059 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2060 // default extension state to DISABLED when the --disable-extensions flag
2061 // is set (http://crbug.com/29067).
2062 if (!extensions_enabled() &&
2063 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:402064 extension->location() != Manifest::COMPONENT &&
2065 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152066 return;
[email protected]330840c2012-08-29 22:21:012067 }
[email protected]c8d407e2011-04-28 21:27:172068
[email protected]b914e2952013-04-26 07:10:032069 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:172070 bool is_extension_installed = false;
2071 const Extension* old = GetInstalledExtension(extension->id());
2072 if (old) {
2073 is_extension_installed = true;
2074 int version_compare_result =
2075 extension->version()->CompareTo(*(old->version()));
2076 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:032077 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2078 // that we aren't downgrading.
2079 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:172080 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:032081 }
2082 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:172083
[email protected]1eb175082010-02-10 09:26:162084 // The extension is now loaded, remove its data from unloaded extension map.
2085 unloaded_extension_paths_.erase(extension->id());
2086
[email protected]bb7f40952011-01-13 00:21:202087 // If a terminated extension is loaded, remove it from the terminated list.
2088 UntrackTerminatedExtension(extension->id());
2089
[email protected]f17dbd42010-08-16 23:21:102090 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:412091 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:102092
[email protected]b914e2952013-04-26 07:10:032093 // Check if the extension's privileges have changed and mark the
2094 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:172095 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:032096
[email protected]116d40e2013-08-08 17:23:172097 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:412098 // To upgrade an extension in place, unload the old one and then load the
2099 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b914e2952013-04-26 07:10:032100 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_UPDATE);
2101 }
[email protected]0c6da502009-08-14 22:32:392102
[email protected]695b5712012-12-06 23:55:282103 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:092104 // Only prefs is checked for the blacklist. We rely on callers to check the
2105 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:072106 // installation then threads through the install and pending install flow
2107 // of this class, and we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:282108 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:412109 } else if (!reloading &&
2110 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482111 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302112 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032113 content::NotificationService::current()->Notify(
2114 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2115 content::Source<Profile>(profile_),
2116 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442117
[email protected]7e9e2422012-12-13 19:54:192118 // Show the extension disabled error if a permissions increase was the
2119 // only reason it was disabled.
2120 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032121 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442122 extensions::AddExtensionDisabledError(this, extension);
2123 }
[email protected]ca2e82022013-04-29 16:48:412124 } else if (reloading) {
2125 // Replace the old extension with the new version.
2126 CHECK(!disabled_extensions_.Insert(extension));
2127 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:282128 } else {
2129 // All apps that are displayed in the launcher are ordered by their ordinals
2130 // so we must ensure they have valid ordinals.
2131 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252132 if (!extension->ShouldDisplayInNewTabPage()) {
2133 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2134 extension->id());
2135 }
[email protected]695b5712012-12-06 23:55:282136 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2137 extension->id(), syncer::StringOrdinal());
2138 }
2139
2140 extensions_.Insert(extension);
2141 SyncExtensionChangeIfNeeded(*extension);
2142 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:312143 }
[email protected]b914e2952013-04-26 07:10:032144 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:352145}
2146
[email protected]8c484b742012-11-29 06:05:362147void ExtensionService::AddComponentExtension(const Extension* extension) {
2148 const std::string old_version_string(
2149 extension_prefs_->GetVersionString(extension->id()));
2150 const Version old_version(old_version_string);
2151
2152 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2153 VLOG(1) << "Component extension " << extension->name() << " ("
2154 << extension->id() << ") installing/upgrading from '"
2155 << old_version_string << "' to " << extension->version()->GetString();
2156
2157 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422158 Extension::ENABLED_COMPONENT,
[email protected]9f3c8532013-07-31 19:52:072159 extensions::Blacklist::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:422160 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362161 return;
2162 }
2163
2164 AddExtension(extension);
2165}
2166
[email protected]b914e2952013-04-26 07:10:032167void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:312168 // If the extension has used the optional permissions API, it will have a
2169 // custom set of active permissions defined in the extension prefs. Here,
2170 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062171 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312172 extension_prefs()->GetActivePermissions(extension->id());
2173
[email protected]dc24976f2013-06-02 21:15:092174 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:312175 // We restrict the active permissions to be within the bounds defined in the
2176 // extension's manifest.
2177 // a) active permissions must be a subset of optional + default permissions
2178 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062179 scoped_refptr<PermissionSet> total_permissions =
2180 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062181 extensions::PermissionsData::GetRequiredPermissions(extension),
2182 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:312183
2184 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062185 scoped_refptr<PermissionSet> adjusted_active =
2186 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312187 total_permissions.get(), active_permissions.get());
2188
2189 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062190 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062191 extensions::PermissionsData::GetRequiredPermissions(extension),
2192 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312193
[email protected]c333e792012-01-06 16:57:392194 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:092195 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312196 }
[email protected]b914e2952013-04-26 07:10:032197}
2198
2199void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:172200 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:032201 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:312202
[email protected]8d888c12010-11-30 00:00:252203 // We keep track of all permissions the user has granted each extension.
2204 // This allows extensions to gracefully support backwards compatibility
2205 // by including unknown permissions in their manifests. When the user
2206 // installs the extension, only the recognized permissions are recorded.
2207 // When the unknown permissions become recognized (e.g., through browser
2208 // upgrade), we can prompt the user to accept these new permissions.
2209 // Extensions can also silently upgrade to less permissions, and then
2210 // silently upgrade to a version that adds these permissions back.
2211 //
2212 // For example, pretend that Chrome 10 includes a permission "omnibox"
2213 // for an API that adds suggestions to the omnibox. An extension can
2214 // maintain backwards compatibility while still having "omnibox" in the
2215 // manifest. If a user installs the extension on Chrome 9, the browser
2216 // will record the permissions it recognized, not including "omnibox."
2217 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2218 // will disable the extension and prompt the user to approve the increase
2219 // in privileges. The extension could then release a new version that
2220 // removes the "omnibox" permission. When the user upgrades, Chrome will
2221 // still remember that "omnibox" had been granted, so that if the
2222 // extension once again includes "omnibox" in an upgrade, the extension
2223 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:282224 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252225
[email protected]211a3f32013-05-28 21:48:182226 bool auto_grant_permission =
[email protected]116d40e2013-08-08 17:23:172227 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]211a3f32013-05-28 21:48:182228 chrome::IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:032229 // Silently grant all active permissions to default apps only on install.
2230 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:182231 // Silently grant all active permissions to apps install in kiosk mode on both
2232 // install and update.
2233 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:032234 GrantPermissions(extension);
2235
2236 bool is_privilege_increase = false;
2237 // We only need to compare the granted permissions to the current permissions
2238 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:052239 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:182240 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:252241 // Add all the recognized permissions if the granted permissions list
2242 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062243 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312244 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522245 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252246
2247 // Here, we check if an extension's privileges have increased in a manner
2248 // that requires the user's approval. This could occur because the browser
2249 // upgraded and recognized additional privileges, or an extension upgrades
2250 // to a version that requires additional privileges.
[email protected]cadac622013-06-11 16:46:362251 is_privilege_increase = granted_permissions->HasLessPrivilegesThan(
[email protected]a0a73052013-07-10 01:42:432252 extension->GetActivePermissions().get(), extension->GetType());
[email protected]8d888c12010-11-30 00:00:252253 }
2254
[email protected]116d40e2013-08-08 17:23:172255 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:032256 // If the extension was already disabled, suppress any alerts for becoming
2257 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:032258 bool previously_disabled =
2259 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:192260 // Legacy disabled extensions do not have a disable reason. Infer that if
2261 // there was no permission increase, it was likely disabled by the user.
2262 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:032263 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:192264 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032265 }
[email protected]7e9e2422012-12-13 19:54:192266 // Extensions that came to us disabled from sync need a similar inference,
2267 // except based on the new version's permissions.
2268 if (previously_disabled &&
2269 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2270 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2271 extension_prefs_->ClearDisableReasons(extension->id());
2272 if (!is_privilege_increase)
2273 disable_reasons |= Extension::DISABLE_USER_ACTION;
2274 }
[email protected]b914e2952013-04-26 07:10:032275 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:252276 }
2277
2278 // Extension has changed permissions significantly. Disable it. A
2279 // notification should be sent by the caller.
2280 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192281 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492282 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2283 RecordPermissionMessagesHistogram(
2284 extension, "Extensions.Permissions_AutoDisable");
2285 }
[email protected]b3317ad2011-04-28 23:46:002286 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252287 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:032288 }
2289 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:282290 extension_prefs_->AddDisableReason(
2291 extension->id(),
2292 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252293 }
2294}
2295
[email protected]eaa7dd182010-12-14 11:09:002296void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392297 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462298 for (ExtensionSet::const_iterator iter = extensions_.begin();
2299 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:362300 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:402301 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462302 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352303 }
2304
[email protected]f6431be82013-09-07 02:53:452305 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252306}
2307
[email protected]9f4e4f082013-06-21 07:11:192308ExtensionService::ImportStatus ExtensionService::SatisfyImports(
2309 const Extension* extension) {
2310 ImportStatus status = IMPORT_STATUS_OK;
2311 std::vector<std::string> pending;
2312 // TODO(elijahtaylor): Message the user if there is a failure that is
2313 // unrecoverable.
2314 if (SharedModuleInfo::ImportsModules(extension)) {
2315 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2316 SharedModuleInfo::GetImports(extension);
2317 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2318 for (i = imports.begin(); i != imports.end(); ++i) {
2319 Version version_required(i->minimum_version);
2320 const Extension* imported_module =
2321 GetExtensionById(i->extension_id, true);
2322 if (!imported_module) {
2323 if (extension->from_webstore()) {
2324 status = IMPORT_STATUS_UNSATISFIED;
2325 pending.push_back(i->extension_id);
2326 } else {
2327 return IMPORT_STATUS_UNRECOVERABLE;
2328 }
2329 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
2330 return IMPORT_STATUS_UNRECOVERABLE;
2331 } else if (version_required.IsValid() &&
2332 imported_module->version()->CompareTo(version_required) < 0) {
2333 if (imported_module->from_webstore()) {
2334 status = IMPORT_STATUS_UNSATISFIED;
2335 } else {
2336 return IMPORT_STATUS_UNRECOVERABLE;
2337 }
2338 }
2339 }
2340 }
2341 if (status == IMPORT_STATUS_UNSATISFIED) {
2342 for (std::vector<std::string>::const_iterator iter = pending.begin();
2343 iter != pending.end();
2344 ++iter) {
2345 pending_extension_manager()->AddFromExtensionImport(
2346 *iter,
2347 extension_urls::GetWebstoreUpdateUrl(),
2348 IsSharedModule);
2349 }
2350 CheckForUpdatesSoon();
2351 }
2352 return status;
2353}
2354
2355scoped_ptr<const ExtensionSet>
2356 ExtensionService::GetDependentExtensions(const Extension* extension) {
2357 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2358 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2359 if (SharedModuleInfo::IsSharedModule(extension)) {
2360 set_to_check->InsertAll(disabled_extensions_);
2361 set_to_check->InsertAll(delayed_installs_);
2362 set_to_check->InsertAll(extensions_);
2363 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2364 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112365 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2366 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192367 dependents->Insert(*iter);
2368 }
2369 }
2370 }
2371 return dependents.PassAs<const ExtensionSet>();
2372}
2373
2374void ExtensionService::PruneSharedModulesOnUninstall(
2375 const Extension* extension) {
2376 if (SharedModuleInfo::ImportsModules(extension)) {
2377 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2378 SharedModuleInfo::GetImports(extension);
2379 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2380 for (i = imports.begin(); i != imports.end(); ++i) {
2381 const Extension* imported_module =
2382 GetExtensionById(i->extension_id, true);
2383 if (imported_module && imported_module->from_webstore()) {
2384 scoped_ptr<const ExtensionSet> dependents =
2385 GetDependentExtensions(imported_module);
2386 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212387 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192388 }
2389 }
2390 }
2391 }
2392}
2393
[email protected]8266d662011-07-12 21:53:262394void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502395 const Extension* extension,
[email protected]98270432012-09-11 20:51:242396 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052397 bool has_requirement_errors,
[email protected]9f3c8532013-07-31 19:52:072398 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052399 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122400 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202401
[email protected]c3cfb012011-04-06 22:07:352402 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032403 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412404 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062405 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402406 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062407 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322408
[email protected]31bb5ee62012-09-12 22:58:402409 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2410 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222411 << " and update URL "
2412 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402413 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542414
[email protected]4416c5a2010-06-26 01:28:572415 // Delete the extension directory since we're not going to
2416 // load it.
[email protected]7f8f24f2012-11-15 19:40:142417 if (!GetFileTaskRunner()->PostTask(
2418 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362419 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402420 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362421 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402422 }
[email protected]4416c5a2010-06-26 01:28:572423 return;
2424 }
[email protected]51a3bf8b2012-06-08 22:53:062425
2426 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572427 } else {
[email protected]c3cfb012011-04-06 22:07:352428 // We explicitly want to re-enable an uninstalled external
2429 // extension; if we're here, that means the user is manually
2430 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332431 if (IsExternalExtensionUninstalled(id)) {
2432 initial_enable = true;
2433 }
[email protected]aa142702010-03-26 01:26:332434 }
2435
[email protected]98270432012-09-11 20:51:242436 // Unsupported requirements overrides the management policy.
2437 if (has_requirement_errors) {
2438 initial_enable = false;
2439 extension_prefs_->AddDisableReason(
2440 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2441 // If the extension was disabled because of unsupported requirements but
2442 // now supports all requirements after an update and there are not other
2443 // disable reasons, enable it.
2444 } else if (extension_prefs_->GetDisableReasons(id) ==
2445 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2446 initial_enable = true;
2447 extension_prefs_->ClearDisableReasons(id);
2448 }
2449
[email protected]9f3c8532013-07-31 19:52:072450 if (blacklist_state == extensions::Blacklist::BLACKLISTED) {
2451 // Installation of a blacklisted extension can happen from sync, policy,
2452 // etc, where to maintain consistency we need to install it, just never
2453 // load it (see AddExtension). Usually it should be the job of callers to
2454 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2455 // showing the install dialogue).
2456 extension_prefs()->AcknowledgeBlacklistedExtension(id);
2457 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2458 extension->location(),
2459 Manifest::NUM_LOCATIONS);
2460 }
2461
[email protected]695b5712012-12-06 23:55:282462 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552463 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2464 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302465 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402466 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552467 RecordPermissionMessagesHistogram(
2468 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302469 } else {
2470 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2471 extension->GetType(), 100);
2472 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402473 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552474 }
2475
[email protected]a39921b42012-02-28 03:42:542476 // Certain extension locations are specific enough that we can
2477 // auto-acknowledge any extension that came from one of them.
[email protected]1d5e58b2013-01-31 08:41:402478 if (extension->location() == Manifest::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542479 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422480 const Extension::State initial_state =
2481 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222482 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072483 extension_prefs_->SetDelayedInstallInfo(
2484 extension,
2485 initial_state,
2486 blacklist_state,
2487 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2488 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052489
2490 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192491 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052492
2493 // Notify extension of available update.
2494 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2495 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452496
2497 // Notify observers that app update is available.
2498 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2499 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052500 return;
2501 }
2502
[email protected]9f4e4f082013-06-21 07:11:192503 ImportStatus status = SatisfyImports(extension);
2504 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072505 extension_prefs_->SetDelayedInstallInfo(
2506 extension,
2507 initial_state,
2508 blacklist_state,
2509 extensions::ExtensionPrefs::DELAY_REASON_GC,
2510 page_ordinal);
[email protected]399583b2012-12-11 09:33:422511 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192512 } else if (status != IMPORT_STATUS_OK) {
2513 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072514 extension_prefs_->SetDelayedInstallInfo(
2515 extension,
2516 initial_state,
2517 blacklist_state,
[email protected]9f4e4f082013-06-21 07:11:192518 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2519 page_ordinal);
2520 delayed_installs_.Insert(extension);
2521 }
[email protected]399583b2012-12-11 09:33:422522 } else {
[email protected]9f3c8532013-07-31 19:52:072523 AddNewOrUpdatedExtension(extension,
2524 initial_state,
2525 blacklist_state,
2526 page_ordinal);
[email protected]399583b2012-12-11 09:33:422527 }
[email protected]8c484b742012-11-29 06:05:362528}
2529
2530void ExtensionService::AddNewOrUpdatedExtension(
2531 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422532 Extension::State initial_state,
[email protected]9f3c8532013-07-31 19:52:072533 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422534 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362535 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9f3c8532013-07-31 19:52:072536 extension_prefs_->OnExtensionInstalled(extension,
2537 initial_state,
2538 blacklist_state,
2539 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122540 delayed_installs_.Remove(extension->id());
[email protected]6f6101832012-11-27 22:10:482541 FinishInstallation(extension);
2542}
2543
2544void ExtensionService::MaybeFinishDelayedInstallation(
2545 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192546 // Check if the extension already got installed.
2547 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482548 return;
[email protected]9f4e4f082013-06-21 07:11:192549 extensions::ExtensionPrefs::DelayReason reason =
2550 extension_prefs_->GetDelayedInstallReason(extension_id);
2551
2552 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2553 // preferences files that will not have set this field but it was previously
2554 // only used for idle updates.
2555 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2556 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
2557 is_ready() && !IsExtensionIdle(extension_id))
[email protected]6f6101832012-11-27 22:10:482558 return;
2559
[email protected]9f4e4f082013-06-21 07:11:192560 const Extension* extension = delayed_installs_.GetByID(extension_id);
2561 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2562 ImportStatus status = SatisfyImports(extension);
2563 if (status != IMPORT_STATUS_OK) {
2564 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2565 delayed_installs_.Remove(extension_id);
2566 // Make sure no version of the extension is actually installed, (i.e.,
2567 // that this delayed install was not an update).
2568 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2569 extension_prefs_->DeleteExtensionPrefs(extension_id);
2570 }
2571 return;
2572 }
2573 }
2574
[email protected]6f6101832012-11-27 22:10:482575 FinishDelayedInstallation(extension_id);
2576}
2577
2578void ExtensionService::FinishDelayedInstallation(
2579 const std::string& extension_id) {
2580 scoped_refptr<const Extension> extension(
2581 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092582 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192583 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482584
[email protected]399583b2012-12-11 09:33:422585 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482586 NOTREACHED();
2587
[email protected]dc24976f2013-06-02 21:15:092588 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482589}
2590
2591void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562592 const extensions::Extension* existing_extension =
2593 GetInstalledExtension(extension->id());
2594 bool is_update = false;
2595 std::string old_name;
2596 if (existing_extension) {
2597 is_update = true;
2598 old_name = existing_extension->name();
2599 }
2600 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152601 content::NotificationService::current()->Notify(
2602 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2603 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022604 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152605
[email protected]d96eb512012-11-01 23:44:082606 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362607
[email protected]399583b2012-12-11 09:33:422608 // Unpacked extensions default to allowing file access, but if that has been
2609 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402610 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422611 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2612 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2613 }
2614
[email protected]6f6101832012-11-27 22:10:482615 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032616
2617 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082618 // so he can reenable it. We do this last so that it has already been
2619 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162620 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032621 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082622 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2623 EXTERNAL_EXTENSION_INSTALLED,
2624 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022625 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2626 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2627 EXTERNAL_EXTENSION_INSTALLED,
2628 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2629 } else {
2630 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2631 EXTERNAL_EXTENSION_INSTALLED,
2632 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2633 }
[email protected]d96eb512012-11-01 23:44:082634 }
[email protected]9f4e4f082013-06-21 07:11:192635
2636 // Check extensions that may have been delayed only because this shared module
2637 // was not available.
2638 if (SharedModuleInfo::IsSharedModule(extension)) {
2639 MaybeFinishDelayedInstallations();
2640 }
[email protected]4a190632009-05-09 01:07:422641}
2642
[email protected]76b65442012-11-17 14:11:482643const Extension* ExtensionService::GetPendingExtensionUpdate(
2644 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192645 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482646}
2647
[email protected]bb7f40952011-01-13 00:21:202648void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462649 if (!terminated_extensions_.Contains(extension->id()))
2650 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202651
[email protected]b3f7fe22011-11-11 19:27:562652 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202653}
2654
2655void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202656 std::string lowercase_id = StringToLowerASCII(id);
[email protected]60a174a2013-08-02 20:29:282657 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
[email protected]84df8332011-12-06 18:22:462658 terminated_extensions_.Remove(lowercase_id);
[email protected]fb789532013-08-27 02:40:212659 if (extension) {
2660 content::NotificationService::current()->Notify(
2661 chrome::NOTIFICATION_EXTENSION_REMOVED,
2662 content::Source<Profile>(profile_),
2663 content::Details<const Extension>(extension));
2664 }
[email protected]bb7f40952011-01-13 00:21:202665}
2666
[email protected]0dfe05c2011-02-23 23:03:362667const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472668 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282669 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472670}
2671
2672const Extension* ExtensionService::GetInstalledExtension(
2673 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282674 int include_mask = INCLUDE_ENABLED |
2675 INCLUDE_DISABLED |
2676 INCLUDE_TERMINATED |
2677 INCLUDE_BLACKLISTED;
2678 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362679}
2680
[email protected]eaa7dd182010-12-14 11:09:002681bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442682 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]be9915fb2013-07-18 09:28:552683 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442684 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402685 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392686}
2687
[email protected]4d007b312012-10-17 03:00:482688bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]be9915fb2013-07-18 09:28:552689 const Extension* extension = extensions_.GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482690 if (extension && extension->is_platform_app()) {
2691 *url = GURL(chrome::kExtensionInvalidRequestURL);
2692 return true;
2693 }
2694
2695 return false;
2696}
2697
[email protected]9060d8b02012-01-13 02:14:302698bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322699 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142700 const Version* version,
[email protected]650b2d52013-02-10 03:41:452701 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402702 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512703 int creation_flags,
2704 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382705 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2706 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012707 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302708 return false;
[email protected]a8af9fdb2010-10-28 21:52:202709
[email protected]7577a5c52009-07-30 06:21:582710 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492711 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582712 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022713 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352714
[email protected]7577a5c52009-07-30 06:21:582715 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352716 // The default apps will have the location set as INTERNAL. Since older
2717 // default apps are installed as EXTERNAL, we override them. However, if the
2718 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402719 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352720 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402721 (location == Manifest::INTERNAL &&
2722 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352723
2724 if (!is_default_apps_migration) {
2725 DCHECK(version);
2726
2727 switch (existing->version()->CompareTo(*version)) {
2728 case -1: // existing version is older, we should upgrade
2729 break;
2730 case 0: // existing version is same, do nothing
2731 return false;
2732 case 1: // existing version is newer, uh-oh
2733 LOG(WARNING) << "Found external version of extension " << id
2734 << "that is older than current version. Current version "
2735 << "is: " << existing->VersionString() << ". New "
2736 << "version is: " << version->GetString()
2737 << ". Keeping current version.";
2738 return false;
2739 }
[email protected]7577a5c52009-07-30 06:21:582740 }
2741 }
2742
[email protected]9060d8b02012-01-13 02:14:302743 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302744 if (!pending_extension_manager()->AddFromExternalFile(
2745 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302746 return false;
[email protected]e3987852012-05-04 10:06:302747 }
[email protected]9c635f22010-12-02 09:36:362748
[email protected]14908b72011-04-20 06:54:362749 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372750 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162751 installer->set_install_source(location);
2752 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072753 installer->set_expected_version(*version);
2754 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312755 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182756#if defined(OS_CHROMEOS)
2757 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2758#else
[email protected]6dfbbf82010-03-12 23:09:162759 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182760#endif
[email protected]47fc70c2011-12-06 07:29:512761
2762 // Depending on the source, a new external extension might not need a user
2763 // notification on installation. For such extensions, mark them acknowledged
2764 // now to suppress the notification.
2765 if (mark_acknowledged)
2766 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302767
2768 return true;
[email protected]7577a5c52009-07-30 06:21:582769}
2770
[email protected]eaa7dd182010-12-14 11:09:002771void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452772 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402773 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402774 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012775 content::NotificationService::current()->Notify(
2776 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2777 content::Source<Profile>(profile_),
2778 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402779
[email protected]8a205c02011-02-04 20:41:332780 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212781 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042782 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492783 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402784 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2785}
2786
[email protected]eaa7dd182010-12-14 11:09:002787void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132788 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182789 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332790 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182791 if (iter == orphaned_dev_tools_.end())
2792 return;
2793
[email protected]04ea1bb2013-07-10 09:26:092794 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182795 orphaned_dev_tools_.erase(iter);
2796}
2797
[email protected]432115822011-07-10 15:52:272798void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532799 const content::NotificationSource& source,
2800 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272801 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542802 case chrome::NOTIFICATION_APP_TERMINATING:
2803 // Shutdown has started. Don't start any more extension installs.
2804 // (We cannot use ExtensionService::Shutdown() for this because it
2805 // happens too late in browser teardown.)
2806 browser_terminating_ = true;
2807 break;
[email protected]432115822011-07-10 15:52:272808 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532809 if (profile_ !=
2810 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322811 break;
[email protected]6c2381d2011-10-19 02:52:532812 }
[email protected]a4ed6282009-12-14 20:51:162813
[email protected]3a1dc572012-07-31 22:25:132814 extensions::ExtensionHost* host =
2815 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262816
[email protected]fa2416f2011-05-03 08:41:202817 // Mark the extension as terminated and Unload it. We want it to
2818 // be in a consistent state: either fully working or not loaded
2819 // at all, but never half-crashed. We do it in a PostTask so
2820 // that other handlers of this notification will still have
2821 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162822 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362823 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362824 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202825 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012826 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202827 host->extension()));
[email protected]31f77262009-12-02 20:48:532828 break;
2829 }
[email protected]432115822011-07-10 15:52:272830 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302831 content::RenderProcessHost* process =
2832 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192833 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302834 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192835 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2836 break;
2837
[email protected]15397b42012-05-16 23:56:062838 // Extensions need to know the channel for API restrictions.
2839 process->Send(new ExtensionMsg_SetChannel(
[email protected]15d4d2d2013-08-09 06:49:032840 extensions::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062841
[email protected]4941fe92013-06-13 23:21:012842 // Platform apps need to know the system font.
2843 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2844 webui::SetFontAndTextDirection(fonts.get());
2845 std::string font_family, font_size;
2846 fonts->GetString("fontfamily", &font_family);
2847 fonts->GetString("fontsize", &font_size);
2848 process->Send(new ExtensionMsg_SetSystemFont(
2849 font_family, font_size));
2850
[email protected]77a6970c2011-04-23 16:58:562851 // Valid extension function names, used to setup bindings in renderer.
2852 std::vector<std::string> function_names;
2853 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2854 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532855
[email protected]77a6970c2011-04-23 16:58:562856 // Scripting whitelist. This is modified by tests and must be communicated
2857 // to renderers.
2858 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2859 *Extension::GetScriptingWhitelist()));
2860
2861 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532862 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462863 for (ExtensionSet::const_iterator iter = extensions_.begin();
2864 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492865 // Renderers don't need to know about themes.
2866 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362867 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562868 }
[email protected]9776e82e2011-11-15 02:17:532869 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562870 break;
2871 }
[email protected]432115822011-07-10 15:52:272872 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302873 content::RenderProcessHost* process =
2874 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192875 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302876 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192877 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2878 break;
2879
[email protected]6bc04fd82011-12-04 02:29:352880 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462881 BrowserThread::PostTask(
2882 BrowserThread::IO, FROM_HERE,
2883 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082884 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302885 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212886 break;
2887 }
[email protected]0db124b02012-11-07 04:55:052888 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2889 extensions::ExtensionHost* host =
2890 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222891 std::string extension_id = host->extension_id();
[email protected]9f4e4f082013-06-21 07:11:192892 if (delayed_installs_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222893 // We were waiting for this extension to become idle, it now might have,
2894 // so maybe finish installation.
[email protected]b3a25092013-05-28 22:08:162895 base::MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052896 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482897 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052898 AsWeakPtr(), extension_id),
2899 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052900 }
2901 break;
2902 }
[email protected]92dd8d92013-02-26 00:41:082903 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2904 // Notify extensions that chrome update is available.
2905 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2906 profile_);
[email protected]75bdcb872013-03-13 00:41:452907
2908 // Notify observers that chrome update is available.
2909 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2910 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082911 break;
2912 }
[email protected]aa96d3a2010-08-21 08:45:252913
[email protected]4814b512009-11-07 00:12:292914 default:
2915 NOTREACHED() << "Unexpected notification type.";
2916 }
2917}
2918
[email protected]90bb38d2012-11-14 18:36:032919void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182920 IdentifyAlertableExtensions();
2921 CheckManagementPolicy();
2922}
2923
[email protected]eaa7dd182010-12-14 11:09:002924bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182925 return !GetAppIds().empty();
2926}
[email protected]377011d2010-07-20 04:18:502927
[email protected]eaa7dd182010-12-14 11:09:002928ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182929 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462930 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502931 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402932 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182933 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502934 }
2935
[email protected]ec5b50d2010-10-09 16:35:182936 return result;
[email protected]377011d2010-07-20 04:18:502937}
[email protected]d7e9a862010-11-03 21:57:492938
[email protected]dc9a74f72012-08-17 18:07:212939bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292940 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212941 return true;
2942 ExtensionRuntimeDataMap::const_iterator it =
2943 extension_runtime_data_.find(extension->id());
2944 return it == extension_runtime_data_.end() ? false :
2945 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492946}
2947
[email protected]eaa7dd182010-12-14 11:09:002948void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292949 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492950 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072951 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272952 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532953 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072954 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492955}
2956
[email protected]dc9a74f72012-08-17 18:07:212957bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2958 ExtensionRuntimeDataMap::const_iterator it =
2959 extension_runtime_data_.find(extension->id());
2960 return it == extension_runtime_data_.end() ? false :
2961 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492962}
2963
[email protected]eaa7dd182010-12-14 11:09:002964void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032965 bool value) {
[email protected]d7e9a862010-11-03 21:57:492966 extension_runtime_data_[extension->id()].being_upgraded = value;
2967}
[email protected]1bead0712010-11-27 17:41:532968
[email protected]e178ad92013-06-28 02:29:252969bool ExtensionService::IsBeingReloaded(
2970 const std::string& extension_id) const {
2971 return ContainsKey(extensions_being_reloaded_, extension_id);
2972}
2973
2974void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432975 bool isBeingReloaded) {
2976 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252977 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432978 else
[email protected]e178ad92013-06-28 02:29:252979 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252980}
2981
[email protected]dc9a74f72012-08-17 18:07:212982bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2983 ExtensionRuntimeDataMap::const_iterator it =
2984 extension_runtime_data_.find(extension->id());
2985 return it == extension_runtime_data_.end() ? false :
2986 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072987}
2988
2989void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2990 bool value) {
2991 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2992}
2993
[email protected]612a1cb12012-10-17 13:18:032994bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2995 // Extensions installed by policy can't be disabled. So even if a previous
2996 // installation disabled the extension, make sure it is now enabled.
2997 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2998 return true;
2999
3000 if (extension_prefs_->IsExtensionDisabled(extension->id()))
3001 return false;
3002
[email protected]41070e8d2012-10-24 01:34:363003 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:033004 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:543005 // enabling them. Hosted apps are excepted because they are not dangerous
3006 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:403007 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
3008 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:033009 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
3010 return false;
3011 }
3012 }
[email protected]612a1cb12012-10-17 13:18:033013
3014 return true;
3015}
[email protected]0db124b02012-11-07 04:55:053016
3017bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:473018 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:333019 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:053020 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:473021 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223022 if (host)
3023 return false;
3024 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
3025}
3026
3027bool ExtensionService::ShouldDelayExtensionUpdate(
3028 const std::string& extension_id,
3029 bool wait_for_idle) const {
3030 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
3031
3032 // If delayed updates are globally disabled, or just for this extension,
3033 // don't delay.
3034 if (!install_updates_when_idle_ || !wait_for_idle)
3035 return false;
3036
[email protected]695b5712012-12-06 23:55:283037 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223038 // If there is no old extension, this is not an update, so don't delay.
3039 if (!old)
3040 return false;
3041
[email protected]9367eabc2013-03-01 01:29:293042 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:223043 // Delay installation if the extension listens for the onUpdateAvailable
3044 // event.
3045 return system_->event_router()->ExtensionHasEventListener(
3046 extension_id, kOnUpdateAvailableEvent);
3047 } else {
3048 // Delay installation if the extension is not idle.
3049 return !IsExtensionIdle(extension_id);
3050 }
[email protected]0db124b02012-11-07 04:55:053051}
[email protected]fdd679b2012-11-15 20:49:393052
[email protected]399583b2012-12-11 09:33:423053void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:453054 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
3055 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:423056 for (ExtensionSet::const_iterator it = extensions_.begin();
3057 it != extensions_.end(); ++it) {
[email protected]cadac622013-06-11 16:46:363058 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
3059 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
3060 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:423061 }
3062 }
3063
[email protected]9f4e4f082013-06-21 07:11:193064 DCHECK(!installs_delayed_for_gc());
3065 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:423066 BrowserContext::GarbageCollectStoragePartitions(
3067 profile_, active_paths.Pass(),
3068 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3069 AsWeakPtr()));
3070}
3071
3072void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:193073 set_installs_delayed_for_gc(false);
3074 MaybeFinishDelayedInstallations();
3075}
3076
3077void ExtensionService::MaybeFinishDelayedInstallations() {
3078 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:423079 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3080 it != delayed_installs_.end();
3081 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193082 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:423083 }
[email protected]9f4e4f082013-06-21 07:11:193084 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
3085 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:423086 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193087 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:423088 }
[email protected]399583b2012-12-11 09:33:423089}
3090
3091void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3092 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3093}
3094
[email protected]fdd679b2012-11-15 20:49:393095void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283096 blacklist_->GetBlacklistedIDs(
3097 GenerateInstalledExtensionsSet()->GetIDs(),
3098 base::Bind(&ExtensionService::ManageBlacklist,
3099 AsWeakPtr(),
3100 blacklisted_extensions_.GetIDs()));
3101}
3102
3103void ExtensionService::ManageBlacklist(
3104 const std::set<std::string>& old_blacklisted_ids,
3105 const std::set<std::string>& new_blacklisted_ids) {
3106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3107
[email protected]16bf7ba72013-08-23 11:52:543108 std::set<std::string> no_longer_blacklisted =
3109 base::STLSetDifference<std::set<std::string> >(old_blacklisted_ids,
3110 new_blacklisted_ids);
3111 std::set<std::string> not_yet_blacklisted =
3112 base::STLSetDifference<std::set<std::string> >(new_blacklisted_ids,
3113 old_blacklisted_ids);
[email protected]695b5712012-12-06 23:55:283114
3115 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3116 it != no_longer_blacklisted.end(); ++it) {
3117 scoped_refptr<const Extension> extension =
3118 blacklisted_extensions_.GetByID(*it);
[email protected]9f3c8532013-07-31 19:52:073119 DCHECK(extension.get()) << "Extension " << *it << " no longer blacklisted, "
3120 << "but it was never blacklisted.";
[email protected]dc24976f2013-06-02 21:15:093121 if (!extension.get())
[email protected]695b5712012-12-06 23:55:283122 continue;
3123 blacklisted_extensions_.Remove(*it);
[email protected]dc24976f2013-06-02 21:15:093124 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:093125 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:093126 extension->location(),
3127 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283128 }
3129
3130 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3131 it != not_yet_blacklisted.end(); ++it) {
3132 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]9f3c8532013-07-31 19:52:073133 DCHECK(extension.get()) << "Extension " << *it << " needs to be "
3134 << "blacklisted, but it's not installed.";
[email protected]dc24976f2013-06-02 21:15:093135 if (!extension.get())
[email protected]695b5712012-12-06 23:55:283136 continue;
3137 blacklisted_extensions_.Insert(extension);
3138 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:093139 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
3140 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283141 }
3142
3143 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393144}
[email protected]75bdcb872013-03-13 00:41:453145
3146void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
3147 update_observers_.AddObserver(observer);
3148}
3149
3150void ExtensionService::RemoveUpdateObserver(
3151 extensions::UpdateObserver* observer) {
3152 update_observers_.RemoveObserver(observer);
3153}