blob: 7c9ee4991ee51371739c8bf0df1b24486cd1d4d1 [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]70019152012-12-19 11:44:1932#include "chrome/browser/devtools/devtools_window.h"
[email protected]32b36c62012-10-24 05:37:4133#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1234#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4435#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]c39df1a2013-01-08 18:22:0736#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3137#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]4636c832013-01-11 02:10:1138#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]5db9ada2012-04-11 13:48:2039#include "chrome/browser/extensions/app_sync_data.h"
[email protected]5a38dfd2012-07-23 23:22:1040#include "chrome/browser/extensions/browser_event_router.h"
[email protected]d8c8f25f2011-11-02 18:18:0141#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1742#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3843#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4444#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2445#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1846#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5647#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2248#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2949#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2350#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4351#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0452#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0853#include "chrome/browser/extensions/extension_system.h"
[email protected]612a1cb12012-10-17 13:18:0354#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2755#include "chrome/browser/extensions/external_provider_impl.h"
56#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0157#include "chrome/browser/extensions/installed_loader.h"
[email protected]6cafe35a2012-08-27 22:40:5058#include "chrome/browser/extensions/lazy_background_task_queue.h"
[email protected]fdd679b2012-11-15 20:49:3959#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3760#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3961#include "chrome/browser/extensions/permissions_updater.h"
[email protected]d8c8f25f2011-11-02 18:18:0162#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]75bdcb872013-03-13 00:41:4563#include "chrome/browser/extensions/update_observer.h"
[email protected]42a08162012-03-16 18:09:1164#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3365#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3966#include "chrome/browser/profiles/profile_manager.h"
[email protected]c8d407e2011-04-28 21:27:1767#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3168#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1569#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5470#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0671#include "chrome/common/chrome_version_info.h"
[email protected]f6431be82013-09-07 02:53:4572#include "chrome/common/crash_keys.h"
[email protected]9367eabc2013-03-01 01:29:2973#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:5874#include "chrome/common/extensions/extension.h"
[email protected]9f4e4f082013-06-21 07:11:1975#include "chrome/common/extensions/extension_constants.h"
[email protected]7c927b62010-02-24 09:54:1376#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1277#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5678#include "chrome/common/extensions/extension_messages.h"
[email protected]612a1cb12012-10-17 13:18:0379#include "chrome/common/extensions/feature_switch.h"
[email protected]15d4d2d2013-08-09 06:49:0380#include "chrome/common/extensions/features/feature_channel.h"
[email protected]06492ed2013-03-24 22:13:1481#include "chrome/common/extensions/incognito_handler.h"
[email protected]561e33d52013-04-03 06:58:4382#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]6b414c232013-06-05 07:53:3483#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]9f4e4f082013-06-21 07:11:1984#include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
[email protected]65348062013-01-15 07:27:2285#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]2cb5e302013-05-09 05:00:0686#include "chrome/common/extensions/permissions/permissions_data.h"
[email protected]8f3bcbd2013-06-05 08:42:4087#include "chrome/common/extensions/sync_helper.h"
[email protected]25b34332009-06-05 21:53:1988#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1489#include "chrome/common/url_constants.h"
[email protected]7a83dd072013-09-03 12:09:1790#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]c38831a12011-10-28 12:44:4991#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3692#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3793#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1694#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3095#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4296#include "content/public/browser/site_instance.h"
97#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:5398#include "content/public/browser/url_data_source.h"
[email protected]836e2982013-05-16 08:07:4299#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:31100#include "extensions/common/error_utils.h"
[email protected]d42c11152013-08-22 19:36:32101#include "extensions/common/manifest.h"
[email protected]65187152012-06-02 13:14:14102#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25103#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12104#include "sync/api/sync_change.h"
105#include "sync/api/sync_error_factory.h"
[email protected]4941fe92013-06-13 23:21:01106#include "ui/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:00107#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38108#include "webkit/browser/database/database_tracker.h"
109#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18110
[email protected]eed367e2011-04-12 03:43:31111#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18112#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32113#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07114#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31115#endif
116
[email protected]55eb70e762012-02-20 17:38:39117using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39118using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36119using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27120using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34121using extensions::Extension;
122using extensions::ExtensionIdSet;
123using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42124using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40125using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06126using extensions::PermissionMessage;
127using extensions::PermissionMessages;
128using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19129using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24130using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05131
[email protected]c6d474f82009-12-16 21:11:06132namespace errors = extension_manifest_errors;
133
[email protected]b6ab96d2009-08-20 18:58:19134namespace {
135
[email protected]d96eb512012-11-01 23:44:08136// Histogram values for logging events related to externally installed
137// extensions.
138enum ExternalExtensionEvent {
139 EXTERNAL_EXTENSION_INSTALLED = 0,
140 EXTERNAL_EXTENSION_IGNORED,
141 EXTERNAL_EXTENSION_REENABLED,
142 EXTERNAL_EXTENSION_UNINSTALLED,
143 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
144};
145
[email protected]612a1cb12012-10-17 13:18:03146// Prompt the user this many times before considering an extension acknowledged.
147static const int kMaxExtensionAcknowledgePromptCount = 3;
148
[email protected]0db124b02012-11-07 04:55:05149// Wait this many seconds after an extensions becomes idle before updating it.
150static const int kUpdateIdleDelay = 5;
151
[email protected]9bd9a6862012-11-29 09:24:22152// Wait this many seconds before trying to garbage collect extensions again.
153static const int kGarbageCollectRetryDelay = 30;
154
[email protected]36fc54f2013-06-14 02:49:34155// Wait this many seconds after startup to see if there are any extensions
156// which can be garbage collected.
157static const int kGarbageCollectStartupDelay = 30;
158
[email protected]9f4e4f082013-06-21 07:11:19159static bool IsSharedModule(const Extension* extension) {
160 return SharedModuleInfo::IsSharedModule(extension);
161}
162
[email protected]4c9201c42013-08-16 04:56:21163static bool IsCWSSharedModule(const Extension* extension) {
164 return extension->from_webstore() && IsSharedModule(extension);
165}
166
167class SharedModuleProvider : public extensions::ManagementPolicy::Provider {
168 public:
169 SharedModuleProvider() {}
170 virtual ~SharedModuleProvider() {}
171
172 virtual std::string GetDebugPolicyProviderName() const OVERRIDE {
173 return "SharedModuleProvider";
174 }
175
176 virtual bool UserMayModifySettings(const Extension* extension,
177 string16* error) const OVERRIDE {
178 return !IsCWSSharedModule(extension);
179 }
180
181 virtual bool MustRemainEnabled(const Extension* extension,
182 string16* error) const OVERRIDE {
183 return IsCWSSharedModule(extension);
184 }
185
186 private:
187 DISALLOW_COPY_AND_ASSIGN(SharedModuleProvider);
188};
189
190
[email protected]c6d474f82009-12-16 21:11:06191} // namespace
[email protected]b6ab96d2009-08-20 18:58:19192
[email protected]eaa7dd182010-12-14 11:09:00193ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49194 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57195 being_upgraded(false),
196 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49197}
198
[email protected]eaa7dd182010-12-14 11:09:00199ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49200}
201
[email protected]eaa7dd182010-12-14 11:09:00202// ExtensionService.
[email protected]6014d672008-12-05 00:38:25203
[email protected]31bb5ee62012-09-12 22:58:40204const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24205 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40206const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24207 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40208const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24209 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40210const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24211 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29212const char ExtensionService::kManagedSettingsDirectoryName[] =
213 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40214const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]a690e292012-12-19 19:22:49215const char ExtensionService::kRulesStoreName[] = "Extension Rules";
[email protected]494c06e2009-07-25 01:06:42216
[email protected]8e4560b62011-01-14 10:09:14217void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12218 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20219
[email protected]7a4c6852010-09-16 03:44:22220 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27221 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21222 for (i = external_extension_providers_.begin();
223 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14224 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21225 if (i->get()->HasExtension(id))
226 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22227 }
228
[email protected]0f48fca2011-05-19 18:46:35229 // We get the list of external extensions to check from preferences.
230 // It is possible that an extension has preferences but is not loaded.
231 // For example, an extension that requires experimental permissions
232 // will not be loaded if the experimental command line flag is not used.
233 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40234 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35235 // We can't call UninstallExtension with an unloaded/invalid
236 // extension ID.
237 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
238 << "with id: " << id;
239 return;
240 }
[email protected]d6ebc9792011-04-07 18:18:33241 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22242}
243
[email protected]7f8f24f2012-11-15 19:40:14244void ExtensionService::SetFileTaskRunnerForTesting(
245 base::SequencedTaskRunner* task_runner) {
246 file_task_runner_ = task_runner;
247}
248
[email protected]8e4560b62011-01-14 10:09:14249void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22250 external_extension_providers_.clear();
251}
252
[email protected]8e4560b62011-01-14 10:09:14253void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27254 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12255 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21256 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27257 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22258}
259
[email protected]9060d8b02012-01-13 02:14:30260bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22261 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42262 const GURL& update_url,
[email protected]1d5e58b2013-01-31 08:41:40263 Manifest::Location location) {
[email protected]ab22ba42011-01-14 16:36:38264 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
265 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20266
[email protected]8a87a5332011-08-11 17:54:59267 const Extension* extension = GetExtensionById(id, true);
268 if (extension) {
269 // Already installed. Skip this install if the current location has
270 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40271 Manifest::Location current = extension->location();
272 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30273 return false;
[email protected]8a87a5332011-08-11 17:54:59274 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22275 }
[email protected]9060d8b02012-01-13 02:14:30276
277 // Add |id| to the set of pending extensions. If it can not be added,
278 // then there is already a pending record from a higher-priority install
279 // source. In this case, signal that this extension will not be
280 // installed by returning false.
281 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40282 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30283 return false;
[email protected]31bb5ee62012-09-12 22:58:40284 }
[email protected]9060d8b02012-01-13 02:14:30285
[email protected]94fde232012-04-27 10:22:30286 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30287 return true;
[email protected]7a4c6852010-09-16 03:44:22288}
289
[email protected]dc9a74f72012-08-17 18:07:21290const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]be9915fb2013-07-18 09:28:55291 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]dc9a74f72012-08-17 18:07:21292 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09293}
294
[email protected]dc9a74f72012-08-17 18:07:21295bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09296 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27297}
298
[email protected]404fb002012-10-02 21:06:17299const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21300 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17301 std::set<std::string> extension_ids =
302 process_map_.GetExtensionsInProcess(renderer_child_id);
303 // All apps in one process share the same partition.
304 // It is only possible for the app to have isolated storage
305 // if there is only 1 app in the process.
306 if (extension_ids.size() != 1)
307 return NULL;
308
309 const extensions::Extension* extension =
310 extensions_.GetByID(*(extension_ids.begin()));
[email protected]561e33d52013-04-03 06:58:43311 // We still need to check if the extension has isolated storage,
[email protected]404fb002012-10-02 21:06:17312 // because it's common for there to be one extension in a process
[email protected]561e33d52013-04-03 06:58:43313 // without isolated storage.
314 if (extension &&
315 extensions::AppIsolationInfo::HasIsolatedStorage(extension))
[email protected]404fb002012-10-02 21:06:17316 return extension;
317
318 return NULL;
[email protected]da5683db2011-04-23 17:12:21319}
320
[email protected]6aeac8342010-10-01 20:21:18321// static
[email protected]d6ebc9792011-04-07 18:18:33322// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30323// --uninstall-extension, and to uninstall an extension via sync. The LOG
324// statements within this function are used to inform the user if the uninstall
325// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00326bool ExtensionService::UninstallExtensionHelper(
327 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18328 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33329 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40330 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18331 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33332 << "id: " << extension_id;
333 return false;
[email protected]6aeac8342010-10-01 20:21:18334 }
335
[email protected]d6ebc9792011-04-07 18:18:33336 // The following call to UninstallExtension will not allow an uninstall of a
337 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14338 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33339 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
340 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
341 << ": " << error;
342 return false;
343 }
[email protected]95da88c42011-03-31 10:07:33344
[email protected]6aeac8342010-10-01 20:21:18345 return true;
346}
347
[email protected]eaa7dd182010-12-14 11:09:00348ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24349 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45350 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23351 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39352 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03353 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35354 bool extensions_enabled,
355 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39356 : extensions::Blacklist::Observer(blacklist),
357 profile_(profile),
[email protected]bd306722012-07-11 20:43:59358 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43359 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28360 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38361 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]9c009092013-05-01 03:14:09362 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23363 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03364 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13365 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33366 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35367 ready_(ready),
[email protected]9c009092013-05-01 03:14:09368 toolbar_model_(this),
[email protected]8b280302011-10-13 22:22:23369 menu_manager_(profile),
[email protected]7c1490da2011-10-11 18:53:25370 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18371 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54372 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19373 installs_delayed_for_gc_(false),
[email protected]460c6712013-04-24 07:20:01374 is_first_run_(false),
[email protected]9c009092013-05-01 03:14:09375 app_sync_bundle_(this),
376 extension_sync_bundle_(this) {
[email protected]c04fd3dd2013-09-05 08:20:10377#if defined(OS_CHROMEOS)
378 disable_garbage_collection_ = false;
379#endif
[email protected]a29a517a2011-01-21 21:11:12380 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20381
[email protected]36a784c2009-06-23 06:21:08382 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40383 if (command_line->HasSwitch(switches::kDisableExtensions) ||
384 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23385 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18386 }
[email protected]36a784c2009-06-23 06:21:08387
[email protected]3c4abc82012-10-22 22:25:54388 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
389 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27390 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07391 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27392 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07393 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27394 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07395 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05396 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
397 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08398 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
399 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28400 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03401 base::Closure callback =
402 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
403 base::Unretained(this));
404 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
405 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24406 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29407
[email protected]93fd78f42009-07-10 16:43:17408 // Set up the ExtensionUpdater
409 if (autoupdate_enabled) {
410 int update_frequency = kDefaultUpdateFrequencySeconds;
411 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25412 base::StringToInt(command_line->GetSwitchValueASCII(
413 switches::kExtensionsUpdateFrequency),
414 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17415 }
[email protected]42a08162012-03-16 18:09:11416 updater_.reset(new extensions::ExtensionUpdater(this,
417 extension_prefs,
418 profile->GetPrefs(),
419 profile,
[email protected]fdd679b2012-11-15 20:49:39420 blacklist,
[email protected]42a08162012-03-16 18:09:11421 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17422 }
423
[email protected]25ae0152011-11-18 14:40:02424 component_loader_.reset(
425 new extensions::ComponentLoader(this,
426 profile->GetPrefs(),
427 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14428
[email protected]0436b102011-04-15 18:30:03429 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25430 extensions::ExternalProviderImpl::CreateExternalProviders(
431 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05432 }
[email protected]b671760b2010-07-15 21:13:47433
[email protected]fb82dcd2012-03-21 14:15:46434 // Set this as the ExtensionService for extension sorting to ensure it
435 // cause syncs if required.
436 extension_prefs_->extension_sorting()->SetExtensionService(this);
437
[email protected]b3aa7182013-04-25 04:45:23438 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
439
[email protected]9ce11d22012-08-08 23:20:13440#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58441 extension_action_storage_manager_.reset(
442 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13443#endif
[email protected]c77f2352012-08-08 22:07:58444
[email protected]4c9201c42013-08-16 04:56:21445 shared_module_policy_provider_.reset(new SharedModuleProvider);
446
[email protected]cb0e50312011-05-09 15:03:07447 // How long is the path to the Extensions directory?
448 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
449 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25450}
451
[email protected]84df8332011-12-06 18:22:46452const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45453 return &extensions_;
454}
455
[email protected]84df8332011-12-06 18:22:46456const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45457 return &disabled_extensions_;
458}
459
[email protected]84df8332011-12-06 18:22:46460const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20461 return &terminated_extensions_;
462}
463
[email protected]695b5712012-12-06 23:55:28464const ExtensionSet* ExtensionService::blacklisted_extensions() const {
465 return &blacklisted_extensions_;
466}
467
[email protected]9e9c1d12013-07-31 01:58:12468const ExtensionSet* ExtensionService::delayed_installs() const {
469 return &delayed_installs_;
470}
471
[email protected]695b5712012-12-06 23:55:28472scoped_ptr<const ExtensionSet>
473 ExtensionService::GenerateInstalledExtensionsSet() const {
474 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01475 installed_extensions->InsertAll(extensions_);
476 installed_extensions->InsertAll(disabled_extensions_);
477 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28478 installed_extensions->InsertAll(blacklisted_extensions_);
479 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01480}
481
[email protected]3f213ad2012-07-26 23:39:41482extensions::PendingExtensionManager*
483 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37484 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45485}
486
[email protected]eaa7dd182010-12-14 11:09:00487ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17488 // No need to unload extensions here because they are profile-scoped, and the
489 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17490
[email protected]5df038b2012-07-16 19:03:27491 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14492 for (i = external_extension_providers_.begin();
493 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27494 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14495 provider->ServiceShutdown();
496 }
[email protected]6014d672008-12-05 00:38:25497}
498
[email protected]6d147912013-07-09 08:02:14499void ExtensionService::SetSyncStartFlare(
500 const syncer::SyncableService::StartSyncFlare& flare) {
501 flare_ = flare;
502}
503
[email protected]eaa7dd182010-12-14 11:09:00504void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18505 if (event_routers_initialized_)
506 return;
507
[email protected]6a3cd37e2012-04-17 17:13:34508#if defined(ENABLE_EXTENSIONS)
[email protected]5a38dfd2012-07-23 23:22:10509 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]6a3cd37e2012-04-17 17:13:34510#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18511 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37512}
513
[email protected]037228a2012-10-05 01:36:16514void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21515 system_->management_policy()->UnregisterProvider(
516 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16517}
518
[email protected]b2907fd2011-03-25 16:43:37519const Extension* ExtensionService::GetExtensionById(
520 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47521 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28522 if (include_disabled) {
523 // Include blacklisted extensions here because there are hundreds of
524 // callers of this function, and many might assume that this includes those
525 // that have been disabled due to blacklisting.
526 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
527 }
528 return GetExtensionById(id, include_mask);
529}
530
[email protected]399583b2012-12-11 09:33:42531GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
532 return content::SiteInstance::GetSiteForURL(
533 profile_,
534 Extension::GetBaseURLFromExtensionId(extension_id));
535}
536
[email protected]695b5712012-12-06 23:55:28537const Extension* ExtensionService::GetExtensionById(
538 const std::string& id, int include_mask) const {
539 std::string lowercase_id = StringToLowerASCII(id);
540 if (include_mask & INCLUDE_ENABLED) {
541 const Extension* extension = extensions_.GetByID(lowercase_id);
542 if (extension)
543 return extension;
544 }
545 if (include_mask & INCLUDE_DISABLED) {
546 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
547 if (extension)
548 return extension;
549 }
550 if (include_mask & INCLUDE_TERMINATED) {
551 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
552 if (extension)
553 return extension;
554 }
555 if (include_mask & INCLUDE_BLACKLISTED) {
556 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
557 if (extension)
558 return extension;
559 }
560 return NULL;
[email protected]78994ab02010-12-08 18:06:44561}
562
[email protected]eaa7dd182010-12-14 11:09:00563void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12564 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20565
[email protected]4a10006a2013-05-17 23:18:35566 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17567 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32568
[email protected]820d9bd2013-04-03 03:46:03569 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03570 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
571 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
572 // The sole purpose of this launch is to install a new extension from CWS
573 // and immediately terminate: loading already installed extensions is
574 // unnecessary and may interfere with the inline install dialog (e.g. if an
575 // extension listens to onStartup and opens a window).
576 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25577 } else {
[email protected]47b60072013-06-20 10:35:56578 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
579 component_loader_->LoadAll();
580 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19581
[email protected]2edd57852013-08-14 22:07:40582 // Attempt to re-enable extensions whose only disable reason is reloading.
583 std::vector<std::string> extensions_to_enable;
584 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
585 iter != disabled_extensions_.end(); ++iter) {
586 const Extension* e = iter->get();
587 if (extension_prefs_->GetDisableReasons(e->id()) ==
588 Extension::DISABLE_RELOAD) {
589 extensions_to_enable.push_back(e->id());
590 }
591 }
592 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
593 it != extensions_to_enable.end(); ++it) {
594 EnableExtension(*it);
595 }
596
[email protected]9f4e4f082013-06-21 07:11:19597 // Finish install (if possible) of extensions that were still delayed while
598 // the browser was shut down.
599 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
600 extension_prefs_->GetAllDelayedInstallInfo());
601 for (size_t i = 0; i < delayed_info->size(); ++i) {
602 ExtensionInfo* info = delayed_info->at(i).get();
603 scoped_refptr<const Extension> extension(NULL);
604 if (info->extension_manifest) {
605 std::string error;
606 extension = Extension::Create(
607 info->extension_path,
608 info->extension_location,
609 *info->extension_manifest,
610 extension_prefs_->GetDelayedInstallCreationFlags(
611 info->extension_id),
612 info->extension_id,
613 &error);
614 if (extension.get())
615 delayed_installs_.Insert(extension);
616 }
617 }
618 MaybeFinishDelayedInstallations();
619
620 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
621 extension_prefs_->GetAllDelayedInstallInfo());
622 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
623 delayed_info2->size() - delayed_info->size());
624
[email protected]47b60072013-06-20 10:35:56625 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25626
[email protected]47b60072013-06-20 10:35:56627 // TODO(erikkay) this should probably be deferred to a future point
628 // rather than running immediately at startup.
629 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57630
[email protected]47b60072013-06-20 10:35:56631 base::MessageLoop::current()->PostDelayedTask(
632 FROM_HERE,
633 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
634 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42635
[email protected]820d9bd2013-04-03 03:46:03636 if (extension_prefs_->NeedsStorageGarbageCollection()) {
637 GarbageCollectIsolatedStorage();
638 extension_prefs_->SetNeedsStorageGarbageCollection(false);
639 }
[email protected]4c9201c42013-08-16 04:56:21640 system_->management_policy()->RegisterProvider(
641 shared_module_policy_provider_.get());
[email protected]399583b2012-12-11 09:33:42642 }
[email protected]6014d672008-12-05 00:38:25643}
644
[email protected]31bb5ee62012-09-12 22:58:40645bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45646 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40647 const GURL& download_url,
648 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12649 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54650 if (browser_terminating_) {
651 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
652 // Leak the temp file at extension_path. We don't want to add to the disk
653 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
654 // the file is in the OS temp directory which should be cleaned up for us.
655 return false;
656 }
[email protected]a8af9fdb2010-10-28 21:52:20657
[email protected]3f213ad2012-07-26 23:39:41658 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06659 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32660
[email protected]695b5712012-12-06 23:55:28661 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06662 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33663 LOG(WARNING) << "Will not update extension " << id
664 << " because it is not installed or pending";
665 // Delete extension_path since we're not creating a CrxInstaller
666 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14667 if (!GetFileTaskRunner()->PostTask(
668 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36669 base::Bind(
[email protected]7296f2762011-11-21 19:23:44670 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36671 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03672
673 return false;
[email protected]e957fe52009-06-23 16:51:05674 }
675
[email protected]aa142702010-03-26 01:26:33676 // We want a silent install only for non-pending extensions and
677 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37678 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05679 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37680 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33681
[email protected]f8636f92013-08-09 21:02:37682 scoped_refptr<CrxInstaller> installer(
683 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16684 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06685 if (pending_extension_info) {
686 installer->set_install_source(pending_extension_info->install_source());
687 if (pending_extension_info->install_silently())
688 installer->set_allow_silent_install(true);
689 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41690 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06691 }
[email protected]7d8b7072012-04-07 01:07:46692 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45693 // its auto-update URL is from the webstore, treat it as a webstore install.
694 // Note that we ignore some older extensions with blank auto-update URLs
695 // because we are mostly concerned with restrictions on NaCl extensions,
696 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53697 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28698 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38699 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35700 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06701 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53702 creation_flags |= Extension::FROM_WEBSTORE;
703 }
704
705 // Bookmark apps being updated is kind of a contradiction, but that's because
706 // we mark the default apps as bookmark apps, and they're hosted in the web
707 // store, thus they can get updated. See http://crbug.com/101605 for more
708 // details.
709 if (extension && extension->from_bookmark())
710 creation_flags |= Extension::FROM_BOOKMARK;
711
[email protected]e33bbc22012-08-27 22:05:46712 if (extension && extension->was_installed_by_default())
713 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
714
[email protected]a12ce8b22012-01-17 18:40:53715 installer->set_creation_flags(creation_flags);
716
[email protected]6dfbbf82010-03-12 23:09:16717 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06718 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07719 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16720 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03721
722 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09723 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03724
725 return true;
[email protected]e957fe52009-06-23 16:51:05726}
727
[email protected]e178ad92013-06-28 02:29:25728void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12729 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05730
731 // If the extension is already reloading, don't reload again.
732 if (extension_prefs_->GetDisableReasons(extension_id) &
733 Extension::DISABLE_RELOAD) {
734 return;
735 }
736
[email protected]650b2d52013-02-10 03:41:45737 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02738 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40739
[email protected]f17dbd42010-08-16 23:21:10740 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06741 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29742 // If the extension has an inspector open for its background page, detach
743 // the inspector and hang onto a cookie for it, so that we can reattach
744 // later.
[email protected]31d8f5f22012-04-02 15:22:08745 // TODO(yoz): this is not incognito-safe!
746 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13747 extensions::ExtensionHost* host =
748 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36749 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29750 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09751 scoped_refptr<DevToolsAgentHost> agent_host =
752 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
753 agent_host->DisconnectRenderViewHost();
754 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29755 }
756
[email protected]b65272f2009-08-31 15:47:06757 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50758 // BeingUpgraded is set back to false when the extension is added.
759 SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03760 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03761 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16762 } else {
763 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06764 }
765
[email protected]9f4e4f082013-06-21 07:11:19766 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48767 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05768 return;
769 }
770
[email protected]43ceb002012-02-10 23:19:15771 // If we're reloading a component extension, use the component extension
772 // loader's reloader.
773 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25774 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15775 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25776 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15777 return;
778 }
779
[email protected]e6090e42010-03-23 22:44:08780 // Check the installed extensions to see if what we're reloading was already
781 // installed.
[email protected]e178ad92013-06-28 02:29:25782 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08783 scoped_ptr<ExtensionInfo> installed_extension(
784 extension_prefs_->GetInstalledExtensionInfo(extension_id));
785 if (installed_extension.get() &&
786 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01787 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08788 } else {
[email protected]d8c8f25f2011-11-02 18:18:01789 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08790 // We should always be able to remember the extension's path. If it's not in
791 // the map, someone failed to update |unloaded_extension_paths_|.
792 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01793 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08794 }
[email protected]e178ad92013-06-28 02:29:25795 // When reloading is done, mark this extension as done reloading.
796 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40797}
798
[email protected]fa2416f2011-05-03 08:41:20799bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08800 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20801 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14802 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12803 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20804
[email protected]0d6ec3a72011-09-02 02:09:43805 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25806
[email protected]e7afe2452010-08-22 16:19:13807 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09808 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32809
[email protected]95da88c42011-03-31 10:07:33810 // Policy change which triggers an uninstall will always set
811 // |external_uninstall| to true so this is the only way to uninstall
812 // managed extensions.
[email protected]65187152012-06-02 13:14:14813 if (!external_uninstall &&
814 !system_->management_policy()->UserMayModifySettings(
815 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07816 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27817 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53818 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09819 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33820 return false;
821 }
[email protected]95da88c42011-03-31 10:07:33822
[email protected]3bdba0d2011-08-23 07:17:30823 // Extract the data we need for sync now, but don't actually sync until we've
824 // completed the uninstallation.
[email protected]6d147912013-07-09 08:02:14825 // TODO(tim): If we get here and IsSyncing is false, this will cause
826 // "back from the dead" style bugs, because sync will add-back the extension
827 // that was uninstalled here when MergeDataAndStartSyncing is called.
828 // See crbug.com/256795.
[email protected]65f173552012-06-28 22:43:58829 syncer::SyncChange sync_change;
[email protected]6d147912013-07-09 08:02:14830 if (extensions::sync_helper::IsSyncableApp(extension.get())) {
831 if (app_sync_bundle_.IsSyncing())
832 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension.get());
833 else if (is_ready() && !flare_.is_null())
834 flare_.Run(syncer::APPS); // Tell sync to start ASAP.
835 } else if (extensions::sync_helper::IsSyncableExtension(extension.get())) {
836 if (extension_sync_bundle_.IsSyncing())
837 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
838 else if (is_ready() && !flare_.is_null())
839 flare_.Run(syncer::EXTENSIONS); // Tell sync to start ASAP.
[email protected]3bdba0d2011-08-23 07:17:30840 }
841
[email protected]dc24976f2013-06-02 21:15:09842 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08843 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
844 EXTERNAL_EXTENSION_UNINSTALLED,
845 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02846 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
847 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
848 EXTERNAL_EXTENSION_UNINSTALLED,
849 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
850 } else {
851 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
852 EXTERNAL_EXTENSION_UNINSTALLED,
853 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
854 }
[email protected]d96eb512012-11-01 23:44:08855 }
[email protected]9b217652010-10-08 22:04:23856 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08857 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09858 RecordPermissionMessagesHistogram(extension.get(),
859 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23860
[email protected]831aa212010-03-26 13:55:19861 // Unload before doing more cleanup to ensure that nothing is hanging on to
862 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59863 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19864
[email protected]0d6ec3a72011-09-02 02:09:43865 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19866 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32867
868 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05869 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14870 if (!GetFileTaskRunner()->PostTask(
871 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36872 base::Bind(
[email protected]14908b72011-04-20 06:54:36873 &extension_file_util::UninstallExtension,
874 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20875 extension_id)))
[email protected]14908b72011-04-20 06:54:36876 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32877 }
878
[email protected]6b414c232013-06-05 07:53:34879 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36880 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43881 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09882 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43883
[email protected]399583b2012-12-11 09:33:42884 if (is_storage_isolated) {
885 BrowserContext::AsyncObliterateStoragePartition(
886 profile_,
887 GetSiteForExtensionId(extension_id),
888 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
889 AsWeakPtr()));
890 } else {
891 if (extension->is_hosted_app() &&
892 !profile_->GetExtensionSpecialStoragePolicy()->
893 IsStorageProtected(launch_web_url_origin)) {
894 extensions::DataDeleter::StartDeleting(
895 profile_, extension_id, launch_web_url_origin);
896 }
897 extensions::DataDeleter::StartDeleting(profile_, extension_id,
898 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43899 }
[email protected]0d6ec3a72011-09-02 02:09:43900
[email protected]0dfe05c2011-02-23 23:03:36901 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06902
903 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07904 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27905 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53906 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09907 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33908
[email protected]5db9ada2012-04-11 13:48:20909 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32910 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20911 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
912 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32913 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20914 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30915 }
916
[email protected]399583b2012-12-11 09:33:42917 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05918
[email protected]198b5902013-06-27 10:36:11919 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19920
[email protected]333b1de2011-09-12 18:28:50921 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18922 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
923
[email protected]d6ebc9792011-04-07 18:18:33924 return true;
[email protected]c10da4b02010-03-25 14:38:32925}
926
[email protected]c3cfb012011-04-06 22:07:35927bool ExtensionService::IsExtensionEnabled(
928 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46929 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21930 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18931 return true;
[email protected]dc9a74f72012-08-17 18:07:21932 }
[email protected]36429da2011-07-11 20:25:18933
[email protected]695b5712012-12-06 23:55:28934 if (disabled_extensions_.Contains(extension_id) ||
935 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25936 return false;
[email protected]695b5712012-12-06 23:55:28937 }
[email protected]ad83ca242011-07-29 01:32:25938
939 // If the extension hasn't been loaded yet, check the prefs for it. Assume
940 // enabled unless otherwise noted.
941 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31942 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35943}
944
945bool ExtensionService::IsExternalExtensionUninstalled(
946 const std::string& extension_id) const {
947 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
948}
949
[email protected]e2377032013-01-18 17:53:22950bool ExtensionService::IsExtensionEnabledForLauncher(
951 const std::string& extension_id) const {
952 return IsExtensionEnabled(extension_id) &&
953 !GetTerminatedExtension(extension_id);
954}
955
[email protected]eaa7dd182010-12-14 11:09:00956void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12957 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20958
[email protected]06f92562011-04-29 19:27:31959 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39960 return;
[email protected]0c6da502009-08-14 22:32:39961
[email protected]b3317ad2011-04-28 23:46:00962 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28963 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52964
[email protected]695b5712012-12-06 23:55:28965 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31966 // This can happen if sync enables an extension that is not
967 // installed yet.
968 if (!extension)
969 return;
970
[email protected]d96eb512012-11-01 23:44:08971 if (IsUnacknowledgedExternalExtension(extension)) {
972 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
973 EXTERNAL_EXTENSION_REENABLED,
974 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02975 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
976 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
977 EXTERNAL_EXTENSION_REENABLED,
978 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
979 } else {
980 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
981 EXTERNAL_EXTENSION_REENABLED,
982 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
983 }
[email protected]612a1cb12012-10-17 13:18:03984 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08985 }
[email protected]612a1cb12012-10-17 13:18:03986
[email protected]0c6da502009-08-14 22:32:39987 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46988 extensions_.Insert(make_scoped_refptr(extension));
989 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39990
[email protected]62d30f42009-10-01 22:36:06991 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30992
[email protected]f74d7f32012-06-19 19:22:51993 // Notify listeners that the extension was enabled.
994 content::NotificationService::current()->Notify(
995 chrome::NOTIFICATION_EXTENSION_ENABLED,
996 content::Source<Profile>(profile_),
997 content::Details<const Extension>(extension));
998
[email protected]3bdba0d2011-08-23 07:17:30999 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:391000}
1001
[email protected]44d62b62012-04-11 00:06:031002void ExtensionService::DisableExtension(
1003 const std::string& extension_id,
1004 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:121005 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201006
[email protected]b2ba9962009-12-10 20:10:151007 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:311008 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:521009 return;
[email protected]1784e83a2009-09-08 21:01:521010
[email protected]06f92562011-04-29 19:27:311011 const Extension* extension = GetInstalledExtension(extension_id);
1012 // |extension| can be NULL if sync disables an extension that is not
1013 // installed yet.
[email protected]65187152012-06-02 13:14:141014 if (extension &&
[email protected]505ccdc2013-06-20 20:15:351015 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:141016 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:331017 return;
[email protected]65187152012-06-02 13:14:141018 }
[email protected]95da88c42011-03-31 10:07:331019
[email protected]b3317ad2011-04-28 23:46:001020 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:281021 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:521022
[email protected]695b5712012-12-06 23:55:281023 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
1024 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:311025 if (!extension)
1026 return;
1027
[email protected]64742de32013-04-22 08:44:341028 // Reset the background_page_ready flag
1029 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
1030 extension_runtime_data_[extension->id()].background_page_ready = false;
1031
[email protected]5c094792012-09-07 19:44:531032 // Move it over to the disabled list. Don't send a second unload notification
1033 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:461034 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:531035 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461036 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:531037 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
1038 } else {
[email protected]84df8332011-12-06 18:22:461039 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:531040 }
[email protected]3bdba0d2011-08-23 07:17:301041
1042 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:521043}
1044
[email protected]1abf05e2013-07-09 17:04:361045void ExtensionService::DisableUserExtensions(
1046 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:141047 extensions::ManagementPolicy* management_policy =
1048 system_->management_policy();
1049 extensions::ExtensionList to_disable;
1050
1051 for (ExtensionSet::const_iterator extension = extensions_.begin();
1052 extension != extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111053 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141054 to_disable.push_back(*extension);
1055 }
1056 for (ExtensionSet::const_iterator extension = terminated_extensions_.begin();
1057 extension != terminated_extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111058 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141059 to_disable.push_back(*extension);
1060 }
1061
1062 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1063 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381064 if ((*extension)->was_installed_by_default() &&
1065 extension_urls::IsWebstoreUpdateUrl(
1066 extensions::ManifestURL::GetUpdateURL(*extension)))
1067 continue;
[email protected]1abf05e2013-07-09 17:04:361068 const std::string& id = (*extension)->id();
1069 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1070 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141071 }
1072}
1073
[email protected]eaa7dd182010-12-14 11:09:001074void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281075 const Extension* extension) {
1076 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491077 RecordPermissionMessagesHistogram(
1078 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251079 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1080 EnableExtension(extension->id());
1081}
1082
[email protected]009633c2013-03-07 22:08:281083void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451084 CHECK(extension);
1085 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281086 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451087}
1088
[email protected]fe2dd7742011-04-19 22:52:491089// static
1090void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051091 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171092 // Since this is called from multiple sources, and since the histogram macros
1093 // use statics, we need to manually lookup the histogram ourselves.
1094 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491095 histogram,
1096 1,
[email protected]c2e66e12012-06-27 06:27:061097 PermissionMessage::kEnumBoundary,
1098 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171099 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491100
[email protected]13c68b62013-05-17 11:29:051101 PermissionMessages permissions =
1102 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491103 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061104 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491105 } else {
[email protected]c2e66e12012-06-27 06:27:061106 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521107 it != permissions.end(); ++it)
1108 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491109 }
1110}
1111
[email protected]eaa7dd182010-12-14 11:09:001112void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421113 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061114 // was loaded, otherwise a race can arise where a renderer that is created
1115 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421116 // that the request context doesn't yet know about. The profile is responsible
1117 // for ensuring its URLRequestContexts appropriately discover the loaded
1118 // extension.
[email protected]31d8f5f22012-04-02 15:22:081119 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061120
[email protected]d5949312012-12-03 22:13:301121 // Tell renderers about the new extension, unless it's a theme (renderers
1122 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191123 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491124 for (content::RenderProcessHost::iterator i(
1125 content::RenderProcessHost::AllHostsIterator());
1126 !i.IsAtEnd(); i.Advance()) {
1127 content::RenderProcessHost* host = i.GetCurrentValue();
1128 Profile* host_profile =
1129 Profile::FromBrowserContext(host->GetBrowserContext());
1130 if (host_profile->GetOriginalProfile() ==
1131 profile_->GetOriginalProfile()) {
1132 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1133 1, ExtensionMsg_Loaded_Params(extension));
1134 host->Send(
1135 new ExtensionMsg_Loaded(loaded_extensions));
1136 }
[email protected]c8d407e2011-04-28 21:27:171137 }
[email protected]77a6970c2011-04-23 16:58:561138 }
1139
[email protected]3442a662012-01-12 08:06:171140 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1141 // extension.
1142 //
1143 // NOTE: It is important that this happen after notifying the renderers about
1144 // the new extensions so that if we navigate to an extension URL in
1145 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1146 content::NotificationService::current()->Notify(
1147 chrome::NOTIFICATION_EXTENSION_LOADED,
1148 content::Source<Profile>(profile_),
1149 content::Details<const Extension>(extension));
1150
[email protected]c8d407e2011-04-28 21:27:171151 // Tell a random-ass collection of other subsystems about the new extension.
1152 // TODO(aa): What should we do with all this goop? Can it move into the
1153 // relevant objects via EXTENSION_LOADED?
1154
1155 profile_->GetExtensionSpecialStoragePolicy()->
1156 GrantRightsForExtension(extension);
1157
1158 UpdateActiveExtensionsInCrashReporter();
1159
[email protected]c8d407e2011-04-28 21:27:171160 // If the extension has permission to load chrome://favicon/ resources we need
1161 // to make sure that the FaviconSource is registered with the
1162 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051163 if (extensions::PermissionsData::HasHostPermission(
1164 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171165 FaviconSource* favicon_source = new FaviconSource(profile_,
1166 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531167 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171168 }
[email protected]b07e606e2012-09-15 20:16:151169
1170#if !defined(OS_ANDROID)
1171 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051172 if (extensions::PermissionsData::HasHostPermission(
1173 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151174 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531175 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151176 }
1177#endif
1178
[email protected]5eddc3e2011-10-26 04:33:311179 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051180 if (extensions::PermissionsData::HasHostPermission(
1181 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]5eddc3e2011-10-26 04:33:311182 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531183 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311184 }
[email protected]62d30f42009-10-01 22:36:061185}
1186
[email protected]a9f39a312010-12-23 22:14:271187void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591188 const Extension* extension,
1189 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271190 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071191 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271192 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531193 content::Source<Profile>(profile_),
1194 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061195
[email protected]f3b1a082011-11-18 00:34:301196 for (content::RenderProcessHost::iterator i(
1197 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561198 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301199 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081200 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301201 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081202 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171203 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561204 }
1205
[email protected]31d8f5f22012-04-02 15:22:081206 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171207 profile_->GetExtensionSpecialStoragePolicy()->
1208 RevokeRightsForExtension(extension);
1209
[email protected]b777b332011-04-16 04:01:081210#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231211 // Revoke external file access for the extension from its file system context.
1212 // It is safe to access the extension's storage partition at this point. The
1213 // storage partition may get destroyed only after the extension gets unloaded.
1214 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1215 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091216 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231217 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091218 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321219 if (filesystem_context && filesystem_context->external_backend()) {
1220 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171221 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061222 }
[email protected]c8d407e2011-04-28 21:27:171223#endif
1224
1225 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061226}
1227
[email protected]0a071a32011-02-08 00:18:241228Profile* ExtensionService::profile() {
1229 return profile_;
1230}
1231
[email protected]45759612012-07-10 17:21:231232extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451233 return extension_prefs_;
1234}
1235
[email protected]0d9a2202011-11-09 13:48:411236extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381237 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201238}
1239
[email protected]e2d720aa2012-05-17 00:11:351240extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441241 return extension_prefs()->content_settings_store();
1242}
1243
[email protected]25ae0152011-11-18 14:40:021244bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351245 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021246}
1247
[email protected]7f8f24f2012-11-15 19:40:141248base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091249 if (file_task_runner_.get())
1250 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141251
1252 // We should be able to interrupt any part of extension install process during
1253 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1254 // will be ignored/deleted while we will block on started tasks.
1255 std::string token("ext_install-");
1256 token.append(profile_->GetPath().AsUTF8Unsafe());
1257 file_task_runner_ = BrowserThread::GetBlockingPool()->
1258 GetSequencedTaskRunnerWithShutdownBehavior(
1259 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1260 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091261 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141262}
1263
[email protected]42a08162012-03-16 18:09:111264extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061265 return updater_.get();
1266}
1267
[email protected]4ee07c62012-08-21 12:40:421268void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251269 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281270
[email protected]aa96d3a2010-08-21 08:45:251271 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461272 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251273 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361274 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421275 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251276 to_be_removed.push_back(extension->id());
1277 }
1278
1279 // UnloadExtension will change the extensions_ list. So, we should
1280 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401281 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591282 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251283}
1284
[email protected]31206602011-04-13 23:07:321285void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351286 if (updater()) {
[email protected]94fde232012-04-27 10:22:301287 if (AreAllExternalProvidersReady()) {
1288 updater()->CheckSoon();
1289 } else {
1290 // Sync can start updating before all the external providers are ready
1291 // during startup. Start the update as soon as those providers are ready,
1292 // but not before.
1293 update_once_all_providers_are_ready_ = true;
1294 }
[email protected]c3cfb012011-04-06 22:07:351295 } else {
[email protected]31206602011-04-13 23:07:321296 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351297 }
1298}
1299
[email protected]3f98fd92012-11-17 15:54:291300syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321301 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581302 const syncer::SyncDataList& initial_sync_data,
1303 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1304 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201305 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281306 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301307
1308 switch (type) {
[email protected]a4a147652012-07-03 23:41:321309 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201310 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281311 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201312 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301313 break;
1314
[email protected]a4a147652012-07-03 23:41:321315 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281316 app_sync_bundle_.SetupSync(sync_processor.release(),
1317 sync_error_factory.release(),
1318 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301319 break;
1320
1321 default:
1322 LOG(FATAL) << "Got " << type << " ModelType";
1323 }
[email protected]3bdba0d2011-08-23 07:17:301324
[email protected]b1417ab2011-12-15 17:09:111325 // Process local extensions.
1326 // TODO(yoz): Determine whether pending extensions should be considered too.
1327 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581328 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1329 syncer::SyncChangeList sync_change_list;
1330 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301331 i != sync_data_list.end();
1332 ++i) {
[email protected]5db9ada2012-04-11 13:48:201333 switch (type) {
[email protected]a4a147652012-07-03 23:41:321334 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201335 sync_change_list.push_back(
1336 extension_sync_bundle_.CreateSyncChange(*i));
1337 break;
[email protected]a4a147652012-07-03 23:41:321338 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201339 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1340 break;
1341 default:
1342 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401343 }
[email protected]3bdba0d2011-08-23 07:17:301344 }
[email protected]3bdba0d2011-08-23 07:17:301345
[email protected]5db9ada2012-04-11 13:48:201346
[email protected]a4a147652012-07-03 23:41:321347 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201348 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321349 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201350 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1351 }
[email protected]0ffd9e52012-01-26 21:44:341352
[email protected]3f98fd92012-11-17 15:54:291353 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301354}
1355
[email protected]a4a147652012-07-03 23:41:321356void ExtensionService::StopSyncing(syncer::ModelType type) {
1357 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201358 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321359 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201360 extension_sync_bundle_.Reset();
1361 }
[email protected]3bdba0d2011-08-23 07:17:301362}
1363
[email protected]65f173552012-06-28 22:43:581364syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321365 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401366 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201367 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401368 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201369 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201370
1371 // We should only get sync data for extensions and apps.
1372 NOTREACHED();
1373
[email protected]65f173552012-06-28 22:43:581374 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301375}
1376
[email protected]65f173552012-06-28 22:43:581377syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301378 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581379 const syncer::SyncChangeList& change_list) {
1380 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301381 i != change_list.end();
1382 ++i) {
[email protected]a4a147652012-07-03 23:41:321383 syncer::ModelType type = i->sync_data().GetDataType();
1384 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201385 extension_sync_bundle_.ProcessSyncChange(
1386 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321387 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201388 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1389 }
[email protected]3bdba0d2011-08-23 07:17:301390 }
1391
[email protected]0ffd9e52012-01-26 21:44:341392 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1393
[email protected]65f173552012-06-28 22:43:581394 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561395}
1396
[email protected]5db9ada2012-04-11 13:48:201397extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1398 const Extension& extension) const {
1399 return extensions::ExtensionSyncData(extension,
1400 IsExtensionEnabled(extension.id()),
1401 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561402}
1403
[email protected]5db9ada2012-04-11 13:48:201404extensions::AppSyncData ExtensionService::GetAppSyncData(
1405 const Extension& extension) const {
1406 return extensions::AppSyncData(
1407 extension,
1408 IsExtensionEnabled(extension.id()),
1409 IsIncognitoEnabled(extension.id()),
[email protected]5db9ada2012-04-11 13:48:201410 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1411 extension.id()),
1412 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1413}
[email protected]3bdba0d2011-08-23 07:17:301414
[email protected]5db9ada2012-04-11 13:48:201415std::vector<extensions::ExtensionSyncData>
1416 ExtensionService::GetExtensionSyncDataList() const {
1417 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1418 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1419 &extension_sync_list);
1420 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1421 &extension_sync_list);
1422 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1423 &extension_sync_list);
1424
1425 std::vector<extensions::ExtensionSyncData> pending_extensions =
1426 extension_sync_bundle_.GetPendingData();
1427 extension_sync_list.insert(extension_sync_list.begin(),
1428 pending_extensions.begin(),
1429 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301430
1431 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561432}
1433
[email protected]5db9ada2012-04-11 13:48:201434std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1435 const {
1436 std::vector<extensions::AppSyncData> app_sync_list;
1437 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1438 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1439 &app_sync_list);
1440 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1441 &app_sync_list);
1442
1443 std::vector<extensions::AppSyncData> pending_apps =
1444 app_sync_bundle_.GetPendingData();
1445 app_sync_list.insert(app_sync_list.begin(),
1446 pending_apps.begin(),
1447 pending_apps.end());
1448
1449 return app_sync_list;
1450}
1451
1452bool ExtensionService::ProcessExtensionSyncData(
1453 const extensions::ExtensionSyncData& extension_sync_data) {
1454 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321455 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201456 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1457 extension_sync_data);
1458 CheckForUpdatesSoon();
1459 return false;
1460 }
1461
1462 return true;
1463}
1464
1465bool ExtensionService::ProcessAppSyncData(
1466 const extensions::AppSyncData& app_sync_data) {
1467 const std::string& id = app_sync_data.id();
[email protected]5db9ada2012-04-11 13:48:201468
1469 if (app_sync_data.app_launch_ordinal().IsValid() &&
1470 app_sync_data.page_ordinal().IsValid()) {
1471 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1472 id,
1473 app_sync_data.app_launch_ordinal());
1474 extension_prefs_->extension_sorting()->SetPageOrdinal(
1475 id,
1476 app_sync_data.page_ordinal());
1477 }
1478
[email protected]5db9ada2012-04-11 13:48:201479 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321480 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201481 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1482 CheckForUpdatesSoon();
1483 return false;
1484 }
1485
1486 return true;
1487}
1488
1489bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321490 syncer::ModelType type) const {
1491 if (type == syncer::EXTENSIONS &&
[email protected]8f3bcbd2013-06-05 08:42:401492 extensions::sync_helper::IsSyncableExtension(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201493 return true;
[email protected]31bb5ee62012-09-12 22:58:401494 }
[email protected]5db9ada2012-04-11 13:48:201495
[email protected]a4a147652012-07-03 23:41:321496 if (type == syncer::APPS &&
[email protected]8f3bcbd2013-06-05 08:42:401497 extensions::sync_helper::IsSyncableApp(&extension)) {
[email protected]5db9ada2012-04-11 13:48:201498 return true;
[email protected]31bb5ee62012-09-12 22:58:401499 }
[email protected]5db9ada2012-04-11 13:48:201500
1501 return false;
1502}
1503
1504bool ExtensionService::ProcessExtensionSyncDataHelper(
1505 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321506 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301507 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061508 const Extension* extension = GetInstalledExtension(id);
1509
1510 // TODO(bolms): we should really handle this better. The particularly bad
1511 // case is where an app becomes an extension or vice versa, and we end up with
1512 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201513 if (extension && !IsCorrectSyncType(*extension, type))
1514 return true;
[email protected]90310d92011-04-17 07:35:041515
1516 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301517 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041518 if (!UninstallExtensionHelper(this, id)) {
1519 LOG(WARNING) << "Could not uninstall extension " << id
1520 << " for sync";
1521 }
[email protected]5db9ada2012-04-11 13:48:201522 return true;
[email protected]90310d92011-04-17 07:35:041523 }
1524
[email protected]caf2d442012-05-04 10:00:501525 // Extension from sync was uninstalled by the user as external extensions.
1526 // Honor user choice and skip installation/enabling.
1527 if (IsExternalExtensionUninstalled(id)) {
1528 LOG(WARNING) << "Extension with id " << id
1529 << " from sync was uninstalled as external extension";
1530 return true;
1531 }
1532
[email protected]06f92562011-04-29 19:27:311533 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191534 // If the extension has been disabled from sync, it may not have
1535 // been installed yet, so we don't know if the disable reason was a
[email protected]b914e2952013-04-26 07:10:031536 // permissions increase. That will be updated once CheckPermissionsIncrease
[email protected]7e9e2422012-12-13 19:54:191537 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401538 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311539 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401540 else
[email protected]7e9e2422012-12-13 19:54:191541 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371542
[email protected]406b5a92011-11-08 11:58:261543 // We need to cache some version information here because setting the
1544 // incognito flag invalidates the |extension| pointer (it reloads the
1545 // extension).
[email protected]9060d8b02012-01-13 02:14:301546 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261547 int result = extension ?
1548 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1549 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1550 extension = NULL; // No longer safe to use.
1551
1552 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311553 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041554 if (result < 0) {
1555 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201556 return false;
[email protected]b020dc12011-12-06 04:35:421557 }
[email protected]06f92562011-04-29 19:27:311558 } else {
[email protected]06f92562011-04-29 19:27:311559 // TODO(akalin): Replace silent update with a list of enabled
1560 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331561 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201562
[email protected]a4a147652012-07-03 23:41:321563 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]8f3bcbd2013-06-05 08:42:401564 extensions::PendingExtensionInfo::ShouldAllowInstallPredicate filter =
1565 (type == syncer::APPS) ? extensions::sync_helper::IsSyncableApp :
1566 extensions::sync_helper::IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201567
[email protected]06f92562011-04-29 19:27:311568 if (!pending_extension_manager()->AddFromSync(
1569 id,
[email protected]3bdba0d2011-08-23 07:17:301570 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201571 filter,
[email protected]6cc7dbae2011-04-29 21:18:331572 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311573 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301574 // This means that the extension is already pending installation, with a
1575 // non-INTERNAL location. Add to pending_sync_data, even though it will
1576 // never be removed (we'll never install a syncable version of the
1577 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311578 }
[email protected]3bdba0d2011-08-23 07:17:301579 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201580 return false;
[email protected]90310d92011-04-17 07:35:041581 }
[email protected]5db9ada2012-04-11 13:48:201582
1583 return true;
[email protected]90310d92011-04-17 07:35:041584}
1585
[email protected]c3cfb012011-04-06 22:07:351586bool ExtensionService::IsIncognitoEnabled(
1587 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481588 const Extension* extension = GetInstalledExtension(extension_id);
1589 if (extension && !extension->can_be_incognito_enabled())
1590 return false;
[email protected]c3cfb012011-04-06 22:07:351591 // If this is an existing component extension we always allow it to
1592 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401593 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411594 return true;
[email protected]fc05749a2013-07-10 06:59:111595 if (extension && extension->force_incognito_enabled())
1596 return true;
[email protected]cb0ce1e022010-03-10 19:54:411597
1598 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351599 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501600}
[email protected]55a35692010-02-11 23:25:211601
[email protected]c3cfb012011-04-06 22:07:351602void ExtensionService::SetIsIncognitoEnabled(
1603 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371604 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481605 if (extension && !extension->can_be_incognito_enabled())
1606 return;
[email protected]1d5e58b2013-01-31 08:41:401607 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481608 // This shouldn't be called for component extensions unless they are
1609 // syncable.
[email protected]8f3bcbd2013-06-05 08:42:401610 DCHECK(extensions::sync_helper::IsSyncable(extension));
[email protected]b873cd92012-02-09 21:51:481611
1612 // If we are here, make sure the we aren't trying to change the value.
1613 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1614
[email protected]c3cfb012011-04-06 22:07:351615 return;
1616 }
1617
[email protected]8c6c8c22011-03-09 12:52:341618 // Broadcast unloaded and loaded events to update browser state. Only bother
1619 // if the value changed and the extension is actually enabled, since there is
1620 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351621 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341622 if (enabled == old_enabled)
1623 return;
1624
[email protected]c3cfb012011-04-06 22:07:351625 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371626
[email protected]ffbf3f12011-12-09 22:10:331627 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341628
1629 // When we reload the extension the ID may be invalidated if we've passed it
1630 // by const ref everywhere. Make a copy to be safe.
1631 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261632 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331633 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301634
[email protected]406b5a92011-11-08 11:58:261635 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341636 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301637 if (extension)
1638 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211639}
1640
[email protected]dc9a74f72012-08-17 18:07:211641bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051642 // We allow the extension to see events and data from another profile iff it
1643 // uses "spanning" behavior and it has incognito access. "split" mode
1644 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221645 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351646 return IsIncognitoEnabled(extension->id()) &&
[email protected]06492ed2013-03-24 22:13:141647 !extensions::IncognitoInfo::IsSplitMode(extension);
[email protected]2a8f24e2010-11-03 21:37:051648}
1649
[email protected]5ef7b002011-05-12 23:16:201650bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1651 if (extension->is_hosted_app())
1652 return true;
1653 // Packaged apps and regular extensions need to be enabled specifically for
1654 // incognito (and split mode should be set).
[email protected]06492ed2013-03-24 22:13:141655 return extensions::IncognitoInfo::IsSplitMode(extension) &&
[email protected]5ef7b002011-05-12 23:16:201656 IsIncognitoEnabled(extension->id());
1657}
1658
[email protected]168389f2011-12-20 17:12:481659void ExtensionService::OnExtensionMoved(
1660 const std::string& moved_extension_id,
1661 const std::string& predecessor_extension_id,
1662 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231663 extension_prefs_->extension_sorting()->OnExtensionMoved(
1664 moved_extension_id,
1665 predecessor_extension_id,
1666 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481667
1668 const Extension* extension = GetInstalledExtension(moved_extension_id);
1669 if (extension)
1670 SyncExtensionChangeIfNeeded(*extension);
1671}
1672
[email protected]dc9a74f72012-08-17 18:07:211673bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081674 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441675 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081676 extension_prefs_->AllowFileAccess(extension->id()));
1677}
1678
[email protected]eaa7dd182010-12-14 11:09:001679void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481680 bool allow) {
1681 // Reload to update browser state. Only bother if the value changed and the
1682 // extension is actually enabled, since there is no UI otherwise.
1683 bool old_allow = AllowFileAccess(extension);
1684 if (allow == old_allow)
1685 return;
1686
[email protected]05c82182010-06-24 17:49:081687 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481688
[email protected]84df8332011-12-06 18:22:461689 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481690 if (extension_is_enabled)
1691 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081692}
1693
[email protected]8e4560b62011-01-14 10:09:141694// Some extensions will autoupdate themselves externally from Chrome. These
1695// are typically part of some larger client application package. To support
1696// these, the extension will register its location in the the preferences file
1697// (and also, on Windows, in the registry) and this code will periodically
1698// check that location for a .crx file, which it will then install locally if
1699// a new version is available.
1700// Errors are reported through ExtensionErrorReporter. Succcess is not
1701// reported.
[email protected]eaa7dd182010-12-14 11:09:001702void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121703 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141704
1705 // Note that this installation is intentionally silent (since it didn't
1706 // go through the front-end). Extensions that are registered in this
1707 // way are effectively considered 'pre-bundled', and so implicitly
1708 // trusted. In general, if something has HKLM or filesystem access,
1709 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121710
[email protected]8e4560b62011-01-14 10:09:141711 // Ask each external extension provider to give us a call back for each
1712 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271713 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141714 for (i = external_extension_providers_.begin();
1715 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271716 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141717 provider->VisitRegisteredExtension();
1718 }
1719
[email protected]50067e52011-10-20 23:17:071720 // Do any required work that we would have done after completion of all
1721 // providers.
1722 if (external_extension_providers_.empty()) {
1723 OnAllExternalProvidersReady();
1724 }
[email protected]9f1087e2009-06-15 17:29:321725}
1726
[email protected]50067e52011-10-20 23:17:071727void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271728 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121729 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071730 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121731
1732 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141733 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301734 if (AreAllExternalProvidersReady())
1735 OnAllExternalProvidersReady();
1736}
1737
1738bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271739 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141740 for (i = external_extension_providers_.begin();
1741 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301742 if (!i->get()->IsReady())
1743 return false;
[email protected]8e4560b62011-01-14 10:09:141744 }
[email protected]94fde232012-04-27 10:22:301745 return true;
[email protected]50067e52011-10-20 23:17:071746}
1747
1748void ExtensionService::OnAllExternalProvidersReady() {
1749 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301750 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1751 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071752
1753 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301754 if (update_once_all_providers_are_ready_ && updater()) {
1755 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061756 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141757 }
1758
1759 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231760 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141761 extension_prefs_->GetInstalledExtensionsInfo());
1762 for (size_t i = 0; i < extensions_info->size(); ++i) {
1763 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401764 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141765 CheckExternalUninstall(info->extension_id);
1766 }
[email protected]e5af875f2011-10-10 21:09:141767 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141768}
1769
1770void ExtensionService::IdentifyAlertableExtensions() {
1771 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1772
[email protected]07c9f2f42012-02-29 18:45:221773 // Build up the lists of extensions that require acknowledgment. If this is
1774 // the first time, grandfather extensions that would have caused
1775 // notification.
[email protected]89226982012-07-16 20:09:181776 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141777
[email protected]a9aa5932012-01-25 08:27:401778 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181779 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011780 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401781 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181782 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141783 } else {
1784 // First run. Just acknowledge all the extensions, silently, by
1785 // shortcutting the display of the UI and going straight to the
1786 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401787 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141788 }
1789 }
[email protected]a9aa5932012-01-25 08:27:401790
[email protected]612a1cb12012-10-17 13:18:031791 UpdateExternalExtensionAlert();
1792
[email protected]a9aa5932012-01-25 08:27:401793 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181794 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141795}
1796
[email protected]89226982012-07-16 20:09:181797bool ExtensionService::PopulateExtensionErrorUI(
1798 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221799 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281800
1801 // Extensions that are blacklisted.
1802 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1803 it != blacklisted_extensions_.end(); ++it) {
1804 std::string id = (*it)->id();
1805 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1806 extension_error_ui->AddBlacklistedExtension(id);
1807 needs_alert = true;
1808 }
1809 }
1810
[email protected]07c9f2f42012-02-29 18:45:221811 for (ExtensionSet::const_iterator iter = extensions_.begin();
1812 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361813 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281814
1815 // Extensions disabled by policy. Note: this no longer includes blacklisted
1816 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391817 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221818 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181819 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221820 needs_alert = true;
1821 }
1822 }
[email protected]07c9f2f42012-02-29 18:45:221823 }
[email protected]695b5712012-12-06 23:55:281824
[email protected]07c9f2f42012-02-29 18:45:221825 return needs_alert;
1826}
1827
[email protected]a9aa5932012-01-25 08:27:401828void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031829 const ExtensionIdSet* extension_ids =
1830 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141831 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1832 iter != extension_ids->end(); ++iter) {
1833 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1834 }
[email protected]879a6092013-03-06 04:12:501835 extension_error_ui_.reset();
1836}
1837
1838void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301839 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141840}
1841
[email protected]47fc70c2011-12-06 07:29:511842void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1843 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031844 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511845}
1846
[email protected]d96eb512012-11-01 23:44:081847bool ExtensionService::IsUnacknowledgedExternalExtension(
1848 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321849 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1850 return false;
1851
[email protected]1d5e58b2013-01-31 08:41:401852 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081853 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1854 !(extension_prefs_->GetDisableReasons(extension->id()) &
1855 Extension::DISABLE_SIDELOAD_WIPEOUT));
1856}
1857
[email protected]89226982012-07-16 20:09:181858void ExtensionService::HandleExtensionAlertDetails() {
1859 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301860 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1861 // causes a navigation.
1862 if (extension_error_ui_)
1863 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101864}
1865
[email protected]612a1cb12012-10-17 13:18:031866void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361867 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031868 return;
1869
1870 const Extension* extension = NULL;
1871 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1872 iter != disabled_extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361873 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081874 if (IsUnacknowledgedExternalExtension(e)) {
1875 extension = e;
1876 break;
[email protected]612a1cb12012-10-17 13:18:031877 }
1878 }
1879
1880 if (extension) {
[email protected]d96eb512012-11-01 23:44:081881 if (!extensions::HasExternalInstallError(this)) {
1882 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031883 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081884 // Stop prompting for this extension, and check if there's another
1885 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031886 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081887 UpdateExternalExtensionAlert();
1888 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1889 EXTERNAL_EXTENSION_IGNORED,
1890 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021891 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1892 UMA_HISTOGRAM_ENUMERATION(
1893 "Extensions.ExternalExtensionEventWebstore",
1894 EXTERNAL_EXTENSION_IGNORED,
1895 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1896 } else {
1897 UMA_HISTOGRAM_ENUMERATION(
1898 "Extensions.ExternalExtensionEventNonWebstore",
1899 EXTERNAL_EXTENSION_IGNORED,
1900 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1901 }
[email protected]d96eb512012-11-01 23:44:081902 return;
[email protected]612a1cb12012-10-17 13:18:031903 }
[email protected]b3aa7182013-04-25 04:45:231904 if (is_first_run_)
1905 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1906 // first_run is true if the extension was installed during a first run
1907 // (even if it's post-first run now).
1908 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1909 extension->id());
1910 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031911 }
1912 } else {
1913 extensions::RemoveExternalInstallError(this);
1914 }
[email protected]612a1cb12012-10-17 13:18:031915}
1916
[email protected]a9f39a312010-12-23 22:14:271917void ExtensionService::UnloadExtension(
1918 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591919 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091920 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281921 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021922 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281923 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251924
[email protected]fa0c96732010-11-17 00:14:231925 // This method can be called via PostTask, so the extension may have been
1926 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091927 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171928 // In case the extension may have crashed/uninstalled. Allow the profile to
1929 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081930 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231931 return;
[email protected]dd163fb02011-05-04 22:22:171932 }
[email protected]0c6da502009-08-14 22:32:391933
[email protected]20c8b942013-05-26 02:03:401934 // If uninstalling let RuntimeEventRouter know.
1935 if (reason == extension_misc::UNLOAD_REASON_UNINSTALL)
1936 extensions::RuntimeEventRouter::OnExtensionUninstalled(
1937 profile_, extension_id);
1938
[email protected]1eb175082010-02-10 09:26:161939 // Keep information about the extension so that we can reload it later
1940 // even if it's not permanently installed.
1941 unloaded_extension_paths_[extension->id()] = extension->path();
1942
[email protected]f17dbd42010-08-16 23:21:101943 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031944 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101945
[email protected]d7e9a862010-11-03 21:57:491946 // Clean up runtime data.
1947 extension_runtime_data_.erase(extension_id);
1948
[email protected]060d4972012-07-19 17:22:391949 if (disabled_extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461950 disabled_extensions_.Remove(extension->id());
[email protected]dd163fb02011-05-04 22:22:171951 // Make sure the profile cleans up its RequestContexts when an already
1952 // disabled extension is unloaded (since they are also tracking the disabled
1953 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081954 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]60a174a2013-08-02 20:29:281955 } else {
1956 // Remove the extension from our list.
1957 extensions_.Remove(extension->id());
1958 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391959 }
1960
[email protected]fb789532013-08-27 02:40:211961 content::NotificationService::current()->Notify(
1962 chrome::NOTIFICATION_EXTENSION_REMOVED,
1963 content::Source<Profile>(profile_),
1964 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251965}
1966
[email protected]8b1ec202013-09-05 02:09:501967void ExtensionService::RemoveComponentExtension(
1968 const std::string& extension_id) {
1969 scoped_refptr<const Extension> extension(
1970 GetExtensionById(extension_id, false));
1971 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
1972 content::NotificationService::current()->Notify(
1973 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1974 content::Source<Profile>(profile_),
1975 content::Details<const Extension>(extension.get()));
1976}
1977
[email protected]eaa7dd182010-12-14 11:09:001978void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401979 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171980
[email protected]84df8332011-12-06 18:22:461981 extensions_.Clear();
1982 disabled_extensions_.Clear();
1983 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491984 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321985
[email protected]c6e4a3412009-06-24 15:45:291986 // TODO(erikkay) should there be a notification for this? We can't use
1987 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1988 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321989}
1990
[email protected]eaa7dd182010-12-14 11:09:001991void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321992 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451993 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011994 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:351995 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
1996 // times.
[email protected]9f1087e2009-06-15 17:29:321997}
1998
[email protected]eaa7dd182010-12-14 11:09:001999void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:102000#if defined(OS_CHROMEOS)
2001 if (disable_garbage_collection_)
2002 return;
2003#endif
2004
[email protected]2f8757c32012-06-19 19:17:472005 if (extension_prefs_->pref_service()->ReadOnly())
2006 return;
2007
[email protected]9bd9a6862012-11-29 09:24:222008 if (pending_extension_manager()->HasPendingExtensions()) {
2009 // Don't garbage collect while there are pending installations, which may
2010 // be using the temporary installation directory. Try to garbage collect
2011 // again later.
[email protected]b3a25092013-05-28 22:08:162012 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:222013 FROM_HERE,
2014 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
2015 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
2016 return;
2017 }
2018
[email protected]45759612012-07-10 17:21:232019 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:472020 extension_prefs_->GetInstalledExtensionsInfo());
2021
[email protected]650b2d52013-02-10 03:41:452022 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:472023 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:052024 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2025 info->at(i)->extension_path));
2026
[email protected]399583b2012-12-11 09:33:422027 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:052028 for (size_t i = 0; i < info->size(); ++i)
2029 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2030 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:472031
[email protected]7f8f24f2012-11-15 19:40:142032 if (!GetFileTaskRunner()->PostTask(
2033 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:472034 base::Bind(
2035 &extension_file_util::GarbageCollectExtensions,
2036 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:402037 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:472038 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402039 }
[email protected]3cf4f0992009-02-03 23:00:302040}
2041
[email protected]fb82dcd2012-03-21 14:15:462042void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]6d147912013-07-09 08:02:142043 if (extensions::sync_helper::IsSyncableApp(&extension)) {
2044 if (app_sync_bundle_.IsSyncing())
2045 app_sync_bundle_.SyncChangeIfNeeded(extension);
2046 else if (is_ready() && !flare_.is_null())
2047 flare_.Run(syncer::APPS);
2048 } else if (extensions::sync_helper::IsSyncableExtension(&extension)) {
2049 if (extension_sync_bundle_.IsSyncing())
2050 extension_sync_bundle_.SyncChangeIfNeeded(extension);
2051 else if (is_ready() && !flare_.is_null())
2052 flare_.Run(syncer::EXTENSIONS);
[email protected]fb82dcd2012-03-21 14:15:462053 }
2054}
2055
[email protected]820d9bd2013-04-03 03:46:032056void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:352057 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:032058 content::NotificationService::current()->Notify(
2059 chrome::NOTIFICATION_EXTENSIONS_READY,
2060 content::Source<Profile>(profile_),
2061 content::NotificationService::NoDetails());
2062}
2063
[email protected]eaa7dd182010-12-14 11:09:002064void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:112065 if (updater_)
[email protected]93fd78f42009-07-10 16:43:172066 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502067
[email protected]bc151cf92013-02-12 04:57:262068 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:512069}
2070
[email protected]fcb58a862012-05-01 01:03:152071void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172072 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2073 // default extension state to DISABLED when the --disable-extensions flag
2074 // is set (http://crbug.com/29067).
2075 if (!extensions_enabled() &&
2076 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:402077 extension->location() != Manifest::COMPONENT &&
2078 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152079 return;
[email protected]330840c2012-08-29 22:21:012080 }
[email protected]c8d407e2011-04-28 21:27:172081
[email protected]b914e2952013-04-26 07:10:032082 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:172083 bool is_extension_installed = false;
2084 const Extension* old = GetInstalledExtension(extension->id());
2085 if (old) {
2086 is_extension_installed = true;
2087 int version_compare_result =
2088 extension->version()->CompareTo(*(old->version()));
2089 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:032090 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2091 // that we aren't downgrading.
2092 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:172093 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:032094 }
2095 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:172096
[email protected]1eb175082010-02-10 09:26:162097 // The extension is now loaded, remove its data from unloaded extension map.
2098 unloaded_extension_paths_.erase(extension->id());
2099
[email protected]bb7f40952011-01-13 00:21:202100 // If a terminated extension is loaded, remove it from the terminated list.
2101 UntrackTerminatedExtension(extension->id());
2102
[email protected]f17dbd42010-08-16 23:21:102103 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:412104 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:102105
[email protected]b914e2952013-04-26 07:10:032106 // Check if the extension's privileges have changed and mark the
2107 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:172108 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:032109
[email protected]116d40e2013-08-08 17:23:172110 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:412111 // To upgrade an extension in place, unload the old one and then load the
2112 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b914e2952013-04-26 07:10:032113 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_UPDATE);
2114 }
[email protected]0c6da502009-08-14 22:32:392115
[email protected]695b5712012-12-06 23:55:282116 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:092117 // Only prefs is checked for the blacklist. We rely on callers to check the
2118 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:072119 // installation then threads through the install and pending install flow
2120 // of this class, and we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:282121 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:412122 } else if (!reloading &&
2123 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482124 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302125 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032126 content::NotificationService::current()->Notify(
2127 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2128 content::Source<Profile>(profile_),
2129 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442130
[email protected]7e9e2422012-12-13 19:54:192131 // Show the extension disabled error if a permissions increase was the
2132 // only reason it was disabled.
2133 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032134 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442135 extensions::AddExtensionDisabledError(this, extension);
2136 }
[email protected]ca2e82022013-04-29 16:48:412137 } else if (reloading) {
2138 // Replace the old extension with the new version.
2139 CHECK(!disabled_extensions_.Insert(extension));
2140 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:282141 } else {
2142 // All apps that are displayed in the launcher are ordered by their ordinals
2143 // so we must ensure they have valid ordinals.
2144 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252145 if (!extension->ShouldDisplayInNewTabPage()) {
2146 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2147 extension->id());
2148 }
[email protected]695b5712012-12-06 23:55:282149 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2150 extension->id(), syncer::StringOrdinal());
2151 }
2152
2153 extensions_.Insert(extension);
2154 SyncExtensionChangeIfNeeded(*extension);
2155 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:312156 }
[email protected]b914e2952013-04-26 07:10:032157 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:352158}
2159
[email protected]8c484b742012-11-29 06:05:362160void ExtensionService::AddComponentExtension(const Extension* extension) {
2161 const std::string old_version_string(
2162 extension_prefs_->GetVersionString(extension->id()));
2163 const Version old_version(old_version_string);
2164
2165 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2166 VLOG(1) << "Component extension " << extension->name() << " ("
2167 << extension->id() << ") installing/upgrading from '"
2168 << old_version_string << "' to " << extension->version()->GetString();
2169
2170 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422171 Extension::ENABLED_COMPONENT,
[email protected]9f3c8532013-07-31 19:52:072172 extensions::Blacklist::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:422173 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362174 return;
2175 }
2176
2177 AddExtension(extension);
2178}
2179
[email protected]b914e2952013-04-26 07:10:032180void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:312181 // If the extension has used the optional permissions API, it will have a
2182 // custom set of active permissions defined in the extension prefs. Here,
2183 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062184 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312185 extension_prefs()->GetActivePermissions(extension->id());
2186
[email protected]dc24976f2013-06-02 21:15:092187 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:312188 // We restrict the active permissions to be within the bounds defined in the
2189 // extension's manifest.
2190 // a) active permissions must be a subset of optional + default permissions
2191 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062192 scoped_refptr<PermissionSet> total_permissions =
2193 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062194 extensions::PermissionsData::GetRequiredPermissions(extension),
2195 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:312196
2197 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062198 scoped_refptr<PermissionSet> adjusted_active =
2199 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312200 total_permissions.get(), active_permissions.get());
2201
2202 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062203 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062204 extensions::PermissionsData::GetRequiredPermissions(extension),
2205 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312206
[email protected]c333e792012-01-06 16:57:392207 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:092208 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312209 }
[email protected]b914e2952013-04-26 07:10:032210}
2211
2212void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:172213 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:032214 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:312215
[email protected]8d888c12010-11-30 00:00:252216 // We keep track of all permissions the user has granted each extension.
2217 // This allows extensions to gracefully support backwards compatibility
2218 // by including unknown permissions in their manifests. When the user
2219 // installs the extension, only the recognized permissions are recorded.
2220 // When the unknown permissions become recognized (e.g., through browser
2221 // upgrade), we can prompt the user to accept these new permissions.
2222 // Extensions can also silently upgrade to less permissions, and then
2223 // silently upgrade to a version that adds these permissions back.
2224 //
2225 // For example, pretend that Chrome 10 includes a permission "omnibox"
2226 // for an API that adds suggestions to the omnibox. An extension can
2227 // maintain backwards compatibility while still having "omnibox" in the
2228 // manifest. If a user installs the extension on Chrome 9, the browser
2229 // will record the permissions it recognized, not including "omnibox."
2230 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2231 // will disable the extension and prompt the user to approve the increase
2232 // in privileges. The extension could then release a new version that
2233 // removes the "omnibox" permission. When the user upgrades, Chrome will
2234 // still remember that "omnibox" had been granted, so that if the
2235 // extension once again includes "omnibox" in an upgrade, the extension
2236 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:282237 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252238
[email protected]211a3f32013-05-28 21:48:182239 bool auto_grant_permission =
[email protected]116d40e2013-08-08 17:23:172240 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]211a3f32013-05-28 21:48:182241 chrome::IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:032242 // Silently grant all active permissions to default apps only on install.
2243 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:182244 // Silently grant all active permissions to apps install in kiosk mode on both
2245 // install and update.
2246 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:032247 GrantPermissions(extension);
2248
2249 bool is_privilege_increase = false;
2250 // We only need to compare the granted permissions to the current permissions
2251 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:052252 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:182253 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:252254 // Add all the recognized permissions if the granted permissions list
2255 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062256 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312257 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522258 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252259
2260 // Here, we check if an extension's privileges have increased in a manner
2261 // that requires the user's approval. This could occur because the browser
2262 // upgraded and recognized additional privileges, or an extension upgrades
2263 // to a version that requires additional privileges.
[email protected]cadac622013-06-11 16:46:362264 is_privilege_increase = granted_permissions->HasLessPrivilegesThan(
[email protected]a0a73052013-07-10 01:42:432265 extension->GetActivePermissions().get(), extension->GetType());
[email protected]8d888c12010-11-30 00:00:252266 }
2267
[email protected]116d40e2013-08-08 17:23:172268 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:032269 // If the extension was already disabled, suppress any alerts for becoming
2270 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:032271 bool previously_disabled =
2272 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:192273 // Legacy disabled extensions do not have a disable reason. Infer that if
2274 // there was no permission increase, it was likely disabled by the user.
2275 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:032276 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:192277 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032278 }
[email protected]7e9e2422012-12-13 19:54:192279 // Extensions that came to us disabled from sync need a similar inference,
2280 // except based on the new version's permissions.
2281 if (previously_disabled &&
2282 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2283 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2284 extension_prefs_->ClearDisableReasons(extension->id());
2285 if (!is_privilege_increase)
2286 disable_reasons |= Extension::DISABLE_USER_ACTION;
2287 }
[email protected]b914e2952013-04-26 07:10:032288 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:252289 }
2290
2291 // Extension has changed permissions significantly. Disable it. A
2292 // notification should be sent by the caller.
2293 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192294 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492295 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2296 RecordPermissionMessagesHistogram(
2297 extension, "Extensions.Permissions_AutoDisable");
2298 }
[email protected]b3317ad2011-04-28 23:46:002299 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252300 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:032301 }
2302 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:282303 extension_prefs_->AddDisableReason(
2304 extension->id(),
2305 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252306 }
2307}
2308
[email protected]eaa7dd182010-12-14 11:09:002309void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392310 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462311 for (ExtensionSet::const_iterator iter = extensions_.begin();
2312 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:362313 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:402314 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462315 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352316 }
2317
[email protected]f6431be82013-09-07 02:53:452318 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252319}
2320
[email protected]9f4e4f082013-06-21 07:11:192321ExtensionService::ImportStatus ExtensionService::SatisfyImports(
2322 const Extension* extension) {
2323 ImportStatus status = IMPORT_STATUS_OK;
2324 std::vector<std::string> pending;
2325 // TODO(elijahtaylor): Message the user if there is a failure that is
2326 // unrecoverable.
2327 if (SharedModuleInfo::ImportsModules(extension)) {
2328 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2329 SharedModuleInfo::GetImports(extension);
2330 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2331 for (i = imports.begin(); i != imports.end(); ++i) {
2332 Version version_required(i->minimum_version);
2333 const Extension* imported_module =
2334 GetExtensionById(i->extension_id, true);
2335 if (!imported_module) {
2336 if (extension->from_webstore()) {
2337 status = IMPORT_STATUS_UNSATISFIED;
2338 pending.push_back(i->extension_id);
2339 } else {
2340 return IMPORT_STATUS_UNRECOVERABLE;
2341 }
2342 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
2343 return IMPORT_STATUS_UNRECOVERABLE;
2344 } else if (version_required.IsValid() &&
2345 imported_module->version()->CompareTo(version_required) < 0) {
2346 if (imported_module->from_webstore()) {
2347 status = IMPORT_STATUS_UNSATISFIED;
2348 } else {
2349 return IMPORT_STATUS_UNRECOVERABLE;
2350 }
2351 }
2352 }
2353 }
2354 if (status == IMPORT_STATUS_UNSATISFIED) {
2355 for (std::vector<std::string>::const_iterator iter = pending.begin();
2356 iter != pending.end();
2357 ++iter) {
2358 pending_extension_manager()->AddFromExtensionImport(
2359 *iter,
2360 extension_urls::GetWebstoreUpdateUrl(),
2361 IsSharedModule);
2362 }
2363 CheckForUpdatesSoon();
2364 }
2365 return status;
2366}
2367
2368scoped_ptr<const ExtensionSet>
2369 ExtensionService::GetDependentExtensions(const Extension* extension) {
2370 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2371 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2372 if (SharedModuleInfo::IsSharedModule(extension)) {
2373 set_to_check->InsertAll(disabled_extensions_);
2374 set_to_check->InsertAll(delayed_installs_);
2375 set_to_check->InsertAll(extensions_);
2376 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2377 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112378 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2379 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192380 dependents->Insert(*iter);
2381 }
2382 }
2383 }
2384 return dependents.PassAs<const ExtensionSet>();
2385}
2386
2387void ExtensionService::PruneSharedModulesOnUninstall(
2388 const Extension* extension) {
2389 if (SharedModuleInfo::ImportsModules(extension)) {
2390 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2391 SharedModuleInfo::GetImports(extension);
2392 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2393 for (i = imports.begin(); i != imports.end(); ++i) {
2394 const Extension* imported_module =
2395 GetExtensionById(i->extension_id, true);
2396 if (imported_module && imported_module->from_webstore()) {
2397 scoped_ptr<const ExtensionSet> dependents =
2398 GetDependentExtensions(imported_module);
2399 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212400 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192401 }
2402 }
2403 }
2404 }
2405}
2406
[email protected]8266d662011-07-12 21:53:262407void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502408 const Extension* extension,
[email protected]98270432012-09-11 20:51:242409 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052410 bool has_requirement_errors,
[email protected]9f3c8532013-07-31 19:52:072411 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052412 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122413 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202414
[email protected]c3cfb012011-04-06 22:07:352415 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032416 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412417 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062418 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402419 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062420 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322421
[email protected]31bb5ee62012-09-12 22:58:402422 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2423 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222424 << " and update URL "
2425 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402426 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542427
[email protected]4416c5a2010-06-26 01:28:572428 // Delete the extension directory since we're not going to
2429 // load it.
[email protected]7f8f24f2012-11-15 19:40:142430 if (!GetFileTaskRunner()->PostTask(
2431 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362432 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402433 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362434 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402435 }
[email protected]4416c5a2010-06-26 01:28:572436 return;
2437 }
[email protected]51a3bf8b2012-06-08 22:53:062438
2439 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572440 } else {
[email protected]c3cfb012011-04-06 22:07:352441 // We explicitly want to re-enable an uninstalled external
2442 // extension; if we're here, that means the user is manually
2443 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332444 if (IsExternalExtensionUninstalled(id)) {
2445 initial_enable = true;
2446 }
[email protected]aa142702010-03-26 01:26:332447 }
2448
[email protected]98270432012-09-11 20:51:242449 // Unsupported requirements overrides the management policy.
2450 if (has_requirement_errors) {
2451 initial_enable = false;
2452 extension_prefs_->AddDisableReason(
2453 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2454 // If the extension was disabled because of unsupported requirements but
2455 // now supports all requirements after an update and there are not other
2456 // disable reasons, enable it.
2457 } else if (extension_prefs_->GetDisableReasons(id) ==
2458 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2459 initial_enable = true;
2460 extension_prefs_->ClearDisableReasons(id);
2461 }
2462
[email protected]9f3c8532013-07-31 19:52:072463 if (blacklist_state == extensions::Blacklist::BLACKLISTED) {
2464 // Installation of a blacklisted extension can happen from sync, policy,
2465 // etc, where to maintain consistency we need to install it, just never
2466 // load it (see AddExtension). Usually it should be the job of callers to
2467 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2468 // showing the install dialogue).
2469 extension_prefs()->AcknowledgeBlacklistedExtension(id);
2470 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2471 extension->location(),
2472 Manifest::NUM_LOCATIONS);
2473 }
2474
[email protected]695b5712012-12-06 23:55:282475 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552476 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2477 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302478 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402479 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552480 RecordPermissionMessagesHistogram(
2481 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302482 } else {
2483 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2484 extension->GetType(), 100);
2485 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402486 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552487 }
2488
[email protected]a39921b42012-02-28 03:42:542489 // Certain extension locations are specific enough that we can
2490 // auto-acknowledge any extension that came from one of them.
[email protected]1d5e58b2013-01-31 08:41:402491 if (extension->location() == Manifest::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542492 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422493 const Extension::State initial_state =
2494 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222495 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072496 extension_prefs_->SetDelayedInstallInfo(
2497 extension,
2498 initial_state,
2499 blacklist_state,
2500 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2501 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052502
2503 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192504 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052505
2506 // Notify extension of available update.
2507 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2508 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452509
2510 // Notify observers that app update is available.
2511 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2512 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052513 return;
2514 }
2515
[email protected]9f4e4f082013-06-21 07:11:192516 ImportStatus status = SatisfyImports(extension);
2517 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072518 extension_prefs_->SetDelayedInstallInfo(
2519 extension,
2520 initial_state,
2521 blacklist_state,
2522 extensions::ExtensionPrefs::DELAY_REASON_GC,
2523 page_ordinal);
[email protected]399583b2012-12-11 09:33:422524 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192525 } else if (status != IMPORT_STATUS_OK) {
2526 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072527 extension_prefs_->SetDelayedInstallInfo(
2528 extension,
2529 initial_state,
2530 blacklist_state,
[email protected]9f4e4f082013-06-21 07:11:192531 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2532 page_ordinal);
2533 delayed_installs_.Insert(extension);
2534 }
[email protected]399583b2012-12-11 09:33:422535 } else {
[email protected]9f3c8532013-07-31 19:52:072536 AddNewOrUpdatedExtension(extension,
2537 initial_state,
2538 blacklist_state,
2539 page_ordinal);
[email protected]399583b2012-12-11 09:33:422540 }
[email protected]8c484b742012-11-29 06:05:362541}
2542
2543void ExtensionService::AddNewOrUpdatedExtension(
2544 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422545 Extension::State initial_state,
[email protected]9f3c8532013-07-31 19:52:072546 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422547 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362548 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9f3c8532013-07-31 19:52:072549 extension_prefs_->OnExtensionInstalled(extension,
2550 initial_state,
2551 blacklist_state,
2552 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122553 delayed_installs_.Remove(extension->id());
[email protected]6f6101832012-11-27 22:10:482554 FinishInstallation(extension);
2555}
2556
2557void ExtensionService::MaybeFinishDelayedInstallation(
2558 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192559 // Check if the extension already got installed.
2560 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482561 return;
[email protected]9f4e4f082013-06-21 07:11:192562 extensions::ExtensionPrefs::DelayReason reason =
2563 extension_prefs_->GetDelayedInstallReason(extension_id);
2564
2565 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2566 // preferences files that will not have set this field but it was previously
2567 // only used for idle updates.
2568 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2569 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
2570 is_ready() && !IsExtensionIdle(extension_id))
[email protected]6f6101832012-11-27 22:10:482571 return;
2572
[email protected]9f4e4f082013-06-21 07:11:192573 const Extension* extension = delayed_installs_.GetByID(extension_id);
2574 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2575 ImportStatus status = SatisfyImports(extension);
2576 if (status != IMPORT_STATUS_OK) {
2577 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2578 delayed_installs_.Remove(extension_id);
2579 // Make sure no version of the extension is actually installed, (i.e.,
2580 // that this delayed install was not an update).
2581 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2582 extension_prefs_->DeleteExtensionPrefs(extension_id);
2583 }
2584 return;
2585 }
2586 }
2587
[email protected]6f6101832012-11-27 22:10:482588 FinishDelayedInstallation(extension_id);
2589}
2590
2591void ExtensionService::FinishDelayedInstallation(
2592 const std::string& extension_id) {
2593 scoped_refptr<const Extension> extension(
2594 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092595 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192596 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482597
[email protected]399583b2012-12-11 09:33:422598 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482599 NOTREACHED();
2600
[email protected]dc24976f2013-06-02 21:15:092601 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482602}
2603
2604void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562605 const extensions::Extension* existing_extension =
2606 GetInstalledExtension(extension->id());
2607 bool is_update = false;
2608 std::string old_name;
2609 if (existing_extension) {
2610 is_update = true;
2611 old_name = existing_extension->name();
2612 }
2613 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152614 content::NotificationService::current()->Notify(
2615 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2616 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022617 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152618
[email protected]d96eb512012-11-01 23:44:082619 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362620
[email protected]399583b2012-12-11 09:33:422621 // Unpacked extensions default to allowing file access, but if that has been
2622 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402623 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422624 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2625 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2626 }
2627
[email protected]6f6101832012-11-27 22:10:482628 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032629
2630 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082631 // so he can reenable it. We do this last so that it has already been
2632 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162633 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032634 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082635 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2636 EXTERNAL_EXTENSION_INSTALLED,
2637 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022638 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2639 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2640 EXTERNAL_EXTENSION_INSTALLED,
2641 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2642 } else {
2643 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2644 EXTERNAL_EXTENSION_INSTALLED,
2645 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2646 }
[email protected]d96eb512012-11-01 23:44:082647 }
[email protected]9f4e4f082013-06-21 07:11:192648
2649 // Check extensions that may have been delayed only because this shared module
2650 // was not available.
2651 if (SharedModuleInfo::IsSharedModule(extension)) {
2652 MaybeFinishDelayedInstallations();
2653 }
[email protected]4a190632009-05-09 01:07:422654}
2655
[email protected]76b65442012-11-17 14:11:482656const Extension* ExtensionService::GetPendingExtensionUpdate(
2657 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192658 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482659}
2660
[email protected]bb7f40952011-01-13 00:21:202661void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462662 if (!terminated_extensions_.Contains(extension->id()))
2663 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202664
[email protected]b3f7fe22011-11-11 19:27:562665 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202666}
2667
2668void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202669 std::string lowercase_id = StringToLowerASCII(id);
[email protected]60a174a2013-08-02 20:29:282670 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
[email protected]84df8332011-12-06 18:22:462671 terminated_extensions_.Remove(lowercase_id);
[email protected]fb789532013-08-27 02:40:212672 if (extension) {
2673 content::NotificationService::current()->Notify(
2674 chrome::NOTIFICATION_EXTENSION_REMOVED,
2675 content::Source<Profile>(profile_),
2676 content::Details<const Extension>(extension));
2677 }
[email protected]bb7f40952011-01-13 00:21:202678}
2679
[email protected]0dfe05c2011-02-23 23:03:362680const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472681 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282682 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472683}
2684
2685const Extension* ExtensionService::GetInstalledExtension(
2686 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282687 int include_mask = INCLUDE_ENABLED |
2688 INCLUDE_DISABLED |
2689 INCLUDE_TERMINATED |
2690 INCLUDE_BLACKLISTED;
2691 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362692}
2693
[email protected]eaa7dd182010-12-14 11:09:002694bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442695 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]be9915fb2013-07-18 09:28:552696 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442697 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402698 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392699}
2700
[email protected]4d007b312012-10-17 03:00:482701bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]be9915fb2013-07-18 09:28:552702 const Extension* extension = extensions_.GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482703 if (extension && extension->is_platform_app()) {
2704 *url = GURL(chrome::kExtensionInvalidRequestURL);
2705 return true;
2706 }
2707
2708 return false;
2709}
2710
[email protected]9060d8b02012-01-13 02:14:302711bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322712 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142713 const Version* version,
[email protected]650b2d52013-02-10 03:41:452714 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402715 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512716 int creation_flags,
2717 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382718 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2719 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012720 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302721 return false;
[email protected]a8af9fdb2010-10-28 21:52:202722
[email protected]7577a5c52009-07-30 06:21:582723 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492724 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582725 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022726 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352727
[email protected]7577a5c52009-07-30 06:21:582728 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352729 // The default apps will have the location set as INTERNAL. Since older
2730 // default apps are installed as EXTERNAL, we override them. However, if the
2731 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402732 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352733 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402734 (location == Manifest::INTERNAL &&
2735 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352736
2737 if (!is_default_apps_migration) {
2738 DCHECK(version);
2739
2740 switch (existing->version()->CompareTo(*version)) {
2741 case -1: // existing version is older, we should upgrade
2742 break;
2743 case 0: // existing version is same, do nothing
2744 return false;
2745 case 1: // existing version is newer, uh-oh
2746 LOG(WARNING) << "Found external version of extension " << id
2747 << "that is older than current version. Current version "
2748 << "is: " << existing->VersionString() << ". New "
2749 << "version is: " << version->GetString()
2750 << ". Keeping current version.";
2751 return false;
2752 }
[email protected]7577a5c52009-07-30 06:21:582753 }
2754 }
2755
[email protected]9060d8b02012-01-13 02:14:302756 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302757 if (!pending_extension_manager()->AddFromExternalFile(
2758 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302759 return false;
[email protected]e3987852012-05-04 10:06:302760 }
[email protected]9c635f22010-12-02 09:36:362761
[email protected]14908b72011-04-20 06:54:362762 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372763 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162764 installer->set_install_source(location);
2765 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072766 installer->set_expected_version(*version);
2767 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312768 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182769#if defined(OS_CHROMEOS)
2770 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2771#else
[email protected]6dfbbf82010-03-12 23:09:162772 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182773#endif
[email protected]47fc70c2011-12-06 07:29:512774
2775 // Depending on the source, a new external extension might not need a user
2776 // notification on installation. For such extensions, mark them acknowledged
2777 // now to suppress the notification.
2778 if (mark_acknowledged)
2779 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302780
2781 return true;
[email protected]7577a5c52009-07-30 06:21:582782}
2783
[email protected]eaa7dd182010-12-14 11:09:002784void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452785 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402786 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402787 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012788 content::NotificationService::current()->Notify(
2789 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2790 content::Source<Profile>(profile_),
2791 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402792
[email protected]8a205c02011-02-04 20:41:332793 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212794 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042795 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492796 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402797 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2798}
2799
[email protected]eaa7dd182010-12-14 11:09:002800void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132801 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182802 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332803 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182804 if (iter == orphaned_dev_tools_.end())
2805 return;
2806
[email protected]04ea1bb2013-07-10 09:26:092807 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182808 orphaned_dev_tools_.erase(iter);
2809}
2810
[email protected]432115822011-07-10 15:52:272811void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532812 const content::NotificationSource& source,
2813 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272814 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542815 case chrome::NOTIFICATION_APP_TERMINATING:
2816 // Shutdown has started. Don't start any more extension installs.
2817 // (We cannot use ExtensionService::Shutdown() for this because it
2818 // happens too late in browser teardown.)
2819 browser_terminating_ = true;
2820 break;
[email protected]432115822011-07-10 15:52:272821 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532822 if (profile_ !=
2823 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322824 break;
[email protected]6c2381d2011-10-19 02:52:532825 }
[email protected]a4ed6282009-12-14 20:51:162826
[email protected]3a1dc572012-07-31 22:25:132827 extensions::ExtensionHost* host =
2828 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262829
[email protected]fa2416f2011-05-03 08:41:202830 // Mark the extension as terminated and Unload it. We want it to
2831 // be in a consistent state: either fully working or not loaded
2832 // at all, but never half-crashed. We do it in a PostTask so
2833 // that other handlers of this notification will still have
2834 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162835 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362836 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362837 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202838 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012839 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202840 host->extension()));
[email protected]31f77262009-12-02 20:48:532841 break;
2842 }
[email protected]432115822011-07-10 15:52:272843 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302844 content::RenderProcessHost* process =
2845 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192846 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302847 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192848 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2849 break;
2850
[email protected]15397b42012-05-16 23:56:062851 // Extensions need to know the channel for API restrictions.
2852 process->Send(new ExtensionMsg_SetChannel(
[email protected]15d4d2d2013-08-09 06:49:032853 extensions::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062854
[email protected]4941fe92013-06-13 23:21:012855 // Platform apps need to know the system font.
2856 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2857 webui::SetFontAndTextDirection(fonts.get());
2858 std::string font_family, font_size;
2859 fonts->GetString("fontfamily", &font_family);
2860 fonts->GetString("fontsize", &font_size);
2861 process->Send(new ExtensionMsg_SetSystemFont(
2862 font_family, font_size));
2863
[email protected]77a6970c2011-04-23 16:58:562864 // Valid extension function names, used to setup bindings in renderer.
2865 std::vector<std::string> function_names;
2866 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2867 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532868
[email protected]77a6970c2011-04-23 16:58:562869 // Scripting whitelist. This is modified by tests and must be communicated
2870 // to renderers.
2871 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2872 *Extension::GetScriptingWhitelist()));
2873
2874 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532875 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462876 for (ExtensionSet::const_iterator iter = extensions_.begin();
2877 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492878 // Renderers don't need to know about themes.
2879 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362880 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562881 }
[email protected]9776e82e2011-11-15 02:17:532882 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562883 break;
2884 }
[email protected]432115822011-07-10 15:52:272885 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302886 content::RenderProcessHost* process =
2887 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192888 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302889 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192890 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2891 break;
2892
[email protected]6bc04fd82011-12-04 02:29:352893 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462894 BrowserThread::PostTask(
2895 BrowserThread::IO, FROM_HERE,
2896 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082897 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302898 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212899 break;
2900 }
[email protected]0db124b02012-11-07 04:55:052901 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2902 extensions::ExtensionHost* host =
2903 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222904 std::string extension_id = host->extension_id();
[email protected]9f4e4f082013-06-21 07:11:192905 if (delayed_installs_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222906 // We were waiting for this extension to become idle, it now might have,
2907 // so maybe finish installation.
[email protected]b3a25092013-05-28 22:08:162908 base::MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052909 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482910 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052911 AsWeakPtr(), extension_id),
2912 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052913 }
2914 break;
2915 }
[email protected]92dd8d92013-02-26 00:41:082916 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2917 // Notify extensions that chrome update is available.
2918 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2919 profile_);
[email protected]75bdcb872013-03-13 00:41:452920
2921 // Notify observers that chrome update is available.
2922 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2923 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082924 break;
2925 }
[email protected]aa96d3a2010-08-21 08:45:252926
[email protected]4814b512009-11-07 00:12:292927 default:
2928 NOTREACHED() << "Unexpected notification type.";
2929 }
2930}
2931
[email protected]90bb38d2012-11-14 18:36:032932void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182933 IdentifyAlertableExtensions();
2934 CheckManagementPolicy();
2935}
2936
[email protected]eaa7dd182010-12-14 11:09:002937bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182938 return !GetAppIds().empty();
2939}
[email protected]377011d2010-07-20 04:18:502940
[email protected]eaa7dd182010-12-14 11:09:002941ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182942 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462943 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502944 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402945 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182946 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502947 }
2948
[email protected]ec5b50d2010-10-09 16:35:182949 return result;
[email protected]377011d2010-07-20 04:18:502950}
[email protected]d7e9a862010-11-03 21:57:492951
[email protected]dc9a74f72012-08-17 18:07:212952bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292953 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212954 return true;
2955 ExtensionRuntimeDataMap::const_iterator it =
2956 extension_runtime_data_.find(extension->id());
2957 return it == extension_runtime_data_.end() ? false :
2958 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492959}
2960
[email protected]eaa7dd182010-12-14 11:09:002961void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292962 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492963 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072964 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272965 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532966 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072967 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492968}
2969
[email protected]65992d52012-09-19 23:05:312970void ExtensionService::InspectBackgroundPage(const Extension* extension) {
2971 DCHECK(extension);
2972
[email protected]fdd679b2012-11-15 20:49:392973 ExtensionProcessManager* pm = system_->process_manager();
[email protected]65992d52012-09-19 23:05:312974 extensions::LazyBackgroundTaskQueue* queue =
[email protected]fdd679b2012-11-15 20:49:392975 system_->lazy_background_task_queue();
[email protected]65992d52012-09-19 23:05:312976
2977 extensions::ExtensionHost* host =
2978 pm->GetBackgroundHostForExtension(extension->id());
2979 if (host) {
2980 InspectExtensionHost(host);
2981 } else {
2982 queue->AddPendingTask(
2983 profile_, extension->id(),
2984 base::Bind(&ExtensionService::InspectExtensionHost,
2985 base::Unretained(this)));
2986 }
2987}
2988
[email protected]dc9a74f72012-08-17 18:07:212989bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2990 ExtensionRuntimeDataMap::const_iterator it =
2991 extension_runtime_data_.find(extension->id());
2992 return it == extension_runtime_data_.end() ? false :
2993 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492994}
2995
[email protected]eaa7dd182010-12-14 11:09:002996void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032997 bool value) {
[email protected]d7e9a862010-11-03 21:57:492998 extension_runtime_data_[extension->id()].being_upgraded = value;
2999}
[email protected]1bead0712010-11-27 17:41:533000
[email protected]e178ad92013-06-28 02:29:253001bool ExtensionService::IsBeingReloaded(
3002 const std::string& extension_id) const {
3003 return ContainsKey(extensions_being_reloaded_, extension_id);
3004}
3005
3006void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:433007 bool isBeingReloaded) {
3008 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:253009 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:433010 else
[email protected]e178ad92013-06-28 02:29:253011 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:253012}
3013
[email protected]dc9a74f72012-08-17 18:07:213014bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
3015 ExtensionRuntimeDataMap::const_iterator it =
3016 extension_runtime_data_.find(extension->id());
3017 return it == extension_runtime_data_.end() ? false :
3018 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:073019}
3020
3021void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
3022 bool value) {
3023 extension_runtime_data_[extension->id()].has_used_webrequest = value;
3024}
3025
[email protected]65992d52012-09-19 23:05:313026void ExtensionService::InspectExtensionHost(
3027 extensions::ExtensionHost* host) {
3028 if (host)
3029 DevToolsWindow::OpenDevToolsWindow(host->render_view_host());
3030}
[email protected]612a1cb12012-10-17 13:18:033031
3032bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
3033 // Extensions installed by policy can't be disabled. So even if a previous
3034 // installation disabled the extension, make sure it is now enabled.
3035 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
3036 return true;
3037
3038 if (extension_prefs_->IsExtensionDisabled(extension->id()))
3039 return false;
3040
[email protected]41070e8d2012-10-24 01:34:363041 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:033042 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:543043 // enabling them. Hosted apps are excepted because they are not dangerous
3044 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:403045 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
3046 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:033047 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
3048 return false;
3049 }
3050 }
[email protected]612a1cb12012-10-17 13:18:033051
3052 return true;
3053}
[email protected]0db124b02012-11-07 04:55:053054
3055bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:473056 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:333057 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:053058 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:473059 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223060 if (host)
3061 return false;
3062 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
3063}
3064
3065bool ExtensionService::ShouldDelayExtensionUpdate(
3066 const std::string& extension_id,
3067 bool wait_for_idle) const {
3068 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
3069
3070 // If delayed updates are globally disabled, or just for this extension,
3071 // don't delay.
3072 if (!install_updates_when_idle_ || !wait_for_idle)
3073 return false;
3074
[email protected]695b5712012-12-06 23:55:283075 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223076 // If there is no old extension, this is not an update, so don't delay.
3077 if (!old)
3078 return false;
3079
[email protected]9367eabc2013-03-01 01:29:293080 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:223081 // Delay installation if the extension listens for the onUpdateAvailable
3082 // event.
3083 return system_->event_router()->ExtensionHasEventListener(
3084 extension_id, kOnUpdateAvailableEvent);
3085 } else {
3086 // Delay installation if the extension is not idle.
3087 return !IsExtensionIdle(extension_id);
3088 }
[email protected]0db124b02012-11-07 04:55:053089}
[email protected]fdd679b2012-11-15 20:49:393090
[email protected]399583b2012-12-11 09:33:423091void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:453092 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
3093 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:423094 for (ExtensionSet::const_iterator it = extensions_.begin();
3095 it != extensions_.end(); ++it) {
[email protected]cadac622013-06-11 16:46:363096 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
3097 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
3098 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:423099 }
3100 }
3101
[email protected]9f4e4f082013-06-21 07:11:193102 DCHECK(!installs_delayed_for_gc());
3103 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:423104 BrowserContext::GarbageCollectStoragePartitions(
3105 profile_, active_paths.Pass(),
3106 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3107 AsWeakPtr()));
3108}
3109
3110void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:193111 set_installs_delayed_for_gc(false);
3112 MaybeFinishDelayedInstallations();
3113}
3114
3115void ExtensionService::MaybeFinishDelayedInstallations() {
3116 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:423117 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3118 it != delayed_installs_.end();
3119 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193120 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:423121 }
[email protected]9f4e4f082013-06-21 07:11:193122 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
3123 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:423124 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193125 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:423126 }
[email protected]399583b2012-12-11 09:33:423127}
3128
3129void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3130 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3131}
3132
[email protected]fdd679b2012-11-15 20:49:393133void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283134 blacklist_->GetBlacklistedIDs(
3135 GenerateInstalledExtensionsSet()->GetIDs(),
3136 base::Bind(&ExtensionService::ManageBlacklist,
3137 AsWeakPtr(),
3138 blacklisted_extensions_.GetIDs()));
3139}
3140
3141void ExtensionService::ManageBlacklist(
3142 const std::set<std::string>& old_blacklisted_ids,
3143 const std::set<std::string>& new_blacklisted_ids) {
3144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3145
[email protected]16bf7ba72013-08-23 11:52:543146 std::set<std::string> no_longer_blacklisted =
3147 base::STLSetDifference<std::set<std::string> >(old_blacklisted_ids,
3148 new_blacklisted_ids);
3149 std::set<std::string> not_yet_blacklisted =
3150 base::STLSetDifference<std::set<std::string> >(new_blacklisted_ids,
3151 old_blacklisted_ids);
[email protected]695b5712012-12-06 23:55:283152
3153 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3154 it != no_longer_blacklisted.end(); ++it) {
3155 scoped_refptr<const Extension> extension =
3156 blacklisted_extensions_.GetByID(*it);
[email protected]9f3c8532013-07-31 19:52:073157 DCHECK(extension.get()) << "Extension " << *it << " no longer blacklisted, "
3158 << "but it was never blacklisted.";
[email protected]dc24976f2013-06-02 21:15:093159 if (!extension.get())
[email protected]695b5712012-12-06 23:55:283160 continue;
3161 blacklisted_extensions_.Remove(*it);
[email protected]dc24976f2013-06-02 21:15:093162 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:093163 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:093164 extension->location(),
3165 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283166 }
3167
3168 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3169 it != not_yet_blacklisted.end(); ++it) {
3170 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]9f3c8532013-07-31 19:52:073171 DCHECK(extension.get()) << "Extension " << *it << " needs to be "
3172 << "blacklisted, but it's not installed.";
[email protected]dc24976f2013-06-02 21:15:093173 if (!extension.get())
[email protected]695b5712012-12-06 23:55:283174 continue;
3175 blacklisted_extensions_.Insert(extension);
3176 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:093177 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
3178 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283179 }
3180
3181 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393182}
[email protected]75bdcb872013-03-13 00:41:453183
3184void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
3185 update_observers_.AddObserver(observer);
3186}
3187
3188void ExtensionService::RemoveUpdateObserver(
3189 extensions::UpdateObserver* observer) {
3190 update_observers_.RemoveObserver(observer);
3191}