blob: 8460e02bf5a08b735cefe8eebe747dbd6d5d8e57 [file] [log] [blame]
[email protected]8e4560b62011-01-14 10:09:141// Copyright (c) 2011 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]a1755bd2011-12-09 00:40:2024#include "base/task.h"
[email protected]34b99632011-01-01 01:01:0625#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3626#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5927#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1928#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3329#include "base/version.h"
[email protected]a692a952011-11-28 08:23:5430#include "chrome/browser/accessibility/accessibility_extension_api.h"
[email protected]23d5f172011-10-25 05:49:5331#include "chrome/browser/bookmarks/bookmark_extension_api.h"
[email protected]15730c42009-09-03 00:03:2032#include "chrome/browser/browser_process.h"
[email protected]dfba8762011-09-02 12:49:5433#include "chrome/browser/chrome_plugin_service_filter.h"
[email protected]cdcf60242011-11-04 15:53:4334#include "chrome/browser/download/download_extension_api.h"
[email protected]a1755bd2011-12-09 00:40:2035#include "chrome/browser/extensions/api/socket/socket_api_controller.h"
[email protected]af9db5f2011-10-05 05:13:1536#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]18cc5a52011-04-15 16:03:3837#include "chrome/browser/extensions/apps_promo.h"
[email protected]d8c8f25f2011-11-02 18:18:0138#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1739#include "chrome/browser/extensions/crx_installer.h"
[email protected]49098f702011-10-13 03:47:1840#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]b68d5ed2009-04-16 02:41:2841#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4442#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3243#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2444#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]e5af875f2011-10-10 21:09:1445#include "chrome/browser/extensions/extension_global_error.h"
[email protected]b1748b1d82009-11-30 20:32:5646#include "chrome/browser/extensions/extension_host.h"
[email protected]8baf0dbb2011-11-27 22:47:0247#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]7596ce72010-08-30 05:10:4648#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4049#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2950#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1151#include "chrome/browser/extensions/extension_processes_api.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]cccdf0aa2011-11-11 03:43:3861#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0162#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]d2e6bd62011-10-24 20:29:0763#include "chrome/browser/history/history_extension_api.h"
[email protected]56ad3792010-05-28 17:45:3364#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0265#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3366#include "chrome/browser/profiles/profile.h"
[email protected]8e5c89a2011-06-07 18:13:3367#include "chrome/browser/search_engines/template_url_service.h"
68#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]3bdba0d2011-08-23 07:17:3069#include "chrome/browser/sync/api/sync_change.h"
[email protected]18280372011-03-22 18:05:2270#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4271#include "chrome/browser/themes/theme_service_factory.h"
[email protected]e5af875f2011-10-10 21:09:1472#include "chrome/browser/ui/browser.h"
[email protected]62433d32011-10-12 22:33:1273#include "chrome/browser/ui/browser_list.h"
[email protected]e5af875f2011-10-10 21:09:1474#include "chrome/browser/ui/global_error_service.h"
75#include "chrome/browser/ui/global_error_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1776#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
[email protected]c9c8b212011-12-13 23:59:4077#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
[email protected]c8d407e2011-04-28 21:27:1778#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3179#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]9de83282011-12-12 11:06:3780#include "chrome/browser/web_applications/web_app.h"
[email protected]aab98a52009-12-02 03:22:3581#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2782#include "chrome/common/chrome_notification_types.h"
[email protected]1e127022011-11-29 21:11:4783#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5484#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5885#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4086#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0887#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1388#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5689#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2190#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1991#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1492#include "chrome/common/url_constants.h"
[email protected]2de307592011-04-05 21:16:5893#include "content/browser/plugin_process_host.h"
94#include "content/browser/plugin_service.h"
[email protected]c38831a12011-10-28 12:44:4995#include "content/public/browser/browser_thread.h"
[email protected]0e12d7d2011-12-01 16:21:4496#include "content/public/browser/devtools_agent_host_registry.h"
97#include "content/public/browser/devtools_manager.h"
[email protected]05aad2da2011-10-28 10:12:3798#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1699#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:30100#include "content/public/browser/render_process_host.h"
[email protected]1e127022011-11-29 21:11:47101#include "content/public/common/pepper_plugin_info.h"
[email protected]c10da4b02010-03-25 14:38:32102#include "googleurl/src/gurl.h"
[email protected]9de83282011-12-12 11:06:37103#include "grit/theme_resources.h"
[email protected]1debbbb62010-10-06 17:23:44104#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:44105#include "webkit/database/database_tracker.h"
106#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:31107
[email protected]eed367e2011-04-12 03:43:31108#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13109#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]eed367e2011-04-12 03:43:31110#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01111#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:40112#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13113#include "chrome/browser/chromeos/input_method/input_method_manager.h"
[email protected]8baf0dbb2011-11-27 22:47:02114#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:08115#include "webkit/fileapi/file_system_context.h"
116#include "webkit/fileapi/file_system_mount_point_provider.h"
117#include "webkit/fileapi/file_system_path_manager.h"
[email protected]eed367e2011-04-12 03:43:31118#endif
119
[email protected]9a2aebe02011-12-02 21:54:14120#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40121#include "chrome/browser/extensions/extension_input_ui_api.h"
122#endif
123
[email protected]5ef47ec2010-01-28 05:58:05124using base::Time;
[email protected]631bb742011-11-02 11:29:39125using content::BrowserThread;
[email protected]0e12d7d2011-12-01 16:21:44126using content::DevToolsAgentHost;
127using content::DevToolsAgentHostRegistry;
[email protected]5ef47ec2010-01-28 05:58:05128
[email protected]c6d474f82009-12-16 21:11:06129namespace errors = extension_manifest_errors;
130
[email protected]b6ab96d2009-08-20 18:58:19131namespace {
132
[email protected]29d0d4ac2010-09-08 21:10:31133#if defined(OS_LINUX)
134static const int kOmniboxIconPaddingLeft = 2;
135static const int kOmniboxIconPaddingRight = 2;
136#elif defined(OS_MACOSX)
137static const int kOmniboxIconPaddingLeft = 0;
138static const int kOmniboxIconPaddingRight = 2;
139#else
140static const int kOmniboxIconPaddingLeft = 0;
141static const int kOmniboxIconPaddingRight = 0;
142#endif
143
[email protected]300c0ea2011-07-15 23:04:33144const char* kNaClPluginMimeType = "application/x-nacl";
145
[email protected]2de307592011-04-05 21:16:58146static void ForceShutdownPlugin(const FilePath& plugin_path) {
147 PluginProcessHost* plugin =
148 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
149 if (plugin)
150 plugin->ForceShutdown();
151}
152
[email protected]3bdba0d2011-08-23 07:17:30153static bool IsSyncableExtension(const Extension& extension) {
154 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
155}
156
157static bool IsSyncableApp(const Extension& extension) {
158 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
159}
160
[email protected]c6d474f82009-12-16 21:11:06161} // namespace
[email protected]b6ab96d2009-08-20 18:58:19162
[email protected]eaa7dd182010-12-14 11:09:00163ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49164 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57165 being_upgraded(false),
166 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49167}
168
[email protected]eaa7dd182010-12-14 11:09:00169ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49170}
171
[email protected]cebc3dc2011-04-18 17:15:00172ExtensionService::NaClModuleInfo::NaClModuleInfo() {
173}
174
175ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
176}
177
[email protected]eaa7dd182010-12-14 11:09:00178// ExtensionService.
[email protected]6014d672008-12-05 00:38:25179
[email protected]eaa7dd182010-12-14 11:09:00180const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]15d5b472011-11-24 04:17:24181
182const char* ExtensionService::kLocalAppSettingsDirectoryName =
183 "Local App Settings";
184const char* ExtensionService::kLocalExtensionSettingsDirectoryName =
185 "Local Extension Settings";
186const char* ExtensionService::kSyncAppSettingsDirectoryName =
187 "Sync App Settings";
188const char* ExtensionService::kSyncExtensionSettingsDirectoryName =
189 "Sync Extension Settings";
[email protected]494c06e2009-07-25 01:06:42190
[email protected]8e4560b62011-01-14 10:09:14191void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12192 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20193
[email protected]7a4c6852010-09-16 03:44:22194 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21195 ProviderCollection::const_iterator i;
196 for (i = external_extension_providers_.begin();
197 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14198 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21199 if (i->get()->HasExtension(id))
200 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22201 }
202
[email protected]0f48fca2011-05-19 18:46:35203 // We get the list of external extensions to check from preferences.
204 // It is possible that an extension has preferences but is not loaded.
205 // For example, an extension that requires experimental permissions
206 // will not be loaded if the experimental command line flag is not used.
207 // In this case, do not uninstall.
208 const Extension* extension = GetInstalledExtension(id);
209 if (!extension) {
210 // We can't call UninstallExtension with an unloaded/invalid
211 // extension ID.
212 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
213 << "with id: " << id;
214 return;
215 }
[email protected]d6ebc9792011-04-07 18:18:33216 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22217}
218
[email protected]8e4560b62011-01-14 10:09:14219void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22220 external_extension_providers_.clear();
221}
222
[email protected]8e4560b62011-01-14 10:09:14223void ExtensionService::AddProviderForTesting(
224 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12225 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21226 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14227 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22228}
229
[email protected]8e4560b62011-01-14 10:09:14230void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22231 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42232 const GURL& update_url,
233 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38234 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
235 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20236
[email protected]8a87a5332011-08-11 17:54:59237 const Extension* extension = GetExtensionById(id, true);
238 if (extension) {
239 // Already installed. Skip this install if the current location has
240 // higher priority than |location|.
241 Extension::Location current = extension->location();
242 if (current == Extension::GetHigherPriorityLocation(current, location))
243 return;
244 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22245 }
[email protected]b2907fd2011-03-25 16:43:37246 pending_extension_manager()->AddFromExternalUpdateUrl(
247 id, update_url, location);
[email protected]50067e52011-10-20 23:17:07248 external_extension_url_added_ = true;
[email protected]7a4c6852010-09-16 03:44:22249}
250
[email protected]454434f2011-08-24 04:00:00251// If a download url matches one of these patterns and has a referrer of the
252// webstore, then we're willing to treat that as a gallery download.
253static const char* kAllowedDownloadURLPatterns[] = {
254 "https://clients2.google.com/service/update2*",
255 "https://clients2.googleusercontent.com/crx/*"
256};
257
[email protected]eaa7dd182010-12-14 11:09:00258bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12259 const GURL& referrer_url) {
[email protected]615d88f2011-12-13 01:47:44260 const Extension* download_extension =
261 extensions_.GetHostedAppByURL(ExtensionURLInfo(download_url));
262 const Extension* referrer_extension =
263 extensions_.GetHostedAppByURL(ExtensionURLInfo(referrer_url));
[email protected]9adb9692010-10-29 23:14:02264 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07265
266 bool referrer_valid = (referrer_extension == webstore_app);
267 bool download_valid = (download_extension == webstore_app);
268
[email protected]454434f2011-08-24 04:00:00269 // We also allow the download to be from a small set of trusted paths.
270 if (!download_valid) {
271 for (size_t i = 0; i < arraysize(kAllowedDownloadURLPatterns); i++) {
272 URLPattern pattern(URLPattern::SCHEME_HTTPS,
273 kAllowedDownloadURLPatterns[i]);
274 if (pattern.MatchesURL(download_url)) {
275 download_valid = true;
276 break;
277 }
278 }
279 }
280
[email protected]d3071992010-10-08 15:24:07281 // If the command-line gallery URL is set, then be a bit more lenient.
282 GURL store_url =
283 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
284 switches::kAppsGalleryURL));
285 if (!store_url.is_empty()) {
286 std::string store_tld =
287 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
288 if (!referrer_valid) {
289 std::string referrer_tld =
290 net::RegistryControlledDomainService::GetDomainAndRegistry(
291 referrer_url);
292 // The referrer gets stripped when transitioning from https to http,
293 // or when hitting an unknown test cert and that commonly happens in
294 // testing environments. Given this, we allow an empty referrer when
295 // the command-line flag is set.
296 // Otherwise, the TLD must match the TLD of the command-line url.
297 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
298 }
299
300 if (!download_valid) {
301 std::string download_tld =
302 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50303 download_url);
[email protected]d3071992010-10-08 15:24:07304
305 // Otherwise, the TLD must match the TLD of the command-line url.
306 download_valid = (download_tld == store_tld);
307 }
308 }
309
310 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23311}
312
[email protected]d9696672011-03-15 22:45:09313const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:44314 const Extension* extension = extensions_.GetExtensionOrAppByURL(
315 ExtensionURLInfo(url));
316 if (extension && extension->is_app())
317 return extension;
[email protected]d9696672011-03-15 22:45:09318
319 return NULL;
320}
321
322bool ExtensionService::IsInstalledApp(const GURL& url) {
323 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27324}
325
[email protected]da5683db2011-04-23 17:12:21326void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
327 const Extension* app) {
328 installed_app_hosts_[renderer_child_id] = app;
329}
330
331const Extension* ExtensionService::GetInstalledAppForRenderer(
332 int renderer_child_id) {
333 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
334 if (i == installed_app_hosts_.end())
335 return NULL;
336 return i->second;
337}
338
[email protected]6aeac8342010-10-01 20:21:18339// static
[email protected]d6ebc9792011-04-07 18:18:33340// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30341// --uninstall-extension, and to uninstall an extension via sync. The LOG
342// statements within this function are used to inform the user if the uninstall
343// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00344bool ExtensionService::UninstallExtensionHelper(
345 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18346 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18347
[email protected]95da88c42011-03-31 10:07:33348 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47349 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33350
[email protected]d6ebc9792011-04-07 18:18:33351 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33352 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18353 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33354 << "id: " << extension_id;
355 return false;
[email protected]6aeac8342010-10-01 20:21:18356 }
357
[email protected]d6ebc9792011-04-07 18:18:33358 // The following call to UninstallExtension will not allow an uninstall of a
359 // policy-controlled extension.
360 std::string error;
361 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
362 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
363 << ": " << error;
364 return false;
365 }
[email protected]95da88c42011-03-31 10:07:33366
[email protected]6aeac8342010-10-01 20:21:18367 return true;
368}
369
[email protected]eaa7dd182010-12-14 11:09:00370ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24371 const CommandLine* command_line,
372 const FilePath& install_directory,
373 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03374 bool autoupdate_enabled,
375 bool extensions_enabled)
[email protected]d8c8f25f2011-11-02 18:18:01376 : profile_(profile),
[email protected]73c47932010-12-06 18:13:43377 extension_prefs_(extension_prefs),
[email protected]cccdf0aa2011-11-11 03:43:38378 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36379 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23380 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03381 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13382 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55383 ready_(false),
[email protected]14908b72011-04-20 06:54:36384 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23385 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15386 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]902fd7b2011-07-27 18:42:31387 permissions_manager_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]18cc5a52011-04-15 16:03:38388 apps_promo_(profile->GetPrefs()),
[email protected]7c1490da2011-10-11 18:53:25389 event_routers_initialized_(false),
[email protected]a1755bd2011-12-09 00:40:20390 extension_warnings_(profile),
[email protected]e1fae1c2011-12-13 22:17:17391 socket_controller_(NULL),
[email protected]9de83282011-12-12 11:06:37392 tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]a29a517a2011-01-21 21:11:12393 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20394
[email protected]36a784c2009-06-23 06:21:08395 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23396 if (command_line->HasSwitch(switches::kDisableExtensions)) {
397 extensions_enabled_ = false;
398 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
399 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18400 }
[email protected]36a784c2009-06-23 06:21:08401
[email protected]432115822011-07-10 15:52:27402 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07403 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27404 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07405 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27406 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07407 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28408 pref_change_registrar_.Init(profile->GetPrefs());
409 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
410 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29411
[email protected]93fd78f42009-07-10 16:43:17412 // Set up the ExtensionUpdater
413 if (autoupdate_enabled) {
414 int update_frequency = kDefaultUpdateFrequencySeconds;
415 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25416 base::StringToInt(command_line->GetSwitchValueASCII(
417 switches::kExtensionsUpdateFrequency),
418 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17419 }
[email protected]82b6e512011-04-12 20:33:27420 updater_.reset(new ExtensionUpdater(this,
421 extension_prefs,
422 profile->GetPrefs(),
423 profile,
424 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17425 }
426
[email protected]25ae0152011-11-18 14:40:02427 component_loader_.reset(
428 new extensions::ComponentLoader(this,
429 profile->GetPrefs(),
430 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14431
[email protected]af9db5f2011-10-05 05:13:15432 app_notification_manager_->Init();
433
[email protected]0436b102011-04-15 18:30:03434 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05435 ExternalExtensionProviderImpl::CreateExternalProviders(
436 this, profile_, &external_extension_providers_);
437 }
[email protected]b671760b2010-07-15 21:13:47438
[email protected]aa96d3a2010-08-21 08:45:25439 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31440 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47441 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31442 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
443 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07444
445 // How long is the path to the Extensions directory?
446 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
447 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25448}
449
[email protected]84df8332011-12-06 18:22:46450const ExtensionSet* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45451 return &extensions_;
452}
453
[email protected]84df8332011-12-06 18:22:46454const ExtensionSet* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45455 return &disabled_extensions_;
456}
457
[email protected]84df8332011-12-06 18:22:46458const ExtensionSet* ExtensionService::terminated_extensions() const {
[email protected]bb7f40952011-01-13 00:21:20459 return &terminated_extensions_;
460}
461
[email protected]b2907fd2011-03-25 16:43:37462PendingExtensionManager* ExtensionService::pending_extension_manager() {
463 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45464}
465
[email protected]eaa7dd182010-12-14 11:09:00466ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17467 // No need to unload extensions here because they are profile-scoped, and the
468 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17469
[email protected]8e4560b62011-01-14 10:09:14470 ProviderCollection::const_iterator i;
471 for (i = external_extension_providers_.begin();
472 i != external_extension_providers_.end(); ++i) {
473 ExternalExtensionProviderInterface* provider = i->get();
474 provider->ServiceShutdown();
475 }
[email protected]a1755bd2011-12-09 00:40:20476
477 // TODO(miket): if we find ourselves adding more and more per-API
478 // controllers, we should manage them all with an
479 // APIControllerController (still working on that name).
[email protected]e1fae1c2011-12-13 22:17:17480 if (socket_controller_) {
481 // If this check failed, then a unit test was using sockets but didn't
482 // provide the IO thread message loop needed for those sockets to do their
483 // job (including destroying themselves at shutdown).
484 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO));
485 BrowserThread::PostTask(
486 BrowserThread::IO, FROM_HERE,
487 new DeleteTask<extensions::SocketController>(socket_controller_));
488 }
[email protected]6014d672008-12-05 00:38:25489}
490
[email protected]d91268022011-08-26 13:17:37491void ExtensionService::InitEventRoutersAfterImport() {
492 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
[email protected]6c2381d2011-10-19 02:52:53493 content::Source<Profile>(profile_));
[email protected]d91268022011-08-26 13:17:37494}
495
[email protected]eaa7dd182010-12-14 11:09:00496void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18497 if (event_routers_initialized_)
498 return;
499
[email protected]370e9502011-09-14 19:31:44500 downloads_event_router_.reset(new ExtensionDownloadsEventRouter(profile_));
[email protected]d2e6bd62011-10-24 20:29:07501 history_event_router_.reset(new HistoryExtensionEventRouter());
[email protected]f825df22011-06-28 17:36:10502 history_event_router_->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08503 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
504 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40505 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]23d5f172011-10-25 05:49:53506 bookmark_event_router_.reset(new BookmarkExtensionEventRouter(
[email protected]f825df22011-06-28 17:36:10507 profile_->GetBookmarkModel()));
508 bookmark_event_router_->Init();
509 cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_));
510 cookies_event_router_->Init();
511 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
512 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11513 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10514 web_navigation_event_router_.reset(
515 new ExtensionWebNavigationEventRouter(profile_));
516 web_navigation_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40517
[email protected]eed367e2011-04-12 03:43:31518#if defined(OS_CHROMEOS)
[email protected]b16fc2e2011-07-21 21:32:58519 file_browser_event_router_.reset(
520 new ExtensionFileBrowserEventRouter(profile_));
521 file_browser_event_router_->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52522
523 input_method_event_router_.reset(
524 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01525
[email protected]c8bf09be2011-06-25 03:22:27526 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]8baf0dbb2011-11-27 22:47:02527 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31528#endif
[email protected]27072cad2011-05-09 19:46:40529
[email protected]9a2aebe02011-12-02 21:54:14530#if defined(OS_CHROMEOS) && defined(USE_VIRTUAL_KEYBOARD)
[email protected]27072cad2011-05-09 19:46:40531 ExtensionInputUiEventRouter::GetInstance()->Init();
532#endif
533
[email protected]b3d62312b12010-10-14 21:10:18534 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37535}
536
[email protected]b2907fd2011-03-25 16:43:37537const Extension* ExtensionService::GetExtensionById(
538 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47539 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44540}
541
[email protected]eaa7dd182010-12-14 11:09:00542void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12543 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20544
[email protected]fa6a9102010-11-22 15:38:50545 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17546 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32547
[email protected]95dd38f2009-10-20 20:09:15548 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
549 // the first extension, because its members listen for loaded notifications.
550 g_browser_process->resource_dispatcher_host();
551
[email protected]d8c8f25f2011-11-02 18:18:01552 component_loader_->LoadAll();
553 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57554
[email protected]7a161db2011-12-15 20:45:51555 // If we are running in the import process, don't bother initializing the
556 // extension service since this can interfere with the main browser process
557 // that is already running an extension service for this profile.
558 // TODO(aa): can we start up even less of ExtensionService?
559 // http://crbug.com/107636
560 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kImport)) {
561 // TODO(erikkay) this should probably be deferred to a future point
562 // rather than running immediately at startup.
563 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57564
[email protected]7a161db2011-12-15 20:45:51565 // TODO(erikkay) this should probably be deferred as well.
566 GarbageCollectExtensions();
567 }
[email protected]6014d672008-12-05 00:38:25568}
569
[email protected]420a0ec2011-06-01 01:07:03570bool ExtensionService::UpdateExtension(
571 const std::string& id,
572 const FilePath& extension_path,
573 const GURL& download_url,
574 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12575 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20576
[email protected]b2907fd2011-03-25 16:43:37577 PendingExtensionInfo pending_extension_info;
578 bool is_pending_extension = pending_extension_manager_.GetById(
579 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32580
[email protected]8001df22011-04-28 19:59:47581 const Extension* extension =
582 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41583 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33584 LOG(WARNING) << "Will not update extension " << id
585 << " because it is not installed or pending";
586 // Delete extension_path since we're not creating a CrxInstaller
587 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36588 if (!BrowserThread::PostTask(
589 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36590 base::Bind(
[email protected]7296f2762011-11-21 19:23:44591 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36592 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03593
594 return false;
[email protected]e957fe52009-06-23 16:51:05595 }
596
[email protected]aa142702010-03-26 01:26:33597 // We want a silent install only for non-pending extensions and
598 // pending extensions that have install_silently set.
599 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37600 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33601 NULL : new ExtensionInstallUI(profile_);
602
[email protected]d8c8f25f2011-11-02 18:18:01603 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16604 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18605 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37606 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41607 else if (extension)
608 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52609 if (pending_extension_info.install_silently())
610 installer->set_allow_silent_install(true);
[email protected]3d729722011-09-20 02:57:09611 if (extension && extension->from_webstore())
612 installer->set_is_gallery_install(true);
[email protected]6dfbbf82010-03-12 23:09:16613 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06614 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07615 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16616 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03617
618 if (out_crx_installer)
619 *out_crx_installer = installer;
620
621 return true;
[email protected]e957fe52009-06-23 16:51:05622}
623
[email protected]eaa7dd182010-12-14 11:09:00624void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12625 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06626 FilePath path;
[email protected]9adb9692010-10-29 23:14:02627 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40628
[email protected]f17dbd42010-08-16 23:21:10629 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06630 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29631 // If the extension has an inspector open for its background page, detach
632 // the inspector and hang onto a cookie for it, so that we can reattach
633 // later.
634 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
[email protected]06024c62011-10-20 20:57:12635 ExtensionHost* host = manager->GetBackgroundHostForExtension(extension_id);
[email protected]0e12d7d2011-12-01 16:21:44636 if (host && DevToolsAgentHostRegistry::HasDevToolsAgentHost(
637 host->render_view_host())) {
[email protected]4814b512009-11-07 00:12:29638 // Look for an open inspector for the background page.
[email protected]0e12d7d2011-12-01 16:21:44639 DevToolsAgentHost* agent =
640 DevToolsAgentHostRegistry::GetDevToolsAgentHost(
641 host->render_view_host());
642 int devtools_cookie =
643 content::DevToolsManager::GetInstance()->DetachClientHost(agent);
[email protected]4814b512009-11-07 00:12:29644 if (devtools_cookie >= 0)
645 orphaned_dev_tools_[extension_id] = devtools_cookie;
646 }
647
[email protected]b65272f2009-08-31 15:47:06648 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10649 DisableExtension(extension_id);
650 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16651 } else {
652 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06653 }
654
[email protected]e6090e42010-03-23 22:44:08655 // Check the installed extensions to see if what we're reloading was already
656 // installed.
657 scoped_ptr<ExtensionInfo> installed_extension(
658 extension_prefs_->GetInstalledExtensionInfo(extension_id));
659 if (installed_extension.get() &&
660 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01661 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08662 } else {
[email protected]d8c8f25f2011-11-02 18:18:01663 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08664 // We should always be able to remember the extension's path. If it's not in
665 // the map, someone failed to update |unloaded_extension_paths_|.
666 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01667 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08668 }
[email protected]9cddd4702009-07-27 22:09:40669}
670
[email protected]fa2416f2011-05-03 08:41:20671bool ExtensionService::UninstallExtension(
[email protected]a12c706e2011-12-01 00:58:08672 std::string extension_id,
[email protected]fa2416f2011-05-03 08:41:20673 bool external_uninstall,
674 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12675 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20676
[email protected]0d6ec3a72011-09-02 02:09:43677 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25678
[email protected]e7afe2452010-08-22 16:19:13679 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48680 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32681
[email protected]95da88c42011-03-31 10:07:33682 // Policy change which triggers an uninstall will always set
683 // |external_uninstall| to true so this is the only way to uninstall
684 // managed extensions.
[email protected]0d6ec3a72011-09-02 02:09:43685 if (!Extension::UserMayDisable(extension->location()) &&
686 !external_uninstall) {
[email protected]ad50def52011-10-19 23:17:07687 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27688 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53689 content::Source<Profile>(profile_),
690 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33691 if (error != NULL) {
692 *error = errors::kCannotUninstallManagedExtension;
693 }
694 return false;
695 }
[email protected]95da88c42011-03-31 10:07:33696
[email protected]3bdba0d2011-08-23 07:17:30697 // Extract the data we need for sync now, but don't actually sync until we've
698 // completed the uninstallation.
699 SyncBundle* sync_bundle = GetSyncBundleForExtension(*extension);
700
701 SyncChange sync_change;
702 if (sync_bundle) {
[email protected]aa7599d2011-10-28 07:24:32703 ExtensionSyncData extension_sync_data(
704 *extension,
705 IsExtensionEnabled(extension_id),
706 IsIncognitoEnabled(extension_id),
[email protected]b2689a902011-12-01 00:41:09707 extension_prefs_->GetAppNotificationClientId(extension_id),
[email protected]aa7599d2011-10-28 07:24:32708 extension_prefs_->IsAppNotificationDisabled(extension_id));
[email protected]3bdba0d2011-08-23 07:17:30709 sync_change = extension_sync_data.GetSyncChange(SyncChange::ACTION_DELETE);
710 }
711
[email protected]9b217652010-10-08 22:04:23712 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08713 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49714 RecordPermissionMessagesHistogram(
715 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23716
[email protected]8e5c89a2011-06-07 18:13:33717 TemplateURLService* url_service =
718 TemplateURLServiceFactory::GetForProfile(profile_);
719 if (url_service)
720 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33721
[email protected]831aa212010-03-26 13:55:19722 // Unload before doing more cleanup to ensure that nothing is hanging on to
723 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59724 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19725
[email protected]0d6ec3a72011-09-02 02:09:43726 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19727 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32728
729 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43730 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36731 if (!BrowserThread::PostTask(
732 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36733 base::Bind(
[email protected]14908b72011-04-20 06:54:36734 &extension_file_util::UninstallExtension,
735 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20736 extension_id)))
[email protected]14908b72011-04-20 06:54:36737 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32738 }
739
[email protected]0d6ec3a72011-09-02 02:09:43740 GURL launch_web_url_origin(extension->launch_web_url());
741 launch_web_url_origin = launch_web_url_origin.GetOrigin();
742 bool is_storage_isolated =
743 (extension->is_storage_isolated() &&
744 extension->HasAPIPermission(ExtensionAPIPermission::kExperimental));
745
746 if (extension->is_hosted_app() &&
747 !profile_->GetExtensionSpecialStoragePolicy()->
748 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13749 ExtensionDataDeleter::StartDeleting(
750 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43751 }
[email protected]dc0b5a12011-10-14 00:06:13752 ExtensionDataDeleter::StartDeleting(
753 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43754
[email protected]0dfe05c2011-02-23 23:03:36755 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06756
757 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07758 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27759 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53760 content::Source<Profile>(profile_),
761 content::Details<const std::string>(&extension_id));
[email protected]d6ebc9792011-04-07 18:18:33762
[email protected]3bdba0d2011-08-23 07:17:30763 if (sync_bundle && sync_bundle->HasExtensionId(extension_id)) {
764 sync_bundle->sync_processor->ProcessSyncChanges(
765 FROM_HERE, SyncChangeList(1, sync_change));
766 sync_bundle->synced_extensions.erase(extension_id);
767 }
768
[email protected]333b1de2011-09-12 18:28:50769 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18770 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
771
772 static bool default_apps_trial_exists =
[email protected]568c3312011-12-06 15:36:04773 base::FieldTrialList::TrialExists(kDefaultAppsTrialName);
[email protected]49098f702011-10-13 03:47:18774 if (default_apps_trial_exists) {
775 UMA_HISTOGRAM_ENUMERATION(
776 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
[email protected]568c3312011-12-06 15:36:04777 kDefaultAppsTrialName),
[email protected]49098f702011-10-13 03:47:18778 1, 2);
779 }
[email protected]333b1de2011-09-12 18:28:50780
[email protected]7c1490da2011-10-11 18:53:25781 // Uninstalling one extension might have solved the problems of others.
782 // Therefore, we clear warnings of this type for all extensions.
783 std::set<ExtensionWarningSet::WarningType> warnings;
784 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
785 extension_warnings_.ClearWarnings(warnings);
786
[email protected]d6ebc9792011-04-07 18:18:33787 return true;
[email protected]c10da4b02010-03-25 14:38:32788}
789
[email protected]c3cfb012011-04-06 22:07:35790bool ExtensionService::IsExtensionEnabled(
791 const std::string& extension_id) const {
[email protected]84df8332011-12-06 18:22:46792 if (extensions_.Contains(extension_id) ||
793 terminated_extensions_.Contains(extension_id))
[email protected]36429da2011-07-11 20:25:18794 return true;
795
[email protected]84df8332011-12-06 18:22:46796 if (disabled_extensions_.Contains(extension_id))
[email protected]ad83ca242011-07-29 01:32:25797 return false;
798
799 // If the extension hasn't been loaded yet, check the prefs for it. Assume
800 // enabled unless otherwise noted.
801 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
802 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35803}
804
805bool ExtensionService::IsExternalExtensionUninstalled(
806 const std::string& extension_id) const {
807 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
808}
809
[email protected]eaa7dd182010-12-14 11:09:00810void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12811 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20812
[email protected]06f92562011-04-29 19:27:31813 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39814 return;
[email protected]0c6da502009-08-14 22:32:39815
[email protected]b3317ad2011-04-28 23:46:00816 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52817
[email protected]06f92562011-04-29 19:27:31818 const Extension* extension =
819 GetExtensionByIdInternal(extension_id, false, true, false);
820 // This can happen if sync enables an extension that is not
821 // installed yet.
822 if (!extension)
823 return;
824
[email protected]0c6da502009-08-14 22:32:39825 // Move it over to the enabled list.
[email protected]84df8332011-12-06 18:22:46826 extensions_.Insert(make_scoped_refptr(extension));
827 disabled_extensions_.Remove(extension->id());
[email protected]0c6da502009-08-14 22:32:39828
[email protected]f681c782010-11-19 11:19:39829 // Make sure any browser action contained within it is not hidden.
830 extension_prefs_->SetBrowserActionVisibility(extension, true);
831
[email protected]62d30f42009-10-01 22:36:06832 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30833
834 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39835}
836
[email protected]eaa7dd182010-12-14 11:09:00837void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12838 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20839
[email protected]b2ba9962009-12-10 20:10:15840 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31841 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52842 return;
[email protected]1784e83a2009-09-08 21:01:52843
[email protected]06f92562011-04-29 19:27:31844 const Extension* extension = GetInstalledExtension(extension_id);
845 // |extension| can be NULL if sync disables an extension that is not
846 // installed yet.
847 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:33848 return;
849
[email protected]b3317ad2011-04-28 23:46:00850 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52851
[email protected]06f92562011-04-29 19:27:31852 extension = GetExtensionByIdInternal(extension_id, true, false, true);
853 if (!extension)
854 return;
855
[email protected]1784e83a2009-09-08 21:01:52856 // Move it over to the disabled list.
[email protected]84df8332011-12-06 18:22:46857 disabled_extensions_.Insert(make_scoped_refptr(extension));
858 if (extensions_.Contains(extension->id()))
859 extensions_.Remove(extension->id());
860 else
861 terminated_extensions_.Remove(extension->id());
[email protected]1784e83a2009-09-08 21:01:52862
[email protected]814a7bf0f2011-08-13 05:30:59863 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:30864
865 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:25866
867 // Deactivating one extension might have solved the problems of others.
868 // Therefore, we clear warnings of this type for all extensions.
869 std::set<ExtensionWarningSet::WarningType> warnings;
870 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
871 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:52872}
873
[email protected]eaa7dd182010-12-14 11:09:00874void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25875 CHECK(extension);
876
[email protected]902fd7b2011-07-27 18:42:31877 // We only maintain the granted permissions prefs for extensions that can't
878 // silently increase their permissions.
879 if (extension->CanSilentlyIncreasePermissions())
880 return;
[email protected]8d888c12010-11-30 00:00:25881
[email protected]8d888c12010-11-30 00:00:25882 extension_prefs_->AddGrantedPermissions(extension->id(),
[email protected]902fd7b2011-07-27 18:42:31883 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:25884}
885
[email protected]eaa7dd182010-12-14 11:09:00886void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25887 const Extension* extension) {
888 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49889 RecordPermissionMessagesHistogram(
890 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25891 GrantPermissions(extension);
892 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
893 EnableExtension(extension->id());
894}
895
[email protected]902fd7b2011-07-27 18:42:31896void ExtensionService::UpdateActivePermissions(
897 const Extension* extension,
898 const ExtensionPermissionSet* permissions) {
899 extension_prefs()->SetActivePermissions(extension->id(), permissions);
900 extension->SetActivePermissions(permissions);
901}
902
[email protected]fe2dd7742011-04-19 22:52:49903// static
904void ExtensionService::RecordPermissionMessagesHistogram(
905 const Extension* e, const char* histogram) {
906 // Since this is called from multiple sources, and since the Histogram macros
907 // use statics, we need to manually lookup the Histogram ourselves.
908 base::Histogram* counter = base::LinearHistogram::FactoryGet(
909 histogram,
910 1,
[email protected]0d3e4a22011-06-23 19:02:52911 ExtensionPermissionMessage::kEnumBoundary,
912 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:49913 base::Histogram::kUmaTargetedHistogramFlag);
914
[email protected]0d3e4a22011-06-23 19:02:52915 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49916 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:52917 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49918 } else {
[email protected]0d3e4a22011-06-23 19:02:52919 for (ExtensionPermissionMessages::iterator it = permissions.begin();
920 it != permissions.end(); ++it)
921 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49922 }
923}
924
[email protected]eaa7dd182010-12-14 11:09:00925void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42926 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06927 // was loaded, otherwise a race can arise where a renderer that is created
928 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42929 // that the request context doesn't yet know about. The profile is responsible
930 // for ensuring its URLRequestContexts appropriately discover the loaded
931 // extension.
[email protected]c8d407e2011-04-28 21:27:17932 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06933
[email protected]c8d407e2011-04-28 21:27:17934 // Tell subsystems that use the EXTENSION_LOADED notification about the new
935 // extension.
[email protected]ad50def52011-10-19 23:17:07936 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27937 chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]6c2381d2011-10-19 02:52:53938 content::Source<Profile>(profile_),
939 content::Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:58940
[email protected]19647262011-12-16 09:57:49941 // Tell renderers about the new extension, unless it's a theme (renderers
942 // don't need to know about themes).
943 if (!extension->is_theme()) {
944 for (content::RenderProcessHost::iterator i(
945 content::RenderProcessHost::AllHostsIterator());
946 !i.IsAtEnd(); i.Advance()) {
947 content::RenderProcessHost* host = i.GetCurrentValue();
948 Profile* host_profile =
949 Profile::FromBrowserContext(host->GetBrowserContext());
950 if (host_profile->GetOriginalProfile() ==
951 profile_->GetOriginalProfile()) {
952 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
953 1, ExtensionMsg_Loaded_Params(extension));
954 host->Send(
955 new ExtensionMsg_Loaded(loaded_extensions));
956 }
[email protected]c8d407e2011-04-28 21:27:17957 }
[email protected]77a6970c2011-04-23 16:58:56958 }
959
[email protected]c8d407e2011-04-28 21:27:17960 // Tell a random-ass collection of other subsystems about the new extension.
961 // TODO(aa): What should we do with all this goop? Can it move into the
962 // relevant objects via EXTENSION_LOADED?
963
964 profile_->GetExtensionSpecialStoragePolicy()->
965 GrantRightsForExtension(extension);
966
967 UpdateActiveExtensionsInCrashReporter();
968
969 ExtensionWebUI::RegisterChromeURLOverrides(
970 profile_, extension->GetChromeURLOverrides());
971
[email protected]8e5c89a2011-06-07 18:13:33972 TemplateURLService* url_service =
973 TemplateURLServiceFactory::GetForProfile(profile_);
974 if (url_service)
975 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:17976
977 // Load the icon for omnibox-enabled extensions so it will be ready to display
978 // in the URL bar.
979 if (!extension->omnibox_keyword().empty()) {
980 omnibox_popup_icon_manager_.LoadIcon(extension);
981 omnibox_icon_manager_.LoadIcon(extension);
982 }
983
984 // If the extension has permission to load chrome://favicon/ resources we need
985 // to make sure that the FaviconSource is registered with the
986 // ChromeURLDataManager.
987 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
988 FaviconSource* favicon_source = new FaviconSource(profile_,
989 FaviconSource::FAVICON);
990 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
991 }
[email protected]5eddc3e2011-10-26 04:33:31992 // Same for chrome://thumb/ resources.
993 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
994 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
995 profile_->GetChromeURLDataManager()->AddDataSource(thumbnail_source);
996 }
[email protected]c8d407e2011-04-28 21:27:17997
998 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:58999 bool plugins_changed = false;
1000 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1001 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:141002 PluginService::GetInstance()->RefreshPlugins();
1003 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581004 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491005 ChromePluginServiceFilter* filter =
1006 ChromePluginServiceFilter::GetInstance();
1007 if (plugin.is_public) {
1008 filter->RestrictPluginToProfileAndOrigin(
1009 plugin.path, profile_, GURL());
1010 } else {
1011 filter->RestrictPluginToProfileAndOrigin(
1012 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:581013 }
1014 }
[email protected]84396dbc2011-04-14 06:33:421015
1016 bool nacl_modules_changed = false;
1017 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1018 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001019 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421020 nacl_modules_changed = true;
1021 }
1022
[email protected]ed0ba002011-05-26 16:55:131023 if (nacl_modules_changed)
1024 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421025
1026 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021027 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541028
[email protected]14da67b02011-08-02 05:49:131029#if defined(OS_CHROMEOS)
[email protected]b4d9d122011-06-17 01:58:541030 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1031 extension->input_components().begin();
1032 component != extension->input_components().end();
1033 ++component) {
[email protected]14da67b02011-08-02 05:49:131034 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
[email protected]8baf0dbb2011-11-27 22:47:021035 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
[email protected]14da67b02011-08-02 05:49:131036 profile_, extension->id(), *component);
1037 }
[email protected]9a2aebe02011-12-02 21:54:141038#if defined(USE_VIRTUAL_KEYBOARD)
[email protected]b4d9d122011-06-17 01:58:541039 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1040 !component->layouts.empty()) {
[email protected]a4215ac02011-12-04 04:11:561041 chromeos::input_method::InputMethodManager* input_method_manager =
1042 chromeos::input_method::InputMethodManager::GetInstance();
1043 const bool is_system_keyboard =
1044 extension->location() == Extension::COMPONENT;
[email protected]cbd4c042011-08-13 18:22:191045 input_method_manager->RegisterVirtualKeyboard(
1046 extension->url(),
1047 component->name, // human-readable name of the keyboard extension.
1048 component->layouts,
[email protected]a4215ac02011-12-04 04:11:561049 is_system_keyboard);
[email protected]b4d9d122011-06-17 01:58:541050 }
[email protected]14da67b02011-08-02 05:49:131051#endif
[email protected]b4d9d122011-06-17 01:58:541052 }
1053#endif
[email protected]62d30f42009-10-01 22:36:061054}
1055
[email protected]a9f39a312010-12-23 22:14:271056void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591057 const Extension* extension,
1058 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271059 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071060 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271061 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531062 content::Source<Profile>(profile_),
1063 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061064
[email protected]f3b1a082011-11-18 00:34:301065 for (content::RenderProcessHost::iterator i(
1066 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561067 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301068 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081069 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301070 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081071 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171072 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561073 }
1074
[email protected]dd163fb02011-05-04 22:22:171075 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171076 profile_->GetExtensionSpecialStoragePolicy()->
1077 RevokeRightsForExtension(extension);
1078
1079 ExtensionWebUI::UnregisterChromeURLOverrides(
1080 profile_, extension->GetChromeURLOverrides());
1081
[email protected]b777b332011-04-16 04:01:081082#if defined(OS_CHROMEOS)
1083 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171084 if (profile_->GetFileSystemContext() &&
1085 profile_->GetFileSystemContext()->path_manager() &&
1086 profile_->GetFileSystemContext()->path_manager()->external_provider()) {
1087 profile_->GetFileSystemContext()->path_manager()->external_provider()->
1088 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061089 }
[email protected]54ccafa2011-12-06 23:36:321090
1091 if (extension->input_components().size() > 0) {
1092 ExtensionInputImeEventRouter::GetInstance()->UnregisterAllImes(
1093 profile_, extension->id());
1094 }
[email protected]c8d407e2011-04-28 21:27:171095#endif
1096
1097 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581098
1099 bool plugins_changed = false;
1100 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1101 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361102 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361103 base::Bind(&ForceShutdownPlugin, plugin.path)))
[email protected]14908b72011-04-20 06:54:361104 NOTREACHED();
[email protected]f520b5b2011-11-08 02:42:141105 PluginService::GetInstance()->RefreshPlugins();
1106 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581107 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491108 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581109 }
[email protected]84396dbc2011-04-14 06:33:421110
1111 bool nacl_modules_changed = false;
1112 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1113 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001114 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421115 nacl_modules_changed = true;
1116 }
1117
[email protected]ed0ba002011-05-26 16:55:131118 if (nacl_modules_changed)
1119 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421120
1121 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021122 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061123}
1124
[email protected]eaa7dd182010-12-14 11:09:001125void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181126 const std::vector<std::string>& blacklist) {
1127 // Use this set to indicate if an extension in the blacklist has been used.
1128 std::set<std::string> blacklist_set;
1129 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1130 if (Extension::IdIsValid(blacklist[i])) {
1131 blacklist_set.insert(blacklist[i]);
1132 }
1133 }
1134 extension_prefs_->UpdateBlacklist(blacklist_set);
1135 std::vector<std::string> to_be_removed;
1136 // Loop current extensions, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461137 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]6b75ec32009-08-14 06:37:181138 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021139 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181140 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1141 to_be_removed.push_back(extension->id());
1142 }
1143 }
1144
1145 // UnloadExtension will change the extensions_ list. So, we should
1146 // call it outside the iterator loop.
1147 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591148 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181149 }
1150}
1151
[email protected]0a071a32011-02-08 00:18:241152Profile* ExtensionService::profile() {
1153 return profile_;
1154}
1155
[email protected]eaa7dd182010-12-14 11:09:001156ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451157 return extension_prefs_;
1158}
1159
[email protected]0d9a2202011-11-09 13:48:411160extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381161 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201162}
1163
[email protected]b790b072011-05-20 09:46:441164ExtensionContentSettingsStore*
1165 ExtensionService::GetExtensionContentSettingsStore() {
1166 return extension_prefs()->content_settings_store();
1167}
1168
[email protected]25ae0152011-11-18 14:40:021169bool ExtensionService::is_ready() {
1170 return ready_;
1171}
1172
[email protected]2859946f2011-04-04 18:18:061173ExtensionUpdater* ExtensionService::updater() {
1174 return updater_.get();
1175}
1176
[email protected]eaa7dd182010-12-14 11:09:001177void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251178 std::vector<std::string> to_be_removed;
1179 // Loop through extensions list, unload installed extensions.
[email protected]84df8332011-12-06 18:22:461180 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]aa96d3a2010-08-21 08:45:251181 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021182 const Extension* extension = (*iter);
[email protected]05aad2da2011-10-28 10:12:371183 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id(),
1184 extension->location())) {
[email protected]aa96d3a2010-08-21 08:45:251185 to_be_removed.push_back(extension->id());
[email protected]05aad2da2011-10-28 10:12:371186 }
[email protected]aa96d3a2010-08-21 08:45:251187 }
1188
1189 // UnloadExtension will change the extensions_ list. So, we should
1190 // call it outside the iterator loop.
1191 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591192 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251193}
1194
[email protected]31206602011-04-13 23:07:321195void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351196 if (updater()) {
[email protected]31206602011-04-13 23:07:321197 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351198 } else {
[email protected]31206602011-04-13 23:07:321199 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351200 }
1201}
1202
[email protected]3bdba0d2011-08-23 07:17:301203namespace {
1204 bool IsSyncableNone(const Extension& extension) { return false; }
[email protected]406b5a92011-11-08 11:58:261205} // namespace
[email protected]3bdba0d2011-08-23 07:17:301206
1207ExtensionService::SyncBundle::SyncBundle()
1208 : filter(IsSyncableNone),
1209 sync_processor(NULL) {
[email protected]b05fb9ff2011-04-23 00:07:561210}
1211
[email protected]3bdba0d2011-08-23 07:17:301212ExtensionService::SyncBundle::~SyncBundle() {
1213}
1214
1215bool ExtensionService::SyncBundle::HasExtensionId(const std::string& id) const {
1216 return synced_extensions.find(id) != synced_extensions.end();
1217}
1218
1219bool ExtensionService::SyncBundle::HasPendingExtensionId(const std::string& id)
1220 const {
1221 return pending_sync_data.find(id) != pending_sync_data.end();
1222}
1223
1224void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
1225 SyncBundle* sync_bundle = GetSyncBundleForExtension(extension);
1226 if (sync_bundle) {
[email protected]aa7599d2011-10-28 07:24:321227 ExtensionSyncData extension_sync_data(
1228 extension,
1229 IsExtensionEnabled(extension.id()),
1230 IsIncognitoEnabled(extension.id()),
[email protected]b2689a902011-12-01 00:41:091231 extension_prefs_->GetAppNotificationClientId(extension.id()),
[email protected]aa7599d2011-10-28 07:24:321232 extension_prefs_->IsAppNotificationDisabled(extension.id()));
[email protected]3bdba0d2011-08-23 07:17:301233
1234 SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
1235 sync_bundle->HasExtensionId(extension.id()) ?
1236 SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD));
1237 sync_bundle->sync_processor->ProcessSyncChanges(
1238 FROM_HERE, sync_change_list);
1239 sync_bundle->synced_extensions.insert(extension.id());
1240 sync_bundle->pending_sync_data.erase(extension.id());
[email protected]b05fb9ff2011-04-23 00:07:561241 }
[email protected]3bdba0d2011-08-23 07:17:301242}
1243
1244ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForExtension(
1245 const Extension& extension) {
1246 if (app_sync_bundle_.filter(extension))
1247 return &app_sync_bundle_;
1248 else if (extension_sync_bundle_.filter(extension))
1249 return &extension_sync_bundle_;
1250 else
1251 return NULL;
1252}
1253
1254ExtensionService::SyncBundle*
1255 ExtensionService::GetSyncBundleForExtensionSyncData(
1256 const ExtensionSyncData& extension_sync_data) {
1257 switch (extension_sync_data.type()) {
1258 case Extension::SYNC_TYPE_APP:
1259 return &app_sync_bundle_;
1260 case Extension::SYNC_TYPE_EXTENSION:
1261 return &extension_sync_bundle_;
1262 default:
1263 NOTREACHED();
1264 return NULL;
1265 }
1266}
1267
1268#define GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY() \
1269 do { \
1270 switch (type) { \
1271 case syncable::APPS: \
1272 return &app_sync_bundle_; \
1273 case syncable::EXTENSIONS: \
1274 return &extension_sync_bundle_; \
1275 default: \
1276 NOTREACHED(); \
1277 return NULL; \
1278 } \
1279 } while (0)
1280
1281const ExtensionService::SyncBundle*
1282 ExtensionService::GetSyncBundleForModelTypeConst(
1283 syncable::ModelType type) const {
1284 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1285}
1286
1287ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForModelType(
1288 syncable::ModelType type) {
1289 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1290}
1291
1292#undef GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY
1293
1294SyncError ExtensionService::MergeDataAndStartSyncing(
1295 syncable::ModelType type,
1296 const SyncDataList& initial_sync_data,
1297 SyncChangeProcessor* sync_processor) {
1298 CHECK(sync_processor);
1299
1300 SyncBundle* bundle = NULL;
1301
1302 switch (type) {
1303 case syncable::EXTENSIONS:
1304 bundle = &extension_sync_bundle_;
1305 bundle->filter = IsSyncableExtension;
1306 break;
1307
1308 case syncable::APPS:
1309 bundle = &app_sync_bundle_;
1310 bundle->filter = IsSyncableApp;
1311 break;
1312
1313 default:
1314 LOG(FATAL) << "Got " << type << " ModelType";
1315 }
1316
1317 bundle->sync_processor = sync_processor;
1318
[email protected]b1417ab2011-12-15 17:09:111319 // Process extensions from sync.
[email protected]3bdba0d2011-08-23 07:17:301320 for (SyncDataList::const_iterator i = initial_sync_data.begin();
1321 i != initial_sync_data.end();
1322 ++i) {
1323 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1324 bundle->synced_extensions.insert(extension_sync_data.id());
1325 ProcessExtensionSyncData(extension_sync_data, *bundle);
1326 }
1327
[email protected]b1417ab2011-12-15 17:09:111328 // Process local extensions.
1329 // TODO(yoz): Determine whether pending extensions should be considered too.
1330 // See crbug.com/104399.
[email protected]3bdba0d2011-08-23 07:17:301331 SyncDataList sync_data_list = GetAllSyncData(type);
1332 SyncChangeList sync_change_list;
1333 for (SyncDataList::const_iterator i = sync_data_list.begin();
1334 i != sync_data_list.end();
1335 ++i) {
[email protected]b1417ab2011-12-15 17:09:111336 if (bundle->HasExtensionId(i->GetTag())) {
[email protected]3bdba0d2011-08-23 07:17:301337 sync_change_list.push_back(SyncChange(SyncChange::ACTION_UPDATE, *i));
[email protected]b1417ab2011-12-15 17:09:111338 } else {
1339 bundle->synced_extensions.insert(i->GetTag());
[email protected]3bdba0d2011-08-23 07:17:301340 sync_change_list.push_back(SyncChange(SyncChange::ACTION_ADD, *i));
[email protected]b1417ab2011-12-15 17:09:111341 }
[email protected]3bdba0d2011-08-23 07:17:301342 }
1343 bundle->sync_processor->ProcessSyncChanges(FROM_HERE, sync_change_list);
1344
1345 return SyncError();
1346}
1347
1348void ExtensionService::StopSyncing(syncable::ModelType type) {
1349 SyncBundle* bundle = GetSyncBundleForModelType(type);
1350 CHECK(bundle);
1351 // This is the simplest way to clear out the bundle.
1352 *bundle = SyncBundle();
1353}
1354
1355SyncDataList ExtensionService::GetAllSyncData(syncable::ModelType type) const {
1356 const SyncBundle* bundle = GetSyncBundleForModelTypeConst(type);
1357 CHECK(bundle);
1358 std::vector<ExtensionSyncData> extension_sync_data = GetSyncDataList(*bundle);
1359 SyncDataList result(extension_sync_data.size());
1360 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
1361 result[i] = extension_sync_data[i].GetSyncData();
1362 }
1363 return result;
1364}
1365
1366SyncError ExtensionService::ProcessSyncChanges(
1367 const tracked_objects::Location& from_here,
1368 const SyncChangeList& change_list) {
1369 for (SyncChangeList::const_iterator i = change_list.begin();
1370 i != change_list.end();
1371 ++i) {
1372 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1373 SyncBundle* bundle = GetSyncBundleForExtensionSyncData(extension_sync_data);
1374 CHECK(bundle);
1375
1376 if (extension_sync_data.uninstalled())
1377 bundle->synced_extensions.erase(extension_sync_data.id());
1378 else
1379 bundle->synced_extensions.insert(extension_sync_data.id());
1380 ProcessExtensionSyncData(extension_sync_data, *bundle);
1381 }
1382
1383 return SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561384}
1385
1386void ExtensionService::GetSyncDataListHelper(
[email protected]84df8332011-12-06 18:22:461387 const ExtensionSet& extensions,
[email protected]3bdba0d2011-08-23 07:17:301388 const SyncBundle& bundle,
[email protected]b05fb9ff2011-04-23 00:07:561389 std::vector<ExtensionSyncData>* sync_data_list) const {
[email protected]84df8332011-12-06 18:22:461390 for (ExtensionSet::const_iterator it = extensions.begin();
[email protected]b05fb9ff2011-04-23 00:07:561391 it != extensions.end(); ++it) {
1392 const Extension& extension = **it;
[email protected]3bdba0d2011-08-23 07:17:301393 if (bundle.filter(extension) &&
1394 // If we have pending extension data for this extension, then this
1395 // version is out of date. We'll sync back the version we got from
1396 // sync.
1397 !bundle.HasPendingExtensionId(extension.id())) {
[email protected]aa7599d2011-10-28 07:24:321398 sync_data_list->push_back(ExtensionSyncData(
1399 extension,
1400 IsExtensionEnabled(extension.id()),
1401 IsIncognitoEnabled(extension.id()),
[email protected]b2689a902011-12-01 00:41:091402 extension_prefs_->GetAppNotificationClientId(extension.id()),
[email protected]aa7599d2011-10-28 07:24:321403 extension_prefs_->IsAppNotificationDisabled(extension.id())));
[email protected]b05fb9ff2011-04-23 00:07:561404 }
1405 }
1406}
1407
1408std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
[email protected]3bdba0d2011-08-23 07:17:301409 const SyncBundle& bundle) const {
1410 std::vector<ExtensionSyncData> extension_sync_list;
1411 GetSyncDataListHelper(extensions_, bundle, &extension_sync_list);
1412 GetSyncDataListHelper(disabled_extensions_, bundle, &extension_sync_list);
1413 GetSyncDataListHelper(terminated_extensions_, bundle, &extension_sync_list);
1414
1415 for (std::map<std::string, ExtensionSyncData>::const_iterator i =
1416 bundle.pending_sync_data.begin();
1417 i != bundle.pending_sync_data.end();
1418 ++i) {
1419 extension_sync_list.push_back(i->second);
1420 }
1421
1422 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561423}
1424
[email protected]3bdba0d2011-08-23 07:17:301425void ExtensionService::ProcessExtensionSyncData(
[email protected]90310d92011-04-17 07:35:041426 const ExtensionSyncData& extension_sync_data,
[email protected]3bdba0d2011-08-23 07:17:301427 SyncBundle& bundle) {
1428 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061429 const Extension* extension = GetInstalledExtension(id);
1430
1431 // TODO(bolms): we should really handle this better. The particularly bad
1432 // case is where an app becomes an extension or vice versa, and we end up with
1433 // a zombie extension that won't go away.
1434 if (extension && !bundle.filter(*extension))
1435 return;
[email protected]90310d92011-04-17 07:35:041436
1437 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301438 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041439 std::string error;
1440 if (!UninstallExtensionHelper(this, id)) {
1441 LOG(WARNING) << "Could not uninstall extension " << id
1442 << " for sync";
1443 }
1444 return;
1445 }
1446
[email protected]06f92562011-04-29 19:27:311447 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301448 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311449 EnableExtension(id);
1450 } else {
1451 DisableExtension(id);
1452 }
[email protected]81b14cc2011-04-29 00:39:371453
[email protected]406b5a92011-11-08 11:58:261454 // We need to cache some version information here because setting the
1455 // incognito flag invalidates the |extension| pointer (it reloads the
1456 // extension).
1457 bool extension_installed = extension != NULL;
1458 int result = extension ?
1459 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1460 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1461 extension = NULL; // No longer safe to use.
1462
1463 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311464 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041465 if (result < 0) {
1466 // Extension is outdated.
[email protected]3bdba0d2011-08-23 07:17:301467 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]90310d92011-04-17 07:35:041468 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041469 }
[email protected]b10a4abe2011-12-10 02:31:231470 if (extension_sync_data.type() == Extension::SYNC_TYPE_APP &&
1471 extension_sync_data.notifications_disabled() !=
[email protected]b020dc12011-12-06 04:35:421472 extension_prefs_->IsAppNotificationDisabled(id)) {
1473 extension_prefs_->SetAppNotificationDisabled(
1474 id, extension_sync_data.notifications_disabled());
1475 }
[email protected]06f92562011-04-29 19:27:311476 } else {
[email protected]06f92562011-04-29 19:27:311477 // TODO(akalin): Replace silent update with a list of enabled
1478 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331479 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311480 if (!pending_extension_manager()->AddFromSync(
1481 id,
[email protected]3bdba0d2011-08-23 07:17:301482 extension_sync_data.update_url(),
1483 bundle.filter,
[email protected]6cc7dbae2011-04-29 21:18:331484 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311485 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301486 // This means that the extension is already pending installation, with a
1487 // non-INTERNAL location. Add to pending_sync_data, even though it will
1488 // never be removed (we'll never install a syncable version of the
1489 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311490 }
[email protected]3bdba0d2011-08-23 07:17:301491 // Track pending extensions so that we can return them in GetAllSyncData().
1492 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]06f92562011-04-29 19:27:311493 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041494 }
[email protected]90310d92011-04-17 07:35:041495}
1496
[email protected]c3cfb012011-04-06 22:07:351497bool ExtensionService::IsIncognitoEnabled(
1498 const std::string& extension_id) const {
1499 // If this is an existing component extension we always allow it to
1500 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371501 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351502 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411503 return true;
1504
1505 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351506 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501507}
[email protected]55a35692010-02-11 23:25:211508
[email protected]c3cfb012011-04-06 22:07:351509void ExtensionService::SetIsIncognitoEnabled(
1510 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371511 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351512 if (extension && extension->location() == Extension::COMPONENT) {
1513 // This shouldn't be called for component extensions.
1514 NOTREACHED();
1515 return;
1516 }
1517
[email protected]8c6c8c22011-03-09 12:52:341518 // Broadcast unloaded and loaded events to update browser state. Only bother
1519 // if the value changed and the extension is actually enabled, since there is
1520 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351521 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341522 if (enabled == old_enabled)
1523 return;
1524
[email protected]c3cfb012011-04-06 22:07:351525 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371526
[email protected]ffbf3f12011-12-09 22:10:331527 bool extension_is_enabled = extensions_.Contains(extension_id);
[email protected]bd9892eb2011-11-11 19:41:341528
1529 // When we reload the extension the ID may be invalidated if we've passed it
1530 // by const ref everywhere. Make a copy to be safe.
1531 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261532 if (extension_is_enabled)
[email protected]ffbf3f12011-12-09 22:10:331533 ReloadExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301534
[email protected]406b5a92011-11-08 11:58:261535 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341536 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301537 if (extension)
1538 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211539}
1540
[email protected]aa7599d2011-10-28 07:24:321541void ExtensionService::SetAppNotificationSetupDone(
1542 const std::string& extension_id,
[email protected]b2689a902011-12-01 00:41:091543 const std::string& oauth_client_id) {
[email protected]aa7599d2011-10-28 07:24:321544 const Extension* extension = GetInstalledExtension(extension_id);
1545 // This method is called when the user sets up app notifications.
1546 // So it is not expected to be called until the extension is installed.
1547 if (!extension) {
1548 NOTREACHED();
1549 return;
1550 }
[email protected]b2689a902011-12-01 00:41:091551 extension_prefs_->SetAppNotificationClientId(extension_id, oauth_client_id);
[email protected]aa7599d2011-10-28 07:24:321552 SyncExtensionChangeIfNeeded(*extension);
1553}
1554
1555void ExtensionService::SetAppNotificationDisabled(
1556 const std::string& extension_id,
1557 bool value) {
1558 const Extension* extension = GetInstalledExtension(extension_id);
1559 // This method is called when the user enables/disables app notifications.
1560 // So it is not expected to be called until the extension is installed.
1561 if (!extension) {
1562 NOTREACHED();
1563 return;
1564 }
[email protected]97e856b12011-12-14 00:21:131565 if (value)
1566 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsDisabled", 1);
1567 else
1568 UMA_HISTOGRAM_COUNTS("Apps.SetAppNotificationsEnabled", 1);
[email protected]aa7599d2011-10-28 07:24:321569 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1570 SyncExtensionChangeIfNeeded(*extension);
1571}
1572
[email protected]eaa7dd182010-12-14 11:09:001573bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051574 // We allow the extension to see events and data from another profile iff it
1575 // uses "spanning" behavior and it has incognito access. "split" mode
1576 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221577 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351578 return IsIncognitoEnabled(extension->id()) &&
1579 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051580}
1581
[email protected]5ef7b002011-05-12 23:16:201582bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1583 if (extension->is_hosted_app())
1584 return true;
1585 // Packaged apps and regular extensions need to be enabled specifically for
1586 // incognito (and split mode should be set).
1587 return extension->incognito_split_mode() &&
1588 IsIncognitoEnabled(extension->id());
1589}
1590
[email protected]eaa7dd182010-12-14 11:09:001591bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081592 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441593 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081594 extension_prefs_->AllowFileAccess(extension->id()));
1595}
1596
[email protected]eaa7dd182010-12-14 11:09:001597void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481598 bool allow) {
1599 // Reload to update browser state. Only bother if the value changed and the
1600 // extension is actually enabled, since there is no UI otherwise.
1601 bool old_allow = AllowFileAccess(extension);
1602 if (allow == old_allow)
1603 return;
1604
[email protected]05c82182010-06-24 17:49:081605 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481606
[email protected]84df8332011-12-06 18:22:461607 bool extension_is_enabled = extensions_.Contains(extension->id());
[email protected]3aff9ad2011-04-01 20:26:481608 if (extension_is_enabled)
1609 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081610}
1611
[email protected]eaa7dd182010-12-14 11:09:001612bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391613 return extension_prefs_->GetBrowserActionVisibility(extension);
1614}
1615
[email protected]eaa7dd182010-12-14 11:09:001616void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141617 bool visible) {
[email protected]f681c782010-11-19 11:19:391618 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1619}
1620
[email protected]8e4560b62011-01-14 10:09:141621// Some extensions will autoupdate themselves externally from Chrome. These
1622// are typically part of some larger client application package. To support
1623// these, the extension will register its location in the the preferences file
1624// (and also, on Windows, in the registry) and this code will periodically
1625// check that location for a .crx file, which it will then install locally if
1626// a new version is available.
1627// Errors are reported through ExtensionErrorReporter. Succcess is not
1628// reported.
[email protected]eaa7dd182010-12-14 11:09:001629void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121630 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141631
1632 // Note that this installation is intentionally silent (since it didn't
1633 // go through the front-end). Extensions that are registered in this
1634 // way are effectively considered 'pre-bundled', and so implicitly
1635 // trusted. In general, if something has HKLM or filesystem access,
1636 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121637
1638 // If any external extension records give a URL, a provider will set
1639 // this to true. Used by OnExternalProviderReady() to see if we need
1640 // to start an update check to fetch a new external extension.
1641 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141642
1643 // Ask each external extension provider to give us a call back for each
1644 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1645 ProviderCollection::const_iterator i;
1646 for (i = external_extension_providers_.begin();
1647 i != external_extension_providers_.end(); ++i) {
1648 ExternalExtensionProviderInterface* provider = i->get();
1649 provider->VisitRegisteredExtension();
1650 }
1651
[email protected]50067e52011-10-20 23:17:071652 // Do any required work that we would have done after completion of all
1653 // providers.
1654 if (external_extension_providers_.empty()) {
1655 OnAllExternalProvidersReady();
1656 }
[email protected]9f1087e2009-06-15 17:29:321657}
1658
[email protected]50067e52011-10-20 23:17:071659void ExtensionService::OnExternalProviderReady(
1660 const ExternalExtensionProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121661 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071662 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121663
1664 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141665 // if all of them are finished. So we check them first.
1666 ProviderCollection::const_iterator i;
1667 for (i = external_extension_providers_.begin();
1668 i != external_extension_providers_.end(); ++i) {
1669 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121670 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141671 return;
[email protected]8e4560b62011-01-14 10:09:141672 }
1673
[email protected]50067e52011-10-20 23:17:071674 OnAllExternalProvidersReady();
1675}
1676
1677void ExtensionService::OnAllExternalProvidersReady() {
1678 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1679
1680 // Install any pending extensions.
[email protected]a29a517a2011-01-21 21:11:121681 if (external_extension_url_added_ && updater()) {
1682 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141683 updater()->CheckNow();
1684 }
1685
1686 // Uninstall all the unclaimed extensions.
1687 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1688 extension_prefs_->GetInstalledExtensionsInfo());
1689 for (size_t i = 0; i < extensions_info->size(); ++i) {
1690 ExtensionInfo* info = extensions_info->at(i).get();
1691 if (Extension::IsExternalLocation(info->extension_location))
1692 CheckExternalUninstall(info->extension_id);
1693 }
[email protected]e5af875f2011-10-10 21:09:141694 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141695}
1696
1697void ExtensionService::IdentifyAlertableExtensions() {
1698 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1699
[email protected]08b83362011-10-19 05:23:171700 if (!CommandLine::ForCurrentProcess()->HasSwitch(
1701 switches::kEnableExtensionAlerts)) {
1702 return; // TODO(miket): enable unconditionally when done.
1703 }
1704
[email protected]e5af875f2011-10-10 21:09:141705 // Build up the lists of extensions that require acknowledgment.
1706 // If this is the first time, grandfather extensions that would have
1707 // caused notification.
1708 scoped_ptr<ExtensionGlobalError> global_error(
[email protected]d8c8f25f2011-11-02 18:18:011709 new ExtensionGlobalError(AsWeakPtr()));
[email protected]e5af875f2011-10-10 21:09:141710 bool needs_alert = false;
[email protected]84df8332011-12-06 18:22:461711 for (ExtensionSet::const_iterator iter = extensions_.begin();
[email protected]e5af875f2011-10-10 21:09:141712 iter != extensions_.end(); ++iter) {
1713 const Extension* e = *iter;
[email protected]e5af875f2011-10-10 21:09:141714 if (Extension::IsExternalLocation(e->location())) {
1715 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
1716 global_error->AddExternalExtension(e->id());
1717 needs_alert = true;
1718 }
1719 }
1720 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
1721 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
1722 global_error->AddBlacklistedExtension(e->id());
1723 needs_alert = true;
1724 }
1725 }
1726 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
1727 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
1728 global_error->AddOrphanedExtension(e->id());
1729 needs_alert = true;
1730 }
1731 }
1732 }
1733
1734 if (needs_alert) {
1735 if (extension_prefs_->SetAlertSystemFirstRun()) {
1736 global_error->set_accept_callback(
1737 base::Bind(&ExtensionService::HandleExtensionAlertAccept,
1738 base::Unretained(this)));
1739 global_error->set_cancel_callback(
1740 base::Bind(&ExtensionService::HandleExtensionAlertDetails,
1741 base::Unretained(this)));
[email protected]62433d32011-10-12 22:33:121742 ShowExtensionAlert(global_error.release());
[email protected]e5af875f2011-10-10 21:09:141743 } else {
1744 // First run. Just acknowledge all the extensions, silently, by
1745 // shortcutting the display of the UI and going straight to the
1746 // callback for pressing the Accept button.
[email protected]62433d32011-10-12 22:33:121747 HandleExtensionAlertAccept(*global_error.get(), NULL);
[email protected]e5af875f2011-10-10 21:09:141748 }
1749 }
1750}
1751
[email protected]62433d32011-10-12 22:33:121752void ExtensionService::ShowExtensionAlert(ExtensionGlobalError* global_error) {
1753 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1754 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_);
1755 if (browser) {
1756 global_error->ShowBubbleView(browser);
1757 }
1758}
1759
[email protected]e5af875f2011-10-10 21:09:141760void ExtensionService::HandleExtensionAlertAccept(
[email protected]62433d32011-10-12 22:33:121761 const ExtensionGlobalError& global_error, Browser* browser) {
[email protected]e5af875f2011-10-10 21:09:141762 const ExtensionIdSet *extension_ids =
1763 global_error.get_external_extension_ids();
1764 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1765 iter != extension_ids->end(); ++iter) {
[email protected]47fc70c2011-12-06 07:29:511766 AcknowledgeExternalExtension(*iter);
[email protected]e5af875f2011-10-10 21:09:141767 }
1768 extension_ids = global_error.get_blacklisted_extension_ids();
1769 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1770 iter != extension_ids->end(); ++iter) {
1771 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1772 }
1773 extension_ids = global_error.get_orphaned_extension_ids();
1774 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1775 iter != extension_ids->end(); ++iter) {
1776 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
1777 }
1778}
1779
[email protected]47fc70c2011-12-06 07:29:511780void ExtensionService::AcknowledgeExternalExtension(const std::string& id) {
1781 extension_prefs_->AcknowledgeExternalExtension(id);
1782}
1783
[email protected]e5af875f2011-10-10 21:09:141784void ExtensionService::HandleExtensionAlertDetails(
[email protected]62433d32011-10-12 22:33:121785 const ExtensionGlobalError& global_error, Browser* browser) {
1786 if (browser) {
1787 browser->ShowExtensionsTab();
1788 }
[email protected]6c751e72010-11-23 10:11:101789}
1790
[email protected]a9f39a312010-12-23 22:14:271791void ExtensionService::UnloadExtension(
1792 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591793 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091794 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021795 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471796 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251797
[email protected]fa0c96732010-11-17 00:14:231798 // This method can be called via PostTask, so the extension may have been
1799 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171800 if (!extension) {
1801 // In case the extension may have crashed/uninstalled. Allow the profile to
1802 // clean up its RequestContexts.
1803 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231804 return;
[email protected]dd163fb02011-05-04 22:22:171805 }
[email protected]0c6da502009-08-14 22:32:391806
[email protected]1eb175082010-02-10 09:26:161807 // Keep information about the extension so that we can reload it later
1808 // even if it's not permanently installed.
1809 unloaded_extension_paths_[extension->id()] = extension->path();
1810
[email protected]f17dbd42010-08-16 23:21:101811 // Clean up if the extension is meant to be enabled after a reload.
1812 disabled_extension_paths_.erase(extension->id());
1813
[email protected]d7e9a862010-11-03 21:57:491814 // Clean up runtime data.
1815 extension_runtime_data_.erase(extension_id);
1816
[email protected]84df8332011-12-06 18:22:461817if (disabled_extensions_.Contains(extension->id())) {
[email protected]a9f39a312010-12-23 22:14:271818 UnloadedExtensionInfo details(extension, reason);
1819 details.already_disabled = true;
[email protected]84df8332011-12-06 18:22:461820 disabled_extensions_.Remove(extension->id());
[email protected]ad50def52011-10-19 23:17:071821 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271822 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531823 content::Source<Profile>(profile_),
1824 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171825 // Make sure the profile cleans up its RequestContexts when an already
1826 // disabled extension is unloaded (since they are also tracking the disabled
1827 // extensions).
1828 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391829 return;
1830 }
1831
[email protected]84df8332011-12-06 18:22:461832// Remove the extension from our list.
1833 extensions_.Remove(extension->id());
[email protected]631cf822009-05-15 07:01:251834
[email protected]a9f39a312010-12-23 22:14:271835 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251836}
1837
[email protected]eaa7dd182010-12-14 11:09:001838void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171839 profile_->GetExtensionSpecialStoragePolicy()->
1840 RevokeRightsForAllExtensions();
1841
[email protected]84df8332011-12-06 18:22:461842 extensions_.Clear();
1843 disabled_extensions_.Clear();
1844 terminated_extensions_.Clear();
[email protected]d7e9a862010-11-03 21:57:491845 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321846
[email protected]c6e4a3412009-06-24 15:45:291847 // TODO(erikkay) should there be a notification for this? We can't use
1848 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1849 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321850}
1851
[email protected]eaa7dd182010-12-14 11:09:001852void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321853 UnloadAllExtensions();
[email protected]d8c8f25f2011-11-02 18:18:011854 component_loader_->LoadAll();
1855 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321856}
1857
[email protected]eaa7dd182010-12-14 11:09:001858void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501859 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391860 return;
1861
[email protected]ca3dbf52010-05-19 22:27:061862 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1863 extension_prefs_->GetInstalledExtensionsInfo());
1864
1865 std::map<std::string, FilePath> extension_paths;
1866 for (size_t i = 0; i < info->size(); ++i)
1867 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1868
[email protected]14908b72011-04-20 06:54:361869 if (!BrowserThread::PostTask(
1870 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361871 base::Bind(
[email protected]14908b72011-04-20 06:54:361872 &extension_file_util::GarbageCollectExtensions,
1873 install_directory_,
1874 extension_paths)))
1875 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341876
1877 // Also garbage-collect themes. We check |profile_| to be
1878 // defensive; in the future, we may call GarbageCollectExtensions()
1879 // from somewhere other than Init() (e.g., in a timer).
1880 if (profile_) {
[email protected]18280372011-03-22 18:05:221881 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341882 }
[email protected]3cf4f0992009-02-03 23:00:301883}
1884
[email protected]eaa7dd182010-12-14 11:09:001885void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171886 if (updater_.get()) {
1887 updater_->Start();
1888 }
[email protected]fa6a9102010-11-22 15:38:501889
1890 ready_ = true;
[email protected]ad50def52011-10-19 23:17:071891 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271892 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:531893 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:071894 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:511895}
1896
[email protected]49cd1572011-02-08 21:38:451897void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461898 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021899 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321900
[email protected]c8d407e2011-04-28 21:27:171901 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1902 // default extension state to DISABLED when the --disable-extensions flag
1903 // is set (http://crbug.com/29067).
1904 if (!extensions_enabled() &&
1905 !extension->is_theme() &&
1906 extension->location() != Extension::COMPONENT &&
1907 !Extension::IsExternalLocation(extension->location()))
1908 return;
1909
1910 SetBeingUpgraded(extension, false);
1911
[email protected]1eb175082010-02-10 09:26:161912 // The extension is now loaded, remove its data from unloaded extension map.
1913 unloaded_extension_paths_.erase(extension->id());
1914
[email protected]bb7f40952011-01-13 00:21:201915 // If a terminated extension is loaded, remove it from the terminated list.
1916 UntrackTerminatedExtension(extension->id());
1917
[email protected]f17dbd42010-08-16 23:21:101918 // If the extension was disabled for a reload, then enable it.
1919 if (disabled_extension_paths_.erase(extension->id()) > 0)
1920 EnableExtension(extension->id());
1921
[email protected]d728e002010-12-08 04:46:231922 // Check if the extension's privileges have changed and disable the
1923 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:311924 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:391925
[email protected]ad83ca242011-07-29 01:32:251926 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:181927 if (disabled) {
[email protected]84df8332011-12-06 18:22:461928 disabled_extensions_.Insert(scoped_extension);
1929 // TODO(aa): This seems dodgy. AddExtension() could get called with a
1930 // disabled extension for other reasons other than that an update was
1931 // disabled, e.g. as in ExtensionManagementTest.InstallRequiresConfirm.
[email protected]ad50def52011-10-19 23:17:071932 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271933 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]6c2381d2011-10-19 02:52:531934 content::Source<Profile>(profile_),
1935 content::Details<const Extension>(extension));
[email protected]3bdba0d2011-08-23 07:17:301936 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:171937 return;
[email protected]e72e8eb82009-06-18 17:21:511938 }
[email protected]aab98a52009-12-02 03:22:351939
[email protected]84df8332011-12-06 18:22:461940 extensions_.Insert(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:301941 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:171942 NotifyExtensionLoaded(extension);
[email protected]d1282c22011-10-18 02:00:391943 IdentifyAlertableExtensions();
[email protected]aab98a52009-12-02 03:22:351944}
1945
[email protected]902fd7b2011-07-27 18:42:311946void ExtensionService::InitializePermissions(const Extension* extension) {
1947 // If the extension has used the optional permissions API, it will have a
1948 // custom set of active permissions defined in the extension prefs. Here,
1949 // we update the extension's active permissions based on the prefs.
1950 scoped_refptr<ExtensionPermissionSet> active_permissions =
1951 extension_prefs()->GetActivePermissions(extension->id());
1952
1953 if (active_permissions.get()) {
1954 // We restrict the active permissions to be within the bounds defined in the
1955 // extension's manifest.
1956 // a) active permissions must be a subset of optional + default permissions
1957 // b) active permissions must contains all default permissions
1958 scoped_refptr<ExtensionPermissionSet> total_permissions =
1959 ExtensionPermissionSet::CreateUnion(
1960 extension->required_permission_set(),
1961 extension->optional_permission_set());
1962
1963 // Make sure the active permissions contain no more than optional + default.
1964 scoped_refptr<ExtensionPermissionSet> adjusted_active =
1965 ExtensionPermissionSet::CreateIntersection(
1966 total_permissions.get(), active_permissions.get());
1967
1968 // Make sure the active permissions contain the default permissions.
1969 adjusted_active = ExtensionPermissionSet::CreateUnion(
1970 extension->required_permission_set(), adjusted_active.get());
1971
1972 UpdateActivePermissions(extension, adjusted_active);
1973 }
1974
[email protected]8d888c12010-11-30 00:00:251975 // We keep track of all permissions the user has granted each extension.
1976 // This allows extensions to gracefully support backwards compatibility
1977 // by including unknown permissions in their manifests. When the user
1978 // installs the extension, only the recognized permissions are recorded.
1979 // When the unknown permissions become recognized (e.g., through browser
1980 // upgrade), we can prompt the user to accept these new permissions.
1981 // Extensions can also silently upgrade to less permissions, and then
1982 // silently upgrade to a version that adds these permissions back.
1983 //
1984 // For example, pretend that Chrome 10 includes a permission "omnibox"
1985 // for an API that adds suggestions to the omnibox. An extension can
1986 // maintain backwards compatibility while still having "omnibox" in the
1987 // manifest. If a user installs the extension on Chrome 9, the browser
1988 // will record the permissions it recognized, not including "omnibox."
1989 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1990 // will disable the extension and prompt the user to approve the increase
1991 // in privileges. The extension could then release a new version that
1992 // removes the "omnibox" permission. When the user upgrades, Chrome will
1993 // still remember that "omnibox" had been granted, so that if the
1994 // extension once again includes "omnibox" in an upgrade, the extension
1995 // can upgrade without requiring this user's approval.
1996 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:471997 true, true, false);
[email protected]8d888c12010-11-30 00:00:251998 bool is_extension_upgrade = old != NULL;
1999 bool is_privilege_increase = false;
2000
[email protected]902fd7b2011-07-27 18:42:312001 // We only need to compare the granted permissions to the current permissions
2002 // if the extension is not allowed to silently increase its permissions.
2003 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:252004 // Add all the recognized permissions if the granted permissions list
2005 // hasn't been initialized yet.
[email protected]902fd7b2011-07-27 18:42:312006 scoped_refptr<ExtensionPermissionSet> granted_permissions =
2007 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:522008 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:252009
2010 // Here, we check if an extension's privileges have increased in a manner
2011 // that requires the user's approval. This could occur because the browser
2012 // upgraded and recognized additional privileges, or an extension upgrades
2013 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:522014 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:312015 granted_permissions->HasLessPrivilegesThan(
2016 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:252017 }
2018
2019 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:252020 // Other than for unpacked extensions, CrxInstaller should have guaranteed
2021 // that we aren't downgrading.
2022 if (extension->location() != Extension::LOAD)
2023 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:252024
2025 // Extensions get upgraded if the privileges are allowed to increase or
2026 // the privileges haven't increased.
2027 if (!is_privilege_increase) {
2028 SetBeingUpgraded(old, true);
2029 SetBeingUpgraded(extension, true);
2030 }
2031
2032 // To upgrade an extension in place, unload the old one and
2033 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592034 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252035 old = NULL;
2036 }
2037
2038 // Extension has changed permissions significantly. Disable it. A
2039 // notification should be sent by the caller.
2040 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492041 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2042 RecordPermissionMessagesHistogram(
2043 extension, "Extensions.Permissions_AutoDisable");
2044 }
[email protected]b3317ad2011-04-28 23:46:002045 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252046 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
2047 }
2048}
2049
[email protected]eaa7dd182010-12-14 11:09:002050void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392051 std::set<std::string> extension_ids;
[email protected]84df8332011-12-06 18:22:462052 for (ExtensionSet::const_iterator iter = extensions_.begin();
2053 iter != extensions_.end(); ++iter) {
2054 const Extension* extension = *iter;
2055 if (!extension->is_theme() && extension->location() != Extension::COMPONENT)
2056 extension_ids.insert(extension->id());
[email protected]aab98a52009-12-02 03:22:352057 }
2058
2059 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252060}
2061
[email protected]8266d662011-07-12 21:53:262062void ExtensionService::OnExtensionInstalled(
[email protected]36a5c4c2011-12-14 16:34:502063 const Extension* extension,
2064 bool from_webstore,
2065 const StringOrdinal& page_ordinal) {
[email protected]a29a517a2011-01-21 21:11:122066 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202067
[email protected]4416c5a2010-06-26 01:28:572068 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022069 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352070 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592071 // Extensions installed by policy can't be disabled. So even if a previous
2072 // installation disabled the extension, make sure it is now enabled.
2073 bool initial_enable =
2074 !extension_prefs_->IsExtensionDisabled(id) ||
2075 !Extension::UserMayDisable(extension->location());
[email protected]b2907fd2011-03-25 16:43:372076 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352077 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2078 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322079
[email protected]483d1ff2011-03-03 17:12:022080 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572081 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542082 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352083 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082084 << " and update URL " << extension->update_url().spec()
2085 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542086
[email protected]ad50def52011-10-19 23:17:072087 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272088 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532089 content::Source<Profile>(profile_),
2090 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542091
[email protected]4416c5a2010-06-26 01:28:572092 // Delete the extension directory since we're not going to
2093 // load it.
[email protected]14908b72011-04-20 06:54:362094 if (!BrowserThread::PostTask(
2095 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362096 base::Bind(&extension_file_util::DeleteFile,
2097 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362098 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572099 return;
2100 }
[email protected]4416c5a2010-06-26 01:28:572101 } else {
[email protected]c3cfb012011-04-06 22:07:352102 // We explicitly want to re-enable an uninstalled external
2103 // extension; if we're here, that means the user is manually
2104 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332105 if (IsExternalExtensionUninstalled(id)) {
2106 initial_enable = true;
2107 }
[email protected]aa142702010-03-26 01:26:332108 }
2109
[email protected]07533022011-06-27 20:42:552110 // Do not record the install histograms for upgrades.
2111 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2112 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2113 extension->GetType(), 100);
2114 RecordPermissionMessagesHistogram(
2115 extension, "Extensions.Permissions_Install");
2116 }
2117
[email protected]4416c5a2010-06-26 01:28:572118 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262119 extension,
2120 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352121 from_webstore,
[email protected]36a5c4c2011-12-14 16:34:502122 page_ordinal);
[email protected]25b34332009-06-05 21:53:192123
[email protected]cdfca9702011-08-08 16:07:012124 // Unpacked extensions default to allowing file access, but if that has been
2125 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192126 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012127 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2128 extension_prefs_->SetAllowFileAccess(id, true);
2129 }
2130
[email protected]b6cf240f2011-10-15 22:09:532131 // If the extension should automatically block network startup (e.g., it uses
2132 // the webRequest API), set the preference. Otherwise clear it, in case the
2133 // extension stopped using a relevant API.
2134 extension_prefs_->SetDelaysNetworkRequests(
2135 extension->id(), extension->ImplicitlyDelaysNetworkStartup());
2136
[email protected]ad50def52011-10-19 23:17:072137 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272138 chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]6c2381d2011-10-19 02:52:532139 content::Source<Profile>(profile_),
2140 content::Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:582141
[email protected]9de83282011-12-12 11:06:372142 // Temporary feature to always install shortcuts for platform apps to
2143 // facilitate early testing.
2144 // TODO(benwells): Remove before launching platform apps.
2145 if (extension->is_platform_app()) {
2146 StartInstallApplicationShortcut(extension);
2147 }
2148
[email protected]49cd1572011-02-08 21:38:452149 // Transfer ownership of |extension| to AddExtension.
2150 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422151}
2152
[email protected]eaa7dd182010-12-14 11:09:002153const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472154 const std::string& id, bool include_enabled, bool include_disabled,
2155 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052156 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392157 if (include_enabled) {
[email protected]84df8332011-12-06 18:22:462158 const Extension* extension = extensions_.GetByID(lowercase_id);
2159 if (extension)
2160 return extension;
[email protected]0c6da502009-08-14 22:32:392161 }
2162 if (include_disabled) {
[email protected]84df8332011-12-06 18:22:462163 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
2164 if (extension)
2165 return extension;
[email protected]ce5c4502009-05-06 16:46:112166 }
[email protected]8001df22011-04-28 19:59:472167 if (include_terminated) {
[email protected]84df8332011-12-06 18:22:462168 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
2169 if (extension)
2170 return extension;
[email protected]8001df22011-04-28 19:59:472171 }
[email protected]ce5c4502009-05-06 16:46:112172 return NULL;
2173}
2174
[email protected]bb7f40952011-01-13 00:21:202175void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
[email protected]84df8332011-12-06 18:22:462176 if (!terminated_extensions_.Contains(extension->id()))
2177 terminated_extensions_.Insert(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202178
[email protected]b3f7fe22011-11-11 19:27:562179 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202180}
2181
2182void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202183 std::string lowercase_id = StringToLowerASCII(id);
[email protected]84df8332011-12-06 18:22:462184 terminated_extensions_.Remove(lowercase_id);
[email protected]bb7f40952011-01-13 00:21:202185}
2186
[email protected]0dfe05c2011-02-23 23:03:362187const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472188 const std::string& id) const {
2189 return GetExtensionByIdInternal(id, false, false, true);
2190}
2191
2192const Extension* ExtensionService::GetInstalledExtension(
2193 const std::string& id) const {
2194 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362195}
2196
[email protected]eaa7dd182010-12-14 11:09:002197const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:072198 return GetExtensionById(extension_misc::kWebStoreAppId, false);
2199}
2200
[email protected]eaa7dd182010-12-14 11:09:002201bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]615d88f2011-12-13 01:47:442202 // Allow bindings for all packaged extensions and component hosted apps.
2203 const Extension* extension = extensions_.GetExtensionOrAppByURL(
2204 ExtensionURLInfo(url));
2205 return extension && (!extension->is_hosted_app() ||
2206 extension->location() == Extension::COMPONENT);
[email protected]6d2e60bd2010-06-03 22:37:392207}
2208
[email protected]eaa7dd182010-12-14 11:09:002209const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472210 const std::string& extension_id) {
2211 return omnibox_icon_manager_.GetIcon(extension_id);
2212}
2213
[email protected]eaa7dd182010-12-14 11:09:002214const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312215 const std::string& extension_id) {
2216 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2217}
2218
[email protected]eaa7dd182010-12-14 11:09:002219void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322220 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142221 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322222 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312223 Extension::Location location,
[email protected]47fc70c2011-12-06 07:29:512224 int creation_flags,
2225 bool mark_acknowledged) {
[email protected]ab22ba42011-01-14 16:36:382226 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2227 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012228 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:572229 return;
[email protected]a8af9fdb2010-10-28 21:52:202230
[email protected]8e4560b62011-01-14 10:09:142231 DCHECK(version);
2232
[email protected]7577a5c52009-07-30 06:21:582233 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492234 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582235 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022236 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582237 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142238 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582239 case -1: // existing version is older, we should upgrade
2240 break;
2241 case 0: // existing version is same, do nothing
2242 return;
2243 case 1: // existing version is newer, uh-oh
2244 LOG(WARNING) << "Found external version of extension " << id
2245 << "that is older than current version. Current version "
2246 << "is: " << existing->VersionString() << ". New version "
2247 << "is: " << version << ". Keeping current version.";
2248 return;
2249 }
2250 }
2251
[email protected]b2907fd2011-03-25 16:43:372252 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362253
[email protected]14908b72011-04-20 06:54:362254 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012255 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162256 installer->set_install_source(location);
2257 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072258 installer->set_expected_version(*version);
2259 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312260 installer->set_creation_flags(creation_flags);
[email protected]6dfbbf82010-03-12 23:09:162261 installer->InstallCrx(path);
[email protected]47fc70c2011-12-06 07:29:512262
2263 // Depending on the source, a new external extension might not need a user
2264 // notification on installation. For such extensions, mark them acknowledged
2265 // now to suppress the notification.
2266 if (mark_acknowledged)
2267 AcknowledgeExternalExtension(id);
[email protected]7577a5c52009-07-30 06:21:582268}
2269
[email protected]eaa7dd182010-12-14 11:09:002270void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402271 const FilePath& extension_path,
2272 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402273 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012274 content::NotificationService::current()->Notify(
2275 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2276 content::Source<Profile>(profile_),
2277 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402278
[email protected]8a205c02011-02-04 20:41:332279 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:042280 std::string message = base::StringPrintf(
2281 "Could not load extension from '%s'. %s",
2282 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:402283 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2284}
2285
[email protected]eaa7dd182010-12-14 11:09:002286void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182287 ExtensionHost* host) {
2288 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332289 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182290 if (iter == orphaned_dev_tools_.end())
2291 return;
2292
[email protected]0e12d7d2011-12-01 16:21:442293 DevToolsAgentHost* agent = DevToolsAgentHostRegistry::GetDevToolsAgentHost(
2294 host->render_view_host());
2295 content::DevToolsManager::GetInstance()->AttachClientHost(iter->second,
2296 agent);
[email protected]406027c02010-09-27 08:03:182297 orphaned_dev_tools_.erase(iter);
2298}
2299
[email protected]432115822011-07-10 15:52:272300void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532301 const content::NotificationSource& source,
2302 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272303 switch (type) {
2304 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532305 if (profile_ !=
2306 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322307 break;
[email protected]6c2381d2011-10-19 02:52:532308 }
[email protected]a4ed6282009-12-14 20:51:162309
[email protected]6c2381d2011-10-19 02:52:532310 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262311
[email protected]fa2416f2011-05-03 08:41:202312 // Mark the extension as terminated and Unload it. We want it to
2313 // be in a consistent state: either fully working or not loaded
2314 // at all, but never half-crashed. We do it in a PostTask so
2315 // that other handlers of this notification will still have
2316 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362317 MessageLoop::current()->PostTask(
2318 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362319 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202320 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012321 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202322 host->extension()));
[email protected]31f77262009-12-02 20:48:532323 break;
2324 }
[email protected]432115822011-07-10 15:52:272325 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302326 content::RenderProcessHost* process =
2327 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192328 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302329 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192330 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2331 break;
2332
[email protected]77a6970c2011-04-23 16:58:562333 // Valid extension function names, used to setup bindings in renderer.
2334 std::vector<std::string> function_names;
2335 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2336 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532337
[email protected]77a6970c2011-04-23 16:58:562338 // Scripting whitelist. This is modified by tests and must be communicated
2339 // to renderers.
2340 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2341 *Extension::GetScriptingWhitelist()));
2342
2343 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532344 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]84df8332011-12-06 18:22:462345 for (ExtensionSet::const_iterator iter = extensions_.begin();
2346 iter != extensions_.end(); ++iter) {
[email protected]19647262011-12-16 09:57:492347 // Renderers don't need to know about themes.
2348 if (!(*iter)->is_theme())
2349 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(*iter));
[email protected]77a6970c2011-04-23 16:58:562350 }
[email protected]9776e82e2011-11-15 02:17:532351 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562352 break;
2353 }
[email protected]432115822011-07-10 15:52:272354 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302355 content::RenderProcessHost* process =
2356 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192357 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302358 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192359 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2360 break;
2361
[email protected]f3b1a082011-11-18 00:34:302362 installed_app_hosts_.erase(process->GetID());
[email protected]6f371442011-11-09 06:45:462363
[email protected]6bc04fd82011-12-04 02:29:352364 process_map_.RemoveAllFromProcess(process->GetID());
[email protected]6f371442011-11-09 06:45:462365 BrowserThread::PostTask(
2366 BrowserThread::IO, FROM_HERE,
2367 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
2368 profile_->GetExtensionInfoMap(),
[email protected]f3b1a082011-11-18 00:34:302369 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212370 break;
2371 }
[email protected]432115822011-07-10 15:52:272372 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532373 std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102374 if (*pref_name == prefs::kExtensionInstallAllowList ||
2375 *pref_name == prefs::kExtensionInstallDenyList) {
2376 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102377 } else {
2378 NOTREACHED() << "Unexpected preference name.";
2379 }
[email protected]aa96d3a2010-08-21 08:45:252380 break;
2381 }
[email protected]d91268022011-08-26 13:17:372382 case chrome::NOTIFICATION_IMPORT_FINISHED: {
2383 registrar_.Remove(this, chrome::NOTIFICATION_IMPORT_FINISHED,
[email protected]6c2381d2011-10-19 02:52:532384 content::Source<Profile>(profile_));
[email protected]d91268022011-08-26 13:17:372385 InitEventRouters();
2386 break;
2387 }
[email protected]aa96d3a2010-08-21 08:45:252388
[email protected]4814b512009-11-07 00:12:292389 default:
2390 NOTREACHED() << "Unexpected notification type.";
2391 }
2392}
2393
[email protected]eaa7dd182010-12-14 11:09:002394bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182395 return !GetAppIds().empty();
2396}
[email protected]377011d2010-07-20 04:18:502397
[email protected]eaa7dd182010-12-14 11:09:002398ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182399 ExtensionIdSet result;
[email protected]84df8332011-12-06 18:22:462400 for (ExtensionSet::const_iterator it = extensions_.begin();
[email protected]377011d2010-07-20 04:18:502401 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182402 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2403 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502404 }
2405
[email protected]ec5b50d2010-10-09 16:35:182406 return result;
[email protected]377011d2010-07-20 04:18:502407}
[email protected]d7e9a862010-11-03 21:57:492408
[email protected]eaa7dd182010-12-14 11:09:002409bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492410 return (extension->background_url().is_empty() ||
2411 extension_runtime_data_[extension->id()].background_page_ready);
2412}
2413
[email protected]eaa7dd182010-12-14 11:09:002414void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492415 DCHECK(!extension->background_url().is_empty());
2416 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072417 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272418 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532419 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072420 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492421}
2422
[email protected]eaa7dd182010-12-14 11:09:002423bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492424 return extension_runtime_data_[extension->id()].being_upgraded;
2425}
2426
[email protected]eaa7dd182010-12-14 11:09:002427void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492428 bool value) {
2429 extension_runtime_data_[extension->id()].being_upgraded = value;
2430}
[email protected]1bead0712010-11-27 17:41:532431
[email protected]39a5b532011-10-22 01:47:072432bool ExtensionService::HasUsedWebRequest(const Extension* extension) {
2433 return extension_runtime_data_[extension->id()].has_used_webrequest;
2434}
2435
2436void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2437 bool value) {
2438 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2439}
2440
[email protected]45644f62011-11-23 00:58:232441base::PropertyBag* ExtensionService::GetPropertyBag(
2442 const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532443 return &extension_runtime_data_[extension->id()].property_bag;
2444}
[email protected]cebc3dc2011-04-18 17:15:002445
2446void ExtensionService::RegisterNaClModule(const GURL& url,
2447 const std::string& mime_type) {
2448 NaClModuleInfo info;
2449 info.url = url;
2450 info.mime_type = mime_type;
2451
2452 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2453 nacl_module_list_.push_front(info);
2454}
2455
2456void ExtensionService::UnregisterNaClModule(const GURL& url) {
2457 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2458 DCHECK(iter != nacl_module_list_.end());
2459 nacl_module_list_.erase(iter);
2460}
2461
[email protected]ed0ba002011-05-26 16:55:132462void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332463 // An extension has been added which has a nacl_module component, which means
2464 // there is a MIME type that module wants to handle, so we need to add that
2465 // MIME type to plugins which handle NaCl modules in order to allow the
2466 // individual modules to handle these types.
[email protected]1e127022011-11-29 21:11:472467 FilePath path;
2468 if (!PathService::Get(chrome::FILE_NACL_PLUGIN, &path))
2469 return;
2470 const content::PepperPluginInfo* pepper_info =
2471 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(path);
2472 if (!pepper_info)
2473 return;
[email protected]300c0ea2011-07-15 23:04:332474
[email protected]1e127022011-11-29 21:11:472475 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
2476 // Check each MIME type the plugins handle for the NaCl MIME type.
2477 for (mime_iter = pepper_info->mime_types.begin();
2478 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2479 if (mime_iter->mime_type == kNaClPluginMimeType) {
2480 // This plugin handles "application/x-nacl".
[email protected]300c0ea2011-07-15 23:04:332481
[email protected]1e127022011-11-29 21:11:472482 PluginService::GetInstance()->
2483 UnregisterInternalPlugin(pepper_info->path);
[email protected]300c0ea2011-07-15 23:04:332484
[email protected]1e127022011-11-29 21:11:472485 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332486
[email protected]1e127022011-11-29 21:11:472487 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2488 nacl_module_list_.begin();
2489 iter != nacl_module_list_.end(); ++iter) {
2490 // Add the MIME type specified in the extension to this NaCl plugin,
2491 // With an extra "nacl" argument to specify the location of the NaCl
2492 // manifest file.
2493 webkit::WebPluginMimeType mime_type_info;
2494 mime_type_info.mime_type = iter->mime_type;
2495 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2496 mime_type_info.additional_param_values.push_back(
2497 UTF8ToUTF16(iter->url.spec()));
2498 info.mime_types.push_back(mime_type_info);
[email protected]300c0ea2011-07-15 23:04:332499 }
[email protected]1e127022011-11-29 21:11:472500
2501 PluginService::GetInstance()->RefreshPlugins();
2502 PluginService::GetInstance()->RegisterInternalPlugin(info);
2503 // This plugin has been modified, no need to check the rest of its
2504 // types, but continue checking other plugins.
2505 break;
[email protected]ed0ba002011-05-26 16:55:132506 }
2507 }
[email protected]ed0ba002011-05-26 16:55:132508}
2509
[email protected]cebc3dc2011-04-18 17:15:002510ExtensionService::NaClModuleInfoList::iterator
2511 ExtensionService::FindNaClModule(const GURL& url) {
2512 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2513 iter != nacl_module_list_.end(); ++iter) {
2514 if (iter->url == url)
2515 return iter;
2516 }
2517 return nacl_module_list_.end();
2518}
[email protected]9de83282011-12-12 11:06:372519
2520void ExtensionService::StartInstallApplicationShortcut(
2521 const Extension* extension) {
2522#if !defined(OS_MACOSX)
2523 const int kAppIconSize = 32;
2524
2525 shortcut_info_.extension_id = extension->id();
2526 shortcut_info_.url = GURL(extension->launch_web_url());
2527 shortcut_info_.title = UTF8ToUTF16(extension->name());
2528 shortcut_info_.description = UTF8ToUTF16(extension->description());
2529 shortcut_info_.create_in_applications_menu = true;
2530 shortcut_info_.create_in_quick_launch_bar = true;
2531 shortcut_info_.create_on_desktop = true;
2532
2533 // The icon will be resized to |max_size|.
2534 const gfx::Size max_size(kAppIconSize, kAppIconSize);
2535
2536 // Look for an icon. If there is no icon at the ideal size, we will resize
2537 // whatever we can get. Making a large icon smaller is prefered to making a
2538 // small icon larger, so look for a larger icon first:
2539 ExtensionResource icon_resource = extension->GetIconResource(
2540 kAppIconSize,
2541 ExtensionIconSet::MATCH_BIGGER);
2542
2543 // If no icon exists that is the desired size or larger, get the
2544 // largest icon available:
2545 if (icon_resource.empty()) {
2546 icon_resource = extension->GetIconResource(
2547 kAppIconSize,
2548 ExtensionIconSet::MATCH_SMALLER);
2549 }
2550
2551 // icon_resource may still be empty at this point, in which case LoadImage
2552 // which call the OnImageLoaded callback with a NULL image and exit
2553 // immediately.
2554 tracker_.LoadImage(extension,
2555 icon_resource,
2556 max_size,
2557 ImageLoadingTracker::DONT_CACHE);
2558#endif
2559}
2560
2561void ExtensionService::OnImageLoaded(SkBitmap *image,
2562 const ExtensionResource &resource,
2563 int index) {
2564 // If the image failed to load (e.g. if the resource being loaded was empty)
2565 // use the standard application icon.
2566 if (!image || image->isNull())
2567 image = ExtensionIconSource::LoadImageByResourceId(IDR_APP_DEFAULT_ICON);
2568
2569 shortcut_info_.favicon = *image;
2570 web_app::CreateShortcut(profile_->GetPath(), shortcut_info_);
2571}
[email protected]e1fae1c2011-12-13 22:17:172572
2573extensions::SocketController* ExtensionService::socket_controller() {
2574 // TODO(miket): Find a better place for SocketController to live. It needs
2575 // to be scoped such that it can be created and destroyed on the IO thread.
2576 //
2577 // To coexist with certain unit tests that don't have an IO thread message
2578 // loop available at ExtensionService shutdown, we lazy-initialize this
2579 // object so that those cases neither create nor destroy a SocketController.
2580 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
2581 if (!socket_controller_) {
2582 socket_controller_ = new extensions::SocketController();
2583 }
2584 return socket_controller_;
2585}