blob: 45e796edb75cec3f381e08f162ab1664e8212065 [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]e2eb43112009-05-29 21:19:5411#include "base/command_line.h"
[email protected]6014d672008-12-05 00:38:2512#include "base/file_util.h"
[email protected]14908b72011-04-20 06:54:3613#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3814#include "base/metrics/histogram.h"
[email protected]cebc3dc2011-04-18 17:15:0015#include "base/path_service.h"
[email protected]cd500f72010-06-25 23:44:3216#include "base/stl_util-inl.h"
[email protected]24b538a2010-02-27 01:22:4417#include "base/string16.h"
[email protected]e83326f2010-07-31 17:29:2518#include "base/string_number_conversions.h"
[email protected]6014d672008-12-05 00:38:2519#include "base/string_util.h"
[email protected]18d4b6c2010-09-21 03:21:0420#include "base/stringprintf.h"
[email protected]34b99632011-01-01 01:01:0621#include "base/threading/thread_restrictions.h"
[email protected]cc2c3432009-11-06 17:24:3622#include "base/time.h"
[email protected]ce7f62e32010-08-10 23:43:5923#include "base/utf_string_conversions.h"
[email protected]cc655912009-01-29 23:19:1924#include "base/values.h"
[email protected]aa142702010-03-26 01:26:3325#include "base/version.h"
[email protected]15730c42009-09-03 00:03:2026#include "chrome/browser/browser_process.h"
[email protected]4814b512009-11-07 00:12:2927#include "chrome/browser/debugger/devtools_manager.h"
[email protected]7577a5c52009-07-30 06:21:5828#include "chrome/browser/extensions/crx_installer.h"
[email protected]18cc5a52011-04-15 16:03:3829#include "chrome/browser/extensions/apps_promo.h"
[email protected]5cbe1e22010-01-30 01:18:5630#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3831#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2832#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4433#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3234#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2435#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]de768a832009-10-30 05:25:0136#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5637#include "chrome/browser/extensions/extension_host.h"
[email protected]7596ce72010-08-30 05:10:4638#include "chrome/browser/extensions/extension_management_api.h"
[email protected]118de7892011-03-16 13:31:4039#include "chrome/browser/extensions/extension_preference_api.h"
[email protected]4814b512009-11-07 00:12:2940#include "chrome/browser/extensions/extension_process_manager.h"
[email protected]8a661f82010-10-19 21:47:1141#include "chrome/browser/extensions/extension_processes_api.h"
[email protected]19eb80152011-02-26 00:28:4342#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]90310d92011-04-17 07:35:0443#include "chrome/browser/extensions/extension_sync_data.h"
[email protected]93fd78f42009-07-10 16:43:1744#include "chrome/browser/extensions/extension_updater.h"
[email protected]8f9d4eb2011-02-05 01:39:1045#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]784688a62010-09-13 07:06:5246#include "chrome/browser/extensions/extension_webnavigation_api.h"
[email protected]8e4560b62011-01-14 10:09:1447#include "chrome/browser/extensions/external_extension_provider_impl.h"
[email protected]42b795652011-02-22 23:32:3848#include "chrome/browser/extensions/external_extension_provider_interface.h"
[email protected]b2907fd2011-03-25 16:43:3749#include "chrome/browser/extensions/pending_extension_manager.h"
[email protected]56ad3792010-05-28 17:45:3350#include "chrome/browser/net/chrome_url_request_context.h"
[email protected]37858e52010-08-26 00:22:0251#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3352#include "chrome/browser/profiles/profile.h"
[email protected]56ad3792010-05-28 17:45:3353#include "chrome/browser/search_engines/template_url_model.h"
[email protected]18280372011-03-22 18:05:2254#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4255#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1756#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
57#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]7c5c6f3a2011-04-28 19:56:4958#include "chrome/browser/ui/webui/ntp/shown_sections_handler.h"
[email protected]aab98a52009-12-02 03:22:3559#include "chrome/common/child_process_logging.h"
[email protected]cebc3dc2011-04-18 17:15:0060#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5461#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5862#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4063#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0864#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1365#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0666#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]77a6970c2011-04-23 16:58:5667#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2168#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1969#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1470#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5371#include "content/browser/browser_thread.h"
[email protected]2de307592011-04-05 21:16:5872#include "content/browser/plugin_process_host.h"
73#include "content/browser/plugin_service.h"
[email protected]77a6970c2011-04-23 16:58:5674#include "content/browser/renderer_host/render_process_host.h"
[email protected]c3113022011-04-16 03:26:3075#include "content/common/json_value_serializer.h"
[email protected]b3841c502011-03-09 01:21:3176#include "content/common/notification_service.h"
77#include "content/common/notification_type.h"
[email protected]cebc3dc2011-04-18 17:15:0078#include "content/common/pepper_plugin_registry.h"
[email protected]c10da4b02010-03-25 14:38:3279#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4480#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4481#include "webkit/database/database_tracker.h"
82#include "webkit/database/database_util.h"
[email protected]2de307592011-04-05 21:16:5883#include "webkit/plugins/npapi/plugin_list.h"
[email protected]c64631652009-04-29 22:24:3184
[email protected]eed367e2011-04-12 03:43:3185#if defined(OS_CHROMEOS)
86#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]b777b332011-04-16 04:01:0887#include "webkit/fileapi/file_system_context.h"
88#include "webkit/fileapi/file_system_mount_point_provider.h"
89#include "webkit/fileapi/file_system_path_manager.h"
[email protected]eed367e2011-04-12 03:43:3190#endif
91
[email protected]5ef47ec2010-01-28 05:58:0592using base::Time;
93
[email protected]c6d474f82009-12-16 21:11:0694namespace errors = extension_manifest_errors;
95
[email protected]b6ab96d2009-08-20 18:58:1996namespace {
97
[email protected]29d0d4ac2010-09-08 21:10:3198#if defined(OS_LINUX)
99static const int kOmniboxIconPaddingLeft = 2;
100static const int kOmniboxIconPaddingRight = 2;
101#elif defined(OS_MACOSX)
102static const int kOmniboxIconPaddingLeft = 0;
103static const int kOmniboxIconPaddingRight = 2;
104#else
105static const int kOmniboxIconPaddingLeft = 0;
106static const int kOmniboxIconPaddingRight = 0;
107#endif
108
[email protected]a315ba92010-11-16 14:12:21109// The following enumeration is used in histograms matching
110// Extensions.ManifestReload* . Values may be added, as long
111// as existing values are not changed.
112enum ManifestReloadReason {
113 NOT_NEEDED = 0, // Reload not needed.
114 UNPACKED_DIR, // Unpacked directory
115 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
116 NUM_MANIFEST_RELOAD_REASONS
117};
[email protected]2111b1a2010-03-12 18:12:44118
[email protected]a315ba92010-11-16 14:12:21119ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
120 // Always reload manifests of unpacked extensions, because they can change
121 // on disk independent of the manifest in our prefs.
122 if (info.extension_location == Extension::LOAD)
123 return UNPACKED_DIR;
124
125 // Reload the manifest if it needs to be relocalized.
126 if (extension_l10n_util::ShouldRelocalizeManifest(info))
127 return NEEDS_RELOCALIZATION;
128
129 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44130}
131
[email protected]2de307592011-04-05 21:16:58132static void ForceShutdownPlugin(const FilePath& plugin_path) {
133 PluginProcessHost* plugin =
134 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
135 if (plugin)
136 plugin->ForceShutdown();
137}
138
[email protected]c6d474f82009-12-16 21:11:06139} // namespace
[email protected]b6ab96d2009-08-20 18:58:19140
[email protected]eaa7dd182010-12-14 11:09:00141ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49142 : background_page_ready(false),
143 being_upgraded(false) {
144}
145
[email protected]eaa7dd182010-12-14 11:09:00146ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49147}
148
[email protected]cebc3dc2011-04-18 17:15:00149ExtensionService::NaClModuleInfo::NaClModuleInfo() {
150}
151
152ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
153}
154
[email protected]eaa7dd182010-12-14 11:09:00155// ExtensionService.
[email protected]6014d672008-12-05 00:38:25156
[email protected]eaa7dd182010-12-14 11:09:00157const char* ExtensionService::kInstallDirectoryName = "Extensions";
158const char* ExtensionService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42159
[email protected]eaa7dd182010-12-14 11:09:00160// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22161
[email protected]eaa7dd182010-12-14 11:09:00162class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14163 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22164 public:
[email protected]1f830eb2010-09-28 08:25:14165 // |install_directory| is a path where to look for extensions to load.
[email protected]14908b72011-04-20 06:54:36166 ExtensionServiceBackend(
167 base::WeakPtr<ExtensionService> frontend,
168 const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22169
170 // Loads a single extension from |path| where |path| is the top directory of
171 // a specific extension where its manifest file lives.
172 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45173 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22174 // TODO(erikkay): It might be useful to be able to load a packed extension
175 // (presumably into memory) without installing it.
[email protected]14908b72011-04-20 06:54:36176 void LoadSingleExtension(const FilePath &path);
[email protected]7a4c6852010-09-16 03:44:22177
[email protected]7a4c6852010-09-16 03:44:22178 private:
[email protected]eaa7dd182010-12-14 11:09:00179 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22180
[email protected]eaa7dd182010-12-14 11:09:00181 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22182
[email protected]7a4c6852010-09-16 03:44:22183 // Notify the frontend that there was an error loading an extension.
184 void ReportExtensionLoadError(const FilePath& extension_path,
185 const std::string& error);
186
[email protected]14908b72011-04-20 06:54:36187 // Notify the frontend that an extension was installed.
188 void OnExtensionInstalled(const scoped_refptr<const Extension>& extension);
189
190 base::WeakPtr<ExtensionService> frontend_;
[email protected]7a4c6852010-09-16 03:44:22191
192 // The top-level extensions directory being installed to.
193 FilePath install_directory_;
194
[email protected]eaa7dd182010-12-14 11:09:00195 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22196};
197
[email protected]eaa7dd182010-12-14 11:09:00198ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]14908b72011-04-20 06:54:36199 base::WeakPtr<ExtensionService> frontend,
[email protected]aebe23a32010-12-10 22:15:48200 const FilePath& install_directory)
[email protected]14908b72011-04-20 06:54:36201 : frontend_(frontend),
202 install_directory_(install_directory) {
203 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]7a4c6852010-09-16 03:44:22204}
205
[email protected]eaa7dd182010-12-14 11:09:00206ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]14908b72011-04-20 06:54:36207 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
208 BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]7a4c6852010-09-16 03:44:22209}
210
[email protected]14908b72011-04-20 06:54:36211void ExtensionServiceBackend::LoadSingleExtension(const FilePath& path_in) {
[email protected]a29a517a2011-01-21 21:11:12212 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20213
[email protected]7a4c6852010-09-16 03:44:22214 FilePath extension_path = path_in;
215 file_util::AbsolutePath(&extension_path);
216
[email protected]3aff9ad2011-04-01 20:26:48217 int flags = Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) ?
218 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
[email protected]83048a22011-03-29 00:14:13219 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
220 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]7a4c6852010-09-16 03:44:22221 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27222 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22223 extension_path,
[email protected]92888082010-10-18 19:24:57224 Extension::LOAD,
[email protected]83048a22011-03-29 00:14:13225 flags,
[email protected]ad8e04a2010-11-01 04:16:27226 &error));
[email protected]7a4c6852010-09-16 03:44:22227
228 if (!extension) {
[email protected]14908b72011-04-20 06:54:36229 if (!BrowserThread::PostTask(
230 BrowserThread::UI, FROM_HERE,
231 NewRunnableMethod(
232 this,
233 &ExtensionServiceBackend::ReportExtensionLoadError,
234 extension_path, error)))
235 NOTREACHED() << error;
[email protected]7a4c6852010-09-16 03:44:22236 return;
237 }
238
[email protected]7a4c6852010-09-16 03:44:22239 // Report this as an installed extension so that it gets remembered in the
240 // prefs.
[email protected]14908b72011-04-20 06:54:36241 if (!BrowserThread::PostTask(
242 BrowserThread::UI, FROM_HERE,
243 NewRunnableMethod(
244 this,
245 &ExtensionServiceBackend::OnExtensionInstalled,
246 extension)))
247 NOTREACHED();
[email protected]7a4c6852010-09-16 03:44:22248}
249
[email protected]eaa7dd182010-12-14 11:09:00250void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22251 const FilePath& extension_path, const std::string &error) {
[email protected]14908b72011-04-20 06:54:36252 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
253 if (frontend_.get())
254 frontend_->ReportExtensionLoadError(
255 extension_path, error, NotificationType::EXTENSION_INSTALL_ERROR,
256 true /* alert_on_error */);
257}
258
259void ExtensionServiceBackend::OnExtensionInstalled(
260 const scoped_refptr<const Extension>& extension) {
261 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
262 if (frontend_.get())
263 frontend_->OnExtensionInstalled(extension);
[email protected]7a4c6852010-09-16 03:44:22264}
265
[email protected]8e4560b62011-01-14 10:09:14266void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12267 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20268
[email protected]7a4c6852010-09-16 03:44:22269 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21270 ProviderCollection::const_iterator i;
271 for (i = external_extension_providers_.begin();
272 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14273 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21274 if (i->get()->HasExtension(id))
275 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22276 }
277
[email protected]7a4c6852010-09-16 03:44:22278 // This is an external extension that we don't have registered. Uninstall.
[email protected]d6ebc9792011-04-07 18:18:33279 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22280}
281
[email protected]8e4560b62011-01-14 10:09:14282void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22283 external_extension_providers_.clear();
284}
285
[email protected]8e4560b62011-01-14 10:09:14286void ExtensionService::AddProviderForTesting(
287 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12288 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21289 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14290 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22291}
292
[email protected]8e4560b62011-01-14 10:09:14293void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22294 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42295 const GURL& update_url,
296 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38297 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
298 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20299
[email protected]8e4560b62011-01-14 10:09:14300 if (GetExtensionById(id, true)) {
[email protected]7a4c6852010-09-16 03:44:22301 // Already installed. Do not change the update URL that the extension set.
302 return;
303 }
[email protected]b2907fd2011-03-25 16:43:37304 pending_extension_manager()->AddFromExternalUpdateUrl(
305 id, update_url, location);
[email protected]a29a517a2011-01-21 21:11:12306 external_extension_url_added_ |= true;
[email protected]7a4c6852010-09-16 03:44:22307}
308
[email protected]eaa7dd182010-12-14 11:09:00309bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12310 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07311 // Special-case the themes mini-gallery.
312 // TODO(erikkay) When that gallery goes away, remove this code.
313 if (IsDownloadFromMiniGallery(download_url) &&
314 StartsWithASCII(referrer_url.spec(),
315 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44316 return true;
[email protected]1debbbb62010-10-06 17:23:44317 }
[email protected]473ff6e2010-05-12 15:31:55318
[email protected]9adb9692010-10-29 23:14:02319 const Extension* download_extension = GetExtensionByWebExtent(download_url);
320 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
321 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07322
323 bool referrer_valid = (referrer_extension == webstore_app);
324 bool download_valid = (download_extension == webstore_app);
325
326 // If the command-line gallery URL is set, then be a bit more lenient.
327 GURL store_url =
328 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
329 switches::kAppsGalleryURL));
330 if (!store_url.is_empty()) {
331 std::string store_tld =
332 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
333 if (!referrer_valid) {
334 std::string referrer_tld =
335 net::RegistryControlledDomainService::GetDomainAndRegistry(
336 referrer_url);
337 // The referrer gets stripped when transitioning from https to http,
338 // or when hitting an unknown test cert and that commonly happens in
339 // testing environments. Given this, we allow an empty referrer when
340 // the command-line flag is set.
341 // Otherwise, the TLD must match the TLD of the command-line url.
342 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
343 }
344
345 if (!download_valid) {
346 std::string download_tld =
347 net::RegistryControlledDomainService::GetDomainAndRegistry(
348 GURL(download_url));
349
350 // Otherwise, the TLD must match the TLD of the command-line url.
351 download_valid = (download_tld == store_tld);
352 }
353 }
354
355 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23356}
357
[email protected]eaa7dd182010-12-14 11:09:00358bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38359 return StartsWithASCII(download_url.spec(),
360 extension_urls::kMiniGalleryDownloadPrefix,
361 false); // case_sensitive
362}
363
[email protected]d9696672011-03-15 22:45:09364const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27365 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09366 const Extension* app = GetExtensionByWebExtent(url);
367 if (app)
368 return app;
[email protected]ffb204f22010-12-05 23:20:27369
370 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09371 app = GetExtensionByURL(url);
372 if (app && app->is_app())
373 return app;
374
375 return NULL;
376}
377
378bool ExtensionService::IsInstalledApp(const GURL& url) {
379 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27380}
381
[email protected]da5683db2011-04-23 17:12:21382void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
383 const Extension* app) {
384 installed_app_hosts_[renderer_child_id] = app;
385}
386
387const Extension* ExtensionService::GetInstalledAppForRenderer(
388 int renderer_child_id) {
389 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
390 if (i == installed_app_hosts_.end())
391 return NULL;
392 return i->second;
393}
394
[email protected]6aeac8342010-10-01 20:21:18395// static
[email protected]d6ebc9792011-04-07 18:18:33396// This function is used to implement the command-line switch
397// --uninstall-extension. The LOG statements within this function are used to
398// inform the user if the uninstall cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00399bool ExtensionService::UninstallExtensionHelper(
400 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18401 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18402
[email protected]95da88c42011-03-31 10:07:33403 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47404 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33405
[email protected]d6ebc9792011-04-07 18:18:33406 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33407 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18408 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33409 << "id: " << extension_id;
410 return false;
[email protected]6aeac8342010-10-01 20:21:18411 }
412
[email protected]d6ebc9792011-04-07 18:18:33413 // The following call to UninstallExtension will not allow an uninstall of a
414 // policy-controlled extension.
415 std::string error;
416 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
417 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
418 << ": " << error;
419 return false;
420 }
[email protected]95da88c42011-03-31 10:07:33421
[email protected]6aeac8342010-10-01 20:21:18422 return true;
423}
424
[email protected]eaa7dd182010-12-14 11:09:00425ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24426 const CommandLine* command_line,
427 const FilePath& install_directory,
428 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03429 bool autoupdate_enabled,
430 bool extensions_enabled)
[email protected]14908b72011-04-20 06:54:36431 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
432 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
433 profile_(profile),
[email protected]73c47932010-12-06 18:13:43434 extension_prefs_(extension_prefs),
[email protected]14908b72011-04-20 06:54:36435 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23436 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03437 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13438 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55439 ready_(false),
[email protected]14908b72011-04-20 06:54:36440 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]18cc5a52011-04-15 16:03:38441 apps_promo_(profile->GetPrefs()),
[email protected]b3d62312b12010-10-14 21:10:18442 event_routers_initialized_(false) {
[email protected]a29a517a2011-01-21 21:11:12443 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20444
[email protected]36a784c2009-06-23 06:21:08445 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23446 if (command_line->HasSwitch(switches::kDisableExtensions)) {
447 extensions_enabled_ = false;
448 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
449 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18450 }
[email protected]36a784c2009-06-23 06:21:08451
[email protected]a4ed6282009-12-14 20:51:16452 registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32453 NotificationService::AllSources());
[email protected]77a6970c2011-04-23 16:58:56454 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CREATED,
455 NotificationService::AllSources());
[email protected]da5683db2011-04-23 17:12:21456 registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED,
457 NotificationService::AllSources());
[email protected]2fb7dc982010-09-29 12:24:28458 pref_change_registrar_.Init(profile->GetPrefs());
459 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
460 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29461
[email protected]93fd78f42009-07-10 16:43:17462 // Set up the ExtensionUpdater
463 if (autoupdate_enabled) {
464 int update_frequency = kDefaultUpdateFrequencySeconds;
465 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25466 base::StringToInt(command_line->GetSwitchValueASCII(
467 switches::kExtensionsUpdateFrequency),
468 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17469 }
[email protected]82b6e512011-04-12 20:33:27470 updater_.reset(new ExtensionUpdater(this,
471 extension_prefs,
472 profile->GetPrefs(),
473 profile,
474 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17475 }
476
[email protected]14908b72011-04-20 06:54:36477 backend_ =
478 new ExtensionServiceBackend(weak_ptr_factory_.GetWeakPtr(),
479 install_directory_);
[email protected]8e4560b62011-01-14 10:09:14480
[email protected]0436b102011-04-15 18:30:03481 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05482 ExternalExtensionProviderImpl::CreateExternalProviders(
483 this, profile_, &external_extension_providers_);
484 }
[email protected]b671760b2010-07-15 21:13:47485
[email protected]aa96d3a2010-08-21 08:45:25486 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31487 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47488 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31489 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
490 0, kOmniboxIconPaddingRight));
[email protected]6014d672008-12-05 00:38:25491}
492
[email protected]eaa7dd182010-12-14 11:09:00493const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45494 return &extensions_;
495}
496
[email protected]eaa7dd182010-12-14 11:09:00497const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45498 return &disabled_extensions_;
499}
500
[email protected]bb7f40952011-01-13 00:21:20501const ExtensionList* ExtensionService::terminated_extensions() const {
502 return &terminated_extensions_;
503}
504
[email protected]b2907fd2011-03-25 16:43:37505PendingExtensionManager* ExtensionService::pending_extension_manager() {
506 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45507}
508
[email protected]eaa7dd182010-12-14 11:09:00509ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17510 // No need to unload extensions here because they are profile-scoped, and the
511 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17512
[email protected]8e4560b62011-01-14 10:09:14513 ProviderCollection::const_iterator i;
514 for (i = external_extension_providers_.begin();
515 i != external_extension_providers_.end(); ++i) {
516 ExternalExtensionProviderInterface* provider = i->get();
517 provider->ServiceShutdown();
518 }
[email protected]eed367e2011-04-12 03:43:31519
520#if defined(OS_CHROMEOS)
521 if (event_routers_initialized_) {
522 ExtensionFileBrowserEventRouter::GetInstance()->
523 StopObservingFileSystemEvents();
524 }
525#endif
[email protected]6014d672008-12-05 00:38:25526}
527
[email protected]eaa7dd182010-12-14 11:09:00528void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18529 if (event_routers_initialized_)
530 return;
531
[email protected]c5ae74ab2010-04-15 18:14:37532 ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_);
533 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08534 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
535 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40536 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]8e8bb6d2010-12-13 08:18:55537 ExtensionBookmarkEventRouter::GetInstance()->Observe(
[email protected]c5ae74ab2010-04-15 18:14:37538 profile_->GetBookmarkModel());
[email protected]2c5e1e12010-06-10 13:14:44539 ExtensionCookiesEventRouter::GetInstance()->Init();
[email protected]7596ce72010-08-30 05:10:46540 ExtensionManagementEventRouter::GetInstance()->Init();
[email protected]8a661f82010-10-19 21:47:11541 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]784688a62010-09-13 07:06:52542 ExtensionWebNavigationEventRouter::GetInstance()->Init();
[email protected]eed367e2011-04-12 03:43:31543#if defined(OS_CHROMEOS)
544 ExtensionFileBrowserEventRouter::GetInstance()->ObserveFileSystemEvents(
545 profile_);
546#endif
[email protected]b3d62312b12010-10-14 21:10:18547 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37548}
549
[email protected]b2907fd2011-03-25 16:43:37550const Extension* ExtensionService::GetExtensionById(
551 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47552 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44553}
554
[email protected]eaa7dd182010-12-14 11:09:00555void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12556 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20557
[email protected]fa6a9102010-11-22 15:38:50558 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17559 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32560
[email protected]95dd38f2009-10-20 20:09:15561 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
562 // the first extension, because its members listen for loaded notifications.
563 g_browser_process->resource_dispatcher_host();
564
[email protected]9f1087e2009-06-15 17:29:32565 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57566
[email protected]9f1087e2009-06-15 17:29:32567 // TODO(erikkay) this should probably be deferred to a future point
568 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17569 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57570
[email protected]9f1087e2009-06-15 17:29:32571 // TODO(erikkay) this should probably be deferred as well.
572 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25573}
574
[email protected]eaa7dd182010-12-14 11:09:00575void ExtensionService::UpdateExtension(const std::string& id,
[email protected]9a8c4022011-01-25 14:25:33576 const FilePath& extension_path,
577 const GURL& download_url) {
[email protected]a29a517a2011-01-21 21:11:12578 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20579
[email protected]b2907fd2011-03-25 16:43:37580 PendingExtensionInfo pending_extension_info;
581 bool is_pending_extension = pending_extension_manager_.GetById(
582 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32583
[email protected]8001df22011-04-28 19:59:47584 const Extension* extension =
585 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41586 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33587 LOG(WARNING) << "Will not update extension " << id
588 << " because it is not installed or pending";
589 // Delete extension_path since we're not creating a CrxInstaller
590 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36591 if (!BrowserThread::PostTask(
592 BrowserThread::FILE, FROM_HERE,
593 NewRunnableFunction(
594 extension_file_util::DeleteFile, extension_path, false)))
595 NOTREACHED();
[email protected]4c967932009-07-31 01:15:49596 return;
[email protected]e957fe52009-06-23 16:51:05597 }
598
[email protected]aa142702010-03-26 01:26:33599 // We want a silent install only for non-pending extensions and
600 // pending extensions that have install_silently set.
601 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37602 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33603 NULL : new ExtensionInstallUI(profile_);
604
[email protected]14908b72011-04-20 06:54:36605 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(client));
[email protected]6dfbbf82010-03-12 23:09:16606 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18607 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37608 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41609 else if (extension)
610 installer->set_install_source(extension->location());
[email protected]6dfbbf82010-03-12 23:09:16611 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34612 installer->set_original_url(download_url);
[email protected]6dfbbf82010-03-12 23:09:16613 installer->InstallCrx(extension_path);
[email protected]e957fe52009-06-23 16:51:05614}
615
[email protected]eaa7dd182010-12-14 11:09:00616void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12617 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06618 FilePath path;
[email protected]9adb9692010-10-29 23:14:02619 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40620
[email protected]f17dbd42010-08-16 23:21:10621 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06622 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29623 // If the extension has an inspector open for its background page, detach
624 // the inspector and hang onto a cookie for it, so that we can reattach
625 // later.
626 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
627 ExtensionHost* host = manager->GetBackgroundHostForExtension(
628 current_extension);
629 if (host) {
630 // Look for an open inspector for the background page.
631 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
632 host->render_view_host());
633 if (devtools_cookie >= 0)
634 orphaned_dev_tools_[extension_id] = devtools_cookie;
635 }
636
[email protected]b65272f2009-08-31 15:47:06637 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10638 DisableExtension(extension_id);
639 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16640 } else {
641 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06642 }
643
[email protected]e6090e42010-03-23 22:44:08644 // Check the installed extensions to see if what we're reloading was already
645 // installed.
646 scoped_ptr<ExtensionInfo> installed_extension(
647 extension_prefs_->GetInstalledExtensionInfo(extension_id));
648 if (installed_extension.get() &&
649 installed_extension->extension_manifest.get()) {
650 LoadInstalledExtension(*installed_extension, false);
651 } else {
652 // We should always be able to remember the extension's path. If it's not in
653 // the map, someone failed to update |unloaded_extension_paths_|.
654 CHECK(!path.empty());
655 LoadExtension(path);
656 }
[email protected]9cddd4702009-07-27 22:09:40657}
658
[email protected]d6ebc9792011-04-07 18:18:33659bool ExtensionService::UninstallExtension(const std::string& extension_id,
660 bool external_uninstall,
661 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12662 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20663
[email protected]8001df22011-04-28 19:59:47664 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]631cf822009-05-15 07:01:25665
[email protected]e7afe2452010-08-22 16:19:13666 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48667 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32668
[email protected]831aa212010-03-26 13:55:19669 // Get hold of information we need after unloading, since the extension
670 // pointer will be invalid then.
671 GURL extension_url(extension->url());
672 Extension::Location location(extension->location());
[email protected]95da88c42011-03-31 10:07:33673
674 // Policy change which triggers an uninstall will always set
675 // |external_uninstall| to true so this is the only way to uninstall
676 // managed extensions.
[email protected]d6ebc9792011-04-07 18:18:33677 if (!Extension::UserMayDisable(location) && !external_uninstall) {
678 NotificationService::current()->Notify(
679 NotificationType::EXTENSION_UNINSTALL_NOT_ALLOWED,
680 Source<Profile>(profile_),
681 Details<const Extension>(extension));
682 if (error != NULL) {
683 *error = errors::kCannotUninstallManagedExtension;
684 }
685 return false;
686 }
[email protected]95da88c42011-03-31 10:07:33687
[email protected]211030342010-09-30 18:41:06688 UninstalledExtensionInfo uninstalled_extension_info(*extension);
[email protected]831aa212010-03-26 13:55:19689
[email protected]9b217652010-10-08 22:04:23690 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08691 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49692 RecordPermissionMessagesHistogram(
693 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23694
[email protected]831aa212010-03-26 13:55:19695 // Also copy the extension identifier since the reference might have been
696 // obtained via Extension::id().
697 std::string extension_id_copy(extension_id);
698
[email protected]56ad3792010-05-28 17:45:33699 if (profile_->GetTemplateURLModel())
700 profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension);
701
[email protected]831aa212010-03-26 13:55:19702 // Unload before doing more cleanup to ensure that nothing is hanging on to
703 // any of these resources.
[email protected]a9f39a312010-12-23 22:14:27704 UnloadExtension(extension_id, UnloadedExtensionInfo::UNINSTALL);
[email protected]831aa212010-03-26 13:55:19705
706 extension_prefs_->OnExtensionUninstalled(extension_id_copy, location,
707 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32708
709 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19710 if (Extension::LOAD != location) {
[email protected]14908b72011-04-20 06:54:36711 if (!BrowserThread::PostTask(
712 BrowserThread::FILE, FROM_HERE,
713 NewRunnableFunction(
714 &extension_file_util::UninstallExtension,
715 install_directory_,
716 extension_id_copy)))
717 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32718 }
719
[email protected]c10da4b02010-03-25 14:38:32720 ClearExtensionData(extension_url);
[email protected]0dfe05c2011-02-23 23:03:36721 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06722
723 // Notify interested parties that we've uninstalled this extension.
724 NotificationService::current()->Notify(
725 NotificationType::EXTENSION_UNINSTALLED,
726 Source<Profile>(profile_),
727 Details<UninstalledExtensionInfo>(&uninstalled_extension_info));
[email protected]d6ebc9792011-04-07 18:18:33728
729 return true;
[email protected]c10da4b02010-03-25 14:38:32730}
731
[email protected]eaa7dd182010-12-14 11:09:00732void ExtensionService::ClearExtensionData(const GURL& extension_url) {
[email protected]c10da4b02010-03-25 14:38:32733 scoped_refptr<ExtensionDataDeleter> deleter(
734 new ExtensionDataDeleter(profile_, extension_url));
735 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32736}
737
[email protected]c3cfb012011-04-06 22:07:35738bool ExtensionService::IsExtensionEnabled(
739 const std::string& extension_id) const {
[email protected]c3cfb012011-04-06 22:07:35740 return
741 extension_prefs_->GetExtensionState(extension_id) == Extension::ENABLED;
742}
743
744bool ExtensionService::IsExternalExtensionUninstalled(
745 const std::string& extension_id) const {
746 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
747}
748
[email protected]eaa7dd182010-12-14 11:09:00749void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12750 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20751
[email protected]06f92562011-04-29 19:27:31752 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39753 return;
[email protected]0c6da502009-08-14 22:32:39754
[email protected]b3317ad2011-04-28 23:46:00755 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52756
[email protected]06f92562011-04-29 19:27:31757 const Extension* extension =
758 GetExtensionByIdInternal(extension_id, false, true, false);
759 // This can happen if sync enables an extension that is not
760 // installed yet.
761 if (!extension)
762 return;
763
[email protected]0c6da502009-08-14 22:32:39764 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57765 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39766 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
767 disabled_extensions_.end(),
768 extension);
769 disabled_extensions_.erase(iter);
770
[email protected]f681c782010-11-19 11:19:39771 // Make sure any browser action contained within it is not hidden.
772 extension_prefs_->SetBrowserActionVisibility(extension, true);
773
[email protected]62d30f42009-10-01 22:36:06774 NotifyExtensionLoaded(extension);
[email protected]0c6da502009-08-14 22:32:39775}
776
[email protected]eaa7dd182010-12-14 11:09:00777void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12778 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20779
[email protected]b2ba9962009-12-10 20:10:15780 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31781 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52782 return;
[email protected]1784e83a2009-09-08 21:01:52783
[email protected]06f92562011-04-29 19:27:31784 const Extension* extension = GetInstalledExtension(extension_id);
785 // |extension| can be NULL if sync disables an extension that is not
786 // installed yet.
787 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:33788 return;
789
[email protected]b3317ad2011-04-28 23:46:00790 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52791
[email protected]06f92562011-04-29 19:27:31792 extension = GetExtensionByIdInternal(extension_id, true, false, true);
793 if (!extension)
794 return;
795
[email protected]1784e83a2009-09-08 21:01:52796 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:57797 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:52798 ExtensionList::iterator iter = std::find(extensions_.begin(),
799 extensions_.end(),
800 extension);
[email protected]06f92562011-04-29 19:27:31801 if (iter != extensions_.end()) {
802 extensions_.erase(iter);
803 } else {
804 iter = std::find(terminated_extensions_.begin(),
805 terminated_extensions_.end(),
806 extension);
807 terminated_extensions_.erase(iter);
808 }
[email protected]1784e83a2009-09-08 21:01:52809
[email protected]a9f39a312010-12-23 22:14:27810 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]1784e83a2009-09-08 21:01:52811}
812
[email protected]eaa7dd182010-12-14 11:09:00813void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25814 CHECK(extension);
815
816 // We only maintain the granted permissions prefs for INTERNAL extensions.
[email protected]139e5732011-03-16 19:29:51817 CHECK_EQ(Extension::INTERNAL, extension->location());
[email protected]8d888c12010-11-30 00:00:25818
819 ExtensionExtent effective_hosts = extension->GetEffectiveHostPermissions();
820 extension_prefs_->AddGrantedPermissions(extension->id(),
821 extension->HasFullPermissions(),
822 extension->api_permissions(),
823 effective_hosts);
824}
825
[email protected]eaa7dd182010-12-14 11:09:00826void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25827 const Extension* extension) {
828 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49829 RecordPermissionMessagesHistogram(
830 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25831 GrantPermissions(extension);
832 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
833 EnableExtension(extension->id());
834}
835
[email protected]eaa7dd182010-12-14 11:09:00836void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]14908b72011-04-20 06:54:36837 if (!BrowserThread::PostTask(
838 BrowserThread::FILE, FROM_HERE,
839 NewRunnableMethod(
840 backend_.get(),
841 &ExtensionServiceBackend::LoadSingleExtension,
842 extension_path)))
843 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32844}
845
[email protected]eaa7dd182010-12-14 11:09:00846void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:34847 for (RegisteredComponentExtensions::iterator it =
848 component_extension_manifests_.begin();
849 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:10850 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:34851 }
852}
853
[email protected]a964e112011-04-14 21:52:51854const Extension* ExtensionService::LoadComponentExtension(
[email protected]61b55b62011-03-24 09:03:10855 const ComponentExtensionInfo &info) {
856 JSONStringValueSerializer serializer(info.manifest);
857 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
858 if (!manifest.get()) {
859 DLOG(ERROR) << "Failed to parse manifest for extension";
[email protected]a964e112011-04-14 21:52:51860 return NULL;
[email protected]61b55b62011-03-24 09:03:10861 }
862
[email protected]83048a22011-03-29 00:14:13863 int flags = Extension::REQUIRE_KEY;
864 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
865 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]61b55b62011-03-24 09:03:10866 std::string error;
867 scoped_refptr<const Extension> extension(Extension::Create(
868 info.root_directory,
869 Extension::COMPONENT,
870 *static_cast<DictionaryValue*>(manifest.get()),
[email protected]83048a22011-03-29 00:14:13871 flags,
[email protected]61b55b62011-03-24 09:03:10872 &error));
873 if (!extension.get()) {
874 NOTREACHED() << error;
[email protected]a964e112011-04-14 21:52:51875 return NULL;
[email protected]61b55b62011-03-24 09:03:10876 }
877 AddExtension(extension);
[email protected]a964e112011-04-14 21:52:51878 return extension;
[email protected]61b55b62011-03-24 09:03:10879}
880
[email protected]eaa7dd182010-12-14 11:09:00881void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:12882 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:21883
[email protected]cc2c3432009-11-06 17:24:36884 base::TimeTicks start_time = base::TimeTicks::Now();
885
[email protected]1952c7d2010-03-04 23:48:34886 // Load any component extensions.
887 LoadComponentExtensions();
888
[email protected]e72e8eb82009-06-18 17:21:51889 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:21890 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:08891 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:06892
[email protected]a315ba92010-11-16 14:12:21893 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
894 bool should_write_prefs = false;
895
896 for (size_t i = 0; i < extensions_info->size(); ++i) {
897 ExtensionInfo* info = extensions_info->at(i).get();
898
899 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
900 ++reload_reason_counts[reload_reason];
901 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
902 reload_reason, 100);
903
904 if (reload_reason != NOT_NEEDED) {
905 // Reloading and extension reads files from disk. We do this on the
906 // UI thread because reloads should be very rare, and the complexity
907 // added by delaying the time when the extensions service knows about
908 // all extensions is significant. See crbug.com/37548 for details.
909 // |allow_io| disables tests that file operations run on the file
910 // thread.
911 base::ThreadRestrictions::ScopedAllowIO allow_io;
912
[email protected]83048a22011-03-29 00:14:13913 int flags = Extension::NO_FLAGS;
914 if (Extension::ShouldDoStrictErrorChecking(info->extension_location))
915 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:48916 if (extension_prefs_->AllowFileAccess(info->extension_id))
917 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]a315ba92010-11-16 14:12:21918 std::string error;
919 scoped_refptr<const Extension> extension(
920 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:31921 info->extension_path,
922 info->extension_location,
[email protected]83048a22011-03-29 00:14:13923 flags,
[email protected]542258c2011-03-04 21:25:31924 &error));
[email protected]a315ba92010-11-16 14:12:21925
926 if (extension.get()) {
927 extensions_info->at(i)->extension_manifest.reset(
928 static_cast<DictionaryValue*>(
929 extension->manifest_value()->DeepCopy()));
930 should_write_prefs = true;
931 }
[email protected]c6d474f82009-12-16 21:11:06932 }
933 }
934
[email protected]a315ba92010-11-16 14:12:21935 for (size_t i = 0; i < extensions_info->size(); ++i) {
936 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:06937 }
938
[email protected]ae09ca62009-08-21 19:46:46939 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:36940
[email protected]a315ba92010-11-16 14:12:21941 // The histograms Extensions.ManifestReload* allow us to validate
942 // the assumption that reloading manifest is a rare event.
943 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
944 reload_reason_counts[NOT_NEEDED]);
945 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
946 reload_reason_counts[UNPACKED_DIR]);
947 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
948 reload_reason_counts[NEEDS_RELOCALIZATION]);
949
[email protected]cc2c3432009-11-06 17:24:36950 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
951 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
952
[email protected]1952c7d2010-03-04 23:48:34953 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
954 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:36955
[email protected]9b217652010-10-08 22:04:23956 int app_count = 0;
957 int hosted_app_count = 0;
958 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:34959 int user_script_count = 0;
960 int extension_count = 0;
961 int theme_count = 0;
962 int external_count = 0;
963 int page_action_count = 0;
964 int browser_action_count = 0;
965 ExtensionList::iterator ex;
966 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:23967 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:08968 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:23969 if ((*ex)->is_app()) {
970 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
971 location, 100);
972 } else if (type == Extension::TYPE_EXTENSION) {
973 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
974 location, 100);
975 }
976
[email protected]1952c7d2010-03-04 23:48:34977 // Don't count component extensions, since they are only extensions as an
978 // implementation detail.
[email protected]9b217652010-10-08 22:04:23979 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:34980 continue;
981
[email protected]e8c729a2010-03-09 19:55:19982 // Don't count unpacked extensions, since they're a developer-specific
983 // feature.
[email protected]9b217652010-10-08 22:04:23984 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:19985 continue;
986
[email protected]9b217652010-10-08 22:04:23987 // Using an enumeration shows us the total installed ratio across all users.
988 // Using the totals per user at each startup tells us the distribution of
989 // usage for each user (e.g. 40% of users have at least one app installed).
990 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
991 switch (type) {
992 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:21993 ++theme_count;
[email protected]9b217652010-10-08 22:04:23994 break;
995 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:21996 ++user_script_count;
[email protected]9b217652010-10-08 22:04:23997 break;
998 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:21999 ++app_count;
1000 ++hosted_app_count;
[email protected]9b217652010-10-08 22:04:231001 break;
1002 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:211003 ++app_count;
1004 ++packaged_app_count;
[email protected]9b217652010-10-08 22:04:231005 break;
1006 case Extension::TYPE_EXTENSION:
1007 default:
[email protected]a315ba92010-11-16 14:12:211008 ++extension_count;
[email protected]9b217652010-10-08 22:04:231009 break;
[email protected]cc2c3432009-11-06 17:24:361010 }
[email protected]9b217652010-10-08 22:04:231011 if (Extension::IsExternalLocation(location))
[email protected]a315ba92010-11-16 14:12:211012 ++external_count;
[email protected]9b217652010-10-08 22:04:231013 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211014 ++page_action_count;
[email protected]9b217652010-10-08 22:04:231015 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211016 ++browser_action_count;
[email protected]fe2dd7742011-04-19 22:52:491017
1018 RecordPermissionMessagesHistogram(
1019 ex->get(), "Extensions.Permissions_Load");
[email protected]cc2c3432009-11-06 17:24:361020 }
[email protected]9b217652010-10-08 22:04:231021 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp", app_count);
1022 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
1023 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]1952c7d2010-03-04 23:48:341024 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
1025 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
1026 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
1027 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
1028 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
1029 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
1030 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:461031}
1032
[email protected]fe2dd7742011-04-19 22:52:491033// static
1034void ExtensionService::RecordPermissionMessagesHistogram(
1035 const Extension* e, const char* histogram) {
1036 // Since this is called from multiple sources, and since the Histogram macros
1037 // use statics, we need to manually lookup the Histogram ourselves.
1038 base::Histogram* counter = base::LinearHistogram::FactoryGet(
1039 histogram,
1040 1,
1041 Extension::PermissionMessage::ID_ENUM_BOUNDARY,
1042 Extension::PermissionMessage::ID_ENUM_BOUNDARY + 1,
1043 base::Histogram::kUmaTargetedHistogramFlag);
1044
1045 std::vector<Extension::PermissionMessage> permissions =
1046 e->GetPermissionMessages();
1047 if (permissions.empty()) {
1048 counter->Add(Extension::PermissionMessage::ID_NONE);
1049 } else {
1050 std::vector<Extension::PermissionMessage>::iterator it;
1051 for (it = permissions.begin(); it != permissions.end(); ++it)
1052 counter->Add(it->message_id());
1053 }
1054}
1055
[email protected]eaa7dd182010-12-14 11:09:001056void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:311057 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:461058 std::string error;
[email protected]9adb9692010-10-29 23:14:021059 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:361060 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
1061 error = errors::kDisabledByPolicy;
1062 } else if (info.extension_manifest.get()) {
[email protected]83048a22011-03-29 00:14:131063 int flags = Extension::NO_FLAGS;
1064 if (info.extension_location != Extension::LOAD)
1065 flags |= Extension::REQUIRE_KEY;
1066 if (Extension::ShouldDoStrictErrorChecking(info.extension_location))
1067 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:481068 if (extension_prefs_->AllowFileAccess(info.extension_id))
1069 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]66e4eb32010-10-27 20:37:411070 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:311071 info.extension_path,
1072 info.extension_location,
1073 *info.extension_manifest,
[email protected]83048a22011-03-29 00:14:131074 flags,
[email protected]542258c2011-03-04 21:25:311075 &error);
[email protected]ae09ca62009-08-21 19:46:461076 } else {
[email protected]c6d474f82009-12-16 21:11:061077 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461078 }
1079
1080 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:061081 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:401082 error,
1083 NotificationType::EXTENSION_INSTALL_ERROR,
1084 false);
[email protected]ae09ca62009-08-21 19:46:461085 return;
1086 }
1087
[email protected]c6d474f82009-12-16 21:11:061088 if (write_to_prefs)
1089 extension_prefs_->UpdateManifest(extension);
1090
[email protected]49cd1572011-02-08 21:38:451091 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:321092}
1093
[email protected]eaa7dd182010-12-14 11:09:001094void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421095 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061096 // was loaded, otherwise a race can arise where a renderer that is created
1097 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421098 // that the request context doesn't yet know about. The profile is responsible
1099 // for ensuring its URLRequestContexts appropriately discover the loaded
1100 // extension.
[email protected]c8d407e2011-04-28 21:27:171101 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061102
[email protected]c8d407e2011-04-28 21:27:171103 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1104 // extension.
[email protected]62d30f42009-10-01 22:36:061105 NotificationService::current()->Notify(
1106 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:341107 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021108 Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:581109
[email protected]c8d407e2011-04-28 21:27:171110 // Tell renderers about the new extension.
[email protected]77a6970c2011-04-23 16:58:561111 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1112 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171113 RenderProcessHost* host = i.GetCurrentValue();
1114 if (host->profile()->GetOriginalProfile() ==
1115 profile_->GetOriginalProfile()) {
1116 host->Send(
1117 new ExtensionMsg_Loaded(ExtensionMsg_Loaded_Params(extension)));
1118 }
[email protected]77a6970c2011-04-23 16:58:561119 }
1120
[email protected]c8d407e2011-04-28 21:27:171121 // Tell a random-ass collection of other subsystems about the new extension.
1122 // TODO(aa): What should we do with all this goop? Can it move into the
1123 // relevant objects via EXTENSION_LOADED?
1124
1125 profile_->GetExtensionSpecialStoragePolicy()->
1126 GrantRightsForExtension(extension);
1127
1128 UpdateActiveExtensionsInCrashReporter();
1129
1130 ExtensionWebUI::RegisterChromeURLOverrides(
1131 profile_, extension->GetChromeURLOverrides());
1132
1133 if (profile_->GetTemplateURLModel())
1134 profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension);
1135
1136 // Load the icon for omnibox-enabled extensions so it will be ready to display
1137 // in the URL bar.
1138 if (!extension->omnibox_keyword().empty()) {
1139 omnibox_popup_icon_manager_.LoadIcon(extension);
1140 omnibox_icon_manager_.LoadIcon(extension);
1141 }
1142
1143 // If the extension has permission to load chrome://favicon/ resources we need
1144 // to make sure that the FaviconSource is registered with the
1145 // ChromeURLDataManager.
1146 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1147 FaviconSource* favicon_source = new FaviconSource(profile_,
1148 FaviconSource::FAVICON);
1149 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
1150 }
1151
1152 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581153 bool plugins_changed = false;
1154 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1155 const Extension::PluginInfo& plugin = extension->plugins()[i];
1156 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1157 webkit::npapi::PluginList::Singleton()->AddExtraPluginPath(plugin.path);
1158 plugins_changed = true;
1159 if (!plugin.is_public) {
1160 PluginService::GetInstance()->RestrictPluginToUrl(
1161 plugin.path, extension->url());
1162 }
1163 }
[email protected]84396dbc2011-04-14 06:33:421164
1165 bool nacl_modules_changed = false;
1166 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1167 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001168 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421169 nacl_modules_changed = true;
1170 }
1171
1172 if (nacl_modules_changed)
[email protected]cebc3dc2011-04-18 17:15:001173 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421174
1175 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581176 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061177}
1178
[email protected]a9f39a312010-12-23 22:14:271179void ExtensionService::NotifyExtensionUnloaded(
1180 const Extension* extension, UnloadedExtensionInfo::Reason reason) {
1181 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:061182 NotificationService::current()->Notify(
1183 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341184 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271185 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061186
[email protected]77a6970c2011-04-23 16:58:561187 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1188 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171189 RenderProcessHost* host = i.GetCurrentValue();
1190 if (host->profile()->GetOriginalProfile() ==
1191 profile_->GetOriginalProfile()) {
1192 host->Send(new ExtensionMsg_Unloaded(extension->id()));
1193 }
[email protected]77a6970c2011-04-23 16:58:561194 }
1195
[email protected]c8d407e2011-04-28 21:27:171196 profile_->UnregisterExtensionWithRequestContexts(extension);
1197 profile_->GetExtensionSpecialStoragePolicy()->
1198 RevokeRightsForExtension(extension);
1199
1200 ExtensionWebUI::UnregisterChromeURLOverrides(
1201 profile_, extension->GetChromeURLOverrides());
1202
[email protected]b777b332011-04-16 04:01:081203#if defined(OS_CHROMEOS)
1204 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171205 if (profile_->GetFileSystemContext() &&
1206 profile_->GetFileSystemContext()->path_manager() &&
1207 profile_->GetFileSystemContext()->path_manager()->external_provider()) {
1208 profile_->GetFileSystemContext()->path_manager()->external_provider()->
1209 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061210 }
[email protected]c8d407e2011-04-28 21:27:171211#endif
1212
1213 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581214
1215 bool plugins_changed = false;
1216 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1217 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361218 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
1219 NewRunnableFunction(&ForceShutdownPlugin,
1220 plugin.path)))
1221 NOTREACHED();
[email protected]2de307592011-04-05 21:16:581222 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1223 webkit::npapi::PluginList::Singleton()->RemoveExtraPluginPath(
1224 plugin.path);
1225 plugins_changed = true;
1226 if (!plugin.is_public)
1227 PluginService::GetInstance()->RestrictPluginToUrl(plugin.path, GURL());
1228 }
[email protected]84396dbc2011-04-14 06:33:421229
1230 bool nacl_modules_changed = false;
1231 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1232 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001233 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421234 nacl_modules_changed = true;
1235 }
1236
1237 if (nacl_modules_changed)
[email protected]cebc3dc2011-04-18 17:15:001238 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421239
1240 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581241 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061242}
1243
[email protected]eaa7dd182010-12-14 11:09:001244void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181245 const std::vector<std::string>& blacklist) {
1246 // Use this set to indicate if an extension in the blacklist has been used.
1247 std::set<std::string> blacklist_set;
1248 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1249 if (Extension::IdIsValid(blacklist[i])) {
1250 blacklist_set.insert(blacklist[i]);
1251 }
1252 }
1253 extension_prefs_->UpdateBlacklist(blacklist_set);
1254 std::vector<std::string> to_be_removed;
1255 // Loop current extensions, unload installed extensions.
1256 for (ExtensionList::const_iterator iter = extensions_.begin();
1257 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021258 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181259 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1260 to_be_removed.push_back(extension->id());
1261 }
1262 }
1263
1264 // UnloadExtension will change the extensions_ list. So, we should
1265 // call it outside the iterator loop.
1266 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]a9f39a312010-12-23 22:14:271267 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]6b75ec32009-08-14 06:37:181268 }
1269}
1270
[email protected]0a071a32011-02-08 00:18:241271Profile* ExtensionService::profile() {
1272 return profile_;
1273}
1274
[email protected]eaa7dd182010-12-14 11:09:001275ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451276 return extension_prefs_;
1277}
1278
[email protected]2859946f2011-04-04 18:18:061279ExtensionUpdater* ExtensionService::updater() {
1280 return updater_.get();
1281}
1282
[email protected]eaa7dd182010-12-14 11:09:001283void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251284 std::vector<std::string> to_be_removed;
1285 // Loop through extensions list, unload installed extensions.
1286 for (ExtensionList::const_iterator iter = extensions_.begin();
1287 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021288 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251289 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1290 to_be_removed.push_back(extension->id());
1291 }
1292
1293 // UnloadExtension will change the extensions_ list. So, we should
1294 // call it outside the iterator loop.
1295 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]a9f39a312010-12-23 22:14:271296 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251297}
1298
[email protected]31206602011-04-13 23:07:321299void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351300 if (updater()) {
[email protected]31206602011-04-13 23:07:321301 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351302 } else {
[email protected]31206602011-04-13 23:07:321303 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351304 }
1305}
1306
[email protected]b05fb9ff2011-04-23 00:07:561307ExtensionSyncData ExtensionService::GetSyncDataHelper(
1308 const Extension& extension) const {
1309 const std::string& id = extension.id();
1310 ExtensionSyncData data;
1311 data.id = id;
1312 data.uninstalled = false;
1313 data.enabled = IsExtensionEnabled(id);
1314 data.incognito_enabled = IsIncognitoEnabled(id);
1315 data.version = *extension.version();
1316 data.update_url = extension.update_url();
1317 data.name = extension.name();
1318 return data;
1319}
1320
1321bool ExtensionService::GetSyncData(
1322 const std::string& id,
1323 ExtensionFilter filter,
1324 ExtensionSyncData* extension_sync_data) const {
1325 DCHECK(Extension::IdIsValid(id));
1326 // TODO(akalin): Figure out what to do with terminated extensions.
1327 const Extension* extension = GetExtensionById(id, true);
1328 if (!extension || !(*filter)(*extension)) {
1329 return false;
1330 }
1331 *extension_sync_data = GetSyncDataHelper(*extension);
1332 return true;
1333}
1334
1335void ExtensionService::GetSyncDataListHelper(
1336 const ExtensionList& extensions,
1337 ExtensionFilter filter,
1338 std::vector<ExtensionSyncData>* sync_data_list) const {
1339 for (ExtensionList::const_iterator it = extensions.begin();
1340 it != extensions.end(); ++it) {
1341 const Extension& extension = **it;
1342 if ((*filter)(extension)) {
1343 sync_data_list->push_back(GetSyncDataHelper(extension));
1344 }
1345 }
1346}
1347
1348std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
1349 ExtensionFilter filter) const {
1350 std::vector<ExtensionSyncData> sync_data_list;
1351 GetSyncDataListHelper(extensions_, filter, &sync_data_list);
1352 GetSyncDataListHelper(disabled_extensions_, filter, &sync_data_list);
1353 // TODO(akalin): Figure out what to do with terminated extensions.
1354 return sync_data_list;
1355}
1356
[email protected]90310d92011-04-17 07:35:041357void ExtensionService::ProcessSyncData(
1358 const ExtensionSyncData& extension_sync_data,
[email protected]b05fb9ff2011-04-23 00:07:561359 ExtensionFilter filter) {
[email protected]90310d92011-04-17 07:35:041360 const std::string& id = extension_sync_data.id;
1361
1362 // Handle uninstalls first.
1363 if (extension_sync_data.uninstalled) {
1364 std::string error;
1365 if (!UninstallExtensionHelper(this, id)) {
1366 LOG(WARNING) << "Could not uninstall extension " << id
1367 << " for sync";
1368 }
1369 return;
1370 }
1371
[email protected]06f92562011-04-29 19:27:311372 // Set user settings.
1373 if (extension_sync_data.enabled) {
1374 EnableExtension(id);
1375 } else {
1376 DisableExtension(id);
1377 }
[email protected]81b14cc2011-04-29 00:39:371378 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled);
1379
[email protected]06f92562011-04-29 19:27:311380 const Extension* extension = GetInstalledExtension(id);
[email protected]90310d92011-04-17 07:35:041381 if (extension) {
[email protected]06f92562011-04-29 19:27:311382 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041383 int result = extension->version()->CompareTo(extension_sync_data.version);
1384 if (result < 0) {
1385 // Extension is outdated.
1386 CheckForUpdatesSoon();
1387 } else if (result > 0) {
1388 // Sync version is outdated. Do nothing for now, as sync code
1389 // in other places will eventually update the sync data.
1390 //
1391 // TODO(akalin): Move that code here.
1392 }
1393 return;
[email protected]06f92562011-04-29 19:27:311394 } else {
1395 // TODO(akalin): Remove need to pass the enabled flag.
1396 //
1397 // TODO(akalin): Replace silent update with a list of enabled
1398 // permissions.
1399 if (!pending_extension_manager()->AddFromSync(
1400 id,
1401 extension_sync_data.update_url,
1402 filter,
1403 true, // install_silently
1404 extension_sync_data.enabled)) {
1405 LOG(WARNING) << "Could not add pending extension for " << id;
1406 return;
1407 }
1408 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041409 }
[email protected]90310d92011-04-17 07:35:041410}
1411
[email protected]c3cfb012011-04-06 22:07:351412bool ExtensionService::IsIncognitoEnabled(
1413 const std::string& extension_id) const {
1414 // If this is an existing component extension we always allow it to
1415 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371416 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351417 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411418 return true;
1419
1420 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351421 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501422}
[email protected]55a35692010-02-11 23:25:211423
[email protected]c3cfb012011-04-06 22:07:351424void ExtensionService::SetIsIncognitoEnabled(
1425 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371426 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351427 if (extension && extension->location() == Extension::COMPONENT) {
1428 // This shouldn't be called for component extensions.
1429 NOTREACHED();
1430 return;
1431 }
1432
[email protected]8c6c8c22011-03-09 12:52:341433 // Broadcast unloaded and loaded events to update browser state. Only bother
1434 // if the value changed and the extension is actually enabled, since there is
1435 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351436 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341437 if (enabled == old_enabled)
1438 return;
1439
[email protected]c3cfb012011-04-06 22:07:351440 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371441
1442 // If the extension is enabled (and not terminated), unload and
1443 // reload it to update UI.
1444 const Extension* enabled_extension = GetExtensionById(extension_id, false);
1445 if (enabled_extension) {
1446 NotifyExtensionUnloaded(enabled_extension, UnloadedExtensionInfo::DISABLE);
1447 NotifyExtensionLoaded(enabled_extension);
[email protected]568f33d2010-08-04 17:06:411448 }
[email protected]55a35692010-02-11 23:25:211449}
1450
[email protected]eaa7dd182010-12-14 11:09:001451bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051452 // We allow the extension to see events and data from another profile iff it
1453 // uses "spanning" behavior and it has incognito access. "split" mode
1454 // extensions only see events for a matching profile.
[email protected]c3cfb012011-04-06 22:07:351455 return IsIncognitoEnabled(extension->id()) &&
1456 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051457}
1458
[email protected]eaa7dd182010-12-14 11:09:001459bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081460 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441461 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081462 extension_prefs_->AllowFileAccess(extension->id()));
1463}
1464
[email protected]eaa7dd182010-12-14 11:09:001465void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481466 bool allow) {
1467 // Reload to update browser state. Only bother if the value changed and the
1468 // extension is actually enabled, since there is no UI otherwise.
1469 bool old_allow = AllowFileAccess(extension);
1470 if (allow == old_allow)
1471 return;
1472
[email protected]05c82182010-06-24 17:49:081473 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481474
1475 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
1476 extension) != extensions_.end();
1477 if (extension_is_enabled)
1478 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081479}
1480
[email protected]eaa7dd182010-12-14 11:09:001481bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391482 return extension_prefs_->GetBrowserActionVisibility(extension);
1483}
1484
[email protected]eaa7dd182010-12-14 11:09:001485void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141486 bool visible) {
[email protected]f681c782010-11-19 11:19:391487 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1488}
1489
[email protected]8e4560b62011-01-14 10:09:141490// Some extensions will autoupdate themselves externally from Chrome. These
1491// are typically part of some larger client application package. To support
1492// these, the extension will register its location in the the preferences file
1493// (and also, on Windows, in the registry) and this code will periodically
1494// check that location for a .crx file, which it will then install locally if
1495// a new version is available.
1496// Errors are reported through ExtensionErrorReporter. Succcess is not
1497// reported.
[email protected]eaa7dd182010-12-14 11:09:001498void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121499 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141500
1501 // Note that this installation is intentionally silent (since it didn't
1502 // go through the front-end). Extensions that are registered in this
1503 // way are effectively considered 'pre-bundled', and so implicitly
1504 // trusted. In general, if something has HKLM or filesystem access,
1505 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121506
1507 // If any external extension records give a URL, a provider will set
1508 // this to true. Used by OnExternalProviderReady() to see if we need
1509 // to start an update check to fetch a new external extension.
1510 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141511
1512 // Ask each external extension provider to give us a call back for each
1513 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1514 ProviderCollection::const_iterator i;
1515 for (i = external_extension_providers_.begin();
1516 i != external_extension_providers_.end(); ++i) {
1517 ExternalExtensionProviderInterface* provider = i->get();
1518 provider->VisitRegisteredExtension();
1519 }
1520
1521 // Uninstall of unclaimed extensions will happen after all the providers
[email protected]a29a517a2011-01-21 21:11:121522 // had reported ready. Every provider calls OnExternalProviderReady()
1523 // when it finishes, and OnExternalProviderReady() only acts when all
1524 // providers are ready. In case there are no providers, we call it
1525 // to trigger removal of extensions that used to have an external source.
1526 if (external_extension_providers_.empty())
1527 OnExternalProviderReady();
[email protected]9f1087e2009-06-15 17:29:321528}
1529
[email protected]8e4560b62011-01-14 10:09:141530void ExtensionService::OnExternalProviderReady() {
[email protected]a29a517a2011-01-21 21:11:121531 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1532
1533 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141534 // if all of them are finished. So we check them first.
1535 ProviderCollection::const_iterator i;
1536 for (i = external_extension_providers_.begin();
1537 i != external_extension_providers_.end(); ++i) {
1538 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121539 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141540 return;
[email protected]8e4560b62011-01-14 10:09:141541 }
1542
[email protected]a29a517a2011-01-21 21:11:121543 // All the providers are ready. Install any pending extensions.
1544 if (external_extension_url_added_ && updater()) {
1545 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141546 updater()->CheckNow();
1547 }
1548
1549 // Uninstall all the unclaimed extensions.
1550 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1551 extension_prefs_->GetInstalledExtensionsInfo());
1552 for (size_t i = 0; i < extensions_info->size(); ++i) {
1553 ExtensionInfo* info = extensions_info->at(i).get();
1554 if (Extension::IsExternalLocation(info->extension_location))
1555 CheckExternalUninstall(info->extension_id);
1556 }
[email protected]6c751e72010-11-23 10:11:101557}
1558
[email protected]a9f39a312010-12-23 22:14:271559void ExtensionService::UnloadExtension(
1560 const std::string& extension_id,
1561 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091562 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021563 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471564 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251565
[email protected]fa0c96732010-11-17 00:14:231566 // This method can be called via PostTask, so the extension may have been
1567 // unloaded by the time this runs.
1568 if (!extension)
1569 return;
[email protected]0c6da502009-08-14 22:32:391570
[email protected]1eb175082010-02-10 09:26:161571 // Keep information about the extension so that we can reload it later
1572 // even if it's not permanently installed.
1573 unloaded_extension_paths_[extension->id()] = extension->path();
1574
[email protected]f17dbd42010-08-16 23:21:101575 // Clean up if the extension is meant to be enabled after a reload.
1576 disabled_extension_paths_.erase(extension->id());
1577
[email protected]d7e9a862010-11-03 21:57:491578 // Clean up runtime data.
1579 extension_runtime_data_.erase(extension_id);
1580
[email protected]0c6da502009-08-14 22:32:391581 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1582 disabled_extensions_.end(),
1583 extension.get());
1584 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:271585 UnloadedExtensionInfo details(extension, reason);
1586 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:391587 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:471588 NotificationService::current()->Notify(
[email protected]a9f39a312010-12-23 22:14:271589 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341590 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271591 Details<UnloadedExtensionInfo>(&details));
[email protected]0c6da502009-08-14 22:32:391592 return;
1593 }
1594
1595 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:571596
[email protected]631cf822009-05-15 07:01:251597 // Remove the extension from our list.
1598 extensions_.erase(iter);
1599
[email protected]a9f39a312010-12-23 22:14:271600 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251601}
1602
[email protected]eaa7dd182010-12-14 11:09:001603void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171604 profile_->GetExtensionSpecialStoragePolicy()->
1605 RevokeRightsForAllExtensions();
1606
[email protected]9f1087e2009-06-15 17:29:321607 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:321608 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:201609 terminated_extension_ids_.clear();
1610 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:491611 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321612
[email protected]c6e4a3412009-06-24 15:45:291613 // TODO(erikkay) should there be a notification for this? We can't use
1614 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1615 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321616}
1617
[email protected]eaa7dd182010-12-14 11:09:001618void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321619 UnloadAllExtensions();
1620 LoadAllExtensions();
1621}
1622
[email protected]eaa7dd182010-12-14 11:09:001623void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501624 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391625 return;
1626
[email protected]ca3dbf52010-05-19 22:27:061627 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1628 extension_prefs_->GetInstalledExtensionsInfo());
1629
1630 std::map<std::string, FilePath> extension_paths;
1631 for (size_t i = 0; i < info->size(); ++i)
1632 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1633
[email protected]14908b72011-04-20 06:54:361634 if (!BrowserThread::PostTask(
1635 BrowserThread::FILE, FROM_HERE,
1636 NewRunnableFunction(
1637 &extension_file_util::GarbageCollectExtensions,
1638 install_directory_,
1639 extension_paths)))
1640 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341641
1642 // Also garbage-collect themes. We check |profile_| to be
1643 // defensive; in the future, we may call GarbageCollectExtensions()
1644 // from somewhere other than Init() (e.g., in a timer).
1645 if (profile_) {
[email protected]18280372011-03-22 18:05:221646 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341647 }
[email protected]3cf4f0992009-02-03 23:00:301648}
1649
[email protected]eaa7dd182010-12-14 11:09:001650void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171651 if (updater_.get()) {
1652 updater_->Start();
1653 }
[email protected]fa6a9102010-11-22 15:38:501654
1655 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:511656 NotificationService::current()->Notify(
1657 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:341658 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:511659 NotificationService::NoDetails());
1660}
1661
[email protected]49cd1572011-02-08 21:38:451662void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461663 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021664 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321665
[email protected]c8d407e2011-04-28 21:27:171666 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1667 // default extension state to DISABLED when the --disable-extensions flag
1668 // is set (http://crbug.com/29067).
1669 if (!extensions_enabled() &&
1670 !extension->is_theme() &&
1671 extension->location() != Extension::COMPONENT &&
1672 !Extension::IsExternalLocation(extension->location()))
1673 return;
1674
1675 SetBeingUpgraded(extension, false);
1676
[email protected]1eb175082010-02-10 09:26:161677 // The extension is now loaded, remove its data from unloaded extension map.
1678 unloaded_extension_paths_.erase(extension->id());
1679
[email protected]bb7f40952011-01-13 00:21:201680 // If a terminated extension is loaded, remove it from the terminated list.
1681 UntrackTerminatedExtension(extension->id());
1682
[email protected]f17dbd42010-08-16 23:21:101683 // If the extension was disabled for a reload, then enable it.
1684 if (disabled_extension_paths_.erase(extension->id()) > 0)
1685 EnableExtension(extension->id());
1686
[email protected]d728e002010-12-08 04:46:231687 // Check if the extension's privileges have changed and disable the
1688 // extension if necessary.
[email protected]8d888c12010-11-30 00:00:251689 DisableIfPrivilegeIncrease(extension);
[email protected]0c6da502009-08-14 22:32:391690
[email protected]c8d407e2011-04-28 21:27:171691 Extension::State state = extension_prefs_->GetExtensionState(extension->id());
1692 if (state == Extension::DISABLED) {
1693 disabled_extensions_.push_back(scoped_extension);
1694 // TODO(aa): This seems dodgy. It seems that AddExtension() could get called
1695 // with a disabled extension for other reasons other than that an update was
1696 // disabled.
1697 NotificationService::current()->Notify(
1698 NotificationType::EXTENSION_UPDATE_DISABLED,
1699 Source<Profile>(profile_),
1700 Details<const Extension>(extension));
1701 return;
[email protected]e72e8eb82009-06-18 17:21:511702 }
[email protected]aab98a52009-12-02 03:22:351703
[email protected]c8d407e2011-04-28 21:27:171704 // It should not be possible to get here with EXTERNAL_EXTENSION_UNINSTALLED
1705 // because we would not have loaded the extension in that case.
1706 CHECK(state == Extension::ENABLED);
1707 extensions_.push_back(scoped_extension);
1708 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:351709}
1710
[email protected]eaa7dd182010-12-14 11:09:001711void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251712 // We keep track of all permissions the user has granted each extension.
1713 // This allows extensions to gracefully support backwards compatibility
1714 // by including unknown permissions in their manifests. When the user
1715 // installs the extension, only the recognized permissions are recorded.
1716 // When the unknown permissions become recognized (e.g., through browser
1717 // upgrade), we can prompt the user to accept these new permissions.
1718 // Extensions can also silently upgrade to less permissions, and then
1719 // silently upgrade to a version that adds these permissions back.
1720 //
1721 // For example, pretend that Chrome 10 includes a permission "omnibox"
1722 // for an API that adds suggestions to the omnibox. An extension can
1723 // maintain backwards compatibility while still having "omnibox" in the
1724 // manifest. If a user installs the extension on Chrome 9, the browser
1725 // will record the permissions it recognized, not including "omnibox."
1726 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1727 // will disable the extension and prompt the user to approve the increase
1728 // in privileges. The extension could then release a new version that
1729 // removes the "omnibox" permission. When the user upgrades, Chrome will
1730 // still remember that "omnibox" had been granted, so that if the
1731 // extension once again includes "omnibox" in an upgrade, the extension
1732 // can upgrade without requiring this user's approval.
1733 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:471734 true, true, false);
[email protected]8d888c12010-11-30 00:00:251735 bool granted_full_access;
1736 std::set<std::string> granted_apis;
1737 ExtensionExtent granted_extent;
1738
1739 bool is_extension_upgrade = old != NULL;
1740 bool is_privilege_increase = false;
1741
1742 // We only record the granted permissions for INTERNAL extensions, since
1743 // they can't silently increase privileges.
1744 if (extension->location() == Extension::INTERNAL) {
1745 // Add all the recognized permissions if the granted permissions list
1746 // hasn't been initialized yet.
1747 if (!extension_prefs_->GetGrantedPermissions(extension->id(),
1748 &granted_full_access,
1749 &granted_apis,
1750 &granted_extent)) {
1751 GrantPermissions(extension);
1752 CHECK(extension_prefs_->GetGrantedPermissions(extension->id(),
1753 &granted_full_access,
1754 &granted_apis,
1755 &granted_extent));
1756 }
1757
1758 // Here, we check if an extension's privileges have increased in a manner
1759 // that requires the user's approval. This could occur because the browser
1760 // upgraded and recognized additional privileges, or an extension upgrades
1761 // to a version that requires additional privileges.
1762 is_privilege_increase = Extension::IsPrivilegeIncrease(
1763 granted_full_access, granted_apis, granted_extent, extension);
1764 }
1765
1766 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:251767 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1768 // that we aren't downgrading.
1769 if (extension->location() != Extension::LOAD)
1770 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:251771
1772 // Extensions get upgraded if the privileges are allowed to increase or
1773 // the privileges haven't increased.
1774 if (!is_privilege_increase) {
1775 SetBeingUpgraded(old, true);
1776 SetBeingUpgraded(extension, true);
1777 }
1778
1779 // To upgrade an extension in place, unload the old one and
1780 // then load the new one.
[email protected]a9f39a312010-12-23 22:14:271781 UnloadExtension(old->id(), UnloadedExtensionInfo::UPDATE);
[email protected]8d888c12010-11-30 00:00:251782 old = NULL;
1783 }
1784
1785 // Extension has changed permissions significantly. Disable it. A
1786 // notification should be sent by the caller.
1787 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:491788 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1789 RecordPermissionMessagesHistogram(
1790 extension, "Extensions.Permissions_AutoDisable");
1791 }
[email protected]b3317ad2011-04-28 23:46:001792 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251793 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1794 }
1795}
1796
[email protected]eaa7dd182010-12-14 11:09:001797void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391798 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:351799 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:341800 if (!extensions_[i]->is_theme() &&
1801 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:391802 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:351803 }
1804
1805 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251806}
1807
[email protected]eaa7dd182010-12-14 11:09:001808void ExtensionService::OnExtensionInstalled(const Extension* extension) {
[email protected]a29a517a2011-01-21 21:11:121809 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201810
[email protected]4416c5a2010-06-26 01:28:571811 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021812 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:351813 const std::string& id = extension->id();
1814 bool initial_enable = false;
[email protected]b2907fd2011-03-25 16:43:371815
1816 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:351817 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
1818 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321819
[email protected]483d1ff2011-03-03 17:12:021820 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:571821 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:541822 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:351823 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:081824 << " and update URL " << extension->update_url().spec()
1825 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541826
1827 NotificationService::current()->Notify(
1828 NotificationType::EXTENSION_INSTALL_NOT_ALLOWED,
1829 Source<Profile>(profile_),
1830 Details<const Extension>(extension));
1831
[email protected]4416c5a2010-06-26 01:28:571832 // Delete the extension directory since we're not going to
1833 // load it.
[email protected]14908b72011-04-20 06:54:361834 if (!BrowserThread::PostTask(
1835 BrowserThread::FILE, FROM_HERE,
1836 NewRunnableFunction(&extension_file_util::DeleteFile,
1837 extension->path(), true)))
1838 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:571839 return;
1840 }
[email protected]8ef78fd2010-08-19 17:14:321841
[email protected]8ef78fd2010-08-19 17:14:321842 if (extension->is_theme()) {
[email protected]483d1ff2011-03-03 17:12:021843 DCHECK(pending_extension_info.enable_on_install());
[email protected]c3cfb012011-04-06 22:07:351844 initial_enable = true;
[email protected]4416c5a2010-06-26 01:28:571845 } else {
[email protected]c3cfb012011-04-06 22:07:351846 initial_enable = pending_extension_info.enable_on_install();
[email protected]4416c5a2010-06-26 01:28:571847 }
[email protected]4416c5a2010-06-26 01:28:571848 } else {
[email protected]c3cfb012011-04-06 22:07:351849 // We explicitly want to re-enable an uninstalled external
1850 // extension; if we're here, that means the user is manually
1851 // installing the extension.
1852 initial_enable =
1853 IsExtensionEnabled(id) || IsExternalExtensionUninstalled(id);
[email protected]aa142702010-03-26 01:26:331854 }
1855
[email protected]9b217652010-10-08 22:04:231856 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
[email protected]7fa19f82010-12-21 19:40:081857 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:491858 RecordPermissionMessagesHistogram(
1859 extension, "Extensions.Permissions_Install");
[email protected]eda3c362010-11-12 08:08:231860 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:571861 extension_prefs_->OnExtensionInstalled(
[email protected]81b14cc2011-04-29 00:39:371862 extension, initial_enable ? Extension::ENABLED : Extension::DISABLED);
[email protected]25b34332009-06-05 21:53:191863
[email protected]3aff9ad2011-04-01 20:26:481864 // Unpacked extensions default to allowing file access, but if that has been
1865 // overridden, don't reset the value.
1866 if (Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) &&
[email protected]c3cfb012011-04-06 22:07:351867 !extension_prefs_->HasAllowFileAccessSetting(id)) {
1868 extension_prefs_->SetAllowFileAccess(id, true);
[email protected]3aff9ad2011-04-01 20:26:481869 }
[email protected]92a5b1d2010-07-20 00:42:001870
[email protected]a3e61e82011-04-15 20:32:081871 NotificationService::current()->Notify(
1872 NotificationType::EXTENSION_INSTALLED,
1873 Source<Profile>(profile_),
1874 Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:581875
[email protected]49cd1572011-02-08 21:38:451876 // Transfer ownership of |extension| to AddExtension.
1877 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:421878}
1879
[email protected]eaa7dd182010-12-14 11:09:001880const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:471881 const std::string& id, bool include_enabled, bool include_disabled,
1882 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:051883 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:391884 if (include_enabled) {
1885 for (ExtensionList::const_iterator iter = extensions_.begin();
1886 iter != extensions_.end(); ++iter) {
1887 if ((*iter)->id() == lowercase_id)
1888 return *iter;
1889 }
1890 }
1891 if (include_disabled) {
1892 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
1893 iter != disabled_extensions_.end(); ++iter) {
1894 if ((*iter)->id() == lowercase_id)
1895 return *iter;
1896 }
[email protected]ce5c4502009-05-06 16:46:111897 }
[email protected]8001df22011-04-28 19:59:471898 if (include_terminated) {
1899 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
1900 iter != terminated_extensions_.end(); ++iter) {
1901 if ((*iter)->id() == lowercase_id)
1902 return *iter;
1903 }
1904 }
[email protected]ce5c4502009-05-06 16:46:111905 return NULL;
1906}
1907
[email protected]bb7f40952011-01-13 00:21:201908void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
1909 if (terminated_extension_ids_.insert(extension->id()).second)
1910 terminated_extensions_.push_back(make_scoped_refptr(extension));
1911}
1912
1913void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
1914 if (terminated_extension_ids_.erase(id) <= 0)
1915 return;
1916
1917 std::string lowercase_id = StringToLowerASCII(id);
1918 for (ExtensionList::iterator iter = terminated_extensions_.begin();
1919 iter != terminated_extensions_.end(); ++iter) {
1920 if ((*iter)->id() == lowercase_id) {
1921 terminated_extensions_.erase(iter);
1922 return;
1923 }
1924 }
1925}
1926
[email protected]0dfe05c2011-02-23 23:03:361927const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:471928 const std::string& id) const {
1929 return GetExtensionByIdInternal(id, false, false, true);
1930}
1931
1932const Extension* ExtensionService::GetInstalledExtension(
1933 const std::string& id) const {
1934 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:361935}
1936
[email protected]eaa7dd182010-12-14 11:09:001937const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:071938 return GetExtensionById(extension_misc::kWebStoreAppId, false);
1939}
1940
[email protected]eaa7dd182010-12-14 11:09:001941const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571942 return url.scheme() != chrome::kExtensionScheme ? NULL :
1943 GetExtensionById(url.host(), false);
1944}
1945
[email protected]eaa7dd182010-12-14 11:09:001946const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571947 for (size_t i = 0; i < extensions_.size(); ++i) {
1948 if (extensions_[i]->web_extent().ContainsURL(url))
1949 return extensions_[i];
1950 }
1951 return NULL;
[email protected]9f1087e2009-06-15 17:29:321952}
1953
[email protected]eaa7dd182010-12-14 11:09:001954bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]583d45c12010-08-31 02:48:121955 // Allow bindings for all packaged extension.
1956 if (GetExtensionByURL(url))
1957 return true;
1958
1959 // Allow bindings for all component, hosted apps.
[email protected]9adb9692010-10-29 23:14:021960 const Extension* extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:121961 return (extension && extension->location() == Extension::COMPONENT);
1962}
1963
[email protected]eaa7dd182010-12-14 11:09:001964const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]9f72aa02010-06-25 10:01:051965 const ExtensionExtent& extent) {
[email protected]22c966c2010-06-26 06:35:021966 for (size_t i = 0; i < extensions_.size(); ++i) {
1967 if (extensions_[i]->web_extent().OverlapsWith(extent))
1968 return extensions_[i];
1969 }
1970
[email protected]6d2e60bd2010-06-03 22:37:391971 return NULL;
1972}
1973
[email protected]eaa7dd182010-12-14 11:09:001974const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:471975 const std::string& extension_id) {
1976 return omnibox_icon_manager_.GetIcon(extension_id);
1977}
1978
[email protected]eaa7dd182010-12-14 11:09:001979const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:311980 const std::string& extension_id) {
1981 return omnibox_popup_icon_manager_.GetIcon(extension_id);
1982}
1983
[email protected]eaa7dd182010-12-14 11:09:001984void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:321985 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:141986 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:321987 const FilePath& path,
1988 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:381989 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1990 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:011991 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:571992 return;
[email protected]a8af9fdb2010-10-28 21:52:201993
[email protected]8e4560b62011-01-14 10:09:141994 DCHECK(version);
1995
[email protected]7577a5c52009-07-30 06:21:581996 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:491997 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:581998 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:021999 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582000 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142001 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582002 case -1: // existing version is older, we should upgrade
2003 break;
2004 case 0: // existing version is same, do nothing
2005 return;
2006 case 1: // existing version is newer, uh-oh
2007 LOG(WARNING) << "Found external version of extension " << id
2008 << "that is older than current version. Current version "
2009 << "is: " << existing->VersionString() << ". New version "
2010 << "is: " << version << ". Keeping current version.";
2011 return;
2012 }
2013 }
2014
[email protected]b2907fd2011-03-25 16:43:372015 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362016
[email protected]14908b72011-04-20 06:54:362017 // no client (silent install)
2018 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(NULL));
[email protected]6dfbbf82010-03-12 23:09:162019 installer->set_install_source(location);
2020 installer->set_expected_id(id);
[email protected]604322d2011-03-22 16:51:562021 installer->set_expected_version(*version),
[email protected]6dfbbf82010-03-12 23:09:162022 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:582023}
2024
[email protected]eaa7dd182010-12-14 11:09:002025void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402026 const FilePath& extension_path,
2027 const std::string &error,
2028 NotificationType type,
2029 bool be_noisy) {
2030 NotificationService* service = NotificationService::current();
2031 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:342032 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:402033 Details<const std::string>(&error));
2034
[email protected]8a205c02011-02-04 20:41:332035 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:042036 std::string message = base::StringPrintf(
2037 "Could not load extension from '%s'. %s",
2038 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:402039 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2040}
2041
[email protected]eaa7dd182010-12-14 11:09:002042void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182043 ExtensionHost* host) {
2044 OrphanedDevTools::iterator iter =
2045 orphaned_dev_tools_.find(host->extension()->id());
2046 if (iter == orphaned_dev_tools_.end())
2047 return;
2048
2049 DevToolsManager::GetInstance()->AttachClientHost(
2050 iter->second, host->render_view_host());
2051 orphaned_dev_tools_.erase(iter);
2052}
2053
[email protected]eaa7dd182010-12-14 11:09:002054void ExtensionService::Observe(NotificationType type,
[email protected]4814b512009-11-07 00:12:292055 const NotificationSource& source,
2056 const NotificationDetails& details) {
2057 switch (type.value) {
[email protected]a4ed6282009-12-14 20:51:162058 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:322059 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
2060 break;
[email protected]a4ed6282009-12-14 20:51:162061
[email protected]f128af42010-08-05 18:05:262062 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
[email protected]bb7f40952011-01-13 00:21:202063 TrackTerminatedExtension(host->extension());
[email protected]f128af42010-08-05 18:05:262064
[email protected]31f77262009-12-02 20:48:532065 // Unload the entire extension. We want it to be in a consistent state:
2066 // either fully working or not loaded at all, but never half-crashed.
[email protected]bc535ee52010-08-31 18:40:322067 // We do it in a PostTask so that other handlers of this notification will
2068 // still have access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362069 MessageLoop::current()->PostTask(
2070 FROM_HERE,
2071 method_factory_.NewRunnableMethod(
2072 &ExtensionService::UnloadExtension,
2073 host->extension()->id(),
2074 UnloadedExtensionInfo::DISABLE));
[email protected]31f77262009-12-02 20:48:532075 break;
2076 }
[email protected]77a6970c2011-04-23 16:58:562077 case NotificationType::RENDERER_PROCESS_CREATED: {
2078 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
2079 // Valid extension function names, used to setup bindings in renderer.
2080 std::vector<std::string> function_names;
2081 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2082 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532083
[email protected]77a6970c2011-04-23 16:58:562084 // Scripting whitelist. This is modified by tests and must be communicated
2085 // to renderers.
2086 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2087 *Extension::GetScriptingWhitelist()));
2088
2089 // Loaded extensions.
2090 for (size_t i = 0; i < extensions_.size(); ++i) {
2091 process->Send(new ExtensionMsg_Loaded(
2092 ExtensionMsg_Loaded_Params(extensions_[i])));
2093 }
2094 break;
2095 }
[email protected]da5683db2011-04-23 17:12:212096 case NotificationType::RENDERER_PROCESS_TERMINATED: {
2097 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
2098 installed_app_hosts_.erase(process->id());
2099 break;
2100 }
[email protected]aa96d3a2010-08-21 08:45:252101 case NotificationType::PREF_CHANGED: {
2102 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102103 if (*pref_name == prefs::kExtensionInstallAllowList ||
2104 *pref_name == prefs::kExtensionInstallDenyList) {
2105 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102106 } else {
2107 NOTREACHED() << "Unexpected preference name.";
2108 }
[email protected]aa96d3a2010-08-21 08:45:252109 break;
2110 }
2111
[email protected]4814b512009-11-07 00:12:292112 default:
2113 NOTREACHED() << "Unexpected notification type.";
2114 }
2115}
2116
[email protected]eaa7dd182010-12-14 11:09:002117bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182118 return !GetAppIds().empty();
2119}
[email protected]377011d2010-07-20 04:18:502120
[email protected]eaa7dd182010-12-14 11:09:002121ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182122 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:502123 for (ExtensionList::const_iterator it = extensions_.begin();
2124 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182125 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2126 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502127 }
2128
[email protected]ec5b50d2010-10-09 16:35:182129 return result;
[email protected]377011d2010-07-20 04:18:502130}
[email protected]d7e9a862010-11-03 21:57:492131
[email protected]14908b72011-04-20 06:54:362132scoped_refptr<CrxInstaller> ExtensionService::MakeCrxInstaller(
2133 ExtensionInstallUI* client) {
2134 return new CrxInstaller(weak_ptr_factory_.GetWeakPtr(), client);
2135}
2136
[email protected]eaa7dd182010-12-14 11:09:002137bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492138 return (extension->background_url().is_empty() ||
2139 extension_runtime_data_[extension->id()].background_page_ready);
2140}
2141
[email protected]eaa7dd182010-12-14 11:09:002142void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492143 DCHECK(!extension->background_url().is_empty());
2144 extension_runtime_data_[extension->id()].background_page_ready = true;
2145 NotificationService::current()->Notify(
2146 NotificationType::EXTENSION_BACKGROUND_PAGE_READY,
2147 Source<const Extension>(extension),
2148 NotificationService::NoDetails());
2149}
2150
[email protected]eaa7dd182010-12-14 11:09:002151bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492152 return extension_runtime_data_[extension->id()].being_upgraded;
2153}
2154
[email protected]eaa7dd182010-12-14 11:09:002155void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492156 bool value) {
2157 extension_runtime_data_[extension->id()].being_upgraded = value;
2158}
[email protected]1bead0712010-11-27 17:41:532159
[email protected]eaa7dd182010-12-14 11:09:002160PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532161 return &extension_runtime_data_[extension->id()].property_bag;
2162}
[email protected]cebc3dc2011-04-18 17:15:002163
2164void ExtensionService::RegisterNaClModule(const GURL& url,
2165 const std::string& mime_type) {
2166 NaClModuleInfo info;
2167 info.url = url;
2168 info.mime_type = mime_type;
2169
2170 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2171 nacl_module_list_.push_front(info);
2172}
2173
2174void ExtensionService::UnregisterNaClModule(const GURL& url) {
2175 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2176 DCHECK(iter != nacl_module_list_.end());
2177 nacl_module_list_.erase(iter);
2178}
2179
2180void ExtensionService::UpdatePluginListWithNaClModules() {
2181 FilePath path;
2182 PathService::Get(chrome::FILE_NACL_PLUGIN, &path);
2183
2184 webkit::npapi::PluginList::Singleton()->UnregisterInternalPlugin(path);
2185
2186 const PepperPluginInfo* pepper_info =
2187 PepperPluginRegistry::GetInstance()->GetInfoForPlugin(path);
2188 webkit::npapi::WebPluginInfo info = pepper_info->ToWebPluginInfo();
2189
2190 DCHECK(nacl_module_list_.size() <= 1);
2191 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2192 iter != nacl_module_list_.end(); ++iter) {
2193 webkit::npapi::WebPluginMimeType mime_type_info;
2194 mime_type_info.mime_type = iter->mime_type;
2195 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2196 mime_type_info.additional_param_values.push_back(
2197 UTF8ToUTF16(iter->url.spec()));
2198 info.mime_types.push_back(mime_type_info);
2199 }
2200
2201 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
2202 webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info);
2203}
2204
2205ExtensionService::NaClModuleInfoList::iterator
2206 ExtensionService::FindNaClModule(const GURL& url) {
2207 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2208 iter != nacl_module_list_.end(); ++iter) {
2209 if (iter->url == url)
2210 return iter;
2211 }
2212 return nacl_module_list_.end();
2213}