blob: f8dd4cc5b447f48595b6ab934aa56d61fca58e5a [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]97d2f1d2011-01-15 00:41:088#include <set>
[email protected]654512b2010-09-01 02:09:429
[email protected]24b538a2010-02-27 01:22:4410#include "base/basictypes.h"
[email protected]05aad2da2011-10-28 10:12:3711#include "base/bind.h"
[email protected]e5af875f2011-10-10 21:09:1412#include "base/callback.h"
[email protected]62433d32011-10-12 22:33:1213#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2514#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3615#include "base/logging.h"
[email protected]49098f702011-10-13 03:47:1816#include "base/metrics/field_trial.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/histogram.h"
[email protected]1e127022011-11-29 21:11:4718#include "base/path_service.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]24b538a2010-02-27 01:22:4420#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2521#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2522#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0423#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0624#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3625#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5926#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1927#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3328#include "base/version.h"
[email protected]a692a952011-11-28 08:23:5429#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]23d5f172011-10-25 05:49:5330#include "chrome/browser/bookmarks/bookmark_extension_api.h"
[email protected]15730c42009-09-03 00:03:2031#include "chrome/browser/browser_process.h"
[email protected]dfba8762011-09-02 12:49:5432#include "chrome/browser/chrome_plugin_service_filter.h"
[email protected]cdcf60242011-11-04 15:53:4333#include "chrome/browser/download/download_extension_api.h"
[email protected]a1755bd2011-12-09 00:40:2034#include "chrome/browser/extensions/api/socket/socket_api_controller.h"
[email protected]af9db5f2011-10-05 05:13:1535#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]18cc5a52011-04-15 16:03:3836#include "chrome/browser/extensions/apps_promo.h"
[email protected]d8c8f25f2011-11-02 18:18:0137#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1738#include "chrome/browser/extensions/crx_installer.h"
[email protected]49098f702011-10-13 03:47:1839#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]b68d5ed2009-04-16 02:41:2840#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4441#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3242#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2443#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]e5af875f2011-10-10 21:09:1444#include "chrome/browser/extensions/extension_global_error.h"
[email protected]b1748b1d82009-11-30 20:32:5645#include "chrome/browser/extensions/extension_host.h"
[email protected]8baf0dbb2011-11-27 22:47:0246#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]7596ce72010-08-30 05:10:4647#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4048#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2949#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1150#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]3569b502012-01-12 20:08:2351#include "chrome/browser/extensions/extension_sorting.h"
[email protected]19eb80152011-02-26 00:28:4352#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0453#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]93fd78f42009-07-10 16:43:1754#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1055#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5256#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1457#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3858#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0159#include "chrome/browser/extensions/installed_loader.h"
[email protected]b2907fd2011-03-25 16:43:3760#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]c333e792012-01-06 16:57:3961#include "chrome/browser/extensions/permissions_updater.h"
[email protected]cccdf0aa2011-11-11 03:43:3862#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0163#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]d2e6bd62011-10-24 20:29:0764#include "chrome/browser/history/history_extension_api.h"
[email protected]56ad3792010-05-28 17:45:3365#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0266#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3367#include "chrome/browser/profiles/profile.h"
[email protected]cf593af2011-12-30 05:44:3968#include "chrome/browser/profiles/profile_manager.h"
[email protected]8e5c89a2011-06-07 18:13:3369#include "chrome/browser/search_engines/template_url_service.h"
70#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]3bdba0d2011-08-23 07:17:3071#include "chrome/browser/sync/api/sync_change.h"
[email protected]18280372011-03-22 18:05:2272#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4273#include "chrome/browser/themes/theme_service_factory.h"
[email protected]e5af875f2011-10-10 21:09:1474#include "chrome/browser/ui/browser.h"
[email protected]62433d32011-10-12 22:33:1275#include "chrome/browser/ui/browser_list.h"
[email protected]e5af875f2011-10-10 21:09:1476#include "chrome/browser/ui/global_error_service.h"
77#include "chrome/browser/ui/global_error_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1778#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
[email protected]c9c8b212011-12-13 23:59:4079#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1780#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3181#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]9de83282011-12-12 11:06:3782#include "chrome/browser/web_applications/web_app.h"
[email protected]aab98a52009-12-02 03:22:3583#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2784#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4785#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5486#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5887#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4088#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0889#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1390#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5691#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2192#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1993#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1494#include "chrome/common/url_constants.h"
[email protected]2de307592011-04-05 21:16:5895#include "content/browser/plugin_process_host.h"
[email protected]c38831a12011-10-28 12:44:4996#include "content/public/browser/browser_thread.h"
[email protected]0e12d7d2011-12-01 16:21:4497#include "content/public/browser/devtools_agent_host_registry.h"
98#include "content/public/browser/devtools_manager.h"
[email protected]05aad2da2011-10-28 10:12:3799#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:16100#include "content/public/browser/notification_types.h"
[email protected]3a5180ae2011-12-21 02:39:38101#include "content/public/browser/plugin_service.h"
[email protected]f3b1a082011-11-18 00:34:30102#include "content/public/browser/render_process_host.h"
[email protected]1e127022011-11-29 21:11:47103#include "content/public/common/pepper_plugin_info.h"
[email protected]c10da4b02010-03-25 14:38:32104#include "googleurl/src/gurl.h"
[email protected]9de83282011-12-12 11:06:37105#include "grit/theme_resources.h"
[email protected]1debbbb62010-10-06 17:23:44106#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:44107#include "webkit/database/database_tracker.h"
108#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:31109
[email protected]eed367e2011-04-12 03:43:31110#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13111#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]eed367e2011-04-12 03:43:31112#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01113#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:40114#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13115#include "chrome/browser/chromeos/input_method/input_method_manager.h"
[email protected]8baf0dbb2011-11-27 22:47:02116#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:08117#include "webkit/fileapi/file_system_context.h"
118#include "webkit/fileapi/file_system_mount_point_provider.h"
[email protected]eed367e2011-04-12 03:43:31119#endif
120
[email protected]9a2aebe02011-12-02 21:54:14121#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40122#include "chrome/browser/extensions/extension_input_ui_api.h"
123#endif
124
[email protected]5ef47ec2010-01-28 05:58:05125using base::Time;
[email protected]631bb742011-11-02 11:29:39126using content::BrowserThread;
[email protected]0e12d7d2011-12-01 16:21:44127using content::DevToolsAgentHost;
128using content::DevToolsAgentHostRegistry;
[email protected]3a5180ae2011-12-21 02:39:38129using content::PluginService;
[email protected]5ef47ec2010-01-28 05:58:05130
[email protected]c6d474f82009-12-16 21:11:06131namespace errors = extension_manifest_errors;
132
[email protected]b6ab96d2009-08-20 18:58:19133namespace {
134
[email protected]29d0d4ac2010-09-08 21:10:31135#if defined(OS_LINUX)
136static const int kOmniboxIconPaddingLeft = 2;
137static const int kOmniboxIconPaddingRight = 2;
138#elif defined(OS_MACOSX)
139static const int kOmniboxIconPaddingLeft = 0;
140static const int kOmniboxIconPaddingRight = 2;
141#else
142static const int kOmniboxIconPaddingLeft = 0;
143static const int kOmniboxIconPaddingRight = 0;
144#endif
145
[email protected]300c0ea2011-07-15 23:04:33146const char* kNaClPluginMimeType = "application/x-nacl";
147
[email protected]2de307592011-04-05 21:16:58148static void ForceShutdownPlugin(const FilePath& plugin_path) {
149 PluginProcessHost* plugin =
150 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
151 if (plugin)
152 plugin->ForceShutdown();
153}
154
[email protected]3bdba0d2011-08-23 07:17:30155static bool IsSyncableExtension(const Extension& extension) {
156 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
157}
158
159static bool IsSyncableApp(const Extension& extension) {
160 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
161}
162
[email protected]c6d474f82009-12-16 21:11:06163} // namespace
[email protected]b6ab96d2009-08-20 18:58:19164
[email protected]eaa7dd182010-12-14 11:09:00165ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49166 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57167 being_upgraded(false),
168 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49169}
170
[email protected]eaa7dd182010-12-14 11:09:00171ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49172}
173
[email protected]cebc3dc2011-04-18 17:15:00174ExtensionService::NaClModuleInfo::NaClModuleInfo() {
175}
176
177ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
178}
179
[email protected]eaa7dd182010-12-14 11:09:00180// ExtensionService.
[email protected]6014d672008-12-05 00:38:25181
[email protected]eaa7dd182010-12-14 11:09:00182const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]15d5b472011-11-24 04:17:24183
184const char* ExtensionService::kLocalAppSettingsDirectoryName =
185 "Local App Settings";
186const char* ExtensionService::kLocalExtensionSettingsDirectoryName =
187 "Local Extension Settings";
188const char* ExtensionService::kSyncAppSettingsDirectoryName =
189 "Sync App Settings";
190const char* ExtensionService::kSyncExtensionSettingsDirectoryName =
191 "Sync Extension Settings";
[email protected]494c06e2009-07-25 01:06:42192
[email protected]8e4560b62011-01-14 10:09:14193void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12194 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20195
[email protected]7a4c6852010-09-16 03:44:22196 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21197 ProviderCollection::const_iterator i;
198 for (i = external_extension_providers_.begin();
199 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14200 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21201 if (i->get()->HasExtension(id))
202 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22203 }
204
[email protected]0f48fca2011-05-19 18:46:35205 // We get the list of external extensions to check from preferences.
206 // It is possible that an extension has preferences but is not loaded.
207 // For example, an extension that requires experimental permissions
208 // will not be loaded if the experimental command line flag is not used.
209 // In this case, do not uninstall.
210 const Extension* extension = GetInstalledExtension(id);
211 if (!extension) {
212 // We can't call UninstallExtension with an unloaded/invalid
213 // extension ID.
214 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
215 << "with id: " << id;
216 return;
217 }
[email protected]d6ebc9792011-04-07 18:18:33218 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22219}
220
[email protected]8e4560b62011-01-14 10:09:14221void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22222 external_extension_providers_.clear();
223}
224
[email protected]8e4560b62011-01-14 10:09:14225void ExtensionService::AddProviderForTesting(
226 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12227 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21228 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14229 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22230}
231
[email protected]8e4560b62011-01-14 10:09:14232void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22233 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42234 const GURL& update_url,
235 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38236 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
237 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20238
[email protected]8a87a5332011-08-11 17:54:59239 const Extension* extension = GetExtensionById(id, true);
240 if (extension) {
241 // Already installed. Skip this install if the current location has
242 // higher priority than |location|.
243 Extension::Location current = extension->location();
244 if (current == Extension::GetHigherPriorityLocation(current, location))
245 return;
246 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22247 }
[email protected]b2907fd2011-03-25 16:43:37248 pending_extension_manager()->AddFromExternalUpdateUrl(
249 id, update_url, location);
[email protected]50067e52011-10-20 23:17:07250 external_extension_url_added_ = true;
[email protected]7a4c6852010-09-16 03:44:22251}
252
[email protected]454434f2011-08-24 04:00:00253// If a download url matches one of these patterns and has a referrer of the
254// webstore, then we're willing to treat that as a gallery download.
255static const char* kAllowedDownloadURLPatterns[] = {
256 "https://clients2.google.com/service/update2*",
257 "https://clients2.googleusercontent.com/crx/*"
258};
259
[email protected]eaa7dd182010-12-14 11:09:00260bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12261 const GURL& referrer_url) {
[email protected]615d88f2011-12-13 01:47:44262 const Extension* download_extension =
263 extensions_.GetHostedAppByURL(ExtensionURLInfo(download_url));
264 const Extension* referrer_extension =
265 extensions_.GetHostedAppByURL(ExtensionURLInfo(referrer_url));
[email protected]9adb9692010-10-29 23:14:02266 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07267
268 bool referrer_valid = (referrer_extension == webstore_app);
269 bool download_valid = (download_extension == webstore_app);
270
[email protected]454434f2011-08-24 04:00:00271 // We also allow the download to be from a small set of trusted paths.
272 if (!download_valid) {
273 for (size_t i = 0; i < arraysize(kAllowedDownloadURLPatterns); i++) {
274 URLPattern pattern(URLPattern::SCHEME_HTTPS,
275 kAllowedDownloadURLPatterns[i]);
276 if (pattern.MatchesURL(download_url)) {
277 download_valid = true;
278 break;
279 }
280 }
281 }
282
[email protected]d3071992010-10-08 15:24:07283 // If the command-line gallery URL is set, then be a bit more lenient.
284 GURL store_url =
285 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
286 switches::kAppsGalleryURL));
287 if (!store_url.is_empty()) {
288 std::string store_tld =
289 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
290 if (!referrer_valid) {
291 std::string referrer_tld =
292 net::RegistryControlledDomainService::GetDomainAndRegistry(
293 referrer_url);
294 // The referrer gets stripped when transitioning from https to http,
295 // or when hitting an unknown test cert and that commonly happens in
296 // testing environments. Given this, we allow an empty referrer when
297 // the command-line flag is set.
298 // Otherwise, the TLD must match the TLD of the command-line url.
299 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
300 }
301
302 if (!download_valid) {
303 std::string download_tld =
304 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50305 download_url);
[email protected]d3071992010-10-08 15:24:07306
307 // Otherwise, the TLD must match the TLD of the command-line url.
308 download_valid = (download_tld == store_tld);
309 }
310 }
311
312 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23313}
314
[email protected]d9696672011-03-15 22:45:09315const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:44316 const Extension* extension = extensions_.GetExtensionOrAppByURL(
317 ExtensionURLInfo(url));
318 if (extension && extension->is_app())
319 return extension;
[email protected]d9696672011-03-15 22:45:09320
321 return NULL;
322}
323
324bool ExtensionService::IsInstalledApp(const GURL& url) {
325 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27326}
327
[email protected]da5683db2011-04-23 17:12:21328void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
329 const Extension* app) {
330 installed_app_hosts_[renderer_child_id] = app;
331}
332
333const Extension* ExtensionService::GetInstalledAppForRenderer(
334 int renderer_child_id) {
335 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
336 if (i == installed_app_hosts_.end())
337 return NULL;
338 return i->second;
339}
340
[email protected]6aeac8342010-10-01 20:21:18341// static
[email protected]d6ebc9792011-04-07 18:18:33342// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30343// --uninstall-extension, and to uninstall an extension via sync. The LOG
344// statements within this function are used to inform the user if the uninstall
345// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00346bool ExtensionService::UninstallExtensionHelper(
347 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18348 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18349
[email protected]95da88c42011-03-31 10:07:33350 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47351 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33352
[email protected]d6ebc9792011-04-07 18:18:33353 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33354 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18355 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33356 << "id: " << extension_id;
357 return false;
[email protected]6aeac8342010-10-01 20:21:18358 }
359
[email protected]d6ebc9792011-04-07 18:18:33360 // The following call to UninstallExtension will not allow an uninstall of a
361 // policy-controlled extension.
362 std::string error;
363 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
364 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
365 << ": " << error;
366 return false;
367 }
[email protected]95da88c42011-03-31 10:07:33368
[email protected]6aeac8342010-10-01 20:21:18369 return true;
370}
371
[email protected]eaa7dd182010-12-14 11:09:00372ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24373 const CommandLine* command_line,
374 const FilePath& install_directory,
375 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03376 bool autoupdate_enabled,
377 bool extensions_enabled)
[email protected]d8c8f25f2011-11-02 18:18:01378 : profile_(profile),
[email protected]73c47932010-12-06 18:13:43379 extension_prefs_(extension_prefs),
[email protected]cccdf0aa2011-11-11 03:43:38380 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36381 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23382 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03383 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13384 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55385 ready_(false),
[email protected]14908b72011-04-20 06:54:36386 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23387 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15388 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]18cc5a52011-04-15 16:03:38389 apps_promo_(profile->GetPrefs()),
[email protected]7c1490da2011-10-11 18:53:25390 event_routers_initialized_(false),
[email protected]a1755bd2011-12-09 00:40:20391 extension_warnings_(profile),
[email protected]e1fae1c2011-12-13 22:17:17392 socket_controller_(NULL),
[email protected]b39e7a88b2012-01-10 21:43:17393 tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]a29a517a2011-01-21 21:11:12394 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20395
[email protected]36a784c2009-06-23 06:21:08396 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23397 if (command_line->HasSwitch(switches::kDisableExtensions)) {
398 extensions_enabled_ = false;
399 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
400 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18401 }
[email protected]36a784c2009-06-23 06:21:08402
[email protected]432115822011-07-10 15:52:27403 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07404 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27405 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07406 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27407 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07408 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28409 pref_change_registrar_.Init(profile->GetPrefs());
410 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
411 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29412
[email protected]93fd78f42009-07-10 16:43:17413 // Set up the ExtensionUpdater
414 if (autoupdate_enabled) {
415 int update_frequency = kDefaultUpdateFrequencySeconds;
416 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25417 base::StringToInt(command_line->GetSwitchValueASCII(
418 switches::kExtensionsUpdateFrequency),
419 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17420 }
[email protected]82b6e512011-04-12 20:33:27421 updater_.reset(new ExtensionUpdater(this,
422 extension_prefs,
423 profile->GetPrefs(),
424 profile,
425 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17426 }
427
[email protected]25ae0152011-11-18 14:40:02428 component_loader_.reset(
429 new extensions::ComponentLoader(this,
430 profile->GetPrefs(),
431 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14432
[email protected]af9db5f2011-10-05 05:13:15433 app_notification_manager_->Init();
434
[email protected]0436b102011-04-15 18:30:03435 if (extensions_enabled_) {
[email protected]1f2f3002011-12-28 00:40:21436 if (!command_line->HasSwitch(switches::kImport) &&
437 !command_line->HasSwitch(switches::kImportFromFile)) {
438 ExternalExtensionProviderImpl::CreateExternalProviders(
439 this, profile_, &external_extension_providers_);
440 }
[email protected]873531342011-03-09 12:16:05441 }
[email protected]b671760b2010-07-15 21:13:47442
[email protected]aa96d3a2010-08-21 08:45:25443 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31444 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47445 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31446 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
447 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07448
449 // How long is the path to the Extensions directory?
450 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
451 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25452}
453
[email protected]84df8332011-12-06 18:22:46454const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45455 return &extensions_;
456}
457
[email protected]84df8332011-12-06 18:22:46458const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45459 return &disabled_extensions_;
460}
461
[email protected]84df8332011-12-06 18:22:46462const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20463 return &terminated_extensions_;
464}
465
[email protected]b2907fd2011-03-25 16:43:37466PendingExtensionManager* ExtensionService::pending_extension_manager() {
467 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45468}
469
[email protected]eaa7dd182010-12-14 11:09:00470ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17471 // No need to unload extensions here because they are profile-scoped, and the
472 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17473
[email protected]8e4560b62011-01-14 10:09:14474 ProviderCollection::const_iterator i;
475 for (i = external_extension_providers_.begin();
476 i != external_extension_providers_.end(); ++i) {
477 ExternalExtensionProviderInterface* provider = i->get();
478 provider->ServiceShutdown();
479 }
[email protected]a1755bd2011-12-09 00:40:20480
481 // TODO(miket): if we find ourselves adding more and more per-API
482 // controllers, we should manage them all with an
483 // APIControllerController (still working on that name).
[email protected]e1fae1c2011-12-13 22:17:17484 if (socket_controller_) {
485 // If this check failed, then a unit test was using sockets but didn't
486 // provide the IO thread message loop needed for those sockets to do their
487 // job (including destroying themselves at shutdown).
488 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO));
[email protected]cff06c572012-01-02 20:03:05489 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, socket_controller_);
[email protected]e1fae1c2011-12-13 22:17:17490 }
[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() {
506 CheckForExternalUpdates();
507
508 GarbageCollectExtensions();
509
510 // Idempotent, so although there is a possible race if the import
511 // process finished sometime in the middle of ProfileImpl::InitExtensions,
512 // it cannot happen twice.
513 InitEventRouters();
[email protected]d91268022011-08-26 13:17:37514}
515
[email protected]eaa7dd182010-12-14 11:09:00516void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18517 if (event_routers_initialized_)
518 return;
519
[email protected]370e9502011-09-14 19:31:44520 downloads_event_router_.reset(new ExtensionDownloadsEventRouter(profile_));
[email protected]d2e6bd62011-10-24 20:29:07521 history_event_router_.reset(new HistoryExtensionEventRouter());
[email protected]f825df22011-06-28 17:36:10522 history_event_router_->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08523 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
524 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40525 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]23d5f172011-10-25 05:49:53526 bookmark_event_router_.reset(new BookmarkExtensionEventRouter(
[email protected]f825df22011-06-28 17:36:10527 profile_->GetBookmarkModel()));
528 bookmark_event_router_->Init();
529 cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_));
530 cookies_event_router_->Init();
531 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
532 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11533 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10534 web_navigation_event_router_.reset(
535 new ExtensionWebNavigationEventRouter(profile_));
536 web_navigation_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40537
[email protected]eed367e2011-04-12 03:43:31538#if defined(OS_CHROMEOS)
[email protected]b16fc2e2011-07-21 21:32:58539 file_browser_event_router_.reset(
540 new ExtensionFileBrowserEventRouter(profile_));
541 file_browser_event_router_->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52542
543 input_method_event_router_.reset(
544 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01545
[email protected]c8bf09be2011-06-25 03:22:27546 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]8baf0dbb2011-11-27 22:47:02547 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31548#endif
[email protected]27072cad2011-05-09 19:46:40549
[email protected]9a2aebe02011-12-02 21:54:14550#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40551 ExtensionInputUiEventRouter::GetInstance()->Init();
552#endif
553
[email protected]b3d62312b12010-10-14 21:10:18554 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37555}
556
[email protected]b2907fd2011-03-25 16:43:37557const Extension* ExtensionService::GetExtensionById(
558 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47559 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44560}
561
[email protected]eaa7dd182010-12-14 11:09:00562void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12563 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20564
[email protected]fa6a9102010-11-22 15:38:50565 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17566 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32567
[email protected]d8c8f25f2011-11-02 18:18:01568 component_loader_->LoadAll();
569 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57570
[email protected]7a161db2011-12-15 20:45:51571 // If we are running in the import process, don't bother initializing the
572 // extension service since this can interfere with the main browser process
573 // that is already running an extension service for this profile.
574 // TODO(aa): can we start up even less of ExtensionService?
575 // http://crbug.com/107636
[email protected]1f2f3002011-12-28 00:40:21576 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kImport) &&
577 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kImportFromFile)) {
[email protected]cf593af2011-12-30 05:44:39578 if (g_browser_process->profile_manager() &&
579 g_browser_process->profile_manager()->will_import()) {
580 RegisterForImportFinished();
581 } else {
582 // TODO(erikkay) this should probably be deferred to a future point
583 // rather than running immediately at startup.
584 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57585
[email protected]cf593af2011-12-30 05:44:39586 // TODO(erikkay) this should probably be deferred as well.
587 GarbageCollectExtensions();
588 }
[email protected]7a161db2011-12-15 20:45:51589 }
[email protected]6014d672008-12-05 00:38:25590}
591
[email protected]420a0ec2011-06-01 01:07:03592bool ExtensionService::UpdateExtension(
593 const std::string& id,
594 const FilePath& extension_path,
595 const GURL& download_url,
596 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12597 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20598
[email protected]b2907fd2011-03-25 16:43:37599 PendingExtensionInfo pending_extension_info;
600 bool is_pending_extension = pending_extension_manager_.GetById(
601 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32602
[email protected]8001df22011-04-28 19:59:47603 const Extension* extension =
604 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41605 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33606 LOG(WARNING) << "Will not update extension " << id
607 << " because it is not installed or pending";
608 // Delete extension_path since we're not creating a CrxInstaller
609 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36610 if (!BrowserThread::PostTask(
611 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36612 base::Bind(
[email protected]7296f2762011-11-21 19:23:44613 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36614 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03615
616 return false;
[email protected]e957fe52009-06-23 16:51:05617 }
618
[email protected]aa142702010-03-26 01:26:33619 // We want a silent install only for non-pending extensions and
620 // pending extensions that have install_silently set.
621 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37622 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33623 NULL : new ExtensionInstallUI(profile_);
624
[email protected]d8c8f25f2011-11-02 18:18:01625 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16626 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18627 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37628 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41629 else if (extension)
630 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52631 if (pending_extension_info.install_silently())
632 installer->set_allow_silent_install(true);
[email protected]75764512011-12-19 19:54:28633 // If the extension came from sync and its auto-update URL is from the
634 // webstore, treat it as a webstore install. Note that we ignore some older
635 // extensions with blank auto-update URLs because we are mostly concerned
636 // with restrictions on NaCl extensions, which are newer.
637 if ((extension && extension->from_webstore()) ||
638 (!extension && pending_extension_info.is_from_sync() &&
639 extension_urls::IsWebstoreUpdateUrl(
640 pending_extension_info.update_url())))
[email protected]3d729722011-09-20 02:57:09641 installer->set_is_gallery_install(true);
[email protected]6dfbbf82010-03-12 23:09:16642 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06643 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07644 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16645 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03646
647 if (out_crx_installer)
648 *out_crx_installer = installer;
649
650 return true;
[email protected]e957fe52009-06-23 16:51:05651}
652
[email protected]eaa7dd182010-12-14 11:09:00653void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12654 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06655 FilePath path;
[email protected]9adb9692010-10-29 23:14:02656 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40657
[email protected]f17dbd42010-08-16 23:21:10658 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06659 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29660 // If the extension has an inspector open for its background page, detach
661 // the inspector and hang onto a cookie for it, so that we can reattach
662 // later.
663 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
[email protected]06024c62011-10-20 20:57:12664 ExtensionHost* host = manager->GetBackgroundHostForExtension(extension_id);
[email protected]0e12d7d2011-12-01 16:21:44665 if (host && DevToolsAgentHostRegistry::HasDevToolsAgentHost(
666 host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29667 // Look for an open inspector for the background page.
[email protected]0e12d7d2011-12-01 16:21:44668 DevToolsAgentHost* agent =
669 DevToolsAgentHostRegistry::GetDevToolsAgentHost(
670 host->render_view_host());
671 int devtools_cookie =
672 content::DevToolsManager::GetInstance()->DetachClientHost(agent);
[email protected]4814b512009-11-07 00:12:29673 if (devtools_cookie >= 0)
674 orphaned_dev_tools_[extension_id] = devtools_cookie;
675 }
676
[email protected]b65272f2009-08-31 15:47:06677 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10678 DisableExtension(extension_id);
679 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16680 } else {
681 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06682 }
683
[email protected]e6090e42010-03-23 22:44:08684 // Check the installed extensions to see if what we're reloading was already
685 // installed.
686 scoped_ptr<ExtensionInfo> installed_extension(
687 extension_prefs_->GetInstalledExtensionInfo(extension_id));
688 if (installed_extension.get() &&
689 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01690 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08691 } else {
[email protected]d8c8f25f2011-11-02 18:18:01692 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08693 // We should always be able to remember the extension's path. If it's not in
694 // the map, someone failed to update |unloaded_extension_paths_|.
695 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01696 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08697 }
[email protected]9cddd4702009-07-27 22:09:40698}
699
[email protected]fa2416f2011-05-03 08:41:20700bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08701 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20702 bool external_uninstall,
703 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12704 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20705
[email protected]0d6ec3a72011-09-02 02:09:43706 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25707
[email protected]e7afe2452010-08-22 16:19:13708 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48709 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32710
[email protected]95da88c42011-03-31 10:07:33711 // Policy change which triggers an uninstall will always set
712 // |external_uninstall| to true so this is the only way to uninstall
713 // managed extensions.
[email protected]0d6ec3a72011-09-02 02:09:43714 if (!Extension::UserMayDisable(extension->location()) &&
715 !external_uninstall) {
[email protected]ad50def52011-10-19 23:17:07716 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27717 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53718 content::Source<Profile>(profile_),
719 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33720 if (error != NULL) {
721 *error = errors::kCannotUninstallManagedExtension;
722 }
723 return false;
724 }
[email protected]95da88c42011-03-31 10:07:33725
[email protected]3bdba0d2011-08-23 07:17:30726 // Extract the data we need for sync now, but don't actually sync until we've
727 // completed the uninstallation.
728 SyncBundle* sync_bundle = GetSyncBundleForExtension(*extension);
729
730 SyncChange sync_change;
731 if (sync_bundle) {
[email protected]aa7599d2011-10-28 07:24:32732 ExtensionSyncData extension_sync_data(
733 *extension,
734 IsExtensionEnabled(extension_id),
735 IsIncognitoEnabled(extension_id),
[email protected]b2689a902011-12-01 00:41:09736 extension_prefs_->GetAppNotificationClientId(extension_id),
[email protected]168389f2011-12-20 17:12:48737 extension_prefs_->IsAppNotificationDisabled(extension_id),
738 GetAppLaunchOrdinal(extension_id),
739 GetPageOrdinal(extension_id));
[email protected]3bdba0d2011-08-23 07:17:30740 sync_change = extension_sync_data.GetSyncChange(SyncChange::ACTION_DELETE);
741 }
742
[email protected]9b217652010-10-08 22:04:23743 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08744 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49745 RecordPermissionMessagesHistogram(
746 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23747
[email protected]8e5c89a2011-06-07 18:13:33748 TemplateURLService* url_service =
749 TemplateURLServiceFactory::GetForProfile(profile_);
750 if (url_service)
751 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33752
[email protected]831aa212010-03-26 13:55:19753 // Unload before doing more cleanup to ensure that nothing is hanging on to
754 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59755 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19756
[email protected]0d6ec3a72011-09-02 02:09:43757 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19758 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32759
760 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43761 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36762 if (!BrowserThread::PostTask(
763 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36764 base::Bind(
[email protected]14908b72011-04-20 06:54:36765 &extension_file_util::UninstallExtension,
766 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20767 extension_id)))
[email protected]14908b72011-04-20 06:54:36768 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32769 }
770
[email protected]0d6ec3a72011-09-02 02:09:43771 GURL launch_web_url_origin(extension->launch_web_url());
772 launch_web_url_origin = launch_web_url_origin.GetOrigin();
773 bool is_storage_isolated =
774 (extension->is_storage_isolated() &&
775 extension->HasAPIPermission(ExtensionAPIPermission::kExperimental));
776
777 if (extension->is_hosted_app() &&
778 !profile_->GetExtensionSpecialStoragePolicy()->
779 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13780 ExtensionDataDeleter::StartDeleting(
781 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43782 }
[email protected]dc0b5a12011-10-14 00:06:13783 ExtensionDataDeleter::StartDeleting(
784 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43785
[email protected]0dfe05c2011-02-23 23:03:36786 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06787
788 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07789 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27790 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53791 content::Source<Profile>(profile_),
792 content::Details<const std::string>(&extension_id));
[email protected]d6ebc9792011-04-07 18:18:33793
[email protected]3bdba0d2011-08-23 07:17:30794 if (sync_bundle && sync_bundle->HasExtensionId(extension_id)) {
795 sync_bundle->sync_processor->ProcessSyncChanges(
796 FROM_HERE, SyncChangeList(1, sync_change));
797 sync_bundle->synced_extensions.erase(extension_id);
798 }
799
[email protected]333b1de2011-09-12 18:28:50800 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18801 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
802
803 static bool default_apps_trial_exists =
[email protected]568c3312011-12-06 15:36:04804 base::FieldTrialList::TrialExists(kDefaultAppsTrialName);
[email protected]49098f702011-10-13 03:47:18805 if (default_apps_trial_exists) {
806 UMA_HISTOGRAM_ENUMERATION(
807 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
[email protected]568c3312011-12-06 15:36:04808 kDefaultAppsTrialName),
[email protected]49098f702011-10-13 03:47:18809 1, 2);
810 }
[email protected]333b1de2011-09-12 18:28:50811
[email protected]7c1490da2011-10-11 18:53:25812 // Uninstalling one extension might have solved the problems of others.
813 // Therefore, we clear warnings of this type for all extensions.
814 std::set<ExtensionWarningSet::WarningType> warnings;
815 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
816 extension_warnings_.ClearWarnings(warnings);
817
[email protected]d6ebc9792011-04-07 18:18:33818 return true;
[email protected]c10da4b02010-03-25 14:38:32819}
820
[email protected]c3cfb012011-04-06 22:07:35821bool ExtensionService::IsExtensionEnabled(
822 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46823 if (extensions_.Contains(extension_id) ||
824 terminated_extensions_.Contains(extension_id))
[email protected]36429da2011-07-11 20:25:18825 return true;
826
[email protected]84df8332011-12-06 18:22:46827 if (disabled_extensions_.Contains(extension_id))
[email protected]ad83ca242011-07-29 01:32:25828 return false;
829
830 // If the extension hasn't been loaded yet, check the prefs for it. Assume
831 // enabled unless otherwise noted.
832 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
833 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35834}
835
836bool ExtensionService::IsExternalExtensionUninstalled(
837 const std::string& extension_id) const {
838 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
839}
840
[email protected]eaa7dd182010-12-14 11:09:00841void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12842 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20843
[email protected]06f92562011-04-29 19:27:31844 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39845 return;
[email protected]0c6da502009-08-14 22:32:39846
[email protected]b3317ad2011-04-28 23:46:00847 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52848
[email protected]06f92562011-04-29 19:27:31849 const Extension* extension =
850 GetExtensionByIdInternal(extension_id, false, true, false);
851 // This can happen if sync enables an extension that is not
852 // installed yet.
853 if (!extension)
854 return;
855
[email protected]0c6da502009-08-14 22:32:39856 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46857 extensions_.Insert(make_scoped_refptr(extension));
858 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39859
[email protected]f681c782010-11-19 11:19:39860 // Make sure any browser action contained within it is not hidden.
861 extension_prefs_->SetBrowserActionVisibility(extension, true);
862
[email protected]62d30f42009-10-01 22:36:06863 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30864
865 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39866}
867
[email protected]eaa7dd182010-12-14 11:09:00868void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12869 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20870
[email protected]b2ba9962009-12-10 20:10:15871 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31872 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52873 return;
[email protected]1784e83a2009-09-08 21:01:52874
[email protected]06f92562011-04-29 19:27:31875 const Extension* extension = GetInstalledExtension(extension_id);
876 // |extension| can be NULL if sync disables an extension that is not
877 // installed yet.
878 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:33879 return;
880
[email protected]b3317ad2011-04-28 23:46:00881 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52882
[email protected]06f92562011-04-29 19:27:31883 extension = GetExtensionByIdInternal(extension_id, true, false, true);
884 if (!extension)
885 return;
886
[email protected]1784e83a2009-09-08 21:01:52887 // Move it over to the disabled list.
[email protected]84df8332011-12-06 18:22:46888 disabled_extensions_.Insert(make_scoped_refptr(extension));
889 if (extensions_.Contains(extension->id()))
890 extensions_.Remove(extension->id());
891 else
892 terminated_extensions_.Remove(extension->id());
[email protected]1784e83a2009-09-08 21:01:52893
[email protected]814a7bf0f2011-08-13 05:30:59894 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:30895
896 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:25897
898 // Deactivating one extension might have solved the problems of others.
899 // Therefore, we clear warnings of this type for all extensions.
900 std::set<ExtensionWarningSet::WarningType> warnings;
901 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
902 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:52903}
904
[email protected]eaa7dd182010-12-14 11:09:00905void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25906 const Extension* extension) {
907 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49908 RecordPermissionMessagesHistogram(
909 extension, "Extensions.Permissions_ReEnable");
[email protected]c333e792012-01-06 16:57:39910 extensions::PermissionsUpdater perms_updater(profile());
911 perms_updater.GrantActivePermissions(extension);
[email protected]8d888c12010-11-30 00:00:25912 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
913 EnableExtension(extension->id());
914}
915
[email protected]fe2dd7742011-04-19 22:52:49916// static
917void ExtensionService::RecordPermissionMessagesHistogram(
918 const Extension* e, const char* histogram) {
919 // Since this is called from multiple sources, and since the Histogram macros
920 // use statics, we need to manually lookup the Histogram ourselves.
921 base::Histogram* counter = base::LinearHistogram::FactoryGet(
922 histogram,
923 1,
[email protected]0d3e4a22011-06-23 19:02:52924 ExtensionPermissionMessage::kEnumBoundary,
925 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:49926 base::Histogram::kUmaTargetedHistogramFlag);
927
[email protected]0d3e4a22011-06-23 19:02:52928 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49929 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:52930 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49931 } else {
[email protected]0d3e4a22011-06-23 19:02:52932 for (ExtensionPermissionMessages::iterator it = permissions.begin();
933 it != permissions.end(); ++it)
934 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49935 }
936}
937
[email protected]eaa7dd182010-12-14 11:09:00938void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42939 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06940 // was loaded, otherwise a race can arise where a renderer that is created
941 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42942 // that the request context doesn't yet know about. The profile is responsible
943 // for ensuring its URLRequestContexts appropriately discover the loaded
944 // extension.
[email protected]c8d407e2011-04-28 21:27:17945 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06946
[email protected]19647262011-12-16 09:57:49947 // Tell renderers about the new extension, unless it's a theme (renderers
948 // don't need to know about themes).
949 if (!extension->is_theme()) {
950 for (content::RenderProcessHost::iterator i(
951 content::RenderProcessHost::AllHostsIterator());
952 !i.IsAtEnd(); i.Advance()) {
953 content::RenderProcessHost* host = i.GetCurrentValue();
954 Profile* host_profile =
955 Profile::FromBrowserContext(host->GetBrowserContext());
956 if (host_profile->GetOriginalProfile() ==
957 profile_->GetOriginalProfile()) {
958 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
959 1, ExtensionMsg_Loaded_Params(extension));
960 host->Send(
961 new ExtensionMsg_Loaded(loaded_extensions));
962 }
[email protected]c8d407e2011-04-28 21:27:17963 }
[email protected]77a6970c2011-04-23 16:58:56964 }
965
[email protected]3442a662012-01-12 08:06:17966 // Tell subsystems that use the EXTENSION_LOADED notification about the new
967 // extension.
968 //
969 // NOTE: It is important that this happen after notifying the renderers about
970 // the new extensions so that if we navigate to an extension URL in
971 // NOTIFICATION_EXTENSION_LOADED, the renderer is guaranteed to know about it.
972 content::NotificationService::current()->Notify(
973 chrome::NOTIFICATION_EXTENSION_LOADED,
974 content::Source<Profile>(profile_),
975 content::Details<const Extension>(extension));
976
[email protected]c8d407e2011-04-28 21:27:17977 // Tell a random-ass collection of other subsystems about the new extension.
978 // TODO(aa): What should we do with all this goop? Can it move into the
979 // relevant objects via EXTENSION_LOADED?
980
981 profile_->GetExtensionSpecialStoragePolicy()->
982 GrantRightsForExtension(extension);
983
984 UpdateActiveExtensionsInCrashReporter();
985
986 ExtensionWebUI::RegisterChromeURLOverrides(
987 profile_, extension->GetChromeURLOverrides());
988
[email protected]8e5c89a2011-06-07 18:13:33989 TemplateURLService* url_service =
990 TemplateURLServiceFactory::GetForProfile(profile_);
991 if (url_service)
992 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:17993
994 // Load the icon for omnibox-enabled extensions so it will be ready to display
995 // in the URL bar.
996 if (!extension->omnibox_keyword().empty()) {
997 omnibox_popup_icon_manager_.LoadIcon(extension);
998 omnibox_icon_manager_.LoadIcon(extension);
999 }
1000
1001 // If the extension has permission to load chrome://favicon/ resources we need
1002 // to make sure that the FaviconSource is registered with the
1003 // ChromeURLDataManager.
1004 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1005 FaviconSource* favicon_source = new FaviconSource(profile_,
1006 FaviconSource::FAVICON);
1007 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
1008 }
[email protected]5eddc3e2011-10-26 04:33:311009 // Same for chrome://thumb/ resources.
1010 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
1011 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
1012 profile_->GetChromeURLDataManager()->AddDataSource(thumbnail_source);
1013 }
[email protected]c8d407e2011-04-28 21:27:171014
1015 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581016 bool plugins_changed = false;
1017 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1018 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141019 PluginService::GetInstance()->RefreshPlugins();
1020 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581021 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491022 ChromePluginServiceFilter* filter =
1023 ChromePluginServiceFilter::GetInstance();
1024 if (plugin.is_public) {
1025 filter->RestrictPluginToProfileAndOrigin(
1026 plugin.path, profile_, GURL());
1027 } else {
1028 filter->RestrictPluginToProfileAndOrigin(
1029 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581030 }
1031 }
[email protected]84396dbc2011-04-14 06:33:421032
1033 bool nacl_modules_changed = false;
1034 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1035 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001036 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421037 nacl_modules_changed = true;
1038 }
1039
[email protected]ed0ba002011-05-26 16:55:131040 if (nacl_modules_changed)
1041 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421042
1043 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021044 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541045
[email protected]14da67b02011-08-02 05:49:131046#if defined(OS_CHROMEOS)
[email protected]b4d9d122011-06-17 01:58:541047 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1048 extension->input_components().begin();
1049 component != extension->input_components().end();
1050 ++component) {
[email protected]14da67b02011-08-02 05:49:131051 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
[email protected]8baf0dbb2011-11-27 22:47:021052 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
[email protected]14da67b02011-08-02 05:49:131053 profile_, extension->id(), *component);
1054 }
[email protected]9a2aebe02011-12-02 21:54:141055#if defined(USE_VIRTUAL_KEYBOARD)
[email protected]b4d9d122011-06-17 01:58:541056 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1057 !component->layouts.empty()) {
[email protected]a4215ac02011-12-04 04:11:561058 chromeos::input_method::InputMethodManager* input_method_manager =
1059 chromeos::input_method::InputMethodManager::GetInstance();
1060 const bool is_system_keyboard =
1061 extension->location() == Extension::COMPONENT;
[email protected]cbd4c042011-08-13 18:22:191062 input_method_manager->RegisterVirtualKeyboard(
1063 extension->url(),
1064 component->name, // human-readable name of the keyboard extension.
1065 component->layouts,
[email protected]a4215ac02011-12-04 04:11:561066 is_system_keyboard);
[email protected]b4d9d122011-06-17 01:58:541067 }
[email protected]14da67b02011-08-02 05:49:131068#endif
[email protected]b4d9d122011-06-17 01:58:541069 }
1070#endif
[email protected]62d30f42009-10-01 22:36:061071}
1072
[email protected]a9f39a312010-12-23 22:14:271073void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591074 const Extension* extension,
1075 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271076 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071077 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271078 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531079 content::Source<Profile>(profile_),
1080 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061081
[email protected]f3b1a082011-11-18 00:34:301082 for (content::RenderProcessHost::iterator i(
1083 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561084 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301085 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081086 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301087 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081088 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171089 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561090 }
1091
[email protected]dd163fb02011-05-04 22:22:171092 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171093 profile_->GetExtensionSpecialStoragePolicy()->
1094 RevokeRightsForExtension(extension);
1095
1096 ExtensionWebUI::UnregisterChromeURLOverrides(
1097 profile_, extension->GetChromeURLOverrides());
1098
[email protected]b777b332011-04-16 04:01:081099#if defined(OS_CHROMEOS)
1100 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171101 if (profile_->GetFileSystemContext() &&
[email protected]e7e46732012-01-05 11:45:551102 profile_->GetFileSystemContext()->external_provider()) {
1103 profile_->GetFileSystemContext()->external_provider()->
[email protected]c8d407e2011-04-28 21:27:171104 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061105 }
[email protected]54ccafa2011-12-06 23:36:321106
1107 if (extension->input_components().size() > 0) {
1108 ExtensionInputImeEventRouter::GetInstance()->UnregisterAllImes(
1109 profile_, extension->id());
1110 }
[email protected]c8d407e2011-04-28 21:27:171111#endif
1112
1113 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581114
1115 bool plugins_changed = false;
1116 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1117 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361118 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361119 base::Bind(&ForceShutdownPlugin, plugin.path)))
[email protected]14908b72011-04-20 06:54:361120 NOTREACHED();
[email protected]f520b5b2011-11-08 02:42:141121 PluginService::GetInstance()->RefreshPlugins();
1122 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581123 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491124 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581125 }
[email protected]84396dbc2011-04-14 06:33:421126
1127 bool nacl_modules_changed = false;
1128 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1129 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001130 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421131 nacl_modules_changed = true;
1132 }
1133
[email protected]ed0ba002011-05-26 16:55:131134 if (nacl_modules_changed)
1135 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421136
1137 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021138 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061139}
1140
[email protected]eaa7dd182010-12-14 11:09:001141void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181142 const std::vector<std::string>& blacklist) {
1143 // Use this set to indicate if an extension in the blacklist has been used.
1144 std::set<std::string> blacklist_set;
1145 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1146 if (Extension::IdIsValid(blacklist[i])) {
1147 blacklist_set.insert(blacklist[i]);
1148 }
1149 }
1150 extension_prefs_->UpdateBlacklist(blacklist_set);
1151 std::vector<std::string> to_be_removed;
1152 // Loop current extensions, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461153 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]6b75ec32009-08-14 06:37:181154 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021155 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181156 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1157 to_be_removed.push_back(extension->id());
1158 }
1159 }
1160
1161 // UnloadExtension will change the extensions_ list. So, we should
1162 // call it outside the iterator loop.
1163 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591164 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181165 }
1166}
1167
[email protected]0a071a32011-02-08 00:18:241168Profile* ExtensionService::profile() {
1169 return profile_;
1170}
1171
[email protected]eaa7dd182010-12-14 11:09:001172ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451173 return extension_prefs_;
1174}
1175
[email protected]0d9a2202011-11-09 13:48:411176extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381177 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201178}
1179
[email protected]b790b072011-05-20 09:46:441180ExtensionContentSettingsStore*
1181 ExtensionService::GetExtensionContentSettingsStore() {
1182 return extension_prefs()->content_settings_store();
1183}
1184
[email protected]25ae0152011-11-18 14:40:021185bool ExtensionService::is_ready() {
1186 return ready_;
1187}
1188
[email protected]2859946f2011-04-04 18:18:061189ExtensionUpdater* ExtensionService::updater() {
1190 return updater_.get();
1191}
1192
[email protected]eaa7dd182010-12-14 11:09:001193void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251194 std::vector<std::string> to_be_removed;
1195 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461196 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251197 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021198 const Extension* extension = (*iter);
[email protected]05aad2da2011-10-28 10:12:371199 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id(),
1200 extension->location())) {
[email protected]aa96d3a2010-08-21 08:45:251201 to_be_removed.push_back(extension->id());
[email protected]05aad2da2011-10-28 10:12:371202 }
[email protected]aa96d3a2010-08-21 08:45:251203 }
1204
1205 // UnloadExtension will change the extensions_ list. So, we should
1206 // call it outside the iterator loop.
1207 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591208 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251209}
1210
[email protected]31206602011-04-13 23:07:321211void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351212 if (updater()) {
[email protected]31206602011-04-13 23:07:321213 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351214 } else {
[email protected]31206602011-04-13 23:07:321215 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351216 }
1217}
1218
[email protected]3bdba0d2011-08-23 07:17:301219namespace {
1220 bool IsSyncableNone(const Extension& extension) { return false; }
[email protected]406b5a92011-11-08 11:58:261221} // namespace
[email protected]3bdba0d2011-08-23 07:17:301222
1223ExtensionService::SyncBundle::SyncBundle()
1224 : filter(IsSyncableNone),
1225 sync_processor(NULL) {
[email protected]b05fb9ff2011-04-23 00:07:561226}
1227
[email protected]3bdba0d2011-08-23 07:17:301228ExtensionService::SyncBundle::~SyncBundle() {
1229}
1230
1231bool ExtensionService::SyncBundle::HasExtensionId(const std::string& id) const {
1232 return synced_extensions.find(id) != synced_extensions.end();
1233}
1234
1235bool ExtensionService::SyncBundle::HasPendingExtensionId(const std::string& id)
1236 const {
1237 return pending_sync_data.find(id) != pending_sync_data.end();
1238}
1239
1240void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
1241 SyncBundle* sync_bundle = GetSyncBundleForExtension(extension);
1242 if (sync_bundle) {
[email protected]aa7599d2011-10-28 07:24:321243 ExtensionSyncData extension_sync_data(
1244 extension,
1245 IsExtensionEnabled(extension.id()),
1246 IsIncognitoEnabled(extension.id()),
[email protected]b2689a902011-12-01 00:41:091247 extension_prefs_->GetAppNotificationClientId(extension.id()),
[email protected]168389f2011-12-20 17:12:481248 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1249 GetAppLaunchOrdinal(extension.id()),
1250 GetPageOrdinal(extension.id()));
[email protected]3bdba0d2011-08-23 07:17:301251
1252 SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
1253 sync_bundle->HasExtensionId(extension.id()) ?
1254 SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD));
1255 sync_bundle->sync_processor->ProcessSyncChanges(
1256 FROM_HERE, sync_change_list);
1257 sync_bundle->synced_extensions.insert(extension.id());
1258 sync_bundle->pending_sync_data.erase(extension.id());
[email protected]b05fb9ff2011-04-23 00:07:561259 }
[email protected]3bdba0d2011-08-23 07:17:301260}
1261
1262ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForExtension(
1263 const Extension& extension) {
1264 if (app_sync_bundle_.filter(extension))
1265 return &app_sync_bundle_;
1266 else if (extension_sync_bundle_.filter(extension))
1267 return &extension_sync_bundle_;
1268 else
1269 return NULL;
1270}
1271
1272ExtensionService::SyncBundle*
1273 ExtensionService::GetSyncBundleForExtensionSyncData(
1274 const ExtensionSyncData& extension_sync_data) {
1275 switch (extension_sync_data.type()) {
1276 case Extension::SYNC_TYPE_APP:
1277 return &app_sync_bundle_;
1278 case Extension::SYNC_TYPE_EXTENSION:
1279 return &extension_sync_bundle_;
1280 default:
1281 NOTREACHED();
1282 return NULL;
1283 }
1284}
1285
1286#define GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY() \
1287 do { \
1288 switch (type) { \
1289 case syncable::APPS: \
1290 return &app_sync_bundle_; \
1291 case syncable::EXTENSIONS: \
1292 return &extension_sync_bundle_; \
1293 default: \
1294 NOTREACHED(); \
1295 return NULL; \
1296 } \
1297 } while (0)
1298
1299const ExtensionService::SyncBundle*
1300 ExtensionService::GetSyncBundleForModelTypeConst(
1301 syncable::ModelType type) const {
1302 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1303}
1304
1305ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForModelType(
1306 syncable::ModelType type) {
1307 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1308}
1309
1310#undef GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY
1311
1312SyncError ExtensionService::MergeDataAndStartSyncing(
1313 syncable::ModelType type,
1314 const SyncDataList& initial_sync_data,
1315 SyncChangeProcessor* sync_processor) {
1316 CHECK(sync_processor);
1317
1318 SyncBundle* bundle = NULL;
1319
1320 switch (type) {
1321 case syncable::EXTENSIONS:
1322 bundle = &extension_sync_bundle_;
1323 bundle->filter = IsSyncableExtension;
1324 break;
1325
1326 case syncable::APPS:
1327 bundle = &app_sync_bundle_;
1328 bundle->filter = IsSyncableApp;
1329 break;
1330
1331 default:
1332 LOG(FATAL) << "Got " << type << " ModelType";
1333 }
1334
1335 bundle->sync_processor = sync_processor;
1336
[email protected]b1417ab2011-12-15 17:09:111337 // Process extensions from sync.
[email protected]3bdba0d2011-08-23 07:17:301338 for (SyncDataList::const_iterator i = initial_sync_data.begin();
1339 i != initial_sync_data.end();
1340 ++i) {
1341 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1342 bundle->synced_extensions.insert(extension_sync_data.id());
1343 ProcessExtensionSyncData(extension_sync_data, *bundle);
1344 }
1345
[email protected]b1417ab2011-12-15 17:09:111346 // Process local extensions.
1347 // TODO(yoz): Determine whether pending extensions should be considered too.
1348 // See crbug.com/104399.
[email protected]3bdba0d2011-08-23 07:17:301349 SyncDataList sync_data_list = GetAllSyncData(type);
1350 SyncChangeList sync_change_list;
1351 for (SyncDataList::const_iterator i = sync_data_list.begin();
1352 i != sync_data_list.end();
1353 ++i) {
[email protected]b1417ab2011-12-15 17:09:111354 if (bundle->HasExtensionId(i->GetTag())) {
[email protected]3bdba0d2011-08-23 07:17:301355 sync_change_list.push_back(SyncChange(SyncChange::ACTION_UPDATE, *i));
[email protected]b1417ab2011-12-15 17:09:111356 } else {
1357 bundle->synced_extensions.insert(i->GetTag());
[email protected]3bdba0d2011-08-23 07:17:301358 sync_change_list.push_back(SyncChange(SyncChange::ACTION_ADD, *i));
[email protected]b1417ab2011-12-15 17:09:111359 }
[email protected]3bdba0d2011-08-23 07:17:301360 }
1361 bundle->sync_processor->ProcessSyncChanges(FROM_HERE, sync_change_list);
1362
1363 return SyncError();
1364}
1365
1366void ExtensionService::StopSyncing(syncable::ModelType type) {
1367 SyncBundle* bundle = GetSyncBundleForModelType(type);
1368 CHECK(bundle);
1369 // This is the simplest way to clear out the bundle.
1370 *bundle = SyncBundle();
1371}
1372
1373SyncDataList ExtensionService::GetAllSyncData(syncable::ModelType type) const {
1374 const SyncBundle* bundle = GetSyncBundleForModelTypeConst(type);
1375 CHECK(bundle);
1376 std::vector<ExtensionSyncData> extension_sync_data = GetSyncDataList(*bundle);
1377 SyncDataList result(extension_sync_data.size());
1378 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
1379 result[i] = extension_sync_data[i].GetSyncData();
1380 }
1381 return result;
1382}
1383
1384SyncError ExtensionService::ProcessSyncChanges(
1385 const tracked_objects::Location& from_here,
1386 const SyncChangeList& change_list) {
1387 for (SyncChangeList::const_iterator i = change_list.begin();
1388 i != change_list.end();
1389 ++i) {
1390 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1391 SyncBundle* bundle = GetSyncBundleForExtensionSyncData(extension_sync_data);
1392 CHECK(bundle);
1393
1394 if (extension_sync_data.uninstalled())
1395 bundle->synced_extensions.erase(extension_sync_data.id());
1396 else
1397 bundle->synced_extensions.insert(extension_sync_data.id());
1398 ProcessExtensionSyncData(extension_sync_data, *bundle);
1399 }
1400
1401 return SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561402}
1403
1404void ExtensionService::GetSyncDataListHelper(
[email protected]84df8332011-12-06 18:22:461405 const ExtensionSet& extensions,
[email protected]3bdba0d2011-08-23 07:17:301406 const SyncBundle& bundle,
[email protected]b05fb9ff2011-04-23 00:07:561407 std::vector<ExtensionSyncData>* sync_data_list) const {
[email protected]84df8332011-12-06 18:22:461408 for (ExtensionSet::const_iterator it = extensions.begin();
[email protected]b05fb9ff2011-04-23 00:07:561409 it != extensions.end(); ++it) {
1410 const Extension& extension = **it;
[email protected]3bdba0d2011-08-23 07:17:301411 if (bundle.filter(extension) &&
1412 // If we have pending extension data for this extension, then this
1413 // version is out of date. We'll sync back the version we got from
1414 // sync.
1415 !bundle.HasPendingExtensionId(extension.id())) {
[email protected]aa7599d2011-10-28 07:24:321416 sync_data_list->push_back(ExtensionSyncData(
1417 extension,
1418 IsExtensionEnabled(extension.id()),
1419 IsIncognitoEnabled(extension.id()),
[email protected]b2689a902011-12-01 00:41:091420 extension_prefs_->GetAppNotificationClientId(extension.id()),
[email protected]168389f2011-12-20 17:12:481421 extension_prefs_->IsAppNotificationDisabled(extension.id()),
1422 GetAppLaunchOrdinal(extension.id()),
1423 GetPageOrdinal(extension.id())));
[email protected]b05fb9ff2011-04-23 00:07:561424 }
1425 }
1426}
1427
1428std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
[email protected]3bdba0d2011-08-23 07:17:301429 const SyncBundle& bundle) const {
1430 std::vector<ExtensionSyncData> extension_sync_list;
1431 GetSyncDataListHelper(extensions_, bundle, &extension_sync_list);
1432 GetSyncDataListHelper(disabled_extensions_, bundle, &extension_sync_list);
1433 GetSyncDataListHelper(terminated_extensions_, bundle, &extension_sync_list);
1434
1435 for (std::map<std::string, ExtensionSyncData>::const_iterator i =
1436 bundle.pending_sync_data.begin();
1437 i != bundle.pending_sync_data.end();
1438 ++i) {
1439 extension_sync_list.push_back(i->second);
1440 }
1441
1442 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561443}
1444
[email protected]3bdba0d2011-08-23 07:17:301445void ExtensionService::ProcessExtensionSyncData(
[email protected]90310d92011-04-17 07:35:041446 const ExtensionSyncData& extension_sync_data,
[email protected]3bdba0d2011-08-23 07:17:301447 SyncBundle& bundle) {
1448 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061449 const Extension* extension = GetInstalledExtension(id);
1450
1451 // TODO(bolms): we should really handle this better. The particularly bad
1452 // case is where an app becomes an extension or vice versa, and we end up with
1453 // a zombie extension that won't go away.
1454 if (extension && !bundle.filter(*extension))
1455 return;
[email protected]90310d92011-04-17 07:35:041456
1457 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301458 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041459 std::string error;
1460 if (!UninstallExtensionHelper(this, id)) {
1461 LOG(WARNING) << "Could not uninstall extension " << id
1462 << " for sync";
1463 }
1464 return;
1465 }
1466
[email protected]06f92562011-04-29 19:27:311467 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301468 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311469 EnableExtension(id);
1470 } else {
1471 DisableExtension(id);
1472 }
[email protected]81b14cc2011-04-29 00:39:371473
[email protected]406b5a92011-11-08 11:58:261474 // We need to cache some version information here because setting the
1475 // incognito flag invalidates the |extension| pointer (it reloads the
1476 // extension).
1477 bool extension_installed = extension != NULL;
1478 int result = extension ?
1479 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1480 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1481 extension = NULL; // No longer safe to use.
1482
1483 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311484 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041485 if (result < 0) {
1486 // Extension is outdated.
[email protected]3bdba0d2011-08-23 07:17:301487 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]90310d92011-04-17 07:35:041488 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041489 }
[email protected]b10a4abe2011-12-10 02:31:231490 if (extension_sync_data.type() == Extension::SYNC_TYPE_APP &&
1491 extension_sync_data.notifications_disabled() !=
[email protected]b020dc12011-12-06 04:35:421492 extension_prefs_->IsAppNotificationDisabled(id)) {
1493 extension_prefs_->SetAppNotificationDisabled(
1494 id, extension_sync_data.notifications_disabled());
1495 }
[email protected]06f92562011-04-29 19:27:311496 } else {
[email protected]06f92562011-04-29 19:27:311497 // TODO(akalin): Replace silent update with a list of enabled
1498 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331499 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311500 if (!pending_extension_manager()->AddFromSync(
1501 id,
[email protected]3bdba0d2011-08-23 07:17:301502 extension_sync_data.update_url(),
1503 bundle.filter,
[email protected]6cc7dbae2011-04-29 21:18:331504 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311505 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301506 // This means that the extension is already pending installation, with a
1507 // non-INTERNAL location. Add to pending_sync_data, even though it will
1508 // never be removed (we'll never install a syncable version of the
1509 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311510 }
[email protected]3bdba0d2011-08-23 07:17:301511 // Track pending extensions so that we can return them in GetAllSyncData().
1512 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]06f92562011-04-29 19:27:311513 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041514 }
[email protected]90310d92011-04-17 07:35:041515}
1516
[email protected]c3cfb012011-04-06 22:07:351517bool ExtensionService::IsIncognitoEnabled(
1518 const std::string& extension_id) const {
1519 // If this is an existing component extension we always allow it to
1520 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371521 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351522 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411523 return true;
1524
1525 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351526 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501527}
[email protected]55a35692010-02-11 23:25:211528
[email protected]c3cfb012011-04-06 22:07:351529void ExtensionService::SetIsIncognitoEnabled(
1530 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371531 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351532 if (extension && extension->location() == Extension::COMPONENT) {
1533 // This shouldn't be called for component extensions.
1534 NOTREACHED();
1535 return;
1536 }
1537
[email protected]8c6c8c22011-03-09 12:52:341538 // Broadcast unloaded and loaded events to update browser state. Only bother
1539 // if the value changed and the extension is actually enabled, since there is
1540 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351541 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341542 if (enabled == old_enabled)
1543 return;
1544
[email protected]c3cfb012011-04-06 22:07:351545 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371546
[email protected]ffbf3f12011-12-09 22:10:331547 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341548
1549 // When we reload the extension the ID may be invalidated if we've passed it
1550 // by const ref everywhere. Make a copy to be safe.
1551 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261552 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331553 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301554
[email protected]406b5a92011-11-08 11:58:261555 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341556 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301557 if (extension)
1558 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211559}
1560
[email protected]aa7599d2011-10-28 07:24:321561void ExtensionService::SetAppNotificationSetupDone(
1562 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091563 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321564 const Extension* extension = GetInstalledExtension(extension_id);
1565 // This method is called when the user sets up app notifications.
1566 // So it is not expected to be called until the extension is installed.
1567 if (!extension) {
1568 NOTREACHED();
1569 return;
1570 }
[email protected]b2689a902011-12-01 00:41:091571 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321572 SyncExtensionChangeIfNeeded(*extension);
1573}
1574
1575void ExtensionService::SetAppNotificationDisabled(
1576 const std::string& extension_id,
1577 bool value) {
1578 const Extension* extension = GetInstalledExtension(extension_id);
1579 // This method is called when the user enables/disables app notifications.
1580 // So it is not expected to be called until the extension is installed.
1581 if (!extension) {
1582 NOTREACHED();
1583 return;
1584 }
[email protected]97e856b12011-12-14 00:21:131585 if (value)
1586 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1587 else
1588 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321589 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1590 SyncExtensionChangeIfNeeded(*extension);
1591}
1592
[email protected]eaa7dd182010-12-14 11:09:001593bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051594 // We allow the extension to see events and data from another profile iff it
1595 // uses "spanning" behavior and it has incognito access. "split" mode
1596 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221597 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351598 return IsIncognitoEnabled(extension->id()) &&
1599 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051600}
1601
[email protected]5ef7b002011-05-12 23:16:201602bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1603 if (extension->is_hosted_app())
1604 return true;
1605 // Packaged apps and regular extensions need to be enabled specifically for
1606 // incognito (and split mode should be set).
1607 return extension->incognito_split_mode() &&
1608 IsIncognitoEnabled(extension->id());
1609}
1610
[email protected]168389f2011-12-20 17:12:481611StringOrdinal ExtensionService::GetAppLaunchOrdinal(
1612 const std::string& extension_id) const {
[email protected]3569b502012-01-12 20:08:231613 return
1614 extension_prefs_->extension_sorting()->GetAppLaunchOrdinal(extension_id);
[email protected]168389f2011-12-20 17:12:481615}
1616
1617void ExtensionService::SetAppLaunchOrdinal(
1618 const std::string& extension_id,
1619 const StringOrdinal& app_launch_index) {
1620 const Extension* ext = GetExtensionById(extension_id, true);
1621 DCHECK(ext->is_app());
1622
[email protected]3569b502012-01-12 20:08:231623 extension_prefs_->extension_sorting()->SetAppLaunchOrdinal(
1624 extension_id, app_launch_index);
[email protected]168389f2011-12-20 17:12:481625
1626 const Extension* extension = GetInstalledExtension(extension_id);
1627 if (extension)
1628 SyncExtensionChangeIfNeeded(*extension);
1629}
1630
1631StringOrdinal ExtensionService::GetPageOrdinal(
1632 const std::string& extension_id) const {
[email protected]3569b502012-01-12 20:08:231633 return extension_prefs_->extension_sorting()->GetPageOrdinal(extension_id);
[email protected]168389f2011-12-20 17:12:481634}
1635
1636void ExtensionService::SetPageOrdinal(const std::string& extension_id,
1637 const StringOrdinal& page_ordinal) {
1638 const Extension* ext = GetExtensionById(extension_id, true);
1639 DCHECK(ext->is_app());
1640
[email protected]3569b502012-01-12 20:08:231641 extension_prefs_->extension_sorting()->SetPageOrdinal(
1642 extension_id, page_ordinal);
[email protected]168389f2011-12-20 17:12:481643
1644 const Extension* extension = GetInstalledExtension(extension_id);
1645 if (extension)
1646 SyncExtensionChangeIfNeeded(*extension);
1647}
1648
1649void ExtensionService::OnExtensionMoved(
1650 const std::string& moved_extension_id,
1651 const std::string& predecessor_extension_id,
1652 const std::string& successor_extension_id) {
[email protected]3569b502012-01-12 20:08:231653 extension_prefs_->extension_sorting()->OnExtensionMoved(
1654 moved_extension_id,
1655 predecessor_extension_id,
1656 successor_extension_id);
[email protected]168389f2011-12-20 17:12:481657
1658 const Extension* extension = GetInstalledExtension(moved_extension_id);
1659 if (extension)
1660 SyncExtensionChangeIfNeeded(*extension);
1661}
1662
[email protected]eaa7dd182010-12-14 11:09:001663bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081664 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441665 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081666 extension_prefs_->AllowFileAccess(extension->id()));
1667}
1668
[email protected]eaa7dd182010-12-14 11:09:001669void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481670 bool allow) {
1671 // Reload to update browser state. Only bother if the value changed and the
1672 // extension is actually enabled, since there is no UI otherwise.
1673 bool old_allow = AllowFileAccess(extension);
1674 if (allow == old_allow)
1675 return;
1676
[email protected]05c82182010-06-24 17:49:081677 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481678
[email protected]84df8332011-12-06 18:22:461679 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481680 if (extension_is_enabled)
1681 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081682}
1683
[email protected]eaa7dd182010-12-14 11:09:001684bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391685 return extension_prefs_->GetBrowserActionVisibility(extension);
1686}
1687
[email protected]eaa7dd182010-12-14 11:09:001688void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141689 bool visible) {
[email protected]f681c782010-11-19 11:19:391690 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1691}
1692
[email protected]8e4560b62011-01-14 10:09:141693// Some extensions will autoupdate themselves externally from Chrome. These
1694// are typically part of some larger client application package. To support
1695// these, the extension will register its location in the the preferences file
1696// (and also, on Windows, in the registry) and this code will periodically
1697// check that location for a .crx file, which it will then install locally if
1698// a new version is available.
1699// Errors are reported through ExtensionErrorReporter. Succcess is not
1700// reported.
[email protected]eaa7dd182010-12-14 11:09:001701void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121702 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141703
1704 // Note that this installation is intentionally silent (since it didn't
1705 // go through the front-end). Extensions that are registered in this
1706 // way are effectively considered 'pre-bundled', and so implicitly
1707 // trusted. In general, if something has HKLM or filesystem access,
1708 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121709
1710 // If any external extension records give a URL, a provider will set
1711 // this to true. Used by OnExternalProviderReady() to see if we need
1712 // to start an update check to fetch a new external extension.
1713 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141714
1715 // Ask each external extension provider to give us a call back for each
1716 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1717 ProviderCollection::const_iterator i;
1718 for (i = external_extension_providers_.begin();
1719 i != external_extension_providers_.end(); ++i) {
1720 ExternalExtensionProviderInterface* provider = i->get();
1721 provider->VisitRegisteredExtension();
1722 }
1723
[email protected]50067e52011-10-20 23:17:071724 // Do any required work that we would have done after completion of all
1725 // providers.
1726 if (external_extension_providers_.empty()) {
1727 OnAllExternalProvidersReady();
1728 }
[email protected]9f1087e2009-06-15 17:29:321729}
1730
[email protected]50067e52011-10-20 23:17:071731void ExtensionService::OnExternalProviderReady(
1732 const ExternalExtensionProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121733 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071734 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121735
1736 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141737 // if all of them are finished. So we check them first.
1738 ProviderCollection::const_iterator i;
1739 for (i = external_extension_providers_.begin();
1740 i != external_extension_providers_.end(); ++i) {
1741 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121742 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141743 return;
[email protected]8e4560b62011-01-14 10:09:141744 }
1745
[email protected]50067e52011-10-20 23:17:071746 OnAllExternalProvidersReady();
1747}
1748
1749void ExtensionService::OnAllExternalProvidersReady() {
1750 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1751
1752 // Install any pending extensions.
[email protected]a29a517a2011-01-21 21:11:121753 if (external_extension_url_added_ && updater()) {
1754 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141755 updater()->CheckNow();
1756 }
1757
1758 // Uninstall all the unclaimed extensions.
1759 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1760 extension_prefs_->GetInstalledExtensionsInfo());
1761 for (size_t i = 0; i < extensions_info->size(); ++i) {
1762 ExtensionInfo* info = extensions_info->at(i).get();
1763 if (Extension::IsExternalLocation(info->extension_location))
1764 CheckExternalUninstall(info->extension_id);
1765 }
[email protected]e5af875f2011-10-10 21:09:141766 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141767}
1768
1769void ExtensionService::IdentifyAlertableExtensions() {
1770 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1771
[email protected]08b83362011-10-19 05:23:171772 if (!CommandLine::ForCurrentProcess()->HasSwitch(
1773 switches::kEnableExtensionAlerts)) {
1774 return; // TODO(miket): enable unconditionally when done.
1775 }
1776
[email protected]e5af875f2011-10-10 21:09:141777 // Build up the lists of extensions that require acknowledgment.
1778 // If this is the first time, grandfather extensions that would have
1779 // caused notification.
1780 scoped_ptr<ExtensionGlobalError> global_error(
[email protected]d8c8f25f2011-11-02 18:18:011781 new ExtensionGlobalError(AsWeakPtr()));
[email protected]e5af875f2011-10-10 21:09:141782 bool needs_alert = false;
[email protected]84df8332011-12-06 18:22:461783 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]e5af875f2011-10-10 21:09:141784 iter != extensions_.end(); ++iter) {
1785 const Extension* e = *iter;
[email protected]e5af875f2011-10-10 21:09:141786 if (Extension::IsExternalLocation(e->location())) {
1787 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
1788 global_error->AddExternalExtension(e->id());
1789 needs_alert = true;
1790 }
1791 }
1792 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
1793 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
1794 global_error->AddBlacklistedExtension(e->id());
1795 needs_alert = true;
1796 }
1797 }
1798 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
1799 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
1800 global_error->AddOrphanedExtension(e->id());
1801 needs_alert = true;
1802 }
1803 }
1804 }
1805
1806 if (needs_alert) {
1807 if (extension_prefs_->SetAlertSystemFirstRun()) {
1808 global_error->set_accept_callback(
1809 base::Bind(&ExtensionService::HandleExtensionAlertAccept,
1810 base::Unretained(this)));
1811 global_error->set_cancel_callback(
1812 base::Bind(&ExtensionService::HandleExtensionAlertDetails,
1813 base::Unretained(this)));
[email protected]62433d32011-10-12 22:33:121814 ShowExtensionAlert(global_error.release());
[email protected]e5af875f2011-10-10 21:09:141815 } else {
1816 // First run. Just acknowledge all the extensions, silently, by
1817 // shortcutting the display of the UI and going straight to the
1818 // callback for pressing the Accept button.
[email protected]62433d32011-10-12 22:33:121819 HandleExtensionAlertAccept(*global_error.get(), NULL);
[email protected]e5af875f2011-10-10 21:09:141820 }
1821 }
1822}
1823
[email protected]62433d32011-10-12 22:33:121824void ExtensionService::ShowExtensionAlert(ExtensionGlobalError* global_error) {
1825 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1826 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_);
1827 if (browser) {
1828 global_error->ShowBubbleView(browser);
1829 }
1830}
1831
[email protected]e5af875f2011-10-10 21:09:141832void ExtensionService::HandleExtensionAlertAccept(
[email protected]62433d32011-10-12 22:33:121833 const ExtensionGlobalError& global_error, Browser* browser) {
[email protected]e5af875f2011-10-10 21:09:141834 const ExtensionIdSet *extension_ids =
1835 global_error.get_external_extension_ids();
1836 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1837 iter != extension_ids->end(); ++iter) {
[email protected]47fc70c2011-12-06 07:29:511838 AcknowledgeExternalExtension(*iter);
[email protected]e5af875f2011-10-10 21:09:141839 }
1840 extension_ids = global_error.get_blacklisted_extension_ids();
1841 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1842 iter != extension_ids->end(); ++iter) {
1843 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1844 }
1845 extension_ids = global_error.get_orphaned_extension_ids();
1846 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1847 iter != extension_ids->end(); ++iter) {
1848 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
1849 }
1850}
1851
[email protected]47fc70c2011-12-06 07:29:511852void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1853 extension_prefs_->AcknowledgeExternalExtension(id);
1854}
1855
[email protected]e5af875f2011-10-10 21:09:141856void ExtensionService::HandleExtensionAlertDetails(
[email protected]62433d32011-10-12 22:33:121857 const ExtensionGlobalError& global_error, Browser* browser) {
1858 if (browser) {
1859 browser->ShowExtensionsTab();
1860 }
[email protected]6c751e72010-11-23 10:11:101861}
1862
[email protected]a9f39a312010-12-23 22:14:271863void ExtensionService::UnloadExtension(
1864 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591865 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091866 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021867 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471868 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251869
[email protected]fa0c96732010-11-17 00:14:231870 // This method can be called via PostTask, so the extension may have been
1871 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171872 if (!extension) {
1873 // In case the extension may have crashed/uninstalled. Allow the profile to
1874 // clean up its RequestContexts.
1875 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231876 return;
[email protected]dd163fb02011-05-04 22:22:171877 }
[email protected]0c6da502009-08-14 22:32:391878
[email protected]1eb175082010-02-10 09:26:161879 // Keep information about the extension so that we can reload it later
1880 // even if it's not permanently installed.
1881 unloaded_extension_paths_[extension->id()] = extension->path();
1882
[email protected]f17dbd42010-08-16 23:21:101883 // Clean up if the extension is meant to be enabled after a reload.
1884 disabled_extension_paths_.erase(extension->id());
1885
[email protected]d7e9a862010-11-03 21:57:491886 // Clean up runtime data.
1887 extension_runtime_data_.erase(extension_id);
1888
[email protected]84df8332011-12-06 18:22:461889if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271890 UnloadedExtensionInfo details(extension, reason);
1891 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461892 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071893 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271894 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531895 content::Source<Profile>(profile_),
1896 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171897 // Make sure the profile cleans up its RequestContexts when an already
1898 // disabled extension is unloaded (since they are also tracking the disabled
1899 // extensions).
1900 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391901 return;
1902 }
1903
[email protected]84df8332011-12-06 18:22:461904// Remove the extension from our list.
1905 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251906
[email protected]a9f39a312010-12-23 22:14:271907 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251908}
1909
[email protected]eaa7dd182010-12-14 11:09:001910void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171911 profile_->GetExtensionSpecialStoragePolicy()->
1912 RevokeRightsForAllExtensions();
1913
[email protected]84df8332011-12-06 18:22:461914 extensions_.Clear();
1915 disabled_extensions_.Clear();
1916 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491917 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321918
[email protected]c6e4a3412009-06-24 15:45:291919 // TODO(erikkay) should there be a notification for this? We can't use
1920 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1921 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321922}
1923
[email protected]eaa7dd182010-12-14 11:09:001924void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321925 UnloadAllExtensions();
[email protected]d8c8f25f2011-11-02 18:18:011926 component_loader_->LoadAll();
1927 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321928}
1929
[email protected]eaa7dd182010-12-14 11:09:001930void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501931 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391932 return;
1933
[email protected]ca3dbf52010-05-19 22:27:061934 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1935 extension_prefs_->GetInstalledExtensionsInfo());
1936
1937 std::map<std::string, FilePath> extension_paths;
1938 for (size_t i = 0; i < info->size(); ++i)
1939 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1940
[email protected]14908b72011-04-20 06:54:361941 if (!BrowserThread::PostTask(
1942 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361943 base::Bind(
[email protected]14908b72011-04-20 06:54:361944 &extension_file_util::GarbageCollectExtensions,
1945 install_directory_,
1946 extension_paths)))
1947 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341948
1949 // Also garbage-collect themes. We check |profile_| to be
1950 // defensive; in the future, we may call GarbageCollectExtensions()
1951 // from somewhere other than Init() (e.g., in a timer).
1952 if (profile_) {
[email protected]18280372011-03-22 18:05:221953 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341954 }
[email protected]3cf4f0992009-02-03 23:00:301955}
1956
[email protected]eaa7dd182010-12-14 11:09:001957void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171958 if (updater_.get()) {
1959 updater_->Start();
1960 }
[email protected]fa6a9102010-11-22 15:38:501961
1962 ready_ = true;
[email protected]ad50def52011-10-19 23:17:071963 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271964 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:531965 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:071966 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:511967}
1968
[email protected]49cd1572011-02-08 21:38:451969void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461970 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021971 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321972
[email protected]c8d407e2011-04-28 21:27:171973 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1974 // default extension state to DISABLED when the --disable-extensions flag
1975 // is set (http://crbug.com/29067).
1976 if (!extensions_enabled() &&
1977 !extension->is_theme() &&
1978 extension->location() != Extension::COMPONENT &&
1979 !Extension::IsExternalLocation(extension->location()))
1980 return;
1981
1982 SetBeingUpgraded(extension, false);
1983
[email protected]1eb175082010-02-10 09:26:161984 // The extension is now loaded, remove its data from unloaded extension map.
1985 unloaded_extension_paths_.erase(extension->id());
1986
[email protected]bb7f40952011-01-13 00:21:201987 // If a terminated extension is loaded, remove it from the terminated list.
1988 UntrackTerminatedExtension(extension->id());
1989
[email protected]f17dbd42010-08-16 23:21:101990 // If the extension was disabled for a reload, then enable it.
1991 if (disabled_extension_paths_.erase(extension->id()) > 0)
1992 EnableExtension(extension->id());
1993
[email protected]d728e002010-12-08 04:46:231994 // Check if the extension's privileges have changed and disable the
1995 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:311996 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:391997
[email protected]ad83ca242011-07-29 01:32:251998 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:181999 if (disabled) {
[email protected]84df8332011-12-06 18:22:462000 disabled_extensions_.Insert(scoped_extension);
2001 // TODO(aa): This seems dodgy. AddExtension() could get called with a
2002 // disabled extension for other reasons other than that an update was
2003 // disabled, e.g. as in ExtensionManagementTest.InstallRequiresConfirm.
[email protected]ad50def52011-10-19 23:17:072004 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272005 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]6c2381d2011-10-19 02:52:532006 content::Source<Profile>(profile_),
2007 content::Details<const Extension>(extension));
[email protected]3bdba0d2011-08-23 07:17:302008 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172009 return;
[email protected]e72e8eb82009-06-18 17:21:512010 }
[email protected]aab98a52009-12-02 03:22:352011
[email protected]84df8332011-12-06 18:22:462012 extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:302013 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:172014 NotifyExtensionLoaded(extension);
[email protected]d1282c22011-10-18 02:00:392015 IdentifyAlertableExtensions();
[email protected]aab98a52009-12-02 03:22:352016}
2017
[email protected]902fd7b2011-07-27 18:42:312018void ExtensionService::InitializePermissions(const Extension* extension) {
2019 // If the extension has used the optional permissions API, it will have a
2020 // custom set of active permissions defined in the extension prefs. Here,
2021 // we update the extension's active permissions based on the prefs.
2022 scoped_refptr<ExtensionPermissionSet> active_permissions =
2023 extension_prefs()->GetActivePermissions(extension->id());
2024
2025 if (active_permissions.get()) {
2026 // We restrict the active permissions to be within the bounds defined in the
2027 // extension's manifest.
2028 // a) active permissions must be a subset of optional + default permissions
2029 // b) active permissions must contains all default permissions
2030 scoped_refptr<ExtensionPermissionSet> total_permissions =
2031 ExtensionPermissionSet::CreateUnion(
2032 extension->required_permission_set(),
2033 extension->optional_permission_set());
2034
2035 // Make sure the active permissions contain no more than optional + default.
2036 scoped_refptr<ExtensionPermissionSet> adjusted_active =
2037 ExtensionPermissionSet::CreateIntersection(
2038 total_permissions.get(), active_permissions.get());
2039
2040 // Make sure the active permissions contain the default permissions.
2041 adjusted_active = ExtensionPermissionSet::CreateUnion(
2042 extension->required_permission_set(), adjusted_active.get());
2043
[email protected]c333e792012-01-06 16:57:392044 extensions::PermissionsUpdater perms_updater(profile());
2045 perms_updater.UpdateActivePermissions(extension, adjusted_active);
[email protected]902fd7b2011-07-27 18:42:312046 }
2047
[email protected]8d888c12010-11-30 00:00:252048 // We keep track of all permissions the user has granted each extension.
2049 // This allows extensions to gracefully support backwards compatibility
2050 // by including unknown permissions in their manifests. When the user
2051 // installs the extension, only the recognized permissions are recorded.
2052 // When the unknown permissions become recognized (e.g., through browser
2053 // upgrade), we can prompt the user to accept these new permissions.
2054 // Extensions can also silently upgrade to less permissions, and then
2055 // silently upgrade to a version that adds these permissions back.
2056 //
2057 // For example, pretend that Chrome 10 includes a permission "omnibox"
2058 // for an API that adds suggestions to the omnibox. An extension can
2059 // maintain backwards compatibility while still having "omnibox" in the
2060 // manifest. If a user installs the extension on Chrome 9, the browser
2061 // will record the permissions it recognized, not including "omnibox."
2062 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
2063 // will disable the extension and prompt the user to approve the increase
2064 // in privileges. The extension could then release a new version that
2065 // removes the "omnibox" permission. When the user upgrades, Chrome will
2066 // still remember that "omnibox" had been granted, so that if the
2067 // extension once again includes "omnibox" in an upgrade, the extension
2068 // can upgrade without requiring this user's approval.
2069 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:472070 true, true, false);
[email protected]8d888c12010-11-30 00:00:252071 bool is_extension_upgrade = old != NULL;
2072 bool is_privilege_increase = false;
2073
[email protected]902fd7b2011-07-27 18:42:312074 // We only need to compare the granted permissions to the current permissions
2075 // if the extension is not allowed to silently increase its permissions.
2076 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252077 // Add all the recognized permissions if the granted permissions list
2078 // hasn't been initialized yet.
[email protected]902fd7b2011-07-27 18:42:312079 scoped_refptr<ExtensionPermissionSet> granted_permissions =
2080 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522081 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252082
2083 // Here, we check if an extension's privileges have increased in a manner
2084 // that requires the user's approval. This could occur because the browser
2085 // upgraded and recognized additional privileges, or an extension upgrades
2086 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522087 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312088 granted_permissions->HasLessPrivilegesThan(
2089 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252090 }
2091
2092 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252093 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2094 // that we aren't downgrading.
2095 if (extension->location() != Extension::LOAD)
2096 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252097
2098 // Extensions get upgraded if the privileges are allowed to increase or
2099 // the privileges haven't increased.
2100 if (!is_privilege_increase) {
2101 SetBeingUpgraded(old, true);
2102 SetBeingUpgraded(extension, true);
2103 }
2104
2105 // To upgrade an extension in place, unload the old one and
2106 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592107 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252108 old = NULL;
2109 }
2110
2111 // Extension has changed permissions significantly. Disable it. A
2112 // notification should be sent by the caller.
2113 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492114 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2115 RecordPermissionMessagesHistogram(
2116 extension, "Extensions.Permissions_AutoDisable");
2117 }
[email protected]b3317ad2011-04-28 23:46:002118 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252119 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
2120 }
2121}
2122
[email protected]eaa7dd182010-12-14 11:09:002123void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392124 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462125 for (ExtensionSet::const_iterator iter = extensions_.begin();
2126 iter != extensions_.end(); ++iter) {
2127 const Extension* extension = *iter;
2128 if (!extension->is_theme() && extension->location() != Extension::COMPONENT)
2129 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352130 }
2131
2132 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252133}
2134
[email protected]8266d662011-07-12 21:53:262135void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502136 const Extension* extension,
2137 bool from_webstore,
2138 const StringOrdinal& page_ordinal) {
[email protected]a29a517a2011-01-21 21:11:122139 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202140
[email protected]4416c5a2010-06-26 01:28:572141 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022142 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352143 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592144 // Extensions installed by policy can't be disabled. So even if a previous
2145 // installation disabled the extension, make sure it is now enabled.
2146 bool initial_enable =
2147 !extension_prefs_->IsExtensionDisabled(id) ||
2148 !Extension::UserMayDisable(extension->location());
[email protected]b2907fd2011-03-25 16:43:372149 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352150 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2151 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322152
[email protected]483d1ff2011-03-03 17:12:022153 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572154 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542155 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352156 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082157 << " and update URL " << extension->update_url().spec()
2158 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542159
[email protected]ad50def52011-10-19 23:17:072160 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272161 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532162 content::Source<Profile>(profile_),
2163 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542164
[email protected]4416c5a2010-06-26 01:28:572165 // Delete the extension directory since we're not going to
2166 // load it.
[email protected]14908b72011-04-20 06:54:362167 if (!BrowserThread::PostTask(
2168 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362169 base::Bind(&extension_file_util::DeleteFile,
2170 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362171 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572172 return;
2173 }
[email protected]4416c5a2010-06-26 01:28:572174 } else {
[email protected]c3cfb012011-04-06 22:07:352175 // We explicitly want to re-enable an uninstalled external
2176 // extension; if we're here, that means the user is manually
2177 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332178 if (IsExternalExtensionUninstalled(id)) {
2179 initial_enable = true;
2180 }
[email protected]aa142702010-03-26 01:26:332181 }
2182
[email protected]07533022011-06-27 20:42:552183 // Do not record the install histograms for upgrades.
2184 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2185 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2186 extension->GetType(), 100);
2187 RecordPermissionMessagesHistogram(
2188 extension, "Extensions.Permissions_Install");
2189 }
2190
[email protected]4416c5a2010-06-26 01:28:572191 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262192 extension,
2193 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352194 from_webstore,
[email protected]36a5c4c2011-12-14 16:34:502195 page_ordinal);
[email protected]25b34332009-06-05 21:53:192196
[email protected]cdfca9702011-08-08 16:07:012197 // Unpacked extensions default to allowing file access, but if that has been
2198 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192199 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012200 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2201 extension_prefs_->SetAllowFileAccess(id, true);
2202 }
2203
[email protected]b6cf240f2011-10-15 22:09:532204 // If the extension should automatically block network startup (e.g., it uses
2205 // the webRequest API), set the preference. Otherwise clear it, in case the
2206 // extension stopped using a relevant API.
2207 extension_prefs_->SetDelaysNetworkRequests(
2208 extension->id(), extension->ImplicitlyDelaysNetworkStartup());
2209
[email protected]ad50def52011-10-19 23:17:072210 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272211 chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]6c2381d2011-10-19 02:52:532212 content::Source<Profile>(profile_),
2213 content::Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:582214
[email protected]9de83282011-12-12 11:06:372215 // Temporary feature to always install shortcuts for platform apps to
2216 // facilitate early testing.
2217 // TODO(benwells): Remove before launching platform apps.
2218 if (extension->is_platform_app()) {
2219 StartInstallApplicationShortcut(extension);
2220 }
2221
[email protected]49cd1572011-02-08 21:38:452222 // Transfer ownership of |extension| to AddExtension.
2223 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422224}
2225
[email protected]eaa7dd182010-12-14 11:09:002226const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472227 const std::string& id, bool include_enabled, bool include_disabled,
2228 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052229 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392230 if (include_enabled) {
[email protected]84df8332011-12-06 18:22:462231 const Extension* extension = extensions_.GetByID(lowercase_id);
2232 if (extension)
2233 return extension;
[email protected]0c6da502009-08-14 22:32:392234 }
2235 if (include_disabled) {
[email protected]84df8332011-12-06 18:22:462236 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
2237 if (extension)
2238 return extension;
[email protected]ce5c4502009-05-06 16:46:112239 }
[email protected]8001df22011-04-28 19:59:472240 if (include_terminated) {
[email protected]84df8332011-12-06 18:22:462241 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
2242 if (extension)
2243 return extension;
[email protected]8001df22011-04-28 19:59:472244 }
[email protected]ce5c4502009-05-06 16:46:112245 return NULL;
2246}
2247
[email protected]bb7f40952011-01-13 00:21:202248void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462249 if (!terminated_extensions_.Contains(extension->id()))
2250 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202251
[email protected]b3f7fe22011-11-11 19:27:562252 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202253}
2254
2255void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202256 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462257 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202258}
2259
[email protected]0dfe05c2011-02-23 23:03:362260const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472261 const std::string& id) const {
2262 return GetExtensionByIdInternal(id, false, false, true);
2263}
2264
2265const Extension* ExtensionService::GetInstalledExtension(
2266 const std::string& id) const {
2267 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362268}
2269
[email protected]eaa7dd182010-12-14 11:09:002270const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:072271 return GetExtensionById(extension_misc::kWebStoreAppId, false);
2272}
2273
[email protected]eaa7dd182010-12-14 11:09:002274bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442275 // Allow bindings for all packaged extensions and component hosted apps.
2276 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2277 ExtensionURLInfo(url));
2278 return extension && (!extension->is_hosted_app() ||
2279 extension->location() == Extension::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392280}
2281
[email protected]eaa7dd182010-12-14 11:09:002282const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472283 const std::string& extension_id) {
2284 return omnibox_icon_manager_.GetIcon(extension_id);
2285}
2286
[email protected]eaa7dd182010-12-14 11:09:002287const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312288 const std::string& extension_id) {
2289 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2290}
2291
[email protected]eaa7dd182010-12-14 11:09:002292void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322293 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142294 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322295 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312296 Extension::Location location,
[email protected]47fc70c2011-12-06 07:29:512297 int creation_flags,
2298 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382299 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2300 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012301 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:572302 return;
[email protected]a8af9fdb2010-10-28 21:52:202303
[email protected]8e4560b62011-01-14 10:09:142304 DCHECK(version);
2305
[email protected]7577a5c52009-07-30 06:21:582306 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492307 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582308 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022309 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582310 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142311 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582312 case -1: // existing version is older, we should upgrade
2313 break;
2314 case 0: // existing version is same, do nothing
2315 return;
2316 case 1: // existing version is newer, uh-oh
2317 LOG(WARNING) << "Found external version of extension " << id
2318 << "that is older than current version. Current version "
2319 << "is: " << existing->VersionString() << ". New version "
[email protected]16d900042012-01-04 21:24:322320 << "is: " << version->GetString()
2321 << ". Keeping current version.";
[email protected]7577a5c52009-07-30 06:21:582322 return;
2323 }
2324 }
2325
[email protected]b2907fd2011-03-25 16:43:372326 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362327
[email protected]14908b72011-04-20 06:54:362328 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012329 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162330 installer->set_install_source(location);
2331 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072332 installer->set_expected_version(*version);
2333 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312334 installer->set_creation_flags(creation_flags);
[email protected]6dfbbf82010-03-12 23:09:162335 installer->InstallCrx(path);
[email protected]47fc70c2011-12-06 07:29:512336
2337 // Depending on the source, a new external extension might not need a user
2338 // notification on installation. For such extensions, mark them acknowledged
2339 // now to suppress the notification.
2340 if (mark_acknowledged)
2341 AcknowledgeExternalExtension(id);
[email protected]7577a5c52009-07-30 06:21:582342}
2343
[email protected]eaa7dd182010-12-14 11:09:002344void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402345 const FilePath& extension_path,
2346 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402347 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012348 content::NotificationService::current()->Notify(
2349 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2350 content::Source<Profile>(profile_),
2351 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402352
[email protected]8a205c02011-02-04 20:41:332353 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]fc670822011-12-17 09:33:492354 string16 message = ASCIIToUTF16(base::StringPrintf(
[email protected]18d4b6c2010-09-21 03:21:042355 "Could not load extension from '%s'. %s",
[email protected]fc670822011-12-17 09:33:492356 path_str.c_str(), error.c_str()));
[email protected]d11c8e92009-10-20 23:26:402357 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2358}
2359
[email protected]eaa7dd182010-12-14 11:09:002360void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182361 ExtensionHost* host) {
2362 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332363 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182364 if (iter == orphaned_dev_tools_.end())
2365 return;
2366
[email protected]0e12d7d2011-12-01 16:21:442367 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
2368 host->render_view_host());
2369 content::DevToolsManager::GetInstance()->AttachClientHost(iter->second,
2370 agent);
[email protected]406027c02010-09-27 08:03:182371 orphaned_dev_tools_.erase(iter);
2372}
2373
[email protected]432115822011-07-10 15:52:272374void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532375 const content::NotificationSource& source,
2376 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272377 switch (type) {
2378 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532379 if (profile_ !=
2380 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322381 break;
[email protected]6c2381d2011-10-19 02:52:532382 }
[email protected]a4ed6282009-12-14 20:51:162383
[email protected]6c2381d2011-10-19 02:52:532384 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262385
[email protected]fa2416f2011-05-03 08:41:202386 // Mark the extension as terminated and Unload it. We want it to
2387 // be in a consistent state: either fully working or not loaded
2388 // at all, but never half-crashed. We do it in a PostTask so
2389 // that other handlers of this notification will still have
2390 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362391 MessageLoop::current()->PostTask(
2392 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362393 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202394 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012395 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202396 host->extension()));
[email protected]31f77262009-12-02 20:48:532397 break;
2398 }
[email protected]432115822011-07-10 15:52:272399 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302400 content::RenderProcessHost* process =
2401 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192402 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302403 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192404 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2405 break;
2406
[email protected]77a6970c2011-04-23 16:58:562407 // Valid extension function names, used to setup bindings in renderer.
2408 std::vector<std::string> function_names;
2409 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2410 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532411
[email protected]77a6970c2011-04-23 16:58:562412 // Scripting whitelist. This is modified by tests and must be communicated
2413 // to renderers.
2414 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2415 *Extension::GetScriptingWhitelist()));
2416
2417 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532418 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462419 for (ExtensionSet::const_iterator iter = extensions_.begin();
2420 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492421 // Renderers don't need to know about themes.
2422 if (!(*iter)->is_theme())
2423 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562424 }
[email protected]9776e82e2011-11-15 02:17:532425 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562426 break;
2427 }
[email protected]432115822011-07-10 15:52:272428 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302429 content::RenderProcessHost* process =
2430 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192431 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302432 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192433 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2434 break;
2435
[email protected]f3b1a082011-11-18 00:34:302436 installed_app_hosts_.erase(process->GetID());
[email protected]6f371442011-11-09 06:45:462437
[email protected]6bc04fd82011-12-04 02:29:352438 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462439 BrowserThread::PostTask(
2440 BrowserThread::IO, FROM_HERE,
2441 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
2442 profile_->GetExtensionInfoMap(),
[email protected]f3b1a082011-11-18 00:34:302443 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212444 break;
2445 }
[email protected]432115822011-07-10 15:52:272446 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532447 std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102448 if (*pref_name == prefs::kExtensionInstallAllowList ||
2449 *pref_name == prefs::kExtensionInstallDenyList) {
2450 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102451 } else {
2452 NOTREACHED() << "Unexpected preference name.";
2453 }
[email protected]aa96d3a2010-08-21 08:45:252454 break;
2455 }
[email protected]d91268022011-08-26 13:17:372456 case chrome::NOTIFICATION_IMPORT_FINISHED: {
[email protected]cf593af2011-12-30 05:44:392457 InitAfterImport();
[email protected]d91268022011-08-26 13:17:372458 break;
2459 }
[email protected]aa96d3a2010-08-21 08:45:252460
[email protected]4814b512009-11-07 00:12:292461 default:
2462 NOTREACHED() << "Unexpected notification type.";
2463 }
2464}
2465
[email protected]eaa7dd182010-12-14 11:09:002466bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182467 return !GetAppIds().empty();
2468}
[email protected]377011d2010-07-20 04:18:502469
[email protected]eaa7dd182010-12-14 11:09:002470ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182471 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462472 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502473 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182474 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2475 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502476 }
2477
[email protected]ec5b50d2010-10-09 16:35:182478 return result;
[email protected]377011d2010-07-20 04:18:502479}
[email protected]d7e9a862010-11-03 21:57:492480
[email protected]eaa7dd182010-12-14 11:09:002481bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282482 return (!extension->has_background_page() ||
[email protected]d7e9a862010-11-03 21:57:492483 extension_runtime_data_[extension->id()].background_page_ready);
2484}
2485
[email protected]eaa7dd182010-12-14 11:09:002486void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]a03d4448f2012-01-10 23:25:282487 DCHECK(extension->has_background_page());
[email protected]d7e9a862010-11-03 21:57:492488 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072489 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272490 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532491 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072492 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492493}
2494
[email protected]eaa7dd182010-12-14 11:09:002495bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492496 return extension_runtime_data_[extension->id()].being_upgraded;
2497}
2498
[email protected]eaa7dd182010-12-14 11:09:002499void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492500 bool value) {
2501 extension_runtime_data_[extension->id()].being_upgraded = value;
2502}
[email protected]1bead0712010-11-27 17:41:532503
[email protected]39a5b532011-10-22 01:47:072504bool ExtensionService::HasUsedWebRequest(const Extension* extension) {
2505 return extension_runtime_data_[extension->id()].has_used_webrequest;
2506}
2507
2508void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2509 bool value) {
2510 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2511}
2512
[email protected]45644f62011-11-23 00:58:232513base::PropertyBag* ExtensionService::GetPropertyBag(
2514 const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532515 return &extension_runtime_data_[extension->id()].property_bag;
2516}
[email protected]cebc3dc2011-04-18 17:15:002517
2518void ExtensionService::RegisterNaClModule(const GURL& url,
2519 const std::string& mime_type) {
2520 NaClModuleInfo info;
2521 info.url = url;
2522 info.mime_type = mime_type;
2523
2524 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2525 nacl_module_list_.push_front(info);
2526}
2527
2528void ExtensionService::UnregisterNaClModule(const GURL& url) {
2529 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2530 DCHECK(iter != nacl_module_list_.end());
2531 nacl_module_list_.erase(iter);
2532}
2533
[email protected]ed0ba002011-05-26 16:55:132534void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332535 // An extension has been added which has a nacl_module component, which means
2536 // there is a MIME type that module wants to handle, so we need to add that
2537 // MIME type to plugins which handle NaCl modules in order to allow the
2538 // individual modules to handle these types.
[email protected]1e127022011-11-29 21:11:472539 FilePath path;
2540 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2541 return;
2542 const content::PepperPluginInfo* pepper_info =
2543 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2544 if (!pepper_info)
2545 return;
[email protected]300c0ea2011-07-15 23:04:332546
[email protected]1e127022011-11-29 21:11:472547 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2548 // Check each MIME type the plugins handle for the NaCl MIME type.
2549 for (mime_iter = pepper_info->mime_types.begin();
2550 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2551 if (mime_iter->mime_type == kNaClPluginMimeType) {
2552 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332553
[email protected]1e127022011-11-29 21:11:472554 PluginService::GetInstance()->
2555 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332556
[email protected]1e127022011-11-29 21:11:472557 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332558
[email protected]1e127022011-11-29 21:11:472559 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2560 nacl_module_list_.begin();
2561 iter != nacl_module_list_.end(); ++iter) {
2562 // Add the MIME type specified in the extension to this NaCl plugin,
2563 // With an extra "nacl" argument to specify the location of the NaCl
2564 // manifest file.
2565 webkit::WebPluginMimeType mime_type_info;
2566 mime_type_info.mime_type = iter->mime_type;
2567 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2568 mime_type_info.additional_param_values.push_back(
2569 UTF8ToUTF16(iter->url.spec()));
2570 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332571 }
[email protected]1e127022011-11-29 21:11:472572
2573 PluginService::GetInstance()->RefreshPlugins();
2574 PluginService::GetInstance()->RegisterInternalPlugin(info);
2575 // This plugin has been modified, no need to check the rest of its
2576 // types, but continue checking other plugins.
2577 break;
[email protected]ed0ba002011-05-26 16:55:132578 }
2579 }
[email protected]ed0ba002011-05-26 16:55:132580}
2581
[email protected]cebc3dc2011-04-18 17:15:002582ExtensionService::NaClModuleInfoList::iterator
2583 ExtensionService::FindNaClModule(const GURL& url) {
2584 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2585 iter != nacl_module_list_.end(); ++iter) {
2586 if (iter->url == url)
2587 return iter;
2588 }
2589 return nacl_module_list_.end();
2590}
[email protected]9de83282011-12-12 11:06:372591
2592void ExtensionService::StartInstallApplicationShortcut(
2593 const Extension* extension) {
2594#if !defined(OS_MACOSX)
2595 const int kAppIconSize = 32;
2596
2597 shortcut_info_.extension_id = extension->id();
2598 shortcut_info_.url = GURL(extension->launch_web_url());
2599 shortcut_info_.title = UTF8ToUTF16(extension->name());
2600 shortcut_info_.description = UTF8ToUTF16(extension->description());
2601 shortcut_info_.create_in_applications_menu = true;
2602 shortcut_info_.create_in_quick_launch_bar = true;
2603 shortcut_info_.create_on_desktop = true;
2604
2605 // The icon will be resized to |max_size|.
2606 const gfx::Size max_size(kAppIconSize, kAppIconSize);
2607
2608 // Look for an icon. If there is no icon at the ideal size, we will resize
2609 // whatever we can get. Making a large icon smaller is prefered to making a
2610 // small icon larger, so look for a larger icon first:
2611 ExtensionResource icon_resource = extension->GetIconResource(
2612 kAppIconSize,
2613 ExtensionIconSet::MATCH_BIGGER);
2614
2615 // If no icon exists that is the desired size or larger, get the
2616 // largest icon available:
2617 if (icon_resource.empty()) {
2618 icon_resource = extension->GetIconResource(
2619 kAppIconSize,
2620 ExtensionIconSet::MATCH_SMALLER);
2621 }
2622
2623 // icon_resource may still be empty at this point, in which case LoadImage
2624 // which call the OnImageLoaded callback with a NULL image and exit
2625 // immediately.
2626 tracker_.LoadImage(extension,
2627 icon_resource,
2628 max_size,
2629 ImageLoadingTracker::DONT_CACHE);
2630#endif
2631}
2632
2633void ExtensionService::OnImageLoaded(SkBitmap *image,
2634 const ExtensionResource &resource,
2635 int index) {
2636 // If the image failed to load (e.g. if the resource being loaded was empty)
2637 // use the standard application icon.
2638 if (!image || image->isNull())
2639 image = ExtensionIconSource::LoadImageByResourceId(IDR_APP_DEFAULT_ICON);
2640
2641 shortcut_info_.favicon = *image;
2642 web_app::CreateShortcut(profile_->GetPath(), shortcut_info_);
2643}
[email protected]e1fae1c2011-12-13 22:17:172644
2645extensions::SocketController* ExtensionService::socket_controller() {
2646 // TODO(miket): Find a better place for SocketController to live. It needs
2647 // to be scoped such that it can be created and destroyed on the IO thread.
2648 //
2649 // To coexist with certain unit tests that don't have an IO thread message
2650 // loop available at ExtensionService shutdown, we lazy-initialize this
2651 // object so that those cases neither create nor destroy a SocketController.
2652 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
2653 if (!socket_controller_) {
2654 socket_controller_ = new extensions::SocketController();
2655 }
2656 return socket_controller_;
2657}