blob: 54dd0c298198ed543094b5e9d0d5bd4a02ed33ff [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]7286e3fc2011-07-19 22:13:2418#include "base/stl_util.h"
[email protected]24b538a2010-02-27 01:22:4419#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2520#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2521#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0422#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0623#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3624#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5925#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1926#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3327#include "base/version.h"
[email protected]23d5f172011-10-25 05:49:5328#include "chrome/browser/bookmarks/bookmark_extension_api.h"
[email protected]15730c42009-09-03 00:03:2029#include "chrome/browser/browser_process.h"
[email protected]dfba8762011-09-02 12:49:5430#include "chrome/browser/chrome_plugin_service_filter.h"
[email protected]cdcf60242011-11-04 15:53:4331#include "chrome/browser/download/download_extension_api.h"
[email protected]af9db5f2011-10-05 05:13:1532#include "chrome/browser/extensions/app_notification_manager.h"
[email protected]18cc5a52011-04-15 16:03:3833#include "chrome/browser/extensions/apps_promo.h"
[email protected]d8c8f25f2011-11-02 18:18:0134#include "chrome/browser/extensions/component_loader.h"
[email protected]e0785902011-05-19 23:34:1735#include "chrome/browser/extensions/crx_installer.h"
[email protected]49098f702011-10-13 03:47:1836#include "chrome/browser/extensions/default_apps_trial.h"
[email protected]5cbe1e22010-01-30 01:18:5637#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]b68d5ed2009-04-16 02:41:2838#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4439#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3240#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2441#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]e5af875f2011-10-10 21:09:1442#include "chrome/browser/extensions/extension_global_error.h"
[email protected]b1748b1d82009-11-30 20:32:5643#include "chrome/browser/extensions/extension_host.h"
[email protected]4c136c62011-11-21 23:55:1944#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]7596ce72010-08-30 05:10:4645#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4046#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2947#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1148#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]19eb80152011-02-26 00:28:4349#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0450#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]93fd78f42009-07-10 16:43:1751#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1052#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5253#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1454#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3855#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]d8c8f25f2011-11-02 18:18:0156#include "chrome/browser/extensions/installed_loader.h"
[email protected]b2907fd2011-03-25 16:43:3757#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]cccdf0aa2011-11-11 03:43:3858#include "chrome/browser/extensions/settings/settings_frontend.h"
[email protected]d8c8f25f2011-11-02 18:18:0159#include "chrome/browser/extensions/unpacked_installer.h"
[email protected]d2e6bd62011-10-24 20:29:0760#include "chrome/browser/history/history_extension_api.h"
[email protected]56ad3792010-05-28 17:45:3361#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0262#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3363#include "chrome/browser/profiles/profile.h"
[email protected]8e5c89a2011-06-07 18:13:3364#include "chrome/browser/search_engines/template_url_service.h"
65#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]3bdba0d2011-08-23 07:17:3066#include "chrome/browser/sync/api/sync_change.h"
[email protected]18280372011-03-22 18:05:2267#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4268#include "chrome/browser/themes/theme_service_factory.h"
[email protected]e5af875f2011-10-10 21:09:1469#include "chrome/browser/ui/browser.h"
[email protected]62433d32011-10-12 22:33:1270#include "chrome/browser/ui/browser_list.h"
[email protected]e5af875f2011-10-10 21:09:1471#include "chrome/browser/ui/global_error_service.h"
72#include "chrome/browser/ui/global_error_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1773#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
74#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]5eddc3e2011-10-26 04:33:3175#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
[email protected]aab98a52009-12-02 03:22:3576#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2777#include "chrome/common/chrome_notification_types.h"
[email protected]e2eb43112009-05-29 21:19:5478#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5879#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4080#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0881#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1382#include "chrome/common/extensions/extension_file_util.h"
[email protected]77a6970c2011-04-23 16:58:5683#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2184#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1985#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1486#include "chrome/common/url_constants.h"
[email protected]b46442d7e2011-06-29 02:16:0687#include "content/browser/debugger/devtools_manager.h"
[email protected]2de307592011-04-05 21:16:5888#include "content/browser/plugin_process_host.h"
89#include "content/browser/plugin_service.h"
[email protected]333b1de2011-09-12 18:28:5090#include "content/browser/user_metrics.h"
[email protected]cebc3dc2011-04-18 17:15:0091#include "content/common/pepper_plugin_registry.h"
[email protected]c38831a12011-10-28 12:44:4992#include "content/public/browser/browser_thread.h"
[email protected]05aad2da2011-10-28 10:12:3793#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1694#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3095#include "content/public/browser/render_process_host.h"
[email protected]c10da4b02010-03-25 14:38:3296#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4497#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4498#include "webkit/database/database_tracker.h"
99#include "webkit/database/database_util.h"
[email protected]c64631652009-04-29 22:24:31100
[email protected]eed367e2011-04-12 03:43:31101#if defined(OS_CHROMEOS)
[email protected]14da67b02011-08-02 05:49:13102#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]eed367e2011-04-12 03:43:31103#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:01104#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:40105#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]14da67b02011-08-02 05:49:13106#include "chrome/browser/chromeos/input_method/input_method_manager.h"
[email protected]4c136c62011-11-21 23:55:19107#include "chrome/browser/extensions/extension_input_ime_api.h"
[email protected]b777b332011-04-16 04:01:08108#include "webkit/fileapi/file_system_context.h"
109#include "webkit/fileapi/file_system_mount_point_provider.h"
110#include "webkit/fileapi/file_system_path_manager.h"
[email protected]eed367e2011-04-12 03:43:31111#endif
112
[email protected]27072cad2011-05-09 19:46:40113#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
114#include "chrome/browser/extensions/extension_input_ui_api.h"
115#endif
116
[email protected]5ef47ec2010-01-28 05:58:05117using base::Time;
[email protected]631bb742011-11-02 11:29:39118using content::BrowserThread;
[email protected]5ef47ec2010-01-28 05:58:05119
[email protected]c6d474f82009-12-16 21:11:06120namespace errors = extension_manifest_errors;
121
[email protected]b6ab96d2009-08-20 18:58:19122namespace {
123
[email protected]29d0d4ac2010-09-08 21:10:31124#if defined(OS_LINUX)
125static const int kOmniboxIconPaddingLeft = 2;
126static const int kOmniboxIconPaddingRight = 2;
127#elif defined(OS_MACOSX)
128static const int kOmniboxIconPaddingLeft = 0;
129static const int kOmniboxIconPaddingRight = 2;
130#else
131static const int kOmniboxIconPaddingLeft = 0;
132static const int kOmniboxIconPaddingRight = 0;
133#endif
134
[email protected]300c0ea2011-07-15 23:04:33135const char* kNaClPluginMimeType = "application/x-nacl";
136
[email protected]2de307592011-04-05 21:16:58137static void ForceShutdownPlugin(const FilePath& plugin_path) {
138 PluginProcessHost* plugin =
139 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
140 if (plugin)
141 plugin->ForceShutdown();
142}
143
[email protected]3bdba0d2011-08-23 07:17:30144static bool IsSyncableExtension(const Extension& extension) {
145 return extension.GetSyncType() == Extension::SYNC_TYPE_EXTENSION;
146}
147
148static bool IsSyncableApp(const Extension& extension) {
149 return extension.GetSyncType() == Extension::SYNC_TYPE_APP;
150}
151
[email protected]c6d474f82009-12-16 21:11:06152} // namespace
[email protected]b6ab96d2009-08-20 18:58:19153
[email protected]eaa7dd182010-12-14 11:09:00154ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49155 : background_page_ready(false),
[email protected]f8bf5992011-10-24 22:38:57156 being_upgraded(false),
157 has_used_webrequest(false) {
[email protected]d7e9a862010-11-03 21:57:49158}
159
[email protected]eaa7dd182010-12-14 11:09:00160ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49161}
162
[email protected]cebc3dc2011-04-18 17:15:00163ExtensionService::NaClModuleInfo::NaClModuleInfo() {
164}
165
166ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
167}
168
[email protected]eaa7dd182010-12-14 11:09:00169// ExtensionService.
[email protected]6014d672008-12-05 00:38:25170
[email protected]eaa7dd182010-12-14 11:09:00171const char* ExtensionService::kInstallDirectoryName = "Extensions";
[email protected]15d5b472011-11-24 04:17:24172
173const char* ExtensionService::kLocalAppSettingsDirectoryName =
174 "Local App Settings";
175const char* ExtensionService::kLocalExtensionSettingsDirectoryName =
176 "Local Extension Settings";
177const char* ExtensionService::kSyncAppSettingsDirectoryName =
178 "Sync App Settings";
179const char* ExtensionService::kSyncExtensionSettingsDirectoryName =
180 "Sync Extension Settings";
[email protected]494c06e2009-07-25 01:06:42181
[email protected]8e4560b62011-01-14 10:09:14182void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12183 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20184
[email protected]7a4c6852010-09-16 03:44:22185 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21186 ProviderCollection::const_iterator i;
187 for (i = external_extension_providers_.begin();
188 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14189 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21190 if (i->get()->HasExtension(id))
191 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22192 }
193
[email protected]0f48fca2011-05-19 18:46:35194 // We get the list of external extensions to check from preferences.
195 // It is possible that an extension has preferences but is not loaded.
196 // For example, an extension that requires experimental permissions
197 // will not be loaded if the experimental command line flag is not used.
198 // In this case, do not uninstall.
199 const Extension* extension = GetInstalledExtension(id);
200 if (!extension) {
201 // We can't call UninstallExtension with an unloaded/invalid
202 // extension ID.
203 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
204 << "with id: " << id;
205 return;
206 }
[email protected]d6ebc9792011-04-07 18:18:33207 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22208}
209
[email protected]8e4560b62011-01-14 10:09:14210void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22211 external_extension_providers_.clear();
212}
213
[email protected]8e4560b62011-01-14 10:09:14214void ExtensionService::AddProviderForTesting(
215 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12216 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21217 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14218 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22219}
220
[email protected]8e4560b62011-01-14 10:09:14221void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22222 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42223 const GURL& update_url,
224 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38225 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
226 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20227
[email protected]8a87a5332011-08-11 17:54:59228 const Extension* extension = GetExtensionById(id, true);
229 if (extension) {
230 // Already installed. Skip this install if the current location has
231 // higher priority than |location|.
232 Extension::Location current = extension->location();
233 if (current == Extension::GetHigherPriorityLocation(current, location))
234 return;
235 // Otherwise, overwrite the current installation.
[email protected]7a4c6852010-09-16 03:44:22236 }
[email protected]b2907fd2011-03-25 16:43:37237 pending_extension_manager()->AddFromExternalUpdateUrl(
238 id, update_url, location);
[email protected]50067e52011-10-20 23:17:07239 external_extension_url_added_ = true;
[email protected]7a4c6852010-09-16 03:44:22240}
241
[email protected]454434f2011-08-24 04:00:00242// If a download url matches one of these patterns and has a referrer of the
243// webstore, then we're willing to treat that as a gallery download.
244static const char* kAllowedDownloadURLPatterns[] = {
245 "https://clients2.google.com/service/update2*",
246 "https://clients2.googleusercontent.com/crx/*"
247};
248
[email protected]eaa7dd182010-12-14 11:09:00249bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12250 const GURL& referrer_url) {
[email protected]9adb9692010-10-29 23:14:02251 const Extension* download_extension = GetExtensionByWebExtent(download_url);
252 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
253 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07254
255 bool referrer_valid = (referrer_extension == webstore_app);
256 bool download_valid = (download_extension == webstore_app);
257
[email protected]454434f2011-08-24 04:00:00258 // We also allow the download to be from a small set of trusted paths.
259 if (!download_valid) {
260 for (size_t i = 0; i < arraysize(kAllowedDownloadURLPatterns); i++) {
261 URLPattern pattern(URLPattern::SCHEME_HTTPS,
262 kAllowedDownloadURLPatterns[i]);
263 if (pattern.MatchesURL(download_url)) {
264 download_valid = true;
265 break;
266 }
267 }
268 }
269
[email protected]d3071992010-10-08 15:24:07270 // If the command-line gallery URL is set, then be a bit more lenient.
271 GURL store_url =
272 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
273 switches::kAppsGalleryURL));
274 if (!store_url.is_empty()) {
275 std::string store_tld =
276 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
277 if (!referrer_valid) {
278 std::string referrer_tld =
279 net::RegistryControlledDomainService::GetDomainAndRegistry(
280 referrer_url);
281 // The referrer gets stripped when transitioning from https to http,
282 // or when hitting an unknown test cert and that commonly happens in
283 // testing environments. Given this, we allow an empty referrer when
284 // the command-line flag is set.
285 // Otherwise, the TLD must match the TLD of the command-line url.
286 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
287 }
288
289 if (!download_valid) {
290 std::string download_tld =
291 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50292 download_url);
[email protected]d3071992010-10-08 15:24:07293
294 // Otherwise, the TLD must match the TLD of the command-line url.
295 download_valid = (download_tld == store_tld);
296 }
297 }
298
299 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23300}
301
[email protected]d9696672011-03-15 22:45:09302const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27303 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09304 const Extension* app = GetExtensionByWebExtent(url);
305 if (app)
306 return app;
[email protected]ffb204f22010-12-05 23:20:27307
308 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09309 app = GetExtensionByURL(url);
310 if (app && app->is_app())
311 return app;
312
313 return NULL;
314}
315
316bool ExtensionService::IsInstalledApp(const GURL& url) {
317 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27318}
319
[email protected]da5683db2011-04-23 17:12:21320void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
321 const Extension* app) {
322 installed_app_hosts_[renderer_child_id] = app;
323}
324
325const Extension* ExtensionService::GetInstalledAppForRenderer(
326 int renderer_child_id) {
327 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
328 if (i == installed_app_hosts_.end())
329 return NULL;
330 return i->second;
331}
332
[email protected]6aeac8342010-10-01 20:21:18333// static
[email protected]d6ebc9792011-04-07 18:18:33334// This function is used to implement the command-line switch
[email protected]3bdba0d2011-08-23 07:17:30335// --uninstall-extension, and to uninstall an extension via sync. The LOG
336// statements within this function are used to inform the user if the uninstall
337// cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00338bool ExtensionService::UninstallExtensionHelper(
339 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18340 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18341
[email protected]95da88c42011-03-31 10:07:33342 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47343 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33344
[email protected]d6ebc9792011-04-07 18:18:33345 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33346 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18347 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33348 << "id: " << extension_id;
349 return false;
[email protected]6aeac8342010-10-01 20:21:18350 }
351
[email protected]d6ebc9792011-04-07 18:18:33352 // The following call to UninstallExtension will not allow an uninstall of a
353 // policy-controlled extension.
354 std::string error;
355 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
356 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
357 << ": " << error;
358 return false;
359 }
[email protected]95da88c42011-03-31 10:07:33360
[email protected]6aeac8342010-10-01 20:21:18361 return true;
362}
363
[email protected]eaa7dd182010-12-14 11:09:00364ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24365 const CommandLine* command_line,
366 const FilePath& install_directory,
367 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03368 bool autoupdate_enabled,
369 bool extensions_enabled)
[email protected]d8c8f25f2011-11-02 18:18:01370 : profile_(profile),
[email protected]73c47932010-12-06 18:13:43371 extension_prefs_(extension_prefs),
[email protected]cccdf0aa2011-11-11 03:43:38372 settings_frontend_(extensions::SettingsFrontend::Create(profile)),
[email protected]14908b72011-04-20 06:54:36373 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23374 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03375 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13376 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55377 ready_(false),
[email protected]14908b72011-04-20 06:54:36378 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]8b280302011-10-13 22:22:23379 menu_manager_(profile),
[email protected]af9db5f2011-10-05 05:13:15380 app_notification_manager_(new AppNotificationManager(profile)),
[email protected]902fd7b2011-07-27 18:42:31381 permissions_manager_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]18cc5a52011-04-15 16:03:38382 apps_promo_(profile->GetPrefs()),
[email protected]7c1490da2011-10-11 18:53:25383 event_routers_initialized_(false),
384 extension_warnings_(profile) {
[email protected]a29a517a2011-01-21 21:11:12385 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20386
[email protected]36a784c2009-06-23 06:21:08387 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23388 if (command_line->HasSwitch(switches::kDisableExtensions)) {
389 extensions_enabled_ = false;
390 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
391 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18392 }
[email protected]36a784c2009-06-23 06:21:08393
[email protected]432115822011-07-10 15:52:27394 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07395 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27396 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]ad50def52011-10-19 23:17:07397 content::NotificationService::AllBrowserContextsAndSources());
[email protected]432115822011-07-10 15:52:27398 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]ad50def52011-10-19 23:17:07399 content::NotificationService::AllBrowserContextsAndSources());
[email protected]2fb7dc982010-09-29 12:24:28400 pref_change_registrar_.Init(profile->GetPrefs());
401 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
402 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29403
[email protected]93fd78f42009-07-10 16:43:17404 // Set up the ExtensionUpdater
405 if (autoupdate_enabled) {
406 int update_frequency = kDefaultUpdateFrequencySeconds;
407 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25408 base::StringToInt(command_line->GetSwitchValueASCII(
409 switches::kExtensionsUpdateFrequency),
410 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17411 }
[email protected]82b6e512011-04-12 20:33:27412 updater_.reset(new ExtensionUpdater(this,
413 extension_prefs,
414 profile->GetPrefs(),
415 profile,
416 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17417 }
418
[email protected]25ae0152011-11-18 14:40:02419 component_loader_.reset(
420 new extensions::ComponentLoader(this,
421 profile->GetPrefs(),
422 g_browser_process->local_state()));
[email protected]8e4560b62011-01-14 10:09:14423
[email protected]af9db5f2011-10-05 05:13:15424 app_notification_manager_->Init();
425
[email protected]0436b102011-04-15 18:30:03426 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05427 ExternalExtensionProviderImpl::CreateExternalProviders(
428 this, profile_, &external_extension_providers_);
429 }
[email protected]b671760b2010-07-15 21:13:47430
[email protected]aa96d3a2010-08-21 08:45:25431 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31432 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47433 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31434 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
435 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07436
437 // How long is the path to the Extensions directory?
438 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
439 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25440}
441
[email protected]eaa7dd182010-12-14 11:09:00442const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45443 return &extensions_;
444}
445
[email protected]eaa7dd182010-12-14 11:09:00446const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45447 return &disabled_extensions_;
448}
449
[email protected]bb7f40952011-01-13 00:21:20450const ExtensionList* ExtensionService::terminated_extensions() const {
451 return &terminated_extensions_;
452}
453
[email protected]b2907fd2011-03-25 16:43:37454PendingExtensionManager* ExtensionService::pending_extension_manager() {
455 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45456}
457
[email protected]eaa7dd182010-12-14 11:09:00458ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17459 // No need to unload extensions here because they are profile-scoped, and the
460 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17461
[email protected]8e4560b62011-01-14 10:09:14462 ProviderCollection::const_iterator i;
463 for (i = external_extension_providers_.begin();
464 i != external_extension_providers_.end(); ++i) {
465 ExternalExtensionProviderInterface* provider = i->get();
466 provider->ServiceShutdown();
467 }
[email protected]6014d672008-12-05 00:38:25468}
469
[email protected]d91268022011-08-26 13:17:37470void ExtensionService::InitEventRoutersAfterImport() {
471 registrar_.Add(this, chrome::NOTIFICATION_IMPORT_FINISHED,
[email protected]6c2381d2011-10-19 02:52:53472 content::Source<Profile>(profile_));
[email protected]d91268022011-08-26 13:17:37473}
474
[email protected]eaa7dd182010-12-14 11:09:00475void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18476 if (event_routers_initialized_)
477 return;
478
[email protected]370e9502011-09-14 19:31:44479 downloads_event_router_.reset(new ExtensionDownloadsEventRouter(profile_));
[email protected]d2e6bd62011-10-24 20:29:07480 history_event_router_.reset(new HistoryExtensionEventRouter());
[email protected]f825df22011-06-28 17:36:10481 history_event_router_->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08482 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
483 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40484 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]23d5f172011-10-25 05:49:53485 bookmark_event_router_.reset(new BookmarkExtensionEventRouter(
[email protected]f825df22011-06-28 17:36:10486 profile_->GetBookmarkModel()));
487 bookmark_event_router_->Init();
488 cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_));
489 cookies_event_router_->Init();
490 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
491 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11492 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10493 web_navigation_event_router_.reset(
494 new ExtensionWebNavigationEventRouter(profile_));
495 web_navigation_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40496
[email protected]eed367e2011-04-12 03:43:31497#if defined(OS_CHROMEOS)
[email protected]b16fc2e2011-07-21 21:32:58498 file_browser_event_router_.reset(
499 new ExtensionFileBrowserEventRouter(profile_));
500 file_browser_event_router_->ObserveFileSystemEvents();
[email protected]771a6182011-10-05 16:32:52501
502 input_method_event_router_.reset(
503 new chromeos::ExtensionInputMethodEventRouter);
[email protected]be5bd5742011-07-14 07:21:01504
[email protected]c8bf09be2011-06-25 03:22:27505 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]4c136c62011-11-21 23:55:19506 ExtensionInputImeEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31507#endif
[email protected]27072cad2011-05-09 19:46:40508
509#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
510 ExtensionInputUiEventRouter::GetInstance()->Init();
511#endif
512
[email protected]b3d62312b12010-10-14 21:10:18513 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37514}
515
[email protected]b2907fd2011-03-25 16:43:37516const Extension* ExtensionService::GetExtensionById(
517 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47518 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44519}
520
[email protected]eaa7dd182010-12-14 11:09:00521void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12522 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20523
[email protected]fa6a9102010-11-22 15:38:50524 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17525 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32526
[email protected]95dd38f2009-10-20 20:09:15527 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
528 // the first extension, because its members listen for loaded notifications.
529 g_browser_process->resource_dispatcher_host();
530
[email protected]d8c8f25f2011-11-02 18:18:01531 component_loader_->LoadAll();
532 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57533
[email protected]9f1087e2009-06-15 17:29:32534 // TODO(erikkay) this should probably be deferred to a future point
535 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17536 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57537
[email protected]9f1087e2009-06-15 17:29:32538 // TODO(erikkay) this should probably be deferred as well.
539 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25540}
541
[email protected]420a0ec2011-06-01 01:07:03542bool ExtensionService::UpdateExtension(
543 const std::string& id,
544 const FilePath& extension_path,
545 const GURL& download_url,
546 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12547 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20548
[email protected]b2907fd2011-03-25 16:43:37549 PendingExtensionInfo pending_extension_info;
550 bool is_pending_extension = pending_extension_manager_.GetById(
551 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32552
[email protected]8001df22011-04-28 19:59:47553 const Extension* extension =
554 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41555 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33556 LOG(WARNING) << "Will not update extension " << id
557 << " because it is not installed or pending";
558 // Delete extension_path since we're not creating a CrxInstaller
559 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36560 if (!BrowserThread::PostTask(
561 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36562 base::Bind(
[email protected]7296f2762011-11-21 19:23:44563 &extension_file_util::DeleteFile, extension_path, false)))
[email protected]14908b72011-04-20 06:54:36564 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03565
566 return false;
[email protected]e957fe52009-06-23 16:51:05567 }
568
[email protected]aa142702010-03-26 01:26:33569 // We want a silent install only for non-pending extensions and
570 // pending extensions that have install_silently set.
571 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37572 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33573 NULL : new ExtensionInstallUI(profile_);
574
[email protected]d8c8f25f2011-11-02 18:18:01575 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, client));
[email protected]6dfbbf82010-03-12 23:09:16576 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18577 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37578 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41579 else if (extension)
580 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52581 if (pending_extension_info.install_silently())
582 installer->set_allow_silent_install(true);
[email protected]3d729722011-09-20 02:57:09583 if (extension && extension->from_webstore())
584 installer->set_is_gallery_install(true);
[email protected]6dfbbf82010-03-12 23:09:16585 installer->set_delete_source(true);
[email protected]655b2b1a2011-10-13 17:13:06586 installer->set_download_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07587 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16588 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03589
590 if (out_crx_installer)
591 *out_crx_installer = installer;
592
593 return true;
[email protected]e957fe52009-06-23 16:51:05594}
595
[email protected]eaa7dd182010-12-14 11:09:00596void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12597 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06598 FilePath path;
[email protected]9adb9692010-10-29 23:14:02599 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40600
[email protected]f17dbd42010-08-16 23:21:10601 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06602 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29603 // If the extension has an inspector open for its background page, detach
604 // the inspector and hang onto a cookie for it, so that we can reattach
605 // later.
606 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
[email protected]06024c62011-10-20 20:57:12607 ExtensionHost* host = manager->GetBackgroundHostForExtension(extension_id);
[email protected]4814b512009-11-07 00:12:29608 if (host) {
609 // Look for an open inspector for the background page.
610 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
611 host->render_view_host());
612 if (devtools_cookie >= 0)
613 orphaned_dev_tools_[extension_id] = devtools_cookie;
614 }
615
[email protected]b65272f2009-08-31 15:47:06616 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10617 DisableExtension(extension_id);
618 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16619 } else {
620 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06621 }
622
[email protected]e6090e42010-03-23 22:44:08623 // Check the installed extensions to see if what we're reloading was already
624 // installed.
625 scoped_ptr<ExtensionInfo> installed_extension(
626 extension_prefs_->GetInstalledExtensionInfo(extension_id));
627 if (installed_extension.get() &&
628 installed_extension->extension_manifest.get()) {
[email protected]d8c8f25f2011-11-02 18:18:01629 extensions::InstalledLoader(this).Load(*installed_extension, false);
[email protected]e6090e42010-03-23 22:44:08630 } else {
[email protected]d8c8f25f2011-11-02 18:18:01631 // Otherwise, the extension is unpacked (location LOAD).
[email protected]e6090e42010-03-23 22:44:08632 // We should always be able to remember the extension's path. If it's not in
633 // the map, someone failed to update |unloaded_extension_paths_|.
634 CHECK(!path.empty());
[email protected]d8c8f25f2011-11-02 18:18:01635 extensions::UnpackedInstaller::Create(this)->Load(path);
[email protected]e6090e42010-03-23 22:44:08636 }
[email protected]9cddd4702009-07-27 22:09:40637}
638
[email protected]fa2416f2011-05-03 08:41:20639bool ExtensionService::UninstallExtension(
640 const std::string& extension_id_unsafe,
641 bool external_uninstall,
642 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12643 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20644
[email protected]fa2416f2011-05-03 08:41:20645 // Copy the extension identifier since the reference might have been
646 // obtained via Extension::id() and the extension may be deleted in
647 // this function.
648 std::string extension_id(extension_id_unsafe);
649
[email protected]0d6ec3a72011-09-02 02:09:43650 scoped_refptr<const Extension> extension(GetInstalledExtension(extension_id));
[email protected]631cf822009-05-15 07:01:25651
[email protected]e7afe2452010-08-22 16:19:13652 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48653 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32654
[email protected]95da88c42011-03-31 10:07:33655 // Policy change which triggers an uninstall will always set
656 // |external_uninstall| to true so this is the only way to uninstall
657 // managed extensions.
[email protected]0d6ec3a72011-09-02 02:09:43658 if (!Extension::UserMayDisable(extension->location()) &&
659 !external_uninstall) {
[email protected]ad50def52011-10-19 23:17:07660 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27661 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:53662 content::Source<Profile>(profile_),
663 content::Details<const Extension>(extension));
[email protected]d6ebc9792011-04-07 18:18:33664 if (error != NULL) {
665 *error = errors::kCannotUninstallManagedExtension;
666 }
667 return false;
668 }
[email protected]95da88c42011-03-31 10:07:33669
[email protected]3bdba0d2011-08-23 07:17:30670 // Extract the data we need for sync now, but don't actually sync until we've
671 // completed the uninstallation.
672 SyncBundle* sync_bundle = GetSyncBundleForExtension(*extension);
673
674 SyncChange sync_change;
675 if (sync_bundle) {
[email protected]aa7599d2011-10-28 07:24:32676 ExtensionSyncData extension_sync_data(
677 *extension,
678 IsExtensionEnabled(extension_id),
679 IsIncognitoEnabled(extension_id),
680 extension_prefs_->IsAppNotificationSetupDone(extension_id),
681 extension_prefs_->IsAppNotificationDisabled(extension_id));
[email protected]3bdba0d2011-08-23 07:17:30682 sync_change = extension_sync_data.GetSyncChange(SyncChange::ACTION_DELETE);
683 }
684
[email protected]9b217652010-10-08 22:04:23685 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08686 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49687 RecordPermissionMessagesHistogram(
688 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23689
[email protected]8e5c89a2011-06-07 18:13:33690 TemplateURLService* url_service =
691 TemplateURLServiceFactory::GetForProfile(profile_);
692 if (url_service)
693 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33694
[email protected]831aa212010-03-26 13:55:19695 // Unload before doing more cleanup to ensure that nothing is hanging on to
696 // any of these resources.
[email protected]814a7bf0f2011-08-13 05:30:59697 UnloadExtension(extension_id, extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]831aa212010-03-26 13:55:19698
[email protected]0d6ec3a72011-09-02 02:09:43699 extension_prefs_->OnExtensionUninstalled(extension_id, extension->location(),
[email protected]831aa212010-03-26 13:55:19700 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32701
702 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]0d6ec3a72011-09-02 02:09:43703 if (Extension::LOAD != extension->location()) {
[email protected]14908b72011-04-20 06:54:36704 if (!BrowserThread::PostTask(
705 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:36706 base::Bind(
[email protected]14908b72011-04-20 06:54:36707 &extension_file_util::UninstallExtension,
708 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20709 extension_id)))
[email protected]14908b72011-04-20 06:54:36710 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32711 }
712
[email protected]0d6ec3a72011-09-02 02:09:43713 GURL launch_web_url_origin(extension->launch_web_url());
714 launch_web_url_origin = launch_web_url_origin.GetOrigin();
715 bool is_storage_isolated =
716 (extension->is_storage_isolated() &&
717 extension->HasAPIPermission(ExtensionAPIPermission::kExperimental));
718
719 if (extension->is_hosted_app() &&
720 !profile_->GetExtensionSpecialStoragePolicy()->
721 IsStorageProtected(launch_web_url_origin)) {
[email protected]dc0b5a12011-10-14 00:06:13722 ExtensionDataDeleter::StartDeleting(
723 profile_, extension_id, launch_web_url_origin, is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43724 }
[email protected]dc0b5a12011-10-14 00:06:13725 ExtensionDataDeleter::StartDeleting(
726 profile_, extension_id, extension->url(), is_storage_isolated);
[email protected]0d6ec3a72011-09-02 02:09:43727
[email protected]0dfe05c2011-02-23 23:03:36728 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06729
730 // Notify interested parties that we've uninstalled this extension.
[email protected]ad50def52011-10-19 23:17:07731 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27732 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]6c2381d2011-10-19 02:52:53733 content::Source<Profile>(profile_),
734 content::Details<const std::string>(&extension_id));
[email protected]d6ebc9792011-04-07 18:18:33735
[email protected]3bdba0d2011-08-23 07:17:30736 if (sync_bundle && sync_bundle->HasExtensionId(extension_id)) {
737 sync_bundle->sync_processor->ProcessSyncChanges(
738 FROM_HERE, SyncChangeList(1, sync_change));
739 sync_bundle->synced_extensions.erase(extension_id);
740 }
741
[email protected]333b1de2011-09-12 18:28:50742 // Track the uninstallation.
[email protected]49098f702011-10-13 03:47:18743 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionUninstalled", 1, 2);
744
745 static bool default_apps_trial_exists =
746 base::FieldTrialList::TrialExists(kDefaultAppsTrial_Name);
747 if (default_apps_trial_exists) {
748 UMA_HISTOGRAM_ENUMERATION(
749 base::FieldTrial::MakeName("Extensions.ExtensionUninstalled",
750 kDefaultAppsTrial_Name),
751 1, 2);
752 }
[email protected]333b1de2011-09-12 18:28:50753
[email protected]7c1490da2011-10-11 18:53:25754 // Uninstalling one extension might have solved the problems of others.
755 // Therefore, we clear warnings of this type for all extensions.
756 std::set<ExtensionWarningSet::WarningType> warnings;
757 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
758 extension_warnings_.ClearWarnings(warnings);
759
[email protected]d6ebc9792011-04-07 18:18:33760 return true;
[email protected]c10da4b02010-03-25 14:38:32761}
762
[email protected]c3cfb012011-04-06 22:07:35763bool ExtensionService::IsExtensionEnabled(
764 const std::string& extension_id) const {
[email protected]36429da2011-07-11 20:25:18765 const Extension* extension =
[email protected]ad83ca242011-07-29 01:32:25766 GetExtensionByIdInternal(extension_id, true, false, true);
[email protected]36429da2011-07-11 20:25:18767 if (extension)
768 return true;
769
[email protected]ad83ca242011-07-29 01:32:25770 extension =
771 GetExtensionByIdInternal(extension_id, false, true, false);
772 if (extension)
773 return false;
774
775 // If the extension hasn't been loaded yet, check the prefs for it. Assume
776 // enabled unless otherwise noted.
777 return !extension_prefs_->IsExtensionDisabled(extension_id) &&
778 !extension_prefs_->IsExternalExtensionUninstalled(extension_id);
[email protected]c3cfb012011-04-06 22:07:35779}
780
781bool ExtensionService::IsExternalExtensionUninstalled(
782 const std::string& extension_id) const {
783 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
784}
785
[email protected]eaa7dd182010-12-14 11:09:00786void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12787 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20788
[email protected]06f92562011-04-29 19:27:31789 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39790 return;
[email protected]0c6da502009-08-14 22:32:39791
[email protected]b3317ad2011-04-28 23:46:00792 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52793
[email protected]06f92562011-04-29 19:27:31794 const Extension* extension =
795 GetExtensionByIdInternal(extension_id, false, true, false);
796 // This can happen if sync enables an extension that is not
797 // installed yet.
798 if (!extension)
799 return;
800
[email protected]0c6da502009-08-14 22:32:39801 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57802 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39803 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
804 disabled_extensions_.end(),
805 extension);
806 disabled_extensions_.erase(iter);
807
[email protected]f681c782010-11-19 11:19:39808 // Make sure any browser action contained within it is not hidden.
809 extension_prefs_->SetBrowserActionVisibility(extension, true);
810
[email protected]62d30f42009-10-01 22:36:06811 NotifyExtensionLoaded(extension);
[email protected]3bdba0d2011-08-23 07:17:30812
813 SyncExtensionChangeIfNeeded(*extension);
[email protected]0c6da502009-08-14 22:32:39814}
815
[email protected]eaa7dd182010-12-14 11:09:00816void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12817 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20818
[email protected]b2ba9962009-12-10 20:10:15819 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31820 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52821 return;
[email protected]1784e83a2009-09-08 21:01:52822
[email protected]06f92562011-04-29 19:27:31823 const Extension* extension = GetInstalledExtension(extension_id);
824 // |extension| can be NULL if sync disables an extension that is not
825 // installed yet.
826 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:33827 return;
828
[email protected]b3317ad2011-04-28 23:46:00829 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52830
[email protected]06f92562011-04-29 19:27:31831 extension = GetExtensionByIdInternal(extension_id, true, false, true);
832 if (!extension)
833 return;
834
[email protected]1784e83a2009-09-08 21:01:52835 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:57836 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:52837 ExtensionList::iterator iter = std::find(extensions_.begin(),
838 extensions_.end(),
839 extension);
[email protected]06f92562011-04-29 19:27:31840 if (iter != extensions_.end()) {
841 extensions_.erase(iter);
842 } else {
843 iter = std::find(terminated_extensions_.begin(),
844 terminated_extensions_.end(),
845 extension);
846 terminated_extensions_.erase(iter);
847 }
[email protected]1784e83a2009-09-08 21:01:52848
[email protected]814a7bf0f2011-08-13 05:30:59849 NotifyExtensionUnloaded(extension, extension_misc::UNLOAD_REASON_DISABLE);
[email protected]3bdba0d2011-08-23 07:17:30850
851 SyncExtensionChangeIfNeeded(*extension);
[email protected]7c1490da2011-10-11 18:53:25852
853 // Deactivating one extension might have solved the problems of others.
854 // Therefore, we clear warnings of this type for all extensions.
855 std::set<ExtensionWarningSet::WarningType> warnings;
856 extension_warnings_.GetWarningsAffectingExtension(extension_id, &warnings);
857 extension_warnings_.ClearWarnings(warnings);
[email protected]1784e83a2009-09-08 21:01:52858}
859
[email protected]eaa7dd182010-12-14 11:09:00860void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25861 CHECK(extension);
862
[email protected]902fd7b2011-07-27 18:42:31863 // We only maintain the granted permissions prefs for extensions that can't
864 // silently increase their permissions.
865 if (extension->CanSilentlyIncreasePermissions())
866 return;
[email protected]8d888c12010-11-30 00:00:25867
[email protected]8d888c12010-11-30 00:00:25868 extension_prefs_->AddGrantedPermissions(extension->id(),
[email protected]902fd7b2011-07-27 18:42:31869 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:25870}
871
[email protected]eaa7dd182010-12-14 11:09:00872void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25873 const Extension* extension) {
874 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49875 RecordPermissionMessagesHistogram(
876 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25877 GrantPermissions(extension);
878 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
879 EnableExtension(extension->id());
880}
881
[email protected]902fd7b2011-07-27 18:42:31882void ExtensionService::UpdateActivePermissions(
883 const Extension* extension,
884 const ExtensionPermissionSet* permissions) {
885 extension_prefs()->SetActivePermissions(extension->id(), permissions);
886 extension->SetActivePermissions(permissions);
887}
888
[email protected]fe2dd7742011-04-19 22:52:49889// static
890void ExtensionService::RecordPermissionMessagesHistogram(
891 const Extension* e, const char* histogram) {
892 // Since this is called from multiple sources, and since the Histogram macros
893 // use statics, we need to manually lookup the Histogram ourselves.
894 base::Histogram* counter = base::LinearHistogram::FactoryGet(
895 histogram,
896 1,
[email protected]0d3e4a22011-06-23 19:02:52897 ExtensionPermissionMessage::kEnumBoundary,
898 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:49899 base::Histogram::kUmaTargetedHistogramFlag);
900
[email protected]0d3e4a22011-06-23 19:02:52901 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:49902 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:52903 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:49904 } else {
[email protected]0d3e4a22011-06-23 19:02:52905 for (ExtensionPermissionMessages::iterator it = permissions.begin();
906 it != permissions.end(); ++it)
907 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:49908 }
909}
910
[email protected]eaa7dd182010-12-14 11:09:00911void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:42912 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:06913 // was loaded, otherwise a race can arise where a renderer that is created
914 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:42915 // that the request context doesn't yet know about. The profile is responsible
916 // for ensuring its URLRequestContexts appropriately discover the loaded
917 // extension.
[email protected]c8d407e2011-04-28 21:27:17918 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:06919
[email protected]c8d407e2011-04-28 21:27:17920 // Tell subsystems that use the EXTENSION_LOADED notification about the new
921 // extension.
[email protected]ad50def52011-10-19 23:17:07922 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27923 chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]6c2381d2011-10-19 02:52:53924 content::Source<Profile>(profile_),
925 content::Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:58926
[email protected]c8d407e2011-04-28 21:27:17927 // Tell renderers about the new extension.
[email protected]f3b1a082011-11-18 00:34:30928 for (content::RenderProcessHost::iterator i(
929 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:56930 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:30931 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:08932 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:30933 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:08934 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile()) {
[email protected]9776e82e2011-11-15 02:17:53935 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions(
936 1, ExtensionMsg_Loaded_Params(extension));
[email protected]c8d407e2011-04-28 21:27:17937 host->Send(
[email protected]9776e82e2011-11-15 02:17:53938 new ExtensionMsg_Loaded(loaded_extensions));
[email protected]c8d407e2011-04-28 21:27:17939 }
[email protected]77a6970c2011-04-23 16:58:56940 }
941
[email protected]c8d407e2011-04-28 21:27:17942 // Tell a random-ass collection of other subsystems about the new extension.
943 // TODO(aa): What should we do with all this goop? Can it move into the
944 // relevant objects via EXTENSION_LOADED?
945
946 profile_->GetExtensionSpecialStoragePolicy()->
947 GrantRightsForExtension(extension);
948
949 UpdateActiveExtensionsInCrashReporter();
950
951 ExtensionWebUI::RegisterChromeURLOverrides(
952 profile_, extension->GetChromeURLOverrides());
953
[email protected]8e5c89a2011-06-07 18:13:33954 TemplateURLService* url_service =
955 TemplateURLServiceFactory::GetForProfile(profile_);
956 if (url_service)
957 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:17958
959 // Load the icon for omnibox-enabled extensions so it will be ready to display
960 // in the URL bar.
961 if (!extension->omnibox_keyword().empty()) {
962 omnibox_popup_icon_manager_.LoadIcon(extension);
963 omnibox_icon_manager_.LoadIcon(extension);
964 }
965
966 // If the extension has permission to load chrome://favicon/ resources we need
967 // to make sure that the FaviconSource is registered with the
968 // ChromeURLDataManager.
969 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
970 FaviconSource* favicon_source = new FaviconSource(profile_,
971 FaviconSource::FAVICON);
972 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
973 }
[email protected]5eddc3e2011-10-26 04:33:31974 // Same for chrome://thumb/ resources.
975 if (extension->HasHostPermission(GURL(chrome::kChromeUIThumbnailURL))) {
976 ThumbnailSource* thumbnail_source = new ThumbnailSource(profile_);
977 profile_->GetChromeURLDataManager()->AddDataSource(thumbnail_source);
978 }
[email protected]c8d407e2011-04-28 21:27:17979
980 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:58981 bool plugins_changed = false;
982 for (size_t i = 0; i < extension->plugins().size(); ++i) {
983 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]f520b5b2011-11-08 02:42:14984 PluginService::GetInstance()->RefreshPlugins();
985 PluginService::GetInstance()->AddExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:58986 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:49987 ChromePluginServiceFilter* filter =
988 ChromePluginServiceFilter::GetInstance();
989 if (plugin.is_public) {
990 filter->RestrictPluginToProfileAndOrigin(
991 plugin.path, profile_, GURL());
992 } else {
993 filter->RestrictPluginToProfileAndOrigin(
994 plugin.path, profile_, extension->url());
[email protected]2de307592011-04-05 21:16:58995 }
996 }
[email protected]84396dbc2011-04-14 06:33:42997
998 bool nacl_modules_changed = false;
999 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1000 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001001 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421002 nacl_modules_changed = true;
1003 }
1004
[email protected]ed0ba002011-05-26 16:55:131005 if (nacl_modules_changed)
1006 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421007
1008 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021009 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]b4d9d122011-06-17 01:58:541010
[email protected]14da67b02011-08-02 05:49:131011#if defined(OS_CHROMEOS)
1012#if defined(TOUCH_UI)
[email protected]c8d4e9462011-06-29 23:18:571013 chromeos::input_method::InputMethodManager* input_method_manager =
1014 chromeos::input_method::InputMethodManager::GetInstance();
[email protected]14da67b02011-08-02 05:49:131015#endif
[email protected]b4d9d122011-06-17 01:58:541016 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1017 extension->input_components().begin();
1018 component != extension->input_components().end();
1019 ++component) {
[email protected]14da67b02011-08-02 05:49:131020 if (component->type == Extension::INPUT_COMPONENT_TYPE_IME) {
[email protected]4c136c62011-11-21 23:55:191021 ExtensionInputImeEventRouter::GetInstance()->RegisterIme(
[email protected]14da67b02011-08-02 05:49:131022 profile_, extension->id(), *component);
1023 }
1024#if defined(TOUCH_UI)
[email protected]b4d9d122011-06-17 01:58:541025 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1026 !component->layouts.empty()) {
1027 const bool is_system =
1028 !Extension::IsExternalLocation(extension->location());
[email protected]cbd4c042011-08-13 18:22:191029 input_method_manager->RegisterVirtualKeyboard(
1030 extension->url(),
1031 component->name, // human-readable name of the keyboard extension.
1032 component->layouts,
1033 is_system);
[email protected]b4d9d122011-06-17 01:58:541034 }
[email protected]14da67b02011-08-02 05:49:131035#endif
[email protected]b4d9d122011-06-17 01:58:541036 }
1037#endif
[email protected]62d30f42009-10-01 22:36:061038}
1039
[email protected]a9f39a312010-12-23 22:14:271040void ExtensionService::NotifyExtensionUnloaded(
[email protected]814a7bf0f2011-08-13 05:30:591041 const Extension* extension,
1042 extension_misc::UnloadedExtensionReason reason) {
[email protected]a9f39a312010-12-23 22:14:271043 UnloadedExtensionInfo details(extension, reason);
[email protected]ad50def52011-10-19 23:17:071044 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271045 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531046 content::Source<Profile>(profile_),
1047 content::Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061048
[email protected]f3b1a082011-11-18 00:34:301049 for (content::RenderProcessHost::iterator i(
1050 content::RenderProcessHost::AllHostsIterator());
[email protected]77a6970c2011-04-23 16:58:561051 !i.IsAtEnd(); i.Advance()) {
[email protected]f3b1a082011-11-18 00:34:301052 content::RenderProcessHost* host = i.GetCurrentValue();
[email protected]9b62ecf2011-07-27 20:23:081053 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:301054 Profile::FromBrowserContext(host->GetBrowserContext());
[email protected]9b62ecf2011-07-27 20:23:081055 if (host_profile->GetOriginalProfile() == profile_->GetOriginalProfile())
[email protected]c8d407e2011-04-28 21:27:171056 host->Send(new ExtensionMsg_Unloaded(extension->id()));
[email protected]77a6970c2011-04-23 16:58:561057 }
1058
[email protected]dd163fb02011-05-04 22:22:171059 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171060 profile_->GetExtensionSpecialStoragePolicy()->
1061 RevokeRightsForExtension(extension);
1062
1063 ExtensionWebUI::UnregisterChromeURLOverrides(
1064 profile_, extension->GetChromeURLOverrides());
1065
[email protected]b777b332011-04-16 04:01:081066#if defined(OS_CHROMEOS)
1067 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171068 if (profile_->GetFileSystemContext() &&
1069 profile_->GetFileSystemContext()->path_manager() &&
1070 profile_->GetFileSystemContext()->path_manager()->external_provider()) {
1071 profile_->GetFileSystemContext()->path_manager()->external_provider()->
1072 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061073 }
[email protected]c8d407e2011-04-28 21:27:171074#endif
1075
1076 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581077
1078 bool plugins_changed = false;
1079 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1080 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361081 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361082 base::Bind(&ForceShutdownPlugin, plugin.path)))
[email protected]14908b72011-04-20 06:54:361083 NOTREACHED();
[email protected]f520b5b2011-11-08 02:42:141084 PluginService::GetInstance()->RefreshPlugins();
1085 PluginService::GetInstance()->RemoveExtraPluginPath(plugin.path);
[email protected]2de307592011-04-05 21:16:581086 plugins_changed = true;
[email protected]476a27032011-09-13 13:40:491087 ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin.path);
[email protected]2de307592011-04-05 21:16:581088 }
[email protected]84396dbc2011-04-14 06:33:421089
1090 bool nacl_modules_changed = false;
1091 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1092 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001093 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421094 nacl_modules_changed = true;
1095 }
1096
[email protected]ed0ba002011-05-26 16:55:131097 if (nacl_modules_changed)
1098 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421099
1100 if (plugins_changed || nacl_modules_changed)
[email protected]45a22e62011-10-12 09:48:021101 PluginService::GetInstance()->PurgePluginListCache(profile_, false);
[email protected]62d30f42009-10-01 22:36:061102}
1103
[email protected]eaa7dd182010-12-14 11:09:001104void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181105 const std::vector<std::string>& blacklist) {
1106 // Use this set to indicate if an extension in the blacklist has been used.
1107 std::set<std::string> blacklist_set;
1108 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1109 if (Extension::IdIsValid(blacklist[i])) {
1110 blacklist_set.insert(blacklist[i]);
1111 }
1112 }
1113 extension_prefs_->UpdateBlacklist(blacklist_set);
1114 std::vector<std::string> to_be_removed;
1115 // Loop current extensions, unload installed extensions.
1116 for (ExtensionList::const_iterator iter = extensions_.begin();
1117 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021118 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181119 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1120 to_be_removed.push_back(extension->id());
1121 }
1122 }
1123
1124 // UnloadExtension will change the extensions_ list. So, we should
1125 // call it outside the iterator loop.
1126 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]814a7bf0f2011-08-13 05:30:591127 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]6b75ec32009-08-14 06:37:181128 }
1129}
1130
[email protected]0a071a32011-02-08 00:18:241131Profile* ExtensionService::profile() {
1132 return profile_;
1133}
1134
[email protected]eaa7dd182010-12-14 11:09:001135ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451136 return extension_prefs_;
1137}
1138
[email protected]0d9a2202011-11-09 13:48:411139extensions::SettingsFrontend* ExtensionService::settings_frontend() {
[email protected]cccdf0aa2011-11-11 03:43:381140 return settings_frontend_.get();
[email protected]b7f853e282011-08-10 09:24:201141}
1142
[email protected]b790b072011-05-20 09:46:441143ExtensionContentSettingsStore*
1144 ExtensionService::GetExtensionContentSettingsStore() {
1145 return extension_prefs()->content_settings_store();
1146}
1147
[email protected]25ae0152011-11-18 14:40:021148bool ExtensionService::is_ready() {
1149 return ready_;
1150}
1151
[email protected]2859946f2011-04-04 18:18:061152ExtensionUpdater* ExtensionService::updater() {
1153 return updater_.get();
1154}
1155
[email protected]eaa7dd182010-12-14 11:09:001156void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251157 std::vector<std::string> to_be_removed;
1158 // Loop through extensions list, unload installed extensions.
1159 for (ExtensionList::const_iterator iter = extensions_.begin();
1160 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021161 const Extension* extension = (*iter);
[email protected]05aad2da2011-10-28 10:12:371162 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id(),
1163 extension->location())) {
[email protected]aa96d3a2010-08-21 08:45:251164 to_be_removed.push_back(extension->id());
[email protected]05aad2da2011-10-28 10:12:371165 }
[email protected]aa96d3a2010-08-21 08:45:251166 }
1167
1168 // UnloadExtension will change the extensions_ list. So, we should
1169 // call it outside the iterator loop.
1170 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]814a7bf0f2011-08-13 05:30:591171 UnloadExtension(to_be_removed[i], extension_misc::UNLOAD_REASON_DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251172}
1173
[email protected]31206602011-04-13 23:07:321174void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351175 if (updater()) {
[email protected]31206602011-04-13 23:07:321176 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351177 } else {
[email protected]31206602011-04-13 23:07:321178 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351179 }
1180}
1181
[email protected]3bdba0d2011-08-23 07:17:301182namespace {
1183 bool IsSyncableNone(const Extension& extension) { return false; }
[email protected]406b5a92011-11-08 11:58:261184} // namespace
[email protected]3bdba0d2011-08-23 07:17:301185
1186ExtensionService::SyncBundle::SyncBundle()
1187 : filter(IsSyncableNone),
1188 sync_processor(NULL) {
[email protected]b05fb9ff2011-04-23 00:07:561189}
1190
[email protected]3bdba0d2011-08-23 07:17:301191ExtensionService::SyncBundle::~SyncBundle() {
1192}
1193
1194bool ExtensionService::SyncBundle::HasExtensionId(const std::string& id) const {
1195 return synced_extensions.find(id) != synced_extensions.end();
1196}
1197
1198bool ExtensionService::SyncBundle::HasPendingExtensionId(const std::string& id)
1199 const {
1200 return pending_sync_data.find(id) != pending_sync_data.end();
1201}
1202
1203void ExtensionService::SyncExtensionChangeIfNeeded(const Extension& extension) {
1204 SyncBundle* sync_bundle = GetSyncBundleForExtension(extension);
1205 if (sync_bundle) {
[email protected]aa7599d2011-10-28 07:24:321206 ExtensionSyncData extension_sync_data(
1207 extension,
1208 IsExtensionEnabled(extension.id()),
1209 IsIncognitoEnabled(extension.id()),
1210 extension_prefs_->IsAppNotificationSetupDone(extension.id()),
1211 extension_prefs_->IsAppNotificationDisabled(extension.id()));
[email protected]3bdba0d2011-08-23 07:17:301212
1213 SyncChangeList sync_change_list(1, extension_sync_data.GetSyncChange(
1214 sync_bundle->HasExtensionId(extension.id()) ?
1215 SyncChange::ACTION_UPDATE : SyncChange::ACTION_ADD));
1216 sync_bundle->sync_processor->ProcessSyncChanges(
1217 FROM_HERE, sync_change_list);
1218 sync_bundle->synced_extensions.insert(extension.id());
1219 sync_bundle->pending_sync_data.erase(extension.id());
[email protected]b05fb9ff2011-04-23 00:07:561220 }
[email protected]3bdba0d2011-08-23 07:17:301221}
1222
1223ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForExtension(
1224 const Extension& extension) {
1225 if (app_sync_bundle_.filter(extension))
1226 return &app_sync_bundle_;
1227 else if (extension_sync_bundle_.filter(extension))
1228 return &extension_sync_bundle_;
1229 else
1230 return NULL;
1231}
1232
1233ExtensionService::SyncBundle*
1234 ExtensionService::GetSyncBundleForExtensionSyncData(
1235 const ExtensionSyncData& extension_sync_data) {
1236 switch (extension_sync_data.type()) {
1237 case Extension::SYNC_TYPE_APP:
1238 return &app_sync_bundle_;
1239 case Extension::SYNC_TYPE_EXTENSION:
1240 return &extension_sync_bundle_;
1241 default:
1242 NOTREACHED();
1243 return NULL;
1244 }
1245}
1246
1247#define GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY() \
1248 do { \
1249 switch (type) { \
1250 case syncable::APPS: \
1251 return &app_sync_bundle_; \
1252 case syncable::EXTENSIONS: \
1253 return &extension_sync_bundle_; \
1254 default: \
1255 NOTREACHED(); \
1256 return NULL; \
1257 } \
1258 } while (0)
1259
1260const ExtensionService::SyncBundle*
1261 ExtensionService::GetSyncBundleForModelTypeConst(
1262 syncable::ModelType type) const {
1263 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1264}
1265
1266ExtensionService::SyncBundle* ExtensionService::GetSyncBundleForModelType(
1267 syncable::ModelType type) {
1268 GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY();
1269}
1270
1271#undef GET_SYNC_BUNDLE_FOR_MODEL_TYPE_BODY
1272
1273SyncError ExtensionService::MergeDataAndStartSyncing(
1274 syncable::ModelType type,
1275 const SyncDataList& initial_sync_data,
1276 SyncChangeProcessor* sync_processor) {
1277 CHECK(sync_processor);
1278
1279 SyncBundle* bundle = NULL;
1280
1281 switch (type) {
1282 case syncable::EXTENSIONS:
1283 bundle = &extension_sync_bundle_;
1284 bundle->filter = IsSyncableExtension;
1285 break;
1286
1287 case syncable::APPS:
1288 bundle = &app_sync_bundle_;
1289 bundle->filter = IsSyncableApp;
1290 break;
1291
1292 default:
1293 LOG(FATAL) << "Got " << type << " ModelType";
1294 }
1295
1296 bundle->sync_processor = sync_processor;
1297
1298 for (SyncDataList::const_iterator i = initial_sync_data.begin();
1299 i != initial_sync_data.end();
1300 ++i) {
1301 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1302 bundle->synced_extensions.insert(extension_sync_data.id());
1303 ProcessExtensionSyncData(extension_sync_data, *bundle);
1304 }
1305
1306 SyncDataList sync_data_list = GetAllSyncData(type);
1307 SyncChangeList sync_change_list;
1308 for (SyncDataList::const_iterator i = sync_data_list.begin();
1309 i != sync_data_list.end();
1310 ++i) {
1311 if (bundle->HasExtensionId(i->GetTag()))
1312 sync_change_list.push_back(SyncChange(SyncChange::ACTION_UPDATE, *i));
1313 else
1314 sync_change_list.push_back(SyncChange(SyncChange::ACTION_ADD, *i));
1315 }
1316 bundle->sync_processor->ProcessSyncChanges(FROM_HERE, sync_change_list);
1317
1318 return SyncError();
1319}
1320
1321void ExtensionService::StopSyncing(syncable::ModelType type) {
1322 SyncBundle* bundle = GetSyncBundleForModelType(type);
1323 CHECK(bundle);
1324 // This is the simplest way to clear out the bundle.
1325 *bundle = SyncBundle();
1326}
1327
1328SyncDataList ExtensionService::GetAllSyncData(syncable::ModelType type) const {
1329 const SyncBundle* bundle = GetSyncBundleForModelTypeConst(type);
1330 CHECK(bundle);
1331 std::vector<ExtensionSyncData> extension_sync_data = GetSyncDataList(*bundle);
1332 SyncDataList result(extension_sync_data.size());
1333 for (int i = 0; i < static_cast<int>(extension_sync_data.size()); ++i) {
1334 result[i] = extension_sync_data[i].GetSyncData();
1335 }
1336 return result;
1337}
1338
1339SyncError ExtensionService::ProcessSyncChanges(
1340 const tracked_objects::Location& from_here,
1341 const SyncChangeList& change_list) {
1342 for (SyncChangeList::const_iterator i = change_list.begin();
1343 i != change_list.end();
1344 ++i) {
1345 ExtensionSyncData extension_sync_data = ExtensionSyncData(*i);
1346 SyncBundle* bundle = GetSyncBundleForExtensionSyncData(extension_sync_data);
1347 CHECK(bundle);
1348
1349 if (extension_sync_data.uninstalled())
1350 bundle->synced_extensions.erase(extension_sync_data.id());
1351 else
1352 bundle->synced_extensions.insert(extension_sync_data.id());
1353 ProcessExtensionSyncData(extension_sync_data, *bundle);
1354 }
1355
1356 return SyncError();
[email protected]b05fb9ff2011-04-23 00:07:561357}
1358
1359void ExtensionService::GetSyncDataListHelper(
1360 const ExtensionList& extensions,
[email protected]3bdba0d2011-08-23 07:17:301361 const SyncBundle& bundle,
[email protected]b05fb9ff2011-04-23 00:07:561362 std::vector<ExtensionSyncData>* sync_data_list) const {
1363 for (ExtensionList::const_iterator it = extensions.begin();
1364 it != extensions.end(); ++it) {
1365 const Extension& extension = **it;
[email protected]3bdba0d2011-08-23 07:17:301366 if (bundle.filter(extension) &&
1367 // If we have pending extension data for this extension, then this
1368 // version is out of date. We'll sync back the version we got from
1369 // sync.
1370 !bundle.HasPendingExtensionId(extension.id())) {
[email protected]aa7599d2011-10-28 07:24:321371 sync_data_list->push_back(ExtensionSyncData(
1372 extension,
1373 IsExtensionEnabled(extension.id()),
1374 IsIncognitoEnabled(extension.id()),
1375 extension_prefs_->IsAppNotificationSetupDone(extension.id()),
1376 extension_prefs_->IsAppNotificationDisabled(extension.id())));
[email protected]b05fb9ff2011-04-23 00:07:561377 }
1378 }
1379}
1380
1381std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
[email protected]3bdba0d2011-08-23 07:17:301382 const SyncBundle& bundle) const {
1383 std::vector<ExtensionSyncData> extension_sync_list;
1384 GetSyncDataListHelper(extensions_, bundle, &extension_sync_list);
1385 GetSyncDataListHelper(disabled_extensions_, bundle, &extension_sync_list);
1386 GetSyncDataListHelper(terminated_extensions_, bundle, &extension_sync_list);
1387
1388 for (std::map<std::string, ExtensionSyncData>::const_iterator i =
1389 bundle.pending_sync_data.begin();
1390 i != bundle.pending_sync_data.end();
1391 ++i) {
1392 extension_sync_list.push_back(i->second);
1393 }
1394
1395 return extension_sync_list;
[email protected]b05fb9ff2011-04-23 00:07:561396}
1397
[email protected]3bdba0d2011-08-23 07:17:301398void ExtensionService::ProcessExtensionSyncData(
[email protected]90310d92011-04-17 07:35:041399 const ExtensionSyncData& extension_sync_data,
[email protected]3bdba0d2011-08-23 07:17:301400 SyncBundle& bundle) {
1401 const std::string& id = extension_sync_data.id();
[email protected]96e989b2011-08-30 19:35:061402 const Extension* extension = GetInstalledExtension(id);
1403
1404 // TODO(bolms): we should really handle this better. The particularly bad
1405 // case is where an app becomes an extension or vice versa, and we end up with
1406 // a zombie extension that won't go away.
1407 if (extension && !bundle.filter(*extension))
1408 return;
[email protected]90310d92011-04-17 07:35:041409
1410 // Handle uninstalls first.
[email protected]3bdba0d2011-08-23 07:17:301411 if (extension_sync_data.uninstalled()) {
[email protected]90310d92011-04-17 07:35:041412 std::string error;
1413 if (!UninstallExtensionHelper(this, id)) {
1414 LOG(WARNING) << "Could not uninstall extension " << id
1415 << " for sync";
1416 }
1417 return;
1418 }
1419
[email protected]06f92562011-04-29 19:27:311420 // Set user settings.
[email protected]3bdba0d2011-08-23 07:17:301421 if (extension_sync_data.enabled()) {
[email protected]06f92562011-04-29 19:27:311422 EnableExtension(id);
1423 } else {
1424 DisableExtension(id);
1425 }
[email protected]81b14cc2011-04-29 00:39:371426
[email protected]406b5a92011-11-08 11:58:261427 // We need to cache some version information here because setting the
1428 // incognito flag invalidates the |extension| pointer (it reloads the
1429 // extension).
1430 bool extension_installed = extension != NULL;
1431 int result = extension ?
1432 extension->version()->CompareTo(extension_sync_data.version()) : 0;
1433 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled());
1434 extension = NULL; // No longer safe to use.
1435
1436 if (extension_installed) {
[email protected]06f92562011-04-29 19:27:311437 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041438 if (result < 0) {
1439 // Extension is outdated.
[email protected]3bdba0d2011-08-23 07:17:301440 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]90310d92011-04-17 07:35:041441 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041442 }
[email protected]06f92562011-04-29 19:27:311443 } else {
[email protected]06f92562011-04-29 19:27:311444 // TODO(akalin): Replace silent update with a list of enabled
1445 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331446 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311447 if (!pending_extension_manager()->AddFromSync(
1448 id,
[email protected]3bdba0d2011-08-23 07:17:301449 extension_sync_data.update_url(),
1450 bundle.filter,
[email protected]6cc7dbae2011-04-29 21:18:331451 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311452 LOG(WARNING) << "Could not add pending extension for " << id;
[email protected]3bdba0d2011-08-23 07:17:301453 // This means that the extension is already pending installation, with a
1454 // non-INTERNAL location. Add to pending_sync_data, even though it will
1455 // never be removed (we'll never install a syncable version of the
1456 // extension), so that GetAllSyncData() continues to send it.
[email protected]06f92562011-04-29 19:27:311457 }
[email protected]3bdba0d2011-08-23 07:17:301458 // Track pending extensions so that we can return them in GetAllSyncData().
1459 bundle.pending_sync_data[extension_sync_data.id()] = extension_sync_data;
[email protected]06f92562011-04-29 19:27:311460 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041461 }
[email protected]90310d92011-04-17 07:35:041462}
1463
[email protected]c3cfb012011-04-06 22:07:351464bool ExtensionService::IsIncognitoEnabled(
1465 const std::string& extension_id) const {
1466 // If this is an existing component extension we always allow it to
1467 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371468 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351469 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411470 return true;
1471
1472 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351473 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501474}
[email protected]55a35692010-02-11 23:25:211475
[email protected]c3cfb012011-04-06 22:07:351476void ExtensionService::SetIsIncognitoEnabled(
1477 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371478 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351479 if (extension && extension->location() == Extension::COMPONENT) {
1480 // This shouldn't be called for component extensions.
1481 NOTREACHED();
1482 return;
1483 }
1484
[email protected]8c6c8c22011-03-09 12:52:341485 // Broadcast unloaded and loaded events to update browser state. Only bother
1486 // if the value changed and the extension is actually enabled, since there is
1487 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351488 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341489 if (enabled == old_enabled)
1490 return;
1491
[email protected]c3cfb012011-04-06 22:07:351492 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371493
[email protected]406b5a92011-11-08 11:58:261494 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
1495 extension) != extensions_.end();
[email protected]bd9892eb2011-11-11 19:41:341496
1497 // When we reload the extension the ID may be invalidated if we've passed it
1498 // by const ref everywhere. Make a copy to be safe.
1499 std::string id = extension_id;
[email protected]406b5a92011-11-08 11:58:261500 if (extension_is_enabled)
1501 ReloadExtension(extension->id());
[email protected]3bdba0d2011-08-23 07:17:301502
[email protected]406b5a92011-11-08 11:58:261503 // Reloading the extension invalidates the |extension| pointer.
[email protected]bd9892eb2011-11-11 19:41:341504 extension = GetInstalledExtension(id);
[email protected]3bdba0d2011-08-23 07:17:301505 if (extension)
1506 SyncExtensionChangeIfNeeded(*extension);
[email protected]55a35692010-02-11 23:25:211507}
1508
[email protected]aa7599d2011-10-28 07:24:321509void ExtensionService::SetAppNotificationSetupDone(
1510 const std::string& extension_id,
1511 bool value) {
1512 const Extension* extension = GetInstalledExtension(extension_id);
1513 // This method is called when the user sets up app notifications.
1514 // So it is not expected to be called until the extension is installed.
1515 if (!extension) {
1516 NOTREACHED();
1517 return;
1518 }
1519 extension_prefs_->SetAppNotificationSetupDone(extension_id, value);
1520 SyncExtensionChangeIfNeeded(*extension);
1521}
1522
1523void ExtensionService::SetAppNotificationDisabled(
1524 const std::string& extension_id,
1525 bool value) {
1526 const Extension* extension = GetInstalledExtension(extension_id);
1527 // This method is called when the user enables/disables app notifications.
1528 // So it is not expected to be called until the extension is installed.
1529 if (!extension) {
1530 NOTREACHED();
1531 return;
1532 }
1533
1534 extension_prefs_->SetAppNotificationDisabled(extension_id, value);
1535 SyncExtensionChangeIfNeeded(*extension);
1536}
1537
[email protected]eaa7dd182010-12-14 11:09:001538bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051539 // We allow the extension to see events and data from another profile iff it
1540 // uses "spanning" behavior and it has incognito access. "split" mode
1541 // extensions only see events for a matching profile.
[email protected]0c41298f2011-09-13 21:17:221542 CHECK(extension);
[email protected]c3cfb012011-04-06 22:07:351543 return IsIncognitoEnabled(extension->id()) &&
1544 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051545}
1546
[email protected]5ef7b002011-05-12 23:16:201547bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1548 if (extension->is_hosted_app())
1549 return true;
1550 // Packaged apps and regular extensions need to be enabled specifically for
1551 // incognito (and split mode should be set).
1552 return extension->incognito_split_mode() &&
1553 IsIncognitoEnabled(extension->id());
1554}
1555
[email protected]eaa7dd182010-12-14 11:09:001556bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081557 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441558 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081559 extension_prefs_->AllowFileAccess(extension->id()));
1560}
1561
[email protected]eaa7dd182010-12-14 11:09:001562void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481563 bool allow) {
1564 // Reload to update browser state. Only bother if the value changed and the
1565 // extension is actually enabled, since there is no UI otherwise.
1566 bool old_allow = AllowFileAccess(extension);
1567 if (allow == old_allow)
1568 return;
1569
[email protected]05c82182010-06-24 17:49:081570 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481571
1572 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
1573 extension) != extensions_.end();
1574 if (extension_is_enabled)
1575 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081576}
1577
[email protected]eaa7dd182010-12-14 11:09:001578bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391579 return extension_prefs_->GetBrowserActionVisibility(extension);
1580}
1581
[email protected]eaa7dd182010-12-14 11:09:001582void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141583 bool visible) {
[email protected]f681c782010-11-19 11:19:391584 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1585}
1586
[email protected]8e4560b62011-01-14 10:09:141587// Some extensions will autoupdate themselves externally from Chrome. These
1588// are typically part of some larger client application package. To support
1589// these, the extension will register its location in the the preferences file
1590// (and also, on Windows, in the registry) and this code will periodically
1591// check that location for a .crx file, which it will then install locally if
1592// a new version is available.
1593// Errors are reported through ExtensionErrorReporter. Succcess is not
1594// reported.
[email protected]eaa7dd182010-12-14 11:09:001595void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121596 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141597
1598 // Note that this installation is intentionally silent (since it didn't
1599 // go through the front-end). Extensions that are registered in this
1600 // way are effectively considered 'pre-bundled', and so implicitly
1601 // trusted. In general, if something has HKLM or filesystem access,
1602 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121603
1604 // If any external extension records give a URL, a provider will set
1605 // this to true. Used by OnExternalProviderReady() to see if we need
1606 // to start an update check to fetch a new external extension.
1607 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141608
1609 // Ask each external extension provider to give us a call back for each
1610 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1611 ProviderCollection::const_iterator i;
1612 for (i = external_extension_providers_.begin();
1613 i != external_extension_providers_.end(); ++i) {
1614 ExternalExtensionProviderInterface* provider = i->get();
1615 provider->VisitRegisteredExtension();
1616 }
1617
[email protected]50067e52011-10-20 23:17:071618 // Do any required work that we would have done after completion of all
1619 // providers.
1620 if (external_extension_providers_.empty()) {
1621 OnAllExternalProvidersReady();
1622 }
[email protected]9f1087e2009-06-15 17:29:321623}
1624
[email protected]50067e52011-10-20 23:17:071625void ExtensionService::OnExternalProviderReady(
1626 const ExternalExtensionProviderInterface* provider) {
[email protected]a29a517a2011-01-21 21:11:121627 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]50067e52011-10-20 23:17:071628 CHECK(provider->IsReady());
[email protected]a29a517a2011-01-21 21:11:121629
1630 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141631 // if all of them are finished. So we check them first.
1632 ProviderCollection::const_iterator i;
1633 for (i = external_extension_providers_.begin();
1634 i != external_extension_providers_.end(); ++i) {
1635 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121636 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141637 return;
[email protected]8e4560b62011-01-14 10:09:141638 }
1639
[email protected]50067e52011-10-20 23:17:071640 OnAllExternalProvidersReady();
1641}
1642
1643void ExtensionService::OnAllExternalProvidersReady() {
1644 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1645
1646 // Install any pending extensions.
[email protected]a29a517a2011-01-21 21:11:121647 if (external_extension_url_added_ && updater()) {
1648 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141649 updater()->CheckNow();
1650 }
1651
1652 // Uninstall all the unclaimed extensions.
1653 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1654 extension_prefs_->GetInstalledExtensionsInfo());
1655 for (size_t i = 0; i < extensions_info->size(); ++i) {
1656 ExtensionInfo* info = extensions_info->at(i).get();
1657 if (Extension::IsExternalLocation(info->extension_location))
1658 CheckExternalUninstall(info->extension_id);
1659 }
[email protected]e5af875f2011-10-10 21:09:141660 IdentifyAlertableExtensions();
[email protected]e5af875f2011-10-10 21:09:141661}
1662
1663void ExtensionService::IdentifyAlertableExtensions() {
1664 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1665
[email protected]08b83362011-10-19 05:23:171666 if (!CommandLine::ForCurrentProcess()->HasSwitch(
1667 switches::kEnableExtensionAlerts)) {
1668 return; // TODO(miket): enable unconditionally when done.
1669 }
1670
[email protected]e5af875f2011-10-10 21:09:141671 // Build up the lists of extensions that require acknowledgment.
1672 // If this is the first time, grandfather extensions that would have
1673 // caused notification.
1674 scoped_ptr<ExtensionGlobalError> global_error(
[email protected]d8c8f25f2011-11-02 18:18:011675 new ExtensionGlobalError(AsWeakPtr()));
[email protected]e5af875f2011-10-10 21:09:141676 bool needs_alert = false;
1677 for (ExtensionList::const_iterator iter = extensions_.begin();
1678 iter != extensions_.end(); ++iter) {
1679 const Extension* e = *iter;
1680 if (!IsExtensionEnabled(e->id())) {
1681 continue;
1682 }
1683 if (Extension::IsExternalLocation(e->location())) {
1684 if (!extension_prefs_->IsExternalExtensionAcknowledged(e->id())) {
1685 global_error->AddExternalExtension(e->id());
1686 needs_alert = true;
1687 }
1688 }
1689 if (extension_prefs_->IsExtensionBlacklisted(e->id())) {
1690 if (!extension_prefs_->IsBlacklistedExtensionAcknowledged(e->id())) {
1691 global_error->AddBlacklistedExtension(e->id());
1692 needs_alert = true;
1693 }
1694 }
1695 if (extension_prefs_->IsExtensionOrphaned(e->id())) {
1696 if (!extension_prefs_->IsOrphanedExtensionAcknowledged(e->id())) {
1697 global_error->AddOrphanedExtension(e->id());
1698 needs_alert = true;
1699 }
1700 }
1701 }
1702
1703 if (needs_alert) {
1704 if (extension_prefs_->SetAlertSystemFirstRun()) {
1705 global_error->set_accept_callback(
1706 base::Bind(&ExtensionService::HandleExtensionAlertAccept,
1707 base::Unretained(this)));
1708 global_error->set_cancel_callback(
1709 base::Bind(&ExtensionService::HandleExtensionAlertDetails,
1710 base::Unretained(this)));
[email protected]62433d32011-10-12 22:33:121711 ShowExtensionAlert(global_error.release());
[email protected]e5af875f2011-10-10 21:09:141712 } else {
1713 // First run. Just acknowledge all the extensions, silently, by
1714 // shortcutting the display of the UI and going straight to the
1715 // callback for pressing the Accept button.
[email protected]62433d32011-10-12 22:33:121716 HandleExtensionAlertAccept(*global_error.get(), NULL);
[email protected]e5af875f2011-10-10 21:09:141717 }
1718 }
1719}
1720
[email protected]62433d32011-10-12 22:33:121721void ExtensionService::ShowExtensionAlert(ExtensionGlobalError* global_error) {
1722 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1723 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_);
1724 if (browser) {
1725 global_error->ShowBubbleView(browser);
1726 }
1727}
1728
[email protected]e5af875f2011-10-10 21:09:141729void ExtensionService::HandleExtensionAlertAccept(
[email protected]62433d32011-10-12 22:33:121730 const ExtensionGlobalError& global_error, Browser* browser) {
[email protected]e5af875f2011-10-10 21:09:141731 const ExtensionIdSet *extension_ids =
1732 global_error.get_external_extension_ids();
1733 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1734 iter != extension_ids->end(); ++iter) {
1735 extension_prefs_->AcknowledgeExternalExtension(*iter);
1736 }
1737 extension_ids = global_error.get_blacklisted_extension_ids();
1738 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1739 iter != extension_ids->end(); ++iter) {
1740 extension_prefs_->AcknowledgeBlacklistedExtension(*iter);
1741 }
1742 extension_ids = global_error.get_orphaned_extension_ids();
1743 for (ExtensionIdSet::const_iterator iter = extension_ids->begin();
1744 iter != extension_ids->end(); ++iter) {
1745 extension_prefs_->AcknowledgeOrphanedExtension(*iter);
1746 }
1747}
1748
1749void ExtensionService::HandleExtensionAlertDetails(
[email protected]62433d32011-10-12 22:33:121750 const ExtensionGlobalError& global_error, Browser* browser) {
1751 if (browser) {
1752 browser->ShowExtensionsTab();
1753 }
[email protected]6c751e72010-11-23 10:11:101754}
1755
[email protected]a9f39a312010-12-23 22:14:271756void ExtensionService::UnloadExtension(
1757 const std::string& extension_id,
[email protected]814a7bf0f2011-08-13 05:30:591758 extension_misc::UnloadedExtensionReason reason) {
[email protected]27e469a2010-01-11 20:35:091759 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021760 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471761 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251762
[email protected]fa0c96732010-11-17 00:14:231763 // This method can be called via PostTask, so the extension may have been
1764 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171765 if (!extension) {
1766 // In case the extension may have crashed/uninstalled. Allow the profile to
1767 // clean up its RequestContexts.
1768 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231769 return;
[email protected]dd163fb02011-05-04 22:22:171770 }
[email protected]0c6da502009-08-14 22:32:391771
[email protected]1eb175082010-02-10 09:26:161772 // Keep information about the extension so that we can reload it later
1773 // even if it's not permanently installed.
1774 unloaded_extension_paths_[extension->id()] = extension->path();
1775
[email protected]f17dbd42010-08-16 23:21:101776 // Clean up if the extension is meant to be enabled after a reload.
1777 disabled_extension_paths_.erase(extension->id());
1778
[email protected]d7e9a862010-11-03 21:57:491779 // Clean up runtime data.
1780 extension_runtime_data_.erase(extension_id);
1781
[email protected]0c6da502009-08-14 22:32:391782 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1783 disabled_extensions_.end(),
1784 extension.get());
1785 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:271786 UnloadedExtensionInfo details(extension, reason);
1787 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:391788 disabled_extensions_.erase(iter);
[email protected]ad50def52011-10-19 23:17:071789 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271790 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]6c2381d2011-10-19 02:52:531791 content::Source<Profile>(profile_),
1792 content::Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171793 // Make sure the profile cleans up its RequestContexts when an already
1794 // disabled extension is unloaded (since they are also tracking the disabled
1795 // extensions).
1796 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391797 return;
1798 }
1799
1800 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:571801
[email protected]631cf822009-05-15 07:01:251802 // Remove the extension from our list.
1803 extensions_.erase(iter);
1804
[email protected]a9f39a312010-12-23 22:14:271805 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251806}
1807
[email protected]eaa7dd182010-12-14 11:09:001808void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171809 profile_->GetExtensionSpecialStoragePolicy()->
1810 RevokeRightsForAllExtensions();
1811
[email protected]9f1087e2009-06-15 17:29:321812 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:321813 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:201814 terminated_extension_ids_.clear();
1815 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:491816 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321817
[email protected]c6e4a3412009-06-24 15:45:291818 // TODO(erikkay) should there be a notification for this? We can't use
1819 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1820 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321821}
1822
[email protected]eaa7dd182010-12-14 11:09:001823void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321824 UnloadAllExtensions();
[email protected]d8c8f25f2011-11-02 18:18:011825 component_loader_->LoadAll();
1826 extensions::InstalledLoader(this).LoadAllExtensions();
[email protected]9f1087e2009-06-15 17:29:321827}
1828
[email protected]eaa7dd182010-12-14 11:09:001829void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501830 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391831 return;
1832
[email protected]ca3dbf52010-05-19 22:27:061833 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1834 extension_prefs_->GetInstalledExtensionsInfo());
1835
1836 std::map<std::string, FilePath> extension_paths;
1837 for (size_t i = 0; i < info->size(); ++i)
1838 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1839
[email protected]14908b72011-04-20 06:54:361840 if (!BrowserThread::PostTask(
1841 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:361842 base::Bind(
[email protected]14908b72011-04-20 06:54:361843 &extension_file_util::GarbageCollectExtensions,
1844 install_directory_,
1845 extension_paths)))
1846 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341847
1848 // Also garbage-collect themes. We check |profile_| to be
1849 // defensive; in the future, we may call GarbageCollectExtensions()
1850 // from somewhere other than Init() (e.g., in a timer).
1851 if (profile_) {
[email protected]18280372011-03-22 18:05:221852 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341853 }
[email protected]3cf4f0992009-02-03 23:00:301854}
1855
[email protected]eaa7dd182010-12-14 11:09:001856void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171857 if (updater_.get()) {
1858 updater_->Start();
1859 }
[email protected]fa6a9102010-11-22 15:38:501860
1861 ready_ = true;
[email protected]ad50def52011-10-19 23:17:071862 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271863 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]6c2381d2011-10-19 02:52:531864 content::Source<Profile>(profile_),
[email protected]ad50def52011-10-19 23:17:071865 content::NotificationService::NoDetails());
[email protected]e72e8eb82009-06-18 17:21:511866}
1867
[email protected]49cd1572011-02-08 21:38:451868void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461869 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021870 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321871
[email protected]c8d407e2011-04-28 21:27:171872 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1873 // default extension state to DISABLED when the --disable-extensions flag
1874 // is set (http://crbug.com/29067).
1875 if (!extensions_enabled() &&
1876 !extension->is_theme() &&
1877 extension->location() != Extension::COMPONENT &&
1878 !Extension::IsExternalLocation(extension->location()))
1879 return;
1880
1881 SetBeingUpgraded(extension, false);
1882
[email protected]1eb175082010-02-10 09:26:161883 // The extension is now loaded, remove its data from unloaded extension map.
1884 unloaded_extension_paths_.erase(extension->id());
1885
[email protected]bb7f40952011-01-13 00:21:201886 // If a terminated extension is loaded, remove it from the terminated list.
1887 UntrackTerminatedExtension(extension->id());
1888
[email protected]f17dbd42010-08-16 23:21:101889 // If the extension was disabled for a reload, then enable it.
1890 if (disabled_extension_paths_.erase(extension->id()) > 0)
1891 EnableExtension(extension->id());
1892
[email protected]d728e002010-12-08 04:46:231893 // Check if the extension's privileges have changed and disable the
1894 // extension if necessary.
[email protected]902fd7b2011-07-27 18:42:311895 InitializePermissions(extension);
[email protected]0c6da502009-08-14 22:32:391896
[email protected]ad83ca242011-07-29 01:32:251897 bool disabled = extension_prefs_->IsExtensionDisabled(extension->id());
[email protected]36429da2011-07-11 20:25:181898 if (disabled) {
[email protected]c8d407e2011-04-28 21:27:171899 disabled_extensions_.push_back(scoped_extension);
1900 // TODO(aa): This seems dodgy. It seems that AddExtension() could get called
1901 // with a disabled extension for other reasons other than that an update was
1902 // disabled.
[email protected]ad50def52011-10-19 23:17:071903 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271904 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]6c2381d2011-10-19 02:52:531905 content::Source<Profile>(profile_),
1906 content::Details<const Extension>(extension));
[email protected]3bdba0d2011-08-23 07:17:301907 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:171908 return;
[email protected]e72e8eb82009-06-18 17:21:511909 }
[email protected]aab98a52009-12-02 03:22:351910
[email protected]c8d407e2011-04-28 21:27:171911 extensions_.push_back(scoped_extension);
[email protected]3bdba0d2011-08-23 07:17:301912 SyncExtensionChangeIfNeeded(*extension);
[email protected]c8d407e2011-04-28 21:27:171913 NotifyExtensionLoaded(extension);
[email protected]d1282c22011-10-18 02:00:391914 IdentifyAlertableExtensions();
[email protected]aab98a52009-12-02 03:22:351915}
1916
[email protected]902fd7b2011-07-27 18:42:311917void ExtensionService::InitializePermissions(const Extension* extension) {
1918 // If the extension has used the optional permissions API, it will have a
1919 // custom set of active permissions defined in the extension prefs. Here,
1920 // we update the extension's active permissions based on the prefs.
1921 scoped_refptr<ExtensionPermissionSet> active_permissions =
1922 extension_prefs()->GetActivePermissions(extension->id());
1923
1924 if (active_permissions.get()) {
1925 // We restrict the active permissions to be within the bounds defined in the
1926 // extension's manifest.
1927 // a) active permissions must be a subset of optional + default permissions
1928 // b) active permissions must contains all default permissions
1929 scoped_refptr<ExtensionPermissionSet> total_permissions =
1930 ExtensionPermissionSet::CreateUnion(
1931 extension->required_permission_set(),
1932 extension->optional_permission_set());
1933
1934 // Make sure the active permissions contain no more than optional + default.
1935 scoped_refptr<ExtensionPermissionSet> adjusted_active =
1936 ExtensionPermissionSet::CreateIntersection(
1937 total_permissions.get(), active_permissions.get());
1938
1939 // Make sure the active permissions contain the default permissions.
1940 adjusted_active = ExtensionPermissionSet::CreateUnion(
1941 extension->required_permission_set(), adjusted_active.get());
1942
1943 UpdateActivePermissions(extension, adjusted_active);
1944 }
1945
[email protected]8d888c12010-11-30 00:00:251946 // We keep track of all permissions the user has granted each extension.
1947 // This allows extensions to gracefully support backwards compatibility
1948 // by including unknown permissions in their manifests. When the user
1949 // installs the extension, only the recognized permissions are recorded.
1950 // When the unknown permissions become recognized (e.g., through browser
1951 // upgrade), we can prompt the user to accept these new permissions.
1952 // Extensions can also silently upgrade to less permissions, and then
1953 // silently upgrade to a version that adds these permissions back.
1954 //
1955 // For example, pretend that Chrome 10 includes a permission "omnibox"
1956 // for an API that adds suggestions to the omnibox. An extension can
1957 // maintain backwards compatibility while still having "omnibox" in the
1958 // manifest. If a user installs the extension on Chrome 9, the browser
1959 // will record the permissions it recognized, not including "omnibox."
1960 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1961 // will disable the extension and prompt the user to approve the increase
1962 // in privileges. The extension could then release a new version that
1963 // removes the "omnibox" permission. When the user upgrades, Chrome will
1964 // still remember that "omnibox" had been granted, so that if the
1965 // extension once again includes "omnibox" in an upgrade, the extension
1966 // can upgrade without requiring this user's approval.
1967 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:471968 true, true, false);
[email protected]8d888c12010-11-30 00:00:251969 bool is_extension_upgrade = old != NULL;
1970 bool is_privilege_increase = false;
1971
[email protected]902fd7b2011-07-27 18:42:311972 // We only need to compare the granted permissions to the current permissions
1973 // if the extension is not allowed to silently increase its permissions.
1974 if (!extension->CanSilentlyIncreasePermissions()) {
[email protected]8d888c12010-11-30 00:00:251975 // Add all the recognized permissions if the granted permissions list
1976 // hasn't been initialized yet.
[email protected]902fd7b2011-07-27 18:42:311977 scoped_refptr<ExtensionPermissionSet> granted_permissions =
1978 extension_prefs_->GetGrantedPermissions(extension->id());
[email protected]0d3e4a22011-06-23 19:02:521979 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251980
1981 // Here, we check if an extension's privileges have increased in a manner
1982 // that requires the user's approval. This could occur because the browser
1983 // upgraded and recognized additional privileges, or an extension upgrades
1984 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:521985 is_privilege_increase =
[email protected]902fd7b2011-07-27 18:42:311986 granted_permissions->HasLessPrivilegesThan(
1987 extension->GetActivePermissions());
[email protected]8d888c12010-11-30 00:00:251988 }
1989
1990 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:251991 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1992 // that we aren't downgrading.
1993 if (extension->location() != Extension::LOAD)
1994 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:251995
1996 // Extensions get upgraded if the privileges are allowed to increase or
1997 // the privileges haven't increased.
1998 if (!is_privilege_increase) {
1999 SetBeingUpgraded(old, true);
2000 SetBeingUpgraded(extension, true);
2001 }
2002
2003 // To upgrade an extension in place, unload the old one and
2004 // then load the new one.
[email protected]814a7bf0f2011-08-13 05:30:592005 UnloadExtension(old->id(), extension_misc::UNLOAD_REASON_UPDATE);
[email protected]8d888c12010-11-30 00:00:252006 old = NULL;
2007 }
2008
2009 // Extension has changed permissions significantly. Disable it. A
2010 // notification should be sent by the caller.
2011 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492012 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2013 RecordPermissionMessagesHistogram(
2014 extension, "Extensions.Permissions_AutoDisable");
2015 }
[email protected]b3317ad2011-04-28 23:46:002016 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252017 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
2018 }
2019}
2020
[email protected]eaa7dd182010-12-14 11:09:002021void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392022 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:352023 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:342024 if (!extensions_[i]->is_theme() &&
2025 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:392026 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:352027 }
2028
2029 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252030}
2031
[email protected]8266d662011-07-12 21:53:262032void ExtensionService::OnExtensionInstalled(
[email protected]cc829cf2011-08-22 21:01:352033 const Extension* extension, bool from_webstore, int page_index) {
[email protected]a29a517a2011-01-21 21:11:122034 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202035
[email protected]4416c5a2010-06-26 01:28:572036 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022037 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352038 const std::string& id = extension->id();
[email protected]8a87a5332011-08-11 17:54:592039 // Extensions installed by policy can't be disabled. So even if a previous
2040 // installation disabled the extension, make sure it is now enabled.
2041 bool initial_enable =
2042 !extension_prefs_->IsExtensionDisabled(id) ||
2043 !Extension::UserMayDisable(extension->location());
[email protected]b2907fd2011-03-25 16:43:372044 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352045 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2046 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322047
[email protected]483d1ff2011-03-03 17:12:022048 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572049 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542050 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352051 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082052 << " and update URL " << extension->update_url().spec()
2053 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542054
[email protected]ad50def52011-10-19 23:17:072055 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272056 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]6c2381d2011-10-19 02:52:532057 content::Source<Profile>(profile_),
2058 content::Details<const Extension>(extension));
[email protected]ae6cb3022011-04-04 20:11:542059
[email protected]4416c5a2010-06-26 01:28:572060 // Delete the extension directory since we're not going to
2061 // load it.
[email protected]14908b72011-04-20 06:54:362062 if (!BrowserThread::PostTask(
2063 BrowserThread::FILE, FROM_HERE,
[email protected]53612e82011-10-18 18:00:362064 base::Bind(&extension_file_util::DeleteFile,
2065 extension->path(), true)))
[email protected]14908b72011-04-20 06:54:362066 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572067 return;
2068 }
[email protected]4416c5a2010-06-26 01:28:572069 } else {
[email protected]c3cfb012011-04-06 22:07:352070 // We explicitly want to re-enable an uninstalled external
2071 // extension; if we're here, that means the user is manually
2072 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332073 if (IsExternalExtensionUninstalled(id)) {
2074 initial_enable = true;
2075 }
[email protected]aa142702010-03-26 01:26:332076 }
2077
[email protected]07533022011-06-27 20:42:552078 // Do not record the install histograms for upgrades.
2079 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2080 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2081 extension->GetType(), 100);
2082 RecordPermissionMessagesHistogram(
2083 extension, "Extensions.Permissions_Install");
2084 }
2085
[email protected]4416c5a2010-06-26 01:28:572086 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262087 extension,
2088 initial_enable ? Extension::ENABLED : Extension::DISABLED,
[email protected]cc829cf2011-08-22 21:01:352089 from_webstore,
2090 page_index);
[email protected]25b34332009-06-05 21:53:192091
[email protected]cdfca9702011-08-08 16:07:012092 // Unpacked extensions default to allowing file access, but if that has been
2093 // overridden, don't reset the value.
[email protected]b6e64fd2011-08-09 19:49:192094 if (Extension::ShouldAlwaysAllowFileAccess(extension->location()) &&
[email protected]cdfca9702011-08-08 16:07:012095 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2096 extension_prefs_->SetAllowFileAccess(id, true);
2097 }
2098
[email protected]b6cf240f2011-10-15 22:09:532099 // If the extension should automatically block network startup (e.g., it uses
2100 // the webRequest API), set the preference. Otherwise clear it, in case the
2101 // extension stopped using a relevant API.
2102 extension_prefs_->SetDelaysNetworkRequests(
2103 extension->id(), extension->ImplicitlyDelaysNetworkStartup());
2104
[email protected]ad50def52011-10-19 23:17:072105 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272106 chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]6c2381d2011-10-19 02:52:532107 content::Source<Profile>(profile_),
2108 content::Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:582109
[email protected]49cd1572011-02-08 21:38:452110 // Transfer ownership of |extension| to AddExtension.
2111 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422112}
2113
[email protected]eaa7dd182010-12-14 11:09:002114const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472115 const std::string& id, bool include_enabled, bool include_disabled,
2116 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052117 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392118 if (include_enabled) {
2119 for (ExtensionList::const_iterator iter = extensions_.begin();
2120 iter != extensions_.end(); ++iter) {
2121 if ((*iter)->id() == lowercase_id)
2122 return *iter;
2123 }
2124 }
2125 if (include_disabled) {
2126 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
2127 iter != disabled_extensions_.end(); ++iter) {
2128 if ((*iter)->id() == lowercase_id)
2129 return *iter;
2130 }
[email protected]ce5c4502009-05-06 16:46:112131 }
[email protected]8001df22011-04-28 19:59:472132 if (include_terminated) {
2133 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
2134 iter != terminated_extensions_.end(); ++iter) {
2135 if ((*iter)->id() == lowercase_id)
2136 return *iter;
2137 }
2138 }
[email protected]ce5c4502009-05-06 16:46:112139 return NULL;
2140}
2141
[email protected]bb7f40952011-01-13 00:21:202142void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
2143 if (terminated_extension_ids_.insert(extension->id()).second)
2144 terminated_extensions_.push_back(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202145
[email protected]b3f7fe22011-11-11 19:27:562146 // TODO(yoz): Listen to navcontrollers for that extension. Is this a todo?
2147
2148 // TODO(yoz): make sure this is okay in *ALL* the listeners!
2149 UnloadExtension(extension->id(), extension_misc::UNLOAD_REASON_TERMINATE);
[email protected]bb7f40952011-01-13 00:21:202150}
2151
2152void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202153 std::string lowercase_id = StringToLowerASCII(id);
2154 if (terminated_extension_ids_.erase(lowercase_id) <= 0)
[email protected]bb7f40952011-01-13 00:21:202155 return;
2156
[email protected]bb7f40952011-01-13 00:21:202157 for (ExtensionList::iterator iter = terminated_extensions_.begin();
2158 iter != terminated_extensions_.end(); ++iter) {
2159 if ((*iter)->id() == lowercase_id) {
2160 terminated_extensions_.erase(iter);
2161 return;
2162 }
2163 }
2164}
2165
[email protected]0dfe05c2011-02-23 23:03:362166const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472167 const std::string& id) const {
2168 return GetExtensionByIdInternal(id, false, false, true);
2169}
2170
2171const Extension* ExtensionService::GetInstalledExtension(
2172 const std::string& id) const {
2173 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362174}
2175
[email protected]eaa7dd182010-12-14 11:09:002176const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:072177 return GetExtensionById(extension_misc::kWebStoreAppId, false);
2178}
2179
[email protected]eaa7dd182010-12-14 11:09:002180const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572181 return url.scheme() != chrome::kExtensionScheme ? NULL :
2182 GetExtensionById(url.host(), false);
2183}
2184
[email protected]eaa7dd182010-12-14 11:09:002185const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572186 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]cced75a2011-05-20 08:31:122187 if (extensions_[i]->web_extent().MatchesURL(url))
[email protected]a888b29e62010-04-01 13:38:572188 return extensions_[i];
2189 }
2190 return NULL;
[email protected]9f1087e2009-06-15 17:29:322191}
2192
[email protected]f9c292b32011-09-13 16:14:052193const Extension* ExtensionService::GetDisabledExtensionByWebExtent(
2194 const GURL& url) {
2195 for (size_t i = 0; i < disabled_extensions_.size(); ++i) {
2196 if (disabled_extensions_[i]->web_extent().MatchesURL(url))
2197 return disabled_extensions_[i];
2198 }
2199 return NULL;
2200}
2201
[email protected]eaa7dd182010-12-14 11:09:002202bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]056ad2a2011-07-12 02:13:552203 // Allow bindings for all packaged extensions.
2204 // Note that GetExtensionByURL may return an Extension for hosted apps
[email protected]15877ca2011-11-18 22:40:522205 // (excluding bookmark apps) if the URL came from GetEffectiveURL.
[email protected]056ad2a2011-07-12 02:13:552206 const Extension* extension = GetExtensionByURL(url);
2207 if (extension && extension->GetType() != Extension::TYPE_HOSTED_APP)
[email protected]583d45c12010-08-31 02:48:122208 return true;
2209
2210 // Allow bindings for all component, hosted apps.
[email protected]056ad2a2011-07-12 02:13:552211 if (!extension)
2212 extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:122213 return (extension && extension->location() == Extension::COMPONENT);
2214}
2215
[email protected]eaa7dd182010-12-14 11:09:002216const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]cced75a2011-05-20 08:31:122217 const URLPatternSet& extent) {
[email protected]22c966c2010-06-26 06:35:022218 for (size_t i = 0; i < extensions_.size(); ++i) {
2219 if (extensions_[i]->web_extent().OverlapsWith(extent))
2220 return extensions_[i];
2221 }
2222
[email protected]6d2e60bd2010-06-03 22:37:392223 return NULL;
2224}
2225
[email protected]eaa7dd182010-12-14 11:09:002226const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472227 const std::string& extension_id) {
2228 return omnibox_icon_manager_.GetIcon(extension_id);
2229}
2230
[email protected]eaa7dd182010-12-14 11:09:002231const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312232 const std::string& extension_id) {
2233 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2234}
2235
[email protected]eaa7dd182010-12-14 11:09:002236void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322237 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142238 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322239 const FilePath& path,
[email protected]1bf73cc32011-10-26 22:38:312240 Extension::Location location,
2241 int creation_flags) {
[email protected]ab22ba42011-01-14 16:36:382242 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2243 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012244 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:572245 return;
[email protected]a8af9fdb2010-10-28 21:52:202246
[email protected]8e4560b62011-01-14 10:09:142247 DCHECK(version);
2248
[email protected]7577a5c52009-07-30 06:21:582249 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492250 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582251 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022252 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582253 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142254 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582255 case -1: // existing version is older, we should upgrade
2256 break;
2257 case 0: // existing version is same, do nothing
2258 return;
2259 case 1: // existing version is newer, uh-oh
2260 LOG(WARNING) << "Found external version of extension " << id
2261 << "that is older than current version. Current version "
2262 << "is: " << existing->VersionString() << ". New version "
2263 << "is: " << version << ". Keeping current version.";
2264 return;
2265 }
2266 }
2267
[email protected]b2907fd2011-03-25 16:43:372268 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362269
[email protected]14908b72011-04-20 06:54:362270 // no client (silent install)
[email protected]d8c8f25f2011-11-02 18:18:012271 scoped_refptr<CrxInstaller> installer(CrxInstaller::Create(this, NULL));
[email protected]6dfbbf82010-03-12 23:09:162272 installer->set_install_source(location);
2273 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072274 installer->set_expected_version(*version);
2275 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]1bf73cc32011-10-26 22:38:312276 installer->set_creation_flags(creation_flags);
[email protected]6dfbbf82010-03-12 23:09:162277 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:582278}
2279
[email protected]eaa7dd182010-12-14 11:09:002280void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402281 const FilePath& extension_path,
2282 const std::string &error,
[email protected]d11c8e92009-10-20 23:26:402283 bool be_noisy) {
[email protected]d8c8f25f2011-11-02 18:18:012284 content::NotificationService::current()->Notify(
2285 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
2286 content::Source<Profile>(profile_),
2287 content::Details<const std::string>(&error));
[email protected]d11c8e92009-10-20 23:26:402288
[email protected]8a205c02011-02-04 20:41:332289 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:042290 std::string message = base::StringPrintf(
2291 "Could not load extension from '%s'. %s",
2292 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:402293 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2294}
2295
[email protected]eaa7dd182010-12-14 11:09:002296void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182297 ExtensionHost* host) {
2298 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332299 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182300 if (iter == orphaned_dev_tools_.end())
2301 return;
2302
2303 DevToolsManager::GetInstance()->AttachClientHost(
2304 iter->second, host->render_view_host());
2305 orphaned_dev_tools_.erase(iter);
2306}
2307
[email protected]432115822011-07-10 15:52:272308void ExtensionService::Observe(int type,
[email protected]6c2381d2011-10-19 02:52:532309 const content::NotificationSource& source,
2310 const content::NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272311 switch (type) {
2312 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]6c2381d2011-10-19 02:52:532313 if (profile_ !=
2314 content::Source<Profile>(source).ptr()->GetOriginalProfile()) {
[email protected]bc535ee52010-08-31 18:40:322315 break;
[email protected]6c2381d2011-10-19 02:52:532316 }
[email protected]a4ed6282009-12-14 20:51:162317
[email protected]6c2381d2011-10-19 02:52:532318 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262319
[email protected]fa2416f2011-05-03 08:41:202320 // Mark the extension as terminated and Unload it. We want it to
2321 // be in a consistent state: either fully working or not loaded
2322 // at all, but never half-crashed. We do it in a PostTask so
2323 // that other handlers of this notification will still have
2324 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362325 MessageLoop::current()->PostTask(
2326 FROM_HERE,
[email protected]53612e82011-10-18 18:00:362327 base::Bind(
[email protected]fa2416f2011-05-03 08:41:202328 &ExtensionService::TrackTerminatedExtension,
[email protected]d8c8f25f2011-11-02 18:18:012329 AsWeakPtr(),
[email protected]fa2416f2011-05-03 08:41:202330 host->extension()));
[email protected]31f77262009-12-02 20:48:532331 break;
2332 }
[email protected]432115822011-07-10 15:52:272333 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]f3b1a082011-11-18 00:34:302334 content::RenderProcessHost* process =
2335 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192336 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302337 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192338 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2339 break;
2340
[email protected]77a6970c2011-04-23 16:58:562341 // Valid extension function names, used to setup bindings in renderer.
2342 std::vector<std::string> function_names;
2343 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2344 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532345
[email protected]77a6970c2011-04-23 16:58:562346 // Scripting whitelist. This is modified by tests and must be communicated
2347 // to renderers.
2348 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2349 *Extension::GetScriptingWhitelist()));
2350
2351 // Loaded extensions.
[email protected]9776e82e2011-11-15 02:17:532352 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions;
[email protected]77a6970c2011-04-23 16:58:562353 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]9776e82e2011-11-15 02:17:532354 loaded_extensions.push_back(
2355 ExtensionMsg_Loaded_Params(extensions_[i]));
[email protected]77a6970c2011-04-23 16:58:562356 }
[email protected]9776e82e2011-11-15 02:17:532357 process->Send(new ExtensionMsg_Loaded(loaded_extensions));
[email protected]77a6970c2011-04-23 16:58:562358 break;
2359 }
[email protected]432115822011-07-10 15:52:272360 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]f3b1a082011-11-18 00:34:302361 content::RenderProcessHost* process =
2362 content::Source<content::RenderProcessHost>(source).ptr();
[email protected]fc30ef082011-08-18 04:07:192363 Profile* host_profile =
[email protected]f3b1a082011-11-18 00:34:302364 Profile::FromBrowserContext(process->GetBrowserContext());
[email protected]fc30ef082011-08-18 04:07:192365 if (!profile_->IsSameProfile(host_profile->GetOriginalProfile()))
2366 break;
2367
[email protected]f3b1a082011-11-18 00:34:302368 installed_app_hosts_.erase(process->GetID());
[email protected]6f371442011-11-09 06:45:462369
[email protected]f3b1a082011-11-18 00:34:302370 process_map_.Remove(process->GetID());
[email protected]6f371442011-11-09 06:45:462371 BrowserThread::PostTask(
2372 BrowserThread::IO, FROM_HERE,
2373 base::Bind(&ExtensionInfoMap::UnregisterAllExtensionsInProcess,
2374 profile_->GetExtensionInfoMap(),
[email protected]f3b1a082011-11-18 00:34:302375 process->GetID()));
[email protected]da5683db2011-04-23 17:12:212376 break;
2377 }
[email protected]432115822011-07-10 15:52:272378 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]6c2381d2011-10-19 02:52:532379 std::string* pref_name = content::Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102380 if (*pref_name == prefs::kExtensionInstallAllowList ||
2381 *pref_name == prefs::kExtensionInstallDenyList) {
2382 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102383 } else {
2384 NOTREACHED() << "Unexpected preference name.";
2385 }
[email protected]aa96d3a2010-08-21 08:45:252386 break;
2387 }
[email protected]d91268022011-08-26 13:17:372388 case chrome::NOTIFICATION_IMPORT_FINISHED: {
2389 registrar_.Remove(this, chrome::NOTIFICATION_IMPORT_FINISHED,
[email protected]6c2381d2011-10-19 02:52:532390 content::Source<Profile>(profile_));
[email protected]d91268022011-08-26 13:17:372391 InitEventRouters();
2392 break;
2393 }
[email protected]aa96d3a2010-08-21 08:45:252394
[email protected]4814b512009-11-07 00:12:292395 default:
2396 NOTREACHED() << "Unexpected notification type.";
2397 }
2398}
2399
[email protected]eaa7dd182010-12-14 11:09:002400bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182401 return !GetAppIds().empty();
2402}
[email protected]377011d2010-07-20 04:18:502403
[email protected]eaa7dd182010-12-14 11:09:002404ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182405 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:502406 for (ExtensionList::const_iterator it = extensions_.begin();
2407 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182408 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2409 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502410 }
2411
[email protected]ec5b50d2010-10-09 16:35:182412 return result;
[email protected]377011d2010-07-20 04:18:502413}
[email protected]d7e9a862010-11-03 21:57:492414
[email protected]eaa7dd182010-12-14 11:09:002415bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492416 return (extension->background_url().is_empty() ||
2417 extension_runtime_data_[extension->id()].background_page_ready);
2418}
2419
[email protected]eaa7dd182010-12-14 11:09:002420void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492421 DCHECK(!extension->background_url().is_empty());
2422 extension_runtime_data_[extension->id()].background_page_ready = true;
[email protected]ad50def52011-10-19 23:17:072423 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272424 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]6c2381d2011-10-19 02:52:532425 content::Source<const Extension>(extension),
[email protected]ad50def52011-10-19 23:17:072426 content::NotificationService::NoDetails());
[email protected]d7e9a862010-11-03 21:57:492427}
2428
[email protected]eaa7dd182010-12-14 11:09:002429bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492430 return extension_runtime_data_[extension->id()].being_upgraded;
2431}
2432
[email protected]eaa7dd182010-12-14 11:09:002433void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492434 bool value) {
2435 extension_runtime_data_[extension->id()].being_upgraded = value;
2436}
[email protected]1bead0712010-11-27 17:41:532437
[email protected]39a5b532011-10-22 01:47:072438bool ExtensionService::HasUsedWebRequest(const Extension* extension) {
2439 return extension_runtime_data_[extension->id()].has_used_webrequest;
2440}
2441
2442void ExtensionService::SetHasUsedWebRequest(const Extension* extension,
2443 bool value) {
2444 extension_runtime_data_[extension->id()].has_used_webrequest = value;
2445}
2446
[email protected]45644f62011-11-23 00:58:232447base::PropertyBag* ExtensionService::GetPropertyBag(
2448 const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532449 return &extension_runtime_data_[extension->id()].property_bag;
2450}
[email protected]cebc3dc2011-04-18 17:15:002451
2452void ExtensionService::RegisterNaClModule(const GURL& url,
2453 const std::string& mime_type) {
2454 NaClModuleInfo info;
2455 info.url = url;
2456 info.mime_type = mime_type;
2457
2458 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2459 nacl_module_list_.push_front(info);
2460}
2461
2462void ExtensionService::UnregisterNaClModule(const GURL& url) {
2463 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2464 DCHECK(iter != nacl_module_list_.end());
2465 nacl_module_list_.erase(iter);
2466}
2467
[email protected]ed0ba002011-05-26 16:55:132468void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332469 // An extension has been added which has a nacl_module component, which means
2470 // there is a MIME type that module wants to handle, so we need to add that
2471 // MIME type to plugins which handle NaCl modules in order to allow the
2472 // individual modules to handle these types.
[email protected]738a7212011-10-21 17:33:522473 const content::PepperPluginInfo* pepper_info = NULL;
2474 std::vector<content::PepperPluginInfo> plugins;
[email protected]ed0ba002011-05-26 16:55:132475 PepperPluginRegistry::ComputeList(&plugins);
[email protected]300c0ea2011-07-15 23:04:332476
2477 // Search the entire plugin list for plugins that handle the NaCl MIME type.
2478 // There can be multiple plugins like this, for instance the internal NaCl
2479 // plugin and a plugin registered by --register-pepper-plugins during tests.
[email protected]ed0ba002011-05-26 16:55:132480 for (size_t i = 0; i < plugins.size(); ++i) {
[email protected]300c0ea2011-07-15 23:04:332481 pepper_info = &plugins[i];
2482 CHECK(pepper_info);
[email protected]91d9f3d2011-08-14 05:24:442483 std::vector<webkit::WebPluginMimeType>::const_iterator mime_iter;
[email protected]300c0ea2011-07-15 23:04:332484 // Check each MIME type the plugins handle for the NaCl MIME type.
2485 for (mime_iter = pepper_info->mime_types.begin();
2486 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2487 if (mime_iter->mime_type == kNaClPluginMimeType) {
2488 // This plugin handles "application/x-nacl".
2489
[email protected]f520b5b2011-11-08 02:42:142490 PluginService::GetInstance()->
[email protected]300c0ea2011-07-15 23:04:332491 UnregisterInternalPlugin(pepper_info->path);
2492
[email protected]91d9f3d2011-08-14 05:24:442493 webkit::WebPluginInfo info = pepper_info->ToWebPluginInfo();
[email protected]300c0ea2011-07-15 23:04:332494
2495 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2496 nacl_module_list_.begin();
2497 iter != nacl_module_list_.end(); ++iter) {
2498 // Add the MIME type specified in the extension to this NaCl plugin,
2499 // With an extra "nacl" argument to specify the location of the NaCl
2500 // manifest file.
[email protected]91d9f3d2011-08-14 05:24:442501 webkit::WebPluginMimeType mime_type_info;
[email protected]300c0ea2011-07-15 23:04:332502 mime_type_info.mime_type = iter->mime_type;
2503 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2504 mime_type_info.additional_param_values.push_back(
2505 UTF8ToUTF16(iter->url.spec()));
2506 info.mime_types.push_back(mime_type_info);
2507 }
2508
[email protected]f520b5b2011-11-08 02:42:142509 PluginService::GetInstance()->RefreshPlugins();
2510 PluginService::GetInstance()->RegisterInternalPlugin(info);
[email protected]300c0ea2011-07-15 23:04:332511 // This plugin has been modified, no need to check the rest of its
2512 // types, but continue checking other plugins.
2513 break;
2514 }
[email protected]ed0ba002011-05-26 16:55:132515 }
2516 }
[email protected]ed0ba002011-05-26 16:55:132517}
2518
[email protected]cebc3dc2011-04-18 17:15:002519ExtensionService::NaClModuleInfoList::iterator
2520 ExtensionService::FindNaClModule(const GURL& url) {
2521 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2522 iter != nacl_module_list_.end(); ++iter) {
2523 if (iter->url == url)
2524 return iter;
2525 }
2526 return nacl_module_list_.end();
2527}