blob: 422430ffa4d4e8fc5568430dd4a754923732e2f5 [file] [log] [blame]
[email protected]16d900042012-01-04 21:24:321// Copyright (c) 2012 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]1e127022011-11-29 21:11:4718#include "base/path_service.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]e83326f2010-07-31 17:29:2520#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2521#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0422#include "base/stringprintf.h"
[email protected]7f8f24f2012-11-15 19:40:1423#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0624#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3625#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5926#include "base/utf_string_conversions.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]a692a952011-11-28 08:23:5429#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]e07f2da2012-08-01 22:46:1230#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]15730c42009-09-03 00:03:2031#include "chrome/browser/browser_process.h"
[email protected]70019152012-12-19 11:44:1932#include "chrome/browser/devtools/devtools_window.h"
[email protected]32b36c62012-10-24 05:37:4133#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1234#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4435#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]c39df1a2013-01-08 18:22:0736#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3137#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]4636c832013-01-11 02:10:1138#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]af9db5f2011-10-05 05:13:1539#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]5db9ada2012-04-11 13:48:2040#include "chrome/browser/extensions/app_sync_data.h"
[email protected]5a38dfd2012-07-23 23:22:1041#include "chrome/browser/extensions/browser_event_router.h"
[email protected]d8c8f25f2011-11-02 18:18:0142#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1743#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3844#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4445#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2446#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1847#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5648#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2249#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2950#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2351#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4352#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0453#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0854#include "chrome/browser/extensions/extension_system.h"
[email protected]612a1cb12012-10-17 13:18:0355#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2756#include "chrome/browser/extensions/external_provider_impl.h"
57#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0158#include "chrome/browser/extensions/installed_loader.h"
[email protected]6cafe35a2012-08-27 22:40:5059#include "chrome/browser/extensions/lazy_background_task_queue.h"
[email protected]fdd679b2012-11-15 20:49:3960#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3761#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3962#include "chrome/browser/extensions/permissions_updater.h"
[email protected]6cafe35a2012-08-27 22:40:5063#include "chrome/browser/extensions/platform_app_launcher.h"
[email protected]6cafe35a2012-08-27 22:40:5064#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]d8c8f25f2011-11-02 18:18:0165#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1166#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]56ad3792010-05-28 17:45:3367#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]c2295f72013-01-03 22:18:5668#include "chrome/browser/plugins/chrome_plugin_service_filter.h"
[email protected]37858e52010-08-26 00:22:0269#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3370#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3971#include "chrome/browser/profiles/profile_manager.h"
[email protected]d5949312012-12-03 22:13:3072#include "chrome/browser/themes/theme_service.h"
73#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1774#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
75#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3176#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1577#include "chrome/browser/ui/webui/theme_source.h"
[email protected]aab98a52009-12-02 03:22:3578#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2779#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4780#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5481#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0682#include "chrome/common/chrome_version_info.h"
[email protected]5b1a0e22009-05-26 19:00:5883#include "chrome/common/extensions/extension.h"
[email protected]7c927b62010-02-24 09:54:1384#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1285#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5686#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2187#include "chrome/common/extensions/extension_resource.h"
[email protected]612a1cb12012-10-17 13:18:0388#include "chrome/common/extensions/feature_switch.h"
[email protected]e9f541a2012-11-19 21:52:3189#include "chrome/common/extensions/features/feature.h"
[email protected]0db124b02012-11-07 04:55:0590#include "chrome/common/extensions/manifest.h"
[email protected]65348062013-01-15 07:27:2291#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]25b34332009-06-05 21:53:1992#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1493#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4994#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3695#include "content/public/browser/devtools_agent_host.h"
[email protected]0e12d7d2011-12-01 16:21:4496#include "content/public/browser/devtools_manager.h"
[email protected]05aad2da2011-10-28 10:12:3797#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1698#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:3899#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:30100#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:42101#include "content/public/browser/site_instance.h"
102#include "content/public/browser/storage_partition.h"
[email protected]1e127022011-11-29 21:11:47103#include "content/public/common/pepper_plugin_info.h"
[email protected]e9f541a2012-11-19 21:52:31104#include "extensions/common/error_utils.h"
[email protected]c10da4b02010-03-25 14:38:32105#include "googleurl/src/gurl.h"
[email protected]65187152012-06-02 13:14:14106#include "grit/generated_resources.h"
[email protected]be28b5f42012-07-20 11:31:25107#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]895a1e52012-05-15 02:50:12108#include "sync/api/sync_change.h"
109#include "sync/api/sync_error_factory.h"
[email protected]24b538a2010-02-27 01:22:44110#include "webkit/database/database_tracker.h"
111#include "webkit/database/database_util.h"
[email protected]79a60642012-10-20 21:03:18112
[email protected]eed367e2011-04-12 03:43:31113#if defined(OS_CHROMEOS)
[email protected]88e8ec9152013-01-17 04:05:18114#include "chrome/browser/chromeos/extensions/install_limiter.h"
[email protected]b777b332011-04-16 04:01:08115#include "webkit/fileapi/file_system_context.h"
116#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31117#endif
118
[email protected]55eb70e762012-02-20 17:38:39119using content::BrowserContext;
[email protected]631bb742011-11-02 11:29:39120using content::BrowserThread;
[email protected]98f66112012-12-25 12:59:36121using content::DevToolsAgentHost;
[email protected]3a5180ae2011-12-21 02:39:38122using content::PluginService;
[email protected]bf3d9df2012-07-24 23:20:27123using extensions::CrxInstaller;
[email protected]1c321ee52012-05-21 03:02:34124using extensions::Extension;
125using extensions::ExtensionIdSet;
126using extensions::ExtensionInfo;
[email protected]215a7be2012-10-22 19:53:42127using extensions::FeatureSwitch;
[email protected]c2e66e12012-06-27 06:27:06128using extensions::PermissionMessage;
129using extensions::PermissionMessages;
130using extensions::PermissionSet;
[email protected]e410b5f2012-12-14 14:02:24131using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05132
[email protected]c6d474f82009-12-16 21:11:06133namespace errors = extension_manifest_errors;
134
[email protected]b6ab96d2009-08-20 18:58:19135namespace {
136
[email protected]d96eb512012-11-01 23:44:08137// Histogram values for logging events related to externally installed
138// extensions.
139enum ExternalExtensionEvent {
140 EXTERNAL_EXTENSION_INSTALLED = 0,
141 EXTERNAL_EXTENSION_IGNORED,
142 EXTERNAL_EXTENSION_REENABLED,
143 EXTERNAL_EXTENSION_UNINSTALLED,
144 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
145};
146
[email protected]612a1cb12012-10-17 13:18:03147// Prompt the user this many times before considering an extension acknowledged.
148static const int kMaxExtensionAcknowledgePromptCount = 3;
149
[email protected]0db124b02012-11-07 04:55:05150// Wait this many seconds after an extensions becomes idle before updating it.
151static const int kUpdateIdleDelay = 5;
152
[email protected]9bd9a6862012-11-29 09:24:22153// Wait this many seconds before trying to garbage collect extensions again.
154static const int kGarbageCollectRetryDelay = 30;
155
[email protected]300c0ea2011-07-15 23:04:33156const char* kNaClPluginMimeType = "application/x-nacl";
157
[email protected]3bdba0d2011-08-23 07:17:30158static bool IsSyncableExtension(const Extension& extension) {
159 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
160}
161
162static bool IsSyncableApp(const Extension& extension) {
163 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
164}
165
[email protected]c6d474f82009-12-16 21:11:06166} // namespace
[email protected]b6ab96d2009-08-20 18:58:19167
[email protected]eaa7dd182010-12-14 11:09:00168ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49169 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57170 being_upgraded(false),
171 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49172}
173
[email protected]eaa7dd182010-12-14 11:09:00174ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49175}
176
[email protected]cebc3dc2011-04-18 17:15:00177ExtensionService::NaClModuleInfo::NaClModuleInfo() {
178}
179
180ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
181}
182
[email protected]eaa7dd182010-12-14 11:09:00183// ExtensionService.
[email protected]6014d672008-12-05 00:38:25184
[email protected]31bb5ee62012-09-12 22:58:40185const char ExtensionService::kInstallDirectoryName[] = "Extensions";
[email protected]15d5b472011-11-24 04:17:24186
[email protected]31bb5ee62012-09-12 22:58:40187const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24188 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40189const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24190 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40191const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24192 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40193const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24194 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29195const char ExtensionService::kManagedSettingsDirectoryName[] =
196 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40197const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]a690e292012-12-19 19:22:49198const char ExtensionService::kRulesStoreName[] = "Extension Rules";
[email protected]494c06e2009-07-25 01:06:42199
[email protected]8e4560b62011-01-14 10:09:14200void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12201 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20202
[email protected]7a4c6852010-09-16 03:44:22203 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27204 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21205 for (i = external_extension_providers_.begin();
206 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14207 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21208 if (i->get()->HasExtension(id))
209 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22210 }
211
[email protected]0f48fca2011-05-19 18:46:35212 // We get the list of external extensions to check from preferences.
213 // It is possible that an extension has preferences but is not loaded.
214 // For example, an extension that requires experimental permissions
215 // will not be loaded if the experimental command line flag is not used.
216 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40217 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35218 // We can't call UninstallExtension with an unloaded/invalid
219 // extension ID.
220 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
221 << "with id: " << id;
222 return;
223 }
[email protected]d6ebc9792011-04-07 18:18:33224 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22225}
226
[email protected]7f8f24f2012-11-15 19:40:14227void ExtensionService::SetFileTaskRunnerForTesting(
228 base::SequencedTaskRunner* task_runner) {
229 file_task_runner_ = task_runner;
230}
231
[email protected]8e4560b62011-01-14 10:09:14232void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22233 external_extension_providers_.clear();
234}
235
[email protected]8e4560b62011-01-14 10:09:14236void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27237 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12238 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21239 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27240 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22241}
242
[email protected]9060d8b02012-01-13 02:14:30243bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22244 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42245 const GURL& update_url,
246 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38247 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
248 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20249
[email protected]8a87a5332011-08-11 17:54:59250 const Extension* extension = GetExtensionById(id, true);
251 if (extension) {
252 // Already installed. Skip this install if the current location has
253 // higher priority than |location|.
254 Extension::Location current = extension->location();
255 if (current == Extension::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30256 return false;
[email protected]8a87a5332011-08-11 17:54:59257 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22258 }
[email protected]9060d8b02012-01-13 02:14:30259
260 // Add |id| to the set of pending extensions. If it can not be added,
261 // then there is already a pending record from a higher-priority install
262 // source. In this case, signal that this extension will not be
263 // installed by returning false.
264 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40265 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30266 return false;
[email protected]31bb5ee62012-09-12 22:58:40267 }
[email protected]9060d8b02012-01-13 02:14:30268
[email protected]94fde232012-04-27 10:22:30269 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30270 return true;
[email protected]7a4c6852010-09-16 03:44:22271}
272
[email protected]dc9a74f72012-08-17 18:07:21273const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]615d88f2011-12-13 01:47:44274 const Extension* extension = extensions_.GetExtensionOrAppByURL(
275 ExtensionURLInfo(url));
[email protected]dc9a74f72012-08-17 18:07:21276 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09277}
278
[email protected]dc9a74f72012-08-17 18:07:21279bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09280 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27281}
282
[email protected]404fb002012-10-02 21:06:17283const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21284 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17285 std::set<std::string> extension_ids =
286 process_map_.GetExtensionsInProcess(renderer_child_id);
287 // All apps in one process share the same partition.
288 // It is only possible for the app to have isolated storage
289 // if there is only 1 app in the process.
290 if (extension_ids.size() != 1)
291 return NULL;
292
293 const extensions::Extension* extension =
294 extensions_.GetByID(*(extension_ids.begin()));
295 // We still need to check is_storage_isolated(),
296 // because it's common for there to be one extension in a process
297 // with is_storage_isolated() == false.
298 if (extension && extension->is_storage_isolated())
299 return extension;
300
301 return NULL;
[email protected]da5683db2011-04-23 17:12:21302}
303
[email protected]6aeac8342010-10-01 20:21:18304// static
[email protected]d6ebc9792011-04-07 18:18:33305// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30306// --uninstall-extension, and to uninstall an extension via sync. The LOG
307// statements within this function are used to inform the user if the uninstall
308// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00309bool ExtensionService::UninstallExtensionHelper(
310 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18311 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33312 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40313 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18314 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33315 << "id: " << extension_id;
316 return false;
[email protected]6aeac8342010-10-01 20:21:18317 }
318
[email protected]d6ebc9792011-04-07 18:18:33319 // The following call to UninstallExtension will not allow an uninstall of a
320 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14321 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33322 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
323 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
324 << ": " << error;
325 return false;
326 }
[email protected]95da88c42011-03-31 10:07:33327
[email protected]6aeac8342010-10-01 20:21:18328 return true;
329}
330
[email protected]eaa7dd182010-12-14 11:09:00331ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24332 const CommandLine* command_line,
333 const FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23334 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39335 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03336 bool autoupdate_enabled,
337 bool extensions_enabled)
[email protected]fdd679b2012-11-15 20:49:39338 : extensions::Blacklist::Observer(blacklist),
339 profile_(profile),
[email protected]bd306722012-07-11 20:43:59340 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43341 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28342 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38343 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36344 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23345 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03346 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13347 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33348 install_updates_when_idle_(true),
[email protected]3ecda252010-11-18 19:50:55349 ready_(false),
[email protected]14908b72011-04-20 06:54:36350 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23351 menu_manager_(profile),
[email protected]8d92e0382012-08-01 18:06:52352 app_notification_manager_(
353 new extensions::AppNotificationManager(profile)),
[email protected]7c1490da2011-10-11 18:53:25354 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18355 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54356 browser_terminating_(false),
[email protected]399583b2012-12-11 09:33:42357 installs_delayed_(false),
[email protected]1ff4d282012-12-08 00:39:21358 wipeout_is_active_(false),
[email protected]3a2748c42013-01-15 10:13:23359 wipeout_count_(0u),
[email protected]5db9ada2012-04-11 13:48:20360 app_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
361 extension_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]178f8512012-02-09 01:49:36362 app_shortcut_manager_(profile) {
[email protected]a29a517a2011-01-21 21:11:12363 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20364
[email protected]36a784c2009-06-23 06:21:08365 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40366 if (command_line->HasSwitch(switches::kDisableExtensions) ||
367 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23368 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18369 }
[email protected]36a784c2009-06-23 06:21:08370
[email protected]3c4abc82012-10-22 22:25:54371 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
372 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27373 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07374 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27375 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07376 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27377 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07378 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05379 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
380 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28381 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03382 base::Closure callback =
383 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
384 base::Unretained(this));
385 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
386 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24387 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29388
[email protected]93fd78f42009-07-10 16:43:17389 // Set up the ExtensionUpdater
390 if (autoupdate_enabled) {
391 int update_frequency = kDefaultUpdateFrequencySeconds;
392 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25393 base::StringToInt(command_line->GetSwitchValueASCII(
394 switches::kExtensionsUpdateFrequency),
395 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17396 }
[email protected]42a08162012-03-16 18:09:11397 updater_.reset(new extensions::ExtensionUpdater(this,
398 extension_prefs,
399 profile->GetPrefs(),
400 profile,
[email protected]fdd679b2012-11-15 20:49:39401 blacklist,
[email protected]42a08162012-03-16 18:09:11402 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17403 }
404
[email protected]25ae0152011-11-18 14:40:02405 component_loader_.reset(
406 new extensions::ComponentLoader(this,
407 profile->GetPrefs(),
408 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14409
[email protected]af9db5f2011-10-05 05:13:15410 app_notification_manager_->Init();
411
[email protected]0436b102011-04-15 18:30:03412 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25413 CHECK(!ProfileManager::IsImportProcess(*command_line));
414 extensions::ExternalProviderImpl::CreateExternalProviders(
415 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05416 }
[email protected]b671760b2010-07-15 21:13:47417
[email protected]fb82dcd2012-03-21 14:15:46418 // Set this as the ExtensionService for extension sorting to ensure it
419 // cause syncs if required.
420 extension_prefs_->extension_sorting()->SetExtensionService(this);
421
[email protected]9ce11d22012-08-08 23:20:13422#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58423 extension_action_storage_manager_.reset(
424 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13425#endif
[email protected]c77f2352012-08-08 22:07:58426
[email protected]1ff4d282012-12-08 00:39:21427 // Before loading any extensions, determine whether Sideload Wipeout is on.
428 wipeout_is_active_ = FeatureSwitch::sideload_wipeout()->IsEnabled() &&
429 !extension_prefs_->GetSideloadWipeoutDone();
430
[email protected]cb0e50312011-05-09 15:03:07431 // How long is the path to the Extensions directory?
432 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
433 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25434}
435
[email protected]84df8332011-12-06 18:22:46436const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45437 return &extensions_;
438}
439
[email protected]84df8332011-12-06 18:22:46440const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45441 return &disabled_extensions_;
442}
443
[email protected]84df8332011-12-06 18:22:46444const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20445 return &terminated_extensions_;
446}
447
[email protected]695b5712012-12-06 23:55:28448const ExtensionSet* ExtensionService::blacklisted_extensions() const {
449 return &blacklisted_extensions_;
450}
451
452scoped_ptr<const ExtensionSet>
453 ExtensionService::GenerateInstalledExtensionsSet() const {
454 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01455 installed_extensions->InsertAll(extensions_);
456 installed_extensions->InsertAll(disabled_extensions_);
457 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28458 installed_extensions->InsertAll(blacklisted_extensions_);
459 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01460}
461
[email protected]695b5712012-12-06 23:55:28462scoped_ptr<const ExtensionSet> ExtensionService::GetWipedOutExtensions() const {
463 scoped_ptr<ExtensionSet> extension_set(new ExtensionSet());
[email protected]215a7be2012-10-22 19:53:42464 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
465 iter != disabled_extensions_.end(); ++iter) {
466 int disabled_reason = extension_prefs_->GetDisableReasons((*iter)->id());
467 if ((disabled_reason & Extension::DISABLE_SIDELOAD_WIPEOUT) != 0)
468 extension_set->Insert(*iter);
469 }
[email protected]695b5712012-12-06 23:55:28470 return extension_set.PassAs<const ExtensionSet>();
[email protected]215a7be2012-10-22 19:53:42471}
472
[email protected]3f213ad2012-07-26 23:39:41473extensions::PendingExtensionManager*
474 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37475 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45476}
477
[email protected]eaa7dd182010-12-14 11:09:00478ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17479 // No need to unload extensions here because they are profile-scoped, and the
480 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17481
[email protected]5df038b2012-07-16 19:03:27482 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14483 for (i = external_extension_providers_.begin();
484 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27485 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14486 provider->ServiceShutdown();
487 }
[email protected]6014d672008-12-05 00:38:25488}
489
[email protected]d91268022011-08-26 13:17:37490void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39491 RegisterForImportFinished();
492}
493
494void ExtensionService::RegisterForImportFinished() {
495 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
496 content::Source<Profile>(profile_))) {
497 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
498 content::Source<Profile>(profile_));
499 }
500}
501
502void ExtensionService::InitAfterImport() {
[email protected]eac88332012-12-26 17:57:45503 component_loader_->LoadAll();
[email protected]ae4c37e2012-12-21 01:16:25504
[email protected]cf593af2011-12-30 05:44:39505 CheckForExternalUpdates();
506
507 GarbageCollectExtensions();
508
509 // Idempotent, so although there is a possible race if the import
510 // process finished sometime in the middle of ProfileImpl::InitExtensions,
511 // it cannot happen twice.
512 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37513}
514
[email protected]eaa7dd182010-12-14 11:09:00515void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18516 if (event_routers_initialized_)
517 return;
518
[email protected]6a3cd37e2012-04-17 17:13:34519#if defined(ENABLE_EXTENSIONS)
[email protected]5a38dfd2012-07-23 23:22:10520 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]6a3cd37e2012-04-17 17:13:34521#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18522 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37523}
524
[email protected]037228a2012-10-05 01:36:16525void ExtensionService::Shutdown() {
526 // Do nothing for now.
527}
528
[email protected]b2907fd2011-03-25 16:43:37529const Extension* ExtensionService::GetExtensionById(
530 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47531 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28532 if (include_disabled) {
533 // Include blacklisted extensions here because there are hundreds of
534 // callers of this function, and many might assume that this includes those
535 // that have been disabled due to blacklisting.
536 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
537 }
538 return GetExtensionById(id, include_mask);
539}
540
[email protected]399583b2012-12-11 09:33:42541GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
542 return content::SiteInstance::GetSiteForURL(
543 profile_,
544 Extension::GetBaseURLFromExtensionId(extension_id));
545}
546
[email protected]695b5712012-12-06 23:55:28547const Extension* ExtensionService::GetExtensionById(
548 const std::string& id, int include_mask) const {
549 std::string lowercase_id = StringToLowerASCII(id);
550 if (include_mask & INCLUDE_ENABLED) {
551 const Extension* extension = extensions_.GetByID(lowercase_id);
552 if (extension)
553 return extension;
554 }
555 if (include_mask & INCLUDE_DISABLED) {
556 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
557 if (extension)
558 return extension;
559 }
560 if (include_mask & INCLUDE_TERMINATED) {
561 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
562 if (extension)
563 return extension;
564 }
565 if (include_mask & INCLUDE_BLACKLISTED) {
566 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
567 if (extension)
568 return extension;
569 }
570 return NULL;
[email protected]78994ab02010-12-08 18:06:44571}
572
[email protected]eaa7dd182010-12-14 11:09:00573void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12574 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20575
[email protected]fa6a9102010-11-22 15:38:50576 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17577 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32578
[email protected]ae4c37e2012-12-21 01:16:25579 CHECK(!ProfileManager::IsImportProcess(*CommandLine::ForCurrentProcess()));
580
[email protected]399583b2012-12-11 09:33:42581 // TODO(mek): It might be cleaner to do the FinishDelayedInstallInfo stuff
582 // here instead of in installedloader.
[email protected]ae4c37e2012-12-21 01:16:25583 if (g_browser_process->profile_manager() &&
584 g_browser_process->profile_manager()->will_import()) {
[email protected]eac88332012-12-26 17:57:45585 // Do not load any component extensions, since they may conflict with the
586 // import process.
[email protected]ae4c37e2012-12-21 01:16:25587
[email protected]ae4c37e2012-12-21 01:16:25588 extensions::InstalledLoader(this).LoadAllExtensions();
589 RegisterForImportFinished();
590 } else {
[email protected]eac88332012-12-26 17:57:45591 component_loader_->LoadAll();
[email protected]ae4c37e2012-12-21 01:16:25592 extensions::InstalledLoader(this).LoadAllExtensions();
593
594 // TODO(erikkay) this should probably be deferred to a future point
595 // rather than running immediately at startup.
596 CheckForExternalUpdates();
597
598 // TODO(erikkay) this should probably be deferred as well.
599 GarbageCollectExtensions();
600 }
[email protected]894bb502009-05-21 22:39:57601
[email protected]215a7be2012-10-22 19:53:42602 // The Sideload Wipeout effort takes place during load (see above), so once
603 // that is done the flag can be set so that we don't have to check again.
[email protected]1ff4d282012-12-08 00:39:21604 if (wipeout_is_active_) {
[email protected]215a7be2012-10-22 19:53:42605 extension_prefs_->SetSideloadWipeoutDone();
[email protected]1ff4d282012-12-08 00:39:21606 wipeout_is_active_ = false; // Wipeout is only on during load.
[email protected]3a2748c42013-01-15 10:13:23607 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.SideloadWipeoutNeeded",
608 wipeout_count_ > 0u);
[email protected]1ff4d282012-12-08 00:39:21609 }
[email protected]215a7be2012-10-22 19:53:42610
[email protected]399583b2012-12-11 09:33:42611 if (extension_prefs_->NeedsStorageGarbageCollection()) {
612 GarbageCollectIsolatedStorage();
613 extension_prefs_->SetNeedsStorageGarbageCollection(false);
614 }
[email protected]6014d672008-12-05 00:38:25615}
616
[email protected]31bb5ee62012-09-12 22:58:40617bool ExtensionService::UpdateExtension(const std::string& id,
618 const FilePath& extension_path,
619 const GURL& download_url,
620 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12621 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54622 if (browser_terminating_) {
623 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
624 // Leak the temp file at extension_path. We don't want to add to the disk
625 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
626 // the file is in the OS temp directory which should be cleaned up for us.
627 return false;
628 }
[email protected]a8af9fdb2010-10-28 21:52:20629
[email protected]3f213ad2012-07-26 23:39:41630 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06631 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32632
[email protected]695b5712012-12-06 23:55:28633 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06634 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33635 LOG(WARNING) << "Will not update extension " << id
636 << " because it is not installed or pending";
637 // Delete extension_path since we're not creating a CrxInstaller
638 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14639 if (!GetFileTaskRunner()->PostTask(
640 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36641 base::Bind(
[email protected]7296f2762011-11-21 19:23:44642 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36643 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03644
645 return false;
[email protected]e957fe52009-06-23 16:51:05646 }
647
[email protected]aa142702010-03-26 01:26:33648 // We want a silent install only for non-pending extensions and
649 // pending extensions that have install_silently set.
[email protected]91e51d612012-10-21 23:03:05650 ExtensionInstallPrompt* client = NULL;
651 if (pending_extension_info && !pending_extension_info->install_silently())
652 client = ExtensionInstallUI::CreateInstallPromptWithProfile(profile_);
[email protected]aa142702010-03-26 01:26:33653
[email protected]d8c8f25f2011-11-02 18:18:01654 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16655 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06656 if (pending_extension_info) {
657 installer->set_install_source(pending_extension_info->install_source());
658 if (pending_extension_info->install_silently())
659 installer->set_allow_silent_install(true);
660 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41661 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06662 }
[email protected]7d8b7072012-04-07 01:07:46663 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45664 // its auto-update URL is from the webstore, treat it as a webstore install.
665 // Note that we ignore some older extensions with blank auto-update URLs
666 // because we are mostly concerned with restrictions on NaCl extensions,
667 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53668 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28669 if ((extension && extension->from_webstore()) ||
[email protected]7d8b7072012-04-07 01:07:46670 (extension && extension->UpdatesFromGallery()) ||
[email protected]963d13c2012-09-29 17:13:35671 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06672 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53673 creation_flags |= Extension::FROM_WEBSTORE;
674 }
675
676 // Bookmark apps being updated is kind of a contradiction, but that's because
677 // we mark the default apps as bookmark apps, and they're hosted in the web
678 // store, thus they can get updated. See http://crbug.com/101605 for more
679 // details.
680 if (extension && extension->from_bookmark())
681 creation_flags |= Extension::FROM_BOOKMARK;
682
[email protected]e33bbc22012-08-27 22:05:46683 if (extension && extension->was_installed_by_default())
684 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
685
[email protected]a12ce8b22012-01-17 18:40:53686 installer->set_creation_flags(creation_flags);
687
[email protected]6dfbbf82010-03-12 23:09:16688 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06689 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07690 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16691 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03692
693 if (out_crx_installer)
694 *out_crx_installer = installer;
695
696 return true;
[email protected]e957fe52009-06-23 16:51:05697}
698
[email protected]eaa7dd182010-12-14 11:09:00699void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]32b36c62012-10-24 05:37:41700 int events = HasShellWindows(extension_id) ? EVENT_LAUNCHED : EVENT_NONE;
701 ReloadExtensionWithEvents(extension_id, events);
702}
703
704void ExtensionService::RestartExtension(const std::string& extension_id) {
[email protected]2380c6b2013-01-09 02:33:13705 ReloadExtensionWithEvents(extension_id, EVENT_RESTARTED);
[email protected]32b36c62012-10-24 05:37:41706}
707
708void ExtensionService::ReloadExtensionWithEvents(
709 const std::string& extension_id,
710 int events) {
[email protected]a29a517a2011-01-21 21:11:12711 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06712 FilePath path;
[email protected]9adb9692010-10-29 23:14:02713 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40714
[email protected]f17dbd42010-08-16 23:21:10715 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06716 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29717 // If the extension has an inspector open for its background page, detach
718 // the inspector and hang onto a cookie for it, so that we can reattach
719 // later.
[email protected]31d8f5f22012-04-02 15:22:08720 // TODO(yoz): this is not incognito-safe!
721 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13722 extensions::ExtensionHost* host =
723 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36724 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29725 // Look for an open inspector for the background page.
[email protected]98f66112012-12-25 12:59:36726 int devtools_cookie = DevToolsAgentHost::DisconnectRenderViewHost(
[email protected]562e20c2012-12-24 18:27:20727 host->render_view_host());
[email protected]4814b512009-11-07 00:12:29728 if (devtools_cookie >= 0)
729 orphaned_dev_tools_[extension_id] = devtools_cookie;
730 }
731
[email protected]32b36c62012-10-24 05:37:41732 on_load_events_[extension_id] = events;
[email protected]6cafe35a2012-08-27 22:40:50733
[email protected]b65272f2009-08-31 15:47:06734 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03735 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]f17dbd42010-08-16 23:21:10736 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16737 } else {
738 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06739 }
740
[email protected]399583b2012-12-11 09:33:42741 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48742 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05743 return;
744 }
745
[email protected]43ceb002012-02-10 23:19:15746 // If we're reloading a component extension, use the component extension
747 // loader's reloader.
748 if (component_loader_->Exists(extension_id)) {
749 component_loader_->Reload(extension_id);
750 return;
751 }
752
[email protected]e6090e42010-03-23 22:44:08753 // Check the installed extensions to see if what we're reloading was already
754 // installed.
755 scoped_ptr<ExtensionInfo> installed_extension(
756 extension_prefs_->GetInstalledExtensionInfo(extension_id));
757 if (installed_extension.get() &&
758 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01759 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08760 } else {
[email protected]d8c8f25f2011-11-02 18:18:01761 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08762 // We should always be able to remember the extension's path. If it's not in
763 // the map, someone failed to update |unloaded_extension_paths_|.
764 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01765 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08766 }
[email protected]9cddd4702009-07-27 22:09:40767}
768
[email protected]fa2416f2011-05-03 08:41:20769bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08770 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20771 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14772 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12773 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20774
[email protected]0d6ec3a72011-09-02 02:09:43775 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25776
[email protected]e7afe2452010-08-22 16:19:13777 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48778 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32779
[email protected]95da88c42011-03-31 10:07:33780 // Policy change which triggers an uninstall will always set
781 // |external_uninstall| to true so this is the only way to uninstall
782 // managed extensions.
[email protected]65187152012-06-02 13:14:14783 if (!external_uninstall &&
784 !system_->management_policy()->UserMayModifySettings(
785 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07786 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27787 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53788 content::Source<Profile>(profile_),
789 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33790 return false;
791 }
[email protected]95da88c42011-03-31 10:07:33792
[email protected]3bdba0d2011-08-23 07:17:30793 // Extract the data we need for sync now, but don't actually sync until we've
794 // completed the uninstallation.
[email protected]65f173552012-06-28 22:43:58795 syncer::SyncChange sync_change;
[email protected]5db9ada2012-04-11 13:48:20796 if (app_sync_bundle_.HandlesApp(*extension)) {
797 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension);
798 } else if (extension_sync_bundle_.HandlesExtension(*extension)) {
799 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
[email protected]3bdba0d2011-08-23 07:17:30800 }
801
[email protected]d96eb512012-11-01 23:44:08802 if (IsUnacknowledgedExternalExtension(extension)) {
803 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
804 EXTERNAL_EXTENSION_UNINSTALLED,
805 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
806 }
[email protected]9b217652010-10-08 22:04:23807 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08808 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49809 RecordPermissionMessagesHistogram(
810 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23811
[email protected]831aa212010-03-26 13:55:19812 // Unload before doing more cleanup to ensure that nothing is hanging on to
813 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59814 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19815
[email protected]0d6ec3a72011-09-02 02:09:43816 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19817 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32818
819 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43820 if (Extension::LOAD != extension->location()) {
[email protected]7f8f24f2012-11-15 19:40:14821 if (!GetFileTaskRunner()->PostTask(
822 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36823 base::Bind(
[email protected]14908b72011-04-20 06:54:36824 &extension_file_util::UninstallExtension,
825 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20826 extension_id)))
[email protected]14908b72011-04-20 06:54:36827 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32828 }
829
[email protected]0d6ec3a72011-09-02 02:09:43830 GURL launch_web_url_origin(extension->launch_web_url());
831 launch_web_url_origin = launch_web_url_origin.GetOrigin();
[email protected]605fb8102012-05-04 01:36:55832 bool is_storage_isolated = extension->is_storage_isolated();
[email protected]0d6ec3a72011-09-02 02:09:43833
[email protected]399583b2012-12-11 09:33:42834 if (is_storage_isolated) {
835 BrowserContext::AsyncObliterateStoragePartition(
836 profile_,
837 GetSiteForExtensionId(extension_id),
838 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
839 AsWeakPtr()));
840 } else {
841 if (extension->is_hosted_app() &&
842 !profile_->GetExtensionSpecialStoragePolicy()->
843 IsStorageProtected(launch_web_url_origin)) {
844 extensions::DataDeleter::StartDeleting(
845 profile_, extension_id, launch_web_url_origin);
846 }
847 extensions::DataDeleter::StartDeleting(profile_, extension_id,
848 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43849 }
[email protected]0d6ec3a72011-09-02 02:09:43850
[email protected]0dfe05c2011-02-23 23:03:36851 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06852
853 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07854 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27855 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53856 content::Source<Profile>(profile_),
[email protected]d67d9412012-07-17 17:19:42857 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33858
[email protected]5db9ada2012-04-11 13:48:20859 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32860 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20861 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
862 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32863 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20864 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30865 }
866
[email protected]399583b2012-12-11 09:33:42867 delayed_updates_for_idle_.Remove(extension_id);
868 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05869
[email protected]333b1de2011-09-12 18:28:50870 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18871 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
872
[email protected]d6ebc9792011-04-07 18:18:33873 return true;
[email protected]c10da4b02010-03-25 14:38:32874}
875
[email protected]c3cfb012011-04-06 22:07:35876bool ExtensionService::IsExtensionEnabled(
877 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46878 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21879 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18880 return true;
[email protected]dc9a74f72012-08-17 18:07:21881 }
[email protected]36429da2011-07-11 20:25:18882
[email protected]695b5712012-12-06 23:55:28883 if (disabled_extensions_.Contains(extension_id) ||
884 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25885 return false;
[email protected]695b5712012-12-06 23:55:28886 }
[email protected]ad83ca242011-07-29 01:32:25887
888 // If the extension hasn't been loaded yet, check the prefs for it. Assume
889 // enabled unless otherwise noted.
890 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31891 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35892}
893
894bool ExtensionService::IsExternalExtensionUninstalled(
895 const std::string& extension_id) const {
896 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
897}
898
[email protected]e2377032013-01-18 17:53:22899bool ExtensionService::IsExtensionEnabledForLauncher(
900 const std::string& extension_id) const {
901 return IsExtensionEnabled(extension_id) &&
902 !GetTerminatedExtension(extension_id);
903}
904
[email protected]eaa7dd182010-12-14 11:09:00905void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12906 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20907
[email protected]06f92562011-04-29 19:27:31908 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39909 return;
[email protected]0c6da502009-08-14 22:32:39910
[email protected]c0231d72012-11-26 22:37:57911 int disable_reasons = extension_prefs_->GetDisableReasons(extension_id);
[email protected]b3317ad2011-04-28 23:46:00912 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28913 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52914
[email protected]695b5712012-12-06 23:55:28915 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31916 // This can happen if sync enables an extension that is not
917 // installed yet.
918 if (!extension)
919 return;
920
[email protected]d96eb512012-11-01 23:44:08921 if (IsUnacknowledgedExternalExtension(extension)) {
922 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
923 EXTERNAL_EXTENSION_REENABLED,
924 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]612a1cb12012-10-17 13:18:03925 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08926 }
[email protected]612a1cb12012-10-17 13:18:03927
[email protected]c0231d72012-11-26 22:37:57928 if (disable_reasons & Extension::DISABLE_SIDELOAD_WIPEOUT)
[email protected]94e14592012-11-19 10:49:42929 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.ExtensionWipedStatus", false);
930
[email protected]0c6da502009-08-14 22:32:39931 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46932 extensions_.Insert(make_scoped_refptr(extension));
933 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39934
[email protected]62d30f42009-10-01 22:36:06935 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30936
[email protected]f74d7f32012-06-19 19:22:51937 // Notify listeners that the extension was enabled.
938 content::NotificationService::current()->Notify(
939 chrome::NOTIFICATION_EXTENSION_ENABLED,
940 content::Source<Profile>(profile_),
941 content::Details<const Extension>(extension));
942
[email protected]3bdba0d2011-08-23 07:17:30943 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39944}
945
[email protected]44d62b62012-04-11 00:06:03946void ExtensionService::DisableExtension(
947 const std::string& extension_id,
948 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12949 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20950
[email protected]b2ba9962009-12-10 20:10:15951 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31952 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52953 return;
[email protected]1784e83a2009-09-08 21:01:52954
[email protected]06f92562011-04-29 19:27:31955 const Extension* extension = GetInstalledExtension(extension_id);
956 // |extension| can be NULL if sync disables an extension that is not
957 // installed yet.
[email protected]65187152012-06-02 13:14:14958 if (extension &&
959 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33960 return;
[email protected]65187152012-06-02 13:14:14961 }
[email protected]95da88c42011-03-31 10:07:33962
[email protected]b3317ad2011-04-28 23:46:00963 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28964 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52965
[email protected]695b5712012-12-06 23:55:28966 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
967 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31968 if (!extension)
969 return;
970
[email protected]5c094792012-09-07 19:44:53971 // Move it over to the disabled list. Don't send a second unload notification
972 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:46973 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53974 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46975 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53976 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
977 } else {
[email protected]84df8332011-12-06 18:22:46978 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53979 }
[email protected]3bdba0d2011-08-23 07:17:30980
981 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:52982}
983
[email protected]eaa7dd182010-12-14 11:09:00984void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]b70a2d92012-06-28 19:51:21985 const Extension* extension, bool record_oauth2_grant) {
[email protected]be083862012-09-01 03:53:45986 GrantPermissions(extension, record_oauth2_grant);
[email protected]fe2dd7742011-04-19 22:52:49987 RecordPermissionMessagesHistogram(
988 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25989 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
990 EnableExtension(extension->id());
991}
992
[email protected]be083862012-09-01 03:53:45993void ExtensionService::GrantPermissions(const Extension* extension,
994 bool record_oauth2_grant) {
995 CHECK(extension);
996 extensions::PermissionsUpdater perms_updater(profile());
997 perms_updater.GrantActivePermissions(extension, record_oauth2_grant);
998}
999
[email protected]fe2dd7742011-04-19 22:52:491000// static
1001void ExtensionService::RecordPermissionMessagesHistogram(
1002 const Extension* e, const char* histogram) {
[email protected]de415552013-01-23 04:12:171003 // Since this is called from multiple sources, and since the histogram macros
1004 // use statics, we need to manually lookup the histogram ourselves.
1005 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491006 histogram,
1007 1,
[email protected]c2e66e12012-06-27 06:27:061008 PermissionMessage::kEnumBoundary,
1009 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171010 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491011
[email protected]c2e66e12012-06-27 06:27:061012 PermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:491013 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061014 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491015 } else {
[email protected]c2e66e12012-06-27 06:27:061016 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521017 it != permissions.end(); ++it)
1018 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491019 }
1020}
1021
[email protected]eaa7dd182010-12-14 11:09:001022void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421023 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061024 // was loaded, otherwise a race can arise where a renderer that is created
1025 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421026 // that the request context doesn't yet know about. The profile is responsible
1027 // for ensuring its URLRequestContexts appropriately discover the loaded
1028 // extension.
[email protected]31d8f5f22012-04-02 15:22:081029 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061030
[email protected]d5949312012-12-03 22:13:301031 // Tell renderers about the new extension, unless it's a theme (renderers
1032 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191033 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491034 for (content::RenderProcessHost::iterator i(
1035 content::RenderProcessHost::AllHostsIterator());
1036 !i.IsAtEnd(); i.Advance()) {
1037 content::RenderProcessHost* host = i.GetCurrentValue();
1038 Profile* host_profile =
1039 Profile::FromBrowserContext(host->GetBrowserContext());
1040 if (host_profile->GetOriginalProfile() ==
1041 profile_->GetOriginalProfile()) {
1042 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1043 1, ExtensionMsg_Loaded_Params(extension));
1044 host->Send(
1045 new ExtensionMsg_Loaded(loaded_extensions));
1046 }
[email protected]c8d407e2011-04-28 21:27:171047 }
[email protected]77a6970c2011-04-23 16:58:561048 }
1049
[email protected]3442a662012-01-12 08:06:171050 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1051 // extension.
1052 //
1053 // NOTE: It is important that this happen after notifying the renderers about
1054 // the new extensions so that if we navigate to an extension URL in
1055 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1056 content::NotificationService::current()->Notify(
1057 chrome::NOTIFICATION_EXTENSION_LOADED,
1058 content::Source<Profile>(profile_),
1059 content::Details<const Extension>(extension));
1060
[email protected]c8d407e2011-04-28 21:27:171061 // Tell a random-ass collection of other subsystems about the new extension.
1062 // TODO(aa): What should we do with all this goop? Can it move into the
1063 // relevant objects via EXTENSION_LOADED?
1064
1065 profile_->GetExtensionSpecialStoragePolicy()->
1066 GrantRightsForExtension(extension);
1067
1068 UpdateActiveExtensionsInCrashReporter();
1069
[email protected]c8d407e2011-04-28 21:27:171070 // If the extension has permission to load chrome://favicon/ resources we need
1071 // to make sure that the FaviconSource is registered with the
1072 // ChromeURLDataManager.
1073 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1074 FaviconSource* favicon_source = new FaviconSource(profile_,
1075 FaviconSource::FAVICON);
[email protected]ef92e172012-04-25 19:40:411076 ChromeURLDataManager::AddDataSource(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171077 }
[email protected]b07e606e2012-09-15 20:16:151078
1079#if !defined(OS_ANDROID)
1080 // Same for chrome://theme/ resources.
1081 if (extension->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
1082 ThemeSource* theme_source = new ThemeSource(profile_);
1083 ChromeURLDataManager::AddDataSource(profile_, theme_source);
1084 }
1085#endif
1086
[email protected]5eddc3e2011-10-26 04:33:311087 // Same for chrome://thumb/ resources.
1088 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
1089 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]92253622013-01-14 20:40:501090 ChromeURLDataManager::AddDataSource(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311091 }
[email protected]c8d407e2011-04-28 21:27:171092
[email protected]ebd71962012-12-20 02:56:551093#if defined(ENABLE_PLUGINS)
[email protected]c8d407e2011-04-28 21:27:171094 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581095 bool plugins_changed = false;
1096 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1097 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141098 PluginService::GetInstance()->RefreshPlugins();
1099 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581100 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491101 ChromePluginServiceFilter* filter =
1102 ChromePluginServiceFilter::GetInstance();
1103 if (plugin.is_public) {
1104 filter->RestrictPluginToProfileAndOrigin(
1105 plugin.path, profile_, GURL());
1106 } else {
1107 filter->RestrictPluginToProfileAndOrigin(
1108 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581109 }
1110 }
[email protected]84396dbc2011-04-14 06:33:421111
1112 bool nacl_modules_changed = false;
1113 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1114 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001115 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421116 nacl_modules_changed = true;
1117 }
1118
[email protected]ed0ba002011-05-26 16:55:131119 if (nacl_modules_changed)
1120 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421121
1122 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021123 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551124#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061125}
1126
[email protected]a9f39a312010-12-23 22:14:271127void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591128 const Extension* extension,
1129 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271130 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071131 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271132 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531133 content::Source<Profile>(profile_),
1134 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061135
[email protected]903d70a2012-12-04 13:00:171136#if defined(ENABLE_THEMES)
1137 // If the current theme is being unloaded, tell ThemeService to revert back
1138 // to the default theme.
1139 if (reason != extension_misc::UNLOAD_REASON_UPDATE && extension->is_theme()) {
1140 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
1141 if (extension->id() == theme_service->GetThemeID())
1142 theme_service->UseDefaultTheme();
1143 }
1144#endif
1145
[email protected]f3b1a082011-11-18 00:34:301146 for (content::RenderProcessHost::iterator i(
1147 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561148 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301149 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081150 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301151 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081152 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171153 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561154 }
1155
[email protected]31d8f5f22012-04-02 15:22:081156 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171157 profile_->GetExtensionSpecialStoragePolicy()->
1158 RevokeRightsForExtension(extension);
1159
[email protected]b777b332011-04-16 04:01:081160#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231161 // Revoke external file access for the extension from its file system context.
1162 // It is safe to access the extension's storage partition at this point. The
1163 // storage partition may get destroyed only after the extension gets unloaded.
1164 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1165 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091166 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231167 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091168 GetFileSystemContext();
1169 if (filesystem_context && filesystem_context->external_provider()) {
1170 filesystem_context->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171171 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061172 }
[email protected]c8d407e2011-04-28 21:27:171173#endif
1174
1175 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581176
[email protected]ebd71962012-12-20 02:56:551177#if defined(ENABLE_PLUGINS)
[email protected]2de307592011-04-05 21:16:581178 bool plugins_changed = false;
1179 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1180 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]b6a2f8de2012-01-31 17:28:491181 PluginService::GetInstance()->ForcePluginShutdown(plugin.path);
[email protected]f520b5b2011-11-08 02:42:141182 PluginService::GetInstance()->RefreshPlugins();
1183 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581184 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491185 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581186 }
[email protected]84396dbc2011-04-14 06:33:421187
1188 bool nacl_modules_changed = false;
1189 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1190 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001191 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421192 nacl_modules_changed = true;
1193 }
1194
[email protected]ed0ba002011-05-26 16:55:131195 if (nacl_modules_changed)
1196 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421197
1198 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021199 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551200#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061201}
1202
[email protected]0a071a32011-02-08 00:18:241203Profile* ExtensionService::profile() {
1204 return profile_;
1205}
1206
[email protected]45759612012-07-10 17:21:231207extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451208 return extension_prefs_;
1209}
1210
[email protected]0d9a2202011-11-09 13:48:411211extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381212 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201213}
1214
[email protected]e2d720aa2012-05-17 00:11:351215extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441216 return extension_prefs()->content_settings_store();
1217}
1218
[email protected]25ae0152011-11-18 14:40:021219bool ExtensionService::is_ready() {
1220 return ready_;
1221}
1222
[email protected]7f8f24f2012-11-15 19:40:141223base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
1224 if (file_task_runner_)
1225 return file_task_runner_;
1226
1227 // We should be able to interrupt any part of extension install process during
1228 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1229 // will be ignored/deleted while we will block on started tasks.
1230 std::string token("ext_install-");
1231 token.append(profile_->GetPath().AsUTF8Unsafe());
1232 file_task_runner_ = BrowserThread::GetBlockingPool()->
1233 GetSequencedTaskRunnerWithShutdownBehavior(
1234 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1235 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
1236 return file_task_runner_;
1237}
1238
[email protected]42a08162012-03-16 18:09:111239extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061240 return updater_.get();
1241}
1242
[email protected]4ee07c62012-08-21 12:40:421243void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251244 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281245
[email protected]aa96d3a2010-08-21 08:45:251246 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461247 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251248 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021249 const Extension* extension = (*iter);
[email protected]4ee07c62012-08-21 12:40:421250 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251251 to_be_removed.push_back(extension->id());
1252 }
1253
1254 // UnloadExtension will change the extensions_ list. So, we should
1255 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401256 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591257 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251258}
1259
[email protected]31206602011-04-13 23:07:321260void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351261 if (updater()) {
[email protected]94fde232012-04-27 10:22:301262 if (AreAllExternalProvidersReady()) {
1263 updater()->CheckSoon();
1264 } else {
1265 // Sync can start updating before all the external providers are ready
1266 // during startup. Start the update as soon as those providers are ready,
1267 // but not before.
1268 update_once_all_providers_are_ready_ = true;
1269 }
[email protected]c3cfb012011-04-06 22:07:351270 } else {
[email protected]31206602011-04-13 23:07:321271 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351272 }
1273}
1274
[email protected]3f98fd92012-11-17 15:54:291275syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321276 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581277 const syncer::SyncDataList& initial_sync_data,
1278 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1279 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201280 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281281 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301282
1283 switch (type) {
[email protected]a4a147652012-07-03 23:41:321284 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201285 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281286 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201287 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301288 break;
1289
[email protected]a4a147652012-07-03 23:41:321290 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281291 app_sync_bundle_.SetupSync(sync_processor.release(),
1292 sync_error_factory.release(),
1293 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301294 break;
1295
1296 default:
1297 LOG(FATAL) << "Got " << type << " ModelType";
1298 }
[email protected]3bdba0d2011-08-23 07:17:301299
[email protected]b1417ab2011-12-15 17:09:111300 // Process local extensions.
1301 // TODO(yoz): Determine whether pending extensions should be considered too.
1302 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581303 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1304 syncer::SyncChangeList sync_change_list;
1305 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301306 i != sync_data_list.end();
1307 ++i) {
[email protected]5db9ada2012-04-11 13:48:201308 switch (type) {
[email protected]a4a147652012-07-03 23:41:321309 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201310 sync_change_list.push_back(
1311 extension_sync_bundle_.CreateSyncChange(*i));
1312 break;
[email protected]a4a147652012-07-03 23:41:321313 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201314 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1315 break;
1316 default:
1317 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401318 }
[email protected]3bdba0d2011-08-23 07:17:301319 }
[email protected]3bdba0d2011-08-23 07:17:301320
[email protected]5db9ada2012-04-11 13:48:201321
[email protected]a4a147652012-07-03 23:41:321322 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201323 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321324 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201325 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1326 }
[email protected]0ffd9e52012-01-26 21:44:341327
[email protected]3f98fd92012-11-17 15:54:291328 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301329}
1330
[email protected]a4a147652012-07-03 23:41:321331void ExtensionService::StopSyncing(syncer::ModelType type) {
1332 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201333 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321334 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201335 extension_sync_bundle_.Reset();
1336 }
[email protected]3bdba0d2011-08-23 07:17:301337}
1338
[email protected]65f173552012-06-28 22:43:581339syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321340 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401341 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201342 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401343 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201344 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201345
1346 // We should only get sync data for extensions and apps.
1347 NOTREACHED();
1348
[email protected]65f173552012-06-28 22:43:581349 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301350}
1351
[email protected]65f173552012-06-28 22:43:581352syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301353 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581354 const syncer::SyncChangeList& change_list) {
1355 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301356 i != change_list.end();
1357 ++i) {
[email protected]a4a147652012-07-03 23:41:321358 syncer::ModelType type = i->sync_data().GetDataType();
1359 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201360 extension_sync_bundle_.ProcessSyncChange(
1361 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321362 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201363 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1364 }
[email protected]3bdba0d2011-08-23 07:17:301365 }
1366
[email protected]0ffd9e52012-01-26 21:44:341367 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1368
[email protected]65f173552012-06-28 22:43:581369 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561370}
1371
[email protected]5db9ada2012-04-11 13:48:201372extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1373 const Extension& extension) const {
1374 return extensions::ExtensionSyncData(extension,
1375 IsExtensionEnabled(extension.id()),
1376 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561377}
1378
[email protected]5db9ada2012-04-11 13:48:201379extensions::AppSyncData ExtensionService::GetAppSyncData(
1380 const Extension& extension) const {
1381 return extensions::AppSyncData(
1382 extension,
1383 IsExtensionEnabled(extension.id()),
1384 IsIncognitoEnabled(extension.id()),
1385 extension_prefs_->GetAppNotificationClientId(extension.id()),
1386 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1387 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1388 extension.id()),
1389 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1390}
[email protected]3bdba0d2011-08-23 07:17:301391
[email protected]5db9ada2012-04-11 13:48:201392std::vector<extensions::ExtensionSyncData>
1393 ExtensionService::GetExtensionSyncDataList() const {
1394 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1395 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1396 &extension_sync_list);
1397 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1398 &extension_sync_list);
1399 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1400 &extension_sync_list);
1401
1402 std::vector<extensions::ExtensionSyncData> pending_extensions =
1403 extension_sync_bundle_.GetPendingData();
1404 extension_sync_list.insert(extension_sync_list.begin(),
1405 pending_extensions.begin(),
1406 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301407
1408 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561409}
1410
[email protected]5db9ada2012-04-11 13:48:201411std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1412 const {
1413 std::vector<extensions::AppSyncData> app_sync_list;
1414 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1415 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1416 &app_sync_list);
1417 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1418 &app_sync_list);
1419
1420 std::vector<extensions::AppSyncData> pending_apps =
1421 app_sync_bundle_.GetPendingData();
1422 app_sync_list.insert(app_sync_list.begin(),
1423 pending_apps.begin(),
1424 pending_apps.end());
1425
1426 return app_sync_list;
1427}
1428
1429bool ExtensionService::ProcessExtensionSyncData(
1430 const extensions::ExtensionSyncData& extension_sync_data) {
1431 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321432 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201433 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1434 extension_sync_data);
1435 CheckForUpdatesSoon();
1436 return false;
1437 }
1438
1439 return true;
1440}
1441
1442bool ExtensionService::ProcessAppSyncData(
1443 const extensions::AppSyncData& app_sync_data) {
1444 const std::string& id = app_sync_data.id();
1445 const Extension* extension = GetInstalledExtension(id);
1446 bool extension_installed = (extension != NULL);
1447
1448 if (app_sync_data.app_launch_ordinal().IsValid() &&
1449 app_sync_data.page_ordinal().IsValid()) {
1450 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1451 id,
1452 app_sync_data.app_launch_ordinal());
1453 extension_prefs_->extension_sorting()->SetPageOrdinal(
1454 id,
1455 app_sync_data.page_ordinal());
1456 }
1457
1458 if (extension_installed) {
1459 if (app_sync_data.notifications_disabled() !=
1460 extension_prefs_->IsAppNotificationDisabled(id)) {
1461 extension_prefs_->SetAppNotificationDisabled(
1462 id, app_sync_data.notifications_disabled());
1463 }
1464 }
1465
1466 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321467 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201468 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1469 CheckForUpdatesSoon();
1470 return false;
1471 }
1472
1473 return true;
1474}
1475
1476bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321477 syncer::ModelType type) const {
1478 if (type == syncer::EXTENSIONS &&
[email protected]31bb5ee62012-09-12 22:58:401479 extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION) {
[email protected]5db9ada2012-04-11 13:48:201480 return true;
[email protected]31bb5ee62012-09-12 22:58:401481 }
[email protected]5db9ada2012-04-11 13:48:201482
[email protected]a4a147652012-07-03 23:41:321483 if (type == syncer::APPS &&
[email protected]31bb5ee62012-09-12 22:58:401484 extension.GetSyncType() == Extension::SYNC_TYPE_APP) {
[email protected]5db9ada2012-04-11 13:48:201485 return true;
[email protected]31bb5ee62012-09-12 22:58:401486 }
[email protected]5db9ada2012-04-11 13:48:201487
1488 return false;
1489}
1490
1491bool ExtensionService::ProcessExtensionSyncDataHelper(
1492 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321493 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301494 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061495 const Extension* extension = GetInstalledExtension(id);
1496
1497 // TODO(bolms): we should really handle this better. The particularly bad
1498 // case is where an app becomes an extension or vice versa, and we end up with
1499 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201500 if (extension && !IsCorrectSyncType(*extension, type))
1501 return true;
[email protected]90310d92011-04-17 07:35:041502
1503 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301504 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041505 std::string error;
1506 if (!UninstallExtensionHelper(this, id)) {
1507 LOG(WARNING) << "Could not uninstall extension " << id
1508 << " for sync";
1509 }
[email protected]5db9ada2012-04-11 13:48:201510 return true;
[email protected]90310d92011-04-17 07:35:041511 }
1512
[email protected]caf2d442012-05-04 10:00:501513 // Extension from sync was uninstalled by the user as external extensions.
1514 // Honor user choice and skip installation/enabling.
1515 if (IsExternalExtensionUninstalled(id)) {
1516 LOG(WARNING) << "Extension with id " << id
1517 << " from sync was uninstalled as external extension";
1518 return true;
1519 }
1520
[email protected]06f92562011-04-29 19:27:311521 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191522 // If the extension has been disabled from sync, it may not have
1523 // been installed yet, so we don't know if the disable reason was a
1524 // permissions increase. That will be updated once InitializePermissions
1525 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401526 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311527 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401528 else
[email protected]7e9e2422012-12-13 19:54:191529 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371530
[email protected]406b5a92011-11-08 11:58:261531 // We need to cache some version information here because setting the
1532 // incognito flag invalidates the |extension| pointer (it reloads the
1533 // extension).
[email protected]9060d8b02012-01-13 02:14:301534 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261535 int result = extension ?
1536 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1537 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1538 extension = NULL; // No longer safe to use.
1539
1540 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311541 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041542 if (result < 0) {
1543 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201544 return false;
[email protected]b020dc12011-12-06 04:35:421545 }
[email protected]06f92562011-04-29 19:27:311546 } else {
[email protected]06f92562011-04-29 19:27:311547 // TODO(akalin): Replace silent update with a list of enabled
1548 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331549 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201550
[email protected]a4a147652012-07-03 23:41:321551 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]5db9ada2012-04-11 13:48:201552 ExtensionFilter filter =
[email protected]a4a147652012-07-03 23:41:321553 (type == syncer::APPS) ? IsSyncableApp : IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201554
[email protected]06f92562011-04-29 19:27:311555 if (!pending_extension_manager()->AddFromSync(
1556 id,
[email protected]3bdba0d2011-08-23 07:17:301557 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201558 filter,
[email protected]6cc7dbae2011-04-29 21:18:331559 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311560 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301561 // This means that the extension is already pending installation, with a
1562 // non-INTERNAL location. Add to pending_sync_data, even though it will
1563 // never be removed (we'll never install a syncable version of the
1564 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311565 }
[email protected]3bdba0d2011-08-23 07:17:301566 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201567 return false;
[email protected]90310d92011-04-17 07:35:041568 }
[email protected]5db9ada2012-04-11 13:48:201569
1570 return true;
[email protected]90310d92011-04-17 07:35:041571}
1572
[email protected]c3cfb012011-04-06 22:07:351573bool ExtensionService::IsIncognitoEnabled(
1574 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481575 const Extension* extension = GetInstalledExtension(extension_id);
1576 if (extension && !extension->can_be_incognito_enabled())
1577 return false;
[email protected]c3cfb012011-04-06 22:07:351578 // If this is an existing component extension we always allow it to
1579 // work in incognito mode.
[email protected]c3cfb012011-04-06 22:07:351580 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411581 return true;
1582
1583 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351584 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501585}
[email protected]55a35692010-02-11 23:25:211586
[email protected]c3cfb012011-04-06 22:07:351587void ExtensionService::SetIsIncognitoEnabled(
1588 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371589 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481590 if (extension && !extension->can_be_incognito_enabled())
1591 return;
[email protected]c3cfb012011-04-06 22:07:351592 if (extension && extension->location() == Extension::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481593 // This shouldn't be called for component extensions unless they are
1594 // syncable.
1595 DCHECK(extension->IsSyncable());
1596
1597 // If we are here, make sure the we aren't trying to change the value.
1598 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1599
[email protected]c3cfb012011-04-06 22:07:351600 return;
1601 }
1602
[email protected]8c6c8c22011-03-09 12:52:341603 // Broadcast unloaded and loaded events to update browser state. Only bother
1604 // if the value changed and the extension is actually enabled, since there is
1605 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351606 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341607 if (enabled == old_enabled)
1608 return;
1609
[email protected]c3cfb012011-04-06 22:07:351610 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371611
[email protected]ffbf3f12011-12-09 22:10:331612 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341613
1614 // When we reload the extension the ID may be invalidated if we've passed it
1615 // by const ref everywhere. Make a copy to be safe.
1616 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261617 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331618 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301619
[email protected]406b5a92011-11-08 11:58:261620 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341621 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301622 if (extension)
1623 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211624}
1625
[email protected]aa7599d2011-10-28 07:24:321626void ExtensionService::SetAppNotificationSetupDone(
1627 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091628 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321629 const Extension* extension = GetInstalledExtension(extension_id);
1630 // This method is called when the user sets up app notifications.
1631 // So it is not expected to be called until the extension is installed.
1632 if (!extension) {
1633 NOTREACHED();
1634 return;
1635 }
[email protected]b2689a902011-12-01 00:41:091636 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321637 SyncExtensionChangeIfNeeded(*extension);
1638}
1639
1640void ExtensionService::SetAppNotificationDisabled(
1641 const std::string& extension_id,
1642 bool value) {
1643 const Extension* extension = GetInstalledExtension(extension_id);
1644 // This method is called when the user enables/disables app notifications.
1645 // So it is not expected to be called until the extension is installed.
1646 if (!extension) {
1647 NOTREACHED();
1648 return;
1649 }
[email protected]97e856b12011-12-14 00:21:131650 if (value)
1651 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1652 else
1653 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321654 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1655 SyncExtensionChangeIfNeeded(*extension);
1656}
1657
[email protected]dc9a74f72012-08-17 18:07:211658bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051659 // We allow the extension to see events and data from another profile iff it
1660 // uses "spanning" behavior and it has incognito access. "split" mode
1661 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221662 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351663 return IsIncognitoEnabled(extension->id()) &&
1664 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051665}
1666
[email protected]5ef7b002011-05-12 23:16:201667bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1668 if (extension->is_hosted_app())
1669 return true;
1670 // Packaged apps and regular extensions need to be enabled specifically for
1671 // incognito (and split mode should be set).
1672 return extension->incognito_split_mode() &&
1673 IsIncognitoEnabled(extension->id());
1674}
1675
[email protected]168389f2011-12-20 17:12:481676void ExtensionService::OnExtensionMoved(
1677 const std::string& moved_extension_id,
1678 const std::string& predecessor_extension_id,
1679 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231680 extension_prefs_->extension_sorting()->OnExtensionMoved(
1681 moved_extension_id,
1682 predecessor_extension_id,
1683 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481684
1685 const Extension* extension = GetInstalledExtension(moved_extension_id);
1686 if (extension)
1687 SyncExtensionChangeIfNeeded(*extension);
1688}
1689
[email protected]dc9a74f72012-08-17 18:07:211690bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081691 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441692 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081693 extension_prefs_->AllowFileAccess(extension->id()));
1694}
1695
[email protected]eaa7dd182010-12-14 11:09:001696void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481697 bool allow) {
1698 // Reload to update browser state. Only bother if the value changed and the
1699 // extension is actually enabled, since there is no UI otherwise.
1700 bool old_allow = AllowFileAccess(extension);
1701 if (allow == old_allow)
1702 return;
1703
[email protected]05c82182010-06-24 17:49:081704 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481705
[email protected]84df8332011-12-06 18:22:461706 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481707 if (extension_is_enabled)
1708 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081709}
1710
[email protected]8e4560b62011-01-14 10:09:141711// Some extensions will autoupdate themselves externally from Chrome. These
1712// are typically part of some larger client application package. To support
1713// these, the extension will register its location in the the preferences file
1714// (and also, on Windows, in the registry) and this code will periodically
1715// check that location for a .crx file, which it will then install locally if
1716// a new version is available.
1717// Errors are reported through ExtensionErrorReporter. Succcess is not
1718// reported.
[email protected]eaa7dd182010-12-14 11:09:001719void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121720 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141721
1722 // Note that this installation is intentionally silent (since it didn't
1723 // go through the front-end). Extensions that are registered in this
1724 // way are effectively considered 'pre-bundled', and so implicitly
1725 // trusted. In general, if something has HKLM or filesystem access,
1726 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121727
[email protected]8e4560b62011-01-14 10:09:141728 // Ask each external extension provider to give us a call back for each
1729 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271730 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141731 for (i = external_extension_providers_.begin();
1732 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271733 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141734 provider->VisitRegisteredExtension();
1735 }
1736
[email protected]50067e52011-10-20 23:17:071737 // Do any required work that we would have done after completion of all
1738 // providers.
1739 if (external_extension_providers_.empty()) {
1740 OnAllExternalProvidersReady();
1741 }
[email protected]9f1087e2009-06-15 17:29:321742}
1743
[email protected]50067e52011-10-20 23:17:071744void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271745 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121746 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071747 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121748
1749 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141750 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301751 if (AreAllExternalProvidersReady())
1752 OnAllExternalProvidersReady();
1753}
1754
1755bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271756 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141757 for (i = external_extension_providers_.begin();
1758 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301759 if (!i->get()->IsReady())
1760 return false;
[email protected]8e4560b62011-01-14 10:09:141761 }
[email protected]94fde232012-04-27 10:22:301762 return true;
[email protected]50067e52011-10-20 23:17:071763}
1764
1765void ExtensionService::OnAllExternalProvidersReady() {
1766 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301767 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1768 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071769
1770 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301771 if (update_once_all_providers_are_ready_ && updater()) {
1772 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061773 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141774 }
1775
1776 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231777 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141778 extension_prefs_->GetInstalledExtensionsInfo());
1779 for (size_t i = 0; i < extensions_info->size(); ++i) {
1780 ExtensionInfo* info = extensions_info->at(i).get();
1781 if (Extension::IsExternalLocation(info->extension_location))
1782 CheckExternalUninstall(info->extension_id);
1783 }
[email protected]e5af875f2011-10-10 21:09:141784 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141785}
1786
1787void ExtensionService::IdentifyAlertableExtensions() {
1788 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1789
[email protected]07c9f2f42012-02-29 18:45:221790 // Build up the lists of extensions that require acknowledgment. If this is
1791 // the first time, grandfather extensions that would have caused
1792 // notification.
[email protected]89226982012-07-16 20:09:181793 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141794
[email protected]a9aa5932012-01-25 08:27:401795 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181796 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]e5af875f2011-10-10 21:09:141797 if (extension_prefs_->SetAlertSystemFirstRun()) {
[email protected]a9aa5932012-01-25 08:27:401798 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181799 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141800 } else {
1801 // First run. Just acknowledge all the extensions, silently, by
1802 // shortcutting the display of the UI and going straight to the
1803 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401804 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141805 }
1806 }
[email protected]a9aa5932012-01-25 08:27:401807
[email protected]612a1cb12012-10-17 13:18:031808 UpdateExternalExtensionAlert();
1809
[email protected]a9aa5932012-01-25 08:27:401810 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181811 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141812}
1813
[email protected]89226982012-07-16 20:09:181814bool ExtensionService::PopulateExtensionErrorUI(
1815 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221816 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281817
1818 // Extensions that are blacklisted.
1819 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1820 it != blacklisted_extensions_.end(); ++it) {
1821 std::string id = (*it)->id();
1822 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1823 extension_error_ui->AddBlacklistedExtension(id);
1824 needs_alert = true;
1825 }
1826 }
1827
[email protected]07c9f2f42012-02-29 18:45:221828 for (ExtensionSet::const_iterator iter = extensions_.begin();
1829 iter != extensions_.end(); ++iter) {
1830 const Extension* e = *iter;
[email protected]695b5712012-12-06 23:55:281831
1832 // Extensions disabled by policy. Note: this no longer includes blacklisted
1833 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391834 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221835 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181836 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221837 needs_alert = true;
1838 }
1839 }
[email protected]07c9f2f42012-02-29 18:45:221840 }
[email protected]695b5712012-12-06 23:55:281841
[email protected]07c9f2f42012-02-29 18:45:221842 return needs_alert;
1843}
1844
[email protected]a9aa5932012-01-25 08:27:401845void ExtensionService::HandleExtensionAlertClosed() {
[email protected]89226982012-07-16 20:09:181846 extension_error_ui_.reset();
[email protected]62433d32011-10-12 22:33:121847}
1848
[email protected]a9aa5932012-01-25 08:27:401849void ExtensionService::HandleExtensionAlertAccept() {
[email protected]612a1cb12012-10-17 13:18:031850 const ExtensionIdSet* extension_ids =
1851 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141852 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1853 iter != extension_ids->end(); ++iter) {
1854 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1855 }
[email protected]e5af875f2011-10-10 21:09:141856}
1857
[email protected]47fc70c2011-12-06 07:29:511858void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1859 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031860 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511861}
1862
[email protected]d96eb512012-11-01 23:44:081863bool ExtensionService::IsUnacknowledgedExternalExtension(
1864 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321865 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1866 return false;
1867
[email protected]d96eb512012-11-01 23:44:081868 return (Extension::IsExternalLocation(extension->location()) &&
1869 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1870 !(extension_prefs_->GetDisableReasons(extension->id()) &
1871 Extension::DISABLE_SIDELOAD_WIPEOUT));
1872}
1873
[email protected]89226982012-07-16 20:09:181874void ExtensionService::HandleExtensionAlertDetails() {
1875 extension_error_ui_->ShowExtensions();
[email protected]6c751e72010-11-23 10:11:101876}
1877
[email protected]612a1cb12012-10-17 13:18:031878void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361879 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031880 return;
1881
1882 const Extension* extension = NULL;
1883 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1884 iter != disabled_extensions_.end(); ++iter) {
1885 const Extension* e = *iter;
[email protected]d96eb512012-11-01 23:44:081886 if (IsUnacknowledgedExternalExtension(e)) {
1887 extension = e;
1888 break;
[email protected]612a1cb12012-10-17 13:18:031889 }
1890 }
1891
1892 if (extension) {
[email protected]d96eb512012-11-01 23:44:081893 if (!extensions::HasExternalInstallError(this)) {
1894 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031895 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081896 // Stop prompting for this extension, and check if there's another
1897 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031898 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081899 UpdateExternalExtensionAlert();
1900 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1901 EXTERNAL_EXTENSION_IGNORED,
1902 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1903 return;
[email protected]612a1cb12012-10-17 13:18:031904 }
[email protected]d96eb512012-11-01 23:44:081905 extensions::AddExternalInstallError(this, extension);
[email protected]612a1cb12012-10-17 13:18:031906 }
1907 } else {
1908 extensions::RemoveExternalInstallError(this);
1909 }
[email protected]612a1cb12012-10-17 13:18:031910}
1911
[email protected]a9f39a312010-12-23 22:14:271912void ExtensionService::UnloadExtension(
1913 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591914 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091915 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281916 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021917 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281918 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251919
[email protected]fa0c96732010-11-17 00:14:231920 // This method can be called via PostTask, so the extension may have been
1921 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171922 if (!extension) {
1923 // In case the extension may have crashed/uninstalled. Allow the profile to
1924 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081925 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231926 return;
[email protected]dd163fb02011-05-04 22:22:171927 }
[email protected]0c6da502009-08-14 22:32:391928
[email protected]1eb175082010-02-10 09:26:161929 // Keep information about the extension so that we can reload it later
1930 // even if it's not permanently installed.
1931 unloaded_extension_paths_[extension->id()] = extension->path();
1932
[email protected]f17dbd42010-08-16 23:21:101933 // Clean up if the extension is meant to be enabled after a reload.
1934 disabled_extension_paths_.erase(extension->id());
1935
[email protected]d7e9a862010-11-03 21:57:491936 // Clean up runtime data.
1937 extension_runtime_data_.erase(extension_id);
1938
[email protected]060d4972012-07-19 17:22:391939 if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271940 UnloadedExtensionInfo details(extension, reason);
1941 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461942 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071943 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271944 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531945 content::Source<Profile>(profile_),
1946 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171947 // Make sure the profile cleans up its RequestContexts when an already
1948 // disabled extension is unloaded (since they are also tracking the disabled
1949 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081950 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391951 return;
1952 }
1953
[email protected]060d4972012-07-19 17:22:391954 // Remove the extension from our list.
[email protected]84df8332011-12-06 18:22:461955 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251956
[email protected]a9f39a312010-12-23 22:14:271957 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251958}
1959
[email protected]eaa7dd182010-12-14 11:09:001960void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401961 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171962
[email protected]84df8332011-12-06 18:22:461963 extensions_.Clear();
1964 disabled_extensions_.Clear();
1965 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491966 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321967
[email protected]c6e4a3412009-06-24 15:45:291968 // TODO(erikkay) should there be a notification for this? We can't use
1969 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1970 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321971}
1972
[email protected]eaa7dd182010-12-14 11:09:001973void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321974 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451975 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011976 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321977}
1978
[email protected]eaa7dd182010-12-14 11:09:001979void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:471980 if (extension_prefs_->pref_service()->ReadOnly())
1981 return;
1982
[email protected]9bd9a6862012-11-29 09:24:221983 if (pending_extension_manager()->HasPendingExtensions()) {
1984 // Don't garbage collect while there are pending installations, which may
1985 // be using the temporary installation directory. Try to garbage collect
1986 // again later.
1987 MessageLoop::current()->PostDelayedTask(
1988 FROM_HERE,
1989 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1990 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1991 return;
1992 }
1993
[email protected]45759612012-07-10 17:21:231994 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:471995 extension_prefs_->GetInstalledExtensionsInfo());
1996
[email protected]0db124b02012-11-07 04:55:051997 std::multimap<std::string, FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:471998 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:051999 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2000 info->at(i)->extension_path));
2001
[email protected]399583b2012-12-11 09:33:422002 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:052003 for (size_t i = 0; i < info->size(); ++i)
2004 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2005 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:472006
[email protected]7f8f24f2012-11-15 19:40:142007 if (!GetFileTaskRunner()->PostTask(
2008 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:472009 base::Bind(
2010 &extension_file_util::GarbageCollectExtensions,
2011 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:402012 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:472013 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402014 }
[email protected]f6ccd652010-11-17 00:48:342015
[email protected]f766a9d2012-07-11 01:57:442016#if defined(ENABLE_THEMES)
[email protected]f6ccd652010-11-17 00:48:342017 // Also garbage-collect themes. We check |profile_| to be
2018 // defensive; in the future, we may call GarbageCollectExtensions()
2019 // from somewhere other than Init() (e.g., in a timer).
2020 if (profile_) {
[email protected]d5949312012-12-03 22:13:302021 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:342022 }
[email protected]f766a9d2012-07-11 01:57:442023#endif
[email protected]3cf4f0992009-02-03 23:00:302024}
2025
[email protected]fb82dcd2012-03-21 14:15:462026void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:202027 if (app_sync_bundle_.HandlesApp(extension)) {
2028 app_sync_bundle_.SyncChangeIfNeeded(extension);
2029 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
2030 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:462031 }
2032}
2033
[email protected]eaa7dd182010-12-14 11:09:002034void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]31bb5ee62012-09-12 22:58:402035 if (updater_.get())
[email protected]93fd78f42009-07-10 16:43:172036 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502037
2038 ready_ = true;
[email protected]ad50def52011-10-19 23:17:072039 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272040 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:532041 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:072042 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:512043}
2044
[email protected]fcb58a862012-05-01 01:03:152045void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172046 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2047 // default extension state to DISABLED when the --disable-extensions flag
2048 // is set (http://crbug.com/29067).
2049 if (!extensions_enabled() &&
2050 !extension->is_theme() &&
2051 extension->location() != Extension::COMPONENT &&
[email protected]330840c2012-08-29 22:21:012052 !Extension::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152053 return;
[email protected]330840c2012-08-29 22:21:012054 }
[email protected]c8d407e2011-04-28 21:27:172055
2056 SetBeingUpgraded(extension, false);
2057
[email protected]1eb175082010-02-10 09:26:162058 // The extension is now loaded, remove its data from unloaded extension map.
2059 unloaded_extension_paths_.erase(extension->id());
2060
[email protected]bb7f40952011-01-13 00:21:202061 // If a terminated extension is loaded, remove it from the terminated list.
2062 UntrackTerminatedExtension(extension->id());
2063
[email protected]f17dbd42010-08-16 23:21:102064 // If the extension was disabled for a reload, then enable it.
2065 if (disabled_extension_paths_.erase(extension->id()) > 0)
2066 EnableExtension(extension->id());
2067
[email protected]d728e002010-12-08 04:46:232068 // Check if the extension's privileges have changed and disable the
2069 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:312070 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:392071
[email protected]215a7be2012-10-22 19:53:422072 // If this extension is a sideloaded extension and we've not performed a
2073 // wipeout before, we might disable this extension here.
2074 MaybeWipeout(extension);
2075
[email protected]695b5712012-12-06 23:55:282076 // Communicated to the Blacklist.
2077 std::set<std::string> already_in_blacklist;
2078
2079 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
2080 // Don't check the Blacklist yet because it's asynchronous (we do it at
2081 // the end). This pre-emptive check is because we will always store the
2082 // blacklisted state of *installed* extensions in prefs, and it's important
2083 // not to re-enable blacklisted extensions.
2084 blacklisted_extensions_.Insert(extension);
2085 already_in_blacklist.insert(extension->id());
2086 } else if (extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482087 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302088 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032089 content::NotificationService::current()->Notify(
2090 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2091 content::Source<Profile>(profile_),
2092 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442093
[email protected]7e9e2422012-12-13 19:54:192094 // Show the extension disabled error if a permissions increase was the
2095 // only reason it was disabled.
2096 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032097 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442098 extensions::AddExtensionDisabledError(this, extension);
2099 }
[email protected]695b5712012-12-06 23:55:282100 } else {
2101 // All apps that are displayed in the launcher are ordered by their ordinals
2102 // so we must ensure they have valid ordinals.
2103 if (extension->RequiresSortOrdinal()) {
2104 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2105 extension->id(), syncer::StringOrdinal());
2106 }
2107
2108 extensions_.Insert(extension);
2109 SyncExtensionChangeIfNeeded(*extension);
2110 NotifyExtensionLoaded(extension);
2111 DoPostLoadTasks(extension);
[email protected]f574c402012-12-04 23:20:312112 }
2113
[email protected]695b5712012-12-06 23:55:282114 // Lastly, begin the process for checking the blacklist status of extensions.
2115 // This may need to go to other threads so is asynchronous.
2116 std::set<std::string> id_set;
2117 id_set.insert(extension->id());
2118 blacklist_->GetBlacklistedIDs(
2119 id_set,
2120 base::Bind(&ExtensionService::ManageBlacklist,
2121 AsWeakPtr(),
2122 already_in_blacklist));
[email protected]aab98a52009-12-02 03:22:352123}
2124
[email protected]8c484b742012-11-29 06:05:362125void ExtensionService::AddComponentExtension(const Extension* extension) {
2126 const std::string old_version_string(
2127 extension_prefs_->GetVersionString(extension->id()));
2128 const Version old_version(old_version_string);
2129
2130 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2131 VLOG(1) << "Component extension " << extension->name() << " ("
2132 << extension->id() << ") installing/upgrading from '"
2133 << old_version_string << "' to " << extension->version()->GetString();
2134
2135 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422136 Extension::ENABLED_COMPONENT,
2137 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362138 return;
2139 }
2140
2141 AddExtension(extension);
2142}
2143
[email protected]902fd7b2011-07-27 18:42:312144void ExtensionService::InitializePermissions(const Extension* extension) {
2145 // If the extension has used the optional permissions API, it will have a
2146 // custom set of active permissions defined in the extension prefs. Here,
2147 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062148 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312149 extension_prefs()->GetActivePermissions(extension->id());
2150
2151 if (active_permissions.get()) {
2152 // We restrict the active permissions to be within the bounds defined in the
2153 // extension's manifest.
2154 // a) active permissions must be a subset of optional + default permissions
2155 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062156 scoped_refptr<PermissionSet> total_permissions =
2157 PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:312158 extension->required_permission_set(),
2159 extension->optional_permission_set());
2160
2161 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062162 scoped_refptr<PermissionSet> adjusted_active =
2163 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312164 total_permissions.get(), active_permissions.get());
2165
2166 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062167 adjusted_active = PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:312168 extension->required_permission_set(), adjusted_active.get());
2169
[email protected]c333e792012-01-06 16:57:392170 extensions::PermissionsUpdater perms_updater(profile());
2171 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312172 }
2173
[email protected]8d888c12010-11-30 00:00:252174 // We keep track of all permissions the user has granted each extension.
2175 // This allows extensions to gracefully support backwards compatibility
2176 // by including unknown permissions in their manifests. When the user
2177 // installs the extension, only the recognized permissions are recorded.
2178 // When the unknown permissions become recognized (e.g., through browser
2179 // upgrade), we can prompt the user to accept these new permissions.
2180 // Extensions can also silently upgrade to less permissions, and then
2181 // silently upgrade to a version that adds these permissions back.
2182 //
2183 // For example, pretend that Chrome 10 includes a permission "omnibox"
2184 // for an API that adds suggestions to the omnibox. An extension can
2185 // maintain backwards compatibility while still having "omnibox" in the
2186 // manifest. If a user installs the extension on Chrome 9, the browser
2187 // will record the permissions it recognized, not including "omnibox."
2188 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2189 // will disable the extension and prompt the user to approve the increase
2190 // in privileges. The extension could then release a new version that
2191 // removes the "omnibox" permission. When the user upgrades, Chrome will
2192 // still remember that "omnibox" had been granted, so that if the
2193 // extension once again includes "omnibox" in an upgrade, the extension
2194 // can upgrade without requiring this user's approval.
[email protected]695b5712012-12-06 23:55:282195 const Extension* old = GetInstalledExtension(extension->id());
[email protected]8d888c12010-11-30 00:00:252196 bool is_extension_upgrade = old != NULL;
2197 bool is_privilege_increase = false;
[email protected]44d62b62012-04-11 00:06:032198 bool previously_disabled = false;
[email protected]eb5e4f92012-08-15 23:33:282199 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252200
[email protected]902fd7b2011-07-27 18:42:312201 // We only need to compare the granted permissions to the current permissions
2202 // if the extension is not allowed to silently increase its permissions.
[email protected]be083862012-09-01 03:53:452203 bool is_default_app_install =
2204 (!is_extension_upgrade && extension->was_installed_by_default());
2205 if (!(extension->CanSilentlyIncreasePermissions()
2206 || is_default_app_install)) {
[email protected]8d888c12010-11-30 00:00:252207 // Add all the recognized permissions if the granted permissions list
2208 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062209 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312210 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522211 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252212
2213 // Here, we check if an extension's privileges have increased in a manner
2214 // that requires the user's approval. This could occur because the browser
2215 // upgraded and recognized additional privileges, or an extension upgrades
2216 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522217 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312218 granted_permissions->HasLessPrivilegesThan(
2219 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252220 }
2221
[email protected]be083862012-09-01 03:53:452222 // Silently grant all active permissions to default apps only on install.
2223 // After install they should behave like other apps.
[email protected]c856e9762012-10-12 19:42:382224 if (is_default_app_install)
2225 GrantPermissions(extension, true);
[email protected]be083862012-09-01 03:53:452226
[email protected]8d888c12010-11-30 00:00:252227 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252228 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2229 // that we aren't downgrading.
2230 if (extension->location() != Extension::LOAD)
[email protected]31bb5ee62012-09-12 22:58:402231 CHECK_GE(extension->version()->CompareTo(*(old->version())), 0);
[email protected]8d888c12010-11-30 00:00:252232
2233 // Extensions get upgraded if the privileges are allowed to increase or
2234 // the privileges haven't increased.
2235 if (!is_privilege_increase) {
2236 SetBeingUpgraded(old, true);
2237 SetBeingUpgraded(extension, true);
2238 }
2239
[email protected]44d62b62012-04-11 00:06:032240 // If the extension was already disabled, suppress any alerts for becoming
2241 // disabled on permissions increase.
2242 previously_disabled = extension_prefs_->IsExtensionDisabled(old->id());
[email protected]7e9e2422012-12-13 19:54:192243 // Legacy disabled extensions do not have a disable reason. Infer that if
2244 // there was no permission increase, it was likely disabled by the user.
2245 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
2246 !extension_prefs_->DidExtensionEscalatePermissions(old->id())) {
2247 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032248 }
[email protected]7e9e2422012-12-13 19:54:192249 // Extensions that came to us disabled from sync need a similar inference,
2250 // except based on the new version's permissions.
2251 if (previously_disabled &&
2252 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2253 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2254 extension_prefs_->ClearDisableReasons(extension->id());
2255 if (!is_privilege_increase)
2256 disable_reasons |= Extension::DISABLE_USER_ACTION;
2257 }
2258 if (disable_reasons != Extension::DISABLE_NONE)
2259 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]44d62b62012-04-11 00:06:032260
[email protected]8d888c12010-11-30 00:00:252261 // To upgrade an extension in place, unload the old one and
2262 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592263 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252264 old = NULL;
2265 }
2266
2267 // Extension has changed permissions significantly. Disable it. A
2268 // notification should be sent by the caller.
2269 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192270 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492271 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2272 RecordPermissionMessagesHistogram(
2273 extension, "Extensions.Permissions_AutoDisable");
2274 }
[email protected]b3317ad2011-04-28 23:46:002275 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252276 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]eb5e4f92012-08-15 23:33:282277 extension_prefs_->AddDisableReason(
2278 extension->id(),
2279 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252280 }
2281}
2282
[email protected]215a7be2012-10-22 19:53:422283void ExtensionService::MaybeWipeout(
2284 const extensions::Extension* extension) {
[email protected]1ff4d282012-12-08 00:39:212285 if (!wipeout_is_active_)
[email protected]215a7be2012-10-22 19:53:422286 return;
2287
[email protected]1ff4d282012-12-08 00:39:212288 if (extension->GetType() != Extension::TYPE_EXTENSION)
[email protected]215a7be2012-10-22 19:53:422289 return;
2290
[email protected]1ff4d282012-12-08 00:39:212291 Extension::Location location = extension->location();
[email protected]eea97032012-12-14 00:16:182292 if (location != Extension::EXTERNAL_REGISTRY)
[email protected]1ff4d282012-12-08 00:39:212293 return;
2294
2295 if (extension_prefs_->IsExternalExtensionExcludedFromWipeout(extension->id()))
2296 return;
2297
[email protected]215a7be2012-10-22 19:53:422298 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]1ff4d282012-12-08 00:39:212299 if (disable_reasons == Extension::DISABLE_NONE) {
2300 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
2301 extension_prefs_->AddDisableReason(
2302 extension->id(),
2303 static_cast<Extension::DisableReason>(
2304 Extension::DISABLE_SIDELOAD_WIPEOUT));
2305 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.ExtensionWipedStatus", true);
[email protected]3a2748c42013-01-15 10:13:232306 wipeout_count_++;
[email protected]215a7be2012-10-22 19:53:422307 }
2308}
2309
[email protected]eaa7dd182010-12-14 11:09:002310void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392311 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462312 for (ExtensionSet::const_iterator iter = extensions_.begin();
2313 iter != extensions_.end(); ++iter) {
2314 const Extension* extension = *iter;
2315 if (!extension->is_theme() && extension->location() != Extension::COMPONENT)
2316 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352317 }
2318
2319 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252320}
2321
[email protected]8266d662011-07-12 21:53:262322void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502323 const Extension* extension,
[email protected]98270432012-09-11 20:51:242324 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052325 bool has_requirement_errors,
2326 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122327 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202328
[email protected]c3cfb012011-04-06 22:07:352329 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032330 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412331 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062332 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
2333 if (!pending_extension_info->ShouldAllowInstall(*extension)) {
2334 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322335
[email protected]31bb5ee62012-09-12 22:58:402336 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2337 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222338 << " and update URL "
2339 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402340 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542341
[email protected]ad50def52011-10-19 23:17:072342 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272343 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532344 content::Source<Profile>(profile_),
2345 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542346
[email protected]4416c5a2010-06-26 01:28:572347 // Delete the extension directory since we're not going to
2348 // load it.
[email protected]7f8f24f2012-11-15 19:40:142349 if (!GetFileTaskRunner()->PostTask(
2350 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362351 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402352 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362353 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402354 }
[email protected]4416c5a2010-06-26 01:28:572355 return;
2356 }
[email protected]51a3bf8b2012-06-08 22:53:062357
2358 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572359 } else {
[email protected]c3cfb012011-04-06 22:07:352360 // We explicitly want to re-enable an uninstalled external
2361 // extension; if we're here, that means the user is manually
2362 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332363 if (IsExternalExtensionUninstalled(id)) {
2364 initial_enable = true;
2365 }
[email protected]aa142702010-03-26 01:26:332366 }
2367
[email protected]98270432012-09-11 20:51:242368 // Unsupported requirements overrides the management policy.
2369 if (has_requirement_errors) {
2370 initial_enable = false;
2371 extension_prefs_->AddDisableReason(
2372 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2373 // If the extension was disabled because of unsupported requirements but
2374 // now supports all requirements after an update and there are not other
2375 // disable reasons, enable it.
2376 } else if (extension_prefs_->GetDisableReasons(id) ==
2377 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2378 initial_enable = true;
2379 extension_prefs_->ClearDisableReasons(id);
2380 }
2381
[email protected]695b5712012-12-06 23:55:282382 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552383 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2384 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302385 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
2386 extension->location(), Extension::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552387 RecordPermissionMessagesHistogram(
2388 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302389 } else {
2390 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2391 extension->GetType(), 100);
2392 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
2393 extension->location(), Extension::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552394 }
2395
[email protected]a39921b42012-02-28 03:42:542396 // Certain extension locations are specific enough that we can
2397 // auto-acknowledge any extension that came from one of them.
[email protected]31bb5ee62012-09-12 22:58:402398 if (extension->location() == Extension::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542399 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422400 const Extension::State initial_state =
2401 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222402 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]399583b2012-12-11 09:33:422403 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2404 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052405
2406 // Transfer ownership of |extension|.
[email protected]399583b2012-12-11 09:33:422407 delayed_updates_for_idle_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052408
2409 // Notify extension of available update.
2410 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2411 profile_, id, extension->manifest()->value());
2412 return;
2413 }
2414
[email protected]399583b2012-12-11 09:33:422415 if (installs_delayed()) {
2416 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2417 page_ordinal);
2418 delayed_installs_.Insert(extension);
2419 } else {
2420 AddNewOrUpdatedExtension(extension, initial_state, page_ordinal);
2421 }
[email protected]8c484b742012-11-29 06:05:362422}
2423
2424void ExtensionService::AddNewOrUpdatedExtension(
2425 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422426 Extension::State initial_state,
2427 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362428 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2429
[email protected]4416c5a2010-06-26 01:28:572430 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262431 extension,
[email protected]8c484b742012-11-29 06:05:362432 initial_state,
[email protected]36a5c4c2011-12-14 16:34:502433 page_ordinal);
[email protected]25b34332009-06-05 21:53:192434
[email protected]6f6101832012-11-27 22:10:482435 FinishInstallation(extension);
2436}
2437
2438void ExtensionService::MaybeFinishDelayedInstallation(
2439 const std::string& extension_id) {
2440 // Check if the extension already got updated.
[email protected]399583b2012-12-11 09:33:422441 if (!delayed_updates_for_idle_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482442 return;
2443 // Check if the extension is idle.
2444 if (!IsExtensionIdle(extension_id))
2445 return;
2446
2447 FinishDelayedInstallation(extension_id);
2448}
2449
2450void ExtensionService::FinishDelayedInstallation(
2451 const std::string& extension_id) {
2452 scoped_refptr<const Extension> extension(
2453 GetPendingExtensionUpdate(extension_id));
2454 CHECK(extension);
[email protected]399583b2012-12-11 09:33:422455 delayed_updates_for_idle_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482456
[email protected]399583b2012-12-11 09:33:422457 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482458 NOTREACHED();
2459
2460 FinishInstallation(extension);
2461}
2462
2463void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]fcb58a862012-05-01 01:03:152464 content::NotificationService::current()->Notify(
2465 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2466 content::Source<Profile>(profile_),
2467 content::Details<const Extension>(extension));
2468
[email protected]d96eb512012-11-01 23:44:082469 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362470
[email protected]399583b2012-12-11 09:33:422471 // Unpacked extensions default to allowing file access, but if that has been
2472 // overridden, don't reset the value.
2473 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
2474 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2475 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2476 }
2477
[email protected]6f6101832012-11-27 22:10:482478 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032479
[email protected]a51c9e92012-12-09 09:19:202480#if defined(ENABLE_THEMES)
2481 // We do this here since AddExtension() is always called on browser
2482 // startup, and we only really care about the last theme installed.
2483 // If that ever changes and we have to move this code somewhere
2484 // else, it should be somewhere that's not in the startup path.
2485 if (extension->is_theme() && extensions_.GetByID(extension->id())) {
2486 DCHECK_EQ(extensions_.GetByID(extension->id()), extension);
2487 // Now that the theme extension is visible from outside the
2488 // ExtensionService, notify the ThemeService about the
2489 // newly-installed theme.
2490 ThemeServiceFactory::GetForProfile(profile_)->SetTheme(extension);
2491 }
2492#endif
2493
[email protected]612a1cb12012-10-17 13:18:032494 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082495 // so he can reenable it. We do this last so that it has already been
2496 // added to our list of extensions.
2497 if (unacknowledged_external) {
[email protected]612a1cb12012-10-17 13:18:032498 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082499 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2500 EXTERNAL_EXTENSION_INSTALLED,
2501 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2502 }
[email protected]4a190632009-05-09 01:07:422503}
2504
[email protected]76b65442012-11-17 14:11:482505const Extension* ExtensionService::GetPendingExtensionUpdate(
2506 const std::string& id) const {
[email protected]399583b2012-12-11 09:33:422507 return delayed_updates_for_idle_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482508}
2509
[email protected]bb7f40952011-01-13 00:21:202510void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462511 if (!terminated_extensions_.Contains(extension->id()))
2512 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202513
[email protected]b3f7fe22011-11-11 19:27:562514 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202515}
2516
2517void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202518 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462519 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202520}
2521
[email protected]0dfe05c2011-02-23 23:03:362522const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472523 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282524 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472525}
2526
2527const Extension* ExtensionService::GetInstalledExtension(
2528 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282529 int include_mask = INCLUDE_ENABLED |
2530 INCLUDE_DISABLED |
2531 INCLUDE_TERMINATED |
2532 INCLUDE_BLACKLISTED;
2533 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362534}
2535
[email protected]eaa7dd182010-12-14 11:09:002536bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442537 // Allow bindings for all packaged extensions and component hosted apps.
2538 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2539 ExtensionURLInfo(url));
2540 return extension && (!extension->is_hosted_app() ||
2541 extension->location() == Extension::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392542}
2543
[email protected]4d007b312012-10-17 03:00:482544bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
2545 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2546 ExtensionURLInfo(*url));
2547 if (extension && extension->is_platform_app()) {
2548 *url = GURL(chrome::kExtensionInvalidRequestURL);
2549 return true;
2550 }
2551
2552 return false;
2553}
2554
[email protected]9060d8b02012-01-13 02:14:302555bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322556 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142557 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322558 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312559 Extension::Location location,
[email protected]47fc70c2011-12-06 07:29:512560 int creation_flags,
2561 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382562 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2563 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012564 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302565 return false;
[email protected]a8af9fdb2010-10-28 21:52:202566
[email protected]7577a5c52009-07-30 06:21:582567 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492568 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582569 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022570 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352571
[email protected]7577a5c52009-07-30 06:21:582572 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352573 // The default apps will have the location set as INTERNAL. Since older
2574 // default apps are installed as EXTERNAL, we override them. However, if the
2575 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402576 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352577 bool is_default_apps_migration =
2578 (location == Extension::INTERNAL &&
2579 Extension::IsExternalLocation(existing->location()));
2580
2581 if (!is_default_apps_migration) {
2582 DCHECK(version);
2583
2584 switch (existing->version()->CompareTo(*version)) {
2585 case -1: // existing version is older, we should upgrade
2586 break;
2587 case 0: // existing version is same, do nothing
2588 return false;
2589 case 1: // existing version is newer, uh-oh
2590 LOG(WARNING) << "Found external version of extension " << id
2591 << "that is older than current version. Current version "
2592 << "is: " << existing->VersionString() << ". New "
2593 << "version is: " << version->GetString()
2594 << ". Keeping current version.";
2595 return false;
2596 }
[email protected]7577a5c52009-07-30 06:21:582597 }
2598 }
2599
[email protected]9060d8b02012-01-13 02:14:302600 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302601 if (!pending_extension_manager()->AddFromExternalFile(
2602 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302603 return false;
[email protected]e3987852012-05-04 10:06:302604 }
[email protected]9c635f22010-12-02 09:36:362605
[email protected]14908b72011-04-20 06:54:362606 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012607 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162608 installer->set_install_source(location);
2609 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072610 installer->set_expected_version(*version);
2611 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312612 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182613#if defined(OS_CHROMEOS)
2614 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2615#else
[email protected]6dfbbf82010-03-12 23:09:162616 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182617#endif
[email protected]47fc70c2011-12-06 07:29:512618
2619 // Depending on the source, a new external extension might not need a user
2620 // notification on installation. For such extensions, mark them acknowledged
2621 // now to suppress the notification.
2622 if (mark_acknowledged)
2623 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302624
2625 return true;
[email protected]7577a5c52009-07-30 06:21:582626}
2627
[email protected]eaa7dd182010-12-14 11:09:002628void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402629 const FilePath& extension_path,
2630 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402631 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012632 content::NotificationService::current()->Notify(
2633 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2634 content::Source<Profile>(profile_),
2635 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402636
[email protected]8a205c02011-02-04 20:41:332637 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212638 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042639 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492640 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402641 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2642}
2643
[email protected]eaa7dd182010-12-14 11:09:002644void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132645 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182646 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332647 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182648 if (iter == orphaned_dev_tools_.end())
2649 return;
2650
[email protected]98f66112012-12-25 12:59:362651 DevToolsAgentHost::ConnectRenderViewHost(iter->second,
2652 host->render_view_host());
[email protected]406027c02010-09-27 08:03:182653 orphaned_dev_tools_.erase(iter);
2654}
2655
[email protected]432115822011-07-10 15:52:272656void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532657 const content::NotificationSource& source,
2658 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272659 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542660 case chrome::NOTIFICATION_APP_TERMINATING:
2661 // Shutdown has started. Don't start any more extension installs.
2662 // (We cannot use ExtensionService::Shutdown() for this because it
2663 // happens too late in browser teardown.)
2664 browser_terminating_ = true;
2665 break;
[email protected]432115822011-07-10 15:52:272666 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532667 if (profile_ !=
2668 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322669 break;
[email protected]6c2381d2011-10-19 02:52:532670 }
[email protected]a4ed6282009-12-14 20:51:162671
[email protected]3a1dc572012-07-31 22:25:132672 extensions::ExtensionHost* host =
2673 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262674
[email protected]fa2416f2011-05-03 08:41:202675 // Mark the extension as terminated and Unload it. We want it to
2676 // be in a consistent state: either fully working or not loaded
2677 // at all, but never half-crashed. We do it in a PostTask so
2678 // that other handlers of this notification will still have
2679 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362680 MessageLoop::current()->PostTask(
2681 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362682 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202683 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012684 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202685 host->extension()));
[email protected]31f77262009-12-02 20:48:532686 break;
2687 }
[email protected]432115822011-07-10 15:52:272688 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302689 content::RenderProcessHost* process =
2690 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192691 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302692 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192693 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2694 break;
2695
[email protected]15397b42012-05-16 23:56:062696 // Extensions need to know the channel for API restrictions.
2697 process->Send(new ExtensionMsg_SetChannel(
[email protected]5243df072012-07-11 21:41:012698 extensions::Feature::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062699
[email protected]77a6970c2011-04-23 16:58:562700 // Valid extension function names, used to setup bindings in renderer.
2701 std::vector<std::string> function_names;
2702 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2703 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532704
[email protected]77a6970c2011-04-23 16:58:562705 // Scripting whitelist. This is modified by tests and must be communicated
2706 // to renderers.
2707 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2708 *Extension::GetScriptingWhitelist()));
2709
2710 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532711 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462712 for (ExtensionSet::const_iterator iter = extensions_.begin();
2713 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492714 // Renderers don't need to know about themes.
2715 if (!(*iter)->is_theme())
2716 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562717 }
[email protected]9776e82e2011-11-15 02:17:532718 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562719 break;
2720 }
[email protected]432115822011-07-10 15:52:272721 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302722 content::RenderProcessHost* process =
2723 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192724 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302725 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192726 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2727 break;
2728
[email protected]6bc04fd82011-12-04 02:29:352729 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462730 BrowserThread::PostTask(
2731 BrowserThread::IO, FROM_HERE,
2732 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082733 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302734 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212735 break;
2736 }
[email protected]d91268022011-08-26 13:17:372737 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392738 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372739 break;
2740 }
[email protected]0db124b02012-11-07 04:55:052741 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2742 extensions::ExtensionHost* host =
2743 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222744 std::string extension_id = host->extension_id();
[email protected]399583b2012-12-11 09:33:422745 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222746 // We were waiting for this extension to become idle, it now might have,
2747 // so maybe finish installation.
2748 MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052749 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482750 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052751 AsWeakPtr(), extension_id),
2752 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052753 }
2754 break;
2755 }
[email protected]aa96d3a2010-08-21 08:45:252756
[email protected]4814b512009-11-07 00:12:292757 default:
2758 NOTREACHED() << "Unexpected notification type.";
2759 }
2760}
2761
[email protected]90bb38d2012-11-14 18:36:032762void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182763 IdentifyAlertableExtensions();
2764 CheckManagementPolicy();
2765}
2766
[email protected]eaa7dd182010-12-14 11:09:002767bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182768 return !GetAppIds().empty();
2769}
[email protected]377011d2010-07-20 04:18:502770
[email protected]eaa7dd182010-12-14 11:09:002771ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182772 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462773 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502774 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182775 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2776 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502777 }
2778
[email protected]ec5b50d2010-10-09 16:35:182779 return result;
[email protected]377011d2010-07-20 04:18:502780}
[email protected]d7e9a862010-11-03 21:57:492781
[email protected]dc9a74f72012-08-17 18:07:212782bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
2783 if (!extension->has_persistent_background_page())
2784 return true;
2785 ExtensionRuntimeDataMap::const_iterator it =
2786 extension_runtime_data_.find(extension->id());
2787 return it == extension_runtime_data_.end() ? false :
2788 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492789}
2790
[email protected]eaa7dd182010-12-14 11:09:002791void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282792 DCHECK(extension->has_background_page());
[email protected]d7e9a862010-11-03 21:57:492793 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072794 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272795 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532796 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072797 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492798}
2799
[email protected]65992d52012-09-19 23:05:312800void ExtensionService::InspectBackgroundPage(const Extension* extension) {
2801 DCHECK(extension);
2802
[email protected]fdd679b2012-11-15 20:49:392803 ExtensionProcessManager* pm = system_->process_manager();
[email protected]65992d52012-09-19 23:05:312804 extensions::LazyBackgroundTaskQueue* queue =
[email protected]fdd679b2012-11-15 20:49:392805 system_->lazy_background_task_queue();
[email protected]65992d52012-09-19 23:05:312806
2807 extensions::ExtensionHost* host =
2808 pm->GetBackgroundHostForExtension(extension->id());
2809 if (host) {
2810 InspectExtensionHost(host);
2811 } else {
2812 queue->AddPendingTask(
2813 profile_, extension->id(),
2814 base::Bind(&ExtensionService::InspectExtensionHost,
2815 base::Unretained(this)));
2816 }
2817}
2818
[email protected]dc9a74f72012-08-17 18:07:212819bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2820 ExtensionRuntimeDataMap::const_iterator it =
2821 extension_runtime_data_.find(extension->id());
2822 return it == extension_runtime_data_.end() ? false :
2823 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492824}
2825
[email protected]eaa7dd182010-12-14 11:09:002826void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492827 bool value) {
2828 extension_runtime_data_[extension->id()].being_upgraded = value;
2829}
[email protected]1bead0712010-11-27 17:41:532830
[email protected]dc9a74f72012-08-17 18:07:212831bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2832 ExtensionRuntimeDataMap::const_iterator it =
2833 extension_runtime_data_.find(extension->id());
2834 return it == extension_runtime_data_.end() ? false :
2835 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072836}
2837
2838void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2839 bool value) {
2840 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2841}
2842
[email protected]cebc3dc2011-04-18 17:15:002843void ExtensionService::RegisterNaClModule(const GURL& url,
2844 const std::string& mime_type) {
2845 NaClModuleInfo info;
2846 info.url = url;
2847 info.mime_type = mime_type;
2848
2849 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2850 nacl_module_list_.push_front(info);
2851}
2852
2853void ExtensionService::UnregisterNaClModule(const GURL& url) {
2854 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2855 DCHECK(iter != nacl_module_list_.end());
2856 nacl_module_list_.erase(iter);
2857}
2858
[email protected]ed0ba002011-05-26 16:55:132859void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332860 // An extension has been added which has a nacl_module component, which means
2861 // there is a MIME type that module wants to handle, so we need to add that
2862 // MIME type to plugins which handle NaCl modules in order to allow the
2863 // individual modules to handle these types.
[email protected]1e127022011-11-29 21:11:472864 FilePath path;
2865 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2866 return;
2867 const content::PepperPluginInfo* pepper_info =
2868 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2869 if (!pepper_info)
2870 return;
[email protected]300c0ea2011-07-15 23:04:332871
[email protected]1e127022011-11-29 21:11:472872 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2873 // Check each MIME type the plugins handle for the NaCl MIME type.
2874 for (mime_iter = pepper_info->mime_types.begin();
2875 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2876 if (mime_iter->mime_type == kNaClPluginMimeType) {
2877 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332878
[email protected]1e127022011-11-29 21:11:472879 PluginService::GetInstance()->
2880 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332881
[email protected]1e127022011-11-29 21:11:472882 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332883
[email protected]1e127022011-11-29 21:11:472884 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2885 nacl_module_list_.begin();
2886 iter != nacl_module_list_.end(); ++iter) {
2887 // Add the MIME type specified in the extension to this NaCl plugin,
2888 // With an extra "nacl" argument to specify the location of the NaCl
2889 // manifest file.
2890 webkit::WebPluginMimeType mime_type_info;
2891 mime_type_info.mime_type = iter->mime_type;
2892 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2893 mime_type_info.additional_param_values.push_back(
2894 UTF8ToUTF16(iter->url.spec()));
2895 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332896 }
[email protected]1e127022011-11-29 21:11:472897
2898 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112899 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472900 // This plugin has been modified, no need to check the rest of its
2901 // types, but continue checking other plugins.
2902 break;
[email protected]ed0ba002011-05-26 16:55:132903 }
2904 }
[email protected]ed0ba002011-05-26 16:55:132905}
2906
[email protected]cebc3dc2011-04-18 17:15:002907ExtensionService::NaClModuleInfoList::iterator
2908 ExtensionService::FindNaClModule(const GURL& url) {
2909 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2910 iter != nacl_module_list_.end(); ++iter) {
2911 if (iter->url == url)
2912 return iter;
2913 }
2914 return nacl_module_list_.end();
2915}
[email protected]6cafe35a2012-08-27 22:40:502916
[email protected]32b36c62012-10-24 05:37:412917void ExtensionService::DoPostLoadTasks(const Extension* extension) {
2918 std::map<std::string, int>::iterator it =
2919 on_load_events_.find(extension->id());
2920 if (it == on_load_events_.end())
2921 return;
2922
2923 int events_to_fire = it->second;
2924 extensions::LazyBackgroundTaskQueue* queue =
2925 system_->lazy_background_task_queue();
2926 if (queue->ShouldEnqueueTask(profile(), extension)) {
2927 if (events_to_fire & EVENT_LAUNCHED)
[email protected]6cafe35a2012-08-27 22:40:502928 queue->AddPendingTask(profile(), extension->id(),
2929 base::Bind(&ExtensionService::LaunchApplication));
[email protected]32b36c62012-10-24 05:37:412930 if (events_to_fire & EVENT_RESTARTED)
2931 queue->AddPendingTask(profile(), extension->id(),
2932 base::Bind(&ExtensionService::RestartApplication));
[email protected]6cafe35a2012-08-27 22:40:502933 }
[email protected]32b36c62012-10-24 05:37:412934
2935 on_load_events_.erase(it);
[email protected]6cafe35a2012-08-27 22:40:502936}
2937
2938// static
2939void ExtensionService::LaunchApplication(
2940 extensions::ExtensionHost* extension_host) {
2941 if (!extension_host)
2942 return;
2943
2944#if !defined(OS_ANDROID)
2945 extensions::LaunchPlatformApp(extension_host->profile(),
2946 extension_host->extension(),
2947 NULL, FilePath());
2948#endif
2949}
[email protected]65992d52012-09-19 23:05:312950
[email protected]32b36c62012-10-24 05:37:412951// static
2952void ExtensionService::RestartApplication(
2953 extensions::ExtensionHost* extension_host) {
2954 if (!extension_host)
2955 return;
2956
2957#if !defined(OS_ANDROID)
2958 extensions::AppEventRouter::DispatchOnRestartedEvent(
2959 extension_host->profile(), extension_host->extension());
2960#endif
2961}
2962
2963bool ExtensionService::HasShellWindows(const std::string& extension_id) {
2964 const Extension* current_extension = GetExtensionById(extension_id, false);
2965 return current_extension && current_extension->is_platform_app() &&
2966 !extensions::ShellWindowRegistry::Get(profile_)->
2967 GetShellWindowsForApp(extension_id).empty();
2968}
2969
[email protected]65992d52012-09-19 23:05:312970void ExtensionService::InspectExtensionHost(
2971 extensions::ExtensionHost* host) {
2972 if (host)
2973 DevToolsWindow::OpenDevToolsWindow(host->render_view_host());
2974}
[email protected]612a1cb12012-10-17 13:18:032975
2976bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2977 // Extensions installed by policy can't be disabled. So even if a previous
2978 // installation disabled the extension, make sure it is now enabled.
2979 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2980 return true;
2981
2982 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2983 return false;
2984
[email protected]41070e8d2012-10-24 01:34:362985 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032986 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542987 // enabling them. Hosted apps are excepted because they are not dangerous
2988 // (they need to be launched by the user anyway).
2989 if (extension->GetType() != Extension::TYPE_HOSTED_APP &&
2990 Extension::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032991 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2992 return false;
2993 }
2994 }
[email protected]612a1cb12012-10-17 13:18:032995
2996 return true;
2997}
[email protected]0db124b02012-11-07 04:55:052998
2999bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:473000 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:333001 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:053002 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:473003 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223004 if (host)
3005 return false;
3006 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
3007}
3008
3009bool ExtensionService::ShouldDelayExtensionUpdate(
3010 const std::string& extension_id,
3011 bool wait_for_idle) const {
3012 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
3013
3014 // If delayed updates are globally disabled, or just for this extension,
3015 // don't delay.
3016 if (!install_updates_when_idle_ || !wait_for_idle)
3017 return false;
3018
[email protected]695b5712012-12-06 23:55:283019 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223020 // If there is no old extension, this is not an update, so don't delay.
3021 if (!old)
3022 return false;
3023
3024 if (old->has_persistent_background_page()) {
3025 // Delay installation if the extension listens for the onUpdateAvailable
3026 // event.
3027 return system_->event_router()->ExtensionHasEventListener(
3028 extension_id, kOnUpdateAvailableEvent);
3029 } else {
3030 // Delay installation if the extension is not idle.
3031 return !IsExtensionIdle(extension_id);
3032 }
[email protected]0db124b02012-11-07 04:55:053033}
[email protected]fdd679b2012-11-15 20:49:393034
[email protected]399583b2012-12-11 09:33:423035void ExtensionService::GarbageCollectIsolatedStorage() {
3036 scoped_ptr<base::hash_set<FilePath> > active_paths(
3037 new base::hash_set<FilePath>());
3038 for (ExtensionSet::const_iterator it = extensions_.begin();
3039 it != extensions_.end(); ++it) {
3040 if ((*it)->is_storage_isolated()) {
3041 active_paths->insert(
3042 BrowserContext::GetStoragePartitionForSite(
3043 profile_,
3044 GetSiteForExtensionId((*it)->id()))->GetPath());
3045 }
3046 }
3047
3048 DCHECK(!installs_delayed());
3049 set_installs_delayed(true);
3050 BrowserContext::GarbageCollectStoragePartitions(
3051 profile_, active_paths.Pass(),
3052 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3053 AsWeakPtr()));
3054}
3055
3056void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
3057 set_installs_delayed(false);
3058 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3059 it != delayed_installs_.end();
3060 ++it) {
3061 FinishDelayedInstallation((*it)->id());
3062 }
3063 for (ExtensionSet::const_iterator it = delayed_updates_for_idle_.begin();
3064 it != delayed_updates_for_idle_.end();
3065 ++it) {
3066 MaybeFinishDelayedInstallation((*it)->id());
3067 }
3068 delayed_installs_.Clear();
3069}
3070
3071void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3072 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3073}
3074
[email protected]fdd679b2012-11-15 20:49:393075void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283076 blacklist_->GetBlacklistedIDs(
3077 GenerateInstalledExtensionsSet()->GetIDs(),
3078 base::Bind(&ExtensionService::ManageBlacklist,
3079 AsWeakPtr(),
3080 blacklisted_extensions_.GetIDs()));
3081}
3082
3083void ExtensionService::ManageBlacklist(
3084 const std::set<std::string>& old_blacklisted_ids,
3085 const std::set<std::string>& new_blacklisted_ids) {
3086 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3087
3088 std::set<std::string> no_longer_blacklisted;
3089 std::set_difference(old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3090 new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3091 std::inserter(no_longer_blacklisted,
3092 no_longer_blacklisted.begin()));
3093 std::set<std::string> not_yet_blacklisted;
3094 std::set_difference(new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3095 old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3096 std::inserter(not_yet_blacklisted,
3097 not_yet_blacklisted.begin()));
3098
3099 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3100 it != no_longer_blacklisted.end(); ++it) {
3101 scoped_refptr<const Extension> extension =
3102 blacklisted_extensions_.GetByID(*it);
3103 DCHECK(extension);
3104 if (!extension)
3105 continue;
3106 blacklisted_extensions_.Remove(*it);
3107 AddExtension(extension);
3108 }
3109
3110 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3111 it != not_yet_blacklisted.end(); ++it) {
3112 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
3113 DCHECK(extension);
3114 if (!extension)
3115 continue;
3116 blacklisted_extensions_.Insert(extension);
3117 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
3118 }
3119
3120 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393121}