blob: a64dfab4f65ede43321f6363a7de266e58cfa97a [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]3853a4c2013-02-11 17:15:5719#include "base/prefs/pref_service.h"
[email protected]7286e3fc2011-07-19 22:13:2420#include "base/stl_util.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]3ea1b182013-02-08 22:38:4123#include "base/strings/string_number_conversions.h"
[email protected]7f8f24f2012-11-15 19:40:1424#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3626#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5927#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1928#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3329#include "base/version.h"
[email protected]a692a952011-11-28 08:23:5430#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]e07f2da2012-08-01 22:46:1231#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]15730c42009-09-03 00:03:2032#include "chrome/browser/browser_process.h"
[email protected]70019152012-12-19 11:44:1933#include "chrome/browser/devtools/devtools_window.h"
[email protected]32b36c62012-10-24 05:37:4134#include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
[email protected]895a1e52012-05-15 02:50:1235#include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
[email protected]ad445762013-01-23 00:47:4436#include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
[email protected]c39df1a2013-01-08 18:22:0737#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
[email protected]e9f541a2012-11-19 21:52:3138#include "chrome/browser/extensions/api/runtime/runtime_api.h"
[email protected]4636c832013-01-11 02:10:1139#include "chrome/browser/extensions/api/storage/settings_frontend.h"
[email protected]af9db5f2011-10-05 05:13:1540#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]5db9ada2012-04-11 13:48:2041#include "chrome/browser/extensions/app_sync_data.h"
[email protected]5a38dfd2012-07-23 23:22:1042#include "chrome/browser/extensions/browser_event_router.h"
[email protected]d8c8f25f2011-11-02 18:18:0143#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1744#include "chrome/browser/extensions/crx_installer.h"
[email protected]d9ede582012-08-14 19:21:3845#include "chrome/browser/extensions/data_deleter.h"
[email protected]62f051c2012-03-29 17:04:4446#include "chrome/browser/extensions/extension_disabled_ui.h"
[email protected]14a000d2010-04-29 21:44:2447#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]89226982012-07-16 20:09:1848#include "chrome/browser/extensions/extension_error_ui.h"
[email protected]b1748b1d82009-11-30 20:32:5649#include "chrome/browser/extensions/extension_host.h"
[email protected]00b38242012-07-18 18:43:2250#include "chrome/browser/extensions/extension_install_ui.h"
[email protected]4814b512009-11-07 00:12:2951#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]3569b502012-01-12 20:08:2352#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4353#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0454#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]31d8f5f22012-04-02 15:22:0855#include "chrome/browser/extensions/extension_system.h"
[email protected]612a1cb12012-10-17 13:18:0356#include "chrome/browser/extensions/external_install_ui.h"
[email protected]5df038b2012-07-16 19:03:2757#include "chrome/browser/extensions/external_provider_impl.h"
58#include "chrome/browser/extensions/external_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0159#include "chrome/browser/extensions/installed_loader.h"
[email protected]6cafe35a2012-08-27 22:40:5060#include "chrome/browser/extensions/lazy_background_task_queue.h"
[email protected]fdd679b2012-11-15 20:49:3961#include "chrome/browser/extensions/management_policy.h"
[email protected]b2907fd2011-03-25 16:43:3762#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3963#include "chrome/browser/extensions/permissions_updater.h"
[email protected]6cafe35a2012-08-27 22:40:5064#include "chrome/browser/extensions/platform_app_launcher.h"
[email protected]6cafe35a2012-08-27 22:40:5065#include "chrome/browser/extensions/shell_window_registry.h"
[email protected]d8c8f25f2011-11-02 18:18:0166#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]42a08162012-03-16 18:09:1167#include "chrome/browser/extensions/updater/extension_updater.h"
[email protected]56ad3792010-05-28 17:45:3368#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]c2295f72013-01-03 22:18:5669#include "chrome/browser/plugins/chrome_plugin_service_filter.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/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3175#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]b07e606e2012-09-15 20:16:1576#include "chrome/browser/ui/webui/theme_source.h"
[email protected]aab98a52009-12-02 03:22:3577#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2778#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4779#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5480#include "chrome/common/chrome_switches.h"
[email protected]15397b42012-05-16 23:56:0681#include "chrome/common/chrome_version_info.h"
[email protected]5b1a0e22009-05-26 19:00:5882#include "chrome/common/extensions/extension.h"
[email protected]7c927b62010-02-24 09:54:1383#include "chrome/common/extensions/extension_file_util.h"
[email protected]895a1e52012-05-15 02:50:1284#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]77a6970c2011-04-23 16:58:5685#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2186#include "chrome/common/extensions/extension_resource.h"
[email protected]612a1cb12012-10-17 13:18:0387#include "chrome/common/extensions/feature_switch.h"
[email protected]e9f541a2012-11-19 21:52:3188#include "chrome/common/extensions/features/feature.h"
[email protected]65348062013-01-15 07:27:2289#include "chrome/common/extensions/manifest_url_handler.h"
[email protected]25b34332009-06-05 21:53:1990#include "chrome/common/pref_names.h"
[email protected]21f2e2d2013-01-23 22:59:5291#include "chrome/common/startup_metric_utils.h"
[email protected]a57209872009-05-04 22:53:1492#include "chrome/common/url_constants.h"
[email protected]c38831a12011-10-28 12:44:4993#include "content/public/browser/browser_thread.h"
[email protected]98f66112012-12-25 12:59:3694#include "content/public/browser/devtools_agent_host.h"
[email protected]0e12d7d2011-12-01 16:21:4495#include "content/public/browser/devtools_manager.h"
[email protected]05aad2da2011-10-28 10:12:3796#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1697#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:3898#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:3099#include "content/public/browser/render_process_host.h"
[email protected]399583b2012-12-11 09:33:42100#include "content/public/browser/site_instance.h"
101#include "content/public/browser/storage_partition.h"
[email protected]24ea7a12013-01-27 23:54:53102#include "content/public/browser/url_data_source.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]1d5e58b2013-01-31 08:41:40128using extensions::Manifest;
[email protected]c2e66e12012-06-27 06:27:06129using extensions::PermissionMessage;
130using extensions::PermissionMessages;
131using extensions::PermissionSet;
[email protected]e410b5f2012-12-14 14:02:24132using extensions::UnloadedExtensionInfo;
[email protected]5ef47ec2010-01-28 05:58:05133
[email protected]c6d474f82009-12-16 21:11:06134namespace errors = extension_manifest_errors;
135
[email protected]b6ab96d2009-08-20 18:58:19136namespace {
137
[email protected]d96eb512012-11-01 23:44:08138// Histogram values for logging events related to externally installed
139// extensions.
140enum ExternalExtensionEvent {
141 EXTERNAL_EXTENSION_INSTALLED = 0,
142 EXTERNAL_EXTENSION_IGNORED,
143 EXTERNAL_EXTENSION_REENABLED,
144 EXTERNAL_EXTENSION_UNINSTALLED,
145 EXTERNAL_EXTENSION_BUCKET_BOUNDARY,
146};
147
[email protected]612a1cb12012-10-17 13:18:03148// Prompt the user this many times before considering an extension acknowledged.
149static const int kMaxExtensionAcknowledgePromptCount = 3;
150
[email protected]0db124b02012-11-07 04:55:05151// Wait this many seconds after an extensions becomes idle before updating it.
152static const int kUpdateIdleDelay = 5;
153
[email protected]9bd9a6862012-11-29 09:24:22154// Wait this many seconds before trying to garbage collect extensions again.
155static const int kGarbageCollectRetryDelay = 30;
156
[email protected]300c0ea2011-07-15 23:04:33157const char* kNaClPluginMimeType = "application/x-nacl";
158
[email protected]3bdba0d2011-08-23 07:17:30159static bool IsSyncableExtension(const Extension& extension) {
160 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
161}
162
163static bool IsSyncableApp(const Extension& extension) {
164 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
165}
166
[email protected]c6d474f82009-12-16 21:11:06167} // namespace
[email protected]b6ab96d2009-08-20 18:58:19168
[email protected]eaa7dd182010-12-14 11:09:00169ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49170 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57171 being_upgraded(false),
172 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49173}
174
[email protected]eaa7dd182010-12-14 11:09:00175ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49176}
177
[email protected]cebc3dc2011-04-18 17:15:00178ExtensionService::NaClModuleInfo::NaClModuleInfo() {
179}
180
181ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
182}
183
[email protected]eaa7dd182010-12-14 11:09:00184// ExtensionService.
[email protected]6014d672008-12-05 00:38:25185
[email protected]31bb5ee62012-09-12 22:58:40186const char ExtensionService::kInstallDirectoryName[] = "Extensions";
[email protected]15d5b472011-11-24 04:17:24187
[email protected]31bb5ee62012-09-12 22:58:40188const char ExtensionService::kLocalAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24189 "Local App Settings";
[email protected]31bb5ee62012-09-12 22:58:40190const char ExtensionService::kLocalExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24191 "Local Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40192const char ExtensionService::kSyncAppSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24193 "Sync App Settings";
[email protected]31bb5ee62012-09-12 22:58:40194const char ExtensionService::kSyncExtensionSettingsDirectoryName[] =
[email protected]15d5b472011-11-24 04:17:24195 "Sync Extension Settings";
[email protected]675bb01012012-12-14 22:03:29196const char ExtensionService::kManagedSettingsDirectoryName[] =
197 "Managed Extension Settings";
[email protected]31bb5ee62012-09-12 22:58:40198const char ExtensionService::kStateStoreName[] = "Extension State";
[email protected]a690e292012-12-19 19:22:49199const char ExtensionService::kRulesStoreName[] = "Extension Rules";
[email protected]494c06e2009-07-25 01:06:42200
[email protected]8e4560b62011-01-14 10:09:14201void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12202 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20203
[email protected]7a4c6852010-09-16 03:44:22204 // Check if the providers know about this extension.
[email protected]5df038b2012-07-16 19:03:27205 extensions::ProviderCollection::const_iterator i;
[email protected]0a60a2e2010-10-25 16:15:21206 for (i = external_extension_providers_.begin();
207 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14208 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21209 if (i->get()->HasExtension(id))
210 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22211 }
212
[email protected]0f48fca2011-05-19 18:46:35213 // We get the list of external extensions to check from preferences.
214 // It is possible that an extension has preferences but is not loaded.
215 // For example, an extension that requires experimental permissions
216 // will not be loaded if the experimental command line flag is not used.
217 // In this case, do not uninstall.
[email protected]31bb5ee62012-09-12 22:58:40218 if (!GetInstalledExtension(id)) {
[email protected]0f48fca2011-05-19 18:46:35219 // We can't call UninstallExtension with an unloaded/invalid
220 // extension ID.
221 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
222 << "with id: " << id;
223 return;
224 }
[email protected]d6ebc9792011-04-07 18:18:33225 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22226}
227
[email protected]7f8f24f2012-11-15 19:40:14228void ExtensionService::SetFileTaskRunnerForTesting(
229 base::SequencedTaskRunner* task_runner) {
230 file_task_runner_ = task_runner;
231}
232
[email protected]8e4560b62011-01-14 10:09:14233void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22234 external_extension_providers_.clear();
235}
236
[email protected]8e4560b62011-01-14 10:09:14237void ExtensionService::AddProviderForTesting(
[email protected]5df038b2012-07-16 19:03:27238 extensions::ExternalProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12239 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21240 external_extension_providers_.push_back(
[email protected]5df038b2012-07-16 19:03:27241 linked_ptr<extensions::ExternalProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22242}
243
[email protected]9060d8b02012-01-13 02:14:30244bool ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22245 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42246 const GURL& update_url,
[email protected]1d5e58b2013-01-31 08:41:40247 Manifest::Location location) {
[email protected]ab22ba42011-01-14 16:36:38248 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20250
[email protected]8a87a5332011-08-11 17:54:59251 const Extension* extension = GetExtensionById(id, true);
252 if (extension) {
253 // Already installed. Skip this install if the current location has
254 // higher priority than |location|.
[email protected]1d5e58b2013-01-31 08:41:40255 Manifest::Location current = extension->location();
256 if (current == Manifest::GetHigherPriorityLocation(current, location))
[email protected]9060d8b02012-01-13 02:14:30257 return false;
[email protected]8a87a5332011-08-11 17:54:59258 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22259 }
[email protected]9060d8b02012-01-13 02:14:30260
261 // Add |id| to the set of pending extensions. If it can not be added,
262 // then there is already a pending record from a higher-priority install
263 // source. In this case, signal that this extension will not be
264 // installed by returning false.
265 if (!pending_extension_manager()->AddFromExternalUpdateUrl(
[email protected]31bb5ee62012-09-12 22:58:40266 id, update_url, location)) {
[email protected]9060d8b02012-01-13 02:14:30267 return false;
[email protected]31bb5ee62012-09-12 22:58:40268 }
[email protected]9060d8b02012-01-13 02:14:30269
[email protected]94fde232012-04-27 10:22:30270 update_once_all_providers_are_ready_ = true;
[email protected]9060d8b02012-01-13 02:14:30271 return true;
[email protected]7a4c6852010-09-16 03:44:22272}
273
[email protected]dc9a74f72012-08-17 18:07:21274const Extension* ExtensionService::GetInstalledApp(const GURL& url) const {
[email protected]615d88f2011-12-13 01:47:44275 const Extension* extension = extensions_.GetExtensionOrAppByURL(
276 ExtensionURLInfo(url));
[email protected]dc9a74f72012-08-17 18:07:21277 return (extension && extension->is_app()) ? extension : NULL;
[email protected]d9696672011-03-15 22:45:09278}
279
[email protected]dc9a74f72012-08-17 18:07:21280bool ExtensionService::IsInstalledApp(const GURL& url) const {
[email protected]d9696672011-03-15 22:45:09281 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27282}
283
[email protected]404fb002012-10-02 21:06:17284const Extension* ExtensionService::GetIsolatedAppForRenderer(
[email protected]dc9a74f72012-08-17 18:07:21285 int renderer_child_id) const {
[email protected]404fb002012-10-02 21:06:17286 std::set<std::string> extension_ids =
287 process_map_.GetExtensionsInProcess(renderer_child_id);
288 // All apps in one process share the same partition.
289 // It is only possible for the app to have isolated storage
290 // if there is only 1 app in the process.
291 if (extension_ids.size() != 1)
292 return NULL;
293
294 const extensions::Extension* extension =
295 extensions_.GetByID(*(extension_ids.begin()));
296 // We still need to check is_storage_isolated(),
297 // because it's common for there to be one extension in a process
298 // with is_storage_isolated() == false.
299 if (extension && extension->is_storage_isolated())
300 return extension;
301
302 return NULL;
[email protected]da5683db2011-04-23 17:12:21303}
304
[email protected]6aeac8342010-10-01 20:21:18305// static
[email protected]d6ebc9792011-04-07 18:18:33306// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30307// --uninstall-extension, and to uninstall an extension via sync. The LOG
308// statements within this function are used to inform the user if the uninstall
309// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00310bool ExtensionService::UninstallExtensionHelper(
311 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18312 const std::string& extension_id) {
[email protected]d6ebc9792011-04-07 18:18:33313 // We can't call UninstallExtension with an invalid extension ID.
[email protected]31bb5ee62012-09-12 22:58:40314 if (!extensions_service->GetInstalledExtension(extension_id)) {
[email protected]6aeac8342010-10-01 20:21:18315 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33316 << "id: " << extension_id;
317 return false;
[email protected]6aeac8342010-10-01 20:21:18318 }
319
[email protected]d6ebc9792011-04-07 18:18:33320 // The following call to UninstallExtension will not allow an uninstall of a
321 // policy-controlled extension.
[email protected]65187152012-06-02 13:14:14322 string16 error;
[email protected]d6ebc9792011-04-07 18:18:33323 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
324 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
325 << ": " << error;
326 return false;
327 }
[email protected]95da88c42011-03-31 10:07:33328
[email protected]6aeac8342010-10-01 20:21:18329 return true;
330}
331
[email protected]eaa7dd182010-12-14 11:09:00332ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24333 const CommandLine* command_line,
[email protected]650b2d52013-02-10 03:41:45334 const base::FilePath& install_directory,
[email protected]45759612012-07-10 17:21:23335 extensions::ExtensionPrefs* extension_prefs,
[email protected]fdd679b2012-11-15 20:49:39336 extensions::Blacklist* blacklist,
[email protected]0436b102011-04-15 18:30:03337 bool autoupdate_enabled,
338 bool extensions_enabled)
[email protected]fdd679b2012-11-15 20:49:39339 : extensions::Blacklist::Observer(blacklist),
340 profile_(profile),
[email protected]bd306722012-07-11 20:43:59341 system_(extensions::ExtensionSystem::Get(profile)),
[email protected]73c47932010-12-06 18:13:43342 extension_prefs_(extension_prefs),
[email protected]695b5712012-12-06 23:55:28343 blacklist_(blacklist),
[email protected]cccdf0aa2011-11-11 03:43:38344 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36345 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23346 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03347 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13348 show_extensions_prompts_(true),
[email protected]fc332ae2012-11-14 20:17:33349 install_updates_when_idle_(true),
[email protected]3ecda252010-11-18 19:50:55350 ready_(false),
[email protected]14908b72011-04-20 06:54:36351 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23352 menu_manager_(profile),
[email protected]8d92e0382012-08-01 18:06:52353 app_notification_manager_(
354 new extensions::AppNotificationManager(profile)),
[email protected]7c1490da2011-10-11 18:53:25355 event_routers_initialized_(false),
[email protected]dff1e042012-04-27 10:59:18356 update_once_all_providers_are_ready_(false),
[email protected]3c4abc82012-10-22 22:25:54357 browser_terminating_(false),
[email protected]399583b2012-12-11 09:33:42358 installs_delayed_(false),
[email protected]1ff4d282012-12-08 00:39:21359 wipeout_is_active_(false),
[email protected]3a2748c42013-01-15 10:13:23360 wipeout_count_(0u),
[email protected]5db9ada2012-04-11 13:48:20361 app_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
362 extension_sync_bundle_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]178f8512012-02-09 01:49:36363 app_shortcut_manager_(profile) {
[email protected]a29a517a2011-01-21 21:11:12364 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20365
[email protected]36a784c2009-06-23 06:21:08366 // Figure out if extension installation should be enabled.
[email protected]31bb5ee62012-09-12 22:58:40367 if (command_line->HasSwitch(switches::kDisableExtensions) ||
368 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
[email protected]6d60703b2009-08-29 01:29:23369 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18370 }
[email protected]36a784c2009-06-23 06:21:08371
[email protected]3c4abc82012-10-22 22:25:54372 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
373 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27374 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07375 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27376 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07377 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27378 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07379 content::NotificationService::AllBrowserContextsAndSources());
[email protected]0db124b02012-11-07 04:55:05380 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
381 content::NotificationService::AllBrowserContextsAndSources());
[email protected]92dd8d92013-02-26 00:41:08382 registrar_.Add(this, chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
383 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28384 pref_change_registrar_.Init(profile->GetPrefs());
[email protected]90bb38d2012-11-14 18:36:03385 base::Closure callback =
386 base::Bind(&ExtensionService::OnExtensionInstallPrefChanged,
387 base::Unretained(this));
388 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, callback);
389 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, callback);
[email protected]e410b5f2012-12-14 14:02:24390 pref_change_registrar_.Add(prefs::kExtensionAllowedTypes, callback);
[email protected]4814b512009-11-07 00:12:29391
[email protected]93fd78f42009-07-10 16:43:17392 // Set up the ExtensionUpdater
393 if (autoupdate_enabled) {
394 int update_frequency = kDefaultUpdateFrequencySeconds;
395 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25396 base::StringToInt(command_line->GetSwitchValueASCII(
397 switches::kExtensionsUpdateFrequency),
398 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17399 }
[email protected]42a08162012-03-16 18:09:11400 updater_.reset(new extensions::ExtensionUpdater(this,
401 extension_prefs,
402 profile->GetPrefs(),
403 profile,
[email protected]fdd679b2012-11-15 20:49:39404 blacklist,
[email protected]42a08162012-03-16 18:09:11405 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17406 }
407
[email protected]25ae0152011-11-18 14:40:02408 component_loader_.reset(
409 new extensions::ComponentLoader(this,
410 profile->GetPrefs(),
411 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14412
[email protected]af9db5f2011-10-05 05:13:15413 app_notification_manager_->Init();
414
[email protected]0436b102011-04-15 18:30:03415 if (extensions_enabled_) {
[email protected]ae4c37e2012-12-21 01:16:25416 CHECK(!ProfileManager::IsImportProcess(*command_line));
417 extensions::ExternalProviderImpl::CreateExternalProviders(
418 this, profile_, &external_extension_providers_);
[email protected]873531342011-03-09 12:16:05419 }
[email protected]b671760b2010-07-15 21:13:47420
[email protected]fb82dcd2012-03-21 14:15:46421 // Set this as the ExtensionService for extension sorting to ensure it
422 // cause syncs if required.
423 extension_prefs_->extension_sorting()->SetExtensionService(this);
424
[email protected]9ce11d22012-08-08 23:20:13425#if defined(ENABLE_EXTENSIONS)
[email protected]c77f2352012-08-08 22:07:58426 extension_action_storage_manager_.reset(
427 new extensions::ExtensionActionStorageManager(profile_));
[email protected]9ce11d22012-08-08 23:20:13428#endif
[email protected]c77f2352012-08-08 22:07:58429
[email protected]1ff4d282012-12-08 00:39:21430 // Before loading any extensions, determine whether Sideload Wipeout is on.
431 wipeout_is_active_ = FeatureSwitch::sideload_wipeout()->IsEnabled() &&
432 !extension_prefs_->GetSideloadWipeoutDone();
433
[email protected]cb0e50312011-05-09 15:03:07434 // How long is the path to the Extensions directory?
435 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
436 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25437}
438
[email protected]84df8332011-12-06 18:22:46439const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45440 return &extensions_;
441}
442
[email protected]84df8332011-12-06 18:22:46443const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45444 return &disabled_extensions_;
445}
446
[email protected]84df8332011-12-06 18:22:46447const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20448 return &terminated_extensions_;
449}
450
[email protected]695b5712012-12-06 23:55:28451const ExtensionSet* ExtensionService::blacklisted_extensions() const {
452 return &blacklisted_extensions_;
453}
454
455scoped_ptr<const ExtensionSet>
456 ExtensionService::GenerateInstalledExtensionsSet() const {
457 scoped_ptr<ExtensionSet> installed_extensions(new ExtensionSet());
[email protected]7f4308d2012-01-18 07:43:01458 installed_extensions->InsertAll(extensions_);
459 installed_extensions->InsertAll(disabled_extensions_);
460 installed_extensions->InsertAll(terminated_extensions_);
[email protected]695b5712012-12-06 23:55:28461 installed_extensions->InsertAll(blacklisted_extensions_);
462 return installed_extensions.PassAs<const ExtensionSet>();
[email protected]7f4308d2012-01-18 07:43:01463}
464
[email protected]695b5712012-12-06 23:55:28465scoped_ptr<const ExtensionSet> ExtensionService::GetWipedOutExtensions() const {
466 scoped_ptr<ExtensionSet> extension_set(new ExtensionSet());
[email protected]215a7be2012-10-22 19:53:42467 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
468 iter != disabled_extensions_.end(); ++iter) {
469 int disabled_reason = extension_prefs_->GetDisableReasons((*iter)->id());
470 if ((disabled_reason & Extension::DISABLE_SIDELOAD_WIPEOUT) != 0)
471 extension_set->Insert(*iter);
472 }
[email protected]695b5712012-12-06 23:55:28473 return extension_set.PassAs<const ExtensionSet>();
[email protected]215a7be2012-10-22 19:53:42474}
475
[email protected]3f213ad2012-07-26 23:39:41476extensions::PendingExtensionManager*
477 ExtensionService::pending_extension_manager() {
[email protected]b2907fd2011-03-25 16:43:37478 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45479}
480
[email protected]eaa7dd182010-12-14 11:09:00481ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17482 // No need to unload extensions here because they are profile-scoped, and the
483 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17484
[email protected]5df038b2012-07-16 19:03:27485 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:14486 for (i = external_extension_providers_.begin();
487 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:27488 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:14489 provider->ServiceShutdown();
490 }
[email protected]6014d672008-12-05 00:38:25491}
492
[email protected]d91268022011-08-26 13:17:37493void ExtensionService::InitEventRoutersAfterImport() {
[email protected]cf593af2011-12-30 05:44:39494 RegisterForImportFinished();
495}
496
497void ExtensionService::RegisterForImportFinished() {
498 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_IMPORT_FINISHED,
499 content::Source<Profile>(profile_))) {
500 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
501 content::Source<Profile>(profile_));
502 }
503}
504
505void ExtensionService::InitAfterImport() {
[email protected]21f2e2d2013-01-23 22:59:52506 startup_metric_utils::ScopedSlowStartupUMA
507 scoped_timer("Startup.SlowStartupExtensionServiceInitAfterImport");
[email protected]eac88332012-12-26 17:57:45508 component_loader_->LoadAll();
[email protected]ae4c37e2012-12-21 01:16:25509
[email protected]cf593af2011-12-30 05:44:39510 CheckForExternalUpdates();
511
512 GarbageCollectExtensions();
513
514 // Idempotent, so although there is a possible race if the import
515 // process finished sometime in the middle of ProfileImpl::InitExtensions,
516 // it cannot happen twice.
517 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37518}
519
[email protected]eaa7dd182010-12-14 11:09:00520void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18521 if (event_routers_initialized_)
522 return;
523
[email protected]6a3cd37e2012-04-17 17:13:34524#if defined(ENABLE_EXTENSIONS)
[email protected]5a38dfd2012-07-23 23:22:10525 browser_event_router_.reset(new extensions::BrowserEventRouter(profile_));
[email protected]6a3cd37e2012-04-17 17:13:34526#endif // defined(ENABLE_EXTENSIONS)
[email protected]b3d62312b12010-10-14 21:10:18527 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37528}
529
[email protected]037228a2012-10-05 01:36:16530void ExtensionService::Shutdown() {
531 // Do nothing for now.
532}
533
[email protected]b2907fd2011-03-25 16:43:37534const Extension* ExtensionService::GetExtensionById(
535 const std::string& id, bool include_disabled) const {
[email protected]81f0d162012-08-15 04:21:47536 int include_mask = INCLUDE_ENABLED;
[email protected]695b5712012-12-06 23:55:28537 if (include_disabled) {
538 // Include blacklisted extensions here because there are hundreds of
539 // callers of this function, and many might assume that this includes those
540 // that have been disabled due to blacklisting.
541 include_mask |= INCLUDE_DISABLED | INCLUDE_BLACKLISTED;
542 }
543 return GetExtensionById(id, include_mask);
544}
545
[email protected]399583b2012-12-11 09:33:42546GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
547 return content::SiteInstance::GetSiteForURL(
548 profile_,
549 Extension::GetBaseURLFromExtensionId(extension_id));
550}
551
[email protected]695b5712012-12-06 23:55:28552const Extension* ExtensionService::GetExtensionById(
553 const std::string& id, int include_mask) const {
554 std::string lowercase_id = StringToLowerASCII(id);
555 if (include_mask & INCLUDE_ENABLED) {
556 const Extension* extension = extensions_.GetByID(lowercase_id);
557 if (extension)
558 return extension;
559 }
560 if (include_mask & INCLUDE_DISABLED) {
561 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
562 if (extension)
563 return extension;
564 }
565 if (include_mask & INCLUDE_TERMINATED) {
566 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
567 if (extension)
568 return extension;
569 }
570 if (include_mask & INCLUDE_BLACKLISTED) {
571 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
572 if (extension)
573 return extension;
574 }
575 return NULL;
[email protected]78994ab02010-12-08 18:06:44576}
577
[email protected]eaa7dd182010-12-14 11:09:00578void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12579 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20580
[email protected]fa6a9102010-11-22 15:38:50581 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17582 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32583
[email protected]ae4c37e2012-12-21 01:16:25584 CHECK(!ProfileManager::IsImportProcess(*CommandLine::ForCurrentProcess()));
585
[email protected]399583b2012-12-11 09:33:42586 // TODO(mek): It might be cleaner to do the FinishDelayedInstallInfo stuff
587 // here instead of in installedloader.
[email protected]ae4c37e2012-12-21 01:16:25588 if (g_browser_process->profile_manager() &&
589 g_browser_process->profile_manager()->will_import()) {
[email protected]eac88332012-12-26 17:57:45590 // Do not load any component extensions, since they may conflict with the
591 // import process.
[email protected]ae4c37e2012-12-21 01:16:25592
[email protected]ae4c37e2012-12-21 01:16:25593 extensions::InstalledLoader(this).LoadAllExtensions();
594 RegisterForImportFinished();
595 } else {
[email protected]eac88332012-12-26 17:57:45596 component_loader_->LoadAll();
[email protected]ae4c37e2012-12-21 01:16:25597 extensions::InstalledLoader(this).LoadAllExtensions();
598
599 // TODO(erikkay) this should probably be deferred to a future point
600 // rather than running immediately at startup.
601 CheckForExternalUpdates();
602
603 // TODO(erikkay) this should probably be deferred as well.
604 GarbageCollectExtensions();
605 }
[email protected]894bb502009-05-21 22:39:57606
[email protected]215a7be2012-10-22 19:53:42607 // The Sideload Wipeout effort takes place during load (see above), so once
608 // that is done the flag can be set so that we don't have to check again.
[email protected]1ff4d282012-12-08 00:39:21609 if (wipeout_is_active_) {
[email protected]215a7be2012-10-22 19:53:42610 extension_prefs_->SetSideloadWipeoutDone();
[email protected]1ff4d282012-12-08 00:39:21611 wipeout_is_active_ = false; // Wipeout is only on during load.
[email protected]3a2748c42013-01-15 10:13:23612 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.SideloadWipeoutNeeded",
613 wipeout_count_ > 0u);
[email protected]1ff4d282012-12-08 00:39:21614 }
[email protected]215a7be2012-10-22 19:53:42615
[email protected]399583b2012-12-11 09:33:42616 if (extension_prefs_->NeedsStorageGarbageCollection()) {
617 GarbageCollectIsolatedStorage();
618 extension_prefs_->SetNeedsStorageGarbageCollection(false);
619 }
[email protected]6014d672008-12-05 00:38:25620}
621
[email protected]31bb5ee62012-09-12 22:58:40622bool ExtensionService::UpdateExtension(const std::string& id,
[email protected]650b2d52013-02-10 03:41:45623 const base::FilePath& extension_path,
[email protected]31bb5ee62012-09-12 22:58:40624 const GURL& download_url,
625 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12626 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3c4abc82012-10-22 22:25:54627 if (browser_terminating_) {
628 LOG(WARNING) << "Skipping UpdateExtension due to browser shutdown";
629 // Leak the temp file at extension_path. We don't want to add to the disk
630 // I/O burden at shutdown, we can't rely on the I/O completing anyway, and
631 // the file is in the OS temp directory which should be cleaned up for us.
632 return false;
633 }
[email protected]a8af9fdb2010-10-28 21:52:20634
[email protected]3f213ad2012-07-26 23:39:41635 const extensions::PendingExtensionInfo* pending_extension_info =
[email protected]51a3bf8b2012-06-08 22:53:06636 pending_extension_manager()->GetById(id);
[email protected]8ef78fd2010-08-19 17:14:32637
[email protected]695b5712012-12-06 23:55:28638 const Extension* extension = GetInstalledExtension(id);
[email protected]51a3bf8b2012-06-08 22:53:06639 if (!pending_extension_info && !extension) {
[email protected]aa142702010-03-26 01:26:33640 LOG(WARNING) << "Will not update extension " << id
641 << " because it is not installed or pending";
642 // Delete extension_path since we're not creating a CrxInstaller
643 // that would do it for us.
[email protected]7f8f24f2012-11-15 19:40:14644 if (!GetFileTaskRunner()->PostTask(
645 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36646 base::Bind(
[email protected]7296f2762011-11-21 19:23:44647 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36648 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03649
650 return false;
[email protected]e957fe52009-06-23 16:51:05651 }
652
[email protected]aa142702010-03-26 01:26:33653 // We want a silent install only for non-pending extensions and
654 // pending extensions that have install_silently set.
[email protected]91e51d612012-10-21 23:03:05655 ExtensionInstallPrompt* client = NULL;
656 if (pending_extension_info && !pending_extension_info->install_silently())
657 client = ExtensionInstallUI::CreateInstallPromptWithProfile(profile_);
[email protected]aa142702010-03-26 01:26:33658
[email protected]d8c8f25f2011-11-02 18:18:01659 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16660 installer->set_expected_id(id);
[email protected]51a3bf8b2012-06-08 22:53:06661 if (pending_extension_info) {
662 installer->set_install_source(pending_extension_info->install_source());
663 if (pending_extension_info->install_silently())
664 installer->set_allow_silent_install(true);
665 } else if (extension) {
[email protected]5eb375e92010-11-26 07:50:41666 installer->set_install_source(extension->location());
[email protected]51a3bf8b2012-06-08 22:53:06667 }
[email protected]7d8b7072012-04-07 01:07:46668 // If the extension was installed from or has migrated to the webstore, or
[email protected]fcb2c2c2012-10-17 21:08:45669 // its auto-update URL is from the webstore, treat it as a webstore install.
670 // Note that we ignore some older extensions with blank auto-update URLs
671 // because we are mostly concerned with restrictions on NaCl extensions,
672 // which are newer.
[email protected]a12ce8b22012-01-17 18:40:53673 int creation_flags = Extension::NO_FLAGS;
[email protected]75764512011-12-19 19:54:28674 if ((extension && extension->from_webstore()) ||
[email protected]7d8b7072012-04-07 01:07:46675 (extension && extension->UpdatesFromGallery()) ||
[email protected]963d13c2012-09-29 17:13:35676 (!extension && extension_urls::IsWebstoreUpdateUrl(
[email protected]51a3bf8b2012-06-08 22:53:06677 pending_extension_info->update_url()))) {
[email protected]a12ce8b22012-01-17 18:40:53678 creation_flags |= Extension::FROM_WEBSTORE;
679 }
680
681 // Bookmark apps being updated is kind of a contradiction, but that's because
682 // we mark the default apps as bookmark apps, and they're hosted in the web
683 // store, thus they can get updated. See http://crbug.com/101605 for more
684 // details.
685 if (extension && extension->from_bookmark())
686 creation_flags |= Extension::FROM_BOOKMARK;
687
[email protected]e33bbc22012-08-27 22:05:46688 if (extension && extension->was_installed_by_default())
689 creation_flags |= Extension::WAS_INSTALLED_BY_DEFAULT;
690
[email protected]a12ce8b22012-01-17 18:40:53691 installer->set_creation_flags(creation_flags);
692
[email protected]6dfbbf82010-03-12 23:09:16693 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06694 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07695 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16696 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03697
698 if (out_crx_installer)
699 *out_crx_installer = installer;
700
701 return true;
[email protected]e957fe52009-06-23 16:51:05702}
703
[email protected]eaa7dd182010-12-14 11:09:00704void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]32b36c62012-10-24 05:37:41705 int events = HasShellWindows(extension_id) ? EVENT_LAUNCHED : EVENT_NONE;
706 ReloadExtensionWithEvents(extension_id, events);
707}
708
709void ExtensionService::RestartExtension(const std::string& extension_id) {
[email protected]2380c6b2013-01-09 02:33:13710 ReloadExtensionWithEvents(extension_id, EVENT_RESTARTED);
[email protected]32b36c62012-10-24 05:37:41711}
712
713void ExtensionService::ReloadExtensionWithEvents(
714 const std::string& extension_id,
715 int events) {
[email protected]a29a517a2011-01-21 21:11:12716 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]650b2d52013-02-10 03:41:45717 base::FilePath path;
[email protected]9adb9692010-10-29 23:14:02718 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40719
[email protected]f17dbd42010-08-16 23:21:10720 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06721 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29722 // If the extension has an inspector open for its background page, detach
723 // the inspector and hang onto a cookie for it, so that we can reattach
724 // later.
[email protected]31d8f5f22012-04-02 15:22:08725 // TODO(yoz): this is not incognito-safe!
726 ExtensionProcessManager* manager = system_->process_manager();
[email protected]3a1dc572012-07-31 22:25:13727 extensions::ExtensionHost* host =
728 manager->GetBackgroundHostForExtension(extension_id);
[email protected]98f66112012-12-25 12:59:36729 if (host && DevToolsAgentHost::HasFor(host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29730 // Look for an open inspector for the background page.
[email protected]98f66112012-12-25 12:59:36731 int devtools_cookie = DevToolsAgentHost::DisconnectRenderViewHost(
[email protected]562e20c2012-12-24 18:27:20732 host->render_view_host());
[email protected]4814b512009-11-07 00:12:29733 if (devtools_cookie >= 0)
734 orphaned_dev_tools_[extension_id] = devtools_cookie;
735 }
736
[email protected]b65272f2009-08-31 15:47:06737 path = current_extension->path();
[email protected]44d62b62012-04-11 00:06:03738 DisableExtension(extension_id, Extension::DISABLE_RELOAD);
[email protected]f17dbd42010-08-16 23:21:10739 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16740 } else {
741 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06742 }
743
[email protected]8b6d9642013-02-01 03:37:40744 on_load_events_[extension_id] = events;
745
[email protected]399583b2012-12-11 09:33:42746 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]6f6101832012-11-27 22:10:48747 FinishDelayedInstallation(extension_id);
[email protected]0db124b02012-11-07 04:55:05748 return;
749 }
750
[email protected]43ceb002012-02-10 23:19:15751 // If we're reloading a component extension, use the component extension
752 // loader's reloader.
753 if (component_loader_->Exists(extension_id)) {
754 component_loader_->Reload(extension_id);
755 return;
756 }
757
[email protected]e6090e42010-03-23 22:44:08758 // Check the installed extensions to see if what we're reloading was already
759 // installed.
760 scoped_ptr<ExtensionInfo> installed_extension(
761 extension_prefs_->GetInstalledExtensionInfo(extension_id));
762 if (installed_extension.get() &&
763 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01764 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08765 } else {
[email protected]d8c8f25f2011-11-02 18:18:01766 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08767 // We should always be able to remember the extension's path. If it's not in
768 // the map, someone failed to update |unloaded_extension_paths_|.
769 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01770 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08771 }
[email protected]9cddd4702009-07-27 22:09:40772}
773
[email protected]fa2416f2011-05-03 08:41:20774bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08775 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20776 bool external_uninstall,
[email protected]65187152012-06-02 13:14:14777 string16* error) {
[email protected]a29a517a2011-01-21 21:11:12778 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20779
[email protected]0d6ec3a72011-09-02 02:09:43780 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25781
[email protected]e7afe2452010-08-22 16:19:13782 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48783 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32784
[email protected]95da88c42011-03-31 10:07:33785 // Policy change which triggers an uninstall will always set
786 // |external_uninstall| to true so this is the only way to uninstall
787 // managed extensions.
[email protected]65187152012-06-02 13:14:14788 if (!external_uninstall &&
789 !system_->management_policy()->UserMayModifySettings(
790 extension.get(), error)) {
[email protected]ad50def52011-10-19 23:17:07791 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27792 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53793 content::Source<Profile>(profile_),
794 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33795 return false;
796 }
[email protected]95da88c42011-03-31 10:07:33797
[email protected]3bdba0d2011-08-23 07:17:30798 // Extract the data we need for sync now, but don't actually sync until we've
799 // completed the uninstallation.
[email protected]65f173552012-06-28 22:43:58800 syncer::SyncChange sync_change;
[email protected]5db9ada2012-04-11 13:48:20801 if (app_sync_bundle_.HandlesApp(*extension)) {
802 sync_change = app_sync_bundle_.CreateSyncChangeToDelete(extension);
803 } else if (extension_sync_bundle_.HandlesExtension(*extension)) {
804 sync_change = extension_sync_bundle_.CreateSyncChangeToDelete(extension);
[email protected]3bdba0d2011-08-23 07:17:30805 }
806
[email protected]d96eb512012-11-01 23:44:08807 if (IsUnacknowledgedExternalExtension(extension)) {
808 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
809 EXTERNAL_EXTENSION_UNINSTALLED,
810 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
811 }
[email protected]9b217652010-10-08 22:04:23812 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08813 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49814 RecordPermissionMessagesHistogram(
815 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23816
[email protected]831aa212010-03-26 13:55:19817 // Unload before doing more cleanup to ensure that nothing is hanging on to
818 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59819 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19820
[email protected]0d6ec3a72011-09-02 02:09:43821 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19822 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32823
824 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]12075d12013-02-27 05:38:05825 if (!Manifest::IsUnpackedLocation(extension->location())) {
[email protected]7f8f24f2012-11-15 19:40:14826 if (!GetFileTaskRunner()->PostTask(
827 FROM_HERE,
[email protected]53612e82011-10-18 18:00:36828 base::Bind(
[email protected]14908b72011-04-20 06:54:36829 &extension_file_util::UninstallExtension,
830 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20831 extension_id)))
[email protected]14908b72011-04-20 06:54:36832 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32833 }
834
[email protected]0d6ec3a72011-09-02 02:09:43835 GURL launch_web_url_origin(extension->launch_web_url());
836 launch_web_url_origin = launch_web_url_origin.GetOrigin();
[email protected]605fb8102012-05-04 01:36:55837 bool is_storage_isolated = extension->is_storage_isolated();
[email protected]0d6ec3a72011-09-02 02:09:43838
[email protected]399583b2012-12-11 09:33:42839 if (is_storage_isolated) {
840 BrowserContext::AsyncObliterateStoragePartition(
841 profile_,
842 GetSiteForExtensionId(extension_id),
843 base::Bind(&ExtensionService::OnNeedsToGarbageCollectIsolatedStorage,
844 AsWeakPtr()));
845 } else {
846 if (extension->is_hosted_app() &&
847 !profile_->GetExtensionSpecialStoragePolicy()->
848 IsStorageProtected(launch_web_url_origin)) {
849 extensions::DataDeleter::StartDeleting(
850 profile_, extension_id, launch_web_url_origin);
851 }
852 extensions::DataDeleter::StartDeleting(profile_, extension_id,
853 extension->url());
[email protected]0d6ec3a72011-09-02 02:09:43854 }
[email protected]0d6ec3a72011-09-02 02:09:43855
[email protected]0dfe05c2011-02-23 23:03:36856 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06857
858 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07859 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27860 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53861 content::Source<Profile>(profile_),
[email protected]d67d9412012-07-17 17:19:42862 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33863
[email protected]5db9ada2012-04-11 13:48:20864 if (app_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32865 sync_change.sync_data().GetDataType() == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:20866 app_sync_bundle_.ProcessDeletion(extension_id, sync_change);
867 } else if (extension_sync_bundle_.HasExtensionId(extension_id) &&
[email protected]a4a147652012-07-03 23:41:32868 sync_change.sync_data().GetDataType() == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:20869 extension_sync_bundle_.ProcessDeletion(extension_id, sync_change);
[email protected]3bdba0d2011-08-23 07:17:30870 }
871
[email protected]399583b2012-12-11 09:33:42872 delayed_updates_for_idle_.Remove(extension_id);
873 delayed_installs_.Remove(extension_id);
[email protected]0db124b02012-11-07 04:55:05874
[email protected]333b1de2011-09-12 18:28:50875 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18876 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
877
[email protected]d6ebc9792011-04-07 18:18:33878 return true;
[email protected]c10da4b02010-03-25 14:38:32879}
880
[email protected]c3cfb012011-04-06 22:07:35881bool ExtensionService::IsExtensionEnabled(
882 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46883 if (extensions_.Contains(extension_id) ||
[email protected]dc9a74f72012-08-17 18:07:21884 terminated_extensions_.Contains(extension_id)) {
[email protected]36429da2011-07-11 20:25:18885 return true;
[email protected]dc9a74f72012-08-17 18:07:21886 }
[email protected]36429da2011-07-11 20:25:18887
[email protected]695b5712012-12-06 23:55:28888 if (disabled_extensions_.Contains(extension_id) ||
889 blacklisted_extensions_.Contains(extension_id)) {
[email protected]ad83ca242011-07-29 01:32:25890 return false;
[email protected]695b5712012-12-06 23:55:28891 }
[email protected]ad83ca242011-07-29 01:32:25892
893 // If the extension hasn't been loaded yet, check the prefs for it. Assume
894 // enabled unless otherwise noted.
895 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
[email protected]f574c402012-12-04 23:20:31896 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35897}
898
899bool ExtensionService::IsExternalExtensionUninstalled(
900 const std::string& extension_id) const {
901 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
902}
903
[email protected]e2377032013-01-18 17:53:22904bool ExtensionService::IsExtensionEnabledForLauncher(
905 const std::string& extension_id) const {
906 return IsExtensionEnabled(extension_id) &&
907 !GetTerminatedExtension(extension_id);
908}
909
[email protected]eaa7dd182010-12-14 11:09:00910void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12911 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20912
[email protected]06f92562011-04-29 19:27:31913 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39914 return;
[email protected]0c6da502009-08-14 22:32:39915
[email protected]c0231d72012-11-26 22:37:57916 int disable_reasons = extension_prefs_->GetDisableReasons(extension_id);
[email protected]b3317ad2011-04-28 23:46:00917 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]eb5e4f92012-08-15 23:33:28918 extension_prefs_->ClearDisableReasons(extension_id);
[email protected]1784e83a2009-09-08 21:01:52919
[email protected]695b5712012-12-06 23:55:28920 const Extension* extension = disabled_extensions_.GetByID(extension_id);
[email protected]06f92562011-04-29 19:27:31921 // This can happen if sync enables an extension that is not
922 // installed yet.
923 if (!extension)
924 return;
925
[email protected]d96eb512012-11-01 23:44:08926 if (IsUnacknowledgedExternalExtension(extension)) {
927 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
928 EXTERNAL_EXTENSION_REENABLED,
929 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
[email protected]612a1cb12012-10-17 13:18:03930 AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:08931 }
[email protected]612a1cb12012-10-17 13:18:03932
[email protected]c0231d72012-11-26 22:37:57933 if (disable_reasons & Extension::DISABLE_SIDELOAD_WIPEOUT)
[email protected]94e14592012-11-19 10:49:42934 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.ExtensionWipedStatus", false);
935
[email protected]0c6da502009-08-14 22:32:39936 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46937 extensions_.Insert(make_scoped_refptr(extension));
938 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39939
[email protected]62d30f42009-10-01 22:36:06940 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30941
[email protected]f74d7f32012-06-19 19:22:51942 // Notify listeners that the extension was enabled.
943 content::NotificationService::current()->Notify(
944 chrome::NOTIFICATION_EXTENSION_ENABLED,
945 content::Source<Profile>(profile_),
946 content::Details<const Extension>(extension));
947
[email protected]3bdba0d2011-08-23 07:17:30948 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39949}
950
[email protected]44d62b62012-04-11 00:06:03951void ExtensionService::DisableExtension(
952 const std::string& extension_id,
953 Extension::DisableReason disable_reason) {
[email protected]a29a517a2011-01-21 21:11:12954 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20955
[email protected]b2ba9962009-12-10 20:10:15956 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31957 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52958 return;
[email protected]1784e83a2009-09-08 21:01:52959
[email protected]06f92562011-04-29 19:27:31960 const Extension* extension = GetInstalledExtension(extension_id);
961 // |extension| can be NULL if sync disables an extension that is not
962 // installed yet.
[email protected]65187152012-06-02 13:14:14963 if (extension &&
964 !system_->management_policy()->UserMayModifySettings(extension, NULL)) {
[email protected]95da88c42011-03-31 10:07:33965 return;
[email protected]65187152012-06-02 13:14:14966 }
[email protected]95da88c42011-03-31 10:07:33967
[email protected]b3317ad2011-04-28 23:46:00968 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]eb5e4f92012-08-15 23:33:28969 extension_prefs_->AddDisableReason(extension_id, disable_reason);
[email protected]1784e83a2009-09-08 21:01:52970
[email protected]695b5712012-12-06 23:55:28971 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_DISABLED;
972 extension = GetExtensionById(extension_id, include_mask);
[email protected]06f92562011-04-29 19:27:31973 if (!extension)
974 return;
975
[email protected]5c094792012-09-07 19:44:53976 // Move it over to the disabled list. Don't send a second unload notification
977 // for terminated extensions being disabled.
[email protected]84df8332011-12-06 18:22:46978 disabled_extensions_.Insert(make_scoped_refptr(extension));
[email protected]5c094792012-09-07 19:44:53979 if (extensions_.Contains(extension->id())) {
[email protected]84df8332011-12-06 18:22:46980 extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53981 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
982 } else {
[email protected]84df8332011-12-06 18:22:46983 terminated_extensions_.Remove(extension->id());
[email protected]5c094792012-09-07 19:44:53984 }
[email protected]3bdba0d2011-08-23 07:17:30985
986 SyncExtensionChangeIfNeeded(*extension);
[email protected]1784e83a2009-09-08 21:01:52987}
988
[email protected]eaa7dd182010-12-14 11:09:00989void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]b70a2d92012-06-28 19:51:21990 const Extension* extension, bool record_oauth2_grant) {
[email protected]be083862012-09-01 03:53:45991 GrantPermissions(extension, record_oauth2_grant);
[email protected]fe2dd7742011-04-19 22:52:49992 RecordPermissionMessagesHistogram(
993 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25994 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
995 EnableExtension(extension->id());
996}
997
[email protected]be083862012-09-01 03:53:45998void ExtensionService::GrantPermissions(const Extension* extension,
999 bool record_oauth2_grant) {
1000 CHECK(extension);
1001 extensions::PermissionsUpdater perms_updater(profile());
1002 perms_updater.GrantActivePermissions(extension, record_oauth2_grant);
1003}
1004
[email protected]fe2dd7742011-04-19 22:52:491005// static
1006void ExtensionService::RecordPermissionMessagesHistogram(
1007 const Extension* e, const char* histogram) {
[email protected]de415552013-01-23 04:12:171008 // Since this is called from multiple sources, and since the histogram macros
1009 // use statics, we need to manually lookup the histogram ourselves.
1010 base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
[email protected]fe2dd7742011-04-19 22:52:491011 histogram,
1012 1,
[email protected]c2e66e12012-06-27 06:27:061013 PermissionMessage::kEnumBoundary,
1014 PermissionMessage::kEnumBoundary + 1,
[email protected]de415552013-01-23 04:12:171015 base::HistogramBase::kUmaTargetedHistogramFlag);
[email protected]fe2dd7742011-04-19 22:52:491016
[email protected]c2e66e12012-06-27 06:27:061017 PermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:491018 if (permissions.empty()) {
[email protected]c2e66e12012-06-27 06:27:061019 counter->Add(PermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491020 } else {
[email protected]c2e66e12012-06-27 06:27:061021 for (PermissionMessages::iterator it = permissions.begin();
[email protected]0d3e4a22011-06-23 19:02:521022 it != permissions.end(); ++it)
1023 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491024 }
1025}
1026
[email protected]eaa7dd182010-12-14 11:09:001027void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421028 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061029 // was loaded, otherwise a race can arise where a renderer that is created
1030 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421031 // that the request context doesn't yet know about. The profile is responsible
1032 // for ensuring its URLRequestContexts appropriately discover the loaded
1033 // extension.
[email protected]31d8f5f22012-04-02 15:22:081034 system_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061035
[email protected]d5949312012-12-03 22:13:301036 // Tell renderers about the new extension, unless it's a theme (renderers
1037 // don't need to know about themes).
[email protected]a58f599c2012-12-01 03:08:191038 if (!extension->is_theme()) {
[email protected]19647262011-12-16 09:57:491039 for (content::RenderProcessHost::iterator i(
1040 content::RenderProcessHost::AllHostsIterator());
1041 !i.IsAtEnd(); i.Advance()) {
1042 content::RenderProcessHost* host = i.GetCurrentValue();
1043 Profile* host_profile =
1044 Profile::FromBrowserContext(host->GetBrowserContext());
1045 if (host_profile->GetOriginalProfile() ==
1046 profile_->GetOriginalProfile()) {
1047 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
1048 1, ExtensionMsg_Loaded_Params(extension));
1049 host->Send(
1050 new ExtensionMsg_Loaded(loaded_extensions));
1051 }
[email protected]c8d407e2011-04-28 21:27:171052 }
[email protected]77a6970c2011-04-23 16:58:561053 }
1054
[email protected]3442a662012-01-12 08:06:171055 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1056 // extension.
1057 //
1058 // NOTE: It is important that this happen after notifying the renderers about
1059 // the new extensions so that if we navigate to an extension URL in
1060 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
1061 content::NotificationService::current()->Notify(
1062 chrome::NOTIFICATION_EXTENSION_LOADED,
1063 content::Source<Profile>(profile_),
1064 content::Details<const Extension>(extension));
1065
[email protected]c8d407e2011-04-28 21:27:171066 // Tell a random-ass collection of other subsystems about the new extension.
1067 // TODO(aa): What should we do with all this goop? Can it move into the
1068 // relevant objects via EXTENSION_LOADED?
1069
1070 profile_->GetExtensionSpecialStoragePolicy()->
1071 GrantRightsForExtension(extension);
1072
1073 UpdateActiveExtensionsInCrashReporter();
1074
[email protected]c8d407e2011-04-28 21:27:171075 // If the extension has permission to load chrome://favicon/ resources we need
1076 // to make sure that the FaviconSource is registered with the
1077 // ChromeURLDataManager.
1078 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1079 FaviconSource* favicon_source = new FaviconSource(profile_,
1080 FaviconSource::FAVICON);
[email protected]24ea7a12013-01-27 23:54:531081 content::URLDataSource::Add(profile_, favicon_source);
[email protected]c8d407e2011-04-28 21:27:171082 }
[email protected]b07e606e2012-09-15 20:16:151083
1084#if !defined(OS_ANDROID)
1085 // Same for chrome://theme/ resources.
1086 if (extension->HasHostPermission(GURL(chrome::kChromeUIThemeURL))) {
1087 ThemeSource* theme_source = new ThemeSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531088 content::URLDataSource::Add(profile_, theme_source);
[email protected]b07e606e2012-09-15 20:16:151089 }
1090#endif
1091
[email protected]5eddc3e2011-10-26 04:33:311092 // Same for chrome://thumb/ resources.
1093 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
1094 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
[email protected]24ea7a12013-01-27 23:54:531095 content::URLDataSource::Add(profile_, thumbnail_source);
[email protected]5eddc3e2011-10-26 04:33:311096 }
[email protected]c8d407e2011-04-28 21:27:171097
[email protected]ebd71962012-12-20 02:56:551098#if defined(ENABLE_PLUGINS)
[email protected]c8d407e2011-04-28 21:27:171099 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581100 bool plugins_changed = false;
1101 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1102 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141103 PluginService::GetInstance()->RefreshPlugins();
1104 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581105 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491106 ChromePluginServiceFilter* filter =
1107 ChromePluginServiceFilter::GetInstance();
1108 if (plugin.is_public) {
1109 filter->RestrictPluginToProfileAndOrigin(
1110 plugin.path, profile_, GURL());
1111 } else {
1112 filter->RestrictPluginToProfileAndOrigin(
1113 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581114 }
1115 }
[email protected]84396dbc2011-04-14 06:33:421116
1117 bool nacl_modules_changed = false;
1118 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1119 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001120 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421121 nacl_modules_changed = true;
1122 }
1123
[email protected]ed0ba002011-05-26 16:55:131124 if (nacl_modules_changed)
1125 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421126
1127 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021128 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551129#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061130}
1131
[email protected]a9f39a312010-12-23 22:14:271132void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591133 const Extension* extension,
1134 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271135 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071136 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271137 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531138 content::Source<Profile>(profile_),
1139 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061140
[email protected]903d70a2012-12-04 13:00:171141#if defined(ENABLE_THEMES)
1142 // If the current theme is being unloaded, tell ThemeService to revert back
1143 // to the default theme.
1144 if (reason != extension_misc::UNLOAD_REASON_UPDATE && extension->is_theme()) {
1145 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
1146 if (extension->id() == theme_service->GetThemeID())
1147 theme_service->UseDefaultTheme();
1148 }
1149#endif
1150
[email protected]f3b1a082011-11-18 00:34:301151 for (content::RenderProcessHost::iterator i(
1152 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561153 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301154 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081155 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301156 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081157 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171158 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561159 }
1160
[email protected]31d8f5f22012-04-02 15:22:081161 system_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171162 profile_->GetExtensionSpecialStoragePolicy()->
1163 RevokeRightsForExtension(extension);
1164
[email protected]b777b332011-04-16 04:01:081165#if defined(OS_CHROMEOS)
[email protected]ac66e452013-01-11 09:21:231166 // Revoke external file access for the extension from its file system context.
1167 // It is safe to access the extension's storage partition at this point. The
1168 // storage partition may get destroyed only after the extension gets unloaded.
1169 GURL site = extensions::ExtensionSystem::Get(profile_)->extension_service()->
1170 GetSiteForExtensionId(extension->id());
[email protected]10eb28162012-09-18 03:04:091171 fileapi::FileSystemContext* filesystem_context =
[email protected]ac66e452013-01-11 09:21:231172 BrowserContext::GetStoragePartitionForSite(profile_, site)->
[email protected]10eb28162012-09-18 03:04:091173 GetFileSystemContext();
1174 if (filesystem_context && filesystem_context->external_provider()) {
1175 filesystem_context->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171176 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061177 }
[email protected]c8d407e2011-04-28 21:27:171178#endif
1179
1180 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581181
[email protected]ebd71962012-12-20 02:56:551182#if defined(ENABLE_PLUGINS)
[email protected]2de307592011-04-05 21:16:581183 bool plugins_changed = false;
1184 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1185 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]b6a2f8de2012-01-31 17:28:491186 PluginService::GetInstance()->ForcePluginShutdown(plugin.path);
[email protected]f520b5b2011-11-08 02:42:141187 PluginService::GetInstance()->RefreshPlugins();
1188 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581189 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491190 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581191 }
[email protected]84396dbc2011-04-14 06:33:421192
1193 bool nacl_modules_changed = false;
1194 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1195 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001196 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421197 nacl_modules_changed = true;
1198 }
1199
[email protected]ed0ba002011-05-26 16:55:131200 if (nacl_modules_changed)
1201 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421202
1203 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021204 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]ebd71962012-12-20 02:56:551205#endif // defined(ENABLE_PLUGINS)
[email protected]62d30f42009-10-01 22:36:061206}
1207
[email protected]0a071a32011-02-08 00:18:241208Profile* ExtensionService::profile() {
1209 return profile_;
1210}
1211
[email protected]45759612012-07-10 17:21:231212extensions::ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451213 return extension_prefs_;
1214}
1215
[email protected]0d9a2202011-11-09 13:48:411216extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381217 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201218}
1219
[email protected]e2d720aa2012-05-17 00:11:351220extensions::ContentSettingsStore* ExtensionService::GetContentSettingsStore() {
[email protected]b790b072011-05-20 09:46:441221 return extension_prefs()->content_settings_store();
1222}
1223
[email protected]25ae0152011-11-18 14:40:021224bool ExtensionService::is_ready() {
1225 return ready_;
1226}
1227
[email protected]7f8f24f2012-11-15 19:40:141228base::SequencedTaskRunner* ExtensionService::GetFileTaskRunner() {
1229 if (file_task_runner_)
1230 return file_task_runner_;
1231
1232 // We should be able to interrupt any part of extension install process during
1233 // shutdown. SKIP_ON_SHUTDOWN ensures that not started extension install tasks
1234 // will be ignored/deleted while we will block on started tasks.
1235 std::string token("ext_install-");
1236 token.append(profile_->GetPath().AsUTF8Unsafe());
1237 file_task_runner_ = BrowserThread::GetBlockingPool()->
1238 GetSequencedTaskRunnerWithShutdownBehavior(
1239 BrowserThread::GetBlockingPool()->GetNamedSequenceToken(token),
1240 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
1241 return file_task_runner_;
1242}
1243
[email protected]42a08162012-03-16 18:09:111244extensions::ExtensionUpdater* ExtensionService::updater() {
[email protected]2859946f2011-04-04 18:18:061245 return updater_.get();
1246}
1247
[email protected]4ee07c62012-08-21 12:40:421248void ExtensionService::CheckManagementPolicy() {
[email protected]aa96d3a2010-08-21 08:45:251249 std::vector<std::string> to_be_removed;
[email protected]695b5712012-12-06 23:55:281250
[email protected]aa96d3a2010-08-21 08:45:251251 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461252 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251253 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021254 const Extension* extension = (*iter);
[email protected]4ee07c62012-08-21 12:40:421255 if (!system_->management_policy()->UserMayLoad(extension, NULL))
[email protected]aa96d3a2010-08-21 08:45:251256 to_be_removed.push_back(extension->id());
1257 }
1258
1259 // UnloadExtension will change the extensions_ list. So, we should
1260 // call it outside the iterator loop.
[email protected]31bb5ee62012-09-12 22:58:401261 for (size_t i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591262 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251263}
1264
[email protected]31206602011-04-13 23:07:321265void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351266 if (updater()) {
[email protected]94fde232012-04-27 10:22:301267 if (AreAllExternalProvidersReady()) {
1268 updater()->CheckSoon();
1269 } else {
1270 // Sync can start updating before all the external providers are ready
1271 // during startup. Start the update as soon as those providers are ready,
1272 // but not before.
1273 update_once_all_providers_are_ready_ = true;
1274 }
[email protected]c3cfb012011-04-06 22:07:351275 } else {
[email protected]31206602011-04-13 23:07:321276 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351277 }
1278}
1279
[email protected]3f98fd92012-11-17 15:54:291280syncer::SyncMergeResult ExtensionService::MergeDataAndStartSyncing(
[email protected]a4a147652012-07-03 23:41:321281 syncer::ModelType type,
[email protected]65f173552012-06-28 22:43:581282 const syncer::SyncDataList& initial_sync_data,
1283 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1284 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
[email protected]5db9ada2012-04-11 13:48:201285 CHECK(sync_processor.get());
[email protected]5d36e052012-04-20 20:12:281286 CHECK(sync_error_factory.get());
[email protected]3bdba0d2011-08-23 07:17:301287
1288 switch (type) {
[email protected]a4a147652012-07-03 23:41:321289 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201290 extension_sync_bundle_.SetupSync(sync_processor.release(),
[email protected]5d36e052012-04-20 20:12:281291 sync_error_factory.release(),
[email protected]5db9ada2012-04-11 13:48:201292 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301293 break;
1294
[email protected]a4a147652012-07-03 23:41:321295 case syncer::APPS:
[email protected]5d36e052012-04-20 20:12:281296 app_sync_bundle_.SetupSync(sync_processor.release(),
1297 sync_error_factory.release(),
1298 initial_sync_data);
[email protected]3bdba0d2011-08-23 07:17:301299 break;
1300
1301 default:
1302 LOG(FATAL) << "Got " << type << " ModelType";
1303 }
[email protected]3bdba0d2011-08-23 07:17:301304
[email protected]b1417ab2011-12-15 17:09:111305 // Process local extensions.
1306 // TODO(yoz): Determine whether pending extensions should be considered too.
1307 // See crbug.com/104399.
[email protected]65f173552012-06-28 22:43:581308 syncer::SyncDataList sync_data_list = GetAllSyncData(type);
1309 syncer::SyncChangeList sync_change_list;
1310 for (syncer::SyncDataList::const_iterator i = sync_data_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301311 i != sync_data_list.end();
1312 ++i) {
[email protected]5db9ada2012-04-11 13:48:201313 switch (type) {
[email protected]a4a147652012-07-03 23:41:321314 case syncer::EXTENSIONS:
[email protected]5db9ada2012-04-11 13:48:201315 sync_change_list.push_back(
1316 extension_sync_bundle_.CreateSyncChange(*i));
1317 break;
[email protected]a4a147652012-07-03 23:41:321318 case syncer::APPS:
[email protected]5db9ada2012-04-11 13:48:201319 sync_change_list.push_back(app_sync_bundle_.CreateSyncChange(*i));
1320 break;
1321 default:
1322 LOG(FATAL) << "Got " << type << " ModelType";
[email protected]31bb5ee62012-09-12 22:58:401323 }
[email protected]3bdba0d2011-08-23 07:17:301324 }
[email protected]3bdba0d2011-08-23 07:17:301325
[email protected]5db9ada2012-04-11 13:48:201326
[email protected]a4a147652012-07-03 23:41:321327 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201328 extension_sync_bundle_.ProcessSyncChangeList(sync_change_list);
[email protected]a4a147652012-07-03 23:41:321329 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201330 app_sync_bundle_.ProcessSyncChangeList(sync_change_list);
1331 }
[email protected]0ffd9e52012-01-26 21:44:341332
[email protected]3f98fd92012-11-17 15:54:291333 return syncer::SyncMergeResult(type);
[email protected]3bdba0d2011-08-23 07:17:301334}
1335
[email protected]a4a147652012-07-03 23:41:321336void ExtensionService::StopSyncing(syncer::ModelType type) {
1337 if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201338 app_sync_bundle_.Reset();
[email protected]a4a147652012-07-03 23:41:321339 } else if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201340 extension_sync_bundle_.Reset();
1341 }
[email protected]3bdba0d2011-08-23 07:17:301342}
1343
[email protected]65f173552012-06-28 22:43:581344syncer::SyncDataList ExtensionService::GetAllSyncData(
[email protected]a4a147652012-07-03 23:41:321345 syncer::ModelType type) const {
[email protected]31bb5ee62012-09-12 22:58:401346 if (type == syncer::EXTENSIONS)
[email protected]5db9ada2012-04-11 13:48:201347 return extension_sync_bundle_.GetAllSyncData();
[email protected]31bb5ee62012-09-12 22:58:401348 if (type == syncer::APPS)
[email protected]5db9ada2012-04-11 13:48:201349 return app_sync_bundle_.GetAllSyncData();
[email protected]5db9ada2012-04-11 13:48:201350
1351 // We should only get sync data for extensions and apps.
1352 NOTREACHED();
1353
[email protected]65f173552012-06-28 22:43:581354 return syncer::SyncDataList();
[email protected]3bdba0d2011-08-23 07:17:301355}
1356
[email protected]65f173552012-06-28 22:43:581357syncer::SyncError ExtensionService::ProcessSyncChanges(
[email protected]3bdba0d2011-08-23 07:17:301358 const tracked_objects::Location& from_here,
[email protected]65f173552012-06-28 22:43:581359 const syncer::SyncChangeList& change_list) {
1360 for (syncer::SyncChangeList::const_iterator i = change_list.begin();
[email protected]3bdba0d2011-08-23 07:17:301361 i != change_list.end();
1362 ++i) {
[email protected]a4a147652012-07-03 23:41:321363 syncer::ModelType type = i->sync_data().GetDataType();
1364 if (type == syncer::EXTENSIONS) {
[email protected]5db9ada2012-04-11 13:48:201365 extension_sync_bundle_.ProcessSyncChange(
1366 extensions::ExtensionSyncData(*i));
[email protected]a4a147652012-07-03 23:41:321367 } else if (type == syncer::APPS) {
[email protected]5db9ada2012-04-11 13:48:201368 app_sync_bundle_.ProcessSyncChange(extensions::AppSyncData(*i));
1369 }
[email protected]3bdba0d2011-08-23 07:17:301370 }
1371
[email protected]0ffd9e52012-01-26 21:44:341372 extension_prefs()->extension_sorting()->FixNTPOrdinalCollisions();
1373
[email protected]65f173552012-06-28 22:43:581374 return syncer::SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561375}
1376
[email protected]5db9ada2012-04-11 13:48:201377extensions::ExtensionSyncData ExtensionService::GetExtensionSyncData(
1378 const Extension& extension) const {
1379 return extensions::ExtensionSyncData(extension,
1380 IsExtensionEnabled(extension.id()),
1381 IsIncognitoEnabled(extension.id()));
[email protected]b05fb9ff2011-04-23 00:07:561382}
1383
[email protected]5db9ada2012-04-11 13:48:201384extensions::AppSyncData ExtensionService::GetAppSyncData(
1385 const Extension& extension) const {
1386 return extensions::AppSyncData(
1387 extension,
1388 IsExtensionEnabled(extension.id()),
1389 IsIncognitoEnabled(extension.id()),
1390 extension_prefs_->GetAppNotificationClientId(extension.id()),
1391 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1392 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(
1393 extension.id()),
1394 extension_prefs_->extension_sorting()->GetPageOrdinal(extension.id()));
1395}
[email protected]3bdba0d2011-08-23 07:17:301396
[email protected]5db9ada2012-04-11 13:48:201397std::vector<extensions::ExtensionSyncData>
1398 ExtensionService::GetExtensionSyncDataList() const {
1399 std::vector<extensions::ExtensionSyncData> extension_sync_list;
1400 extension_sync_bundle_.GetExtensionSyncDataListHelper(extensions_,
1401 &extension_sync_list);
1402 extension_sync_bundle_.GetExtensionSyncDataListHelper(disabled_extensions_,
1403 &extension_sync_list);
1404 extension_sync_bundle_.GetExtensionSyncDataListHelper(terminated_extensions_,
1405 &extension_sync_list);
1406
1407 std::vector<extensions::ExtensionSyncData> pending_extensions =
1408 extension_sync_bundle_.GetPendingData();
1409 extension_sync_list.insert(extension_sync_list.begin(),
1410 pending_extensions.begin(),
1411 pending_extensions.end());
[email protected]3bdba0d2011-08-23 07:17:301412
1413 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561414}
1415
[email protected]5db9ada2012-04-11 13:48:201416std::vector<extensions::AppSyncData> ExtensionService::GetAppSyncDataList()
1417 const {
1418 std::vector<extensions::AppSyncData> app_sync_list;
1419 app_sync_bundle_.GetAppSyncDataListHelper(extensions_, &app_sync_list);
1420 app_sync_bundle_.GetAppSyncDataListHelper(disabled_extensions_,
1421 &app_sync_list);
1422 app_sync_bundle_.GetAppSyncDataListHelper(terminated_extensions_,
1423 &app_sync_list);
1424
1425 std::vector<extensions::AppSyncData> pending_apps =
1426 app_sync_bundle_.GetPendingData();
1427 app_sync_list.insert(app_sync_list.begin(),
1428 pending_apps.begin(),
1429 pending_apps.end());
1430
1431 return app_sync_list;
1432}
1433
1434bool ExtensionService::ProcessExtensionSyncData(
1435 const extensions::ExtensionSyncData& extension_sync_data) {
1436 if (!ProcessExtensionSyncDataHelper(extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321437 syncer::EXTENSIONS)) {
[email protected]5db9ada2012-04-11 13:48:201438 extension_sync_bundle_.AddPendingExtension(extension_sync_data.id(),
1439 extension_sync_data);
1440 CheckForUpdatesSoon();
1441 return false;
1442 }
1443
1444 return true;
1445}
1446
1447bool ExtensionService::ProcessAppSyncData(
1448 const extensions::AppSyncData& app_sync_data) {
1449 const std::string& id = app_sync_data.id();
1450 const Extension* extension = GetInstalledExtension(id);
1451 bool extension_installed = (extension != NULL);
1452
1453 if (app_sync_data.app_launch_ordinal().IsValid() &&
1454 app_sync_data.page_ordinal().IsValid()) {
1455 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1456 id,
1457 app_sync_data.app_launch_ordinal());
1458 extension_prefs_->extension_sorting()->SetPageOrdinal(
1459 id,
1460 app_sync_data.page_ordinal());
1461 }
1462
1463 if (extension_installed) {
1464 if (app_sync_data.notifications_disabled() !=
1465 extension_prefs_->IsAppNotificationDisabled(id)) {
1466 extension_prefs_->SetAppNotificationDisabled(
1467 id, app_sync_data.notifications_disabled());
1468 }
1469 }
1470
1471 if (!ProcessExtensionSyncDataHelper(app_sync_data.extension_sync_data(),
[email protected]a4a147652012-07-03 23:41:321472 syncer::APPS)) {
[email protected]5db9ada2012-04-11 13:48:201473 app_sync_bundle_.AddPendingApp(id, app_sync_data);
1474 CheckForUpdatesSoon();
1475 return false;
1476 }
1477
1478 return true;
1479}
1480
1481bool ExtensionService::IsCorrectSyncType(const Extension& extension,
[email protected]a4a147652012-07-03 23:41:321482 syncer::ModelType type) const {
1483 if (type == syncer::EXTENSIONS &&
[email protected]31bb5ee62012-09-12 22:58:401484 extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION) {
[email protected]5db9ada2012-04-11 13:48:201485 return true;
[email protected]31bb5ee62012-09-12 22:58:401486 }
[email protected]5db9ada2012-04-11 13:48:201487
[email protected]a4a147652012-07-03 23:41:321488 if (type == syncer::APPS &&
[email protected]31bb5ee62012-09-12 22:58:401489 extension.GetSyncType() == Extension::SYNC_TYPE_APP) {
[email protected]5db9ada2012-04-11 13:48:201490 return true;
[email protected]31bb5ee62012-09-12 22:58:401491 }
[email protected]5db9ada2012-04-11 13:48:201492
1493 return false;
1494}
1495
1496bool ExtensionService::ProcessExtensionSyncDataHelper(
1497 const extensions::ExtensionSyncData& extension_sync_data,
[email protected]a4a147652012-07-03 23:41:321498 syncer::ModelType type) {
[email protected]3bdba0d2011-08-23 07:17:301499 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061500 const Extension* extension = GetInstalledExtension(id);
1501
1502 // TODO(bolms): we should really handle this better. The particularly bad
1503 // case is where an app becomes an extension or vice versa, and we end up with
1504 // a zombie extension that won't go away.
[email protected]5db9ada2012-04-11 13:48:201505 if (extension && !IsCorrectSyncType(*extension, type))
1506 return true;
[email protected]90310d92011-04-17 07:35:041507
1508 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301509 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041510 std::string error;
1511 if (!UninstallExtensionHelper(this, id)) {
1512 LOG(WARNING) << "Could not uninstall extension " << id
1513 << " for sync";
1514 }
[email protected]5db9ada2012-04-11 13:48:201515 return true;
[email protected]90310d92011-04-17 07:35:041516 }
1517
[email protected]caf2d442012-05-04 10:00:501518 // Extension from sync was uninstalled by the user as external extensions.
1519 // Honor user choice and skip installation/enabling.
1520 if (IsExternalExtensionUninstalled(id)) {
1521 LOG(WARNING) << "Extension with id " << id
1522 << " from sync was uninstalled as external extension";
1523 return true;
1524 }
1525
[email protected]06f92562011-04-29 19:27:311526 // Set user settings.
[email protected]7e9e2422012-12-13 19:54:191527 // If the extension has been disabled from sync, it may not have
1528 // been installed yet, so we don't know if the disable reason was a
1529 // permissions increase. That will be updated once InitializePermissions
1530 // is called for it.
[email protected]31bb5ee62012-09-12 22:58:401531 if (extension_sync_data.enabled())
[email protected]06f92562011-04-29 19:27:311532 EnableExtension(id);
[email protected]31bb5ee62012-09-12 22:58:401533 else
[email protected]7e9e2422012-12-13 19:54:191534 DisableExtension(id, Extension::DISABLE_UNKNOWN_FROM_SYNC);
[email protected]81b14cc2011-04-29 00:39:371535
[email protected]406b5a92011-11-08 11:58:261536 // We need to cache some version information here because setting the
1537 // incognito flag invalidates the |extension| pointer (it reloads the
1538 // extension).
[email protected]9060d8b02012-01-13 02:14:301539 bool extension_installed = (extension != NULL);
[email protected]406b5a92011-11-08 11:58:261540 int result = extension ?
1541 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1542 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1543 extension = NULL; // No longer safe to use.
1544
1545 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311546 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041547 if (result < 0) {
1548 // Extension is outdated.
[email protected]5db9ada2012-04-11 13:48:201549 return false;
[email protected]b020dc12011-12-06 04:35:421550 }
[email protected]06f92562011-04-29 19:27:311551 } else {
[email protected]06f92562011-04-29 19:27:311552 // TODO(akalin): Replace silent update with a list of enabled
1553 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331554 const bool kInstallSilently = true;
[email protected]5db9ada2012-04-11 13:48:201555
[email protected]a4a147652012-07-03 23:41:321556 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
[email protected]5db9ada2012-04-11 13:48:201557 ExtensionFilter filter =
[email protected]a4a147652012-07-03 23:41:321558 (type == syncer::APPS) ? IsSyncableApp : IsSyncableExtension;
[email protected]5db9ada2012-04-11 13:48:201559
[email protected]06f92562011-04-29 19:27:311560 if (!pending_extension_manager()->AddFromSync(
1561 id,
[email protected]3bdba0d2011-08-23 07:17:301562 extension_sync_data.update_url(),
[email protected]5db9ada2012-04-11 13:48:201563 filter,
[email protected]6cc7dbae2011-04-29 21:18:331564 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311565 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301566 // This means that the extension is already pending installation, with a
1567 // non-INTERNAL location. Add to pending_sync_data, even though it will
1568 // never be removed (we'll never install a syncable version of the
1569 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311570 }
[email protected]3bdba0d2011-08-23 07:17:301571 // Track pending extensions so that we can return them in GetAllSyncData().
[email protected]5db9ada2012-04-11 13:48:201572 return false;
[email protected]90310d92011-04-17 07:35:041573 }
[email protected]5db9ada2012-04-11 13:48:201574
1575 return true;
[email protected]90310d92011-04-17 07:35:041576}
1577
[email protected]c3cfb012011-04-06 22:07:351578bool ExtensionService::IsIncognitoEnabled(
1579 const std::string& extension_id) const {
[email protected]cdc7b1f42012-12-07 19:39:481580 const Extension* extension = GetInstalledExtension(extension_id);
1581 if (extension && !extension->can_be_incognito_enabled())
1582 return false;
[email protected]c3cfb012011-04-06 22:07:351583 // If this is an existing component extension we always allow it to
1584 // work in incognito mode.
[email protected]1d5e58b2013-01-31 08:41:401585 if (extension && extension->location() == Manifest::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411586 return true;
1587
1588 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351589 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501590}
[email protected]55a35692010-02-11 23:25:211591
[email protected]c3cfb012011-04-06 22:07:351592void ExtensionService::SetIsIncognitoEnabled(
1593 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371594 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]cdc7b1f42012-12-07 19:39:481595 if (extension && !extension->can_be_incognito_enabled())
1596 return;
[email protected]1d5e58b2013-01-31 08:41:401597 if (extension && extension->location() == Manifest::COMPONENT) {
[email protected]b873cd92012-02-09 21:51:481598 // This shouldn't be called for component extensions unless they are
1599 // syncable.
1600 DCHECK(extension->IsSyncable());
1601
1602 // If we are here, make sure the we aren't trying to change the value.
1603 DCHECK_EQ(enabled, IsIncognitoEnabled(extension_id));
1604
[email protected]c3cfb012011-04-06 22:07:351605 return;
1606 }
1607
[email protected]8c6c8c22011-03-09 12:52:341608 // Broadcast unloaded and loaded events to update browser state. Only bother
1609 // if the value changed and the extension is actually enabled, since there is
1610 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351611 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341612 if (enabled == old_enabled)
1613 return;
1614
[email protected]c3cfb012011-04-06 22:07:351615 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371616
[email protected]ffbf3f12011-12-09 22:10:331617 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341618
1619 // When we reload the extension the ID may be invalidated if we've passed it
1620 // by const ref everywhere. Make a copy to be safe.
1621 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261622 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331623 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301624
[email protected]406b5a92011-11-08 11:58:261625 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341626 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301627 if (extension)
1628 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211629}
1630
[email protected]aa7599d2011-10-28 07:24:321631void ExtensionService::SetAppNotificationSetupDone(
1632 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091633 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321634 const Extension* extension = GetInstalledExtension(extension_id);
1635 // This method is called when the user sets up app notifications.
1636 // So it is not expected to be called until the extension is installed.
1637 if (!extension) {
1638 NOTREACHED();
1639 return;
1640 }
[email protected]b2689a902011-12-01 00:41:091641 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321642 SyncExtensionChangeIfNeeded(*extension);
1643}
1644
1645void ExtensionService::SetAppNotificationDisabled(
1646 const std::string& extension_id,
1647 bool value) {
1648 const Extension* extension = GetInstalledExtension(extension_id);
1649 // This method is called when the user enables/disables app notifications.
1650 // So it is not expected to be called until the extension is installed.
1651 if (!extension) {
1652 NOTREACHED();
1653 return;
1654 }
[email protected]97e856b12011-12-14 00:21:131655 if (value)
1656 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1657 else
1658 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321659 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1660 SyncExtensionChangeIfNeeded(*extension);
1661}
1662
[email protected]dc9a74f72012-08-17 18:07:211663bool ExtensionService::CanCrossIncognito(const Extension* extension) const {
[email protected]2a8f24e2010-11-03 21:37:051664 // We allow the extension to see events and data from another profile iff it
1665 // uses "spanning" behavior and it has incognito access. "split" mode
1666 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221667 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351668 return IsIncognitoEnabled(extension->id()) &&
1669 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051670}
1671
[email protected]5ef7b002011-05-12 23:16:201672bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1673 if (extension->is_hosted_app())
1674 return true;
1675 // Packaged apps and regular extensions need to be enabled specifically for
1676 // incognito (and split mode should be set).
1677 return extension->incognito_split_mode() &&
1678 IsIncognitoEnabled(extension->id());
1679}
1680
[email protected]168389f2011-12-20 17:12:481681void ExtensionService::OnExtensionMoved(
1682 const std::string& moved_extension_id,
1683 const std::string& predecessor_extension_id,
1684 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231685 extension_prefs_->extension_sorting()->OnExtensionMoved(
1686 moved_extension_id,
1687 predecessor_extension_id,
1688 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481689
1690 const Extension* extension = GetInstalledExtension(moved_extension_id);
1691 if (extension)
1692 SyncExtensionChangeIfNeeded(*extension);
1693}
1694
[email protected]dc9a74f72012-08-17 18:07:211695bool ExtensionService::AllowFileAccess(const Extension* extension) const {
[email protected]05c82182010-06-24 17:49:081696 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441697 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081698 extension_prefs_->AllowFileAccess(extension->id()));
1699}
1700
[email protected]eaa7dd182010-12-14 11:09:001701void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481702 bool allow) {
1703 // Reload to update browser state. Only bother if the value changed and the
1704 // extension is actually enabled, since there is no UI otherwise.
1705 bool old_allow = AllowFileAccess(extension);
1706 if (allow == old_allow)
1707 return;
1708
[email protected]05c82182010-06-24 17:49:081709 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481710
[email protected]84df8332011-12-06 18:22:461711 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481712 if (extension_is_enabled)
1713 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081714}
1715
[email protected]8e4560b62011-01-14 10:09:141716// Some extensions will autoupdate themselves externally from Chrome. These
1717// are typically part of some larger client application package. To support
1718// these, the extension will register its location in the the preferences file
1719// (and also, on Windows, in the registry) and this code will periodically
1720// check that location for a .crx file, which it will then install locally if
1721// a new version is available.
1722// Errors are reported through ExtensionErrorReporter. Succcess is not
1723// reported.
[email protected]eaa7dd182010-12-14 11:09:001724void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121725 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141726
1727 // Note that this installation is intentionally silent (since it didn't
1728 // go through the front-end). Extensions that are registered in this
1729 // way are effectively considered 'pre-bundled', and so implicitly
1730 // trusted. In general, if something has HKLM or filesystem access,
1731 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121732
[email protected]8e4560b62011-01-14 10:09:141733 // Ask each external extension provider to give us a call back for each
1734 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
[email protected]5df038b2012-07-16 19:03:271735 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141736 for (i = external_extension_providers_.begin();
1737 i != external_extension_providers_.end(); ++i) {
[email protected]5df038b2012-07-16 19:03:271738 extensions::ExternalProviderInterface* provider = i->get();
[email protected]8e4560b62011-01-14 10:09:141739 provider->VisitRegisteredExtension();
1740 }
1741
[email protected]50067e52011-10-20 23:17:071742 // Do any required work that we would have done after completion of all
1743 // providers.
1744 if (external_extension_providers_.empty()) {
1745 OnAllExternalProvidersReady();
1746 }
[email protected]9f1087e2009-06-15 17:29:321747}
1748
[email protected]50067e52011-10-20 23:17:071749void ExtensionService::OnExternalProviderReady(
[email protected]5df038b2012-07-16 19:03:271750 const extensions::ExternalProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121751 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071752 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121753
1754 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141755 // if all of them are finished. So we check them first.
[email protected]94fde232012-04-27 10:22:301756 if (AreAllExternalProvidersReady())
1757 OnAllExternalProvidersReady();
1758}
1759
1760bool ExtensionService::AreAllExternalProvidersReady() const {
[email protected]5df038b2012-07-16 19:03:271761 extensions::ProviderCollection::const_iterator i;
[email protected]8e4560b62011-01-14 10:09:141762 for (i = external_extension_providers_.begin();
1763 i != external_extension_providers_.end(); ++i) {
[email protected]94fde232012-04-27 10:22:301764 if (!i->get()->IsReady())
1765 return false;
[email protected]8e4560b62011-01-14 10:09:141766 }
[email protected]94fde232012-04-27 10:22:301767 return true;
[email protected]50067e52011-10-20 23:17:071768}
1769
1770void ExtensionService::OnAllExternalProvidersReady() {
1771 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]94fde232012-04-27 10:22:301772 base::TimeDelta elapsed = base::Time::Now() - profile_->GetStartTime();
1773 UMA_HISTOGRAM_TIMES("Extension.ExternalProvidersReadyAfter", elapsed);
[email protected]50067e52011-10-20 23:17:071774
1775 // Install any pending extensions.
[email protected]94fde232012-04-27 10:22:301776 if (update_once_all_providers_are_ready_ && updater()) {
1777 update_once_all_providers_are_ready_ = false;
[email protected]42774e42012-11-14 21:01:061778 updater()->CheckNow(extensions::ExtensionUpdater::CheckParams());
[email protected]8e4560b62011-01-14 10:09:141779 }
1780
1781 // Uninstall all the unclaimed extensions.
[email protected]45759612012-07-10 17:21:231782 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]8e4560b62011-01-14 10:09:141783 extension_prefs_->GetInstalledExtensionsInfo());
1784 for (size_t i = 0; i < extensions_info->size(); ++i) {
1785 ExtensionInfo* info = extensions_info->at(i).get();
[email protected]1d5e58b2013-01-31 08:41:401786 if (Manifest::IsExternalLocation(info->extension_location))
[email protected]8e4560b62011-01-14 10:09:141787 CheckExternalUninstall(info->extension_id);
1788 }
[email protected]e5af875f2011-10-10 21:09:141789 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141790}
1791
1792void ExtensionService::IdentifyAlertableExtensions() {
1793 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1794
[email protected]07c9f2f42012-02-29 18:45:221795 // Build up the lists of extensions that require acknowledgment. If this is
1796 // the first time, grandfather extensions that would have caused
1797 // notification.
[email protected]89226982012-07-16 20:09:181798 extension_error_ui_.reset(ExtensionErrorUI::Create(this));
[email protected]e5af875f2011-10-10 21:09:141799
[email protected]a9aa5932012-01-25 08:27:401800 bool did_show_alert = false;
[email protected]89226982012-07-16 20:09:181801 if (PopulateExtensionErrorUI(extension_error_ui_.get())) {
[email protected]e5af875f2011-10-10 21:09:141802 if (extension_prefs_->SetAlertSystemFirstRun()) {
[email protected]a9aa5932012-01-25 08:27:401803 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]89226982012-07-16 20:09:181804 did_show_alert = extension_error_ui_->ShowErrorInBubbleView();
[email protected]e5af875f2011-10-10 21:09:141805 } else {
1806 // First run. Just acknowledge all the extensions, silently, by
1807 // shortcutting the display of the UI and going straight to the
1808 // callback for pressing the Accept button.
[email protected]a9aa5932012-01-25 08:27:401809 HandleExtensionAlertAccept();
[email protected]e5af875f2011-10-10 21:09:141810 }
1811 }
[email protected]a9aa5932012-01-25 08:27:401812
[email protected]612a1cb12012-10-17 13:18:031813 UpdateExternalExtensionAlert();
1814
[email protected]a9aa5932012-01-25 08:27:401815 if (!did_show_alert)
[email protected]89226982012-07-16 20:09:181816 extension_error_ui_.reset();
[email protected]e5af875f2011-10-10 21:09:141817}
1818
[email protected]89226982012-07-16 20:09:181819bool ExtensionService::PopulateExtensionErrorUI(
1820 ExtensionErrorUI* extension_error_ui) {
[email protected]07c9f2f42012-02-29 18:45:221821 bool needs_alert = false;
[email protected]695b5712012-12-06 23:55:281822
1823 // Extensions that are blacklisted.
1824 for (ExtensionSet::const_iterator it = blacklisted_extensions_.begin();
1825 it != blacklisted_extensions_.end(); ++it) {
1826 std::string id = (*it)->id();
1827 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(id)) {
1828 extension_error_ui->AddBlacklistedExtension(id);
1829 needs_alert = true;
1830 }
1831 }
1832
[email protected]07c9f2f42012-02-29 18:45:221833 for (ExtensionSet::const_iterator iter = extensions_.begin();
1834 iter != extensions_.end(); ++iter) {
1835 const Extension* e = *iter;
[email protected]695b5712012-12-06 23:55:281836
1837 // Extensions disabled by policy. Note: this no longer includes blacklisted
1838 // extensions, though we still show the same UI.
[email protected]fdd679b2012-11-15 20:49:391839 if (!system_->management_policy()->UserMayLoad(e, NULL)) {
[email protected]07c9f2f42012-02-29 18:45:221840 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
[email protected]89226982012-07-16 20:09:181841 extension_error_ui->AddBlacklistedExtension(e->id());
[email protected]07c9f2f42012-02-29 18:45:221842 needs_alert = true;
1843 }
1844 }
[email protected]07c9f2f42012-02-29 18:45:221845 }
[email protected]695b5712012-12-06 23:55:281846
[email protected]07c9f2f42012-02-29 18:45:221847 return needs_alert;
1848}
1849
[email protected]a9aa5932012-01-25 08:27:401850void ExtensionService::HandleExtensionAlertClosed() {
[email protected]89226982012-07-16 20:09:181851 extension_error_ui_.reset();
[email protected]62433d32011-10-12 22:33:121852}
1853
[email protected]a9aa5932012-01-25 08:27:401854void ExtensionService::HandleExtensionAlertAccept() {
[email protected]612a1cb12012-10-17 13:18:031855 const ExtensionIdSet* extension_ids =
1856 extension_error_ui_->get_blacklisted_extension_ids();
[email protected]e5af875f2011-10-10 21:09:141857 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1858 iter != extension_ids->end(); ++iter) {
1859 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1860 }
[email protected]e5af875f2011-10-10 21:09:141861}
1862
[email protected]47fc70c2011-12-06 07:29:511863void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1864 extension_prefs_->AcknowledgeExternalExtension(id);
[email protected]612a1cb12012-10-17 13:18:031865 UpdateExternalExtensionAlert();
[email protected]47fc70c2011-12-06 07:29:511866}
1867
[email protected]d96eb512012-11-01 23:44:081868bool ExtensionService::IsUnacknowledgedExternalExtension(
1869 const Extension* extension) {
[email protected]b82d18da2012-11-09 00:11:321870 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
1871 return false;
1872
[email protected]1d5e58b2013-01-31 08:41:401873 return (Manifest::IsExternalLocation(extension->location()) &&
[email protected]d96eb512012-11-01 23:44:081874 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id()) &&
1875 !(extension_prefs_->GetDisableReasons(extension->id()) &
1876 Extension::DISABLE_SIDELOAD_WIPEOUT));
1877}
1878
[email protected]89226982012-07-16 20:09:181879void ExtensionService::HandleExtensionAlertDetails() {
1880 extension_error_ui_->ShowExtensions();
[email protected]6c751e72010-11-23 10:11:101881}
1882
[email protected]612a1cb12012-10-17 13:18:031883void ExtensionService::UpdateExternalExtensionAlert() {
[email protected]41070e8d2012-10-24 01:34:361884 if (!FeatureSwitch::prompt_for_external_extensions()->IsEnabled())
[email protected]612a1cb12012-10-17 13:18:031885 return;
1886
1887 const Extension* extension = NULL;
1888 for (ExtensionSet::const_iterator iter = disabled_extensions_.begin();
1889 iter != disabled_extensions_.end(); ++iter) {
1890 const Extension* e = *iter;
[email protected]d96eb512012-11-01 23:44:081891 if (IsUnacknowledgedExternalExtension(e)) {
1892 extension = e;
1893 break;
[email protected]612a1cb12012-10-17 13:18:031894 }
1895 }
1896
1897 if (extension) {
[email protected]d96eb512012-11-01 23:44:081898 if (!extensions::HasExternalInstallError(this)) {
1899 if (extension_prefs_->IncrementAcknowledgePromptCount(extension->id()) >
[email protected]612a1cb12012-10-17 13:18:031900 kMaxExtensionAcknowledgePromptCount) {
[email protected]d96eb512012-11-01 23:44:081901 // Stop prompting for this extension, and check if there's another
1902 // one that needs prompting.
[email protected]612a1cb12012-10-17 13:18:031903 extension_prefs_->AcknowledgeExternalExtension(extension->id());
[email protected]d96eb512012-11-01 23:44:081904 UpdateExternalExtensionAlert();
1905 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
1906 EXTERNAL_EXTENSION_IGNORED,
1907 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
1908 return;
[email protected]612a1cb12012-10-17 13:18:031909 }
[email protected]d96eb512012-11-01 23:44:081910 extensions::AddExternalInstallError(this, extension);
[email protected]612a1cb12012-10-17 13:18:031911 }
1912 } else {
1913 extensions::RemoveExternalInstallError(this);
1914 }
[email protected]612a1cb12012-10-17 13:18:031915}
1916
[email protected]a9f39a312010-12-23 22:14:271917void ExtensionService::UnloadExtension(
1918 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591919 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091920 // Make sure the extension gets deleted after we return from this function.
[email protected]695b5712012-12-06 23:55:281921 int include_mask = INCLUDE_EVERYTHING & ~INCLUDE_TERMINATED;
[email protected]9adb9692010-10-29 23:14:021922 scoped_refptr<const Extension> extension(
[email protected]695b5712012-12-06 23:55:281923 GetExtensionById(extension_id, include_mask));
[email protected]631cf822009-05-15 07:01:251924
[email protected]fa0c96732010-11-17 00:14:231925 // This method can be called via PostTask, so the extension may have been
1926 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171927 if (!extension) {
1928 // In case the extension may have crashed/uninstalled. Allow the profile to
1929 // clean up its RequestContexts.
[email protected]31d8f5f22012-04-02 15:22:081930 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231931 return;
[email protected]dd163fb02011-05-04 22:22:171932 }
[email protected]0c6da502009-08-14 22:32:391933
[email protected]1eb175082010-02-10 09:26:161934 // Keep information about the extension so that we can reload it later
1935 // even if it's not permanently installed.
1936 unloaded_extension_paths_[extension->id()] = extension->path();
1937
[email protected]f17dbd42010-08-16 23:21:101938 // Clean up if the extension is meant to be enabled after a reload.
1939 disabled_extension_paths_.erase(extension->id());
1940
[email protected]d7e9a862010-11-03 21:57:491941 // Clean up runtime data.
1942 extension_runtime_data_.erase(extension_id);
1943
[email protected]060d4972012-07-19 17:22:391944 if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271945 UnloadedExtensionInfo details(extension, reason);
1946 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461947 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071948 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271949 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531950 content::Source<Profile>(profile_),
1951 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171952 // Make sure the profile cleans up its RequestContexts when an already
1953 // disabled extension is unloaded (since they are also tracking the disabled
1954 // extensions).
[email protected]31d8f5f22012-04-02 15:22:081955 system_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391956 return;
1957 }
1958
[email protected]060d4972012-07-19 17:22:391959 // Remove the extension from our list.
[email protected]84df8332011-12-06 18:22:461960 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251961
[email protected]a9f39a312010-12-23 22:14:271962 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251963}
1964
[email protected]eaa7dd182010-12-14 11:09:001965void ExtensionService::UnloadAllExtensions() {
[email protected]31bb5ee62012-09-12 22:58:401966 profile_->GetExtensionSpecialStoragePolicy()->RevokeRightsForAllExtensions();
[email protected]c8d407e2011-04-28 21:27:171967
[email protected]84df8332011-12-06 18:22:461968 extensions_.Clear();
1969 disabled_extensions_.Clear();
1970 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491971 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321972
[email protected]c6e4a3412009-06-24 15:45:291973 // TODO(erikkay) should there be a notification for this? We can't use
1974 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1975 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321976}
1977
[email protected]eaa7dd182010-12-14 11:09:001978void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321979 UnloadAllExtensions();
[email protected]eac88332012-12-26 17:57:451980 component_loader_->LoadAll();
[email protected]d8c8f25f2011-11-02 18:18:011981 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321982}
1983
[email protected]eaa7dd182010-12-14 11:09:001984void ExtensionService::GarbageCollectExtensions() {
[email protected]2f8757c32012-06-19 19:17:471985 if (extension_prefs_->pref_service()->ReadOnly())
1986 return;
1987
[email protected]9bd9a6862012-11-29 09:24:221988 if (pending_extension_manager()->HasPendingExtensions()) {
1989 // Don't garbage collect while there are pending installations, which may
1990 // be using the temporary installation directory. Try to garbage collect
1991 // again later.
1992 MessageLoop::current()->PostDelayedTask(
1993 FROM_HERE,
1994 base::Bind(&ExtensionService::GarbageCollectExtensions, AsWeakPtr()),
1995 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelay));
1996 return;
1997 }
1998
[email protected]45759612012-07-10 17:21:231999 scoped_ptr<extensions::ExtensionPrefs::ExtensionsInfo> info(
[email protected]2f8757c32012-06-19 19:17:472000 extension_prefs_->GetInstalledExtensionsInfo());
2001
[email protected]650b2d52013-02-10 03:41:452002 std::multimap<std::string, base::FilePath> extension_paths;
[email protected]2f8757c32012-06-19 19:17:472003 for (size_t i = 0; i < info->size(); ++i)
[email protected]0db124b02012-11-07 04:55:052004 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2005 info->at(i)->extension_path));
2006
[email protected]399583b2012-12-11 09:33:422007 info = extension_prefs_->GetAllDelayedInstallInfo();
[email protected]0db124b02012-11-07 04:55:052008 for (size_t i = 0; i < info->size(); ++i)
2009 extension_paths.insert(std::make_pair(info->at(i)->extension_id,
2010 info->at(i)->extension_path));
[email protected]2f8757c32012-06-19 19:17:472011
[email protected]7f8f24f2012-11-15 19:40:142012 if (!GetFileTaskRunner()->PostTask(
2013 FROM_HERE,
[email protected]2f8757c32012-06-19 19:17:472014 base::Bind(
2015 &extension_file_util::GarbageCollectExtensions,
2016 install_directory_,
[email protected]31bb5ee62012-09-12 22:58:402017 extension_paths))) {
[email protected]2f8757c32012-06-19 19:17:472018 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402019 }
[email protected]f6ccd652010-11-17 00:48:342020
[email protected]f766a9d2012-07-11 01:57:442021#if defined(ENABLE_THEMES)
[email protected]f6ccd652010-11-17 00:48:342022 // Also garbage-collect themes. We check |profile_| to be
2023 // defensive; in the future, we may call GarbageCollectExtensions()
2024 // from somewhere other than Init() (e.g., in a timer).
2025 if (profile_) {
[email protected]d5949312012-12-03 22:13:302026 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:342027 }
[email protected]f766a9d2012-07-11 01:57:442028#endif
[email protected]3cf4f0992009-02-03 23:00:302029}
2030
[email protected]fb82dcd2012-03-21 14:15:462031void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
[email protected]5db9ada2012-04-11 13:48:202032 if (app_sync_bundle_.HandlesApp(extension)) {
2033 app_sync_bundle_.SyncChangeIfNeeded(extension);
2034 } else if (extension_sync_bundle_.HandlesExtension(extension)) {
2035 extension_sync_bundle_.SyncChangeIfNeeded(extension);
[email protected]fb82dcd2012-03-21 14:15:462036 }
2037}
2038
[email protected]eaa7dd182010-12-14 11:09:002039void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]31bb5ee62012-09-12 22:58:402040 if (updater_.get())
[email protected]93fd78f42009-07-10 16:43:172041 updater_->Start();
[email protected]fa6a9102010-11-22 15:38:502042
[email protected]bc151cf92013-02-12 04:57:262043 OnBlacklistUpdated();
2044
[email protected]fa6a9102010-11-22 15:38:502045 ready_ = true;
[email protected]ad50def52011-10-19 23:17:072046 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272047 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:532048 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:072049 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:512050}
2051
[email protected]fcb58a862012-05-01 01:03:152052void ExtensionService::AddExtension(const Extension* extension) {
[email protected]c8d407e2011-04-28 21:27:172053 // TODO(jstritar): We may be able to get rid of this branch by overriding the
2054 // default extension state to DISABLED when the --disable-extensions flag
2055 // is set (http://crbug.com/29067).
2056 if (!extensions_enabled() &&
2057 !extension->is_theme() &&
[email protected]1d5e58b2013-01-31 08:41:402058 extension->location() != Manifest::COMPONENT &&
2059 !Manifest::IsExternalLocation(extension->location())) {
[email protected]fcb58a862012-05-01 01:03:152060 return;
[email protected]330840c2012-08-29 22:21:012061 }
[email protected]c8d407e2011-04-28 21:27:172062
2063 SetBeingUpgraded(extension, false);
2064
[email protected]1eb175082010-02-10 09:26:162065 // The extension is now loaded, remove its data from unloaded extension map.
2066 unloaded_extension_paths_.erase(extension->id());
2067
[email protected]bb7f40952011-01-13 00:21:202068 // If a terminated extension is loaded, remove it from the terminated list.
2069 UntrackTerminatedExtension(extension->id());
2070
[email protected]f17dbd42010-08-16 23:21:102071 // If the extension was disabled for a reload, then enable it.
2072 if (disabled_extension_paths_.erase(extension->id()) > 0)
2073 EnableExtension(extension->id());
2074
[email protected]d728e002010-12-08 04:46:232075 // Check if the extension's privileges have changed and disable the
2076 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:312077 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:392078
[email protected]215a7be2012-10-22 19:53:422079 // If this extension is a sideloaded extension and we've not performed a
2080 // wipeout before, we might disable this extension here.
2081 MaybeWipeout(extension);
2082
[email protected]695b5712012-12-06 23:55:282083 if (extension_prefs_->IsExtensionBlacklisted(extension->id())) {
2084 // Don't check the Blacklist yet because it's asynchronous (we do it at
2085 // the end). This pre-emptive check is because we will always store the
2086 // blacklisted state of *installed* extensions in prefs, and it's important
2087 // not to re-enable blacklisted extensions.
2088 blacklisted_extensions_.Insert(extension);
[email protected]695b5712012-12-06 23:55:282089 } else if (extension_prefs_->IsExtensionDisabled(extension->id())) {
[email protected]6f6101832012-11-27 22:10:482090 disabled_extensions_.Insert(extension);
[email protected]3bdba0d2011-08-23 07:17:302091 SyncExtensionChangeIfNeeded(*extension);
[email protected]44d62b62012-04-11 00:06:032092 content::NotificationService::current()->Notify(
2093 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
2094 content::Source<Profile>(profile_),
2095 content::Details<const Extension>(extension));
[email protected]62f051c2012-03-29 17:04:442096
[email protected]7e9e2422012-12-13 19:54:192097 // Show the extension disabled error if a permissions increase was the
2098 // only reason it was disabled.
2099 if (extension_prefs_->GetDisableReasons(extension->id()) ==
[email protected]44d62b62012-04-11 00:06:032100 Extension::DISABLE_PERMISSIONS_INCREASE) {
[email protected]62f051c2012-03-29 17:04:442101 extensions::AddExtensionDisabledError(this, extension);
2102 }
[email protected]695b5712012-12-06 23:55:282103 } else {
2104 // All apps that are displayed in the launcher are ordered by their ordinals
2105 // so we must ensure they have valid ordinals.
2106 if (extension->RequiresSortOrdinal()) {
[email protected]bd36b652013-02-27 02:13:252107 if (!extension->ShouldDisplayInNewTabPage()) {
2108 extension_prefs_->extension_sorting()->MarkExtensionAsHidden(
2109 extension->id());
2110 }
[email protected]695b5712012-12-06 23:55:282111 extension_prefs_->extension_sorting()->EnsureValidOrdinals(
2112 extension->id(), syncer::StringOrdinal());
2113 }
2114
2115 extensions_.Insert(extension);
2116 SyncExtensionChangeIfNeeded(*extension);
2117 NotifyExtensionLoaded(extension);
2118 DoPostLoadTasks(extension);
[email protected]f574c402012-12-04 23:20:312119 }
[email protected]aab98a52009-12-02 03:22:352120}
2121
[email protected]8c484b742012-11-29 06:05:362122void ExtensionService::AddComponentExtension(const Extension* extension) {
2123 const std::string old_version_string(
2124 extension_prefs_->GetVersionString(extension->id()));
2125 const Version old_version(old_version_string);
2126
2127 if (!old_version.IsValid() || !old_version.Equals(*extension->version())) {
2128 VLOG(1) << "Component extension " << extension->name() << " ("
2129 << extension->id() << ") installing/upgrading from '"
2130 << old_version_string << "' to " << extension->version()->GetString();
2131
2132 AddNewOrUpdatedExtension(extension,
[email protected]399583b2012-12-11 09:33:422133 Extension::ENABLED_COMPONENT,
2134 syncer::StringOrdinal());
[email protected]8c484b742012-11-29 06:05:362135 return;
2136 }
2137
2138 AddExtension(extension);
2139}
2140
[email protected]902fd7b2011-07-27 18:42:312141void ExtensionService::InitializePermissions(const Extension* extension) {
2142 // If the extension has used the optional permissions API, it will have a
2143 // custom set of active permissions defined in the extension prefs. Here,
2144 // we update the extension's active permissions based on the prefs.
[email protected]c2e66e12012-06-27 06:27:062145 scoped_refptr<PermissionSet> active_permissions =
[email protected]902fd7b2011-07-27 18:42:312146 extension_prefs()->GetActivePermissions(extension->id());
2147
2148 if (active_permissions.get()) {
2149 // We restrict the active permissions to be within the bounds defined in the
2150 // extension's manifest.
2151 // a) active permissions must be a subset of optional + default permissions
2152 // b) active permissions must contains all default permissions
[email protected]c2e66e12012-06-27 06:27:062153 scoped_refptr<PermissionSet> total_permissions =
2154 PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:312155 extension->required_permission_set(),
2156 extension->optional_permission_set());
2157
2158 // Make sure the active permissions contain no more than optional + default.
[email protected]c2e66e12012-06-27 06:27:062159 scoped_refptr<PermissionSet> adjusted_active =
2160 PermissionSet::CreateIntersection(
[email protected]902fd7b2011-07-27 18:42:312161 total_permissions.get(), active_permissions.get());
2162
2163 // Make sure the active permissions contain the default permissions.
[email protected]c2e66e12012-06-27 06:27:062164 adjusted_active = PermissionSet::CreateUnion(
[email protected]902fd7b2011-07-27 18:42:312165 extension->required_permission_set(), adjusted_active.get());
2166
[email protected]c333e792012-01-06 16:57:392167 extensions::PermissionsUpdater perms_updater(profile());
2168 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312169 }
2170
[email protected]8d888c12010-11-30 00:00:252171 // We keep track of all permissions the user has granted each extension.
2172 // This allows extensions to gracefully support backwards compatibility
2173 // by including unknown permissions in their manifests. When the user
2174 // installs the extension, only the recognized permissions are recorded.
2175 // When the unknown permissions become recognized (e.g., through browser
2176 // upgrade), we can prompt the user to accept these new permissions.
2177 // Extensions can also silently upgrade to less permissions, and then
2178 // silently upgrade to a version that adds these permissions back.
2179 //
2180 // For example, pretend that Chrome 10 includes a permission "omnibox"
2181 // for an API that adds suggestions to the omnibox. An extension can
2182 // maintain backwards compatibility while still having "omnibox" in the
2183 // manifest. If a user installs the extension on Chrome 9, the browser
2184 // will record the permissions it recognized, not including "omnibox."
2185 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2186 // will disable the extension and prompt the user to approve the increase
2187 // in privileges. The extension could then release a new version that
2188 // removes the "omnibox" permission. When the user upgrades, Chrome will
2189 // still remember that "omnibox" had been granted, so that if the
2190 // extension once again includes "omnibox" in an upgrade, the extension
2191 // can upgrade without requiring this user's approval.
[email protected]695b5712012-12-06 23:55:282192 const Extension* old = GetInstalledExtension(extension->id());
[email protected]8d888c12010-11-30 00:00:252193 bool is_extension_upgrade = old != NULL;
2194 bool is_privilege_increase = false;
[email protected]44d62b62012-04-11 00:06:032195 bool previously_disabled = false;
[email protected]eb5e4f92012-08-15 23:33:282196 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]8d888c12010-11-30 00:00:252197
[email protected]902fd7b2011-07-27 18:42:312198 // We only need to compare the granted permissions to the current permissions
2199 // if the extension is not allowed to silently increase its permissions.
[email protected]be083862012-09-01 03:53:452200 bool is_default_app_install =
2201 (!is_extension_upgrade && extension->was_installed_by_default());
2202 if (!(extension->CanSilentlyIncreasePermissions()
2203 || is_default_app_install)) {
[email protected]8d888c12010-11-30 00:00:252204 // Add all the recognized permissions if the granted permissions list
2205 // hasn't been initialized yet.
[email protected]c2e66e12012-06-27 06:27:062206 scoped_refptr<PermissionSet> granted_permissions =
[email protected]902fd7b2011-07-27 18:42:312207 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522208 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252209
2210 // Here, we check if an extension's privileges have increased in a manner
2211 // that requires the user's approval. This could occur because the browser
2212 // upgraded and recognized additional privileges, or an extension upgrades
2213 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522214 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312215 granted_permissions->HasLessPrivilegesThan(
2216 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252217 }
2218
[email protected]be083862012-09-01 03:53:452219 // Silently grant all active permissions to default apps only on install.
2220 // After install they should behave like other apps.
[email protected]c856e9762012-10-12 19:42:382221 if (is_default_app_install)
2222 GrantPermissions(extension, true);
[email protected]be083862012-09-01 03:53:452223
[email protected]8d888c12010-11-30 00:00:252224 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252225 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2226 // that we aren't downgrading.
[email protected]12075d12013-02-27 05:38:052227 if (!Manifest::IsUnpackedLocation(extension->location()))
[email protected]31bb5ee62012-09-12 22:58:402228 CHECK_GE(extension->version()->CompareTo(*(old->version())), 0);
[email protected]8d888c12010-11-30 00:00:252229
2230 // Extensions get upgraded if the privileges are allowed to increase or
2231 // the privileges haven't increased.
2232 if (!is_privilege_increase) {
2233 SetBeingUpgraded(old, true);
2234 SetBeingUpgraded(extension, true);
2235 }
2236
[email protected]44d62b62012-04-11 00:06:032237 // If the extension was already disabled, suppress any alerts for becoming
2238 // disabled on permissions increase.
2239 previously_disabled = extension_prefs_->IsExtensionDisabled(old->id());
[email protected]7e9e2422012-12-13 19:54:192240 // Legacy disabled extensions do not have a disable reason. Infer that if
2241 // there was no permission increase, it was likely disabled by the user.
2242 if (previously_disabled && disable_reasons == Extension::DISABLE_NONE &&
2243 !extension_prefs_->DidExtensionEscalatePermissions(old->id())) {
2244 disable_reasons |= Extension::DISABLE_USER_ACTION;
[email protected]44d62b62012-04-11 00:06:032245 }
[email protected]7e9e2422012-12-13 19:54:192246 // Extensions that came to us disabled from sync need a similar inference,
2247 // except based on the new version's permissions.
2248 if (previously_disabled &&
2249 disable_reasons == Extension::DISABLE_UNKNOWN_FROM_SYNC) {
2250 // Remove the DISABLE_UNKNOWN_FROM_SYNC reason.
2251 extension_prefs_->ClearDisableReasons(extension->id());
2252 if (!is_privilege_increase)
2253 disable_reasons |= Extension::DISABLE_USER_ACTION;
2254 }
2255 if (disable_reasons != Extension::DISABLE_NONE)
2256 disable_reasons &= ~Extension::DISABLE_UNKNOWN_FROM_SYNC;
[email protected]44d62b62012-04-11 00:06:032257
[email protected]8d888c12010-11-30 00:00:252258 // To upgrade an extension in place, unload the old one and
2259 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592260 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252261 old = NULL;
2262 }
2263
2264 // Extension has changed permissions significantly. Disable it. A
2265 // notification should be sent by the caller.
2266 if (is_privilege_increase) {
[email protected]7e9e2422012-12-13 19:54:192267 disable_reasons |= Extension::DISABLE_PERMISSIONS_INCREASE;
[email protected]fe2dd7742011-04-19 22:52:492268 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2269 RecordPermissionMessagesHistogram(
2270 extension, "Extensions.Permissions_AutoDisable");
2271 }
[email protected]b3317ad2011-04-28 23:46:002272 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252273 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
[email protected]eb5e4f92012-08-15 23:33:282274 extension_prefs_->AddDisableReason(
2275 extension->id(),
2276 static_cast<Extension::DisableReason>(disable_reasons));
[email protected]8d888c12010-11-30 00:00:252277 }
2278}
2279
[email protected]215a7be2012-10-22 19:53:422280void ExtensionService::MaybeWipeout(
2281 const extensions::Extension* extension) {
[email protected]1ff4d282012-12-08 00:39:212282 if (!wipeout_is_active_)
[email protected]215a7be2012-10-22 19:53:422283 return;
2284
[email protected]1d5e58b2013-01-31 08:41:402285 if (extension->GetType() != Manifest::TYPE_EXTENSION)
[email protected]215a7be2012-10-22 19:53:422286 return;
2287
[email protected]1d5e58b2013-01-31 08:41:402288 Manifest::Location location = extension->location();
2289 if (location != Manifest::EXTERNAL_REGISTRY)
[email protected]1ff4d282012-12-08 00:39:212290 return;
2291
2292 if (extension_prefs_->IsExternalExtensionExcludedFromWipeout(extension->id()))
2293 return;
2294
[email protected]215a7be2012-10-22 19:53:422295 int disable_reasons = extension_prefs_->GetDisableReasons(extension->id());
[email protected]1ff4d282012-12-08 00:39:212296 if (disable_reasons == Extension::DISABLE_NONE) {
2297 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
2298 extension_prefs_->AddDisableReason(
2299 extension->id(),
2300 static_cast<Extension::DisableReason>(
2301 Extension::DISABLE_SIDELOAD_WIPEOUT));
2302 UMA_HISTOGRAM_BOOLEAN("DisabledExtension.ExtensionWipedStatus", true);
[email protected]3a2748c42013-01-15 10:13:232303 wipeout_count_++;
[email protected]215a7be2012-10-22 19:53:422304 }
2305}
2306
[email protected]eaa7dd182010-12-14 11:09:002307void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392308 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462309 for (ExtensionSet::const_iterator iter = extensions_.begin();
2310 iter != extensions_.end(); ++iter) {
2311 const Extension* extension = *iter;
[email protected]1d5e58b2013-01-31 08:41:402312 if (!extension->is_theme() && extension->location() != Manifest::COMPONENT)
[email protected]84df8332011-12-06 18:22:462313 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352314 }
2315
2316 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252317}
2318
[email protected]9d02fa12013-02-19 05:12:572319void ExtensionService::ScheduleLaunchOnLoad(const std::string& extension_id) {
2320 on_load_events_[extension_id] = EVENT_LAUNCHED;
2321}
2322
[email protected]8266d662011-07-12 21:53:262323void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502324 const Extension* extension,
[email protected]98270432012-09-11 20:51:242325 const syncer::StringOrdinal& page_ordinal,
[email protected]0db124b02012-11-07 04:55:052326 bool has_requirement_errors,
2327 bool wait_for_idle) {
[email protected]a29a517a2011-01-21 21:11:122328 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202329
[email protected]c3cfb012011-04-06 22:07:352330 const std::string& id = extension->id();
[email protected]612a1cb12012-10-17 13:18:032331 bool initial_enable = ShouldEnableOnInstall(extension);
[email protected]3f213ad2012-07-26 23:39:412332 const extensions::PendingExtensionInfo* pending_extension_info = NULL;
[email protected]51a3bf8b2012-06-08 22:53:062333 if ((pending_extension_info = pending_extension_manager()->GetById(id))) {
2334 if (!pending_extension_info->ShouldAllowInstall(*extension)) {
2335 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322336
[email protected]31bb5ee62012-09-12 22:58:402337 LOG(WARNING) << "ShouldAllowInstall() returned false for "
2338 << id << " of type " << extension->GetType()
[email protected]65348062013-01-15 07:27:222339 << " and update URL "
2340 << extensions::ManifestURL::GetUpdateURL(extension).spec()
[email protected]31bb5ee62012-09-12 22:58:402341 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542342
[email protected]ad50def52011-10-19 23:17:072343 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272344 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532345 content::Source<Profile>(profile_),
2346 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542347
[email protected]4416c5a2010-06-26 01:28:572348 // Delete the extension directory since we're not going to
2349 // load it.
[email protected]7f8f24f2012-11-15 19:40:142350 if (!GetFileTaskRunner()->PostTask(
2351 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362352 base::Bind(&extension_file_util::DeleteFile,
[email protected]31bb5ee62012-09-12 22:58:402353 extension->path(), true))) {
[email protected]14908b72011-04-20 06:54:362354 NOTREACHED();
[email protected]31bb5ee62012-09-12 22:58:402355 }
[email protected]4416c5a2010-06-26 01:28:572356 return;
2357 }
[email protected]51a3bf8b2012-06-08 22:53:062358
2359 pending_extension_manager()->Remove(id);
[email protected]4416c5a2010-06-26 01:28:572360 } else {
[email protected]c3cfb012011-04-06 22:07:352361 // We explicitly want to re-enable an uninstalled external
2362 // extension; if we're here, that means the user is manually
2363 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332364 if (IsExternalExtensionUninstalled(id)) {
2365 initial_enable = true;
2366 }
[email protected]aa142702010-03-26 01:26:332367 }
2368
[email protected]98270432012-09-11 20:51:242369 // Unsupported requirements overrides the management policy.
2370 if (has_requirement_errors) {
2371 initial_enable = false;
2372 extension_prefs_->AddDisableReason(
2373 id, Extension::DISABLE_UNSUPPORTED_REQUIREMENT);
2374 // If the extension was disabled because of unsupported requirements but
2375 // now supports all requirements after an update and there are not other
2376 // disable reasons, enable it.
2377 } else if (extension_prefs_->GetDisableReasons(id) ==
2378 Extension::DISABLE_UNSUPPORTED_REQUIREMENT) {
2379 initial_enable = true;
2380 extension_prefs_->ClearDisableReasons(id);
2381 }
2382
[email protected]695b5712012-12-06 23:55:282383 if (!GetInstalledExtension(extension->id())) {
[email protected]07533022011-06-27 20:42:552384 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2385 extension->GetType(), 100);
[email protected]69084dc2012-11-17 07:39:302386 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallSource",
[email protected]1d5e58b2013-01-31 08:41:402387 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552388 RecordPermissionMessagesHistogram(
2389 extension, "Extensions.Permissions_Install");
[email protected]69084dc2012-11-17 07:39:302390 } else {
2391 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateType",
2392 extension->GetType(), 100);
2393 UMA_HISTOGRAM_ENUMERATION("Extensions.UpdateSource",
[email protected]1d5e58b2013-01-31 08:41:402394 extension->location(), Manifest::NUM_LOCATIONS);
[email protected]07533022011-06-27 20:42:552395 }
2396
[email protected]a39921b42012-02-28 03:42:542397 // Certain extension locations are specific enough that we can
2398 // auto-acknowledge any extension that came from one of them.
[email protected]1d5e58b2013-01-31 08:41:402399 if (extension->location() == Manifest::EXTERNAL_POLICY_DOWNLOAD)
[email protected]a39921b42012-02-28 03:42:542400 AcknowledgeExternalExtension(extension->id());
[email protected]399583b2012-12-11 09:33:422401 const Extension::State initial_state =
2402 initial_enable ? Extension::ENABLED : Extension::DISABLED;
[email protected]e7aa7b7e2012-11-27 04:51:222403 if (ShouldDelayExtensionUpdate(id, wait_for_idle)) {
[email protected]399583b2012-12-11 09:33:422404 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2405 page_ordinal);
[email protected]0db124b02012-11-07 04:55:052406
2407 // Transfer ownership of |extension|.
[email protected]399583b2012-12-11 09:33:422408 delayed_updates_for_idle_.Insert(extension);
[email protected]0db124b02012-11-07 04:55:052409
2410 // Notify extension of available update.
2411 extensions::RuntimeEventRouter::DispatchOnUpdateAvailableEvent(
2412 profile_, id, extension->manifest()->value());
2413 return;
2414 }
2415
[email protected]399583b2012-12-11 09:33:422416 if (installs_delayed()) {
2417 extension_prefs_->SetDelayedInstallInfo(extension, initial_state,
2418 page_ordinal);
2419 delayed_installs_.Insert(extension);
2420 } else {
2421 AddNewOrUpdatedExtension(extension, initial_state, page_ordinal);
2422 }
[email protected]8c484b742012-11-29 06:05:362423}
2424
2425void ExtensionService::AddNewOrUpdatedExtension(
2426 const Extension* extension,
[email protected]399583b2012-12-11 09:33:422427 Extension::State initial_state,
2428 const syncer::StringOrdinal& page_ordinal) {
[email protected]8c484b742012-11-29 06:05:362429 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2430
[email protected]4416c5a2010-06-26 01:28:572431 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262432 extension,
[email protected]8c484b742012-11-29 06:05:362433 initial_state,
[email protected]36a5c4c2011-12-14 16:34:502434 page_ordinal);
[email protected]25b34332009-06-05 21:53:192435
[email protected]6f6101832012-11-27 22:10:482436 FinishInstallation(extension);
2437}
2438
2439void ExtensionService::MaybeFinishDelayedInstallation(
2440 const std::string& extension_id) {
2441 // Check if the extension already got updated.
[email protected]399583b2012-12-11 09:33:422442 if (!delayed_updates_for_idle_.Contains(extension_id))
[email protected]6f6101832012-11-27 22:10:482443 return;
2444 // Check if the extension is idle.
2445 if (!IsExtensionIdle(extension_id))
2446 return;
2447
2448 FinishDelayedInstallation(extension_id);
2449}
2450
2451void ExtensionService::FinishDelayedInstallation(
2452 const std::string& extension_id) {
2453 scoped_refptr<const Extension> extension(
2454 GetPendingExtensionUpdate(extension_id));
2455 CHECK(extension);
[email protected]399583b2012-12-11 09:33:422456 delayed_updates_for_idle_.Remove(extension_id);
[email protected]6f6101832012-11-27 22:10:482457
[email protected]399583b2012-12-11 09:33:422458 if (!extension_prefs_->FinishDelayedInstallInfo(extension_id))
[email protected]6f6101832012-11-27 22:10:482459 NOTREACHED();
2460
2461 FinishInstallation(extension);
2462}
2463
2464void ExtensionService::FinishInstallation(const Extension* extension) {
[email protected]fcb58a862012-05-01 01:03:152465 content::NotificationService::current()->Notify(
2466 chrome::NOTIFICATION_EXTENSION_INSTALLED,
2467 content::Source<Profile>(profile_),
2468 content::Details<const Extension>(extension));
2469
[email protected]d96eb512012-11-01 23:44:082470 bool unacknowledged_external = IsUnacknowledgedExternalExtension(extension);
[email protected]41070e8d2012-10-24 01:34:362471
[email protected]399583b2012-12-11 09:33:422472 // Unpacked extensions default to allowing file access, but if that has been
2473 // overridden, don't reset the value.
[email protected]1d5e58b2013-01-31 08:41:402474 if (Manifest::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]399583b2012-12-11 09:33:422475 !extension_prefs_->HasAllowFileAccessSetting(extension->id())) {
2476 extension_prefs_->SetAllowFileAccess(extension->id(), true);
2477 }
2478
[email protected]6f6101832012-11-27 22:10:482479 AddExtension(extension);
[email protected]612a1cb12012-10-17 13:18:032480
[email protected]a51c9e92012-12-09 09:19:202481#if defined(ENABLE_THEMES)
[email protected]bc151cf92013-02-12 04:57:262482 // We do this here since AddExtension() is always called on browser startup,
2483 // and we only really care about the last theme installed.
[email protected]a51c9e92012-12-09 09:19:202484 // If that ever changes and we have to move this code somewhere
2485 // else, it should be somewhere that's not in the startup path.
2486 if (extension->is_theme() && extensions_.GetByID(extension->id())) {
2487 DCHECK_EQ(extensions_.GetByID(extension->id()), extension);
2488 // Now that the theme extension is visible from outside the
2489 // ExtensionService, notify the ThemeService about the
2490 // newly-installed theme.
2491 ThemeServiceFactory::GetForProfile(profile_)->SetTheme(extension);
2492 }
2493#endif
2494
[email protected]612a1cb12012-10-17 13:18:032495 // If this is a new external extension that was disabled, alert the user
[email protected]d96eb512012-11-01 23:44:082496 // so he can reenable it. We do this last so that it has already been
2497 // added to our list of extensions.
2498 if (unacknowledged_external) {
[email protected]612a1cb12012-10-17 13:18:032499 UpdateExternalExtensionAlert();
[email protected]d96eb512012-11-01 23:44:082500 UMA_HISTOGRAM_ENUMERATION("Extensions.ExternalExtensionEvent",
2501 EXTERNAL_EXTENSION_INSTALLED,
2502 EXTERNAL_EXTENSION_BUCKET_BOUNDARY);
2503 }
[email protected]4a190632009-05-09 01:07:422504}
2505
[email protected]76b65442012-11-17 14:11:482506const Extension* ExtensionService::GetPendingExtensionUpdate(
2507 const std::string& id) const {
[email protected]399583b2012-12-11 09:33:422508 return delayed_updates_for_idle_.GetByID(id);
[email protected]76b65442012-11-17 14:11:482509}
2510
[email protected]bb7f40952011-01-13 00:21:202511void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462512 if (!terminated_extensions_.Contains(extension->id()))
2513 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202514
[email protected]b3f7fe22011-11-11 19:27:562515 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202516}
2517
2518void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202519 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462520 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202521}
2522
[email protected]0dfe05c2011-02-23 23:03:362523const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472524 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282525 return GetExtensionById(id, INCLUDE_TERMINATED);
[email protected]8001df22011-04-28 19:59:472526}
2527
2528const Extension* ExtensionService::GetInstalledExtension(
2529 const std::string& id) const {
[email protected]695b5712012-12-06 23:55:282530 int include_mask = INCLUDE_ENABLED |
2531 INCLUDE_DISABLED |
2532 INCLUDE_TERMINATED |
2533 INCLUDE_BLACKLISTED;
2534 return GetExtensionById(id, include_mask);
[email protected]0dfe05c2011-02-23 23:03:362535}
2536
[email protected]eaa7dd182010-12-14 11:09:002537bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442538 // Allow bindings for all packaged extensions and component hosted apps.
2539 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2540 ExtensionURLInfo(url));
2541 return extension && (!extension->is_hosted_app() ||
[email protected]1d5e58b2013-01-31 08:41:402542 extension->location() == Manifest::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392543}
2544
[email protected]4d007b312012-10-17 03:00:482545bool ExtensionService::ShouldBlockUrlInBrowserTab(GURL* url) {
2546 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2547 ExtensionURLInfo(*url));
2548 if (extension && extension->is_platform_app()) {
2549 *url = GURL(chrome::kExtensionInvalidRequestURL);
2550 return true;
2551 }
2552
2553 return false;
2554}
2555
[email protected]9060d8b02012-01-13 02:14:302556bool ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322557 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142558 const Version* version,
[email protected]650b2d52013-02-10 03:41:452559 const base::FilePath& path,
[email protected]1d5e58b2013-01-31 08:41:402560 Manifest::Location location,
[email protected]47fc70c2011-12-06 07:29:512561 int creation_flags,
2562 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382563 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2564 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012565 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]9060d8b02012-01-13 02:14:302566 return false;
[email protected]a8af9fdb2010-10-28 21:52:202567
[email protected]7577a5c52009-07-30 06:21:582568 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492569 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582570 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022571 const Extension* existing = GetExtensionById(id, true);
[email protected]910f72ce2012-08-24 01:38:352572
[email protected]7577a5c52009-07-30 06:21:582573 if (existing) {
[email protected]910f72ce2012-08-24 01:38:352574 // The default apps will have the location set as INTERNAL. Since older
2575 // default apps are installed as EXTERNAL, we override them. However, if the
2576 // app is already installed as internal, then do the version check.
[email protected]31bb5ee62012-09-12 22:58:402577 // TODO(grv) : Remove after Q1-2013.
[email protected]910f72ce2012-08-24 01:38:352578 bool is_default_apps_migration =
[email protected]1d5e58b2013-01-31 08:41:402579 (location == Manifest::INTERNAL &&
2580 Manifest::IsExternalLocation(existing->location()));
[email protected]910f72ce2012-08-24 01:38:352581
2582 if (!is_default_apps_migration) {
2583 DCHECK(version);
2584
2585 switch (existing->version()->CompareTo(*version)) {
2586 case -1: // existing version is older, we should upgrade
2587 break;
2588 case 0: // existing version is same, do nothing
2589 return false;
2590 case 1: // existing version is newer, uh-oh
2591 LOG(WARNING) << "Found external version of extension " << id
2592 << "that is older than current version. Current version "
2593 << "is: " << existing->VersionString() << ". New "
2594 << "version is: " << version->GetString()
2595 << ". Keeping current version.";
2596 return false;
2597 }
[email protected]7577a5c52009-07-30 06:21:582598 }
2599 }
2600
[email protected]9060d8b02012-01-13 02:14:302601 // If the extension is already pending, don't start an install.
[email protected]e3987852012-05-04 10:06:302602 if (!pending_extension_manager()->AddFromExternalFile(
2603 id, location, *version)) {
[email protected]9060d8b02012-01-13 02:14:302604 return false;
[email protected]e3987852012-05-04 10:06:302605 }
[email protected]9c635f22010-12-02 09:36:362606
[email protected]14908b72011-04-20 06:54:362607 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012608 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162609 installer->set_install_source(location);
2610 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072611 installer->set_expected_version(*version);
2612 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312613 installer->set_creation_flags(creation_flags);
[email protected]88e8ec9152013-01-17 04:05:182614#if defined(OS_CHROMEOS)
2615 extensions::InstallLimiter::Get(profile_)->Add(installer, path);
2616#else
[email protected]6dfbbf82010-03-12 23:09:162617 installer->InstallCrx(path);
[email protected]88e8ec9152013-01-17 04:05:182618#endif
[email protected]47fc70c2011-12-06 07:29:512619
2620 // Depending on the source, a new external extension might not need a user
2621 // notification on installation. For such extensions, mark them acknowledged
2622 // now to suppress the notification.
2623 if (mark_acknowledged)
2624 AcknowledgeExternalExtension(id);
[email protected]9060d8b02012-01-13 02:14:302625
2626 return true;
[email protected]7577a5c52009-07-30 06:21:582627}
2628
[email protected]eaa7dd182010-12-14 11:09:002629void ExtensionService::ReportExtensionLoadError(
[email protected]650b2d52013-02-10 03:41:452630 const base::FilePath& extension_path,
[email protected]d11c8e92009-10-20 23:26:402631 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402632 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012633 content::NotificationService::current()->Notify(
2634 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2635 content::Source<Profile>(profile_),
2636 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402637
[email protected]8a205c02011-02-04 20:41:332638 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]438afb12012-07-05 03:18:212639 string16 message = UTF8ToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042640 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492641 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402642 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2643}
2644
[email protected]eaa7dd182010-12-14 11:09:002645void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]3a1dc572012-07-31 22:25:132646 extensions::ExtensionHost* host) {
[email protected]406027c02010-09-27 08:03:182647 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332648 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182649 if (iter == orphaned_dev_tools_.end())
2650 return;
2651
[email protected]98f66112012-12-25 12:59:362652 DevToolsAgentHost::ConnectRenderViewHost(iter->second,
2653 host->render_view_host());
[email protected]406027c02010-09-27 08:03:182654 orphaned_dev_tools_.erase(iter);
2655}
2656
[email protected]432115822011-07-10 15:52:272657void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532658 const content::NotificationSource& source,
2659 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272660 switch (type) {
[email protected]3c4abc82012-10-22 22:25:542661 case chrome::NOTIFICATION_APP_TERMINATING:
2662 // Shutdown has started. Don't start any more extension installs.
2663 // (We cannot use ExtensionService::Shutdown() for this because it
2664 // happens too late in browser teardown.)
2665 browser_terminating_ = true;
2666 break;
[email protected]432115822011-07-10 15:52:272667 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532668 if (profile_ !=
2669 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322670 break;
[email protected]6c2381d2011-10-19 02:52:532671 }
[email protected]a4ed6282009-12-14 20:51:162672
[email protected]3a1dc572012-07-31 22:25:132673 extensions::ExtensionHost* host =
2674 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262675
[email protected]fa2416f2011-05-03 08:41:202676 // Mark the extension as terminated and Unload it. We want it to
2677 // be in a consistent state: either fully working or not loaded
2678 // at all, but never half-crashed. We do it in a PostTask so
2679 // that other handlers of this notification will still have
2680 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362681 MessageLoop::current()->PostTask(
2682 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362683 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202684 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012685 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202686 host->extension()));
[email protected]31f77262009-12-02 20:48:532687 break;
2688 }
[email protected]432115822011-07-10 15:52:272689 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302690 content::RenderProcessHost* process =
2691 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192692 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302693 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192694 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2695 break;
2696
[email protected]15397b42012-05-16 23:56:062697 // Extensions need to know the channel for API restrictions.
2698 process->Send(new ExtensionMsg_SetChannel(
[email protected]5243df072012-07-11 21:41:012699 extensions::Feature::GetCurrentChannel()));
[email protected]15397b42012-05-16 23:56:062700
[email protected]77a6970c2011-04-23 16:58:562701 // Valid extension function names, used to setup bindings in renderer.
2702 std::vector<std::string> function_names;
2703 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2704 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532705
[email protected]77a6970c2011-04-23 16:58:562706 // Scripting whitelist. This is modified by tests and must be communicated
2707 // to renderers.
2708 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2709 *Extension::GetScriptingWhitelist()));
2710
2711 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532712 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462713 for (ExtensionSet::const_iterator iter = extensions_.begin();
2714 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492715 // Renderers don't need to know about themes.
2716 if (!(*iter)->is_theme())
2717 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562718 }
[email protected]9776e82e2011-11-15 02:17:532719 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562720 break;
2721 }
[email protected]432115822011-07-10 15:52:272722 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302723 content::RenderProcessHost* process =
2724 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192725 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302726 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192727 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2728 break;
2729
[email protected]6bc04fd82011-12-04 02:29:352730 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462731 BrowserThread::PostTask(
2732 BrowserThread::IO, FROM_HERE,
2733 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
[email protected]31d8f5f22012-04-02 15:22:082734 system_->info_map(),
[email protected]f3b1a082011-11-18 00:34:302735 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212736 break;
2737 }
[email protected]d91268022011-08-26 13:17:372738 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392739 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372740 break;
2741 }
[email protected]0db124b02012-11-07 04:55:052742 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
2743 extensions::ExtensionHost* host =
2744 content::Details<extensions::ExtensionHost>(details).ptr();
[email protected]e7aa7b7e2012-11-27 04:51:222745 std::string extension_id = host->extension_id();
[email protected]399583b2012-12-11 09:33:422746 if (delayed_updates_for_idle_.Contains(extension_id)) {
[email protected]e7aa7b7e2012-11-27 04:51:222747 // We were waiting for this extension to become idle, it now might have,
2748 // so maybe finish installation.
2749 MessageLoop::current()->PostDelayedTask(
[email protected]0db124b02012-11-07 04:55:052750 FROM_HERE,
[email protected]6f6101832012-11-27 22:10:482751 base::Bind(&ExtensionService::MaybeFinishDelayedInstallation,
[email protected]0db124b02012-11-07 04:55:052752 AsWeakPtr(), extension_id),
2753 base::TimeDelta::FromSeconds(kUpdateIdleDelay));
[email protected]0db124b02012-11-07 04:55:052754 }
2755 break;
2756 }
[email protected]92dd8d92013-02-26 00:41:082757 case chrome::NOTIFICATION_UPGRADE_RECOMMENDED: {
2758 // Notify extensions that chrome update is available.
2759 extensions::RuntimeEventRouter::DispatchOnBrowserUpdateAvailableEvent(
2760 profile_);
2761 break;
2762 }
[email protected]aa96d3a2010-08-21 08:45:252763
[email protected]4814b512009-11-07 00:12:292764 default:
2765 NOTREACHED() << "Unexpected notification type.";
2766 }
2767}
2768
[email protected]90bb38d2012-11-14 18:36:032769void ExtensionService::OnExtensionInstallPrefChanged() {
[email protected]a6a7ced2012-11-01 17:24:182770 IdentifyAlertableExtensions();
2771 CheckManagementPolicy();
2772}
2773
[email protected]eaa7dd182010-12-14 11:09:002774bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182775 return !GetAppIds().empty();
2776}
[email protected]377011d2010-07-20 04:18:502777
[email protected]eaa7dd182010-12-14 11:09:002778ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182779 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462780 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502781 it != extensions_.end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:402782 if ((*it)->is_app() && (*it)->location() != Manifest::COMPONENT)
[email protected]ec5b50d2010-10-09 16:35:182783 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502784 }
2785
[email protected]ec5b50d2010-10-09 16:35:182786 return result;
[email protected]377011d2010-07-20 04:18:502787}
[email protected]d7e9a862010-11-03 21:57:492788
[email protected]dc9a74f72012-08-17 18:07:212789bool ExtensionService::IsBackgroundPageReady(const Extension* extension) const {
2790 if (!extension->has_persistent_background_page())
2791 return true;
2792 ExtensionRuntimeDataMap::const_iterator it =
2793 extension_runtime_data_.find(extension->id());
2794 return it == extension_runtime_data_.end() ? false :
2795 it->second.background_page_ready;
[email protected]d7e9a862010-11-03 21:57:492796}
2797
[email protected]eaa7dd182010-12-14 11:09:002798void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282799 DCHECK(extension->has_background_page());
[email protected]d7e9a862010-11-03 21:57:492800 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072801 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272802 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532803 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072804 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492805}
2806
[email protected]65992d52012-09-19 23:05:312807void ExtensionService::InspectBackgroundPage(const Extension* extension) {
2808 DCHECK(extension);
2809
[email protected]fdd679b2012-11-15 20:49:392810 ExtensionProcessManager* pm = system_->process_manager();
[email protected]65992d52012-09-19 23:05:312811 extensions::LazyBackgroundTaskQueue* queue =
[email protected]fdd679b2012-11-15 20:49:392812 system_->lazy_background_task_queue();
[email protected]65992d52012-09-19 23:05:312813
2814 extensions::ExtensionHost* host =
2815 pm->GetBackgroundHostForExtension(extension->id());
2816 if (host) {
2817 InspectExtensionHost(host);
2818 } else {
2819 queue->AddPendingTask(
2820 profile_, extension->id(),
2821 base::Bind(&ExtensionService::InspectExtensionHost,
2822 base::Unretained(this)));
2823 }
2824}
2825
[email protected]dc9a74f72012-08-17 18:07:212826bool ExtensionService::IsBeingUpgraded(const Extension* extension) const {
2827 ExtensionRuntimeDataMap::const_iterator it =
2828 extension_runtime_data_.find(extension->id());
2829 return it == extension_runtime_data_.end() ? false :
2830 it->second.being_upgraded;
[email protected]d7e9a862010-11-03 21:57:492831}
2832
[email protected]eaa7dd182010-12-14 11:09:002833void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492834 bool value) {
2835 extension_runtime_data_[extension->id()].being_upgraded = value;
2836}
[email protected]1bead0712010-11-27 17:41:532837
[email protected]dc9a74f72012-08-17 18:07:212838bool ExtensionService::HasUsedWebRequest(const Extension* extension) const {
2839 ExtensionRuntimeDataMap::const_iterator it =
2840 extension_runtime_data_.find(extension->id());
2841 return it == extension_runtime_data_.end() ? false :
2842 it->second.has_used_webrequest;
[email protected]39a5b532011-10-22 01:47:072843}
2844
2845void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2846 bool value) {
2847 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2848}
2849
[email protected]cebc3dc2011-04-18 17:15:002850void ExtensionService::RegisterNaClModule(const GURL& url,
2851 const std::string& mime_type) {
2852 NaClModuleInfo info;
2853 info.url = url;
2854 info.mime_type = mime_type;
2855
2856 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2857 nacl_module_list_.push_front(info);
2858}
2859
2860void ExtensionService::UnregisterNaClModule(const GURL& url) {
2861 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2862 DCHECK(iter != nacl_module_list_.end());
2863 nacl_module_list_.erase(iter);
2864}
2865
[email protected]ed0ba002011-05-26 16:55:132866void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332867 // An extension has been added which has a nacl_module component, which means
2868 // there is a MIME type that module wants to handle, so we need to add that
2869 // MIME type to plugins which handle NaCl modules in order to allow the
2870 // individual modules to handle these types.
[email protected]650b2d52013-02-10 03:41:452871 base::FilePath path;
[email protected]1e127022011-11-29 21:11:472872 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2873 return;
2874 const content::PepperPluginInfo* pepper_info =
2875 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2876 if (!pepper_info)
2877 return;
[email protected]300c0ea2011-07-15 23:04:332878
[email protected]1e127022011-11-29 21:11:472879 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2880 // Check each MIME type the plugins handle for the NaCl MIME type.
2881 for (mime_iter = pepper_info->mime_types.begin();
2882 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2883 if (mime_iter->mime_type == kNaClPluginMimeType) {
2884 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332885
[email protected]1e127022011-11-29 21:11:472886 PluginService::GetInstance()->
2887 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332888
[email protected]1e127022011-11-29 21:11:472889 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332890
[email protected]1e127022011-11-29 21:11:472891 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2892 nacl_module_list_.begin();
2893 iter != nacl_module_list_.end(); ++iter) {
2894 // Add the MIME type specified in the extension to this NaCl plugin,
2895 // With an extra "nacl" argument to specify the location of the NaCl
2896 // manifest file.
2897 webkit::WebPluginMimeType mime_type_info;
2898 mime_type_info.mime_type = iter->mime_type;
2899 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2900 mime_type_info.additional_param_values.push_back(
2901 UTF8ToUTF16(iter->url.spec()));
2902 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332903 }
[email protected]1e127022011-11-29 21:11:472904
2905 PluginService::GetInstance()->RefreshPlugins();
[email protected]c6f3dea2012-01-14 02:23:112906 PluginService::GetInstance()->RegisterInternalPlugin(info, true);
[email protected]1e127022011-11-29 21:11:472907 // This plugin has been modified, no need to check the rest of its
2908 // types, but continue checking other plugins.
2909 break;
[email protected]ed0ba002011-05-26 16:55:132910 }
2911 }
[email protected]ed0ba002011-05-26 16:55:132912}
2913
[email protected]cebc3dc2011-04-18 17:15:002914ExtensionService::NaClModuleInfoList::iterator
2915 ExtensionService::FindNaClModule(const GURL& url) {
2916 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2917 iter != nacl_module_list_.end(); ++iter) {
2918 if (iter->url == url)
2919 return iter;
2920 }
2921 return nacl_module_list_.end();
2922}
[email protected]6cafe35a2012-08-27 22:40:502923
[email protected]32b36c62012-10-24 05:37:412924void ExtensionService::DoPostLoadTasks(const Extension* extension) {
2925 std::map<std::string, int>::iterator it =
2926 on_load_events_.find(extension->id());
2927 if (it == on_load_events_.end())
2928 return;
2929
2930 int events_to_fire = it->second;
2931 extensions::LazyBackgroundTaskQueue* queue =
2932 system_->lazy_background_task_queue();
2933 if (queue->ShouldEnqueueTask(profile(), extension)) {
2934 if (events_to_fire & EVENT_LAUNCHED)
[email protected]6cafe35a2012-08-27 22:40:502935 queue->AddPendingTask(profile(), extension->id(),
2936 base::Bind(&ExtensionService::LaunchApplication));
[email protected]32b36c62012-10-24 05:37:412937 if (events_to_fire & EVENT_RESTARTED)
2938 queue->AddPendingTask(profile(), extension->id(),
2939 base::Bind(&ExtensionService::RestartApplication));
[email protected]6cafe35a2012-08-27 22:40:502940 }
[email protected]32b36c62012-10-24 05:37:412941
2942 on_load_events_.erase(it);
[email protected]6cafe35a2012-08-27 22:40:502943}
2944
2945// static
2946void ExtensionService::LaunchApplication(
2947 extensions::ExtensionHost* extension_host) {
2948 if (!extension_host)
2949 return;
2950
2951#if !defined(OS_ANDROID)
2952 extensions::LaunchPlatformApp(extension_host->profile(),
2953 extension_host->extension(),
[email protected]650b2d52013-02-10 03:41:452954 NULL, base::FilePath());
[email protected]6cafe35a2012-08-27 22:40:502955#endif
2956}
[email protected]65992d52012-09-19 23:05:312957
[email protected]32b36c62012-10-24 05:37:412958// static
2959void ExtensionService::RestartApplication(
2960 extensions::ExtensionHost* extension_host) {
2961 if (!extension_host)
2962 return;
2963
2964#if !defined(OS_ANDROID)
2965 extensions::AppEventRouter::DispatchOnRestartedEvent(
2966 extension_host->profile(), extension_host->extension());
2967#endif
2968}
2969
2970bool ExtensionService::HasShellWindows(const std::string& extension_id) {
2971 const Extension* current_extension = GetExtensionById(extension_id, false);
2972 return current_extension && current_extension->is_platform_app() &&
2973 !extensions::ShellWindowRegistry::Get(profile_)->
2974 GetShellWindowsForApp(extension_id).empty();
2975}
2976
[email protected]65992d52012-09-19 23:05:312977void ExtensionService::InspectExtensionHost(
2978 extensions::ExtensionHost* host) {
2979 if (host)
2980 DevToolsWindow::OpenDevToolsWindow(host->render_view_host());
2981}
[email protected]612a1cb12012-10-17 13:18:032982
2983bool ExtensionService::ShouldEnableOnInstall(const Extension* extension) {
2984 // Extensions installed by policy can't be disabled. So even if a previous
2985 // installation disabled the extension, make sure it is now enabled.
2986 if (system_->management_policy()->MustRemainEnabled(extension, NULL))
2987 return true;
2988
2989 if (extension_prefs_->IsExtensionDisabled(extension->id()))
2990 return false;
2991
[email protected]41070e8d2012-10-24 01:34:362992 if (FeatureSwitch::prompt_for_external_extensions()->IsEnabled()) {
[email protected]612a1cb12012-10-17 13:18:032993 // External extensions are initially disabled. We prompt the user before
[email protected]2c495c42013-01-04 21:49:542994 // enabling them. Hosted apps are excepted because they are not dangerous
2995 // (they need to be launched by the user anyway).
[email protected]1d5e58b2013-01-31 08:41:402996 if (extension->GetType() != Manifest::TYPE_HOSTED_APP &&
2997 Manifest::IsExternalLocation(extension->location()) &&
[email protected]612a1cb12012-10-17 13:18:032998 !extension_prefs_->IsExternalExtensionAcknowledged(extension->id())) {
2999 return false;
3000 }
3001 }
[email protected]612a1cb12012-10-17 13:18:033002
3003 return true;
3004}
[email protected]0db124b02012-11-07 04:55:053005
3006bool ExtensionService::IsExtensionIdle(const std::string& extension_id) const {
[email protected]d9a61e12012-11-14 02:43:473007 ExtensionProcessManager* process_manager = system_->process_manager();
[email protected]fc332ae2012-11-14 20:17:333008 DCHECK(process_manager);
[email protected]0db124b02012-11-07 04:55:053009 extensions::ExtensionHost* host =
[email protected]d9a61e12012-11-14 02:43:473010 process_manager->GetBackgroundHostForExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223011 if (host)
3012 return false;
3013 return process_manager->GetRenderViewHostsForExtension(extension_id).empty();
3014}
3015
3016bool ExtensionService::ShouldDelayExtensionUpdate(
3017 const std::string& extension_id,
3018 bool wait_for_idle) const {
3019 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
3020
3021 // If delayed updates are globally disabled, or just for this extension,
3022 // don't delay.
3023 if (!install_updates_when_idle_ || !wait_for_idle)
3024 return false;
3025
[email protected]695b5712012-12-06 23:55:283026 const Extension* old = GetInstalledExtension(extension_id);
[email protected]e7aa7b7e2012-11-27 04:51:223027 // If there is no old extension, this is not an update, so don't delay.
3028 if (!old)
3029 return false;
3030
3031 if (old->has_persistent_background_page()) {
3032 // Delay installation if the extension listens for the onUpdateAvailable
3033 // event.
3034 return system_->event_router()->ExtensionHasEventListener(
3035 extension_id, kOnUpdateAvailableEvent);
3036 } else {
3037 // Delay installation if the extension is not idle.
3038 return !IsExtensionIdle(extension_id);
3039 }
[email protected]0db124b02012-11-07 04:55:053040}
[email protected]fdd679b2012-11-15 20:49:393041
[email protected]399583b2012-12-11 09:33:423042void ExtensionService::GarbageCollectIsolatedStorage() {
[email protected]650b2d52013-02-10 03:41:453043 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
3044 new base::hash_set<base::FilePath>());
[email protected]399583b2012-12-11 09:33:423045 for (ExtensionSet::const_iterator it = extensions_.begin();
3046 it != extensions_.end(); ++it) {
3047 if ((*it)->is_storage_isolated()) {
3048 active_paths->insert(
3049 BrowserContext::GetStoragePartitionForSite(
3050 profile_,
3051 GetSiteForExtensionId((*it)->id()))->GetPath());
3052 }
3053 }
3054
3055 DCHECK(!installs_delayed());
3056 set_installs_delayed(true);
3057 BrowserContext::GarbageCollectStoragePartitions(
3058 profile_, active_paths.Pass(),
3059 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
3060 AsWeakPtr()));
3061}
3062
3063void ExtensionService::OnGarbageCollectIsolatedStorageFinished() {
3064 set_installs_delayed(false);
3065 for (ExtensionSet::const_iterator it = delayed_installs_.begin();
3066 it != delayed_installs_.end();
3067 ++it) {
3068 FinishDelayedInstallation((*it)->id());
3069 }
3070 for (ExtensionSet::const_iterator it = delayed_updates_for_idle_.begin();
3071 it != delayed_updates_for_idle_.end();
3072 ++it) {
3073 MaybeFinishDelayedInstallation((*it)->id());
3074 }
3075 delayed_installs_.Clear();
3076}
3077
3078void ExtensionService::OnNeedsToGarbageCollectIsolatedStorage() {
3079 extension_prefs_->SetNeedsStorageGarbageCollection(true);
3080}
3081
[email protected]fdd679b2012-11-15 20:49:393082void ExtensionService::OnBlacklistUpdated() {
[email protected]695b5712012-12-06 23:55:283083 blacklist_->GetBlacklistedIDs(
3084 GenerateInstalledExtensionsSet()->GetIDs(),
3085 base::Bind(&ExtensionService::ManageBlacklist,
3086 AsWeakPtr(),
3087 blacklisted_extensions_.GetIDs()));
3088}
3089
3090void ExtensionService::ManageBlacklist(
3091 const std::set<std::string>& old_blacklisted_ids,
3092 const std::set<std::string>& new_blacklisted_ids) {
3093 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3094
3095 std::set<std::string> no_longer_blacklisted;
3096 std::set_difference(old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3097 new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3098 std::inserter(no_longer_blacklisted,
3099 no_longer_blacklisted.begin()));
3100 std::set<std::string> not_yet_blacklisted;
3101 std::set_difference(new_blacklisted_ids.begin(), new_blacklisted_ids.end(),
3102 old_blacklisted_ids.begin(), old_blacklisted_ids.end(),
3103 std::inserter(not_yet_blacklisted,
3104 not_yet_blacklisted.begin()));
3105
3106 for (std::set<std::string>::iterator it = no_longer_blacklisted.begin();
3107 it != no_longer_blacklisted.end(); ++it) {
3108 scoped_refptr<const Extension> extension =
3109 blacklisted_extensions_.GetByID(*it);
3110 DCHECK(extension);
3111 if (!extension)
3112 continue;
3113 blacklisted_extensions_.Remove(*it);
3114 AddExtension(extension);
3115 }
3116
3117 for (std::set<std::string>::iterator it = not_yet_blacklisted.begin();
3118 it != not_yet_blacklisted.end(); ++it) {
3119 scoped_refptr<const Extension> extension = GetInstalledExtension(*it);
3120 DCHECK(extension);
3121 if (!extension)
3122 continue;
3123 blacklisted_extensions_.Insert(extension);
3124 UnloadExtension(*it, extension_misc::UNLOAD_REASON_BLACKLIST);
3125 }
3126
3127 IdentifyAlertableExtensions();
[email protected]fdd679b2012-11-15 20:49:393128}