blob: e9a0d3c7181c5a46c3577aa0d58595484744227c [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]27072cad2011-05-09 19:46:4092#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
93#include "chrome/browser/extensions/extension_input_ui_api.h"
94#endif
95
[email protected]5ef47ec2010-01-28 05:58:0596using base::Time;
97
[email protected]c6d474f82009-12-16 21:11:0698namespace errors = extension_manifest_errors;
99
[email protected]b6ab96d2009-08-20 18:58:19100namespace {
101
[email protected]29d0d4ac2010-09-08 21:10:31102#if defined(OS_LINUX)
103static const int kOmniboxIconPaddingLeft = 2;
104static const int kOmniboxIconPaddingRight = 2;
105#elif defined(OS_MACOSX)
106static const int kOmniboxIconPaddingLeft = 0;
107static const int kOmniboxIconPaddingRight = 2;
108#else
109static const int kOmniboxIconPaddingLeft = 0;
110static const int kOmniboxIconPaddingRight = 0;
111#endif
112
[email protected]a315ba92010-11-16 14:12:21113// The following enumeration is used in histograms matching
114// Extensions.ManifestReload* . Values may be added, as long
115// as existing values are not changed.
116enum ManifestReloadReason {
117 NOT_NEEDED = 0, // Reload not needed.
118 UNPACKED_DIR, // Unpacked directory
119 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
120 NUM_MANIFEST_RELOAD_REASONS
121};
[email protected]2111b1a2010-03-12 18:12:44122
[email protected]a315ba92010-11-16 14:12:21123ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
124 // Always reload manifests of unpacked extensions, because they can change
125 // on disk independent of the manifest in our prefs.
126 if (info.extension_location == Extension::LOAD)
127 return UNPACKED_DIR;
128
129 // Reload the manifest if it needs to be relocalized.
130 if (extension_l10n_util::ShouldRelocalizeManifest(info))
131 return NEEDS_RELOCALIZATION;
132
133 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44134}
135
[email protected]2de307592011-04-05 21:16:58136static void ForceShutdownPlugin(const FilePath& plugin_path) {
137 PluginProcessHost* plugin =
138 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
139 if (plugin)
140 plugin->ForceShutdown();
141}
142
[email protected]c6d474f82009-12-16 21:11:06143} // namespace
[email protected]b6ab96d2009-08-20 18:58:19144
[email protected]eaa7dd182010-12-14 11:09:00145ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49146 : background_page_ready(false),
147 being_upgraded(false) {
148}
149
[email protected]eaa7dd182010-12-14 11:09:00150ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49151}
152
[email protected]cebc3dc2011-04-18 17:15:00153ExtensionService::NaClModuleInfo::NaClModuleInfo() {
154}
155
156ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
157}
158
[email protected]eaa7dd182010-12-14 11:09:00159// ExtensionService.
[email protected]6014d672008-12-05 00:38:25160
[email protected]eaa7dd182010-12-14 11:09:00161const char* ExtensionService::kInstallDirectoryName = "Extensions";
162const char* ExtensionService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42163
[email protected]eaa7dd182010-12-14 11:09:00164// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22165
[email protected]eaa7dd182010-12-14 11:09:00166class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14167 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22168 public:
[email protected]1f830eb2010-09-28 08:25:14169 // |install_directory| is a path where to look for extensions to load.
[email protected]14908b72011-04-20 06:54:36170 ExtensionServiceBackend(
171 base::WeakPtr<ExtensionService> frontend,
172 const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22173
174 // Loads a single extension from |path| where |path| is the top directory of
175 // a specific extension where its manifest file lives.
176 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45177 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22178 // TODO(erikkay): It might be useful to be able to load a packed extension
179 // (presumably into memory) without installing it.
[email protected]14908b72011-04-20 06:54:36180 void LoadSingleExtension(const FilePath &path);
[email protected]7a4c6852010-09-16 03:44:22181
[email protected]7a4c6852010-09-16 03:44:22182 private:
[email protected]eaa7dd182010-12-14 11:09:00183 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22184
[email protected]eaa7dd182010-12-14 11:09:00185 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22186
[email protected]7a4c6852010-09-16 03:44:22187 // Notify the frontend that there was an error loading an extension.
188 void ReportExtensionLoadError(const FilePath& extension_path,
189 const std::string& error);
190
[email protected]14908b72011-04-20 06:54:36191 // Notify the frontend that an extension was installed.
192 void OnExtensionInstalled(const scoped_refptr<const Extension>& extension);
193
194 base::WeakPtr<ExtensionService> frontend_;
[email protected]7a4c6852010-09-16 03:44:22195
196 // The top-level extensions directory being installed to.
197 FilePath install_directory_;
198
[email protected]eaa7dd182010-12-14 11:09:00199 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22200};
201
[email protected]eaa7dd182010-12-14 11:09:00202ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]14908b72011-04-20 06:54:36203 base::WeakPtr<ExtensionService> frontend,
[email protected]aebe23a32010-12-10 22:15:48204 const FilePath& install_directory)
[email protected]14908b72011-04-20 06:54:36205 : frontend_(frontend),
206 install_directory_(install_directory) {
207 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]7a4c6852010-09-16 03:44:22208}
209
[email protected]eaa7dd182010-12-14 11:09:00210ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]14908b72011-04-20 06:54:36211 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
212 BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]7a4c6852010-09-16 03:44:22213}
214
[email protected]14908b72011-04-20 06:54:36215void ExtensionServiceBackend::LoadSingleExtension(const FilePath& path_in) {
[email protected]a29a517a2011-01-21 21:11:12216 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20217
[email protected]7a4c6852010-09-16 03:44:22218 FilePath extension_path = path_in;
219 file_util::AbsolutePath(&extension_path);
220
[email protected]3aff9ad2011-04-01 20:26:48221 int flags = Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) ?
222 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
[email protected]83048a22011-03-29 00:14:13223 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
224 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]7a4c6852010-09-16 03:44:22225 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27226 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22227 extension_path,
[email protected]92888082010-10-18 19:24:57228 Extension::LOAD,
[email protected]83048a22011-03-29 00:14:13229 flags,
[email protected]ad8e04a2010-11-01 04:16:27230 &error));
[email protected]7a4c6852010-09-16 03:44:22231
232 if (!extension) {
[email protected]14908b72011-04-20 06:54:36233 if (!BrowserThread::PostTask(
234 BrowserThread::UI, FROM_HERE,
235 NewRunnableMethod(
236 this,
237 &ExtensionServiceBackend::ReportExtensionLoadError,
238 extension_path, error)))
239 NOTREACHED() << error;
[email protected]7a4c6852010-09-16 03:44:22240 return;
241 }
242
[email protected]7a4c6852010-09-16 03:44:22243 // Report this as an installed extension so that it gets remembered in the
244 // prefs.
[email protected]14908b72011-04-20 06:54:36245 if (!BrowserThread::PostTask(
246 BrowserThread::UI, FROM_HERE,
247 NewRunnableMethod(
248 this,
249 &ExtensionServiceBackend::OnExtensionInstalled,
250 extension)))
251 NOTREACHED();
[email protected]7a4c6852010-09-16 03:44:22252}
253
[email protected]eaa7dd182010-12-14 11:09:00254void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22255 const FilePath& extension_path, const std::string &error) {
[email protected]14908b72011-04-20 06:54:36256 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
257 if (frontend_.get())
258 frontend_->ReportExtensionLoadError(
259 extension_path, error, NotificationType::EXTENSION_INSTALL_ERROR,
260 true /* alert_on_error */);
261}
262
263void ExtensionServiceBackend::OnExtensionInstalled(
264 const scoped_refptr<const Extension>& extension) {
265 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266 if (frontend_.get())
267 frontend_->OnExtensionInstalled(extension);
[email protected]7a4c6852010-09-16 03:44:22268}
269
[email protected]8e4560b62011-01-14 10:09:14270void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12271 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20272
[email protected]7a4c6852010-09-16 03:44:22273 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21274 ProviderCollection::const_iterator i;
275 for (i = external_extension_providers_.begin();
276 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14277 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21278 if (i->get()->HasExtension(id))
279 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22280 }
281
[email protected]7a4c6852010-09-16 03:44:22282 // This is an external extension that we don't have registered. Uninstall.
[email protected]d6ebc9792011-04-07 18:18:33283 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22284}
285
[email protected]8e4560b62011-01-14 10:09:14286void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22287 external_extension_providers_.clear();
288}
289
[email protected]8e4560b62011-01-14 10:09:14290void ExtensionService::AddProviderForTesting(
291 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12292 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21293 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14294 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22295}
296
[email protected]8e4560b62011-01-14 10:09:14297void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22298 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42299 const GURL& update_url,
300 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38301 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
302 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20303
[email protected]8e4560b62011-01-14 10:09:14304 if (GetExtensionById(id, true)) {
[email protected]7a4c6852010-09-16 03:44:22305 // Already installed. Do not change the update URL that the extension set.
306 return;
307 }
[email protected]b2907fd2011-03-25 16:43:37308 pending_extension_manager()->AddFromExternalUpdateUrl(
309 id, update_url, location);
[email protected]a29a517a2011-01-21 21:11:12310 external_extension_url_added_ |= true;
[email protected]7a4c6852010-09-16 03:44:22311}
312
[email protected]eaa7dd182010-12-14 11:09:00313bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12314 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07315 // Special-case the themes mini-gallery.
316 // TODO(erikkay) When that gallery goes away, remove this code.
317 if (IsDownloadFromMiniGallery(download_url) &&
318 StartsWithASCII(referrer_url.spec(),
319 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44320 return true;
[email protected]1debbbb62010-10-06 17:23:44321 }
[email protected]473ff6e2010-05-12 15:31:55322
[email protected]9adb9692010-10-29 23:14:02323 const Extension* download_extension = GetExtensionByWebExtent(download_url);
324 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
325 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07326
327 bool referrer_valid = (referrer_extension == webstore_app);
328 bool download_valid = (download_extension == webstore_app);
329
330 // If the command-line gallery URL is set, then be a bit more lenient.
331 GURL store_url =
332 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
333 switches::kAppsGalleryURL));
334 if (!store_url.is_empty()) {
335 std::string store_tld =
336 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
337 if (!referrer_valid) {
338 std::string referrer_tld =
339 net::RegistryControlledDomainService::GetDomainAndRegistry(
340 referrer_url);
341 // The referrer gets stripped when transitioning from https to http,
342 // or when hitting an unknown test cert and that commonly happens in
343 // testing environments. Given this, we allow an empty referrer when
344 // the command-line flag is set.
345 // Otherwise, the TLD must match the TLD of the command-line url.
346 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
347 }
348
349 if (!download_valid) {
350 std::string download_tld =
351 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50352 download_url);
[email protected]d3071992010-10-08 15:24:07353
354 // Otherwise, the TLD must match the TLD of the command-line url.
355 download_valid = (download_tld == store_tld);
356 }
357 }
358
359 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23360}
361
[email protected]eaa7dd182010-12-14 11:09:00362bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38363 return StartsWithASCII(download_url.spec(),
364 extension_urls::kMiniGalleryDownloadPrefix,
365 false); // case_sensitive
366}
367
[email protected]d9696672011-03-15 22:45:09368const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27369 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09370 const Extension* app = GetExtensionByWebExtent(url);
371 if (app)
372 return app;
[email protected]ffb204f22010-12-05 23:20:27373
374 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09375 app = GetExtensionByURL(url);
376 if (app && app->is_app())
377 return app;
378
379 return NULL;
380}
381
382bool ExtensionService::IsInstalledApp(const GURL& url) {
383 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27384}
385
[email protected]da5683db2011-04-23 17:12:21386void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
387 const Extension* app) {
388 installed_app_hosts_[renderer_child_id] = app;
389}
390
391const Extension* ExtensionService::GetInstalledAppForRenderer(
392 int renderer_child_id) {
393 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
394 if (i == installed_app_hosts_.end())
395 return NULL;
396 return i->second;
397}
398
[email protected]6aeac8342010-10-01 20:21:18399// static
[email protected]d6ebc9792011-04-07 18:18:33400// This function is used to implement the command-line switch
401// --uninstall-extension. The LOG statements within this function are used to
402// inform the user if the uninstall cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00403bool ExtensionService::UninstallExtensionHelper(
404 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18405 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18406
[email protected]95da88c42011-03-31 10:07:33407 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47408 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33409
[email protected]d6ebc9792011-04-07 18:18:33410 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33411 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18412 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33413 << "id: " << extension_id;
414 return false;
[email protected]6aeac8342010-10-01 20:21:18415 }
416
[email protected]d6ebc9792011-04-07 18:18:33417 // The following call to UninstallExtension will not allow an uninstall of a
418 // policy-controlled extension.
419 std::string error;
420 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
421 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
422 << ": " << error;
423 return false;
424 }
[email protected]95da88c42011-03-31 10:07:33425
[email protected]6aeac8342010-10-01 20:21:18426 return true;
427}
428
[email protected]eaa7dd182010-12-14 11:09:00429ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24430 const CommandLine* command_line,
431 const FilePath& install_directory,
432 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03433 bool autoupdate_enabled,
434 bool extensions_enabled)
[email protected]14908b72011-04-20 06:54:36435 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
436 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
437 profile_(profile),
[email protected]73c47932010-12-06 18:13:43438 extension_prefs_(extension_prefs),
[email protected]14908b72011-04-20 06:54:36439 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23440 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03441 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13442 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55443 ready_(false),
[email protected]14908b72011-04-20 06:54:36444 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]18cc5a52011-04-15 16:03:38445 apps_promo_(profile->GetPrefs()),
[email protected]b3d62312b12010-10-14 21:10:18446 event_routers_initialized_(false) {
[email protected]a29a517a2011-01-21 21:11:12447 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20448
[email protected]36a784c2009-06-23 06:21:08449 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23450 if (command_line->HasSwitch(switches::kDisableExtensions)) {
451 extensions_enabled_ = false;
452 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
453 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18454 }
[email protected]36a784c2009-06-23 06:21:08455
[email protected]a4ed6282009-12-14 20:51:16456 registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32457 NotificationService::AllSources());
[email protected]77a6970c2011-04-23 16:58:56458 registrar_.Add(this, NotificationType::RENDERER_PROCESS_CREATED,
459 NotificationService::AllSources());
[email protected]da5683db2011-04-23 17:12:21460 registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED,
461 NotificationService::AllSources());
[email protected]2fb7dc982010-09-29 12:24:28462 pref_change_registrar_.Init(profile->GetPrefs());
463 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
464 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29465
[email protected]93fd78f42009-07-10 16:43:17466 // Set up the ExtensionUpdater
467 if (autoupdate_enabled) {
468 int update_frequency = kDefaultUpdateFrequencySeconds;
469 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25470 base::StringToInt(command_line->GetSwitchValueASCII(
471 switches::kExtensionsUpdateFrequency),
472 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17473 }
[email protected]82b6e512011-04-12 20:33:27474 updater_.reset(new ExtensionUpdater(this,
475 extension_prefs,
476 profile->GetPrefs(),
477 profile,
478 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17479 }
480
[email protected]14908b72011-04-20 06:54:36481 backend_ =
482 new ExtensionServiceBackend(weak_ptr_factory_.GetWeakPtr(),
483 install_directory_);
[email protected]8e4560b62011-01-14 10:09:14484
[email protected]0436b102011-04-15 18:30:03485 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05486 ExternalExtensionProviderImpl::CreateExternalProviders(
487 this, profile_, &external_extension_providers_);
488 }
[email protected]b671760b2010-07-15 21:13:47489
[email protected]aa96d3a2010-08-21 08:45:25490 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31491 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47492 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31493 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
494 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07495
496 // How long is the path to the Extensions directory?
497 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
498 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25499}
500
[email protected]eaa7dd182010-12-14 11:09:00501const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45502 return &extensions_;
503}
504
[email protected]eaa7dd182010-12-14 11:09:00505const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45506 return &disabled_extensions_;
507}
508
[email protected]bb7f40952011-01-13 00:21:20509const ExtensionList* ExtensionService::terminated_extensions() const {
510 return &terminated_extensions_;
511}
512
[email protected]b2907fd2011-03-25 16:43:37513PendingExtensionManager* ExtensionService::pending_extension_manager() {
514 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45515}
516
[email protected]eaa7dd182010-12-14 11:09:00517ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17518 // No need to unload extensions here because they are profile-scoped, and the
519 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17520
[email protected]8e4560b62011-01-14 10:09:14521 ProviderCollection::const_iterator i;
522 for (i = external_extension_providers_.begin();
523 i != external_extension_providers_.end(); ++i) {
524 ExternalExtensionProviderInterface* provider = i->get();
525 provider->ServiceShutdown();
526 }
[email protected]eed367e2011-04-12 03:43:31527
528#if defined(OS_CHROMEOS)
529 if (event_routers_initialized_) {
530 ExtensionFileBrowserEventRouter::GetInstance()->
531 StopObservingFileSystemEvents();
532 }
533#endif
[email protected]6014d672008-12-05 00:38:25534}
535
[email protected]eaa7dd182010-12-14 11:09:00536void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18537 if (event_routers_initialized_)
538 return;
539
[email protected]c5ae74ab2010-04-15 18:14:37540 ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_);
541 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08542 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
543 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40544 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]8e8bb6d2010-12-13 08:18:55545 ExtensionBookmarkEventRouter::GetInstance()->Observe(
[email protected]c5ae74ab2010-04-15 18:14:37546 profile_->GetBookmarkModel());
[email protected]2c5e1e12010-06-10 13:14:44547 ExtensionCookiesEventRouter::GetInstance()->Init();
[email protected]7596ce72010-08-30 05:10:46548 ExtensionManagementEventRouter::GetInstance()->Init();
[email protected]8a661f82010-10-19 21:47:11549 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]784688a62010-09-13 07:06:52550 ExtensionWebNavigationEventRouter::GetInstance()->Init();
[email protected]27072cad2011-05-09 19:46:40551
[email protected]eed367e2011-04-12 03:43:31552#if defined(OS_CHROMEOS)
553 ExtensionFileBrowserEventRouter::GetInstance()->ObserveFileSystemEvents(
554 profile_);
555#endif
[email protected]27072cad2011-05-09 19:46:40556
557#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
558 ExtensionInputUiEventRouter::GetInstance()->Init();
559#endif
560
[email protected]b3d62312b12010-10-14 21:10:18561 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37562}
563
[email protected]b2907fd2011-03-25 16:43:37564const Extension* ExtensionService::GetExtensionById(
565 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47566 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44567}
568
[email protected]eaa7dd182010-12-14 11:09:00569void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12570 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20571
[email protected]fa6a9102010-11-22 15:38:50572 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17573 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32574
[email protected]95dd38f2009-10-20 20:09:15575 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
576 // the first extension, because its members listen for loaded notifications.
577 g_browser_process->resource_dispatcher_host();
578
[email protected]9f1087e2009-06-15 17:29:32579 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57580
[email protected]9f1087e2009-06-15 17:29:32581 // TODO(erikkay) this should probably be deferred to a future point
582 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17583 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57584
[email protected]9f1087e2009-06-15 17:29:32585 // TODO(erikkay) this should probably be deferred as well.
586 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25587}
588
[email protected]eaa7dd182010-12-14 11:09:00589void ExtensionService::UpdateExtension(const std::string& id,
[email protected]9a8c4022011-01-25 14:25:33590 const FilePath& extension_path,
591 const GURL& download_url) {
[email protected]a29a517a2011-01-21 21:11:12592 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20593
[email protected]b2907fd2011-03-25 16:43:37594 PendingExtensionInfo pending_extension_info;
595 bool is_pending_extension = pending_extension_manager_.GetById(
596 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32597
[email protected]8001df22011-04-28 19:59:47598 const Extension* extension =
599 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41600 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33601 LOG(WARNING) << "Will not update extension " << id
602 << " because it is not installed or pending";
603 // Delete extension_path since we're not creating a CrxInstaller
604 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36605 if (!BrowserThread::PostTask(
606 BrowserThread::FILE, FROM_HERE,
607 NewRunnableFunction(
608 extension_file_util::DeleteFile, extension_path, false)))
609 NOTREACHED();
[email protected]4c967932009-07-31 01:15:49610 return;
[email protected]e957fe52009-06-23 16:51:05611 }
612
[email protected]aa142702010-03-26 01:26:33613 // We want a silent install only for non-pending extensions and
614 // pending extensions that have install_silently set.
615 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37616 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33617 NULL : new ExtensionInstallUI(profile_);
618
[email protected]14908b72011-04-20 06:54:36619 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(client));
[email protected]6dfbbf82010-03-12 23:09:16620 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18621 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37622 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41623 else if (extension)
624 installer->set_install_source(extension->location());
[email protected]6dfbbf82010-03-12 23:09:16625 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34626 installer->set_original_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07627 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16628 installer->InstallCrx(extension_path);
[email protected]e957fe52009-06-23 16:51:05629}
630
[email protected]eaa7dd182010-12-14 11:09:00631void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12632 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06633 FilePath path;
[email protected]9adb9692010-10-29 23:14:02634 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40635
[email protected]f17dbd42010-08-16 23:21:10636 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06637 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29638 // If the extension has an inspector open for its background page, detach
639 // the inspector and hang onto a cookie for it, so that we can reattach
640 // later.
641 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
642 ExtensionHost* host = manager->GetBackgroundHostForExtension(
643 current_extension);
644 if (host) {
645 // Look for an open inspector for the background page.
646 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
647 host->render_view_host());
648 if (devtools_cookie >= 0)
649 orphaned_dev_tools_[extension_id] = devtools_cookie;
650 }
651
[email protected]b65272f2009-08-31 15:47:06652 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10653 DisableExtension(extension_id);
654 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16655 } else {
656 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06657 }
658
[email protected]e6090e42010-03-23 22:44:08659 // Check the installed extensions to see if what we're reloading was already
660 // installed.
661 scoped_ptr<ExtensionInfo> installed_extension(
662 extension_prefs_->GetInstalledExtensionInfo(extension_id));
663 if (installed_extension.get() &&
664 installed_extension->extension_manifest.get()) {
665 LoadInstalledExtension(*installed_extension, false);
666 } else {
667 // We should always be able to remember the extension's path. If it's not in
668 // the map, someone failed to update |unloaded_extension_paths_|.
669 CHECK(!path.empty());
670 LoadExtension(path);
671 }
[email protected]9cddd4702009-07-27 22:09:40672}
673
[email protected]fa2416f2011-05-03 08:41:20674bool ExtensionService::UninstallExtension(
675 const std::string& extension_id_unsafe,
676 bool external_uninstall,
677 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12678 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20679
[email protected]fa2416f2011-05-03 08:41:20680 // Copy the extension identifier since the reference might have been
681 // obtained via Extension::id() and the extension may be deleted in
682 // this function.
683 std::string extension_id(extension_id_unsafe);
684
[email protected]8001df22011-04-28 19:59:47685 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]631cf822009-05-15 07:01:25686
[email protected]e7afe2452010-08-22 16:19:13687 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48688 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32689
[email protected]831aa212010-03-26 13:55:19690 // Get hold of information we need after unloading, since the extension
691 // pointer will be invalid then.
692 GURL extension_url(extension->url());
693 Extension::Location location(extension->location());
[email protected]95da88c42011-03-31 10:07:33694
695 // Policy change which triggers an uninstall will always set
696 // |external_uninstall| to true so this is the only way to uninstall
697 // managed extensions.
[email protected]d6ebc9792011-04-07 18:18:33698 if (!Extension::UserMayDisable(location) && !external_uninstall) {
699 NotificationService::current()->Notify(
700 NotificationType::EXTENSION_UNINSTALL_NOT_ALLOWED,
701 Source<Profile>(profile_),
702 Details<const Extension>(extension));
703 if (error != NULL) {
704 *error = errors::kCannotUninstallManagedExtension;
705 }
706 return false;
707 }
[email protected]95da88c42011-03-31 10:07:33708
[email protected]211030342010-09-30 18:41:06709 UninstalledExtensionInfo uninstalled_extension_info(*extension);
[email protected]831aa212010-03-26 13:55:19710
[email protected]9b217652010-10-08 22:04:23711 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08712 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49713 RecordPermissionMessagesHistogram(
714 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23715
[email protected]56ad3792010-05-28 17:45:33716 if (profile_->GetTemplateURLModel())
717 profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension);
718
[email protected]831aa212010-03-26 13:55:19719 // Unload before doing more cleanup to ensure that nothing is hanging on to
720 // any of these resources.
[email protected]a9f39a312010-12-23 22:14:27721 UnloadExtension(extension_id, UnloadedExtensionInfo::UNINSTALL);
[email protected]831aa212010-03-26 13:55:19722
[email protected]fa2416f2011-05-03 08:41:20723 extension_prefs_->OnExtensionUninstalled(extension_id, location,
[email protected]831aa212010-03-26 13:55:19724 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32725
726 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19727 if (Extension::LOAD != location) {
[email protected]14908b72011-04-20 06:54:36728 if (!BrowserThread::PostTask(
729 BrowserThread::FILE, FROM_HERE,
730 NewRunnableFunction(
731 &extension_file_util::UninstallExtension,
732 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20733 extension_id)))
[email protected]14908b72011-04-20 06:54:36734 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32735 }
736
[email protected]c10da4b02010-03-25 14:38:32737 ClearExtensionData(extension_url);
[email protected]0dfe05c2011-02-23 23:03:36738 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06739
740 // Notify interested parties that we've uninstalled this extension.
741 NotificationService::current()->Notify(
742 NotificationType::EXTENSION_UNINSTALLED,
743 Source<Profile>(profile_),
744 Details<UninstalledExtensionInfo>(&uninstalled_extension_info));
[email protected]d6ebc9792011-04-07 18:18:33745
746 return true;
[email protected]c10da4b02010-03-25 14:38:32747}
748
[email protected]eaa7dd182010-12-14 11:09:00749void ExtensionService::ClearExtensionData(const GURL& extension_url) {
[email protected]c10da4b02010-03-25 14:38:32750 scoped_refptr<ExtensionDataDeleter> deleter(
751 new ExtensionDataDeleter(profile_, extension_url));
752 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32753}
754
[email protected]c3cfb012011-04-06 22:07:35755bool ExtensionService::IsExtensionEnabled(
756 const std::string& extension_id) const {
[email protected]c3cfb012011-04-06 22:07:35757 return
758 extension_prefs_->GetExtensionState(extension_id) == Extension::ENABLED;
759}
760
761bool ExtensionService::IsExternalExtensionUninstalled(
762 const std::string& extension_id) const {
763 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
764}
765
[email protected]eaa7dd182010-12-14 11:09:00766void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12767 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20768
[email protected]06f92562011-04-29 19:27:31769 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39770 return;
[email protected]0c6da502009-08-14 22:32:39771
[email protected]b3317ad2011-04-28 23:46:00772 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52773
[email protected]06f92562011-04-29 19:27:31774 const Extension* extension =
775 GetExtensionByIdInternal(extension_id, false, true, false);
776 // This can happen if sync enables an extension that is not
777 // installed yet.
778 if (!extension)
779 return;
780
[email protected]0c6da502009-08-14 22:32:39781 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57782 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39783 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
784 disabled_extensions_.end(),
785 extension);
786 disabled_extensions_.erase(iter);
787
[email protected]f681c782010-11-19 11:19:39788 // Make sure any browser action contained within it is not hidden.
789 extension_prefs_->SetBrowserActionVisibility(extension, true);
790
[email protected]62d30f42009-10-01 22:36:06791 NotifyExtensionLoaded(extension);
[email protected]0c6da502009-08-14 22:32:39792}
793
[email protected]eaa7dd182010-12-14 11:09:00794void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12795 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20796
[email protected]b2ba9962009-12-10 20:10:15797 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31798 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52799 return;
[email protected]1784e83a2009-09-08 21:01:52800
[email protected]06f92562011-04-29 19:27:31801 const Extension* extension = GetInstalledExtension(extension_id);
802 // |extension| can be NULL if sync disables an extension that is not
803 // installed yet.
804 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:33805 return;
806
[email protected]b3317ad2011-04-28 23:46:00807 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52808
[email protected]06f92562011-04-29 19:27:31809 extension = GetExtensionByIdInternal(extension_id, true, false, true);
810 if (!extension)
811 return;
812
[email protected]1784e83a2009-09-08 21:01:52813 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:57814 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:52815 ExtensionList::iterator iter = std::find(extensions_.begin(),
816 extensions_.end(),
817 extension);
[email protected]06f92562011-04-29 19:27:31818 if (iter != extensions_.end()) {
819 extensions_.erase(iter);
820 } else {
821 iter = std::find(terminated_extensions_.begin(),
822 terminated_extensions_.end(),
823 extension);
824 terminated_extensions_.erase(iter);
825 }
[email protected]1784e83a2009-09-08 21:01:52826
[email protected]a9f39a312010-12-23 22:14:27827 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]1784e83a2009-09-08 21:01:52828}
829
[email protected]eaa7dd182010-12-14 11:09:00830void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25831 CHECK(extension);
832
833 // We only maintain the granted permissions prefs for INTERNAL extensions.
[email protected]139e5732011-03-16 19:29:51834 CHECK_EQ(Extension::INTERNAL, extension->location());
[email protected]8d888c12010-11-30 00:00:25835
836 ExtensionExtent effective_hosts = extension->GetEffectiveHostPermissions();
837 extension_prefs_->AddGrantedPermissions(extension->id(),
838 extension->HasFullPermissions(),
839 extension->api_permissions(),
840 effective_hosts);
841}
842
[email protected]eaa7dd182010-12-14 11:09:00843void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25844 const Extension* extension) {
845 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49846 RecordPermissionMessagesHistogram(
847 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25848 GrantPermissions(extension);
849 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
850 EnableExtension(extension->id());
851}
852
[email protected]eaa7dd182010-12-14 11:09:00853void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]14908b72011-04-20 06:54:36854 if (!BrowserThread::PostTask(
855 BrowserThread::FILE, FROM_HERE,
856 NewRunnableMethod(
857 backend_.get(),
858 &ExtensionServiceBackend::LoadSingleExtension,
859 extension_path)))
860 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32861}
862
[email protected]eaa7dd182010-12-14 11:09:00863void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:34864 for (RegisteredComponentExtensions::iterator it =
865 component_extension_manifests_.begin();
866 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:10867 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:34868 }
869}
870
[email protected]a964e112011-04-14 21:52:51871const Extension* ExtensionService::LoadComponentExtension(
[email protected]61b55b62011-03-24 09:03:10872 const ComponentExtensionInfo &info) {
873 JSONStringValueSerializer serializer(info.manifest);
874 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
875 if (!manifest.get()) {
876 DLOG(ERROR) << "Failed to parse manifest for extension";
[email protected]a964e112011-04-14 21:52:51877 return NULL;
[email protected]61b55b62011-03-24 09:03:10878 }
879
[email protected]83048a22011-03-29 00:14:13880 int flags = Extension::REQUIRE_KEY;
881 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
882 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]61b55b62011-03-24 09:03:10883 std::string error;
884 scoped_refptr<const Extension> extension(Extension::Create(
885 info.root_directory,
886 Extension::COMPONENT,
887 *static_cast<DictionaryValue*>(manifest.get()),
[email protected]83048a22011-03-29 00:14:13888 flags,
[email protected]61b55b62011-03-24 09:03:10889 &error));
890 if (!extension.get()) {
891 NOTREACHED() << error;
[email protected]a964e112011-04-14 21:52:51892 return NULL;
[email protected]61b55b62011-03-24 09:03:10893 }
894 AddExtension(extension);
[email protected]a964e112011-04-14 21:52:51895 return extension;
[email protected]61b55b62011-03-24 09:03:10896}
897
[email protected]eaa7dd182010-12-14 11:09:00898void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:12899 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:21900
[email protected]cc2c3432009-11-06 17:24:36901 base::TimeTicks start_time = base::TimeTicks::Now();
902
[email protected]1952c7d2010-03-04 23:48:34903 // Load any component extensions.
904 LoadComponentExtensions();
905
[email protected]e72e8eb82009-06-18 17:21:51906 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:21907 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:08908 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:06909
[email protected]a315ba92010-11-16 14:12:21910 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
911 bool should_write_prefs = false;
912
913 for (size_t i = 0; i < extensions_info->size(); ++i) {
914 ExtensionInfo* info = extensions_info->at(i).get();
915
916 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
917 ++reload_reason_counts[reload_reason];
918 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
919 reload_reason, 100);
920
921 if (reload_reason != NOT_NEEDED) {
922 // Reloading and extension reads files from disk. We do this on the
923 // UI thread because reloads should be very rare, and the complexity
924 // added by delaying the time when the extensions service knows about
925 // all extensions is significant. See crbug.com/37548 for details.
926 // |allow_io| disables tests that file operations run on the file
927 // thread.
928 base::ThreadRestrictions::ScopedAllowIO allow_io;
929
[email protected]83048a22011-03-29 00:14:13930 int flags = Extension::NO_FLAGS;
931 if (Extension::ShouldDoStrictErrorChecking(info->extension_location))
932 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:48933 if (extension_prefs_->AllowFileAccess(info->extension_id))
934 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]a315ba92010-11-16 14:12:21935 std::string error;
936 scoped_refptr<const Extension> extension(
937 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:31938 info->extension_path,
939 info->extension_location,
[email protected]83048a22011-03-29 00:14:13940 flags,
[email protected]542258c2011-03-04 21:25:31941 &error));
[email protected]a315ba92010-11-16 14:12:21942
943 if (extension.get()) {
944 extensions_info->at(i)->extension_manifest.reset(
945 static_cast<DictionaryValue*>(
946 extension->manifest_value()->DeepCopy()));
947 should_write_prefs = true;
948 }
[email protected]c6d474f82009-12-16 21:11:06949 }
950 }
951
[email protected]a315ba92010-11-16 14:12:21952 for (size_t i = 0; i < extensions_info->size(); ++i) {
953 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:06954 }
955
[email protected]ae09ca62009-08-21 19:46:46956 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:36957
[email protected]a315ba92010-11-16 14:12:21958 // The histograms Extensions.ManifestReload* allow us to validate
959 // the assumption that reloading manifest is a rare event.
960 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
961 reload_reason_counts[NOT_NEEDED]);
962 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
963 reload_reason_counts[UNPACKED_DIR]);
964 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
965 reload_reason_counts[NEEDS_RELOCALIZATION]);
966
[email protected]cc2c3432009-11-06 17:24:36967 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
968 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
969
[email protected]1952c7d2010-03-04 23:48:34970 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
971 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:36972
[email protected]9b217652010-10-08 22:04:23973 int app_count = 0;
974 int hosted_app_count = 0;
975 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:34976 int user_script_count = 0;
977 int extension_count = 0;
978 int theme_count = 0;
979 int external_count = 0;
980 int page_action_count = 0;
981 int browser_action_count = 0;
982 ExtensionList::iterator ex;
983 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:23984 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:08985 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:23986 if ((*ex)->is_app()) {
987 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
988 location, 100);
989 } else if (type == Extension::TYPE_EXTENSION) {
990 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
991 location, 100);
992 }
993
[email protected]1952c7d2010-03-04 23:48:34994 // Don't count component extensions, since they are only extensions as an
995 // implementation detail.
[email protected]9b217652010-10-08 22:04:23996 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:34997 continue;
998
[email protected]e8c729a2010-03-09 19:55:19999 // Don't count unpacked extensions, since they're a developer-specific
1000 // feature.
[email protected]9b217652010-10-08 22:04:231001 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:191002 continue;
1003
[email protected]9b217652010-10-08 22:04:231004 // Using an enumeration shows us the total installed ratio across all users.
1005 // Using the totals per user at each startup tells us the distribution of
1006 // usage for each user (e.g. 40% of users have at least one app installed).
1007 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
1008 switch (type) {
1009 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:211010 ++theme_count;
[email protected]9b217652010-10-08 22:04:231011 break;
1012 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:211013 ++user_script_count;
[email protected]9b217652010-10-08 22:04:231014 break;
1015 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:211016 ++app_count;
1017 ++hosted_app_count;
[email protected]9b217652010-10-08 22:04:231018 break;
1019 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:211020 ++app_count;
1021 ++packaged_app_count;
[email protected]9b217652010-10-08 22:04:231022 break;
1023 case Extension::TYPE_EXTENSION:
1024 default:
[email protected]a315ba92010-11-16 14:12:211025 ++extension_count;
[email protected]9b217652010-10-08 22:04:231026 break;
[email protected]cc2c3432009-11-06 17:24:361027 }
[email protected]9b217652010-10-08 22:04:231028 if (Extension::IsExternalLocation(location))
[email protected]a315ba92010-11-16 14:12:211029 ++external_count;
[email protected]9b217652010-10-08 22:04:231030 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211031 ++page_action_count;
[email protected]9b217652010-10-08 22:04:231032 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211033 ++browser_action_count;
[email protected]fe2dd7742011-04-19 22:52:491034
1035 RecordPermissionMessagesHistogram(
1036 ex->get(), "Extensions.Permissions_Load");
[email protected]cc2c3432009-11-06 17:24:361037 }
[email protected]9b217652010-10-08 22:04:231038 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp", app_count);
1039 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
1040 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]1952c7d2010-03-04 23:48:341041 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
1042 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
1043 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
1044 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
1045 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
1046 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
1047 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:461048}
1049
[email protected]fe2dd7742011-04-19 22:52:491050// static
1051void ExtensionService::RecordPermissionMessagesHistogram(
1052 const Extension* e, const char* histogram) {
1053 // Since this is called from multiple sources, and since the Histogram macros
1054 // use statics, we need to manually lookup the Histogram ourselves.
1055 base::Histogram* counter = base::LinearHistogram::FactoryGet(
1056 histogram,
1057 1,
1058 Extension::PermissionMessage::ID_ENUM_BOUNDARY,
1059 Extension::PermissionMessage::ID_ENUM_BOUNDARY + 1,
1060 base::Histogram::kUmaTargetedHistogramFlag);
1061
1062 std::vector<Extension::PermissionMessage> permissions =
1063 e->GetPermissionMessages();
1064 if (permissions.empty()) {
1065 counter->Add(Extension::PermissionMessage::ID_NONE);
1066 } else {
1067 std::vector<Extension::PermissionMessage>::iterator it;
1068 for (it = permissions.begin(); it != permissions.end(); ++it)
1069 counter->Add(it->message_id());
1070 }
1071}
1072
[email protected]eaa7dd182010-12-14 11:09:001073void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:311074 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:461075 std::string error;
[email protected]9adb9692010-10-29 23:14:021076 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:361077 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
1078 error = errors::kDisabledByPolicy;
1079 } else if (info.extension_manifest.get()) {
[email protected]83048a22011-03-29 00:14:131080 int flags = Extension::NO_FLAGS;
1081 if (info.extension_location != Extension::LOAD)
1082 flags |= Extension::REQUIRE_KEY;
1083 if (Extension::ShouldDoStrictErrorChecking(info.extension_location))
1084 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:481085 if (extension_prefs_->AllowFileAccess(info.extension_id))
1086 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]66e4eb32010-10-27 20:37:411087 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:311088 info.extension_path,
1089 info.extension_location,
1090 *info.extension_manifest,
[email protected]83048a22011-03-29 00:14:131091 flags,
[email protected]542258c2011-03-04 21:25:311092 &error);
[email protected]ae09ca62009-08-21 19:46:461093 } else {
[email protected]c6d474f82009-12-16 21:11:061094 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461095 }
1096
1097 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:061098 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:401099 error,
1100 NotificationType::EXTENSION_INSTALL_ERROR,
1101 false);
[email protected]ae09ca62009-08-21 19:46:461102 return;
1103 }
1104
[email protected]c6d474f82009-12-16 21:11:061105 if (write_to_prefs)
1106 extension_prefs_->UpdateManifest(extension);
1107
[email protected]49cd1572011-02-08 21:38:451108 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:321109}
1110
[email protected]eaa7dd182010-12-14 11:09:001111void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421112 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061113 // was loaded, otherwise a race can arise where a renderer that is created
1114 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421115 // that the request context doesn't yet know about. The profile is responsible
1116 // for ensuring its URLRequestContexts appropriately discover the loaded
1117 // extension.
[email protected]c8d407e2011-04-28 21:27:171118 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061119
[email protected]c8d407e2011-04-28 21:27:171120 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1121 // extension.
[email protected]62d30f42009-10-01 22:36:061122 NotificationService::current()->Notify(
1123 NotificationType::EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:341124 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021125 Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:581126
[email protected]c8d407e2011-04-28 21:27:171127 // Tell renderers about the new extension.
[email protected]77a6970c2011-04-23 16:58:561128 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1129 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171130 RenderProcessHost* host = i.GetCurrentValue();
1131 if (host->profile()->GetOriginalProfile() ==
1132 profile_->GetOriginalProfile()) {
1133 host->Send(
1134 new ExtensionMsg_Loaded(ExtensionMsg_Loaded_Params(extension)));
1135 }
[email protected]77a6970c2011-04-23 16:58:561136 }
1137
[email protected]c8d407e2011-04-28 21:27:171138 // Tell a random-ass collection of other subsystems about the new extension.
1139 // TODO(aa): What should we do with all this goop? Can it move into the
1140 // relevant objects via EXTENSION_LOADED?
1141
1142 profile_->GetExtensionSpecialStoragePolicy()->
1143 GrantRightsForExtension(extension);
1144
1145 UpdateActiveExtensionsInCrashReporter();
1146
1147 ExtensionWebUI::RegisterChromeURLOverrides(
1148 profile_, extension->GetChromeURLOverrides());
1149
1150 if (profile_->GetTemplateURLModel())
1151 profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension);
1152
1153 // Load the icon for omnibox-enabled extensions so it will be ready to display
1154 // in the URL bar.
1155 if (!extension->omnibox_keyword().empty()) {
1156 omnibox_popup_icon_manager_.LoadIcon(extension);
1157 omnibox_icon_manager_.LoadIcon(extension);
1158 }
1159
1160 // If the extension has permission to load chrome://favicon/ resources we need
1161 // to make sure that the FaviconSource is registered with the
1162 // ChromeURLDataManager.
1163 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1164 FaviconSource* favicon_source = new FaviconSource(profile_,
1165 FaviconSource::FAVICON);
1166 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
1167 }
1168
1169 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581170 bool plugins_changed = false;
1171 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1172 const Extension::PluginInfo& plugin = extension->plugins()[i];
1173 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1174 webkit::npapi::PluginList::Singleton()->AddExtraPluginPath(plugin.path);
1175 plugins_changed = true;
1176 if (!plugin.is_public) {
1177 PluginService::GetInstance()->RestrictPluginToUrl(
1178 plugin.path, extension->url());
1179 }
1180 }
[email protected]84396dbc2011-04-14 06:33:421181
1182 bool nacl_modules_changed = false;
1183 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1184 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001185 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421186 nacl_modules_changed = true;
1187 }
1188
1189 if (nacl_modules_changed)
[email protected]cebc3dc2011-04-18 17:15:001190 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421191
1192 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581193 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061194}
1195
[email protected]a9f39a312010-12-23 22:14:271196void ExtensionService::NotifyExtensionUnloaded(
1197 const Extension* extension, UnloadedExtensionInfo::Reason reason) {
1198 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:061199 NotificationService::current()->Notify(
1200 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341201 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271202 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061203
[email protected]77a6970c2011-04-23 16:58:561204 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1205 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171206 RenderProcessHost* host = i.GetCurrentValue();
1207 if (host->profile()->GetOriginalProfile() ==
1208 profile_->GetOriginalProfile()) {
1209 host->Send(new ExtensionMsg_Unloaded(extension->id()));
1210 }
[email protected]77a6970c2011-04-23 16:58:561211 }
1212
[email protected]dd163fb02011-05-04 22:22:171213 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171214 profile_->GetExtensionSpecialStoragePolicy()->
1215 RevokeRightsForExtension(extension);
1216
1217 ExtensionWebUI::UnregisterChromeURLOverrides(
1218 profile_, extension->GetChromeURLOverrides());
1219
[email protected]b777b332011-04-16 04:01:081220#if defined(OS_CHROMEOS)
1221 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171222 if (profile_->GetFileSystemContext() &&
1223 profile_->GetFileSystemContext()->path_manager() &&
1224 profile_->GetFileSystemContext()->path_manager()->external_provider()) {
1225 profile_->GetFileSystemContext()->path_manager()->external_provider()->
1226 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061227 }
[email protected]c8d407e2011-04-28 21:27:171228#endif
1229
1230 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581231
1232 bool plugins_changed = false;
1233 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1234 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361235 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
1236 NewRunnableFunction(&ForceShutdownPlugin,
1237 plugin.path)))
1238 NOTREACHED();
[email protected]2de307592011-04-05 21:16:581239 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1240 webkit::npapi::PluginList::Singleton()->RemoveExtraPluginPath(
1241 plugin.path);
1242 plugins_changed = true;
1243 if (!plugin.is_public)
1244 PluginService::GetInstance()->RestrictPluginToUrl(plugin.path, GURL());
1245 }
[email protected]84396dbc2011-04-14 06:33:421246
1247 bool nacl_modules_changed = false;
1248 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1249 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001250 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421251 nacl_modules_changed = true;
1252 }
1253
1254 if (nacl_modules_changed)
[email protected]cebc3dc2011-04-18 17:15:001255 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421256
1257 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581258 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061259}
1260
[email protected]eaa7dd182010-12-14 11:09:001261void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181262 const std::vector<std::string>& blacklist) {
1263 // Use this set to indicate if an extension in the blacklist has been used.
1264 std::set<std::string> blacklist_set;
1265 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1266 if (Extension::IdIsValid(blacklist[i])) {
1267 blacklist_set.insert(blacklist[i]);
1268 }
1269 }
1270 extension_prefs_->UpdateBlacklist(blacklist_set);
1271 std::vector<std::string> to_be_removed;
1272 // Loop current extensions, unload installed extensions.
1273 for (ExtensionList::const_iterator iter = extensions_.begin();
1274 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021275 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181276 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1277 to_be_removed.push_back(extension->id());
1278 }
1279 }
1280
1281 // UnloadExtension will change the extensions_ list. So, we should
1282 // call it outside the iterator loop.
1283 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]a9f39a312010-12-23 22:14:271284 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]6b75ec32009-08-14 06:37:181285 }
1286}
1287
[email protected]0a071a32011-02-08 00:18:241288Profile* ExtensionService::profile() {
1289 return profile_;
1290}
1291
[email protected]eaa7dd182010-12-14 11:09:001292ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451293 return extension_prefs_;
1294}
1295
[email protected]2859946f2011-04-04 18:18:061296ExtensionUpdater* ExtensionService::updater() {
1297 return updater_.get();
1298}
1299
[email protected]eaa7dd182010-12-14 11:09:001300void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251301 std::vector<std::string> to_be_removed;
1302 // Loop through extensions list, unload installed extensions.
1303 for (ExtensionList::const_iterator iter = extensions_.begin();
1304 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021305 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251306 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1307 to_be_removed.push_back(extension->id());
1308 }
1309
1310 // UnloadExtension will change the extensions_ list. So, we should
1311 // call it outside the iterator loop.
1312 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]a9f39a312010-12-23 22:14:271313 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251314}
1315
[email protected]31206602011-04-13 23:07:321316void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351317 if (updater()) {
[email protected]31206602011-04-13 23:07:321318 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351319 } else {
[email protected]31206602011-04-13 23:07:321320 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351321 }
1322}
1323
[email protected]b05fb9ff2011-04-23 00:07:561324ExtensionSyncData ExtensionService::GetSyncDataHelper(
1325 const Extension& extension) const {
1326 const std::string& id = extension.id();
1327 ExtensionSyncData data;
1328 data.id = id;
1329 data.uninstalled = false;
1330 data.enabled = IsExtensionEnabled(id);
1331 data.incognito_enabled = IsIncognitoEnabled(id);
1332 data.version = *extension.version();
1333 data.update_url = extension.update_url();
1334 data.name = extension.name();
1335 return data;
1336}
1337
1338bool ExtensionService::GetSyncData(
[email protected]85fc9202011-05-05 00:04:591339 const Extension& extension,
[email protected]b05fb9ff2011-04-23 00:07:561340 ExtensionFilter filter,
1341 ExtensionSyncData* extension_sync_data) const {
[email protected]85fc9202011-05-05 00:04:591342 if (!(*filter)(extension)) {
[email protected]b05fb9ff2011-04-23 00:07:561343 return false;
1344 }
[email protected]85fc9202011-05-05 00:04:591345 *extension_sync_data = GetSyncDataHelper(extension);
[email protected]b05fb9ff2011-04-23 00:07:561346 return true;
1347}
1348
1349void ExtensionService::GetSyncDataListHelper(
1350 const ExtensionList& extensions,
1351 ExtensionFilter filter,
1352 std::vector<ExtensionSyncData>* sync_data_list) const {
1353 for (ExtensionList::const_iterator it = extensions.begin();
1354 it != extensions.end(); ++it) {
1355 const Extension& extension = **it;
1356 if ((*filter)(extension)) {
1357 sync_data_list->push_back(GetSyncDataHelper(extension));
1358 }
1359 }
1360}
1361
1362std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
1363 ExtensionFilter filter) const {
1364 std::vector<ExtensionSyncData> sync_data_list;
1365 GetSyncDataListHelper(extensions_, filter, &sync_data_list);
1366 GetSyncDataListHelper(disabled_extensions_, filter, &sync_data_list);
[email protected]45b6fee2011-05-03 09:41:311367 GetSyncDataListHelper(terminated_extensions_, filter, &sync_data_list);
[email protected]b05fb9ff2011-04-23 00:07:561368 return sync_data_list;
1369}
1370
[email protected]90310d92011-04-17 07:35:041371void ExtensionService::ProcessSyncData(
1372 const ExtensionSyncData& extension_sync_data,
[email protected]b05fb9ff2011-04-23 00:07:561373 ExtensionFilter filter) {
[email protected]90310d92011-04-17 07:35:041374 const std::string& id = extension_sync_data.id;
1375
1376 // Handle uninstalls first.
1377 if (extension_sync_data.uninstalled) {
1378 std::string error;
1379 if (!UninstallExtensionHelper(this, id)) {
1380 LOG(WARNING) << "Could not uninstall extension " << id
1381 << " for sync";
1382 }
1383 return;
1384 }
1385
[email protected]06f92562011-04-29 19:27:311386 // Set user settings.
1387 if (extension_sync_data.enabled) {
1388 EnableExtension(id);
1389 } else {
1390 DisableExtension(id);
1391 }
[email protected]81b14cc2011-04-29 00:39:371392 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled);
1393
[email protected]06f92562011-04-29 19:27:311394 const Extension* extension = GetInstalledExtension(id);
[email protected]90310d92011-04-17 07:35:041395 if (extension) {
[email protected]06f92562011-04-29 19:27:311396 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041397 int result = extension->version()->CompareTo(extension_sync_data.version);
1398 if (result < 0) {
1399 // Extension is outdated.
1400 CheckForUpdatesSoon();
1401 } else if (result > 0) {
1402 // Sync version is outdated. Do nothing for now, as sync code
1403 // in other places will eventually update the sync data.
1404 //
1405 // TODO(akalin): Move that code here.
1406 }
[email protected]06f92562011-04-29 19:27:311407 } else {
[email protected]06f92562011-04-29 19:27:311408 // TODO(akalin): Replace silent update with a list of enabled
1409 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331410 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311411 if (!pending_extension_manager()->AddFromSync(
1412 id,
1413 extension_sync_data.update_url,
1414 filter,
[email protected]6cc7dbae2011-04-29 21:18:331415 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311416 LOG(WARNING) << "Could not add pending extension for " << id;
1417 return;
1418 }
1419 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041420 }
[email protected]90310d92011-04-17 07:35:041421}
1422
[email protected]c3cfb012011-04-06 22:07:351423bool ExtensionService::IsIncognitoEnabled(
1424 const std::string& extension_id) const {
1425 // If this is an existing component extension we always allow it to
1426 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371427 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351428 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411429 return true;
1430
1431 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351432 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501433}
[email protected]55a35692010-02-11 23:25:211434
[email protected]c3cfb012011-04-06 22:07:351435void ExtensionService::SetIsIncognitoEnabled(
1436 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371437 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351438 if (extension && extension->location() == Extension::COMPONENT) {
1439 // This shouldn't be called for component extensions.
1440 NOTREACHED();
1441 return;
1442 }
1443
[email protected]8c6c8c22011-03-09 12:52:341444 // Broadcast unloaded and loaded events to update browser state. Only bother
1445 // if the value changed and the extension is actually enabled, since there is
1446 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351447 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341448 if (enabled == old_enabled)
1449 return;
1450
[email protected]c3cfb012011-04-06 22:07:351451 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371452
1453 // If the extension is enabled (and not terminated), unload and
1454 // reload it to update UI.
1455 const Extension* enabled_extension = GetExtensionById(extension_id, false);
1456 if (enabled_extension) {
1457 NotifyExtensionUnloaded(enabled_extension, UnloadedExtensionInfo::DISABLE);
1458 NotifyExtensionLoaded(enabled_extension);
[email protected]568f33d2010-08-04 17:06:411459 }
[email protected]55a35692010-02-11 23:25:211460}
1461
[email protected]eaa7dd182010-12-14 11:09:001462bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051463 // We allow the extension to see events and data from another profile iff it
1464 // uses "spanning" behavior and it has incognito access. "split" mode
1465 // extensions only see events for a matching profile.
[email protected]c3cfb012011-04-06 22:07:351466 return IsIncognitoEnabled(extension->id()) &&
1467 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051468}
1469
[email protected]eaa7dd182010-12-14 11:09:001470bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081471 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441472 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081473 extension_prefs_->AllowFileAccess(extension->id()));
1474}
1475
[email protected]eaa7dd182010-12-14 11:09:001476void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481477 bool allow) {
1478 // Reload to update browser state. Only bother if the value changed and the
1479 // extension is actually enabled, since there is no UI otherwise.
1480 bool old_allow = AllowFileAccess(extension);
1481 if (allow == old_allow)
1482 return;
1483
[email protected]05c82182010-06-24 17:49:081484 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481485
1486 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
1487 extension) != extensions_.end();
1488 if (extension_is_enabled)
1489 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081490}
1491
[email protected]eaa7dd182010-12-14 11:09:001492bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391493 return extension_prefs_->GetBrowserActionVisibility(extension);
1494}
1495
[email protected]eaa7dd182010-12-14 11:09:001496void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141497 bool visible) {
[email protected]f681c782010-11-19 11:19:391498 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1499}
1500
[email protected]8e4560b62011-01-14 10:09:141501// Some extensions will autoupdate themselves externally from Chrome. These
1502// are typically part of some larger client application package. To support
1503// these, the extension will register its location in the the preferences file
1504// (and also, on Windows, in the registry) and this code will periodically
1505// check that location for a .crx file, which it will then install locally if
1506// a new version is available.
1507// Errors are reported through ExtensionErrorReporter. Succcess is not
1508// reported.
[email protected]eaa7dd182010-12-14 11:09:001509void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121510 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141511
1512 // Note that this installation is intentionally silent (since it didn't
1513 // go through the front-end). Extensions that are registered in this
1514 // way are effectively considered 'pre-bundled', and so implicitly
1515 // trusted. In general, if something has HKLM or filesystem access,
1516 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121517
1518 // If any external extension records give a URL, a provider will set
1519 // this to true. Used by OnExternalProviderReady() to see if we need
1520 // to start an update check to fetch a new external extension.
1521 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141522
1523 // Ask each external extension provider to give us a call back for each
1524 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1525 ProviderCollection::const_iterator i;
1526 for (i = external_extension_providers_.begin();
1527 i != external_extension_providers_.end(); ++i) {
1528 ExternalExtensionProviderInterface* provider = i->get();
1529 provider->VisitRegisteredExtension();
1530 }
1531
1532 // Uninstall of unclaimed extensions will happen after all the providers
[email protected]a29a517a2011-01-21 21:11:121533 // had reported ready. Every provider calls OnExternalProviderReady()
1534 // when it finishes, and OnExternalProviderReady() only acts when all
1535 // providers are ready. In case there are no providers, we call it
1536 // to trigger removal of extensions that used to have an external source.
1537 if (external_extension_providers_.empty())
1538 OnExternalProviderReady();
[email protected]9f1087e2009-06-15 17:29:321539}
1540
[email protected]8e4560b62011-01-14 10:09:141541void ExtensionService::OnExternalProviderReady() {
[email protected]a29a517a2011-01-21 21:11:121542 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1543
1544 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141545 // if all of them are finished. So we check them first.
1546 ProviderCollection::const_iterator i;
1547 for (i = external_extension_providers_.begin();
1548 i != external_extension_providers_.end(); ++i) {
1549 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121550 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141551 return;
[email protected]8e4560b62011-01-14 10:09:141552 }
1553
[email protected]a29a517a2011-01-21 21:11:121554 // All the providers are ready. Install any pending extensions.
1555 if (external_extension_url_added_ && updater()) {
1556 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141557 updater()->CheckNow();
1558 }
1559
1560 // Uninstall all the unclaimed extensions.
1561 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1562 extension_prefs_->GetInstalledExtensionsInfo());
1563 for (size_t i = 0; i < extensions_info->size(); ++i) {
1564 ExtensionInfo* info = extensions_info->at(i).get();
1565 if (Extension::IsExternalLocation(info->extension_location))
1566 CheckExternalUninstall(info->extension_id);
1567 }
[email protected]6c751e72010-11-23 10:11:101568}
1569
[email protected]a9f39a312010-12-23 22:14:271570void ExtensionService::UnloadExtension(
1571 const std::string& extension_id,
1572 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091573 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021574 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471575 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251576
[email protected]fa0c96732010-11-17 00:14:231577 // This method can be called via PostTask, so the extension may have been
1578 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171579 if (!extension) {
1580 // In case the extension may have crashed/uninstalled. Allow the profile to
1581 // clean up its RequestContexts.
1582 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231583 return;
[email protected]dd163fb02011-05-04 22:22:171584 }
[email protected]0c6da502009-08-14 22:32:391585
[email protected]1eb175082010-02-10 09:26:161586 // Keep information about the extension so that we can reload it later
1587 // even if it's not permanently installed.
1588 unloaded_extension_paths_[extension->id()] = extension->path();
1589
[email protected]f17dbd42010-08-16 23:21:101590 // Clean up if the extension is meant to be enabled after a reload.
1591 disabled_extension_paths_.erase(extension->id());
1592
[email protected]d7e9a862010-11-03 21:57:491593 // Clean up runtime data.
1594 extension_runtime_data_.erase(extension_id);
1595
[email protected]0c6da502009-08-14 22:32:391596 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1597 disabled_extensions_.end(),
1598 extension.get());
1599 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:271600 UnloadedExtensionInfo details(extension, reason);
1601 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:391602 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:471603 NotificationService::current()->Notify(
[email protected]a9f39a312010-12-23 22:14:271604 NotificationType::EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341605 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271606 Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171607 // Make sure the profile cleans up its RequestContexts when an already
1608 // disabled extension is unloaded (since they are also tracking the disabled
1609 // extensions).
1610 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391611 return;
1612 }
1613
1614 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:571615
[email protected]631cf822009-05-15 07:01:251616 // Remove the extension from our list.
1617 extensions_.erase(iter);
1618
[email protected]a9f39a312010-12-23 22:14:271619 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251620}
1621
[email protected]eaa7dd182010-12-14 11:09:001622void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171623 profile_->GetExtensionSpecialStoragePolicy()->
1624 RevokeRightsForAllExtensions();
1625
[email protected]9f1087e2009-06-15 17:29:321626 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:321627 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:201628 terminated_extension_ids_.clear();
1629 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:491630 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321631
[email protected]c6e4a3412009-06-24 15:45:291632 // TODO(erikkay) should there be a notification for this? We can't use
1633 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1634 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321635}
1636
[email protected]eaa7dd182010-12-14 11:09:001637void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321638 UnloadAllExtensions();
1639 LoadAllExtensions();
1640}
1641
[email protected]eaa7dd182010-12-14 11:09:001642void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501643 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391644 return;
1645
[email protected]ca3dbf52010-05-19 22:27:061646 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1647 extension_prefs_->GetInstalledExtensionsInfo());
1648
1649 std::map<std::string, FilePath> extension_paths;
1650 for (size_t i = 0; i < info->size(); ++i)
1651 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1652
[email protected]14908b72011-04-20 06:54:361653 if (!BrowserThread::PostTask(
1654 BrowserThread::FILE, FROM_HERE,
1655 NewRunnableFunction(
1656 &extension_file_util::GarbageCollectExtensions,
1657 install_directory_,
1658 extension_paths)))
1659 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341660
1661 // Also garbage-collect themes. We check |profile_| to be
1662 // defensive; in the future, we may call GarbageCollectExtensions()
1663 // from somewhere other than Init() (e.g., in a timer).
1664 if (profile_) {
[email protected]18280372011-03-22 18:05:221665 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341666 }
[email protected]3cf4f0992009-02-03 23:00:301667}
1668
[email protected]eaa7dd182010-12-14 11:09:001669void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171670 if (updater_.get()) {
1671 updater_->Start();
1672 }
[email protected]fa6a9102010-11-22 15:38:501673
1674 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:511675 NotificationService::current()->Notify(
1676 NotificationType::EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:341677 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:511678 NotificationService::NoDetails());
1679}
1680
[email protected]49cd1572011-02-08 21:38:451681void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461682 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021683 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321684
[email protected]c8d407e2011-04-28 21:27:171685 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1686 // default extension state to DISABLED when the --disable-extensions flag
1687 // is set (http://crbug.com/29067).
1688 if (!extensions_enabled() &&
1689 !extension->is_theme() &&
1690 extension->location() != Extension::COMPONENT &&
1691 !Extension::IsExternalLocation(extension->location()))
1692 return;
1693
1694 SetBeingUpgraded(extension, false);
1695
[email protected]1eb175082010-02-10 09:26:161696 // The extension is now loaded, remove its data from unloaded extension map.
1697 unloaded_extension_paths_.erase(extension->id());
1698
[email protected]bb7f40952011-01-13 00:21:201699 // If a terminated extension is loaded, remove it from the terminated list.
1700 UntrackTerminatedExtension(extension->id());
1701
[email protected]f17dbd42010-08-16 23:21:101702 // If the extension was disabled for a reload, then enable it.
1703 if (disabled_extension_paths_.erase(extension->id()) > 0)
1704 EnableExtension(extension->id());
1705
[email protected]d728e002010-12-08 04:46:231706 // Check if the extension's privileges have changed and disable the
1707 // extension if necessary.
[email protected]8d888c12010-11-30 00:00:251708 DisableIfPrivilegeIncrease(extension);
[email protected]0c6da502009-08-14 22:32:391709
[email protected]c8d407e2011-04-28 21:27:171710 Extension::State state = extension_prefs_->GetExtensionState(extension->id());
1711 if (state == Extension::DISABLED) {
1712 disabled_extensions_.push_back(scoped_extension);
1713 // TODO(aa): This seems dodgy. It seems that AddExtension() could get called
1714 // with a disabled extension for other reasons other than that an update was
1715 // disabled.
1716 NotificationService::current()->Notify(
1717 NotificationType::EXTENSION_UPDATE_DISABLED,
1718 Source<Profile>(profile_),
1719 Details<const Extension>(extension));
1720 return;
[email protected]e72e8eb82009-06-18 17:21:511721 }
[email protected]aab98a52009-12-02 03:22:351722
[email protected]c8d407e2011-04-28 21:27:171723 // It should not be possible to get here with EXTERNAL_EXTENSION_UNINSTALLED
1724 // because we would not have loaded the extension in that case.
1725 CHECK(state == Extension::ENABLED);
1726 extensions_.push_back(scoped_extension);
1727 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:351728}
1729
[email protected]eaa7dd182010-12-14 11:09:001730void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251731 // We keep track of all permissions the user has granted each extension.
1732 // This allows extensions to gracefully support backwards compatibility
1733 // by including unknown permissions in their manifests. When the user
1734 // installs the extension, only the recognized permissions are recorded.
1735 // When the unknown permissions become recognized (e.g., through browser
1736 // upgrade), we can prompt the user to accept these new permissions.
1737 // Extensions can also silently upgrade to less permissions, and then
1738 // silently upgrade to a version that adds these permissions back.
1739 //
1740 // For example, pretend that Chrome 10 includes a permission "omnibox"
1741 // for an API that adds suggestions to the omnibox. An extension can
1742 // maintain backwards compatibility while still having "omnibox" in the
1743 // manifest. If a user installs the extension on Chrome 9, the browser
1744 // will record the permissions it recognized, not including "omnibox."
1745 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1746 // will disable the extension and prompt the user to approve the increase
1747 // in privileges. The extension could then release a new version that
1748 // removes the "omnibox" permission. When the user upgrades, Chrome will
1749 // still remember that "omnibox" had been granted, so that if the
1750 // extension once again includes "omnibox" in an upgrade, the extension
1751 // can upgrade without requiring this user's approval.
1752 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:471753 true, true, false);
[email protected]8d888c12010-11-30 00:00:251754 bool granted_full_access;
1755 std::set<std::string> granted_apis;
1756 ExtensionExtent granted_extent;
1757
1758 bool is_extension_upgrade = old != NULL;
1759 bool is_privilege_increase = false;
1760
1761 // We only record the granted permissions for INTERNAL extensions, since
1762 // they can't silently increase privileges.
1763 if (extension->location() == Extension::INTERNAL) {
1764 // Add all the recognized permissions if the granted permissions list
1765 // hasn't been initialized yet.
1766 if (!extension_prefs_->GetGrantedPermissions(extension->id(),
1767 &granted_full_access,
1768 &granted_apis,
1769 &granted_extent)) {
1770 GrantPermissions(extension);
1771 CHECK(extension_prefs_->GetGrantedPermissions(extension->id(),
1772 &granted_full_access,
1773 &granted_apis,
1774 &granted_extent));
1775 }
1776
1777 // Here, we check if an extension's privileges have increased in a manner
1778 // that requires the user's approval. This could occur because the browser
1779 // upgraded and recognized additional privileges, or an extension upgrades
1780 // to a version that requires additional privileges.
1781 is_privilege_increase = Extension::IsPrivilegeIncrease(
1782 granted_full_access, granted_apis, granted_extent, extension);
1783 }
1784
1785 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:251786 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1787 // that we aren't downgrading.
1788 if (extension->location() != Extension::LOAD)
1789 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:251790
1791 // Extensions get upgraded if the privileges are allowed to increase or
1792 // the privileges haven't increased.
1793 if (!is_privilege_increase) {
1794 SetBeingUpgraded(old, true);
1795 SetBeingUpgraded(extension, true);
1796 }
1797
1798 // To upgrade an extension in place, unload the old one and
1799 // then load the new one.
[email protected]a9f39a312010-12-23 22:14:271800 UnloadExtension(old->id(), UnloadedExtensionInfo::UPDATE);
[email protected]8d888c12010-11-30 00:00:251801 old = NULL;
1802 }
1803
1804 // Extension has changed permissions significantly. Disable it. A
1805 // notification should be sent by the caller.
1806 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:491807 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
1808 RecordPermissionMessagesHistogram(
1809 extension, "Extensions.Permissions_AutoDisable");
1810 }
[email protected]b3317ad2011-04-28 23:46:001811 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:251812 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
1813 }
1814}
1815
[email protected]eaa7dd182010-12-14 11:09:001816void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:391817 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:351818 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:341819 if (!extensions_[i]->is_theme() &&
1820 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:391821 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:351822 }
1823
1824 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:251825}
1826
[email protected]eaa7dd182010-12-14 11:09:001827void ExtensionService::OnExtensionInstalled(const Extension* extension) {
[email protected]a29a517a2011-01-21 21:11:121828 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:201829
[email protected]4416c5a2010-06-26 01:28:571830 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021831 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:351832 const std::string& id = extension->id();
[email protected]6cc7dbae2011-04-29 21:18:331833 bool initial_enable = IsExtensionEnabled(id);
[email protected]b2907fd2011-03-25 16:43:371834
1835 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:351836 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
1837 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:321838
[email protected]483d1ff2011-03-03 17:12:021839 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:571840 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:541841 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:351842 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:081843 << " and update URL " << extension->update_url().spec()
1844 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:541845
1846 NotificationService::current()->Notify(
1847 NotificationType::EXTENSION_INSTALL_NOT_ALLOWED,
1848 Source<Profile>(profile_),
1849 Details<const Extension>(extension));
1850
[email protected]4416c5a2010-06-26 01:28:571851 // Delete the extension directory since we're not going to
1852 // load it.
[email protected]14908b72011-04-20 06:54:361853 if (!BrowserThread::PostTask(
1854 BrowserThread::FILE, FROM_HERE,
1855 NewRunnableFunction(&extension_file_util::DeleteFile,
1856 extension->path(), true)))
1857 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:571858 return;
1859 }
[email protected]4416c5a2010-06-26 01:28:571860 } else {
[email protected]c3cfb012011-04-06 22:07:351861 // We explicitly want to re-enable an uninstalled external
1862 // extension; if we're here, that means the user is manually
1863 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:331864 if (IsExternalExtensionUninstalled(id)) {
1865 initial_enable = true;
1866 }
[email protected]aa142702010-03-26 01:26:331867 }
1868
[email protected]9b217652010-10-08 22:04:231869 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
[email protected]7fa19f82010-12-21 19:40:081870 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:491871 RecordPermissionMessagesHistogram(
1872 extension, "Extensions.Permissions_Install");
[email protected]eda3c362010-11-12 08:08:231873 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:571874 extension_prefs_->OnExtensionInstalled(
[email protected]81b14cc2011-04-29 00:39:371875 extension, initial_enable ? Extension::ENABLED : Extension::DISABLED);
[email protected]25b34332009-06-05 21:53:191876
[email protected]3aff9ad2011-04-01 20:26:481877 // Unpacked extensions default to allowing file access, but if that has been
1878 // overridden, don't reset the value.
1879 if (Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) &&
[email protected]c3cfb012011-04-06 22:07:351880 !extension_prefs_->HasAllowFileAccessSetting(id)) {
1881 extension_prefs_->SetAllowFileAccess(id, true);
[email protected]3aff9ad2011-04-01 20:26:481882 }
[email protected]92a5b1d2010-07-20 00:42:001883
[email protected]a3e61e82011-04-15 20:32:081884 NotificationService::current()->Notify(
1885 NotificationType::EXTENSION_INSTALLED,
1886 Source<Profile>(profile_),
1887 Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:581888
[email protected]49cd1572011-02-08 21:38:451889 // Transfer ownership of |extension| to AddExtension.
1890 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:421891}
1892
[email protected]eaa7dd182010-12-14 11:09:001893const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:471894 const std::string& id, bool include_enabled, bool include_disabled,
1895 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:051896 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:391897 if (include_enabled) {
1898 for (ExtensionList::const_iterator iter = extensions_.begin();
1899 iter != extensions_.end(); ++iter) {
1900 if ((*iter)->id() == lowercase_id)
1901 return *iter;
1902 }
1903 }
1904 if (include_disabled) {
1905 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
1906 iter != disabled_extensions_.end(); ++iter) {
1907 if ((*iter)->id() == lowercase_id)
1908 return *iter;
1909 }
[email protected]ce5c4502009-05-06 16:46:111910 }
[email protected]8001df22011-04-28 19:59:471911 if (include_terminated) {
1912 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
1913 iter != terminated_extensions_.end(); ++iter) {
1914 if ((*iter)->id() == lowercase_id)
1915 return *iter;
1916 }
1917 }
[email protected]ce5c4502009-05-06 16:46:111918 return NULL;
1919}
1920
[email protected]bb7f40952011-01-13 00:21:201921void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
1922 if (terminated_extension_ids_.insert(extension->id()).second)
1923 terminated_extensions_.push_back(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:201924
1925 UnloadExtension(extension->id(), UnloadedExtensionInfo::DISABLE);
[email protected]bb7f40952011-01-13 00:21:201926}
1927
1928void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:201929 std::string lowercase_id = StringToLowerASCII(id);
1930 if (terminated_extension_ids_.erase(lowercase_id) <= 0)
[email protected]bb7f40952011-01-13 00:21:201931 return;
1932
[email protected]bb7f40952011-01-13 00:21:201933 for (ExtensionList::iterator iter = terminated_extensions_.begin();
1934 iter != terminated_extensions_.end(); ++iter) {
1935 if ((*iter)->id() == lowercase_id) {
1936 terminated_extensions_.erase(iter);
1937 return;
1938 }
1939 }
1940}
1941
[email protected]0dfe05c2011-02-23 23:03:361942const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:471943 const std::string& id) const {
1944 return GetExtensionByIdInternal(id, false, false, true);
1945}
1946
1947const Extension* ExtensionService::GetInstalledExtension(
1948 const std::string& id) const {
1949 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:361950}
1951
[email protected]eaa7dd182010-12-14 11:09:001952const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:071953 return GetExtensionById(extension_misc::kWebStoreAppId, false);
1954}
1955
[email protected]eaa7dd182010-12-14 11:09:001956const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571957 return url.scheme() != chrome::kExtensionScheme ? NULL :
1958 GetExtensionById(url.host(), false);
1959}
1960
[email protected]eaa7dd182010-12-14 11:09:001961const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:571962 for (size_t i = 0; i < extensions_.size(); ++i) {
1963 if (extensions_[i]->web_extent().ContainsURL(url))
1964 return extensions_[i];
1965 }
1966 return NULL;
[email protected]9f1087e2009-06-15 17:29:321967}
1968
[email protected]eaa7dd182010-12-14 11:09:001969bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]583d45c12010-08-31 02:48:121970 // Allow bindings for all packaged extension.
1971 if (GetExtensionByURL(url))
1972 return true;
1973
1974 // Allow bindings for all component, hosted apps.
[email protected]9adb9692010-10-29 23:14:021975 const Extension* extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:121976 return (extension && extension->location() == Extension::COMPONENT);
1977}
1978
[email protected]eaa7dd182010-12-14 11:09:001979const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]9f72aa02010-06-25 10:01:051980 const ExtensionExtent& extent) {
[email protected]22c966c2010-06-26 06:35:021981 for (size_t i = 0; i < extensions_.size(); ++i) {
1982 if (extensions_[i]->web_extent().OverlapsWith(extent))
1983 return extensions_[i];
1984 }
1985
[email protected]6d2e60bd2010-06-03 22:37:391986 return NULL;
1987}
1988
[email protected]eaa7dd182010-12-14 11:09:001989const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:471990 const std::string& extension_id) {
1991 return omnibox_icon_manager_.GetIcon(extension_id);
1992}
1993
[email protected]eaa7dd182010-12-14 11:09:001994const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:311995 const std::string& extension_id) {
1996 return omnibox_popup_icon_manager_.GetIcon(extension_id);
1997}
1998
[email protected]eaa7dd182010-12-14 11:09:001999void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322000 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142001 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322002 const FilePath& path,
2003 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:382004 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2005 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012006 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:572007 return;
[email protected]a8af9fdb2010-10-28 21:52:202008
[email protected]8e4560b62011-01-14 10:09:142009 DCHECK(version);
2010
[email protected]7577a5c52009-07-30 06:21:582011 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492012 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582013 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022014 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582015 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142016 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582017 case -1: // existing version is older, we should upgrade
2018 break;
2019 case 0: // existing version is same, do nothing
2020 return;
2021 case 1: // existing version is newer, uh-oh
2022 LOG(WARNING) << "Found external version of extension " << id
2023 << "that is older than current version. Current version "
2024 << "is: " << existing->VersionString() << ". New version "
2025 << "is: " << version << ". Keeping current version.";
2026 return;
2027 }
2028 }
2029
[email protected]b2907fd2011-03-25 16:43:372030 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362031
[email protected]14908b72011-04-20 06:54:362032 // no client (silent install)
2033 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(NULL));
[email protected]6dfbbf82010-03-12 23:09:162034 installer->set_install_source(location);
2035 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072036 installer->set_expected_version(*version);
2037 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]6dfbbf82010-03-12 23:09:162038 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:582039}
2040
[email protected]eaa7dd182010-12-14 11:09:002041void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402042 const FilePath& extension_path,
2043 const std::string &error,
2044 NotificationType type,
2045 bool be_noisy) {
2046 NotificationService* service = NotificationService::current();
2047 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:342048 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:402049 Details<const std::string>(&error));
2050
[email protected]8a205c02011-02-04 20:41:332051 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:042052 std::string message = base::StringPrintf(
2053 "Could not load extension from '%s'. %s",
2054 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:402055 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2056}
2057
[email protected]eaa7dd182010-12-14 11:09:002058void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182059 ExtensionHost* host) {
2060 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332061 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182062 if (iter == orphaned_dev_tools_.end())
2063 return;
2064
2065 DevToolsManager::GetInstance()->AttachClientHost(
2066 iter->second, host->render_view_host());
2067 orphaned_dev_tools_.erase(iter);
2068}
2069
[email protected]eaa7dd182010-12-14 11:09:002070void ExtensionService::Observe(NotificationType type,
[email protected]fa2416f2011-05-03 08:41:202071 const NotificationSource& source,
2072 const NotificationDetails& details) {
[email protected]4814b512009-11-07 00:12:292073 switch (type.value) {
[email protected]a4ed6282009-12-14 20:51:162074 case NotificationType::EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:322075 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
2076 break;
[email protected]a4ed6282009-12-14 20:51:162077
[email protected]f128af42010-08-05 18:05:262078 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
[email protected]f128af42010-08-05 18:05:262079
[email protected]fa2416f2011-05-03 08:41:202080 // Mark the extension as terminated and Unload it. We want it to
2081 // be in a consistent state: either fully working or not loaded
2082 // at all, but never half-crashed. We do it in a PostTask so
2083 // that other handlers of this notification will still have
2084 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362085 MessageLoop::current()->PostTask(
2086 FROM_HERE,
2087 method_factory_.NewRunnableMethod(
[email protected]fa2416f2011-05-03 08:41:202088 &ExtensionService::TrackTerminatedExtension,
2089 host->extension()));
[email protected]31f77262009-12-02 20:48:532090 break;
2091 }
[email protected]77a6970c2011-04-23 16:58:562092 case NotificationType::RENDERER_PROCESS_CREATED: {
2093 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
2094 // Valid extension function names, used to setup bindings in renderer.
2095 std::vector<std::string> function_names;
2096 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2097 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532098
[email protected]77a6970c2011-04-23 16:58:562099 // Scripting whitelist. This is modified by tests and must be communicated
2100 // to renderers.
2101 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2102 *Extension::GetScriptingWhitelist()));
2103
2104 // Loaded extensions.
2105 for (size_t i = 0; i < extensions_.size(); ++i) {
2106 process->Send(new ExtensionMsg_Loaded(
2107 ExtensionMsg_Loaded_Params(extensions_[i])));
2108 }
2109 break;
2110 }
[email protected]da5683db2011-04-23 17:12:212111 case NotificationType::RENDERER_PROCESS_TERMINATED: {
2112 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
2113 installed_app_hosts_.erase(process->id());
2114 break;
2115 }
[email protected]aa96d3a2010-08-21 08:45:252116 case NotificationType::PREF_CHANGED: {
2117 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102118 if (*pref_name == prefs::kExtensionInstallAllowList ||
2119 *pref_name == prefs::kExtensionInstallDenyList) {
2120 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102121 } else {
2122 NOTREACHED() << "Unexpected preference name.";
2123 }
[email protected]aa96d3a2010-08-21 08:45:252124 break;
2125 }
2126
[email protected]4814b512009-11-07 00:12:292127 default:
2128 NOTREACHED() << "Unexpected notification type.";
2129 }
2130}
2131
[email protected]eaa7dd182010-12-14 11:09:002132bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182133 return !GetAppIds().empty();
2134}
[email protected]377011d2010-07-20 04:18:502135
[email protected]eaa7dd182010-12-14 11:09:002136ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182137 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:502138 for (ExtensionList::const_iterator it = extensions_.begin();
2139 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182140 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2141 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502142 }
2143
[email protected]ec5b50d2010-10-09 16:35:182144 return result;
[email protected]377011d2010-07-20 04:18:502145}
[email protected]d7e9a862010-11-03 21:57:492146
[email protected]14908b72011-04-20 06:54:362147scoped_refptr<CrxInstaller> ExtensionService::MakeCrxInstaller(
2148 ExtensionInstallUI* client) {
2149 return new CrxInstaller(weak_ptr_factory_.GetWeakPtr(), client);
2150}
2151
[email protected]eaa7dd182010-12-14 11:09:002152bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492153 return (extension->background_url().is_empty() ||
2154 extension_runtime_data_[extension->id()].background_page_ready);
2155}
2156
[email protected]eaa7dd182010-12-14 11:09:002157void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492158 DCHECK(!extension->background_url().is_empty());
2159 extension_runtime_data_[extension->id()].background_page_ready = true;
2160 NotificationService::current()->Notify(
2161 NotificationType::EXTENSION_BACKGROUND_PAGE_READY,
2162 Source<const Extension>(extension),
2163 NotificationService::NoDetails());
2164}
2165
[email protected]eaa7dd182010-12-14 11:09:002166bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492167 return extension_runtime_data_[extension->id()].being_upgraded;
2168}
2169
[email protected]eaa7dd182010-12-14 11:09:002170void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492171 bool value) {
2172 extension_runtime_data_[extension->id()].being_upgraded = value;
2173}
[email protected]1bead0712010-11-27 17:41:532174
[email protected]eaa7dd182010-12-14 11:09:002175PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532176 return &extension_runtime_data_[extension->id()].property_bag;
2177}
[email protected]cebc3dc2011-04-18 17:15:002178
2179void ExtensionService::RegisterNaClModule(const GURL& url,
2180 const std::string& mime_type) {
2181 NaClModuleInfo info;
2182 info.url = url;
2183 info.mime_type = mime_type;
2184
2185 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2186 nacl_module_list_.push_front(info);
2187}
2188
2189void ExtensionService::UnregisterNaClModule(const GURL& url) {
2190 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2191 DCHECK(iter != nacl_module_list_.end());
2192 nacl_module_list_.erase(iter);
2193}
2194
2195void ExtensionService::UpdatePluginListWithNaClModules() {
2196 FilePath path;
2197 PathService::Get(chrome::FILE_NACL_PLUGIN, &path);
2198
2199 webkit::npapi::PluginList::Singleton()->UnregisterInternalPlugin(path);
2200
2201 const PepperPluginInfo* pepper_info =
2202 PepperPluginRegistry::GetInstance()->GetInfoForPlugin(path);
2203 webkit::npapi::WebPluginInfo info = pepper_info->ToWebPluginInfo();
2204
2205 DCHECK(nacl_module_list_.size() <= 1);
2206 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2207 iter != nacl_module_list_.end(); ++iter) {
2208 webkit::npapi::WebPluginMimeType mime_type_info;
2209 mime_type_info.mime_type = iter->mime_type;
2210 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2211 mime_type_info.additional_param_values.push_back(
2212 UTF8ToUTF16(iter->url.spec()));
2213 info.mime_types.push_back(mime_type_info);
2214 }
2215
2216 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
2217 webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info);
2218}
2219
2220ExtensionService::NaClModuleInfoList::iterator
2221 ExtensionService::FindNaClModule(const GURL& url) {
2222 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2223 iter != nacl_module_list_.end(); ++iter) {
2224 if (iter->url == url)
2225 return iter;
2226 }
2227 return nacl_module_list_.end();
2228}