blob: 23f684e72e11aea79962b416b419b31ecdd8aee7 [file] [log] [blame]
[email protected]098fa7a2013-03-08 22:11:171// Copyright (c) 2013 The Chromium Authors. All rights reserved.
[email protected]6014d672008-12-05 00:38:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eaa7dd182010-12-14 11:09:005#include "chrome/browser/extensions/extension_service.h"
[email protected]6014d672008-12-05 00:38:256
[email protected]654512b2010-09-01 02:09:427#include <algorithm>
[email protected]695b5712012-12-06 23:55:288#include <iterator>
[email protected]97d2f1d2011-01-15 00:41:089#include <set>
[email protected]654512b2010-09-01 02:09:4210
[email protected]24b538a2010-02-27 01:22:4411#include "base/basictypes.h"
[email protected]05aad2da2011-10-28 10:12:3712#include "base/bind.h"
[email protected]e5af875f2011-10-10 21:09:1413#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1214#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2515#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3616#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:5718#include "base/prefs/pref_service.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]3ea1b182013-02-08 22:38:4120#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1721#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
[email protected]112158af2013-06-07 23:46:1823#include "base/strings/utf_string_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1424#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread_restrictions.h"
[email protected]41a17c52013-06-28 00:27:5326#include "base/time/time.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]211a3f32013-05-28 21:48:1829#include "chrome/browser/app_mode/app_mode_utils.h"
[email protected]15730c42009-09-03 00:03:2030#include "chrome/browser/browser_process.h"
[email protected]9ea0cd32013-07-12 01:50:3631#include "chrome/browser/chrome_notification_types.h"
[email protected]32b36c62012-10-24 05:37:4132#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1233#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4434#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]c39df1a2013-01-08 18:22:0735#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3136#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]4636c832013-01-11 02:10:1137#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]5db9ada2012-04-11 13:48:2038#include "chrome/browser/extensions/app_sync_data.h"
[email protected]d8c8f25f2011-11-02 18:18:0139#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1740#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3841#include "chrome/browser/extensions/data_deleter.h"
[email protected]ebc8d932013-09-11 23:47:2942#include "chrome/browser/extensions/event_router.h"
[email protected]62f051c2012-03-29 17:04:4443#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2444#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1845#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5646#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2247#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2948#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2349#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4350#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0451#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0852#include "chrome/browser/extensions/extension_system.h"
[email protected]612a1cb12012-10-17 13:18:0353#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2754#include "chrome/browser/extensions/external_provider_impl.h"
55#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0156#include "chrome/browser/extensions/installed_loader.h"
[email protected]fdd679b2012-11-15 20:49:3957#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3758#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3959#include "chrome/browser/extensions/permissions_updater.h"
[email protected]d8c8f25f2011-11-02 18:18:0160#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]75bdcb872013-03-13 00:41:4561#include "chrome/browser/extensions/update_observer.h"
[email protected]42a08162012-03-16 18:09:1162#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]8ecad5e2010-12-02 21:18:3363#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3964#include "chrome/browser/profiles/profile_manager.h"
[email protected]50736a12013-09-26 08:58:3465#include "chrome/browser/sync/sync_prefs.h"
[email protected]c8d407e2011-04-28 21:27:1766#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3167#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1568#include "chrome/browser/ui/webui/theme_source.h"
[email protected]e2eb43112009-05-29 21:19:5469#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0670#include "chrome/common/chrome_version_info.h"
[email protected]f6431be82013-09-07 02:53:4571#include "chrome/common/crash_keys.h"
[email protected]9367eabc2013-03-01 01:29:2972#include "chrome/common/extensions/background_info.h"
[email protected]5b1a0e22009-05-26 19:00:5873#include "chrome/common/extensions/extension.h"
[email protected]9f4e4f082013-06-21 07:11:1974#include "chrome/common/extensions/extension_constants.h"
[email protected]7c927b62010-02-24 09:54:1375#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5676#include "chrome/common/extensions/extension_messages.h"
[email protected]612a1cb12012-10-17 13:18:0377#include "chrome/common/extensions/feature_switch.h"
[email protected]15d4d2d2013-08-09 06:49:0378#include "chrome/common/extensions/features/feature_channel.h"
[email protected]06492ed2013-03-24 22:13:1479#include "chrome/common/extensions/incognito_handler.h"
[email protected]561e33d52013-04-03 06:58:4380#include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
[email protected]6b414c232013-06-05 07:53:3481#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
[email protected]9f4e4f082013-06-21 07:11:1982#include "chrome/common/extensions/manifest_handlers/shared_module_info.h"
[email protected]65348062013-01-15 07:27:2283#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]2cb5e302013-05-09 05:00:0684#include "chrome/common/extensions/permissions/permissions_data.h"
[email protected]8f3bcbd2013-06-05 08:42:4085#include "chrome/common/extensions/sync_helper.h"
[email protected]25b34332009-06-05 21:53:1986#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1487#include "chrome/common/url_constants.h"
[email protected]7a83dd072013-09-03 12:09:1788#include "components/startup_metric_utils/startup_metric_utils.h"
[email protected]c38831a12011-10-28 12:44:4989#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3690#include "content/public/browser/devtools_agent_host.h"
[email protected]05aad2da2011-10-28 10:12:3791#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1692#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3093#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:4294#include "content/public/browser/site_instance.h"
95#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:5396#include "content/public/browser/url_data_source.h"
[email protected]836e2982013-05-16 08:07:4297#include "extensions/common/constants.h"
[email protected]e9f541a2012-11-19 21:52:3198#include "extensions/common/error_utils.h"
[email protected]d42c11152013-08-22 19:36:3299#include "extensions/common/manifest.h"
[email protected]0c3c9732013-09-16 08:53:41100#include "extensions/common/manifest_constants.h"
[email protected]65187152012-06-02 13:14:14101#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25102#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12103#include "sync/api/sync_change.h"
104#include "sync/api/sync_error_factory.h"
[email protected]4941fe92013-06-13 23:21:01105#include "ui/webui/web_ui_util.h"
[email protected]a6483d22013-07-03 22:11:00106#include "url/gurl.h"
[email protected]08b1f75f2013-05-22 22:02:38107#include "webkit/browser/database/database_tracker.h"
108#include "webkit/browser/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18109
[email protected]eed367e2011-04-12 03:43:31110#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18111#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]f19bbf62013-07-09 01:22:32112#include "webkit/browser/fileapi/file_system_backend.h"
[email protected]c6f9203a2013-05-28 02:08:07113#include "webkit/browser/fileapi/file_system_context.h"
[email protected]eed367e2011-04-12 03:43:31114#endif
115
[email protected]55eb70e762012-02-20 17:38:39116using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39117using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36118using content::DevToolsAgentHost;
[email protected]bf3d9df2012-07-24 23:20:27119using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34120using extensions::Extension;
121using extensions::ExtensionIdSet;
122using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42123using extensions::FeatureSwitch;
[email protected]1d5e58b2013-01-31 08:41:40124using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06125using extensions::PermissionMessage;
126using extensions::PermissionMessages;
127using extensions::PermissionSet;
[email protected]9f4e4f082013-06-21 07:11:19128using extensions::SharedModuleInfo;
[email protected]e410b5f2012-12-14 14:02:24129using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05130
[email protected]0c3c9732013-09-16 08:53:41131namespace errors = extensions::manifest_errors;
[email protected]c6d474f82009-12-16 21:11:06132
[email protected]b6ab96d2009-08-20 18:58:19133namespace {
134
[email protected]d96eb512012-11-01 23:44:08135// Histogram values for logging events related to externally installed
136// extensions.
137enum ExternalExtensionEvent {
138 EXTERNAL_EXTENSION_INSTALLED = 0,
139 EXTERNAL_EXTENSION_IGNORED,
140 EXTERNAL_EXTENSION_REENABLED,
141 EXTERNAL_EXTENSION_UNINSTALLED,
142 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
143};
144
[email protected]612a1cb12012-10-17 13:18:03145// Prompt the user this many times before considering an extension acknowledged.
146static const int kMaxExtensionAcknowledgePromptCount = 3;
147
[email protected]0db124b02012-11-07 04:55:05148// Wait this many seconds after an extensions becomes idle before updating it.
149static const int kUpdateIdleDelay = 5;
150
[email protected]9bd9a6862012-11-29 09:24:22151// Wait this many seconds before trying to garbage collect extensions again.
152static const int kGarbageCollectRetryDelay = 30;
153
[email protected]36fc54f2013-06-14 02:49:34154// Wait this many seconds after startup to see if there are any extensions
155// which can be garbage collected.
156static const int kGarbageCollectStartupDelay = 30;
157
[email protected]9f4e4f082013-06-21 07:11:19158static bool IsSharedModule(const Extension* extension) {
159 return SharedModuleInfo::IsSharedModule(extension);
160}
161
[email protected]4c9201c42013-08-16 04:56:21162static bool IsCWSSharedModule(const Extension* extension) {
163 return extension->from_webstore() && IsSharedModule(extension);
164}
165
166class SharedModuleProvider : public extensions::ManagementPolicy::Provider {
167 public:
168 SharedModuleProvider() {}
169 virtual ~SharedModuleProvider() {}
170
171 virtual std::string GetDebugPolicyProviderName() const OVERRIDE {
172 return "SharedModuleProvider";
173 }
174
175 virtual bool UserMayModifySettings(const Extension* extension,
176 string16* error) const OVERRIDE {
177 return !IsCWSSharedModule(extension);
178 }
179
180 virtual bool MustRemainEnabled(const Extension* extension,
181 string16* error) const OVERRIDE {
182 return IsCWSSharedModule(extension);
183 }
184
185 private:
186 DISALLOW_COPY_AND_ASSIGN(SharedModuleProvider);
187};
188
189
[email protected]c6d474f82009-12-16 21:11:06190} // namespace
[email protected]b6ab96d2009-08-20 18:58:19191
[email protected]eaa7dd182010-12-14 11:09:00192ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49193 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57194 being_upgraded(false),
195 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49196}
197
[email protected]eaa7dd182010-12-14 11:09:00198ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49199}
200
[email protected]eaa7dd182010-12-14 11:09:00201// ExtensionService.
[email protected]6014d672008-12-05 00:38:25202
[email protected]8e4560b62011-01-14 10:09:14203void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12204 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20205
[email protected]7a4c6852010-09-16 03:44:22206 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27207 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21208 for (i = external_extension_providers_.begin();
209 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14210 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21211 if (i->get()->HasExtension(id))
212 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22213 }
214
[email protected]0f48fca2011-05-19 18:46:35215 // We get the list of external extensions to check from preferences.
216 // It is possible that an extension has preferences but is not loaded.
217 // For example, an extension that requires experimental permissions
218 // will not be loaded if the experimental command line flag is not used.
219 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40220 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35221 // We can't call UninstallExtension with an unloaded/invalid
222 // extension ID.
223 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
224 << "with id: " << id;
225 return;
226 }
[email protected]d6ebc9792011-04-07 18:18:33227 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22228}
229
[email protected]7f8f24f2012-11-15 19:40:14230void ExtensionService::SetFileTaskRunnerForTesting(
231 base::SequencedTaskRunner* task_runner) {
232 file_task_runner_ = task_runner;
233}
234
[email protected]8e4560b62011-01-14 10:09:14235void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22236 external_extension_providers_.clear();
237}
238
[email protected]8e4560b62011-01-14 10:09:14239void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27240 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12241 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21242 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27243 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22244}
245
[email protected]9060d8b02012-01-13 02:14:30246bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22247 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42248 const GURL& update_url,
[email protected]1d5e58b2013-01-31 08:41:40249 Manifest::Location location) {
[email protected]ab22ba42011-01-14 16:36:38250 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
251 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20252
[email protected]8a87a5332011-08-11 17:54:59253 const Extension* extension = GetExtensionById(id, true);
254 if (extension) {
255 // Already installed. Skip this install if the current location has
256 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40257 Manifest::Location current = extension->location();
258 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30259 return false;
[email protected]8a87a5332011-08-11 17:54:59260 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22261 }
[email protected]9060d8b02012-01-13 02:14:30262
263 // Add |id| to the set of pending extensions. If it can not be added,
264 // then there is already a pending record from a higher-priority install
265 // source. In this case, signal that this extension will not be
266 // installed by returning false.
267 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40268 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30269 return false;
[email protected]31bb5ee62012-09-12 22:58:40270 }
[email protected]9060d8b02012-01-13 02:14:30271
[email protected]94fde232012-04-27 10:22:30272 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30273 return true;
[email protected]7a4c6852010-09-16 03:44:22274}
275
[email protected]dc9a74f72012-08-17 18:07:21276const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]be9915fb2013-07-18 09:28:55277 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]dc9a74f72012-08-17 18:07:21278 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09279}
280
[email protected]dc9a74f72012-08-17 18:07:21281bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09282 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27283}
284
[email protected]404fb002012-10-02 21:06:17285const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21286 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17287 std::set<std::string> extension_ids =
288 process_map_.GetExtensionsInProcess(renderer_child_id);
289 // All apps in one process share the same partition.
290 // It is only possible for the app to have isolated storage
291 // if there is only 1 app in the process.
292 if (extension_ids.size() != 1)
293 return NULL;
294
295 const extensions::Extension* extension =
296 extensions_.GetByID(*(extension_ids.begin()));
[email protected]561e33d52013-04-03 06:58:43297 // We still need to check if the extension has isolated storage,
[email protected]404fb002012-10-02 21:06:17298 // because it's common for there to be one extension in a process
[email protected]561e33d52013-04-03 06:58:43299 // without isolated storage.
300 if (extension &&
301 extensions::AppIsolationInfo::HasIsolatedStorage(extension))
[email protected]404fb002012-10-02 21:06:17302 return extension;
303
304 return NULL;
[email protected]da5683db2011-04-23 17:12:21305}
306
[email protected]6aeac8342010-10-01 20:21:18307// static
[email protected]d6ebc9792011-04-07 18:18:33308// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30309// --uninstall-extension, and to uninstall an extension via sync. The LOG
310// statements within this function are used to inform the user if the uninstall
311// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00312bool ExtensionService::UninstallExtensionHelper(
313 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18314 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33315 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40316 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18317 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33318 << "id: " << extension_id;
319 return false;
[email protected]6aeac8342010-10-01 20:21:18320 }
321
[email protected]d6ebc9792011-04-07 18:18:33322 // The following call to UninstallExtension will not allow an uninstall of a
323 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14324 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33325 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
326 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
327 << ": " << error;
328 return false;
329 }
[email protected]95da88c42011-03-31 10:07:33330
[email protected]6aeac8342010-10-01 20:21:18331 return true;
332}
333
[email protected]eaa7dd182010-12-14 11:09:00334ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24335 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45336 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23337 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39338 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03339 bool autoupdate_enabled,
[email protected]4a10006a2013-05-17 23:18:35340 bool extensions_enabled,
341 extensions::OneShotEvent* ready)
[email protected]fdd679b2012-11-15 20:49:39342 : extensions::Blacklist::Observer(blacklist),
343 profile_(profile),
[email protected]bd306722012-07-11 20:43:59344 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43345 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28346 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38347 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]9c009092013-05-01 03:14:09348 pending_extension_manager_(*this),
[email protected]a9b00ac2009-06-25 21:03:23349 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03350 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13351 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33352 install_updates_when_idle_(true),
[email protected]4a10006a2013-05-17 23:18:35353 ready_(ready),
[email protected]9c009092013-05-01 03:14:09354 toolbar_model_(this),
[email protected]8b280302011-10-13 22:22:23355 menu_manager_(profile),
[email protected]dff1e042012-04-27 10:59:18356 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54357 browser_terminating_(false),
[email protected]9f4e4f082013-06-21 07:11:19358 installs_delayed_for_gc_(false),
[email protected]460c6712013-04-24 07:20:01359 is_first_run_(false),
[email protected]9c009092013-05-01 03:14:09360 app_sync_bundle_(this),
[email protected]50736a12013-09-26 08:58:34361 extension_sync_bundle_(this),
362 pending_app_enables_(
363 make_scoped_ptr(new browser_sync::SyncPrefs(
364 extension_prefs_->pref_service())),
365 &app_sync_bundle_,
366 syncer::APPS),
367 pending_extension_enables_(
368 make_scoped_ptr(new browser_sync::SyncPrefs(
369 extension_prefs_->pref_service())),
370 &extension_sync_bundle_,
371 syncer::EXTENSIONS) {
[email protected]c04fd3dd2013-09-05 08:20:10372#if defined(OS_CHROMEOS)
373 disable_garbage_collection_ = false;
374#endif
[email protected]a29a517a2011-01-21 21:11:12375 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20376
[email protected]36a784c2009-06-23 06:21:08377 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40378 if (command_line->HasSwitch(switches::kDisableExtensions) ||
379 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23380 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18381 }
[email protected]36a784c2009-06-23 06:21:08382
[email protected]3c4abc82012-10-22 22:25:54383 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
384 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27385 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07386 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27387 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07388 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27389 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07390 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05391 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
392 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08393 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
394 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28395 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03396 base::Closure callback =
397 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
398 base::Unretained(this));
399 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
400 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24401 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29402
[email protected]93fd78f42009-07-10 16:43:17403 // Set up the ExtensionUpdater
404 if (autoupdate_enabled) {
[email protected]501105b2013-09-26 05:42:02405 int update_frequency = extensions::kDefaultUpdateFrequencySeconds;
[email protected]93fd78f42009-07-10 16:43:17406 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25407 base::StringToInt(command_line->GetSwitchValueASCII(
408 switches::kExtensionsUpdateFrequency),
409 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17410 }
[email protected]42a08162012-03-16 18:09:11411 updater_.reset(new extensions::ExtensionUpdater(this,
412 extension_prefs,
413 profile->GetPrefs(),
414 profile,
415 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17416 }
417
[email protected]25ae0152011-11-18 14:40:02418 component_loader_.reset(
419 new extensions::ComponentLoader(this,
420 profile->GetPrefs(),
421 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14422
[email protected]0436b102011-04-15 18:30:03423 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25424 extensions::ExternalProviderImpl::CreateExternalProviders(
425 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05426 }
[email protected]b671760b2010-07-15 21:13:47427
[email protected]fb82dcd2012-03-21 14:15:46428 // Set this as the ExtensionService for extension sorting to ensure it
429 // cause syncs if required.
430 extension_prefs_->extension_sorting()->SetExtensionService(this);
431
[email protected]b3aa7182013-04-25 04:45:23432 is_first_run_ = !extension_prefs_->SetAlertSystemFirstRun();
433
[email protected]9ce11d22012-08-08 23:20:13434#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58435 extension_action_storage_manager_.reset(
436 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13437#endif
[email protected]c77f2352012-08-08 22:07:58438
[email protected]4c9201c42013-08-16 04:56:21439 shared_module_policy_provider_.reset(new SharedModuleProvider);
440
[email protected]cb0e50312011-05-09 15:03:07441 // How long is the path to the Extensions directory?
442 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
443 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25444}
445
[email protected]84df8332011-12-06 18:22:46446const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45447 return &extensions_;
448}
449
[email protected]84df8332011-12-06 18:22:46450const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45451 return &disabled_extensions_;
452}
453
[email protected]84df8332011-12-06 18:22:46454const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20455 return &terminated_extensions_;
456}
457
[email protected]695b5712012-12-06 23:55:28458const ExtensionSet* ExtensionService::blacklisted_extensions() const {
459 return &blacklisted_extensions_;
460}
461
[email protected]9e9c1d12013-07-31 01:58:12462const ExtensionSet* ExtensionService::delayed_installs() const {
463 return &delayed_installs_;
464}
465
[email protected]695b5712012-12-06 23:55:28466scoped_ptr<const ExtensionSet>
467 ExtensionService::GenerateInstalledExtensionsSet() const {
468 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01469 installed_extensions->InsertAll(extensions_);
470 installed_extensions->InsertAll(disabled_extensions_);
471 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28472 installed_extensions->InsertAll(blacklisted_extensions_);
473 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01474}
475
[email protected]3f213ad2012-07-26 23:39:41476extensions::PendingExtensionManager*
477 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37478 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45479}
480
[email protected]eaa7dd182010-12-14 11:09:00481ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17482 // No need to unload extensions here because they are profile-scoped, and the
483 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17484
[email protected]5df038b2012-07-16 19:03:27485 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14486 for (i = external_extension_providers_.begin();
487 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27488 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14489 provider->ServiceShutdown();
490 }
[email protected]6014d672008-12-05 00:38:25491}
492
[email protected]6d147912013-07-09 08:02:14493void ExtensionService::SetSyncStartFlare(
494 const syncer::SyncableService::StartSyncFlare& flare) {
495 flare_ = flare;
496}
497
[email protected]037228a2012-10-05 01:36:16498void ExtensionService::Shutdown() {
[email protected]4c9201c42013-08-16 04:56:21499 system_->management_policy()->UnregisterProvider(
500 shared_module_policy_provider_.get());
[email protected]037228a2012-10-05 01:36:16501}
502
[email protected]b2907fd2011-03-25 16:43:37503const Extension* ExtensionService::GetExtensionById(
504 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47505 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28506 if (include_disabled) {
507 // Include blacklisted extensions here because there are hundreds of
508 // callers of this function, and many might assume that this includes those
509 // that have been disabled due to blacklisting.
510 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
511 }
512 return GetExtensionById(id, include_mask);
513}
514
[email protected]399583b2012-12-11 09:33:42515GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
516 return content::SiteInstance::GetSiteForURL(
517 profile_,
518 Extension::GetBaseURLFromExtensionId(extension_id));
519}
520
[email protected]695b5712012-12-06 23:55:28521const Extension* ExtensionService::GetExtensionById(
522 const std::string& id, int include_mask) const {
523 std::string lowercase_id = StringToLowerASCII(id);
524 if (include_mask & INCLUDE_ENABLED) {
525 const Extension* extension = extensions_.GetByID(lowercase_id);
526 if (extension)
527 return extension;
528 }
529 if (include_mask & INCLUDE_DISABLED) {
530 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
531 if (extension)
532 return extension;
533 }
534 if (include_mask & INCLUDE_TERMINATED) {
535 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
536 if (extension)
537 return extension;
538 }
539 if (include_mask & INCLUDE_BLACKLISTED) {
540 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
541 if (extension)
542 return extension;
543 }
544 return NULL;
[email protected]78994ab02010-12-08 18:06:44545}
546
[email protected]eaa7dd182010-12-14 11:09:00547void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12548 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20549
[email protected]4a10006a2013-05-17 23:18:35550 DCHECK(!is_ready()); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17551 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32552
[email protected]820d9bd2013-04-03 03:46:03553 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]820d9bd2013-04-03 03:46:03554 if (cmd_line->HasSwitch(switches::kInstallFromWebstore) ||
555 cmd_line->HasSwitch(switches::kLimitedInstallFromWebstore)) {
556 // The sole purpose of this launch is to install a new extension from CWS
557 // and immediately terminate: loading already installed extensions is
558 // unnecessary and may interfere with the inline install dialog (e.g. if an
559 // extension listens to onStartup and opens a window).
560 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25561 } else {
[email protected]47b60072013-06-20 10:35:56562 // LoadAllExtensions() calls OnLoadedInstalledExtensions().
563 component_loader_->LoadAll();
564 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f4e4f082013-06-21 07:11:19565
[email protected]2edd57852013-08-14 22:07:40566 // Attempt to re-enable extensions whose only disable reason is reloading.
567 std::vector<std::string> extensions_to_enable;
568 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
569 iter != disabled_extensions_.end(); ++iter) {
570 const Extension* e = iter->get();
571 if (extension_prefs_->GetDisableReasons(e->id()) ==
572 Extension::DISABLE_RELOAD) {
573 extensions_to_enable.push_back(e->id());
574 }
575 }
576 for (std::vector<std::string>::iterator it = extensions_to_enable.begin();
577 it != extensions_to_enable.end(); ++it) {
578 EnableExtension(*it);
579 }
580
[email protected]9f4e4f082013-06-21 07:11:19581 // Finish install (if possible) of extensions that were still delayed while
582 // the browser was shut down.
583 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info(
584 extension_prefs_->GetAllDelayedInstallInfo());
585 for (size_t i = 0; i < delayed_info->size(); ++i) {
586 ExtensionInfo* info = delayed_info->at(i).get();
587 scoped_refptr<const Extension> extension(NULL);
588 if (info->extension_manifest) {
589 std::string error;
590 extension = Extension::Create(
591 info->extension_path,
592 info->extension_location,
593 *info->extension_manifest,
594 extension_prefs_->GetDelayedInstallCreationFlags(
595 info->extension_id),
596 info->extension_id,
597 &error);
598 if (extension.get())
599 delayed_installs_.Insert(extension);
600 }
601 }
602 MaybeFinishDelayedInstallations();
603
604 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> delayed_info2(
605 extension_prefs_->GetAllDelayedInstallInfo());
606 UMA_HISTOGRAM_COUNTS_100("Extensions.UpdateOnLoad",
607 delayed_info2->size() - delayed_info->size());
608
[email protected]47b60072013-06-20 10:35:56609 SetReadyAndNotifyListeners();
[email protected]ae4c37e2012-12-21 01:16:25610
[email protected]47b60072013-06-20 10:35:56611 // TODO(erikkay) this should probably be deferred to a future point
612 // rather than running immediately at startup.
613 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57614
[email protected]47b60072013-06-20 10:35:56615 base::MessageLoop::current()->PostDelayedTask(
616 FROM_HERE,
617 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
618 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
[email protected]215a7be2012-10-22 19:53:42619
[email protected]820d9bd2013-04-03 03:46:03620 if (extension_prefs_->NeedsStorageGarbageCollection()) {
621 GarbageCollectIsolatedStorage();
622 extension_prefs_->SetNeedsStorageGarbageCollection(false);
623 }
[email protected]4c9201c42013-08-16 04:56:21624 system_->management_policy()->RegisterProvider(
625 shared_module_policy_provider_.get());
[email protected]399583b2012-12-11 09:33:42626 }
[email protected]6014d672008-12-05 00:38:25627}
628
[email protected]31bb5ee62012-09-12 22:58:40629bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45630 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40631 const GURL& download_url,
632 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12633 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54634 if (browser_terminating_) {
635 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
636 // Leak the temp file at extension_path. We don't want to add to the disk
637 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
638 // the file is in the OS temp directory which should be cleaned up for us.
639 return false;
640 }
[email protected]a8af9fdb2010-10-28 21:52:20641
[email protected]3f213ad2012-07-26 23:39:41642 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06643 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32644
[email protected]695b5712012-12-06 23:55:28645 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06646 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33647 LOG(WARNING) << "Will not update extension " << id
648 << " because it is not installed or pending";
649 // Delete extension_path since we're not creating a CrxInstaller
650 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14651 if (!GetFileTaskRunner()->PostTask(
652 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36653 base::Bind(
[email protected]7296f2762011-11-21 19:23:44654 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36655 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03656
657 return false;
[email protected]e957fe52009-06-23 16:51:05658 }
659
[email protected]aa142702010-03-26 01:26:33660 // We want a silent install only for non-pending extensions and
661 // pending extensions that have install_silently set.
[email protected]f8636f92013-08-09 21:02:37662 scoped_ptr<ExtensionInstallPrompt> client;
[email protected]91e51d612012-10-21 23:03:05663 if (pending_extension_info && !pending_extension_info->install_silently())
[email protected]f8636f92013-08-09 21:02:37664 client.reset(ExtensionInstallUI::CreateInstallPromptWithProfile(profile_));
[email protected]aa142702010-03-26 01:26:33665
[email protected]f8636f92013-08-09 21:02:37666 scoped_refptr<CrxInstaller> installer(
667 CrxInstaller::Create(this, client.Pass()));
[email protected]6dfbbf82010-03-12 23:09:16668 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06669 if (pending_extension_info) {
670 installer->set_install_source(pending_extension_info->install_source());
671 if (pending_extension_info->install_silently())
672 installer->set_allow_silent_install(true);
673 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41674 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06675 }
[email protected]7d8b7072012-04-07 01:07:46676 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45677 // its auto-update URL is from the webstore, treat it as a webstore install.
678 // Note that we ignore some older extensions with blank auto-update URLs
679 // because we are mostly concerned with restrictions on NaCl extensions,
680 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53681 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28682 if ((extension && extension->from_webstore()) ||
[email protected]4a5fe3e22013-06-04 07:06:38683 (extension && extensions::ManifestURL::UpdatesFromGallery(extension)) ||
[email protected]963d13c2012-09-29 17:13:35684 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06685 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53686 creation_flags |= Extension::FROM_WEBSTORE;
687 }
688
689 // Bookmark apps being updated is kind of a contradiction, but that's because
690 // we mark the default apps as bookmark apps, and they're hosted in the web
691 // store, thus they can get updated. See http://crbug.com/101605 for more
692 // details.
693 if (extension && extension->from_bookmark())
694 creation_flags |= Extension::FROM_BOOKMARK;
695
[email protected]e33bbc22012-08-27 22:05:46696 if (extension && extension->was_installed_by_default())
697 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
698
[email protected]a12ce8b22012-01-17 18:40:53699 installer->set_creation_flags(creation_flags);
700
[email protected]6dfbbf82010-03-12 23:09:16701 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06702 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07703 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16704 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03705
706 if (out_crx_installer)
[email protected]dc24976f2013-06-02 21:15:09707 *out_crx_installer = installer.get();
[email protected]420a0ec2011-06-01 01:07:03708
709 return true;
[email protected]e957fe52009-06-23 16:51:05710}
711
[email protected]e178ad92013-06-28 02:29:25712void ExtensionService::ReloadExtension(const std::string extension_id) {
[email protected]a29a517a2011-01-21 21:11:12713 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2a947202013-03-06 04:58:05714
715 // If the extension is already reloading, don't reload again.
716 if (extension_prefs_->GetDisableReasons(extension_id) &
717 Extension::DISABLE_RELOAD) {
718 return;
719 }
720
[email protected]650b2d52013-02-10 03:41:45721 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02722 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40723
[email protected]f17dbd42010-08-16 23:21:10724 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06725 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29726 // If the extension has an inspector open for its background page, detach
727 // the inspector and hang onto a cookie for it, so that we can reattach
728 // later.
[email protected]31d8f5f22012-04-02 15:22:08729 // TODO(yoz): this is not incognito-safe!
730 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13731 extensions::ExtensionHost* host =
732 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36733 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29734 // Look for an open inspector for the background page.
[email protected]04ea1bb2013-07-10 09:26:09735 scoped_refptr<DevToolsAgentHost> agent_host =
736 DevToolsAgentHost::GetOrCreateFor(host->render_view_host());
737 agent_host->DisconnectRenderViewHost();
738 orphaned_dev_tools_[extension_id] = agent_host;
[email protected]4814b512009-11-07 00:12:29739 }
740
[email protected]b65272f2009-08-31 15:47:06741 path = current_extension->path();
[email protected]58076192013-07-19 19:43:50742 // BeingUpgraded is set back to false when the extension is added.
743 SetBeingUpgraded(current_extension, true);
[email protected]44d62b62012-04-11 00:06:03744 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]b914e2952013-04-26 07:10:03745 reloading_extensions_.insert(extension_id);
[email protected]1eb175082010-02-10 09:26:16746 } else {
747 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06748 }
749
[email protected]9f4e4f082013-06-21 07:11:19750 if (delayed_installs_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48751 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05752 return;
753 }
754
[email protected]43ceb002012-02-10 23:19:15755 // If we're reloading a component extension, use the component extension
756 // loader's reloader.
757 if (component_loader_->Exists(extension_id)) {
[email protected]e178ad92013-06-28 02:29:25758 SetBeingReloaded(extension_id, true);
[email protected]43ceb002012-02-10 23:19:15759 component_loader_->Reload(extension_id);
[email protected]e178ad92013-06-28 02:29:25760 SetBeingReloaded(extension_id, false);
[email protected]43ceb002012-02-10 23:19:15761 return;
762 }
763
[email protected]e6090e42010-03-23 22:44:08764 // Check the installed extensions to see if what we're reloading was already
765 // installed.
[email protected]e178ad92013-06-28 02:29:25766 SetBeingReloaded(extension_id, true);
[email protected]e6090e42010-03-23 22:44:08767 scoped_ptr<ExtensionInfo> installed_extension(
768 extension_prefs_->GetInstalledExtensionInfo(extension_id));
769 if (installed_extension.get() &&
770 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01771 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08772 } else {
[email protected]d8c8f25f2011-11-02 18:18:01773 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08774 // We should always be able to remember the extension's path. If it's not in
775 // the map, someone failed to update |unloaded_extension_paths_|.
776 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01777 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08778 }
[email protected]e178ad92013-06-28 02:29:25779 // When reloading is done, mark this extension as done reloading.
780 SetBeingReloaded(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40781}
782
[email protected]fa2416f2011-05-03 08:41:20783bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08784 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20785 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14786 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12787 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20788
[email protected]0d6ec3a72011-09-02 02:09:43789 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25790
[email protected]e7afe2452010-08-22 16:19:13791 // Callers should not send us nonexistent extensions.
[email protected]dc24976f2013-06-02 21:15:09792 CHECK(extension.get());
[email protected]9f1087e2009-06-15 17:29:32793
[email protected]95da88c42011-03-31 10:07:33794 // Policy change which triggers an uninstall will always set
795 // |external_uninstall| to true so this is the only way to uninstall
796 // managed extensions.
[email protected]65187152012-06-02 13:14:14797 if (!external_uninstall &&
798 !system_->management_policy()->UserMayModifySettings(
799 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07800 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27801 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53802 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09803 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33804 return false;
805 }
[email protected]95da88c42011-03-31 10:07:33806
[email protected]3bdba0d2011-08-23 07:17:30807 // Extract the data we need for sync now, but don't actually sync until we've
808 // completed the uninstallation.
[email protected]6d147912013-07-09 08:02:14809 // TODO(tim): If we get here and IsSyncing is false, this will cause
810 // "back from the dead" style bugs, because sync will add-back the extension
811 // that was uninstalled here when MergeDataAndStartSyncing is called.
812 // See crbug.com/256795.
[email protected]65f173552012-06-28 22:43:58813 syncer::SyncChange sync_change;
[email protected]6d147912013-07-09 08:02:14814 if (extensions::sync_helper::IsSyncableApp(extension.get())) {
815 if (app_sync_bundle_.IsSyncing())
816 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension.get());
817 else if (is_ready() && !flare_.is_null())
818 flare_.Run(syncer::APPS); // Tell sync to start ASAP.
819 } else if (extensions::sync_helper::IsSyncableExtension(extension.get())) {
820 if (extension_sync_bundle_.IsSyncing())
821 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
822 else if (is_ready() && !flare_.is_null())
823 flare_.Run(syncer::EXTENSIONS); // Tell sync to start ASAP.
[email protected]3bdba0d2011-08-23 07:17:30824 }
825
[email protected]dc24976f2013-06-02 21:15:09826 if (IsUnacknowledgedExternalExtension(extension.get())) {
[email protected]d96eb512012-11-01 23:44:08827 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
828 EXTERNAL_EXTENSION_UNINSTALLED,
829 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02830 if (extensions::ManifestURL::UpdatesFromGallery(extension.get())) {
831 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
832 EXTERNAL_EXTENSION_UNINSTALLED,
833 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
834 } else {
835 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
836 EXTERNAL_EXTENSION_UNINSTALLED,
837 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
838 }
[email protected]d96eb512012-11-01 23:44:08839 }
[email protected]9b217652010-10-08 22:04:23840 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08841 extension->GetType(), 100);
[email protected]dc24976f2013-06-02 21:15:09842 RecordPermissionMessagesHistogram(extension.get(),
843 "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23844
[email protected]831aa212010-03-26 13:55:19845 // Unload before doing more cleanup to ensure that nothing is hanging on to
846 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59847 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19848
[email protected]9f1087e2009-06-15 17:29:32849 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05850 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14851 if (!GetFileTaskRunner()->PostTask(
852 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36853 base::Bind(
[email protected]14908b72011-04-20 06:54:36854 &extension_file_util::UninstallExtension,
855 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20856 extension_id)))
[email protected]14908b72011-04-20 06:54:36857 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32858 }
859
[email protected]6b414c232013-06-05 07:53:34860 GURL launch_web_url_origin(
[email protected]cadac622013-06-11 16:46:36861 extensions::AppLaunchInfo::GetLaunchWebURL(extension.get()).GetOrigin());
[email protected]561e33d52013-04-03 06:58:43862 bool is_storage_isolated =
[email protected]dc24976f2013-06-02 21:15:09863 extensions::AppIsolationInfo::HasIsolatedStorage(extension.get());
[email protected]0d6ec3a72011-09-02 02:09:43864
[email protected]399583b2012-12-11 09:33:42865 if (is_storage_isolated) {
866 BrowserContext::AsyncObliterateStoragePartition(
867 profile_,
868 GetSiteForExtensionId(extension_id),
869 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
870 AsWeakPtr()));
871 } else {
872 if (extension->is_hosted_app() &&
873 !profile_->GetExtensionSpecialStoragePolicy()->
874 IsStorageProtected(launch_web_url_origin)) {
875 extensions::DataDeleter::StartDeleting(
876 profile_, extension_id, launch_web_url_origin);
877 }
878 extensions::DataDeleter::StartDeleting(profile_, extension_id,
879 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43880 }
[email protected]0d6ec3a72011-09-02 02:09:43881
[email protected]0dfe05c2011-02-23 23:03:36882 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06883
884 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07885 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27886 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53887 content::Source<Profile>(profile_),
[email protected]dc24976f2013-06-02 21:15:09888 content::Details<const Extension>(extension.get()));
[email protected]d6ebc9792011-04-07 18:18:33889
[email protected]5db9ada2012-04-11 13:48:20890 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32891 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20892 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
893 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32894 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20895 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30896 }
897
[email protected]399583b2012-12-11 09:33:42898 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05899
[email protected]198b5902013-06-27 10:36:11900 PruneSharedModulesOnUninstall(extension.get());
[email protected]9f4e4f082013-06-21 07:11:19901
[email protected]d4eda592013-09-18 03:37:57902 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
903 external_uninstall);
904
[email protected]333b1de2011-09-12 18:28:50905 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18906 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
907
[email protected]d6ebc9792011-04-07 18:18:33908 return true;
[email protected]c10da4b02010-03-25 14:38:32909}
910
[email protected]c3cfb012011-04-06 22:07:35911bool ExtensionService::IsExtensionEnabled(
912 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46913 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21914 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18915 return true;
[email protected]dc9a74f72012-08-17 18:07:21916 }
[email protected]36429da2011-07-11 20:25:18917
[email protected]695b5712012-12-06 23:55:28918 if (disabled_extensions_.Contains(extension_id) ||
919 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25920 return false;
[email protected]695b5712012-12-06 23:55:28921 }
[email protected]ad83ca242011-07-29 01:32:25922
923 // If the extension hasn't been loaded yet, check the prefs for it. Assume
924 // enabled unless otherwise noted.
925 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31926 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35927}
928
929bool ExtensionService::IsExternalExtensionUninstalled(
930 const std::string& extension_id) const {
931 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
932}
933
[email protected]e2377032013-01-18 17:53:22934bool ExtensionService::IsExtensionEnabledForLauncher(
935 const std::string& extension_id) const {
936 return IsExtensionEnabled(extension_id) &&
937 !GetTerminatedExtension(extension_id);
938}
939
[email protected]eaa7dd182010-12-14 11:09:00940void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12941 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20942
[email protected]06f92562011-04-29 19:27:31943 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39944 return;
[email protected]0c6da502009-08-14 22:32:39945
[email protected]b3317ad2011-04-28 23:46:00946 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28947 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52948
[email protected]695b5712012-12-06 23:55:28949 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31950 // This can happen if sync enables an extension that is not
951 // installed yet.
952 if (!extension)
953 return;
954
[email protected]d96eb512012-11-01 23:44:08955 if (IsUnacknowledgedExternalExtension(extension)) {
956 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
957 EXTERNAL_EXTENSION_REENABLED,
958 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:02959 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
960 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
961 EXTERNAL_EXTENSION_REENABLED,
962 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
963 } else {
964 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
965 EXTERNAL_EXTENSION_REENABLED,
966 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
967 }
[email protected]612a1cb12012-10-17 13:18:03968 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08969 }
[email protected]612a1cb12012-10-17 13:18:03970
[email protected]0c6da502009-08-14 22:32:39971 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46972 extensions_.Insert(make_scoped_refptr(extension));
973 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39974
[email protected]62d30f42009-10-01 22:36:06975 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30976
[email protected]f74d7f32012-06-19 19:22:51977 // Notify listeners that the extension was enabled.
978 content::NotificationService::current()->Notify(
979 chrome::NOTIFICATION_EXTENSION_ENABLED,
980 content::Source<Profile>(profile_),
981 content::Details<const Extension>(extension));
982
[email protected]50736a12013-09-26 08:58:34983 // Syncing may not have started yet, so handle pending enables.
984 if (extensions::sync_helper::IsSyncableApp(extension))
985 pending_app_enables_.OnExtensionEnabled(extension->id());
986
987 if (extensions::sync_helper::IsSyncableExtension(extension))
988 pending_extension_enables_.OnExtensionEnabled(extension->id());
989
[email protected]3bdba0d2011-08-23 07:17:30990 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39991}
992
[email protected]44d62b62012-04-11 00:06:03993void ExtensionService::DisableExtension(
994 const std::string& extension_id,
995 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12996 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20997
[email protected]b2ba9962009-12-10 20:10:15998 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31999 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:521000 return;
[email protected]1784e83a2009-09-08 21:01:521001
[email protected]06f92562011-04-29 19:27:311002 const Extension* extension = GetInstalledExtension(extension_id);
1003 // |extension| can be NULL if sync disables an extension that is not
1004 // installed yet.
[email protected]65187152012-06-02 13:14:141005 if (extension &&
[email protected]505ccdc2013-06-20 20:15:351006 disable_reason != Extension::DISABLE_RELOAD &&
[email protected]65187152012-06-02 13:14:141007 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:331008 return;
[email protected]65187152012-06-02 13:14:141009 }
[email protected]95da88c42011-03-31 10:07:331010
[email protected]b3317ad2011-04-28 23:46:001011 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:281012 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:521013
[email protected]695b5712012-12-06 23:55:281014 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
1015 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:311016 if (!extension)
1017 return;
1018
[email protected]64742de32013-04-22 08:44:341019 // Reset the background_page_ready flag
1020 if (extensions::BackgroundInfo::HasBackgroundPage(extension))
1021 extension_runtime_data_[extension->id()].background_page_ready = false;
1022
[email protected]5c094792012-09-07 19:44:531023 // Move it over to the disabled list. Don't send a second unload notification
1024 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:461025 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:531026 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461027 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:531028 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
1029 } else {
[email protected]84df8332011-12-06 18:22:461030 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:531031 }
[email protected]3bdba0d2011-08-23 07:17:301032
[email protected]50736a12013-09-26 08:58:341033 // Syncing may not have started yet, so handle pending enables.
1034 if (extensions::sync_helper::IsSyncableApp(extension))
1035 pending_app_enables_.OnExtensionDisabled(extension->id());
1036
1037 if (extensions::sync_helper::IsSyncableExtension(extension))
1038 pending_extension_enables_.OnExtensionDisabled(extension->id());
1039
[email protected]3bdba0d2011-08-23 07:17:301040 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:521041}
1042
[email protected]1abf05e2013-07-09 17:04:361043void ExtensionService::DisableUserExtensions(
1044 const std::vector<std::string>& except_ids) {
[email protected]e516e4c2013-06-12 17:41:141045 extensions::ManagementPolicy* management_policy =
1046 system_->management_policy();
1047 extensions::ExtensionList to_disable;
1048
1049 for (ExtensionSet::const_iterator extension = extensions_.begin();
1050 extension != extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111051 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141052 to_disable.push_back(*extension);
1053 }
1054 for (ExtensionSet::const_iterator extension = terminated_extensions_.begin();
1055 extension != terminated_extensions_.end(); ++extension) {
[email protected]198b5902013-06-27 10:36:111056 if (management_policy->UserMayModifySettings(extension->get(), NULL))
[email protected]e516e4c2013-06-12 17:41:141057 to_disable.push_back(*extension);
1058 }
1059
1060 for (extensions::ExtensionList::const_iterator extension = to_disable.begin();
1061 extension != to_disable.end(); ++extension) {
[email protected]3082fe32013-08-06 11:12:381062 if ((*extension)->was_installed_by_default() &&
1063 extension_urls::IsWebstoreUpdateUrl(
1064 extensions::ManifestURL::GetUpdateURL(*extension)))
1065 continue;
[email protected]1abf05e2013-07-09 17:04:361066 const std::string& id = (*extension)->id();
1067 if (except_ids.end() == std::find(except_ids.begin(), except_ids.end(), id))
1068 DisableExtension(id, extensions::Extension::DISABLE_USER_ACTION);
[email protected]e516e4c2013-06-12 17:41:141069 }
1070}
1071
[email protected]eaa7dd182010-12-14 11:09:001072void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]009633c2013-03-07 22:08:281073 const Extension* extension) {
1074 GrantPermissions(extension);
[email protected]fe2dd7742011-04-19 22:52:491075 RecordPermissionMessagesHistogram(
1076 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:251077 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
1078 EnableExtension(extension->id());
1079}
1080
[email protected]009633c2013-03-07 22:08:281081void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]be083862012-09-01 03:53:451082 CHECK(extension);
1083 extensions::PermissionsUpdater perms_updater(profile());
[email protected]009633c2013-03-07 22:08:281084 perms_updater.GrantActivePermissions(extension);
[email protected]be083862012-09-01 03:53:451085}
1086
[email protected]fe2dd7742011-04-19 22:52:491087// static
1088void ExtensionService::RecordPermissionMessagesHistogram(
[email protected]13c68b62013-05-17 11:29:051089 const Extension* extension, const char* histogram) {
[email protected]de415552013-01-23 04:12:171090 // Since this is called from multiple sources, and since the histogram macros
1091 // use statics, we need to manually lookup the histogram ourselves.
1092 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491093 histogram,
1094 1,
[email protected]c2e66e12012-06-27 06:27:061095 PermissionMessage::kEnumBoundary,
1096 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171097 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491098
[email protected]13c68b62013-05-17 11:29:051099 PermissionMessages permissions =
1100 extensions::PermissionsData::GetPermissionMessages(extension);
[email protected]fe2dd7742011-04-19 22:52:491101 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061102 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491103 } else {
[email protected]c2e66e12012-06-27 06:27:061104 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521105 it != permissions.end(); ++it)
1106 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491107 }
1108}
1109
[email protected]eaa7dd182010-12-14 11:09:001110void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421111 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061112 // was loaded, otherwise a race can arise where a renderer that is created
1113 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421114 // that the request context doesn't yet know about. The profile is responsible
1115 // for ensuring its URLRequestContexts appropriately discover the loaded
1116 // extension.
[email protected]31d8f5f22012-04-02 15:22:081117 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061118
[email protected]d5949312012-12-03 22:13:301119 // Tell renderers about the new extension, unless it's a theme (renderers
1120 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191121 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491122 for (content::RenderProcessHost::iterator i(
1123 content::RenderProcessHost::AllHostsIterator());
1124 !i.IsAtEnd(); i.Advance()) {
1125 content::RenderProcessHost* host = i.GetCurrentValue();
1126 Profile* host_profile =
1127 Profile::FromBrowserContext(host->GetBrowserContext());
1128 if (host_profile->GetOriginalProfile() ==
1129 profile_->GetOriginalProfile()) {
1130 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1131 1, ExtensionMsg_Loaded_Params(extension));
1132 host->Send(
1133 new ExtensionMsg_Loaded(loaded_extensions));
1134 }
[email protected]c8d407e2011-04-28 21:27:171135 }
[email protected]77a6970c2011-04-23 16:58:561136 }
1137
[email protected]3442a662012-01-12 08:06:171138 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1139 // extension.
1140 //
1141 // NOTE: It is important that this happen after notifying the renderers about
1142 // the new extensions so that if we navigate to an extension URL in
1143 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1144 content::NotificationService::current()->Notify(
1145 chrome::NOTIFICATION_EXTENSION_LOADED,
1146 content::Source<Profile>(profile_),
1147 content::Details<const Extension>(extension));
1148
[email protected]c8d407e2011-04-28 21:27:171149 // Tell a random-ass collection of other subsystems about the new extension.
1150 // TODO(aa): What should we do with all this goop? Can it move into the
1151 // relevant objects via EXTENSION_LOADED?
1152
1153 profile_->GetExtensionSpecialStoragePolicy()->
1154 GrantRightsForExtension(extension);
1155
1156 UpdateActiveExtensionsInCrashReporter();
1157
[email protected]c8d407e2011-04-28 21:27:171158 // If the extension has permission to load chrome://favicon/ resources we need
1159 // to make sure that the FaviconSource is registered with the
1160 // ChromeURLDataManager.
[email protected]13c68b62013-05-17 11:29:051161 if (extensions::PermissionsData::HasHostPermission(
1162 extension, GURL(chrome::kChromeUIFaviconURL))) {
[email protected]c8d407e2011-04-28 21:27:171163 FaviconSource* favicon_source = new FaviconSource(profile_,
1164 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531165 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171166 }
[email protected]b07e606e2012-09-15 20:16:151167
1168#if !defined(OS_ANDROID)
1169 // Same for chrome://theme/ resources.
[email protected]13c68b62013-05-17 11:29:051170 if (extensions::PermissionsData::HasHostPermission(
1171 extension, GURL(chrome::kChromeUIThemeURL))) {
[email protected]b07e606e2012-09-15 20:16:151172 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531173 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151174 }
1175#endif
1176
[email protected]5eddc3e2011-10-26 04:33:311177 // Same for chrome://thumb/ resources.
[email protected]13c68b62013-05-17 11:29:051178 if (extensions::PermissionsData::HasHostPermission(
1179 extension, GURL(chrome::kChromeUIThumbnailURL))) {
[email protected]420e6d92013-09-17 01:48:511180 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_, false);
[email protected]24ea7a12013-01-27 23:54:531181 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311182 }
[email protected]62d30f42009-10-01 22:36:061183}
1184
[email protected]a9f39a312010-12-23 22:14:271185void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591186 const Extension* extension,
1187 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271188 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071189 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271190 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531191 content::Source<Profile>(profile_),
1192 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061193
[email protected]f3b1a082011-11-18 00:34:301194 for (content::RenderProcessHost::iterator i(
1195 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561196 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301197 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081198 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301199 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081200 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171201 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561202 }
1203
[email protected]31d8f5f22012-04-02 15:22:081204 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171205 profile_->GetExtensionSpecialStoragePolicy()->
1206 RevokeRightsForExtension(extension);
1207
[email protected]b777b332011-04-16 04:01:081208#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231209 // Revoke external file access for the extension from its file system context.
1210 // It is safe to access the extension's storage partition at this point. The
1211 // storage partition may get destroyed only after the extension gets unloaded.
1212 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1213 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091214 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231215 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091216 GetFileSystemContext();
[email protected]f19bbf62013-07-09 01:22:321217 if (filesystem_context && filesystem_context->external_backend()) {
1218 filesystem_context->external_backend()->
[email protected]c8d407e2011-04-28 21:27:171219 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061220 }
[email protected]c8d407e2011-04-28 21:27:171221#endif
1222
1223 UpdateActiveExtensionsInCrashReporter();
[email protected]62d30f42009-10-01 22:36:061224}
1225
[email protected]0a071a32011-02-08 00:18:241226Profile* ExtensionService::profile() {
1227 return profile_;
1228}
1229
[email protected]45759612012-07-10 17:21:231230extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451231 return extension_prefs_;
1232}
1233
[email protected]0d9a2202011-11-09 13:48:411234extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381235 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201236}
1237
[email protected]e2d720aa2012-05-17 00:11:351238extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441239 return extension_prefs()->content_settings_store();
1240}
1241
[email protected]25ae0152011-11-18 14:40:021242bool ExtensionService::is_ready() {
[email protected]4a10006a2013-05-17 23:18:351243 return ready_->is_signaled();
[email protected]25ae0152011-11-18 14:40:021244}
1245
[email protected]7f8f24f2012-11-15 19:40:141246base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
[email protected]dc24976f2013-06-02 21:15:091247 if (file_task_runner_.get())
1248 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141249
1250 // We should be able to interrupt any part of extension install process during
1251 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1252 // will be ignored/deleted while we will block on started tasks.
1253 std::string token("ext_install-");
1254 token.append(profile_->GetPath().AsUTF8Unsafe());
1255 file_task_runner_ = BrowserThread::GetBlockingPool()->
1256 GetSequencedTaskRunnerWithShutdownBehavior(
1257 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1258 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
[email protected]dc24976f2013-06-02 21:15:091259 return file_task_runner_.get();
[email protected]7f8f24f2012-11-15 19:40:141260}
1261
[email protected]42a08162012-03-16 18:09:111262extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061263 return updater_.get();
1264}
1265
[email protected]4ee07c62012-08-21 12:40:421266void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251267 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281268
[email protected]aa96d3a2010-08-21 08:45:251269 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461270 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251271 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361272 const Extension* extension = (iter->get());
[email protected]4ee07c62012-08-21 12:40:421273 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251274 to_be_removed.push_back(extension->id());
1275 }
1276
1277 // UnloadExtension will change the extensions_ list. So, we should
1278 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401279 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591280 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251281}
1282
[email protected]31206602011-04-13 23:07:321283void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351284 if (updater()) {
[email protected]94fde232012-04-27 10:22:301285 if (AreAllExternalProvidersReady()) {
1286 updater()->CheckSoon();
1287 } else {
1288 // Sync can start updating before all the external providers are ready
1289 // during startup. Start the update as soon as those providers are ready,
1290 // but not before.
1291 update_once_all_providers_are_ready_ = true;
1292 }
[email protected]c3cfb012011-04-06 22:07:351293 } else {
[email protected]31206602011-04-13 23:07:321294 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351295 }
1296}
1297
[email protected]3f98fd92012-11-17 15:54:291298syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321299 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581300 const syncer::SyncDataList& initial_sync_data,
1301 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1302 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201303 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281304 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301305
1306 switch (type) {
[email protected]a4a147652012-07-03 23:41:321307 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201308 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281309 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201310 initial_sync_data);
[email protected]50736a12013-09-26 08:58:341311 pending_extension_enables_.OnSyncStarted(this);
[email protected]3bdba0d2011-08-23 07:17:301312 break;
1313
[email protected]a4a147652012-07-03 23:41:321314 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281315 app_sync_bundle_.SetupSync(sync_processor.release(),
1316 sync_error_factory.release(),
1317 initial_sync_data);
[email protected]50736a12013-09-26 08:58:341318 pending_app_enables_.OnSyncStarted(this);
[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
[email protected]50736a12013-09-26 08:58:341504bool ExtensionService::IsPendingEnable(const std::string& extension_id) const {
1505 return pending_app_enables_.Contains(extension_id) ||
1506 pending_extension_enables_.Contains(extension_id);
1507}
1508
[email protected]5db9ada2012-04-11 13:48:201509bool ExtensionService::ProcessExtensionSyncDataHelper(
1510 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321511 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301512 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061513 const Extension* extension = GetInstalledExtension(id);
1514
1515 // TODO(bolms): we should really handle this better. The particularly bad
1516 // case is where an app becomes an extension or vice versa, and we end up with
1517 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201518 if (extension && !IsCorrectSyncType(*extension, type))
1519 return true;
[email protected]90310d92011-04-17 07:35:041520
1521 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301522 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041523 if (!UninstallExtensionHelper(this, id)) {
1524 LOG(WARNING) << "Could not uninstall extension " << id
1525 << " for sync";
1526 }
[email protected]5db9ada2012-04-11 13:48:201527 return true;
[email protected]90310d92011-04-17 07:35:041528 }
1529
[email protected]caf2d442012-05-04 10:00:501530 // Extension from sync was uninstalled by the user as external extensions.
1531 // Honor user choice and skip installation/enabling.
1532 if (IsExternalExtensionUninstalled(id)) {
1533 LOG(WARNING) << "Extension with id " << id
1534 << " from sync was uninstalled as external extension";
1535 return true;
1536 }
1537
[email protected]06f92562011-04-29 19:27:311538 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191539 // If the extension has been disabled from sync, it may not have
1540 // been installed yet, so we don't know if the disable reason was a
[email protected]b914e2952013-04-26 07:10:031541 // permissions increase. That will be updated once CheckPermissionsIncrease
[email protected]7e9e2422012-12-13 19:54:191542 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401543 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311544 EnableExtension(id);
[email protected]50736a12013-09-26 08:58:341545 else if (!IsPendingEnable(id))
[email protected]7e9e2422012-12-13 19:54:191546 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371547
[email protected]406b5a92011-11-08 11:58:261548 // We need to cache some version information here because setting the
1549 // incognito flag invalidates the |extension| pointer (it reloads the
1550 // extension).
[email protected]9060d8b02012-01-13 02:14:301551 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261552 int result = extension ?
1553 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1554 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1555 extension = NULL; // No longer safe to use.
1556
1557 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311558 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041559 if (result < 0) {
1560 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201561 return false;
[email protected]b020dc12011-12-06 04:35:421562 }
[email protected]06f92562011-04-29 19:27:311563 } else {
[email protected]06f92562011-04-29 19:27:311564 // TODO(akalin): Replace silent update with a list of enabled
1565 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331566 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201567
[email protected]a4a147652012-07-03 23:41:321568 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]8f3bcbd2013-06-05 08:42:401569 extensions::PendingExtensionInfo::ShouldAllowInstallPredicate filter =
1570 (type == syncer::APPS) ? extensions::sync_helper::IsSyncableApp :
1571 extensions::sync_helper::IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201572
[email protected]06f92562011-04-29 19:27:311573 if (!pending_extension_manager()->AddFromSync(
1574 id,
[email protected]3bdba0d2011-08-23 07:17:301575 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201576 filter,
[email protected]6cc7dbae2011-04-29 21:18:331577 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311578 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301579 // This means that the extension is already pending installation, with a
1580 // non-INTERNAL location. Add to pending_sync_data, even though it will
1581 // never be removed (we'll never install a syncable version of the
1582 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311583 }
[email protected]3bdba0d2011-08-23 07:17:301584 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201585 return false;
[email protected]90310d92011-04-17 07:35:041586 }
[email protected]5db9ada2012-04-11 13:48:201587
1588 return true;
[email protected]90310d92011-04-17 07:35:041589}
1590
[email protected]c3cfb012011-04-06 22:07:351591bool ExtensionService::IsIncognitoEnabled(
1592 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481593 const Extension* extension = GetInstalledExtension(extension_id);
1594 if (extension && !extension->can_be_incognito_enabled())
1595 return false;
[email protected]c3cfb012011-04-06 22:07:351596 // If this is an existing component extension we always allow it to
1597 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401598 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411599 return true;
[email protected]fc05749a2013-07-10 06:59:111600 if (extension && extension->force_incognito_enabled())
1601 return true;
[email protected]cb0ce1e022010-03-10 19:54:411602
1603 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351604 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501605}
[email protected]55a35692010-02-11 23:25:211606
[email protected]c3cfb012011-04-06 22:07:351607void ExtensionService::SetIsIncognitoEnabled(
1608 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371609 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481610 if (extension && !extension->can_be_incognito_enabled())
1611 return;
[email protected]1d5e58b2013-01-31 08:41:401612 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481613 // This shouldn't be called for component extensions unless they are
1614 // syncable.
[email protected]8f3bcbd2013-06-05 08:42:401615 DCHECK(extensions::sync_helper::IsSyncable(extension));
[email protected]b873cd92012-02-09 21:51:481616
1617 // If we are here, make sure the we aren't trying to change the value.
1618 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1619
[email protected]c3cfb012011-04-06 22:07:351620 return;
1621 }
1622
[email protected]8c6c8c22011-03-09 12:52:341623 // Broadcast unloaded and loaded events to update browser state. Only bother
1624 // if the value changed and the extension is actually enabled, since there is
1625 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351626 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341627 if (enabled == old_enabled)
1628 return;
1629
[email protected]c3cfb012011-04-06 22:07:351630 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371631
[email protected]ffbf3f12011-12-09 22:10:331632 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341633
1634 // When we reload the extension the ID may be invalidated if we've passed it
1635 // by const ref everywhere. Make a copy to be safe.
1636 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261637 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331638 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301639
[email protected]406b5a92011-11-08 11:58:261640 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341641 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301642 if (extension)
1643 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211644}
1645
[email protected]dc9a74f72012-08-17 18:07:211646bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051647 // We allow the extension to see events and data from another profile iff it
1648 // uses "spanning" behavior and it has incognito access. "split" mode
1649 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221650 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351651 return IsIncognitoEnabled(extension->id()) &&
[email protected]06492ed2013-03-24 22:13:141652 !extensions::IncognitoInfo::IsSplitMode(extension);
[email protected]2a8f24e2010-11-03 21:37:051653}
1654
[email protected]5ef7b002011-05-12 23:16:201655bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1656 if (extension->is_hosted_app())
1657 return true;
1658 // Packaged apps and regular extensions need to be enabled specifically for
1659 // incognito (and split mode should be set).
[email protected]06492ed2013-03-24 22:13:141660 return extensions::IncognitoInfo::IsSplitMode(extension) &&
[email protected]5ef7b002011-05-12 23:16:201661 IsIncognitoEnabled(extension->id());
1662}
1663
[email protected]168389f2011-12-20 17:12:481664void ExtensionService::OnExtensionMoved(
1665 const std::string& moved_extension_id,
1666 const std::string& predecessor_extension_id,
1667 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231668 extension_prefs_->extension_sorting()->OnExtensionMoved(
1669 moved_extension_id,
1670 predecessor_extension_id,
1671 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481672
1673 const Extension* extension = GetInstalledExtension(moved_extension_id);
1674 if (extension)
1675 SyncExtensionChangeIfNeeded(*extension);
1676}
1677
[email protected]dc9a74f72012-08-17 18:07:211678bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081679 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441680 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081681 extension_prefs_->AllowFileAccess(extension->id()));
1682}
1683
[email protected]eaa7dd182010-12-14 11:09:001684void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481685 bool allow) {
1686 // Reload to update browser state. Only bother if the value changed and the
1687 // extension is actually enabled, since there is no UI otherwise.
1688 bool old_allow = AllowFileAccess(extension);
1689 if (allow == old_allow)
1690 return;
1691
[email protected]05c82182010-06-24 17:49:081692 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481693
[email protected]84df8332011-12-06 18:22:461694 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481695 if (extension_is_enabled)
1696 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081697}
1698
[email protected]8e4560b62011-01-14 10:09:141699// Some extensions will autoupdate themselves externally from Chrome. These
1700// are typically part of some larger client application package. To support
1701// these, the extension will register its location in the the preferences file
1702// (and also, on Windows, in the registry) and this code will periodically
1703// check that location for a .crx file, which it will then install locally if
1704// a new version is available.
1705// Errors are reported through ExtensionErrorReporter. Succcess is not
1706// reported.
[email protected]eaa7dd182010-12-14 11:09:001707void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121708 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141709
1710 // Note that this installation is intentionally silent (since it didn't
1711 // go through the front-end). Extensions that are registered in this
1712 // way are effectively considered 'pre-bundled', and so implicitly
1713 // trusted. In general, if something has HKLM or filesystem access,
1714 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121715
[email protected]8e4560b62011-01-14 10:09:141716 // Ask each external extension provider to give us a call back for each
1717 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271718 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141719 for (i = external_extension_providers_.begin();
1720 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271721 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141722 provider->VisitRegisteredExtension();
1723 }
1724
[email protected]50067e52011-10-20 23:17:071725 // Do any required work that we would have done after completion of all
1726 // providers.
1727 if (external_extension_providers_.empty()) {
1728 OnAllExternalProvidersReady();
1729 }
[email protected]9f1087e2009-06-15 17:29:321730}
1731
[email protected]50067e52011-10-20 23:17:071732void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271733 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121734 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071735 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121736
1737 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141738 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301739 if (AreAllExternalProvidersReady())
1740 OnAllExternalProvidersReady();
1741}
1742
1743bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271744 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141745 for (i = external_extension_providers_.begin();
1746 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301747 if (!i->get()->IsReady())
1748 return false;
[email protected]8e4560b62011-01-14 10:09:141749 }
[email protected]94fde232012-04-27 10:22:301750 return true;
[email protected]50067e52011-10-20 23:17:071751}
1752
1753void ExtensionService::OnAllExternalProvidersReady() {
1754 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301755 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1756 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071757
1758 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301759 if (update_once_all_providers_are_ready_ && updater()) {
1760 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061761 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141762 }
1763
1764 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231765 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141766 extension_prefs_->GetInstalledExtensionsInfo());
1767 for (size_t i = 0; i < extensions_info->size(); ++i) {
1768 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401769 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141770 CheckExternalUninstall(info->extension_id);
1771 }
[email protected]e5af875f2011-10-10 21:09:141772 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141773}
1774
1775void ExtensionService::IdentifyAlertableExtensions() {
1776 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1777
[email protected]07c9f2f42012-02-29 18:45:221778 // Build up the lists of extensions that require acknowledgment. If this is
1779 // the first time, grandfather extensions that would have caused
1780 // notification.
[email protected]89226982012-07-16 20:09:181781 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141782
[email protected]a9aa5932012-01-25 08:27:401783 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181784 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]460c6712013-04-24 07:20:011785 if (!is_first_run_) {
[email protected]a9aa5932012-01-25 08:27:401786 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181787 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141788 } else {
1789 // First run. Just acknowledge all the extensions, silently, by
1790 // shortcutting the display of the UI and going straight to the
1791 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401792 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141793 }
1794 }
[email protected]a9aa5932012-01-25 08:27:401795
[email protected]612a1cb12012-10-17 13:18:031796 UpdateExternalExtensionAlert();
1797
[email protected]a9aa5932012-01-25 08:27:401798 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181799 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141800}
1801
[email protected]89226982012-07-16 20:09:181802bool ExtensionService::PopulateExtensionErrorUI(
1803 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221804 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281805
1806 // Extensions that are blacklisted.
1807 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1808 it != blacklisted_extensions_.end(); ++it) {
1809 std::string id = (*it)->id();
1810 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1811 extension_error_ui->AddBlacklistedExtension(id);
1812 needs_alert = true;
1813 }
1814 }
1815
[email protected]07c9f2f42012-02-29 18:45:221816 for (ExtensionSet::const_iterator iter = extensions_.begin();
1817 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361818 const Extension* e = iter->get();
[email protected]695b5712012-12-06 23:55:281819
1820 // Extensions disabled by policy. Note: this no longer includes blacklisted
1821 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391822 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221823 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181824 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221825 needs_alert = true;
1826 }
1827 }
[email protected]07c9f2f42012-02-29 18:45:221828 }
[email protected]695b5712012-12-06 23:55:281829
[email protected]07c9f2f42012-02-29 18:45:221830 return needs_alert;
1831}
1832
[email protected]a9aa5932012-01-25 08:27:401833void ExtensionService::HandleExtensionAlertClosed() {
[email protected]612a1cb12012-10-17 13:18:031834 const ExtensionIdSet* extension_ids =
1835 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141836 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1837 iter != extension_ids->end(); ++iter) {
1838 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1839 }
[email protected]879a6092013-03-06 04:12:501840 extension_error_ui_.reset();
1841}
1842
1843void ExtensionService::HandleExtensionAlertAccept() {
[email protected]73a85e012013-04-04 10:45:301844 extension_error_ui_->Close();
[email protected]e5af875f2011-10-10 21:09:141845}
1846
[email protected]47fc70c2011-12-06 07:29:511847void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1848 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031849 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511850}
1851
[email protected]d96eb512012-11-01 23:44:081852bool ExtensionService::IsUnacknowledgedExternalExtension(
1853 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321854 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1855 return false;
1856
[email protected]1d5e58b2013-01-31 08:41:401857 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081858 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1859 !(extension_prefs_->GetDisableReasons(extension->id()) &
1860 Extension::DISABLE_SIDELOAD_WIPEOUT));
1861}
1862
[email protected]89226982012-07-16 20:09:181863void ExtensionService::HandleExtensionAlertDetails() {
1864 extension_error_ui_->ShowExtensions();
[email protected]73a85e012013-04-04 10:45:301865 // ShowExtensions may cause the error UI to close synchronously, e.g. if it
1866 // causes a navigation.
1867 if (extension_error_ui_)
1868 extension_error_ui_->Close();
[email protected]6c751e72010-11-23 10:11:101869}
1870
[email protected]612a1cb12012-10-17 13:18:031871void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361872 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031873 return;
1874
1875 const Extension* extension = NULL;
1876 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1877 iter != disabled_extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:361878 const Extension* e = iter->get();
[email protected]d96eb512012-11-01 23:44:081879 if (IsUnacknowledgedExternalExtension(e)) {
1880 extension = e;
1881 break;
[email protected]612a1cb12012-10-17 13:18:031882 }
1883 }
1884
1885 if (extension) {
[email protected]d96eb512012-11-01 23:44:081886 if (!extensions::HasExternalInstallError(this)) {
1887 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031888 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081889 // Stop prompting for this extension, and check if there's another
1890 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031891 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081892 UpdateExternalExtensionAlert();
1893 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1894 EXTERNAL_EXTENSION_IGNORED,
1895 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:021896 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
1897 UMA_HISTOGRAM_ENUMERATION(
1898 "Extensions.ExternalExtensionEventWebstore",
1899 EXTERNAL_EXTENSION_IGNORED,
1900 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1901 } else {
1902 UMA_HISTOGRAM_ENUMERATION(
1903 "Extensions.ExternalExtensionEventNonWebstore",
1904 EXTERNAL_EXTENSION_IGNORED,
1905 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1906 }
[email protected]d96eb512012-11-01 23:44:081907 return;
[email protected]612a1cb12012-10-17 13:18:031908 }
[email protected]b3aa7182013-04-25 04:45:231909 if (is_first_run_)
1910 extension_prefs_->SetExternalInstallFirstRun(extension->id());
1911 // first_run is true if the extension was installed during a first run
1912 // (even if it's post-first run now).
1913 bool first_run = extension_prefs_->IsExternalInstallFirstRun(
1914 extension->id());
1915 extensions::AddExternalInstallError(this, extension, first_run);
[email protected]612a1cb12012-10-17 13:18:031916 }
1917 } else {
1918 extensions::RemoveExternalInstallError(this);
1919 }
[email protected]612a1cb12012-10-17 13:18:031920}
1921
[email protected]a9f39a312010-12-23 22:14:271922void ExtensionService::UnloadExtension(
1923 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591924 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091925 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281926 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021927 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281928 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251929
[email protected]fa0c96732010-11-17 00:14:231930 // This method can be called via PostTask, so the extension may have been
1931 // unloaded by the time this runs.
[email protected]dc24976f2013-06-02 21:15:091932 if (!extension.get()) {
[email protected]dd163fb02011-05-04 22:22:171933 // In case the extension may have crashed/uninstalled. Allow the profile to
1934 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081935 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231936 return;
[email protected]dd163fb02011-05-04 22:22:171937 }
[email protected]0c6da502009-08-14 22:32:391938
[email protected]20c8b942013-05-26 02:03:401939 // If uninstalling let RuntimeEventRouter know.
1940 if (reason == extension_misc::UNLOAD_REASON_UNINSTALL)
1941 extensions::RuntimeEventRouter::OnExtensionUninstalled(
1942 profile_, extension_id);
1943
[email protected]1eb175082010-02-10 09:26:161944 // Keep information about the extension so that we can reload it later
1945 // even if it's not permanently installed.
1946 unloaded_extension_paths_[extension->id()] = extension->path();
1947
[email protected]f17dbd42010-08-16 23:21:101948 // Clean up if the extension is meant to be enabled after a reload.
[email protected]b914e2952013-04-26 07:10:031949 reloading_extensions_.erase(extension->id());
[email protected]f17dbd42010-08-16 23:21:101950
[email protected]d7e9a862010-11-03 21:57:491951 // Clean up runtime data.
1952 extension_runtime_data_.erase(extension_id);
1953
[email protected]060d4972012-07-19 17:22:391954 if (disabled_extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:461955 disabled_extensions_.Remove(extension->id());
[email protected]dd163fb02011-05-04 22:22:171956 // Make sure the profile cleans up its RequestContexts when an already
1957 // disabled extension is unloaded (since they are also tracking the disabled
1958 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081959 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]60a174a2013-08-02 20:29:281960 } else {
1961 // Remove the extension from our list.
1962 extensions_.Remove(extension->id());
1963 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]0c6da502009-08-14 22:32:391964 }
1965
[email protected]fb789532013-08-27 02:40:211966 content::NotificationService::current()->Notify(
1967 chrome::NOTIFICATION_EXTENSION_REMOVED,
1968 content::Source<Profile>(profile_),
1969 content::Details<const Extension>(extension.get()));
[email protected]631cf822009-05-15 07:01:251970}
1971
[email protected]8b1ec202013-09-05 02:09:501972void ExtensionService::RemoveComponentExtension(
1973 const std::string& extension_id) {
1974 scoped_refptr<const Extension> extension(
1975 GetExtensionById(extension_id, false));
1976 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
1977 content::NotificationService::current()->Notify(
1978 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
1979 content::Source<Profile>(profile_),
1980 content::Details<const Extension>(extension.get()));
1981}
1982
[email protected]eaa7dd182010-12-14 11:09:001983void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401984 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171985
[email protected]84df8332011-12-06 18:22:461986 extensions_.Clear();
1987 disabled_extensions_.Clear();
1988 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491989 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321990
[email protected]c6e4a3412009-06-24 15:45:291991 // TODO(erikkay) should there be a notification for this? We can't use
1992 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1993 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321994}
1995
[email protected]eaa7dd182010-12-14 11:09:001996void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321997 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451998 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011999 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]4a10006a2013-05-17 23:18:352000 // Don't call SetReadyAndNotifyListeners() since tests call this multiple
2001 // times.
[email protected]9f1087e2009-06-15 17:29:322002}
2003
[email protected]eaa7dd182010-12-14 11:09:002004void ExtensionService::GarbageCollectExtensions() {
[email protected]c04fd3dd2013-09-05 08:20:102005#if defined(OS_CHROMEOS)
2006 if (disable_garbage_collection_)
2007 return;
2008#endif
2009
[email protected]2f8757c32012-06-19 19:17:472010 if (extension_prefs_->pref_service()->ReadOnly())
2011 return;
2012
[email protected]9bd9a6862012-11-29 09:24:222013 if (pending_extension_manager()->HasPendingExtensions()) {
2014 // Don't garbage collect while there are pending installations, which may
2015 // be using the temporary installation directory. Try to garbage collect
2016 // again later.
[email protected]b3a25092013-05-28 22:08:162017 base::MessageLoop::current()->PostDelayedTask(
[email protected]9bd9a6862012-11-29 09:24:222018 FROM_HERE,
2019 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
2020 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
2021 return;
2022 }
2023
[email protected]45759612012-07-10 17:21:232024 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:472025 extension_prefs_->GetInstalledExtensionsInfo());
2026
[email protected]650b2d52013-02-10 03:41:452027 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:472028 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:052029 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2030 info->at(i)->extension_path));
2031
[email protected]399583b2012-12-11 09:33:422032 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:052033 for (size_t i = 0; i < info->size(); ++i)
2034 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2035 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:472036
[email protected]7f8f24f2012-11-15 19:40:142037 if (!GetFileTaskRunner()->PostTask(
2038 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:472039 base::Bind(
2040 &extension_file_util::GarbageCollectExtensions,
2041 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:402042 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:472043 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402044 }
[email protected]3cf4f0992009-02-03 23:00:302045}
2046
[email protected]fb82dcd2012-03-21 14:15:462047void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]6d147912013-07-09 08:02:142048 if (extensions::sync_helper::IsSyncableApp(&extension)) {
2049 if (app_sync_bundle_.IsSyncing())
2050 app_sync_bundle_.SyncChangeIfNeeded(extension);
2051 else if (is_ready() && !flare_.is_null())
2052 flare_.Run(syncer::APPS);
2053 } else if (extensions::sync_helper::IsSyncableExtension(&extension)) {
2054 if (extension_sync_bundle_.IsSyncing())
2055 extension_sync_bundle_.SyncChangeIfNeeded(extension);
2056 else if (is_ready() && !flare_.is_null())
2057 flare_.Run(syncer::EXTENSIONS);
[email protected]fb82dcd2012-03-21 14:15:462058 }
2059}
2060
[email protected]820d9bd2013-04-03 03:46:032061void ExtensionService::SetReadyAndNotifyListeners() {
[email protected]4a10006a2013-05-17 23:18:352062 ready_->Signal();
[email protected]820d9bd2013-04-03 03:46:032063 content::NotificationService::current()->Notify(
2064 chrome::NOTIFICATION_EXTENSIONS_READY,
2065 content::Source<Profile>(profile_),
2066 content::NotificationService::NoDetails());
2067}
2068
[email protected]eaa7dd182010-12-14 11:09:002069void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]3eeddd892013-04-17 17:00:112070 if (updater_)
[email protected]93fd78f42009-07-10 16:43:172071 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502072
[email protected]bc151cf92013-02-12 04:57:262073 OnBlacklistUpdated();
[email protected]e72e8eb82009-06-18 17:21:512074}
2075
[email protected]fcb58a862012-05-01 01:03:152076void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172077 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2078 // default extension state to DISABLED when the --disable-extensions flag
2079 // is set (http://crbug.com/29067).
2080 if (!extensions_enabled() &&
2081 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:402082 extension->location() != Manifest::COMPONENT &&
2083 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152084 return;
[email protected]330840c2012-08-29 22:21:012085 }
[email protected]c8d407e2011-04-28 21:27:172086
[email protected]b914e2952013-04-26 07:10:032087 bool is_extension_upgrade = false;
[email protected]116d40e2013-08-08 17:23:172088 bool is_extension_installed = false;
2089 const Extension* old = GetInstalledExtension(extension->id());
2090 if (old) {
2091 is_extension_installed = true;
2092 int version_compare_result =
2093 extension->version()->CompareTo(*(old->version()));
2094 is_extension_upgrade = version_compare_result > 0;
[email protected]b914e2952013-04-26 07:10:032095 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2096 // that we aren't downgrading.
2097 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]116d40e2013-08-08 17:23:172098 CHECK_GE(version_compare_result, 0);
[email protected]b914e2952013-04-26 07:10:032099 }
2100 SetBeingUpgraded(extension, is_extension_upgrade);
[email protected]c8d407e2011-04-28 21:27:172101
[email protected]1eb175082010-02-10 09:26:162102 // The extension is now loaded, remove its data from unloaded extension map.
2103 unloaded_extension_paths_.erase(extension->id());
2104
[email protected]bb7f40952011-01-13 00:21:202105 // If a terminated extension is loaded, remove it from the terminated list.
2106 UntrackTerminatedExtension(extension->id());
2107
[email protected]f17dbd42010-08-16 23:21:102108 // If the extension was disabled for a reload, then enable it.
[email protected]ca2e82022013-04-29 16:48:412109 bool reloading = reloading_extensions_.erase(extension->id()) > 0;
[email protected]f17dbd42010-08-16 23:21:102110
[email protected]b914e2952013-04-26 07:10:032111 // Check if the extension's privileges have changed and mark the
2112 // extension disabled if necessary.
[email protected]116d40e2013-08-08 17:23:172113 CheckPermissionsIncrease(extension, is_extension_installed);
[email protected]b914e2952013-04-26 07:10:032114
[email protected]116d40e2013-08-08 17:23:172115 if (is_extension_installed && !reloading) {
[email protected]ca2e82022013-04-29 16:48:412116 // To upgrade an extension in place, unload the old one and then load the
2117 // new one. ReloadExtension disables the extension, which is sufficient.
[email protected]b914e2952013-04-26 07:10:032118 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_UPDATE);
2119 }
[email protected]0c6da502009-08-14 22:32:392120
[email protected]695b5712012-12-06 23:55:282121 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
[email protected]ac875372013-02-28 04:36:092122 // Only prefs is checked for the blacklist. We rely on callers to check the
2123 // blacklist before calling into here, e.g. CrxInstaller checks before
[email protected]9f3c8532013-07-31 19:52:072124 // installation then threads through the install and pending install flow
2125 // of this class, and we check when loading installed extensions.
[email protected]695b5712012-12-06 23:55:282126 blacklisted_extensions_.Insert(extension);
[email protected]ca2e82022013-04-29 16:48:412127 } else if (!reloading &&
2128 extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482129 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302130 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032131 content::NotificationService::current()->Notify(
2132 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2133 content::Source<Profile>(profile_),
2134 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442135
[email protected]7e9e2422012-12-13 19:54:192136 // Show the extension disabled error if a permissions increase was the
2137 // only reason it was disabled.
2138 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032139 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442140 extensions::AddExtensionDisabledError(this, extension);
2141 }
[email protected]ca2e82022013-04-29 16:48:412142 } else if (reloading) {
2143 // Replace the old extension with the new version.
2144 CHECK(!disabled_extensions_.Insert(extension));
2145 EnableExtension(extension->id());
[email protected]695b5712012-12-06 23:55:282146 } else {
2147 // All apps that are displayed in the launcher are ordered by their ordinals
2148 // so we must ensure they have valid ordinals.
2149 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252150 if (!extension->ShouldDisplayInNewTabPage()) {
2151 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2152 extension->id());
2153 }
[email protected]695b5712012-12-06 23:55:282154 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2155 extension->id(), syncer::StringOrdinal());
2156 }
2157
2158 extensions_.Insert(extension);
2159 SyncExtensionChangeIfNeeded(*extension);
2160 NotifyExtensionLoaded(extension);
[email protected]f574c402012-12-04 23:20:312161 }
[email protected]b914e2952013-04-26 07:10:032162 SetBeingUpgraded(extension, false);
[email protected]aab98a52009-12-02 03:22:352163}
2164
[email protected]8c484b742012-11-29 06:05:362165void ExtensionService::AddComponentExtension(const Extension* extension) {
2166 const std::string old_version_string(
2167 extension_prefs_->GetVersionString(extension->id()));
2168 const Version old_version(old_version_string);
2169
2170 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2171 VLOG(1) << "Component extension " << extension->name() << " ("
2172 << extension->id() << ") installing/upgrading from '"
2173 << old_version_string << "' to " << extension->version()->GetString();
2174
2175 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422176 Extension::ENABLED_COMPONENT,
[email protected]9f3c8532013-07-31 19:52:072177 extensions::Blacklist::NOT_BLACKLISTED,
[email protected]399583b2012-12-11 09:33:422178 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362179 return;
2180 }
2181
2182 AddExtension(extension);
2183}
2184
[email protected]b914e2952013-04-26 07:10:032185void ExtensionService::UpdateActivePermissions(const Extension* extension) {
[email protected]902fd7b2011-07-27 18:42:312186 // If the extension has used the optional permissions API, it will have a
2187 // custom set of active permissions defined in the extension prefs. Here,
2188 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062189 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312190 extension_prefs()->GetActivePermissions(extension->id());
2191
[email protected]dc24976f2013-06-02 21:15:092192 if (active_permissions.get()) {
[email protected]902fd7b2011-07-27 18:42:312193 // We restrict the active permissions to be within the bounds defined in the
2194 // extension's manifest.
2195 // a) active permissions must be a subset of optional + default permissions
2196 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062197 scoped_refptr<PermissionSet> total_permissions =
2198 PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062199 extensions::PermissionsData::GetRequiredPermissions(extension),
2200 extensions::PermissionsData::GetOptionalPermissions(extension));
[email protected]902fd7b2011-07-27 18:42:312201
2202 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062203 scoped_refptr<PermissionSet> adjusted_active =
2204 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312205 total_permissions.get(), active_permissions.get());
2206
2207 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062208 adjusted_active = PermissionSet::CreateUnion(
[email protected]2cb5e302013-05-09 05:00:062209 extensions::PermissionsData::GetRequiredPermissions(extension),
2210 adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312211
[email protected]c333e792012-01-06 16:57:392212 extensions::PermissionsUpdater perms_updater(profile());
[email protected]dc24976f2013-06-02 21:15:092213 perms_updater.UpdateActivePermissions(extension, adjusted_active.get());
[email protected]902fd7b2011-07-27 18:42:312214 }
[email protected]b914e2952013-04-26 07:10:032215}
2216
2217void ExtensionService::CheckPermissionsIncrease(const Extension* extension,
[email protected]116d40e2013-08-08 17:23:172218 bool is_extension_installed) {
[email protected]b914e2952013-04-26 07:10:032219 UpdateActivePermissions(extension);
[email protected]902fd7b2011-07-27 18:42:312220
[email protected]8d888c12010-11-30 00:00:252221 // We keep track of all permissions the user has granted each extension.
2222 // This allows extensions to gracefully support backwards compatibility
2223 // by including unknown permissions in their manifests. When the user
2224 // installs the extension, only the recognized permissions are recorded.
2225 // When the unknown permissions become recognized (e.g., through browser
2226 // upgrade), we can prompt the user to accept these new permissions.
2227 // Extensions can also silently upgrade to less permissions, and then
2228 // silently upgrade to a version that adds these permissions back.
2229 //
2230 // For example, pretend that Chrome 10 includes a permission "omnibox"
2231 // for an API that adds suggestions to the omnibox. An extension can
2232 // maintain backwards compatibility while still having "omnibox" in the
2233 // manifest. If a user installs the extension on Chrome 9, the browser
2234 // will record the permissions it recognized, not including "omnibox."
2235 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2236 // will disable the extension and prompt the user to approve the increase
2237 // in privileges. The extension could then release a new version that
2238 // removes the "omnibox" permission. When the user upgrades, Chrome will
2239 // still remember that "omnibox" had been granted, so that if the
2240 // extension once again includes "omnibox" in an upgrade, the extension
2241 // can upgrade without requiring this user's approval.
[email protected]eb5e4f92012-08-15 23:33:282242 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252243
[email protected]211a3f32013-05-28 21:48:182244 bool auto_grant_permission =
[email protected]116d40e2013-08-08 17:23:172245 (!is_extension_installed && extension->was_installed_by_default()) ||
[email protected]211a3f32013-05-28 21:48:182246 chrome::IsRunningInForcedAppMode();
[email protected]b914e2952013-04-26 07:10:032247 // Silently grant all active permissions to default apps only on install.
2248 // After install they should behave like other apps.
[email protected]211a3f32013-05-28 21:48:182249 // Silently grant all active permissions to apps install in kiosk mode on both
2250 // install and update.
2251 if (auto_grant_permission)
[email protected]b914e2952013-04-26 07:10:032252 GrantPermissions(extension);
2253
2254 bool is_privilege_increase = false;
2255 // We only need to compare the granted permissions to the current permissions
2256 // if the extension is not allowed to silently increase its permissions.
[email protected]13c68b62013-05-17 11:29:052257 if (!extensions::PermissionsData::CanSilentlyIncreasePermissions(extension) &&
[email protected]211a3f32013-05-28 21:48:182258 !auto_grant_permission) {
[email protected]8d888c12010-11-30 00:00:252259 // Add all the recognized permissions if the granted permissions list
2260 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062261 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312262 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522263 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252264
2265 // Here, we check if an extension's privileges have increased in a manner
2266 // that requires the user's approval. This could occur because the browser
2267 // upgraded and recognized additional privileges, or an extension upgrades
2268 // to a version that requires additional privileges.
[email protected]cadac622013-06-11 16:46:362269 is_privilege_increase = granted_permissions->HasLessPrivilegesThan(
[email protected]a0a73052013-07-10 01:42:432270 extension->GetActivePermissions().get(), extension->GetType());
[email protected]8d888c12010-11-30 00:00:252271 }
2272
[email protected]116d40e2013-08-08 17:23:172273 if (is_extension_installed) {
[email protected]44d62b62012-04-11 00:06:032274 // If the extension was already disabled, suppress any alerts for becoming
2275 // disabled on permissions increase.
[email protected]b914e2952013-04-26 07:10:032276 bool previously_disabled =
2277 extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]7e9e2422012-12-13 19:54:192278 // Legacy disabled extensions do not have a disable reason. Infer that if
2279 // there was no permission increase, it was likely disabled by the user.
2280 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
[email protected]b914e2952013-04-26 07:10:032281 !extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
[email protected]7e9e2422012-12-13 19:54:192282 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032283 }
[email protected]7e9e2422012-12-13 19:54:192284 // Extensions that came to us disabled from sync need a similar inference,
2285 // except based on the new version's permissions.
2286 if (previously_disabled &&
2287 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2288 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2289 extension_prefs_->ClearDisableReasons(extension->id());
2290 if (!is_privilege_increase)
2291 disable_reasons |= Extension::DISABLE_USER_ACTION;
2292 }
[email protected]b914e2952013-04-26 07:10:032293 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]8d888c12010-11-30 00:00:252294 }
2295
2296 // Extension has changed permissions significantly. Disable it. A
2297 // notification should be sent by the caller.
2298 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192299 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492300 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2301 RecordPermissionMessagesHistogram(
2302 extension, "Extensions.Permissions_AutoDisable");
2303 }
[email protected]b3317ad2011-04-28 23:46:002304 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252305 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]b914e2952013-04-26 07:10:032306 }
2307 if (disable_reasons != Extension::DISABLE_NONE) {
[email protected]eb5e4f92012-08-15 23:33:282308 extension_prefs_->AddDisableReason(
2309 extension->id(),
2310 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252311 }
2312}
2313
[email protected]eaa7dd182010-12-14 11:09:002314void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392315 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462316 for (ExtensionSet::const_iterator iter = extensions_.begin();
2317 iter != extensions_.end(); ++iter) {
[email protected]cadac622013-06-11 16:46:362318 const Extension* extension = iter->get();
[email protected]1d5e58b2013-01-31 08:41:402319 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462320 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352321 }
2322
[email protected]f6431be82013-09-07 02:53:452323 crash_keys::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252324}
2325
[email protected]9f4e4f082013-06-21 07:11:192326ExtensionService::ImportStatus ExtensionService::SatisfyImports(
2327 const Extension* extension) {
2328 ImportStatus status = IMPORT_STATUS_OK;
2329 std::vector<std::string> pending;
2330 // TODO(elijahtaylor): Message the user if there is a failure that is
2331 // unrecoverable.
2332 if (SharedModuleInfo::ImportsModules(extension)) {
2333 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2334 SharedModuleInfo::GetImports(extension);
2335 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2336 for (i = imports.begin(); i != imports.end(); ++i) {
2337 Version version_required(i->minimum_version);
2338 const Extension* imported_module =
2339 GetExtensionById(i->extension_id, true);
2340 if (!imported_module) {
2341 if (extension->from_webstore()) {
2342 status = IMPORT_STATUS_UNSATISFIED;
2343 pending.push_back(i->extension_id);
2344 } else {
2345 return IMPORT_STATUS_UNRECOVERABLE;
2346 }
2347 } else if (!SharedModuleInfo::IsSharedModule(imported_module)) {
2348 return IMPORT_STATUS_UNRECOVERABLE;
2349 } else if (version_required.IsValid() &&
2350 imported_module->version()->CompareTo(version_required) < 0) {
2351 if (imported_module->from_webstore()) {
2352 status = IMPORT_STATUS_UNSATISFIED;
2353 } else {
2354 return IMPORT_STATUS_UNRECOVERABLE;
2355 }
2356 }
2357 }
2358 }
2359 if (status == IMPORT_STATUS_UNSATISFIED) {
2360 for (std::vector<std::string>::const_iterator iter = pending.begin();
2361 iter != pending.end();
2362 ++iter) {
2363 pending_extension_manager()->AddFromExtensionImport(
2364 *iter,
2365 extension_urls::GetWebstoreUpdateUrl(),
2366 IsSharedModule);
2367 }
2368 CheckForUpdatesSoon();
2369 }
2370 return status;
2371}
2372
2373scoped_ptr<const ExtensionSet>
2374 ExtensionService::GetDependentExtensions(const Extension* extension) {
2375 scoped_ptr<ExtensionSet> dependents(new ExtensionSet());
2376 scoped_ptr<ExtensionSet> set_to_check(new ExtensionSet());
2377 if (SharedModuleInfo::IsSharedModule(extension)) {
2378 set_to_check->InsertAll(disabled_extensions_);
2379 set_to_check->InsertAll(delayed_installs_);
2380 set_to_check->InsertAll(extensions_);
2381 for (ExtensionSet::const_iterator iter = set_to_check->begin();
2382 iter != set_to_check->end(); ++iter) {
[email protected]198b5902013-06-27 10:36:112383 if (SharedModuleInfo::ImportsExtensionById(iter->get(),
2384 extension->id())) {
[email protected]9f4e4f082013-06-21 07:11:192385 dependents->Insert(*iter);
2386 }
2387 }
2388 }
2389 return dependents.PassAs<const ExtensionSet>();
2390}
2391
2392void ExtensionService::PruneSharedModulesOnUninstall(
2393 const Extension* extension) {
2394 if (SharedModuleInfo::ImportsModules(extension)) {
2395 const std::vector<SharedModuleInfo::ImportInfo>& imports =
2396 SharedModuleInfo::GetImports(extension);
2397 std::vector<SharedModuleInfo::ImportInfo>::const_iterator i;
2398 for (i = imports.begin(); i != imports.end(); ++i) {
2399 const Extension* imported_module =
2400 GetExtensionById(i->extension_id, true);
2401 if (imported_module && imported_module->from_webstore()) {
2402 scoped_ptr<const ExtensionSet> dependents =
2403 GetDependentExtensions(imported_module);
2404 if (dependents->size() == 0) {
[email protected]4c9201c42013-08-16 04:56:212405 UninstallExtension(i->extension_id, true, NULL);
[email protected]9f4e4f082013-06-21 07:11:192406 }
2407 }
2408 }
2409 }
2410}
2411
[email protected]8266d662011-07-12 21:53:262412void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502413 const Extension* extension,
[email protected]98270432012-09-11 20:51:242414 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052415 bool has_requirement_errors,
[email protected]9f3c8532013-07-31 19:52:072416 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]0db124b02012-11-07 04:55:052417 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122418 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202419
[email protected]c3cfb012011-04-06 22:07:352420 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032421 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412422 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062423 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
[email protected]8f3bcbd2013-06-05 08:42:402424 if (!pending_extension_info->ShouldAllowInstall(extension)) {
[email protected]51a3bf8b2012-06-08 22:53:062425 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322426
[email protected]31bb5ee62012-09-12 22:58:402427 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2428 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222429 << " and update URL "
2430 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402431 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542432
[email protected]4416c5a2010-06-26 01:28:572433 // Delete the extension directory since we're not going to
2434 // load it.
[email protected]7f8f24f2012-11-15 19:40:142435 if (!GetFileTaskRunner()->PostTask(
2436 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362437 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402438 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362439 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402440 }
[email protected]4416c5a2010-06-26 01:28:572441 return;
2442 }
[email protected]51a3bf8b2012-06-08 22:53:062443
2444 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572445 } else {
[email protected]c3cfb012011-04-06 22:07:352446 // We explicitly want to re-enable an uninstalled external
2447 // extension; if we're here, that means the user is manually
2448 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332449 if (IsExternalExtensionUninstalled(id)) {
2450 initial_enable = true;
2451 }
[email protected]aa142702010-03-26 01:26:332452 }
2453
[email protected]98270432012-09-11 20:51:242454 // Unsupported requirements overrides the management policy.
2455 if (has_requirement_errors) {
2456 initial_enable = false;
2457 extension_prefs_->AddDisableReason(
2458 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2459 // If the extension was disabled because of unsupported requirements but
2460 // now supports all requirements after an update and there are not other
2461 // disable reasons, enable it.
2462 } else if (extension_prefs_->GetDisableReasons(id) ==
2463 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2464 initial_enable = true;
2465 extension_prefs_->ClearDisableReasons(id);
2466 }
2467
[email protected]9f3c8532013-07-31 19:52:072468 if (blacklist_state == extensions::Blacklist::BLACKLISTED) {
2469 // Installation of a blacklisted extension can happen from sync, policy,
2470 // etc, where to maintain consistency we need to install it, just never
2471 // load it (see AddExtension). Usually it should be the job of callers to
2472 // incercept blacklisted extension earlier (e.g. CrxInstaller, before even
2473 // showing the install dialogue).
2474 extension_prefs()->AcknowledgeBlacklistedExtension(id);
2475 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.SilentInstall",
2476 extension->location(),
2477 Manifest::NUM_LOCATIONS);
2478 }
2479
[email protected]695b5712012-12-06 23:55:282480 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552481 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2482 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302483 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402484 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552485 RecordPermissionMessagesHistogram(
2486 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302487 } else {
2488 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2489 extension->GetType(), 100);
2490 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402491 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552492 }
2493
[email protected]a39921b42012-02-28 03:42:542494 // Certain extension locations are specific enough that we can
2495 // auto-acknowledge any extension that came from one of them.
[email protected]9bb691632013-09-26 18:50:122496 if (Manifest::IsPolicyLocation(extension->location()))
[email protected]a39921b42012-02-28 03:42:542497 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422498 const Extension::State initial_state =
2499 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222500 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]9f3c8532013-07-31 19:52:072501 extension_prefs_->SetDelayedInstallInfo(
2502 extension,
2503 initial_state,
2504 blacklist_state,
2505 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
2506 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052507
2508 // Transfer ownership of |extension|.
[email protected]9f4e4f082013-06-21 07:11:192509 delayed_installs_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052510
2511 // Notify extension of available update.
2512 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2513 profile_, id, extension->manifest()->value());
[email protected]75bdcb872013-03-13 00:41:452514
2515 // Notify observers that app update is available.
2516 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2517 OnAppUpdateAvailable(extension->id()));
[email protected]0db124b02012-11-07 04:55:052518 return;
2519 }
2520
[email protected]9f4e4f082013-06-21 07:11:192521 ImportStatus status = SatisfyImports(extension);
2522 if (installs_delayed_for_gc()) {
[email protected]9f3c8532013-07-31 19:52:072523 extension_prefs_->SetDelayedInstallInfo(
2524 extension,
2525 initial_state,
2526 blacklist_state,
2527 extensions::ExtensionPrefs::DELAY_REASON_GC,
2528 page_ordinal);
[email protected]399583b2012-12-11 09:33:422529 delayed_installs_.Insert(extension);
[email protected]9f4e4f082013-06-21 07:11:192530 } else if (status != IMPORT_STATUS_OK) {
2531 if (status == IMPORT_STATUS_UNSATISFIED) {
[email protected]9f3c8532013-07-31 19:52:072532 extension_prefs_->SetDelayedInstallInfo(
2533 extension,
2534 initial_state,
2535 blacklist_state,
[email protected]9f4e4f082013-06-21 07:11:192536 extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS,
2537 page_ordinal);
2538 delayed_installs_.Insert(extension);
2539 }
[email protected]399583b2012-12-11 09:33:422540 } else {
[email protected]9f3c8532013-07-31 19:52:072541 AddNewOrUpdatedExtension(extension,
2542 initial_state,
2543 blacklist_state,
2544 page_ordinal);
[email protected]399583b2012-12-11 09:33:422545 }
[email protected]8c484b742012-11-29 06:05:362546}
2547
2548void ExtensionService::AddNewOrUpdatedExtension(
2549 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422550 Extension::State initial_state,
[email protected]9f3c8532013-07-31 19:52:072551 extensions::Blacklist::BlacklistState blacklist_state,
[email protected]399583b2012-12-11 09:33:422552 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362553 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9f3c8532013-07-31 19:52:072554 extension_prefs_->OnExtensionInstalled(extension,
2555 initial_state,
2556 blacklist_state,
2557 page_ordinal);
[email protected]9e9c1d12013-07-31 01:58:122558 delayed_installs_.Remove(extension->id());
[email protected]6f6101832012-11-27 22:10:482559 FinishInstallation(extension);
2560}
2561
2562void ExtensionService::MaybeFinishDelayedInstallation(
2563 const std::string& extension_id) {
[email protected]9f4e4f082013-06-21 07:11:192564 // Check if the extension already got installed.
2565 if (!delayed_installs_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482566 return;
[email protected]9f4e4f082013-06-21 07:11:192567 extensions::ExtensionPrefs::DelayReason reason =
2568 extension_prefs_->GetDelayedInstallReason(extension_id);
2569
2570 // Check if the extension is idle. DELAY_REASON_NONE is used for older
2571 // preferences files that will not have set this field but it was previously
2572 // only used for idle updates.
2573 if ((reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE ||
2574 reason == extensions::ExtensionPrefs::DELAY_REASON_NONE) &&
2575 is_ready() && !IsExtensionIdle(extension_id))
[email protected]6f6101832012-11-27 22:10:482576 return;
2577
[email protected]9f4e4f082013-06-21 07:11:192578 const Extension* extension = delayed_installs_.GetByID(extension_id);
2579 if (reason == extensions::ExtensionPrefs::DELAY_REASON_WAIT_FOR_IMPORTS) {
2580 ImportStatus status = SatisfyImports(extension);
2581 if (status != IMPORT_STATUS_OK) {
2582 if (status == IMPORT_STATUS_UNRECOVERABLE) {
2583 delayed_installs_.Remove(extension_id);
2584 // Make sure no version of the extension is actually installed, (i.e.,
2585 // that this delayed install was not an update).
2586 CHECK(!extension_prefs_->GetInstalledExtensionInfo(extension_id).get());
2587 extension_prefs_->DeleteExtensionPrefs(extension_id);
2588 }
2589 return;
2590 }
2591 }
2592
[email protected]6f6101832012-11-27 22:10:482593 FinishDelayedInstallation(extension_id);
2594}
2595
2596void ExtensionService::FinishDelayedInstallation(
2597 const std::string& extension_id) {
2598 scoped_refptr<const Extension> extension(
2599 GetPendingExtensionUpdate(extension_id));
[email protected]dc24976f2013-06-02 21:15:092600 CHECK(extension.get());
[email protected]9f4e4f082013-06-21 07:11:192601 delayed_installs_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482602
[email protected]399583b2012-12-11 09:33:422603 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482604 NOTREACHED();
2605
[email protected]dc24976f2013-06-02 21:15:092606 FinishInstallation(extension.get());
[email protected]6f6101832012-11-27 22:10:482607}
2608
2609void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]e7554c3f2013-05-29 00:36:562610 const extensions::Extension* existing_extension =
2611 GetInstalledExtension(extension->id());
2612 bool is_update = false;
2613 std::string old_name;
2614 if (existing_extension) {
2615 is_update = true;
2616 old_name = existing_extension->name();
2617 }
2618 extensions::InstalledExtensionInfo details(extension, is_update, old_name);
[email protected]fcb58a862012-05-01 01:03:152619 content::NotificationService::current()->Notify(
2620 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2621 content::Source<Profile>(profile_),
[email protected]41bb80bd2013-05-03 10:56:022622 content::Details<const extensions::InstalledExtensionInfo>(&details));
[email protected]fcb58a862012-05-01 01:03:152623
[email protected]d96eb512012-11-01 23:44:082624 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362625
[email protected]399583b2012-12-11 09:33:422626 // Unpacked extensions default to allowing file access, but if that has been
2627 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402628 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422629 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2630 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2631 }
2632
[email protected]6f6101832012-11-27 22:10:482633 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032634
2635 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082636 // so he can reenable it. We do this last so that it has already been
2637 // added to our list of extensions.
[email protected]3e3fafbb2013-08-17 13:33:162638 if (unacknowledged_external && !is_update) {
[email protected]612a1cb12012-10-17 13:18:032639 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082640 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2641 EXTERNAL_EXTENSION_INSTALLED,
2642 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]446e37e2013-08-07 00:30:022643 if (extensions::ManifestURL::UpdatesFromGallery(extension)) {
2644 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventWebstore",
2645 EXTERNAL_EXTENSION_INSTALLED,
2646 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2647 } else {
2648 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEventNonWebstore",
2649 EXTERNAL_EXTENSION_INSTALLED,
2650 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2651 }
[email protected]d96eb512012-11-01 23:44:082652 }
[email protected]9f4e4f082013-06-21 07:11:192653
2654 // Check extensions that may have been delayed only because this shared module
2655 // was not available.
2656 if (SharedModuleInfo::IsSharedModule(extension)) {
2657 MaybeFinishDelayedInstallations();
2658 }
[email protected]4a190632009-05-09 01:07:422659}
2660
[email protected]76b65442012-11-17 14:11:482661const Extension* ExtensionService::GetPendingExtensionUpdate(
2662 const std::string& id) const {
[email protected]9f4e4f082013-06-21 07:11:192663 return delayed_installs_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482664}
2665
[email protected]bb7f40952011-01-13 00:21:202666void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462667 if (!terminated_extensions_.Contains(extension->id()))
2668 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202669
[email protected]b3f7fe22011-11-11 19:27:562670 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202671}
2672
2673void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202674 std::string lowercase_id = StringToLowerASCII(id);
[email protected]60a174a2013-08-02 20:29:282675 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
[email protected]84df8332011-12-06 18:22:462676 terminated_extensions_.Remove(lowercase_id);
[email protected]fb789532013-08-27 02:40:212677 if (extension) {
2678 content::NotificationService::current()->Notify(
2679 chrome::NOTIFICATION_EXTENSION_REMOVED,
2680 content::Source<Profile>(profile_),
2681 content::Details<const Extension>(extension));
2682 }
[email protected]bb7f40952011-01-13 00:21:202683}
2684
[email protected]0dfe05c2011-02-23 23:03:362685const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472686 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282687 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472688}
2689
2690const Extension* ExtensionService::GetInstalledExtension(
2691 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282692 int include_mask = INCLUDE_ENABLED |
2693 INCLUDE_DISABLED |
2694 INCLUDE_TERMINATED |
2695 INCLUDE_BLACKLISTED;
2696 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362697}
2698
[email protected]eaa7dd182010-12-14 11:09:002699bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442700 // Allow bindings for all packaged extensions and component hosted apps.
[email protected]be9915fb2013-07-18 09:28:552701 const Extension* extension = extensions_.GetExtensionOrAppByURL(url);
[email protected]615d88f2011-12-13 01:47:442702 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402703 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392704}
2705
[email protected]4d007b312012-10-17 03:00:482706bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
[email protected]be9915fb2013-07-18 09:28:552707 const Extension* extension = extensions_.GetExtensionOrAppByURL(*url);
[email protected]4d007b312012-10-17 03:00:482708 if (extension && extension->is_platform_app()) {
2709 *url = GURL(chrome::kExtensionInvalidRequestURL);
2710 return true;
2711 }
2712
2713 return false;
2714}
2715
[email protected]9060d8b02012-01-13 02:14:302716bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322717 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142718 const Version* version,
[email protected]650b2d52013-02-10 03:41:452719 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402720 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512721 int creation_flags,
2722 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382723 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2724 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012725 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302726 return false;
[email protected]a8af9fdb2010-10-28 21:52:202727
[email protected]7577a5c52009-07-30 06:21:582728 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492729 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582730 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022731 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352732
[email protected]7577a5c52009-07-30 06:21:582733 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352734 // The default apps will have the location set as INTERNAL. Since older
2735 // default apps are installed as EXTERNAL, we override them. However, if the
2736 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402737 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352738 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402739 (location == Manifest::INTERNAL &&
2740 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352741
2742 if (!is_default_apps_migration) {
2743 DCHECK(version);
2744
2745 switch (existing->version()->CompareTo(*version)) {
2746 case -1: // existing version is older, we should upgrade
2747 break;
2748 case 0: // existing version is same, do nothing
2749 return false;
2750 case 1: // existing version is newer, uh-oh
2751 LOG(WARNING) << "Found external version of extension " << id
2752 << "that is older than current version. Current version "
2753 << "is: " << existing->VersionString() << ". New "
2754 << "version is: " << version->GetString()
2755 << ". Keeping current version.";
2756 return false;
2757 }
[email protected]7577a5c52009-07-30 06:21:582758 }
2759 }
2760
[email protected]9060d8b02012-01-13 02:14:302761 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302762 if (!pending_extension_manager()->AddFromExternalFile(
2763 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302764 return false;
[email protected]e3987852012-05-04 10:06:302765 }
[email protected]9c635f22010-12-02 09:36:362766
[email protected]14908b72011-04-20 06:54:362767 // no client (silent install)
[email protected]f8636f92013-08-09 21:02:372768 scoped_refptr<CrxInstaller> installer(CrxInstaller::CreateSilent(this));
[email protected]6dfbbf82010-03-12 23:09:162769 installer->set_install_source(location);
2770 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072771 installer->set_expected_version(*version);
2772 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312773 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182774#if defined(OS_CHROMEOS)
2775 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2776#else
[email protected]6dfbbf82010-03-12 23:09:162777 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182778#endif
[email protected]47fc70c2011-12-06 07:29:512779
2780 // Depending on the source, a new external extension might not need a user
2781 // notification on installation. For such extensions, mark them acknowledged
2782 // now to suppress the notification.
2783 if (mark_acknowledged)
2784 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302785
2786 return true;
[email protected]7577a5c52009-07-30 06:21:582787}
2788
[email protected]eaa7dd182010-12-14 11:09:002789void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452790 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402791 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402792 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012793 content::NotificationService::current()->Notify(
2794 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2795 content::Source<Profile>(profile_),
2796 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402797
[email protected]8a205c02011-02-04 20:41:332798 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212799 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042800 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492801 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402802 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2803}
2804
[email protected]eaa7dd182010-12-14 11:09:002805void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132806 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182807 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332808 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182809 if (iter == orphaned_dev_tools_.end())
2810 return;
2811
[email protected]04ea1bb2013-07-10 09:26:092812 iter->second->ConnectRenderViewHost(host->render_view_host());
[email protected]406027c02010-09-27 08:03:182813 orphaned_dev_tools_.erase(iter);
2814}
2815
[email protected]432115822011-07-10 15:52:272816void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532817 const content::NotificationSource& source,
2818 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272819 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542820 case chrome::NOTIFICATION_APP_TERMINATING:
2821 // Shutdown has started. Don't start any more extension installs.
2822 // (We cannot use ExtensionService::Shutdown() for this because it
2823 // happens too late in browser teardown.)
2824 browser_terminating_ = true;
2825 break;
[email protected]432115822011-07-10 15:52:272826 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532827 if (profile_ !=
2828 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322829 break;
[email protected]6c2381d2011-10-19 02:52:532830 }
[email protected]a4ed6282009-12-14 20:51:162831
[email protected]3a1dc572012-07-31 22:25:132832 extensions::ExtensionHost* host =
2833 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262834
[email protected]fa2416f2011-05-03 08:41:202835 // Mark the extension as terminated and Unload it. We want it to
2836 // be in a consistent state: either fully working or not loaded
2837 // at all, but never half-crashed. We do it in a PostTask so
2838 // that other handlers of this notification will still have
2839 // access to the Extension and ExtensionHost.
[email protected]b3a25092013-05-28 22:08:162840 base::MessageLoop::current()->PostTask(
[email protected]14908b72011-04-20 06:54:362841 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362842 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202843 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012844 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202845 host->extension()));
[email protected]31f77262009-12-02 20:48:532846 break;
2847 }
[email protected]432115822011-07-10 15:52:272848 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302849 content::RenderProcessHost* process =
2850 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192851 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302852 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192853 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2854 break;
2855
[email protected]15397b42012-05-16 23:56:062856 // Extensions need to know the channel for API restrictions.
2857 process->Send(new ExtensionMsg_SetChannel(
[email protected]15d4d2d2013-08-09 06:49:032858 extensions::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062859
[email protected]4941fe92013-06-13 23:21:012860 // Platform apps need to know the system font.
2861 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue);
2862 webui::SetFontAndTextDirection(fonts.get());
2863 std::string font_family, font_size;
2864 fonts->GetString("fontfamily", &font_family);
2865 fonts->GetString("fontsize", &font_size);
2866 process->Send(new ExtensionMsg_SetSystemFont(
2867 font_family, font_size));
2868
[email protected]77a6970c2011-04-23 16:58:562869 // Valid extension function names, used to setup bindings in renderer.
2870 std::vector<std::string> function_names;
2871 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2872 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532873
[email protected]77a6970c2011-04-23 16:58:562874 // Scripting whitelist. This is modified by tests and must be communicated
2875 // to renderers.
2876 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2877 *Extension::GetScriptingWhitelist()));
2878
2879 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532880 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462881 for (ExtensionSet::const_iterator iter = extensions_.begin();
2882 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492883 // Renderers don't need to know about themes.
2884 if (!(*iter)->is_theme())
[email protected]cadac622013-06-11 16:46:362885 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get()));
[email protected]77a6970c2011-04-23 16:58:562886 }
[email protected]9776e82e2011-11-15 02:17:532887 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562888 break;
2889 }
[email protected]432115822011-07-10 15:52:272890 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302891 content::RenderProcessHost* process =
2892 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192893 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302894 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192895 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2896 break;
2897
[email protected]6bc04fd82011-12-04 02:29:352898 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462899 BrowserThread::PostTask(
2900 BrowserThread::IO, FROM_HERE,
2901 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082902 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302903 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212904 break;
2905 }
[email protected]0db124b02012-11-07 04:55:052906 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2907 extensions::ExtensionHost* host =
2908 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222909 std::string extension_id = host->extension_id();
[email protected]9f4e4f082013-06-21 07:11:192910 if (delayed_installs_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222911 // We were waiting for this extension to become idle, it now might have,
2912 // so maybe finish installation.
[email protected]b3a25092013-05-28 22:08:162913 base::MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052914 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482915 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052916 AsWeakPtr(), extension_id),
2917 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052918 }
2919 break;
2920 }
[email protected]92dd8d92013-02-26 00:41:082921 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2922 // Notify extensions that chrome update is available.
2923 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2924 profile_);
[email protected]75bdcb872013-03-13 00:41:452925
2926 // Notify observers that chrome update is available.
2927 FOR_EACH_OBSERVER(extensions::UpdateObserver, update_observers_,
2928 OnChromeUpdateAvailable());
[email protected]92dd8d92013-02-26 00:41:082929 break;
2930 }
[email protected]aa96d3a2010-08-21 08:45:252931
[email protected]4814b512009-11-07 00:12:292932 default:
2933 NOTREACHED() << "Unexpected notification type.";
2934 }
2935}
2936
[email protected]90bb38d2012-11-14 18:36:032937void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182938 IdentifyAlertableExtensions();
2939 CheckManagementPolicy();
2940}
2941
[email protected]eaa7dd182010-12-14 11:09:002942bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182943 return !GetAppIds().empty();
2944}
[email protected]377011d2010-07-20 04:18:502945
[email protected]eaa7dd182010-12-14 11:09:002946ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182947 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462948 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502949 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402950 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182951 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502952 }
2953
[email protected]ec5b50d2010-10-09 16:35:182954 return result;
[email protected]377011d2010-07-20 04:18:502955}
[email protected]d7e9a862010-11-03 21:57:492956
[email protected]dc9a74f72012-08-17 18:07:212957bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
[email protected]9367eabc2013-03-01 01:29:292958 if (!extensions::BackgroundInfo::HasPersistentBackgroundPage(extension))
[email protected]dc9a74f72012-08-17 18:07:212959 return true;
2960 ExtensionRuntimeDataMap::const_iterator it =
2961 extension_runtime_data_.find(extension->id());
2962 return it == extension_runtime_data_.end() ? false :
2963 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492964}
2965
[email protected]eaa7dd182010-12-14 11:09:002966void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]9367eabc2013-03-01 01:29:292967 DCHECK(extensions::BackgroundInfo::HasBackgroundPage(extension));
[email protected]d7e9a862010-11-03 21:57:492968 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072969 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272970 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532971 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072972 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492973}
2974
[email protected]dc9a74f72012-08-17 18:07:212975bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2976 ExtensionRuntimeDataMap::const_iterator it =
2977 extension_runtime_data_.find(extension->id());
2978 return it == extension_runtime_data_.end() ? false :
2979 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492980}
2981
[email protected]eaa7dd182010-12-14 11:09:002982void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]b914e2952013-04-26 07:10:032983 bool value) {
[email protected]d7e9a862010-11-03 21:57:492984 extension_runtime_data_[extension->id()].being_upgraded = value;
2985}
[email protected]1bead0712010-11-27 17:41:532986
[email protected]e178ad92013-06-28 02:29:252987bool ExtensionService::IsBeingReloaded(
2988 const std::string& extension_id) const {
2989 return ContainsKey(extensions_being_reloaded_, extension_id);
2990}
2991
2992void ExtensionService::SetBeingReloaded(const std::string& extension_id,
[email protected]5e7015c2013-07-23 23:29:432993 bool isBeingReloaded) {
2994 if (isBeingReloaded)
[email protected]e178ad92013-06-28 02:29:252995 extensions_being_reloaded_.insert(extension_id);
[email protected]5e7015c2013-07-23 23:29:432996 else
[email protected]e178ad92013-06-28 02:29:252997 extensions_being_reloaded_.erase(extension_id);
[email protected]e178ad92013-06-28 02:29:252998}
2999
[email protected]dc9a74f72012-08-17 18:07:213000bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
3001 ExtensionRuntimeDataMap::const_iterator it =
3002 extension_runtime_data_.find(extension->id());
3003 return it == extension_runtime_data_.end() ? false :
3004 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:073005}
3006
3007void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
3008 bool value) {
3009 extension_runtime_data_[extension->id()].has_used_webrequest = value;
3010}
3011
[email protected]612a1cb12012-10-17 13:18:033012bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
3013 // Extensions installed by policy can't be disabled. So even if a previous
3014 // installation disabled the extension, make sure it is now enabled.
3015 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
3016 return true;
3017
3018 if (extension_prefs_->IsExtensionDisabled(extension->id()))
3019 return false;
3020
[email protected]41070e8d2012-10-24 01:34:363021 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:033022 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:543023 // enabling them. Hosted apps are excepted because they are not dangerous
3024 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:403025 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
3026 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:033027 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
3028 return false;
3029 }
3030 }
[email protected]612a1cb12012-10-17 13:18:033031
3032 return true;
3033}
[email protected]0db124b02012-11-07 04:55:053034
3035bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:473036 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:333037 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:053038 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:473039 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223040 if (host)
3041 return false;
3042 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
3043}
3044
3045bool ExtensionService::ShouldDelayExtensionUpdate(
3046 const std::string& extension_id,
3047 bool wait_for_idle) const {
3048 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
3049
3050 // If delayed updates are globally disabled, or just for this extension,
3051 // don't delay.
3052 if (!install_updates_when_idle_ || !wait_for_idle)
3053 return false;
3054
[email protected]695b5712012-12-06 23:55:283055 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223056 // If there is no old extension, this is not an update, so don't delay.
3057 if (!old)
3058 return false;
3059
[email protected]9367eabc2013-03-01 01:29:293060 if (extensions::BackgroundInfo::HasPersistentBackgroundPage(old)) {
[email protected]e7aa7b7e2012-11-27 04:51:223061 // Delay installation if the extension listens for the onUpdateAvailable
3062 // event.
3063 return system_->event_router()->ExtensionHasEventListener(
3064 extension_id, kOnUpdateAvailableEvent);
3065 } else {
3066 // Delay installation if the extension is not idle.
3067 return !IsExtensionIdle(extension_id);
3068 }
[email protected]0db124b02012-11-07 04:55:053069}
[email protected]fdd679b2012-11-15 20:49:393070
[email protected]399583b2012-12-11 09:33:423071void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:453072 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
3073 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:423074 for (ExtensionSet::const_iterator it = extensions_.begin();
3075 it != extensions_.end(); ++it) {
[email protected]cadac622013-06-11 16:46:363076 if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
3077 active_paths->insert(BrowserContext::GetStoragePartitionForSite(
3078 profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
[email protected]399583b2012-12-11 09:33:423079 }
3080 }
3081
[email protected]9f4e4f082013-06-21 07:11:193082 DCHECK(!installs_delayed_for_gc());
3083 set_installs_delayed_for_gc(true);
[email protected]399583b2012-12-11 09:33:423084 BrowserContext::GarbageCollectStoragePartitions(
3085 profile_, active_paths.Pass(),
3086 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3087 AsWeakPtr()));
3088}
3089
3090void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
[email protected]9f4e4f082013-06-21 07:11:193091 set_installs_delayed_for_gc(false);
3092 MaybeFinishDelayedInstallations();
3093}
3094
3095void ExtensionService::MaybeFinishDelayedInstallations() {
3096 std::vector<std::string> to_be_installed;
[email protected]399583b2012-12-11 09:33:423097 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3098 it != delayed_installs_.end();
3099 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193100 to_be_installed.push_back((*it)->id());
[email protected]399583b2012-12-11 09:33:423101 }
[email protected]9f4e4f082013-06-21 07:11:193102 for (std::vector<std::string>::const_iterator it = to_be_installed.begin();
3103 it != to_be_installed.end();
[email protected]399583b2012-12-11 09:33:423104 ++it) {
[email protected]9f4e4f082013-06-21 07:11:193105 MaybeFinishDelayedInstallation(*it);
[email protected]399583b2012-12-11 09:33:423106 }
[email protected]399583b2012-12-11 09:33:423107}
3108
3109void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3110 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3111}
3112
[email protected]fdd679b2012-11-15 20:49:393113void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283114 blacklist_->GetBlacklistedIDs(
3115 GenerateInstalledExtensionsSet()->GetIDs(),
[email protected]3f2a2fa2013-09-24 02:55:253116 base::Bind(&ExtensionService::ManageBlacklist, AsWeakPtr()));
[email protected]695b5712012-12-06 23:55:283117}
3118
[email protected]3f2a2fa2013-09-24 02:55:253119void ExtensionService::ManageBlacklist(const std::set<std::string>& updated) {
[email protected]695b5712012-12-06 23:55:283120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3121
[email protected]3f2a2fa2013-09-24 02:55:253122 std::set<std::string> before = blacklisted_extensions_.GetIDs();
[email protected]16bf7ba72013-08-23 11:52:543123 std::set<std::string> no_longer_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:253124 base::STLSetDifference<std::set<std::string> >(before, updated);
[email protected]16bf7ba72013-08-23 11:52:543125 std::set<std::string> not_yet_blacklisted =
[email protected]3f2a2fa2013-09-24 02:55:253126 base::STLSetDifference<std::set<std::string> >(updated, before);
[email protected]695b5712012-12-06 23:55:283127
3128 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3129 it != no_longer_blacklisted.end(); ++it) {
3130 scoped_refptr<const Extension> extension =
3131 blacklisted_extensions_.GetByID(*it);
[email protected]3f2a2fa2013-09-24 02:55:253132 if (!extension.get()) {
3133 NOTREACHED() << "Extension " << *it << " no longer blacklisted, "
3134 << "but it was never blacklisted.";
[email protected]695b5712012-12-06 23:55:283135 continue;
[email protected]3f2a2fa2013-09-24 02:55:253136 }
[email protected]695b5712012-12-06 23:55:283137 blacklisted_extensions_.Remove(*it);
[email protected]3f2a2fa2013-09-24 02:55:253138 extension_prefs_->SetExtensionBlacklisted(extension->id(), false);
[email protected]dc24976f2013-06-02 21:15:093139 AddExtension(extension.get());
[email protected]ac875372013-02-28 04:36:093140 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.UnblacklistInstalled",
[email protected]dc24976f2013-06-02 21:15:093141 extension->location(),
3142 Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283143 }
3144
3145 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3146 it != not_yet_blacklisted.end(); ++it) {
3147 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
[email protected]3f2a2fa2013-09-24 02:55:253148 if (!extension.get()) {
3149 NOTREACHED() << "Extension " << *it << " needs to be "
3150 << "blacklisted, but it's not installed.";
[email protected]695b5712012-12-06 23:55:283151 continue;
[email protected]3f2a2fa2013-09-24 02:55:253152 }
[email protected]695b5712012-12-06 23:55:283153 blacklisted_extensions_.Insert(extension);
[email protected]3f2a2fa2013-09-24 02:55:253154 extension_prefs_->SetExtensionBlacklisted(extension->id(), true);
[email protected]695b5712012-12-06 23:55:283155 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
[email protected]ac875372013-02-28 04:36:093156 UMA_HISTOGRAM_ENUMERATION("ExtensionBlacklist.BlacklistInstalled",
3157 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]695b5712012-12-06 23:55:283158 }
3159
3160 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393161}
[email protected]75bdcb872013-03-13 00:41:453162
3163void ExtensionService::AddUpdateObserver(extensions::UpdateObserver* observer) {
3164 update_observers_.AddObserver(observer);
3165}
3166
3167void ExtensionService::RemoveUpdateObserver(
3168 extensions::UpdateObserver* observer) {
3169 update_observers_.RemoveObserver(observer);
3170}