blob: de9ce77f2970ecd9bbcc92bda1b607c69b863a88 [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]18cc5a52011-04-15 16:03:3827#include "chrome/browser/extensions/apps_promo.h"
[email protected]e0785902011-05-19 23:34:1728#include "chrome/browser/extensions/crx_installer.h"
[email protected]5cbe1e22010-01-30 01:18:5629#include "chrome/browser/extensions/extension_accessibility_api.h"
[email protected]840b0db2009-11-20 03:00:3830#include "chrome/browser/extensions/extension_bookmarks_module.h"
[email protected]b68d5ed2009-04-16 02:41:2831#include "chrome/browser/extensions/extension_browser_event_router.h"
[email protected]2c5e1e12010-06-10 13:14:4432#include "chrome/browser/extensions/extension_cookies_api.h"
[email protected]c10da4b02010-03-25 14:38:3233#include "chrome/browser/extensions/extension_data_deleter.h"
[email protected]14a000d2010-04-29 21:44:2434#include "chrome/browser/extensions/extension_error_reporter.h"
[email protected]de768a832009-10-30 05:25:0135#include "chrome/browser/extensions/extension_history_api.h"
[email protected]b1748b1d82009-11-30 20:32:5636#include "chrome/browser/extensions/extension_host.h"
[email protected]5fb889382011-06-03 00:29:2037#include "chrome/browser/extensions/extension_install_ui.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]8e5c89a2011-06-07 18:13:3353#include "chrome/browser/search_engines/template_url_service.h"
54#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]18280372011-03-22 18:05:2255#include "chrome/browser/themes/theme_service.h"
[email protected]a0ea76c2011-03-23 17:36:4256#include "chrome/browser/themes/theme_service_factory.h"
[email protected]c8d407e2011-04-28 21:27:1757#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
58#include "chrome/browser/ui/webui/favicon_source.h"
[email protected]7c5c6f3a2011-04-28 19:56:4959#include "chrome/browser/ui/webui/ntp/shown_sections_handler.h"
[email protected]aab98a52009-12-02 03:22:3560#include "chrome/common/child_process_logging.h"
[email protected]432115822011-07-10 15:52:2761#include "chrome/common/chrome_notification_types.h"
[email protected]cebc3dc2011-04-18 17:15:0062#include "chrome/common/chrome_paths.h"
[email protected]e2eb43112009-05-29 21:19:5463#include "chrome/common/chrome_switches.h"
[email protected]5b1a0e22009-05-26 19:00:5864#include "chrome/common/extensions/extension.h"
[email protected]d7b36dc2009-10-29 21:47:4065#include "chrome/common/extensions/extension_constants.h"
[email protected]05c82182010-06-24 17:49:0866#include "chrome/common/extensions/extension_error_utils.h"
[email protected]7c927b62010-02-24 09:54:1367#include "chrome/common/extensions/extension_file_util.h"
[email protected]c6d474f82009-12-16 21:11:0668#include "chrome/common/extensions/extension_l10n_util.h"
[email protected]77a6970c2011-04-23 16:58:5669#include "chrome/common/extensions/extension_messages.h"
[email protected]a315ba92010-11-16 14:12:2170#include "chrome/common/extensions/extension_resource.h"
[email protected]25b34332009-06-05 21:53:1971#include "chrome/common/pref_names.h"
[email protected]a57209872009-05-04 22:53:1472#include "chrome/common/url_constants.h"
[email protected]5f945a0e2011-03-01 17:47:5373#include "content/browser/browser_thread.h"
[email protected]b46442d7e2011-06-29 02:16:0674#include "content/browser/debugger/devtools_manager.h"
[email protected]2de307592011-04-05 21:16:5875#include "content/browser/plugin_process_host.h"
76#include "content/browser/plugin_service.h"
[email protected]77a6970c2011-04-23 16:58:5677#include "content/browser/renderer_host/render_process_host.h"
[email protected]432115822011-07-10 15:52:2778#include "content/common/content_notification_types.h"
[email protected]c3113022011-04-16 03:26:3079#include "content/common/json_value_serializer.h"
[email protected]b3841c502011-03-09 01:21:3180#include "content/common/notification_service.h"
[email protected]cebc3dc2011-04-18 17:15:0081#include "content/common/pepper_plugin_registry.h"
[email protected]c10da4b02010-03-25 14:38:3282#include "googleurl/src/gurl.h"
[email protected]1debbbb62010-10-06 17:23:4483#include "net/base/registry_controlled_domain.h"
[email protected]24b538a2010-02-27 01:22:4484#include "webkit/database/database_tracker.h"
85#include "webkit/database/database_util.h"
[email protected]2de307592011-04-05 21:16:5886#include "webkit/plugins/npapi/plugin_list.h"
[email protected]c64631652009-04-29 22:24:3187
[email protected]eed367e2011-04-12 03:43:3188#if defined(OS_CHROMEOS)
89#include "chrome/browser/chromeos/extensions/file_browser_event_router.h"
[email protected]be5bd5742011-07-14 07:21:0190#include "chrome/browser/chromeos/extensions/input_method_event_router.h"
[email protected]ce9802042011-05-27 10:11:4091#include "chrome/browser/chromeos/extensions/media_player_event_router.h"
[email protected]b777b332011-04-16 04:01:0892#include "webkit/fileapi/file_system_context.h"
93#include "webkit/fileapi/file_system_mount_point_provider.h"
94#include "webkit/fileapi/file_system_path_manager.h"
[email protected]eed367e2011-04-12 03:43:3195#endif
96
[email protected]27072cad2011-05-09 19:46:4097#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
[email protected]c8d4e9462011-06-29 23:18:5798#include "chrome/browser/chromeos/input_method/input_method_manager.h"
[email protected]27072cad2011-05-09 19:46:4099#include "chrome/browser/extensions/extension_input_ui_api.h"
100#endif
101
[email protected]5ef47ec2010-01-28 05:58:05102using base::Time;
103
[email protected]c6d474f82009-12-16 21:11:06104namespace errors = extension_manifest_errors;
105
[email protected]b6ab96d2009-08-20 18:58:19106namespace {
107
[email protected]29d0d4ac2010-09-08 21:10:31108#if defined(OS_LINUX)
109static const int kOmniboxIconPaddingLeft = 2;
110static const int kOmniboxIconPaddingRight = 2;
111#elif defined(OS_MACOSX)
112static const int kOmniboxIconPaddingLeft = 0;
113static const int kOmniboxIconPaddingRight = 2;
114#else
115static const int kOmniboxIconPaddingLeft = 0;
116static const int kOmniboxIconPaddingRight = 0;
117#endif
118
[email protected]300c0ea2011-07-15 23:04:33119const char* kNaClPluginMimeType = "application/x-nacl";
120
[email protected]a315ba92010-11-16 14:12:21121// The following enumeration is used in histograms matching
122// Extensions.ManifestReload* . Values may be added, as long
123// as existing values are not changed.
124enum ManifestReloadReason {
125 NOT_NEEDED = 0, // Reload not needed.
126 UNPACKED_DIR, // Unpacked directory
127 NEEDS_RELOCALIZATION, // The local has changed since we read this extension.
128 NUM_MANIFEST_RELOAD_REASONS
129};
[email protected]2111b1a2010-03-12 18:12:44130
[email protected]a315ba92010-11-16 14:12:21131ManifestReloadReason ShouldReloadExtensionManifest(const ExtensionInfo& info) {
132 // Always reload manifests of unpacked extensions, because they can change
133 // on disk independent of the manifest in our prefs.
134 if (info.extension_location == Extension::LOAD)
135 return UNPACKED_DIR;
136
137 // Reload the manifest if it needs to be relocalized.
138 if (extension_l10n_util::ShouldRelocalizeManifest(info))
139 return NEEDS_RELOCALIZATION;
140
141 return NOT_NEEDED;
[email protected]2111b1a2010-03-12 18:12:44142}
143
[email protected]2de307592011-04-05 21:16:58144static void ForceShutdownPlugin(const FilePath& plugin_path) {
145 PluginProcessHost* plugin =
146 PluginService::GetInstance()->FindNpapiPluginProcess(plugin_path);
147 if (plugin)
148 plugin->ForceShutdown();
149}
150
[email protected]5fb889382011-06-03 00:29:20151// Manages an ExtensionInstallUI for a particular extension.
152class SimpleExtensionLoadPrompt : public ExtensionInstallUI::Delegate {
153 public:
154 SimpleExtensionLoadPrompt(Profile* profile,
155 base::WeakPtr<ExtensionService> extension_service,
156 const Extension* extension);
157 ~SimpleExtensionLoadPrompt();
158
159 void ShowPrompt();
160
161 // ExtensionInstallUI::Delegate
162 virtual void InstallUIProceed();
[email protected]d828fac2011-06-28 05:43:04163 virtual void InstallUIAbort(bool user_initiated);
[email protected]5fb889382011-06-03 00:29:20164
165 private:
166 base::WeakPtr<ExtensionService> extension_service_;
167 scoped_ptr<ExtensionInstallUI> install_ui_;
168 scoped_refptr<const Extension> extension_;
169};
170
171SimpleExtensionLoadPrompt::SimpleExtensionLoadPrompt(
172 Profile* profile,
173 base::WeakPtr<ExtensionService> extension_service,
174 const Extension* extension)
175 : extension_service_(extension_service),
176 install_ui_(new ExtensionInstallUI(profile)),
177 extension_(extension) {
178}
179
180SimpleExtensionLoadPrompt::~SimpleExtensionLoadPrompt() {
181}
182
183void SimpleExtensionLoadPrompt::ShowPrompt() {
184 install_ui_->ConfirmInstall(this, extension_);
185}
186
187void SimpleExtensionLoadPrompt::InstallUIProceed() {
188 if (extension_service_.get())
[email protected]8266d662011-07-12 21:53:26189 extension_service_->OnExtensionInstalled(
190 extension_, false); // Not from web store.
[email protected]5fb889382011-06-03 00:29:20191 delete this;
192}
193
[email protected]d828fac2011-06-28 05:43:04194void SimpleExtensionLoadPrompt::InstallUIAbort(bool user_initiated) {
[email protected]5fb889382011-06-03 00:29:20195 delete this;
196}
197
[email protected]c6d474f82009-12-16 21:11:06198} // namespace
[email protected]b6ab96d2009-08-20 18:58:19199
[email protected]ad63a792011-06-16 06:41:11200bool ExtensionService::ComponentExtensionInfo::Equals(
201 const ComponentExtensionInfo& other) const {
202 return other.manifest == manifest && other.root_directory == root_directory;
203}
204
[email protected]eaa7dd182010-12-14 11:09:00205ExtensionService::ExtensionRuntimeData::ExtensionRuntimeData()
[email protected]d7e9a862010-11-03 21:57:49206 : background_page_ready(false),
207 being_upgraded(false) {
208}
209
[email protected]eaa7dd182010-12-14 11:09:00210ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
[email protected]d7e9a862010-11-03 21:57:49211}
212
[email protected]cebc3dc2011-04-18 17:15:00213ExtensionService::NaClModuleInfo::NaClModuleInfo() {
214}
215
216ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
217}
218
[email protected]eaa7dd182010-12-14 11:09:00219// ExtensionService.
[email protected]6014d672008-12-05 00:38:25220
[email protected]eaa7dd182010-12-14 11:09:00221const char* ExtensionService::kInstallDirectoryName = "Extensions";
222const char* ExtensionService::kCurrentVersionFileName = "Current Version";
[email protected]494c06e2009-07-25 01:06:42223
[email protected]eaa7dd182010-12-14 11:09:00224// Implements IO for the ExtensionService.
[email protected]7a4c6852010-09-16 03:44:22225
[email protected]eaa7dd182010-12-14 11:09:00226class ExtensionServiceBackend
[email protected]8e4560b62011-01-14 10:09:14227 : public base::RefCountedThreadSafe<ExtensionServiceBackend> {
[email protected]7a4c6852010-09-16 03:44:22228 public:
[email protected]1f830eb2010-09-28 08:25:14229 // |install_directory| is a path where to look for extensions to load.
[email protected]14908b72011-04-20 06:54:36230 ExtensionServiceBackend(
231 base::WeakPtr<ExtensionService> frontend,
232 const FilePath& install_directory);
[email protected]7a4c6852010-09-16 03:44:22233
234 // Loads a single extension from |path| where |path| is the top directory of
[email protected]e49002a2011-07-13 03:40:44235 // a specific extension where its manifest file lives. If |prompt_for_plugins|
236 // is true and the extension contains plugins, we prompt the user before
237 // loading.
[email protected]7a4c6852010-09-16 03:44:22238 // Errors are reported through ExtensionErrorReporter. On success,
[email protected]49cd1572011-02-08 21:38:45239 // AddExtension() is called.
[email protected]7a4c6852010-09-16 03:44:22240 // TODO(erikkay): It might be useful to be able to load a packed extension
241 // (presumably into memory) without installing it.
[email protected]e49002a2011-07-13 03:40:44242 void LoadSingleExtension(const FilePath &path, bool prompt_for_plugins);
[email protected]7a4c6852010-09-16 03:44:22243
[email protected]7a4c6852010-09-16 03:44:22244 private:
[email protected]eaa7dd182010-12-14 11:09:00245 friend class base::RefCountedThreadSafe<ExtensionServiceBackend>;
[email protected]7a4c6852010-09-16 03:44:22246
[email protected]eaa7dd182010-12-14 11:09:00247 virtual ~ExtensionServiceBackend();
[email protected]7a4c6852010-09-16 03:44:22248
[email protected]d10f4602011-06-16 15:44:50249 // LoadSingleExtension needs to check the file access preference, which needs
250 // to happen back on the UI thread, so it posts CheckExtensionFileAccess on
251 // the UI thread. In turn, once that gets the pref, it goes back to the
252 // file thread with LoadSingleExtensionWithFileAccess.
[email protected]e49002a2011-07-13 03:40:44253 void CheckExtensionFileAccess(const FilePath& extension_path,
254 bool prompt_for_plugins);
[email protected]d10f4602011-06-16 15:44:50255 void LoadSingleExtensionWithFileAccess(
[email protected]e49002a2011-07-13 03:40:44256 const FilePath &path, bool allow_file_access, bool prompt_for_plugins);
[email protected]d10f4602011-06-16 15:44:50257
[email protected]7a4c6852010-09-16 03:44:22258 // Notify the frontend that there was an error loading an extension.
259 void ReportExtensionLoadError(const FilePath& extension_path,
260 const std::string& error);
261
[email protected]14908b72011-04-20 06:54:36262 // Notify the frontend that an extension was installed.
[email protected]e49002a2011-07-13 03:40:44263 void OnLoadSingleExtension(const scoped_refptr<const Extension>& extension,
264 bool prompt_for_plugins);
[email protected]14908b72011-04-20 06:54:36265
266 base::WeakPtr<ExtensionService> frontend_;
[email protected]7a4c6852010-09-16 03:44:22267
268 // The top-level extensions directory being installed to.
269 FilePath install_directory_;
270
[email protected]eaa7dd182010-12-14 11:09:00271 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceBackend);
[email protected]7a4c6852010-09-16 03:44:22272};
273
[email protected]eaa7dd182010-12-14 11:09:00274ExtensionServiceBackend::ExtensionServiceBackend(
[email protected]14908b72011-04-20 06:54:36275 base::WeakPtr<ExtensionService> frontend,
[email protected]aebe23a32010-12-10 22:15:48276 const FilePath& install_directory)
[email protected]14908b72011-04-20 06:54:36277 : frontend_(frontend),
278 install_directory_(install_directory) {
279 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]7a4c6852010-09-16 03:44:22280}
281
[email protected]eaa7dd182010-12-14 11:09:00282ExtensionServiceBackend::~ExtensionServiceBackend() {
[email protected]14908b72011-04-20 06:54:36283 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
284 BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]7a4c6852010-09-16 03:44:22285}
286
[email protected]e49002a2011-07-13 03:40:44287void ExtensionServiceBackend::LoadSingleExtension(const FilePath& path_in,
288 bool prompt_for_plugins) {
[email protected]a29a517a2011-01-21 21:11:12289 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]a8af9fdb2010-10-28 21:52:20290
[email protected]7a4c6852010-09-16 03:44:22291 FilePath extension_path = path_in;
292 file_util::AbsolutePath(&extension_path);
293
[email protected]e49002a2011-07-13 03:40:44294 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
295 NewRunnableMethod(this,
296 &ExtensionServiceBackend::CheckExtensionFileAccess,
297 extension_path, prompt_for_plugins));
[email protected]d10f4602011-06-16 15:44:50298}
299
300void ExtensionServiceBackend::CheckExtensionFileAccess(
[email protected]e49002a2011-07-13 03:40:44301 const FilePath& extension_path, bool prompt_for_plugins) {
[email protected]d10f4602011-06-16 15:44:50302 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
303 std::string id = Extension::GenerateIdForPath(extension_path);
304 // Unpacked extensions default to allowing file access, but if that has been
305 // overridden, don't reset the value.
306 bool allow_file_access =
307 Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) &&
308 !frontend_->extension_prefs()->HasAllowFileAccessSetting(id);
309
[email protected]e49002a2011-07-13 03:40:44310 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
311 NewRunnableMethod(
312 this,
313 &ExtensionServiceBackend::LoadSingleExtensionWithFileAccess,
314 extension_path, allow_file_access, prompt_for_plugins));
[email protected]d10f4602011-06-16 15:44:50315}
316
[email protected]d10f4602011-06-16 15:44:50317void ExtensionServiceBackend::LoadSingleExtensionWithFileAccess(
[email protected]e49002a2011-07-13 03:40:44318 const FilePath& extension_path,
319 bool allow_file_access,
320 bool prompt_for_plugins) {
[email protected]d10f4602011-06-16 15:44:50321 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
322 int flags = allow_file_access ?
[email protected]3aff9ad2011-04-01 20:26:48323 Extension::ALLOW_FILE_ACCESS : Extension::NO_FLAGS;
[email protected]83048a22011-03-29 00:14:13324 if (Extension::ShouldDoStrictErrorChecking(Extension::LOAD))
325 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]7a4c6852010-09-16 03:44:22326 std::string error;
[email protected]ad8e04a2010-11-01 04:16:27327 scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
[email protected]7a4c6852010-09-16 03:44:22328 extension_path,
[email protected]92888082010-10-18 19:24:57329 Extension::LOAD,
[email protected]83048a22011-03-29 00:14:13330 flags,
[email protected]ad8e04a2010-11-01 04:16:27331 &error));
[email protected]7a4c6852010-09-16 03:44:22332
333 if (!extension) {
[email protected]e49002a2011-07-13 03:40:44334 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
335 NewRunnableMethod(
336 this,
337 &ExtensionServiceBackend::ReportExtensionLoadError,
338 extension_path, error));
[email protected]7a4c6852010-09-16 03:44:22339 return;
340 }
341
[email protected]7a4c6852010-09-16 03:44:22342 // Report this as an installed extension so that it gets remembered in the
343 // prefs.
[email protected]e49002a2011-07-13 03:40:44344 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
345 NewRunnableMethod(
346 this,
347 &ExtensionServiceBackend::OnLoadSingleExtension,
348 extension, prompt_for_plugins));
[email protected]7a4c6852010-09-16 03:44:22349}
350
[email protected]eaa7dd182010-12-14 11:09:00351void ExtensionServiceBackend::ReportExtensionLoadError(
[email protected]7a4c6852010-09-16 03:44:22352 const FilePath& extension_path, const std::string &error) {
[email protected]14908b72011-04-20 06:54:36353 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
354 if (frontend_.get())
355 frontend_->ReportExtensionLoadError(
[email protected]432115822011-07-10 15:52:27356 extension_path, error, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
[email protected]14908b72011-04-20 06:54:36357 true /* alert_on_error */);
358}
359
[email protected]5fb889382011-06-03 00:29:20360void ExtensionServiceBackend::OnLoadSingleExtension(
[email protected]e49002a2011-07-13 03:40:44361 const scoped_refptr<const Extension>& extension, bool prompt_for_plugins) {
[email protected]14908b72011-04-20 06:54:36362 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
363 if (frontend_.get())
[email protected]e49002a2011-07-13 03:40:44364 frontend_->OnLoadSingleExtension(extension, prompt_for_plugins);
[email protected]7a4c6852010-09-16 03:44:22365}
366
[email protected]8e4560b62011-01-14 10:09:14367void ExtensionService::CheckExternalUninstall(const std::string& id) {
[email protected]a29a517a2011-01-21 21:11:12368 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20369
[email protected]7a4c6852010-09-16 03:44:22370 // Check if the providers know about this extension.
[email protected]0a60a2e2010-10-25 16:15:21371 ProviderCollection::const_iterator i;
372 for (i = external_extension_providers_.begin();
373 i != external_extension_providers_.end(); ++i) {
[email protected]8e4560b62011-01-14 10:09:14374 DCHECK(i->get()->IsReady());
[email protected]0a60a2e2010-10-25 16:15:21375 if (i->get()->HasExtension(id))
376 return; // Yup, known extension, don't uninstall.
[email protected]7a4c6852010-09-16 03:44:22377 }
378
[email protected]0f48fca2011-05-19 18:46:35379 // We get the list of external extensions to check from preferences.
380 // It is possible that an extension has preferences but is not loaded.
381 // For example, an extension that requires experimental permissions
382 // will not be loaded if the experimental command line flag is not used.
383 // In this case, do not uninstall.
384 const Extension* extension = GetInstalledExtension(id);
385 if (!extension) {
386 // We can't call UninstallExtension with an unloaded/invalid
387 // extension ID.
388 LOG(WARNING) << "Attempted uninstallation of unloaded/invalid extension "
389 << "with id: " << id;
390 return;
391 }
[email protected]d6ebc9792011-04-07 18:18:33392 UninstallExtension(id, true, NULL);
[email protected]7a4c6852010-09-16 03:44:22393}
394
[email protected]8e4560b62011-01-14 10:09:14395void ExtensionService::ClearProvidersForTesting() {
[email protected]7a4c6852010-09-16 03:44:22396 external_extension_providers_.clear();
397}
398
[email protected]8e4560b62011-01-14 10:09:14399void ExtensionService::AddProviderForTesting(
400 ExternalExtensionProviderInterface* test_provider) {
[email protected]a29a517a2011-01-21 21:11:12401 CHECK(test_provider);
[email protected]0a60a2e2010-10-25 16:15:21402 external_extension_providers_.push_back(
[email protected]8e4560b62011-01-14 10:09:14403 linked_ptr<ExternalExtensionProviderInterface>(test_provider));
[email protected]7a4c6852010-09-16 03:44:22404}
405
[email protected]8e4560b62011-01-14 10:09:14406void ExtensionService::OnExternalExtensionUpdateUrlFound(
[email protected]7a4c6852010-09-16 03:44:22407 const std::string& id,
[email protected]21a5a672010-11-04 10:47:42408 const GURL& update_url,
409 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:38410 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
411 CHECK(Extension::IdIsValid(id));
[email protected]a8af9fdb2010-10-28 21:52:20412
[email protected]8e4560b62011-01-14 10:09:14413 if (GetExtensionById(id, true)) {
[email protected]7a4c6852010-09-16 03:44:22414 // Already installed. Do not change the update URL that the extension set.
415 return;
416 }
[email protected]b2907fd2011-03-25 16:43:37417 pending_extension_manager()->AddFromExternalUpdateUrl(
418 id, update_url, location);
[email protected]a29a517a2011-01-21 21:11:12419 external_extension_url_added_ |= true;
[email protected]7a4c6852010-09-16 03:44:22420}
421
[email protected]eaa7dd182010-12-14 11:09:00422bool ExtensionService::IsDownloadFromGallery(const GURL& download_url,
[email protected]a29a517a2011-01-21 21:11:12423 const GURL& referrer_url) {
[email protected]d3071992010-10-08 15:24:07424 // Special-case the themes mini-gallery.
425 // TODO(erikkay) When that gallery goes away, remove this code.
426 if (IsDownloadFromMiniGallery(download_url) &&
427 StartsWithASCII(referrer_url.spec(),
428 extension_urls::kMiniGalleryBrowsePrefix, false)) {
[email protected]334e04a2010-06-24 23:34:44429 return true;
[email protected]1debbbb62010-10-06 17:23:44430 }
[email protected]473ff6e2010-05-12 15:31:55431
[email protected]9adb9692010-10-29 23:14:02432 const Extension* download_extension = GetExtensionByWebExtent(download_url);
433 const Extension* referrer_extension = GetExtensionByWebExtent(referrer_url);
434 const Extension* webstore_app = GetWebStoreApp();
[email protected]d3071992010-10-08 15:24:07435
436 bool referrer_valid = (referrer_extension == webstore_app);
437 bool download_valid = (download_extension == webstore_app);
438
439 // If the command-line gallery URL is set, then be a bit more lenient.
440 GURL store_url =
441 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
442 switches::kAppsGalleryURL));
443 if (!store_url.is_empty()) {
444 std::string store_tld =
445 net::RegistryControlledDomainService::GetDomainAndRegistry(store_url);
446 if (!referrer_valid) {
447 std::string referrer_tld =
448 net::RegistryControlledDomainService::GetDomainAndRegistry(
449 referrer_url);
450 // The referrer gets stripped when transitioning from https to http,
451 // or when hitting an unknown test cert and that commonly happens in
452 // testing environments. Given this, we allow an empty referrer when
453 // the command-line flag is set.
454 // Otherwise, the TLD must match the TLD of the command-line url.
455 referrer_valid = referrer_url.is_empty() || (referrer_tld == store_tld);
456 }
457
458 if (!download_valid) {
459 std::string download_tld =
460 net::RegistryControlledDomainService::GetDomainAndRegistry(
[email protected]04866c42011-05-03 20:03:50461 download_url);
[email protected]d3071992010-10-08 15:24:07462
463 // Otherwise, the TLD must match the TLD of the command-line url.
464 download_valid = (download_tld == store_tld);
465 }
466 }
467
468 return (referrer_valid && download_valid);
[email protected]b7c2f252009-12-08 00:47:23469}
470
[email protected]eaa7dd182010-12-14 11:09:00471bool ExtensionService::IsDownloadFromMiniGallery(const GURL& download_url) {
[email protected]ac025282009-12-16 19:16:38472 return StartsWithASCII(download_url.spec(),
473 extension_urls::kMiniGalleryDownloadPrefix,
474 false); // case_sensitive
475}
476
[email protected]d9696672011-03-15 22:45:09477const Extension* ExtensionService::GetInstalledApp(const GURL& url) {
[email protected]ffb204f22010-12-05 23:20:27478 // Check for hosted app.
[email protected]d9696672011-03-15 22:45:09479 const Extension* app = GetExtensionByWebExtent(url);
480 if (app)
481 return app;
[email protected]ffb204f22010-12-05 23:20:27482
483 // Check for packaged app.
[email protected]d9696672011-03-15 22:45:09484 app = GetExtensionByURL(url);
485 if (app && app->is_app())
486 return app;
487
488 return NULL;
489}
490
491bool ExtensionService::IsInstalledApp(const GURL& url) {
492 return !!GetInstalledApp(url);
[email protected]ffb204f22010-12-05 23:20:27493}
494
[email protected]da5683db2011-04-23 17:12:21495void ExtensionService::SetInstalledAppForRenderer(int renderer_child_id,
496 const Extension* app) {
497 installed_app_hosts_[renderer_child_id] = app;
498}
499
500const Extension* ExtensionService::GetInstalledAppForRenderer(
501 int renderer_child_id) {
502 InstalledAppMap::iterator i = installed_app_hosts_.find(renderer_child_id);
503 if (i == installed_app_hosts_.end())
504 return NULL;
505 return i->second;
506}
507
[email protected]6aeac8342010-10-01 20:21:18508// static
[email protected]d6ebc9792011-04-07 18:18:33509// This function is used to implement the command-line switch
510// --uninstall-extension. The LOG statements within this function are used to
511// inform the user if the uninstall cannot be done.
[email protected]eaa7dd182010-12-14 11:09:00512bool ExtensionService::UninstallExtensionHelper(
513 ExtensionService* extensions_service,
[email protected]6aeac8342010-10-01 20:21:18514 const std::string& extension_id) {
[email protected]6aeac8342010-10-01 20:21:18515
[email protected]95da88c42011-03-31 10:07:33516 const Extension* extension =
[email protected]8001df22011-04-28 19:59:47517 extensions_service->GetInstalledExtension(extension_id);
[email protected]95da88c42011-03-31 10:07:33518
[email protected]d6ebc9792011-04-07 18:18:33519 // We can't call UninstallExtension with an invalid extension ID.
[email protected]95da88c42011-03-31 10:07:33520 if (!extension) {
[email protected]6aeac8342010-10-01 20:21:18521 LOG(WARNING) << "Attempted uninstallation of non-existent extension with "
[email protected]95da88c42011-03-31 10:07:33522 << "id: " << extension_id;
523 return false;
[email protected]6aeac8342010-10-01 20:21:18524 }
525
[email protected]d6ebc9792011-04-07 18:18:33526 // The following call to UninstallExtension will not allow an uninstall of a
527 // policy-controlled extension.
528 std::string error;
529 if (!extensions_service->UninstallExtension(extension_id, false, &error)) {
530 LOG(WARNING) << "Cannot uninstall extension with id " << extension_id
531 << ": " << error;
532 return false;
533 }
[email protected]95da88c42011-03-31 10:07:33534
[email protected]6aeac8342010-10-01 20:21:18535 return true;
536}
537
[email protected]eaa7dd182010-12-14 11:09:00538ExtensionService::ExtensionService(Profile* profile,
[email protected]f0841cd2011-01-19 15:07:24539 const CommandLine* command_line,
540 const FilePath& install_directory,
541 ExtensionPrefs* extension_prefs,
[email protected]0436b102011-04-15 18:30:03542 bool autoupdate_enabled,
543 bool extensions_enabled)
[email protected]14908b72011-04-20 06:54:36544 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
545 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
546 profile_(profile),
[email protected]73c47932010-12-06 18:13:43547 extension_prefs_(extension_prefs),
[email protected]14908b72011-04-20 06:54:36548 pending_extension_manager_(*ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]a9b00ac2009-06-25 21:03:23549 install_directory_(install_directory),
[email protected]0436b102011-04-15 18:30:03550 extensions_enabled_(extensions_enabled),
[email protected]e81dba32009-06-19 20:19:13551 show_extensions_prompts_(true),
[email protected]3ecda252010-11-18 19:50:55552 ready_(false),
[email protected]14908b72011-04-20 06:54:36553 toolbar_model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
[email protected]18cc5a52011-04-15 16:03:38554 apps_promo_(profile->GetPrefs()),
[email protected]b3d62312b12010-10-14 21:10:18555 event_routers_initialized_(false) {
[email protected]a29a517a2011-01-21 21:11:12556 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20557
[email protected]36a784c2009-06-23 06:21:08558 // Figure out if extension installation should be enabled.
[email protected]6d60703b2009-08-29 01:29:23559 if (command_line->HasSwitch(switches::kDisableExtensions)) {
560 extensions_enabled_ = false;
561 } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) {
562 extensions_enabled_ = false;
[email protected]6b75ec32009-08-14 06:37:18563 }
[email protected]36a784c2009-06-23 06:21:08564
[email protected]432115822011-07-10 15:52:27565 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
[email protected]bc535ee52010-08-31 18:40:32566 NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27567 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
[email protected]77a6970c2011-04-23 16:58:56568 NotificationService::AllSources());
[email protected]432115822011-07-10 15:52:27569 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
[email protected]da5683db2011-04-23 17:12:21570 NotificationService::AllSources());
[email protected]2fb7dc982010-09-29 12:24:28571 pref_change_registrar_.Init(profile->GetPrefs());
572 pref_change_registrar_.Add(prefs::kExtensionInstallAllowList, this);
573 pref_change_registrar_.Add(prefs::kExtensionInstallDenyList, this);
[email protected]4814b512009-11-07 00:12:29574
[email protected]93fd78f42009-07-10 16:43:17575 // Set up the ExtensionUpdater
576 if (autoupdate_enabled) {
577 int update_frequency = kDefaultUpdateFrequencySeconds;
578 if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) {
[email protected]e83326f2010-07-31 17:29:25579 base::StringToInt(command_line->GetSwitchValueASCII(
580 switches::kExtensionsUpdateFrequency),
581 &update_frequency);
[email protected]93fd78f42009-07-10 16:43:17582 }
[email protected]82b6e512011-04-12 20:33:27583 updater_.reset(new ExtensionUpdater(this,
584 extension_prefs,
585 profile->GetPrefs(),
586 profile,
587 update_frequency));
[email protected]93fd78f42009-07-10 16:43:17588 }
589
[email protected]14908b72011-04-20 06:54:36590 backend_ =
591 new ExtensionServiceBackend(weak_ptr_factory_.GetWeakPtr(),
592 install_directory_);
[email protected]8e4560b62011-01-14 10:09:14593
[email protected]0436b102011-04-15 18:30:03594 if (extensions_enabled_) {
[email protected]873531342011-03-09 12:16:05595 ExternalExtensionProviderImpl::CreateExternalProviders(
596 this, profile_, &external_extension_providers_);
597 }
[email protected]b671760b2010-07-15 21:13:47598
[email protected]aa96d3a2010-08-21 08:45:25599 // Use monochrome icons for Omnibox icons.
[email protected]29d0d4ac2010-09-08 21:10:31600 omnibox_popup_icon_manager_.set_monochrome(true);
[email protected]b671760b2010-07-15 21:13:47601 omnibox_icon_manager_.set_monochrome(true);
[email protected]29d0d4ac2010-09-08 21:10:31602 omnibox_icon_manager_.set_padding(gfx::Insets(0, kOmniboxIconPaddingLeft,
603 0, kOmniboxIconPaddingRight));
[email protected]cb0e50312011-05-09 15:03:07604
605 // How long is the path to the Extensions directory?
606 UMA_HISTOGRAM_CUSTOM_COUNTS("Extensions.ExtensionRootPathLength",
607 install_directory_.value().length(), 0, 500, 100);
[email protected]6014d672008-12-05 00:38:25608}
609
[email protected]eaa7dd182010-12-14 11:09:00610const ExtensionList* ExtensionService::extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45611 return &extensions_;
612}
613
[email protected]eaa7dd182010-12-14 11:09:00614const ExtensionList* ExtensionService::disabled_extensions() const {
[email protected]ddb1e5a2010-12-13 20:10:45615 return &disabled_extensions_;
616}
617
[email protected]bb7f40952011-01-13 00:21:20618const ExtensionList* ExtensionService::terminated_extensions() const {
619 return &terminated_extensions_;
620}
621
[email protected]b2907fd2011-03-25 16:43:37622PendingExtensionManager* ExtensionService::pending_extension_manager() {
623 return &pending_extension_manager_;
[email protected]ddb1e5a2010-12-13 20:10:45624}
625
[email protected]ad63a792011-06-16 06:41:11626void ExtensionService::UnregisterComponentExtension(
627 const ComponentExtensionInfo& info) {
628 RegisteredComponentExtensions new_component_extension_manifests;
629 for (RegisteredComponentExtensions::iterator it =
630 component_extension_manifests_.begin();
631 it != component_extension_manifests_.end(); ++it) {
632 if (!it->Equals(info))
633 new_component_extension_manifests.push_back(*it);
634 }
635 component_extension_manifests_.swap(new_component_extension_manifests);
636}
637
[email protected]eaa7dd182010-12-14 11:09:00638ExtensionService::~ExtensionService() {
[email protected]c8d407e2011-04-28 21:27:17639 // No need to unload extensions here because they are profile-scoped, and the
640 // profile is in the process of being deleted.
[email protected]94b4ffc2011-02-04 05:55:17641
[email protected]8e4560b62011-01-14 10:09:14642 ProviderCollection::const_iterator i;
643 for (i = external_extension_providers_.begin();
644 i != external_extension_providers_.end(); ++i) {
645 ExternalExtensionProviderInterface* provider = i->get();
646 provider->ServiceShutdown();
647 }
[email protected]c8bf09be2011-06-25 03:22:27648
649#if defined(OS_CHROMEOS)
650 if (event_routers_initialized_) {
651 ExtensionFileBrowserEventRouter::GetInstance()->
652 StopObservingFileSystemEvents();
653 }
654#endif
[email protected]6014d672008-12-05 00:38:25655}
656
[email protected]eaa7dd182010-12-14 11:09:00657void ExtensionService::InitEventRouters() {
[email protected]b3d62312b12010-10-14 21:10:18658 if (event_routers_initialized_)
659 return;
660
[email protected]f825df22011-06-28 17:36:10661 history_event_router_.reset(new ExtensionHistoryEventRouter());
662 history_event_router_->ObserveProfile(profile_);
[email protected]7da9a8232011-05-27 21:07:11663 ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]97d2f1d2011-01-15 00:41:08664 browser_event_router_.reset(new ExtensionBrowserEventRouter(profile_));
665 browser_event_router_->Init();
[email protected]118de7892011-03-16 13:31:40666 preference_event_router_.reset(new ExtensionPreferenceEventRouter(profile_));
[email protected]f825df22011-06-28 17:36:10667 bookmark_event_router_.reset(new ExtensionBookmarkEventRouter(
668 profile_->GetBookmarkModel()));
669 bookmark_event_router_->Init();
670 cookies_event_router_.reset(new ExtensionCookiesEventRouter(profile_));
671 cookies_event_router_->Init();
672 management_event_router_.reset(new ExtensionManagementEventRouter(profile_));
673 management_event_router_->Init();
[email protected]7da9a8232011-05-27 21:07:11674 ExtensionProcessesEventRouter::GetInstance()->ObserveProfile(profile_);
[email protected]f825df22011-06-28 17:36:10675 web_navigation_event_router_.reset(
676 new ExtensionWebNavigationEventRouter(profile_));
677 web_navigation_event_router_->Init();
[email protected]27072cad2011-05-09 19:46:40678
[email protected]eed367e2011-04-12 03:43:31679#if defined(OS_CHROMEOS)
[email protected]c8bf09be2011-06-25 03:22:27680 ExtensionFileBrowserEventRouter::GetInstance()->ObserveFileSystemEvents(
681 profile_);
[email protected]be5bd5742011-07-14 07:21:01682 // Lazy initialization.
683 chromeos::ExtensionInputMethodEventRouter::GetInstance();
684
[email protected]c8bf09be2011-06-25 03:22:27685 ExtensionMediaPlayerEventRouter::GetInstance()->Init(profile_);
[email protected]eed367e2011-04-12 03:43:31686#endif
[email protected]27072cad2011-05-09 19:46:40687
688#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
689 ExtensionInputUiEventRouter::GetInstance()->Init();
690#endif
691
[email protected]b3d62312b12010-10-14 21:10:18692 event_routers_initialized_ = true;
[email protected]c5ae74ab2010-04-15 18:14:37693}
694
[email protected]b2907fd2011-03-25 16:43:37695const Extension* ExtensionService::GetExtensionById(
696 const std::string& id, bool include_disabled) const {
[email protected]8001df22011-04-28 19:59:47697 return GetExtensionByIdInternal(id, true, include_disabled, false);
[email protected]78994ab02010-12-08 18:06:44698}
699
[email protected]eaa7dd182010-12-14 11:09:00700void ExtensionService::Init() {
[email protected]a29a517a2011-01-21 21:11:12701 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20702
[email protected]fa6a9102010-11-22 15:38:50703 DCHECK(!ready_); // Can't redo init.
[email protected]93fd78f42009-07-10 16:43:17704 DCHECK_EQ(extensions_.size(), 0u);
[email protected]9f1087e2009-06-15 17:29:32705
[email protected]95dd38f2009-10-20 20:09:15706 // Hack: we need to ensure the ResourceDispatcherHost is ready before we load
707 // the first extension, because its members listen for loaded notifications.
708 g_browser_process->resource_dispatcher_host();
709
[email protected]9f1087e2009-06-15 17:29:32710 LoadAllExtensions();
[email protected]894bb502009-05-21 22:39:57711
[email protected]9f1087e2009-06-15 17:29:32712 // TODO(erikkay) this should probably be deferred to a future point
713 // rather than running immediately at startup.
[email protected]93fd78f42009-07-10 16:43:17714 CheckForExternalUpdates();
[email protected]894bb502009-05-21 22:39:57715
[email protected]9f1087e2009-06-15 17:29:32716 // TODO(erikkay) this should probably be deferred as well.
717 GarbageCollectExtensions();
[email protected]6014d672008-12-05 00:38:25718}
719
[email protected]420a0ec2011-06-01 01:07:03720bool ExtensionService::UpdateExtension(
721 const std::string& id,
722 const FilePath& extension_path,
723 const GURL& download_url,
724 CrxInstaller** out_crx_installer) {
[email protected]a29a517a2011-01-21 21:11:12725 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20726
[email protected]b2907fd2011-03-25 16:43:37727 PendingExtensionInfo pending_extension_info;
728 bool is_pending_extension = pending_extension_manager_.GetById(
729 id, &pending_extension_info);
[email protected]8ef78fd2010-08-19 17:14:32730
[email protected]8001df22011-04-28 19:59:47731 const Extension* extension =
732 GetExtensionByIdInternal(id, true, true, false);
[email protected]5eb375e92010-11-26 07:50:41733 if (!is_pending_extension && !extension) {
[email protected]aa142702010-03-26 01:26:33734 LOG(WARNING) << "Will not update extension " << id
735 << " because it is not installed or pending";
736 // Delete extension_path since we're not creating a CrxInstaller
737 // that would do it for us.
[email protected]14908b72011-04-20 06:54:36738 if (!BrowserThread::PostTask(
739 BrowserThread::FILE, FROM_HERE,
740 NewRunnableFunction(
741 extension_file_util::DeleteFile, extension_path, false)))
742 NOTREACHED();
[email protected]420a0ec2011-06-01 01:07:03743
744 return false;
[email protected]e957fe52009-06-23 16:51:05745 }
746
[email protected]aa142702010-03-26 01:26:33747 // We want a silent install only for non-pending extensions and
748 // pending extensions that have install_silently set.
749 ExtensionInstallUI* client =
[email protected]b2907fd2011-03-25 16:43:37750 (!is_pending_extension || pending_extension_info.install_silently()) ?
[email protected]aa142702010-03-26 01:26:33751 NULL : new ExtensionInstallUI(profile_);
752
[email protected]14908b72011-04-20 06:54:36753 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(client));
[email protected]6dfbbf82010-03-12 23:09:16754 installer->set_expected_id(id);
[email protected]ec5b50d2010-10-09 16:35:18755 if (is_pending_extension)
[email protected]b2907fd2011-03-25 16:43:37756 installer->set_install_source(pending_extension_info.install_source());
[email protected]5eb375e92010-11-26 07:50:41757 else if (extension)
758 installer->set_install_source(extension->location());
[email protected]0d3e4a22011-06-23 19:02:52759 if (pending_extension_info.install_silently())
760 installer->set_allow_silent_install(true);
[email protected]6dfbbf82010-03-12 23:09:16761 installer->set_delete_source(true);
[email protected]5c8516202010-03-18 21:43:34762 installer->set_original_url(download_url);
[email protected]cb0e50312011-05-09 15:03:07763 installer->set_install_cause(extension_misc::INSTALL_CAUSE_UPDATE);
[email protected]6dfbbf82010-03-12 23:09:16764 installer->InstallCrx(extension_path);
[email protected]420a0ec2011-06-01 01:07:03765
766 if (out_crx_installer)
767 *out_crx_installer = installer;
768
769 return true;
[email protected]e957fe52009-06-23 16:51:05770}
771
[email protected]eaa7dd182010-12-14 11:09:00772void ExtensionService::ReloadExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12773 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]b65272f2009-08-31 15:47:06774 FilePath path;
[email protected]9adb9692010-10-29 23:14:02775 const Extension* current_extension = GetExtensionById(extension_id, false);
[email protected]9cddd4702009-07-27 22:09:40776
[email protected]f17dbd42010-08-16 23:21:10777 // Disable the extension if it's loaded. It might not be loaded if it crashed.
[email protected]b65272f2009-08-31 15:47:06778 if (current_extension) {
[email protected]4814b512009-11-07 00:12:29779 // If the extension has an inspector open for its background page, detach
780 // the inspector and hang onto a cookie for it, so that we can reattach
781 // later.
782 ExtensionProcessManager* manager = profile_->GetExtensionProcessManager();
783 ExtensionHost* host = manager->GetBackgroundHostForExtension(
784 current_extension);
785 if (host) {
786 // Look for an open inspector for the background page.
787 int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost(
788 host->render_view_host());
789 if (devtools_cookie >= 0)
790 orphaned_dev_tools_[extension_id] = devtools_cookie;
791 }
792
[email protected]b65272f2009-08-31 15:47:06793 path = current_extension->path();
[email protected]f17dbd42010-08-16 23:21:10794 DisableExtension(extension_id);
795 disabled_extension_paths_[extension_id] = path;
[email protected]1eb175082010-02-10 09:26:16796 } else {
797 path = unloaded_extension_paths_[extension_id];
[email protected]b65272f2009-08-31 15:47:06798 }
799
[email protected]e6090e42010-03-23 22:44:08800 // Check the installed extensions to see if what we're reloading was already
801 // installed.
802 scoped_ptr<ExtensionInfo> installed_extension(
803 extension_prefs_->GetInstalledExtensionInfo(extension_id));
804 if (installed_extension.get() &&
805 installed_extension->extension_manifest.get()) {
806 LoadInstalledExtension(*installed_extension, false);
807 } else {
808 // We should always be able to remember the extension's path. If it's not in
809 // the map, someone failed to update |unloaded_extension_paths_|.
810 CHECK(!path.empty());
811 LoadExtension(path);
812 }
[email protected]9cddd4702009-07-27 22:09:40813}
814
[email protected]fa2416f2011-05-03 08:41:20815bool ExtensionService::UninstallExtension(
816 const std::string& extension_id_unsafe,
817 bool external_uninstall,
818 std::string* error) {
[email protected]a29a517a2011-01-21 21:11:12819 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20820
[email protected]fa2416f2011-05-03 08:41:20821 // Copy the extension identifier since the reference might have been
822 // obtained via Extension::id() and the extension may be deleted in
823 // this function.
824 std::string extension_id(extension_id_unsafe);
825
[email protected]8001df22011-04-28 19:59:47826 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]631cf822009-05-15 07:01:25827
[email protected]e7afe2452010-08-22 16:19:13828 // Callers should not send us nonexistent extensions.
[email protected]aebe23a32010-12-10 22:15:48829 CHECK(extension);
[email protected]9f1087e2009-06-15 17:29:32830
[email protected]831aa212010-03-26 13:55:19831 // Get hold of information we need after unloading, since the extension
832 // pointer will be invalid then.
833 GURL extension_url(extension->url());
834 Extension::Location location(extension->location());
[email protected]95da88c42011-03-31 10:07:33835
836 // Policy change which triggers an uninstall will always set
837 // |external_uninstall| to true so this is the only way to uninstall
838 // managed extensions.
[email protected]d6ebc9792011-04-07 18:18:33839 if (!Extension::UserMayDisable(location) && !external_uninstall) {
840 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27841 chrome::NOTIFICATION_EXTENSION_UNINSTALL_NOT_ALLOWED,
[email protected]d6ebc9792011-04-07 18:18:33842 Source<Profile>(profile_),
843 Details<const Extension>(extension));
844 if (error != NULL) {
845 *error = errors::kCannotUninstallManagedExtension;
846 }
847 return false;
848 }
[email protected]95da88c42011-03-31 10:07:33849
[email protected]211030342010-09-30 18:41:06850 UninstalledExtensionInfo uninstalled_extension_info(*extension);
[email protected]831aa212010-03-26 13:55:19851
[email protected]9b217652010-10-08 22:04:23852 UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallType",
[email protected]7fa19f82010-12-21 19:40:08853 extension->GetType(), 100);
[email protected]fe2dd7742011-04-19 22:52:49854 RecordPermissionMessagesHistogram(
855 extension, "Extensions.Permissions_Uninstall");
[email protected]9b217652010-10-08 22:04:23856
[email protected]8e5c89a2011-06-07 18:13:33857 TemplateURLService* url_service =
858 TemplateURLServiceFactory::GetForProfile(profile_);
859 if (url_service)
860 url_service->UnregisterExtensionKeyword(extension);
[email protected]56ad3792010-05-28 17:45:33861
[email protected]831aa212010-03-26 13:55:19862 // Unload before doing more cleanup to ensure that nothing is hanging on to
863 // any of these resources.
[email protected]a9f39a312010-12-23 22:14:27864 UnloadExtension(extension_id, UnloadedExtensionInfo::UNINSTALL);
[email protected]831aa212010-03-26 13:55:19865
[email protected]fa2416f2011-05-03 08:41:20866 extension_prefs_->OnExtensionUninstalled(extension_id, location,
[email protected]831aa212010-03-26 13:55:19867 external_uninstall);
[email protected]9f1087e2009-06-15 17:29:32868
869 // Tell the backend to start deleting installed extensions on the file thread.
[email protected]831aa212010-03-26 13:55:19870 if (Extension::LOAD != location) {
[email protected]14908b72011-04-20 06:54:36871 if (!BrowserThread::PostTask(
872 BrowserThread::FILE, FROM_HERE,
873 NewRunnableFunction(
874 &extension_file_util::UninstallExtension,
875 install_directory_,
[email protected]fa2416f2011-05-03 08:41:20876 extension_id)))
[email protected]14908b72011-04-20 06:54:36877 NOTREACHED();
[email protected]9f1087e2009-06-15 17:29:32878 }
879
[email protected]c10da4b02010-03-25 14:38:32880 ClearExtensionData(extension_url);
[email protected]0dfe05c2011-02-23 23:03:36881 UntrackTerminatedExtension(extension_id);
[email protected]211030342010-09-30 18:41:06882
883 // Notify interested parties that we've uninstalled this extension.
884 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27885 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
[email protected]211030342010-09-30 18:41:06886 Source<Profile>(profile_),
887 Details<UninstalledExtensionInfo>(&uninstalled_extension_info));
[email protected]d6ebc9792011-04-07 18:18:33888
889 return true;
[email protected]c10da4b02010-03-25 14:38:32890}
891
[email protected]eaa7dd182010-12-14 11:09:00892void ExtensionService::ClearExtensionData(const GURL& extension_url) {
[email protected]c10da4b02010-03-25 14:38:32893 scoped_refptr<ExtensionDataDeleter> deleter(
894 new ExtensionDataDeleter(profile_, extension_url));
895 deleter->StartDeleting();
[email protected]9f1087e2009-06-15 17:29:32896}
897
[email protected]c3cfb012011-04-06 22:07:35898bool ExtensionService::IsExtensionEnabled(
899 const std::string& extension_id) const {
[email protected]36429da2011-07-11 20:25:18900 const Extension* extension =
901 GetExtensionByIdInternal(extension_id, true, false, false);
902 if (extension)
903 return true;
904
905 // If the extension hasn't been loaded yet, check the prefs for it.
906 return extension_prefs_->GetExtensionState(extension_id) ==
907 Extension::ENABLED;
[email protected]c3cfb012011-04-06 22:07:35908}
909
910bool ExtensionService::IsExternalExtensionUninstalled(
911 const std::string& extension_id) const {
912 return extension_prefs_->IsExternalExtensionUninstalled(extension_id);
913}
914
[email protected]eaa7dd182010-12-14 11:09:00915void ExtensionService::EnableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12916 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20917
[email protected]06f92562011-04-29 19:27:31918 if (IsExtensionEnabled(extension_id))
[email protected]0c6da502009-08-14 22:32:39919 return;
[email protected]0c6da502009-08-14 22:32:39920
[email protected]b3317ad2011-04-28 23:46:00921 extension_prefs_->SetExtensionState(extension_id, Extension::ENABLED);
[email protected]1784e83a2009-09-08 21:01:52922
[email protected]06f92562011-04-29 19:27:31923 const Extension* extension =
924 GetExtensionByIdInternal(extension_id, false, true, false);
925 // This can happen if sync enables an extension that is not
926 // installed yet.
927 if (!extension)
928 return;
929
[email protected]0c6da502009-08-14 22:32:39930 // Move it over to the enabled list.
[email protected]00cd9c42010-11-02 20:15:57931 extensions_.push_back(make_scoped_refptr(extension));
[email protected]0c6da502009-08-14 22:32:39932 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
933 disabled_extensions_.end(),
934 extension);
935 disabled_extensions_.erase(iter);
936
[email protected]f681c782010-11-19 11:19:39937 // Make sure any browser action contained within it is not hidden.
938 extension_prefs_->SetBrowserActionVisibility(extension, true);
939
[email protected]62d30f42009-10-01 22:36:06940 NotifyExtensionLoaded(extension);
[email protected]0c6da502009-08-14 22:32:39941}
942
[email protected]eaa7dd182010-12-14 11:09:00943void ExtensionService::DisableExtension(const std::string& extension_id) {
[email protected]a29a517a2011-01-21 21:11:12944 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:20945
[email protected]b2ba9962009-12-10 20:10:15946 // The extension may have been disabled already.
[email protected]06f92562011-04-29 19:27:31947 if (!IsExtensionEnabled(extension_id))
[email protected]1784e83a2009-09-08 21:01:52948 return;
[email protected]1784e83a2009-09-08 21:01:52949
[email protected]06f92562011-04-29 19:27:31950 const Extension* extension = GetInstalledExtension(extension_id);
951 // |extension| can be NULL if sync disables an extension that is not
952 // installed yet.
953 if (extension && !Extension::UserMayDisable(extension->location()))
[email protected]95da88c42011-03-31 10:07:33954 return;
955
[email protected]b3317ad2011-04-28 23:46:00956 extension_prefs_->SetExtensionState(extension_id, Extension::DISABLED);
[email protected]1784e83a2009-09-08 21:01:52957
[email protected]06f92562011-04-29 19:27:31958 extension = GetExtensionByIdInternal(extension_id, true, false, true);
959 if (!extension)
960 return;
961
[email protected]1784e83a2009-09-08 21:01:52962 // Move it over to the disabled list.
[email protected]00cd9c42010-11-02 20:15:57963 disabled_extensions_.push_back(make_scoped_refptr(extension));
[email protected]1784e83a2009-09-08 21:01:52964 ExtensionList::iterator iter = std::find(extensions_.begin(),
965 extensions_.end(),
966 extension);
[email protected]06f92562011-04-29 19:27:31967 if (iter != extensions_.end()) {
968 extensions_.erase(iter);
969 } else {
970 iter = std::find(terminated_extensions_.begin(),
971 terminated_extensions_.end(),
972 extension);
973 terminated_extensions_.erase(iter);
974 }
[email protected]1784e83a2009-09-08 21:01:52975
[email protected]a9f39a312010-12-23 22:14:27976 NotifyExtensionUnloaded(extension, UnloadedExtensionInfo::DISABLE);
[email protected]1784e83a2009-09-08 21:01:52977}
978
[email protected]eaa7dd182010-12-14 11:09:00979void ExtensionService::GrantPermissions(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:25980 CHECK(extension);
981
982 // We only maintain the granted permissions prefs for INTERNAL extensions.
[email protected]139e5732011-03-16 19:29:51983 CHECK_EQ(Extension::INTERNAL, extension->location());
[email protected]8d888c12010-11-30 00:00:25984
[email protected]8d888c12010-11-30 00:00:25985 extension_prefs_->AddGrantedPermissions(extension->id(),
[email protected]0d3e4a22011-06-23 19:02:52986 extension->permission_set());
[email protected]8d888c12010-11-30 00:00:25987}
988
[email protected]eaa7dd182010-12-14 11:09:00989void ExtensionService::GrantPermissionsAndEnableExtension(
[email protected]8d888c12010-11-30 00:00:25990 const Extension* extension) {
991 CHECK(extension);
[email protected]fe2dd7742011-04-19 22:52:49992 RecordPermissionMessagesHistogram(
993 extension, "Extensions.Permissions_ReEnable");
[email protected]8d888c12010-11-30 00:00:25994 GrantPermissions(extension);
995 extension_prefs_->SetDidExtensionEscalatePermissions(extension, false);
996 EnableExtension(extension->id());
997}
998
[email protected]eaa7dd182010-12-14 11:09:00999void ExtensionService::LoadExtension(const FilePath& extension_path) {
[email protected]e49002a2011-07-13 03:40:441000 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
1001 NewRunnableMethod(backend_.get(),
1002 &ExtensionServiceBackend::LoadSingleExtension,
1003 extension_path, true));
1004}
1005
1006void ExtensionService::LoadExtensionFromCommandLine(
1007 const FilePath& extension_path) {
1008 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
1009 NewRunnableMethod(backend_.get(),
1010 &ExtensionServiceBackend::LoadSingleExtension,
1011 extension_path, false));
[email protected]9f1087e2009-06-15 17:29:321012}
1013
[email protected]eaa7dd182010-12-14 11:09:001014void ExtensionService::LoadComponentExtensions() {
[email protected]1952c7d2010-03-04 23:48:341015 for (RegisteredComponentExtensions::iterator it =
1016 component_extension_manifests_.begin();
1017 it != component_extension_manifests_.end(); ++it) {
[email protected]61b55b62011-03-24 09:03:101018 LoadComponentExtension(*it);
[email protected]1952c7d2010-03-04 23:48:341019 }
1020}
1021
[email protected]a964e112011-04-14 21:52:511022const Extension* ExtensionService::LoadComponentExtension(
[email protected]61b55b62011-03-24 09:03:101023 const ComponentExtensionInfo &info) {
1024 JSONStringValueSerializer serializer(info.manifest);
1025 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
1026 if (!manifest.get()) {
1027 DLOG(ERROR) << "Failed to parse manifest for extension";
[email protected]a964e112011-04-14 21:52:511028 return NULL;
[email protected]61b55b62011-03-24 09:03:101029 }
1030
[email protected]83048a22011-03-29 00:14:131031 int flags = Extension::REQUIRE_KEY;
1032 if (Extension::ShouldDoStrictErrorChecking(Extension::COMPONENT))
1033 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]61b55b62011-03-24 09:03:101034 std::string error;
1035 scoped_refptr<const Extension> extension(Extension::Create(
1036 info.root_directory,
1037 Extension::COMPONENT,
1038 *static_cast<DictionaryValue*>(manifest.get()),
[email protected]83048a22011-03-29 00:14:131039 flags,
[email protected]61b55b62011-03-24 09:03:101040 &error));
1041 if (!extension.get()) {
1042 NOTREACHED() << error;
[email protected]a964e112011-04-14 21:52:511043 return NULL;
[email protected]61b55b62011-03-24 09:03:101044 }
1045 AddExtension(extension);
[email protected]a964e112011-04-14 21:52:511046 return extension;
[email protected]61b55b62011-03-24 09:03:101047}
1048
[email protected]ad63a792011-06-16 06:41:111049void ExtensionService::UnloadComponentExtension(
1050 const ComponentExtensionInfo& info) {
1051 JSONStringValueSerializer serializer(info.manifest);
1052 scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL));
1053 if (!manifest.get()) {
1054 DLOG(ERROR) << "Failed to parse manifest for extension";
1055 return;
1056 }
1057 std::string public_key;
1058 std::string public_key_bytes;
1059 std::string id;
1060 if (!static_cast<DictionaryValue*>(manifest.get())->
1061 GetString(extension_manifest_keys::kPublicKey, &public_key) ||
1062 !Extension::ParsePEMKeyBytes(public_key, &public_key_bytes) ||
1063 !Extension::GenerateId(public_key_bytes, &id)) {
1064 DLOG(ERROR) << "Failed to get extension id";
1065 return;
1066 }
1067 UnloadExtension(id, UnloadedExtensionInfo::DISABLE);
1068}
1069
[email protected]eaa7dd182010-12-14 11:09:001070void ExtensionService::LoadAllExtensions() {
[email protected]a29a517a2011-01-21 21:11:121071 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a315ba92010-11-16 14:12:211072
[email protected]cc2c3432009-11-06 17:24:361073 base::TimeTicks start_time = base::TimeTicks::Now();
1074
[email protected]1952c7d2010-03-04 23:48:341075 // Load any component extensions.
1076 LoadComponentExtensions();
1077
[email protected]e72e8eb82009-06-18 17:21:511078 // Load the previously installed extensions.
[email protected]a315ba92010-11-16 14:12:211079 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
[email protected]e6090e42010-03-23 22:44:081080 extension_prefs_->GetInstalledExtensionsInfo());
[email protected]c6d474f82009-12-16 21:11:061081
[email protected]a315ba92010-11-16 14:12:211082 std::vector<int> reload_reason_counts(NUM_MANIFEST_RELOAD_REASONS, 0);
1083 bool should_write_prefs = false;
1084
1085 for (size_t i = 0; i < extensions_info->size(); ++i) {
1086 ExtensionInfo* info = extensions_info->at(i).get();
1087
1088 ManifestReloadReason reload_reason = ShouldReloadExtensionManifest(*info);
1089 ++reload_reason_counts[reload_reason];
1090 UMA_HISTOGRAM_ENUMERATION("Extensions.ManifestReloadEnumValue",
1091 reload_reason, 100);
1092
1093 if (reload_reason != NOT_NEEDED) {
1094 // Reloading and extension reads files from disk. We do this on the
1095 // UI thread because reloads should be very rare, and the complexity
1096 // added by delaying the time when the extensions service knows about
1097 // all extensions is significant. See crbug.com/37548 for details.
1098 // |allow_io| disables tests that file operations run on the file
1099 // thread.
1100 base::ThreadRestrictions::ScopedAllowIO allow_io;
1101
[email protected]83048a22011-03-29 00:14:131102 int flags = Extension::NO_FLAGS;
1103 if (Extension::ShouldDoStrictErrorChecking(info->extension_location))
1104 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:481105 if (extension_prefs_->AllowFileAccess(info->extension_id))
1106 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]620db1762011-07-15 21:57:341107 if (extension_prefs_->IsFromWebStore(info->extension_id))
1108 flags |= Extension::FROM_WEBSTORE;
[email protected]a315ba92010-11-16 14:12:211109 std::string error;
1110 scoped_refptr<const Extension> extension(
1111 extension_file_util::LoadExtension(
[email protected]542258c2011-03-04 21:25:311112 info->extension_path,
1113 info->extension_location,
[email protected]83048a22011-03-29 00:14:131114 flags,
[email protected]542258c2011-03-04 21:25:311115 &error));
[email protected]a315ba92010-11-16 14:12:211116
1117 if (extension.get()) {
1118 extensions_info->at(i)->extension_manifest.reset(
1119 static_cast<DictionaryValue*>(
1120 extension->manifest_value()->DeepCopy()));
1121 should_write_prefs = true;
1122 }
[email protected]c6d474f82009-12-16 21:11:061123 }
1124 }
1125
[email protected]a315ba92010-11-16 14:12:211126 for (size_t i = 0; i < extensions_info->size(); ++i) {
1127 LoadInstalledExtension(*extensions_info->at(i), should_write_prefs);
[email protected]c6d474f82009-12-16 21:11:061128 }
1129
[email protected]ae09ca62009-08-21 19:46:461130 OnLoadedInstalledExtensions();
[email protected]cc2c3432009-11-06 17:24:361131
[email protected]a315ba92010-11-16 14:12:211132 // The histograms Extensions.ManifestReload* allow us to validate
1133 // the assumption that reloading manifest is a rare event.
1134 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNotNeeded",
1135 reload_reason_counts[NOT_NEEDED]);
1136 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadUnpackedDir",
1137 reload_reason_counts[UNPACKED_DIR]);
1138 UMA_HISTOGRAM_COUNTS_100("Extensions.ManifestReloadNeedsRelocalization",
1139 reload_reason_counts[NEEDS_RELOCALIZATION]);
1140
[email protected]cc2c3432009-11-06 17:24:361141 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size());
1142 UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size());
1143
[email protected]1952c7d2010-03-04 23:48:341144 UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime",
1145 base::TimeTicks::Now() - start_time);
[email protected]cc2c3432009-11-06 17:24:361146
[email protected]9b217652010-10-08 22:04:231147 int app_count = 0;
1148 int hosted_app_count = 0;
1149 int packaged_app_count = 0;
[email protected]1952c7d2010-03-04 23:48:341150 int user_script_count = 0;
1151 int extension_count = 0;
1152 int theme_count = 0;
1153 int external_count = 0;
1154 int page_action_count = 0;
1155 int browser_action_count = 0;
1156 ExtensionList::iterator ex;
1157 for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) {
[email protected]9b217652010-10-08 22:04:231158 Extension::Location location = (*ex)->location();
[email protected]7fa19f82010-12-21 19:40:081159 Extension::Type type = (*ex)->GetType();
[email protected]9b217652010-10-08 22:04:231160 if ((*ex)->is_app()) {
1161 UMA_HISTOGRAM_ENUMERATION("Extensions.AppLocation",
1162 location, 100);
1163 } else if (type == Extension::TYPE_EXTENSION) {
1164 UMA_HISTOGRAM_ENUMERATION("Extensions.ExtensionLocation",
1165 location, 100);
1166 }
1167
[email protected]1952c7d2010-03-04 23:48:341168 // Don't count component extensions, since they are only extensions as an
1169 // implementation detail.
[email protected]9b217652010-10-08 22:04:231170 if (location == Extension::COMPONENT)
[email protected]1952c7d2010-03-04 23:48:341171 continue;
1172
[email protected]e8c729a2010-03-09 19:55:191173 // Don't count unpacked extensions, since they're a developer-specific
1174 // feature.
[email protected]9b217652010-10-08 22:04:231175 if (location == Extension::LOAD)
[email protected]e8c729a2010-03-09 19:55:191176 continue;
1177
[email protected]9b217652010-10-08 22:04:231178 // Using an enumeration shows us the total installed ratio across all users.
1179 // Using the totals per user at each startup tells us the distribution of
1180 // usage for each user (e.g. 40% of users have at least one app installed).
1181 UMA_HISTOGRAM_ENUMERATION("Extensions.LoadType", type, 100);
1182 switch (type) {
1183 case Extension::TYPE_THEME:
[email protected]a315ba92010-11-16 14:12:211184 ++theme_count;
[email protected]9b217652010-10-08 22:04:231185 break;
1186 case Extension::TYPE_USER_SCRIPT:
[email protected]a315ba92010-11-16 14:12:211187 ++user_script_count;
[email protected]9b217652010-10-08 22:04:231188 break;
1189 case Extension::TYPE_HOSTED_APP:
[email protected]a315ba92010-11-16 14:12:211190 ++app_count;
1191 ++hosted_app_count;
[email protected]9b217652010-10-08 22:04:231192 break;
1193 case Extension::TYPE_PACKAGED_APP:
[email protected]a315ba92010-11-16 14:12:211194 ++app_count;
1195 ++packaged_app_count;
[email protected]9b217652010-10-08 22:04:231196 break;
1197 case Extension::TYPE_EXTENSION:
1198 default:
[email protected]a315ba92010-11-16 14:12:211199 ++extension_count;
[email protected]9b217652010-10-08 22:04:231200 break;
[email protected]cc2c3432009-11-06 17:24:361201 }
[email protected]9b217652010-10-08 22:04:231202 if (Extension::IsExternalLocation(location))
[email protected]a315ba92010-11-16 14:12:211203 ++external_count;
[email protected]9b217652010-10-08 22:04:231204 if ((*ex)->page_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211205 ++page_action_count;
[email protected]9b217652010-10-08 22:04:231206 if ((*ex)->browser_action() != NULL)
[email protected]a315ba92010-11-16 14:12:211207 ++browser_action_count;
[email protected]fe2dd7742011-04-19 22:52:491208
1209 RecordPermissionMessagesHistogram(
1210 ex->get(), "Extensions.Permissions_Load");
[email protected]cc2c3432009-11-06 17:24:361211 }
[email protected]9b217652010-10-08 22:04:231212 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadApp", app_count);
1213 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadHostedApp", hosted_app_count);
1214 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPackagedApp", packaged_app_count);
[email protected]1952c7d2010-03-04 23:48:341215 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count);
1216 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count);
1217 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count);
1218 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count);
1219 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count);
1220 UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction",
1221 browser_action_count);
[email protected]ae09ca62009-08-21 19:46:461222}
1223
[email protected]fe2dd7742011-04-19 22:52:491224// static
1225void ExtensionService::RecordPermissionMessagesHistogram(
1226 const Extension* e, const char* histogram) {
1227 // Since this is called from multiple sources, and since the Histogram macros
1228 // use statics, we need to manually lookup the Histogram ourselves.
1229 base::Histogram* counter = base::LinearHistogram::FactoryGet(
1230 histogram,
1231 1,
[email protected]0d3e4a22011-06-23 19:02:521232 ExtensionPermissionMessage::kEnumBoundary,
1233 ExtensionPermissionMessage::kEnumBoundary + 1,
[email protected]fe2dd7742011-04-19 22:52:491234 base::Histogram::kUmaTargetedHistogramFlag);
1235
[email protected]0d3e4a22011-06-23 19:02:521236 ExtensionPermissionMessages permissions = e->GetPermissionMessages();
[email protected]fe2dd7742011-04-19 22:52:491237 if (permissions.empty()) {
[email protected]0d3e4a22011-06-23 19:02:521238 counter->Add(ExtensionPermissionMessage::kNone);
[email protected]fe2dd7742011-04-19 22:52:491239 } else {
[email protected]0d3e4a22011-06-23 19:02:521240 for (ExtensionPermissionMessages::iterator it = permissions.begin();
1241 it != permissions.end(); ++it)
1242 counter->Add(it->id());
[email protected]fe2dd7742011-04-19 22:52:491243 }
1244}
1245
[email protected]eaa7dd182010-12-14 11:09:001246void ExtensionService::LoadInstalledExtension(const ExtensionInfo& info,
[email protected]542258c2011-03-04 21:25:311247 bool write_to_prefs) {
[email protected]ae09ca62009-08-21 19:46:461248 std::string error;
[email protected]9adb9692010-10-29 23:14:021249 scoped_refptr<const Extension> extension(NULL);
[email protected]306a2bd2010-08-11 14:56:361250 if (!extension_prefs_->IsExtensionAllowedByPolicy(info.extension_id)) {
1251 error = errors::kDisabledByPolicy;
1252 } else if (info.extension_manifest.get()) {
[email protected]83048a22011-03-29 00:14:131253 int flags = Extension::NO_FLAGS;
1254 if (info.extension_location != Extension::LOAD)
1255 flags |= Extension::REQUIRE_KEY;
1256 if (Extension::ShouldDoStrictErrorChecking(info.extension_location))
1257 flags |= Extension::STRICT_ERROR_CHECKS;
[email protected]3aff9ad2011-04-01 20:26:481258 if (extension_prefs_->AllowFileAccess(info.extension_id))
1259 flags |= Extension::ALLOW_FILE_ACCESS;
[email protected]620db1762011-07-15 21:57:341260 if (extension_prefs_->IsFromWebStore(info.extension_id))
1261 flags |= Extension::FROM_WEBSTORE;
[email protected]66e4eb32010-10-27 20:37:411262 extension = Extension::Create(
[email protected]542258c2011-03-04 21:25:311263 info.extension_path,
1264 info.extension_location,
1265 *info.extension_manifest,
[email protected]83048a22011-03-29 00:14:131266 flags,
[email protected]542258c2011-03-04 21:25:311267 &error);
[email protected]ae09ca62009-08-21 19:46:461268 } else {
[email protected]c6d474f82009-12-16 21:11:061269 error = errors::kManifestUnreadable;
[email protected]ae09ca62009-08-21 19:46:461270 }
1271
1272 if (!extension) {
[email protected]c6d474f82009-12-16 21:11:061273 ReportExtensionLoadError(info.extension_path,
[email protected]d11c8e92009-10-20 23:26:401274 error,
[email protected]432115822011-07-10 15:52:271275 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
[email protected]d11c8e92009-10-20 23:26:401276 false);
[email protected]ae09ca62009-08-21 19:46:461277 return;
1278 }
1279
[email protected]c6d474f82009-12-16 21:11:061280 if (write_to_prefs)
1281 extension_prefs_->UpdateManifest(extension);
1282
[email protected]49cd1572011-02-08 21:38:451283 AddExtension(extension);
[email protected]9f1087e2009-06-15 17:29:321284}
1285
[email protected]eaa7dd182010-12-14 11:09:001286void ExtensionService::NotifyExtensionLoaded(const Extension* extension) {
[email protected]57a777f72010-03-31 01:09:421287 // The ChromeURLRequestContexts need to be first to know that the extension
[email protected]62d30f42009-10-01 22:36:061288 // was loaded, otherwise a race can arise where a renderer that is created
1289 // for the extension may try to load an extension URL with an extension id
[email protected]57a777f72010-03-31 01:09:421290 // that the request context doesn't yet know about. The profile is responsible
1291 // for ensuring its URLRequestContexts appropriately discover the loaded
1292 // extension.
[email protected]c8d407e2011-04-28 21:27:171293 profile_->RegisterExtensionWithRequestContexts(extension);
[email protected]62d30f42009-10-01 22:36:061294
[email protected]c8d407e2011-04-28 21:27:171295 // Tell subsystems that use the EXTENSION_LOADED notification about the new
1296 // extension.
[email protected]62d30f42009-10-01 22:36:061297 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271298 chrome::NOTIFICATION_EXTENSION_LOADED,
[email protected]24e7a9d2009-11-04 11:11:341299 Source<Profile>(profile_),
[email protected]9adb9692010-10-29 23:14:021300 Details<const Extension>(extension));
[email protected]2de307592011-04-05 21:16:581301
[email protected]c8d407e2011-04-28 21:27:171302 // Tell renderers about the new extension.
[email protected]77a6970c2011-04-23 16:58:561303 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1304 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171305 RenderProcessHost* host = i.GetCurrentValue();
1306 if (host->profile()->GetOriginalProfile() ==
1307 profile_->GetOriginalProfile()) {
1308 host->Send(
1309 new ExtensionMsg_Loaded(ExtensionMsg_Loaded_Params(extension)));
1310 }
[email protected]77a6970c2011-04-23 16:58:561311 }
1312
[email protected]c8d407e2011-04-28 21:27:171313 // Tell a random-ass collection of other subsystems about the new extension.
1314 // TODO(aa): What should we do with all this goop? Can it move into the
1315 // relevant objects via EXTENSION_LOADED?
1316
1317 profile_->GetExtensionSpecialStoragePolicy()->
1318 GrantRightsForExtension(extension);
1319
1320 UpdateActiveExtensionsInCrashReporter();
1321
1322 ExtensionWebUI::RegisterChromeURLOverrides(
1323 profile_, extension->GetChromeURLOverrides());
1324
[email protected]8e5c89a2011-06-07 18:13:331325 TemplateURLService* url_service =
1326 TemplateURLServiceFactory::GetForProfile(profile_);
1327 if (url_service)
1328 url_service->RegisterExtensionKeyword(extension);
[email protected]c8d407e2011-04-28 21:27:171329
1330 // Load the icon for omnibox-enabled extensions so it will be ready to display
1331 // in the URL bar.
1332 if (!extension->omnibox_keyword().empty()) {
1333 omnibox_popup_icon_manager_.LoadIcon(extension);
1334 omnibox_icon_manager_.LoadIcon(extension);
1335 }
1336
1337 // If the extension has permission to load chrome://favicon/ resources we need
1338 // to make sure that the FaviconSource is registered with the
1339 // ChromeURLDataManager.
1340 if (extension->HasHostPermission(GURL(chrome::kChromeUIFaviconURL))) {
1341 FaviconSource* favicon_source = new FaviconSource(profile_,
1342 FaviconSource::FAVICON);
1343 profile_->GetChromeURLDataManager()->AddDataSource(favicon_source);
1344 }
1345
1346 // TODO(mpcomplete): This ends up affecting all profiles. See crbug.com/80757.
[email protected]2de307592011-04-05 21:16:581347 bool plugins_changed = false;
1348 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1349 const Extension::PluginInfo& plugin = extension->plugins()[i];
1350 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1351 webkit::npapi::PluginList::Singleton()->AddExtraPluginPath(plugin.path);
1352 plugins_changed = true;
1353 if (!plugin.is_public) {
1354 PluginService::GetInstance()->RestrictPluginToUrl(
1355 plugin.path, extension->url());
1356 }
1357 }
[email protected]84396dbc2011-04-14 06:33:421358
1359 bool nacl_modules_changed = false;
1360 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1361 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001362 RegisterNaClModule(module.url, module.mime_type);
[email protected]84396dbc2011-04-14 06:33:421363 nacl_modules_changed = true;
1364 }
1365
[email protected]ed0ba002011-05-26 16:55:131366 if (nacl_modules_changed)
1367 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421368
1369 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581370 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]b4d9d122011-06-17 01:58:541371
1372#if defined(OS_CHROMEOS) && defined(TOUCH_UI)
[email protected]c8d4e9462011-06-29 23:18:571373 chromeos::input_method::InputMethodManager* input_method_manager =
1374 chromeos::input_method::InputMethodManager::GetInstance();
[email protected]b4d9d122011-06-17 01:58:541375 for (std::vector<Extension::InputComponentInfo>::const_iterator component =
1376 extension->input_components().begin();
1377 component != extension->input_components().end();
1378 ++component) {
1379 if (component->type == Extension::INPUT_COMPONENT_TYPE_VIRTUAL_KEYBOARD &&
1380 !component->layouts.empty()) {
1381 const bool is_system =
1382 !Extension::IsExternalLocation(extension->location());
[email protected]c8d4e9462011-06-29 23:18:571383 input_method_manager->RegisterVirtualKeyboard(extension->url(),
[email protected]b4d9d122011-06-17 01:58:541384 component->layouts,
1385 is_system);
1386 }
1387 }
1388#endif
[email protected]62d30f42009-10-01 22:36:061389}
1390
[email protected]a9f39a312010-12-23 22:14:271391void ExtensionService::NotifyExtensionUnloaded(
1392 const Extension* extension, UnloadedExtensionInfo::Reason reason) {
1393 UnloadedExtensionInfo details(extension, reason);
[email protected]62d30f42009-10-01 22:36:061394 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271395 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341396 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271397 Details<UnloadedExtensionInfo>(&details));
[email protected]62d30f42009-10-01 22:36:061398
[email protected]77a6970c2011-04-23 16:58:561399 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
1400 !i.IsAtEnd(); i.Advance()) {
[email protected]c8d407e2011-04-28 21:27:171401 RenderProcessHost* host = i.GetCurrentValue();
1402 if (host->profile()->GetOriginalProfile() ==
1403 profile_->GetOriginalProfile()) {
1404 host->Send(new ExtensionMsg_Unloaded(extension->id()));
1405 }
[email protected]77a6970c2011-04-23 16:58:561406 }
1407
[email protected]dd163fb02011-05-04 22:22:171408 profile_->UnregisterExtensionWithRequestContexts(extension->id(), reason);
[email protected]c8d407e2011-04-28 21:27:171409 profile_->GetExtensionSpecialStoragePolicy()->
1410 RevokeRightsForExtension(extension);
1411
1412 ExtensionWebUI::UnregisterChromeURLOverrides(
1413 profile_, extension->GetChromeURLOverrides());
1414
[email protected]b777b332011-04-16 04:01:081415#if defined(OS_CHROMEOS)
1416 // Revoke external file access to
[email protected]c8d407e2011-04-28 21:27:171417 if (profile_->GetFileSystemContext() &&
1418 profile_->GetFileSystemContext()->path_manager() &&
1419 profile_->GetFileSystemContext()->path_manager()->external_provider()) {
1420 profile_->GetFileSystemContext()->path_manager()->external_provider()->
1421 RevokeAccessForExtension(extension->id());
[email protected]62d30f42009-10-01 22:36:061422 }
[email protected]c8d407e2011-04-28 21:27:171423#endif
1424
1425 UpdateActiveExtensionsInCrashReporter();
[email protected]2de307592011-04-05 21:16:581426
1427 bool plugins_changed = false;
1428 for (size_t i = 0; i < extension->plugins().size(); ++i) {
1429 const Extension::PluginInfo& plugin = extension->plugins()[i];
[email protected]14908b72011-04-20 06:54:361430 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
1431 NewRunnableFunction(&ForceShutdownPlugin,
1432 plugin.path)))
1433 NOTREACHED();
[email protected]2de307592011-04-05 21:16:581434 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
1435 webkit::npapi::PluginList::Singleton()->RemoveExtraPluginPath(
1436 plugin.path);
1437 plugins_changed = true;
1438 if (!plugin.is_public)
1439 PluginService::GetInstance()->RestrictPluginToUrl(plugin.path, GURL());
1440 }
[email protected]84396dbc2011-04-14 06:33:421441
1442 bool nacl_modules_changed = false;
1443 for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
1444 const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
[email protected]cebc3dc2011-04-18 17:15:001445 UnregisterNaClModule(module.url);
[email protected]84396dbc2011-04-14 06:33:421446 nacl_modules_changed = true;
1447 }
1448
[email protected]ed0ba002011-05-26 16:55:131449 if (nacl_modules_changed)
1450 UpdatePluginListWithNaClModules();
[email protected]84396dbc2011-04-14 06:33:421451
1452 if (plugins_changed || nacl_modules_changed)
[email protected]2de307592011-04-05 21:16:581453 PluginService::GetInstance()->PurgePluginListCache(false);
[email protected]62d30f42009-10-01 22:36:061454}
1455
[email protected]eaa7dd182010-12-14 11:09:001456void ExtensionService::UpdateExtensionBlacklist(
[email protected]6b75ec32009-08-14 06:37:181457 const std::vector<std::string>& blacklist) {
1458 // Use this set to indicate if an extension in the blacklist has been used.
1459 std::set<std::string> blacklist_set;
1460 for (unsigned int i = 0; i < blacklist.size(); ++i) {
1461 if (Extension::IdIsValid(blacklist[i])) {
1462 blacklist_set.insert(blacklist[i]);
1463 }
1464 }
1465 extension_prefs_->UpdateBlacklist(blacklist_set);
1466 std::vector<std::string> to_be_removed;
1467 // Loop current extensions, unload installed extensions.
1468 for (ExtensionList::const_iterator iter = extensions_.begin();
1469 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021470 const Extension* extension = (*iter);
[email protected]6b75ec32009-08-14 06:37:181471 if (blacklist_set.find(extension->id()) != blacklist_set.end()) {
1472 to_be_removed.push_back(extension->id());
1473 }
1474 }
1475
1476 // UnloadExtension will change the extensions_ list. So, we should
1477 // call it outside the iterator loop.
1478 for (unsigned int i = 0; i < to_be_removed.size(); ++i) {
[email protected]a9f39a312010-12-23 22:14:271479 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]6b75ec32009-08-14 06:37:181480 }
1481}
1482
[email protected]0a071a32011-02-08 00:18:241483Profile* ExtensionService::profile() {
1484 return profile_;
1485}
1486
[email protected]eaa7dd182010-12-14 11:09:001487ExtensionPrefs* ExtensionService::extension_prefs() {
[email protected]ddb1e5a2010-12-13 20:10:451488 return extension_prefs_;
1489}
1490
[email protected]b790b072011-05-20 09:46:441491ExtensionContentSettingsStore*
1492 ExtensionService::GetExtensionContentSettingsStore() {
1493 return extension_prefs()->content_settings_store();
1494}
1495
[email protected]2859946f2011-04-04 18:18:061496ExtensionUpdater* ExtensionService::updater() {
1497 return updater_.get();
1498}
1499
[email protected]eaa7dd182010-12-14 11:09:001500void ExtensionService::CheckAdminBlacklist() {
[email protected]aa96d3a2010-08-21 08:45:251501 std::vector<std::string> to_be_removed;
1502 // Loop through extensions list, unload installed extensions.
1503 for (ExtensionList::const_iterator iter = extensions_.begin();
1504 iter != extensions_.end(); ++iter) {
[email protected]9adb9692010-10-29 23:14:021505 const Extension* extension = (*iter);
[email protected]aa96d3a2010-08-21 08:45:251506 if (!extension_prefs_->IsExtensionAllowedByPolicy(extension->id()))
1507 to_be_removed.push_back(extension->id());
1508 }
1509
1510 // UnloadExtension will change the extensions_ list. So, we should
1511 // call it outside the iterator loop.
1512 for (unsigned int i = 0; i < to_be_removed.size(); ++i)
[email protected]a9f39a312010-12-23 22:14:271513 UnloadExtension(to_be_removed[i], UnloadedExtensionInfo::DISABLE);
[email protected]aa96d3a2010-08-21 08:45:251514}
1515
[email protected]31206602011-04-13 23:07:321516void ExtensionService::CheckForUpdatesSoon() {
[email protected]c3cfb012011-04-06 22:07:351517 if (updater()) {
[email protected]31206602011-04-13 23:07:321518 updater()->CheckSoon();
[email protected]c3cfb012011-04-06 22:07:351519 } else {
[email protected]31206602011-04-13 23:07:321520 LOG(WARNING) << "CheckForUpdatesSoon() called with auto-update turned off";
[email protected]c3cfb012011-04-06 22:07:351521 }
1522}
1523
[email protected]b05fb9ff2011-04-23 00:07:561524ExtensionSyncData ExtensionService::GetSyncDataHelper(
1525 const Extension& extension) const {
1526 const std::string& id = extension.id();
1527 ExtensionSyncData data;
1528 data.id = id;
1529 data.uninstalled = false;
1530 data.enabled = IsExtensionEnabled(id);
1531 data.incognito_enabled = IsIncognitoEnabled(id);
1532 data.version = *extension.version();
1533 data.update_url = extension.update_url();
1534 data.name = extension.name();
1535 return data;
1536}
1537
1538bool ExtensionService::GetSyncData(
[email protected]85fc9202011-05-05 00:04:591539 const Extension& extension,
[email protected]b05fb9ff2011-04-23 00:07:561540 ExtensionFilter filter,
1541 ExtensionSyncData* extension_sync_data) const {
[email protected]85fc9202011-05-05 00:04:591542 if (!(*filter)(extension)) {
[email protected]b05fb9ff2011-04-23 00:07:561543 return false;
1544 }
[email protected]85fc9202011-05-05 00:04:591545 *extension_sync_data = GetSyncDataHelper(extension);
[email protected]b05fb9ff2011-04-23 00:07:561546 return true;
1547}
1548
1549void ExtensionService::GetSyncDataListHelper(
1550 const ExtensionList& extensions,
1551 ExtensionFilter filter,
1552 std::vector<ExtensionSyncData>* sync_data_list) const {
1553 for (ExtensionList::const_iterator it = extensions.begin();
1554 it != extensions.end(); ++it) {
1555 const Extension& extension = **it;
1556 if ((*filter)(extension)) {
1557 sync_data_list->push_back(GetSyncDataHelper(extension));
1558 }
1559 }
1560}
1561
1562std::vector<ExtensionSyncData> ExtensionService::GetSyncDataList(
1563 ExtensionFilter filter) const {
1564 std::vector<ExtensionSyncData> sync_data_list;
1565 GetSyncDataListHelper(extensions_, filter, &sync_data_list);
1566 GetSyncDataListHelper(disabled_extensions_, filter, &sync_data_list);
[email protected]45b6fee2011-05-03 09:41:311567 GetSyncDataListHelper(terminated_extensions_, filter, &sync_data_list);
[email protected]b05fb9ff2011-04-23 00:07:561568 return sync_data_list;
1569}
1570
[email protected]90310d92011-04-17 07:35:041571void ExtensionService::ProcessSyncData(
1572 const ExtensionSyncData& extension_sync_data,
[email protected]b05fb9ff2011-04-23 00:07:561573 ExtensionFilter filter) {
[email protected]90310d92011-04-17 07:35:041574 const std::string& id = extension_sync_data.id;
1575
1576 // Handle uninstalls first.
1577 if (extension_sync_data.uninstalled) {
1578 std::string error;
1579 if (!UninstallExtensionHelper(this, id)) {
1580 LOG(WARNING) << "Could not uninstall extension " << id
1581 << " for sync";
1582 }
1583 return;
1584 }
1585
[email protected]06f92562011-04-29 19:27:311586 // Set user settings.
1587 if (extension_sync_data.enabled) {
1588 EnableExtension(id);
1589 } else {
1590 DisableExtension(id);
1591 }
[email protected]81b14cc2011-04-29 00:39:371592 SetIsIncognitoEnabled(id, extension_sync_data.incognito_enabled);
1593
[email protected]06f92562011-04-29 19:27:311594 const Extension* extension = GetInstalledExtension(id);
[email protected]90310d92011-04-17 07:35:041595 if (extension) {
[email protected]06f92562011-04-29 19:27:311596 // If the extension is already installed, check if it's outdated.
[email protected]90310d92011-04-17 07:35:041597 int result = extension->version()->CompareTo(extension_sync_data.version);
1598 if (result < 0) {
1599 // Extension is outdated.
1600 CheckForUpdatesSoon();
1601 } else if (result > 0) {
1602 // Sync version is outdated. Do nothing for now, as sync code
1603 // in other places will eventually update the sync data.
1604 //
1605 // TODO(akalin): Move that code here.
1606 }
[email protected]06f92562011-04-29 19:27:311607 } else {
[email protected]06f92562011-04-29 19:27:311608 // TODO(akalin): Replace silent update with a list of enabled
1609 // permissions.
[email protected]6cc7dbae2011-04-29 21:18:331610 const bool kInstallSilently = true;
[email protected]06f92562011-04-29 19:27:311611 if (!pending_extension_manager()->AddFromSync(
1612 id,
1613 extension_sync_data.update_url,
1614 filter,
[email protected]6cc7dbae2011-04-29 21:18:331615 kInstallSilently)) {
[email protected]06f92562011-04-29 19:27:311616 LOG(WARNING) << "Could not add pending extension for " << id;
1617 return;
1618 }
1619 CheckForUpdatesSoon();
[email protected]90310d92011-04-17 07:35:041620 }
[email protected]90310d92011-04-17 07:35:041621}
1622
[email protected]c3cfb012011-04-06 22:07:351623bool ExtensionService::IsIncognitoEnabled(
1624 const std::string& extension_id) const {
1625 // If this is an existing component extension we always allow it to
1626 // work in incognito mode.
[email protected]81b14cc2011-04-29 00:39:371627 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351628 if (extension && extension->location() == Extension::COMPONENT)
[email protected]cb0ce1e022010-03-10 19:54:411629 return true;
1630
1631 // Check the prefs.
[email protected]c3cfb012011-04-06 22:07:351632 return extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]db7331a2010-02-25 22:10:501633}
[email protected]55a35692010-02-11 23:25:211634
[email protected]c3cfb012011-04-06 22:07:351635void ExtensionService::SetIsIncognitoEnabled(
1636 const std::string& extension_id, bool enabled) {
[email protected]81b14cc2011-04-29 00:39:371637 const Extension* extension = GetInstalledExtension(extension_id);
[email protected]c3cfb012011-04-06 22:07:351638 if (extension && extension->location() == Extension::COMPONENT) {
1639 // This shouldn't be called for component extensions.
1640 NOTREACHED();
1641 return;
1642 }
1643
[email protected]8c6c8c22011-03-09 12:52:341644 // Broadcast unloaded and loaded events to update browser state. Only bother
1645 // if the value changed and the extension is actually enabled, since there is
1646 // no UI otherwise.
[email protected]c3cfb012011-04-06 22:07:351647 bool old_enabled = extension_prefs_->IsIncognitoEnabled(extension_id);
[email protected]8c6c8c22011-03-09 12:52:341648 if (enabled == old_enabled)
1649 return;
1650
[email protected]c3cfb012011-04-06 22:07:351651 extension_prefs_->SetIsIncognitoEnabled(extension_id, enabled);
[email protected]81b14cc2011-04-29 00:39:371652
1653 // If the extension is enabled (and not terminated), unload and
1654 // reload it to update UI.
1655 const Extension* enabled_extension = GetExtensionById(extension_id, false);
1656 if (enabled_extension) {
1657 NotifyExtensionUnloaded(enabled_extension, UnloadedExtensionInfo::DISABLE);
1658 NotifyExtensionLoaded(enabled_extension);
[email protected]568f33d2010-08-04 17:06:411659 }
[email protected]55a35692010-02-11 23:25:211660}
1661
[email protected]eaa7dd182010-12-14 11:09:001662bool ExtensionService::CanCrossIncognito(const Extension* extension) {
[email protected]2a8f24e2010-11-03 21:37:051663 // We allow the extension to see events and data from another profile iff it
1664 // uses "spanning" behavior and it has incognito access. "split" mode
1665 // extensions only see events for a matching profile.
[email protected]c3cfb012011-04-06 22:07:351666 return IsIncognitoEnabled(extension->id()) &&
1667 !extension->incognito_split_mode();
[email protected]2a8f24e2010-11-03 21:37:051668}
1669
[email protected]5ef7b002011-05-12 23:16:201670bool ExtensionService::CanLoadInIncognito(const Extension* extension) const {
1671 if (extension->is_hosted_app())
1672 return true;
1673 // Packaged apps and regular extensions need to be enabled specifically for
1674 // incognito (and split mode should be set).
1675 return extension->incognito_split_mode() &&
1676 IsIncognitoEnabled(extension->id());
1677}
1678
[email protected]eaa7dd182010-12-14 11:09:001679bool ExtensionService::AllowFileAccess(const Extension* extension) {
[email protected]05c82182010-06-24 17:49:081680 return (CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]334e04a2010-06-24 23:34:441681 switches::kDisableExtensionsFileAccessCheck) ||
[email protected]05c82182010-06-24 17:49:081682 extension_prefs_->AllowFileAccess(extension->id()));
1683}
1684
[email protected]eaa7dd182010-12-14 11:09:001685void ExtensionService::SetAllowFileAccess(const Extension* extension,
[email protected]3aff9ad2011-04-01 20:26:481686 bool allow) {
1687 // Reload to update browser state. Only bother if the value changed and the
1688 // extension is actually enabled, since there is no UI otherwise.
1689 bool old_allow = AllowFileAccess(extension);
1690 if (allow == old_allow)
1691 return;
1692
[email protected]05c82182010-06-24 17:49:081693 extension_prefs_->SetAllowFileAccess(extension->id(), allow);
[email protected]3aff9ad2011-04-01 20:26:481694
1695 bool extension_is_enabled = std::find(extensions_.begin(), extensions_.end(),
1696 extension) != extensions_.end();
1697 if (extension_is_enabled)
1698 ReloadExtension(extension->id());
[email protected]05c82182010-06-24 17:49:081699}
1700
[email protected]eaa7dd182010-12-14 11:09:001701bool ExtensionService::GetBrowserActionVisibility(const Extension* extension) {
[email protected]f681c782010-11-19 11:19:391702 return extension_prefs_->GetBrowserActionVisibility(extension);
1703}
1704
[email protected]eaa7dd182010-12-14 11:09:001705void ExtensionService::SetBrowserActionVisibility(const Extension* extension,
[email protected]8e4560b62011-01-14 10:09:141706 bool visible) {
[email protected]f681c782010-11-19 11:19:391707 extension_prefs_->SetBrowserActionVisibility(extension, visible);
1708}
1709
[email protected]8e4560b62011-01-14 10:09:141710// Some extensions will autoupdate themselves externally from Chrome. These
1711// are typically part of some larger client application package. To support
1712// these, the extension will register its location in the the preferences file
1713// (and also, on Windows, in the registry) and this code will periodically
1714// check that location for a .crx file, which it will then install locally if
1715// a new version is available.
1716// Errors are reported through ExtensionErrorReporter. Succcess is not
1717// reported.
[email protected]eaa7dd182010-12-14 11:09:001718void ExtensionService::CheckForExternalUpdates() {
[email protected]a29a517a2011-01-21 21:11:121719 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8e4560b62011-01-14 10:09:141720
1721 // Note that this installation is intentionally silent (since it didn't
1722 // go through the front-end). Extensions that are registered in this
1723 // way are effectively considered 'pre-bundled', and so implicitly
1724 // trusted. In general, if something has HKLM or filesystem access,
1725 // they could install an extension manually themselves anyway.
[email protected]a29a517a2011-01-21 21:11:121726
1727 // If any external extension records give a URL, a provider will set
1728 // this to true. Used by OnExternalProviderReady() to see if we need
1729 // to start an update check to fetch a new external extension.
1730 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141731
1732 // Ask each external extension provider to give us a call back for each
1733 // extension they know about. See OnExternalExtension(File|UpdateUrl)Found.
1734 ProviderCollection::const_iterator i;
1735 for (i = external_extension_providers_.begin();
1736 i != external_extension_providers_.end(); ++i) {
1737 ExternalExtensionProviderInterface* provider = i->get();
1738 provider->VisitRegisteredExtension();
1739 }
1740
1741 // Uninstall of unclaimed extensions will happen after all the providers
[email protected]a29a517a2011-01-21 21:11:121742 // had reported ready. Every provider calls OnExternalProviderReady()
1743 // when it finishes, and OnExternalProviderReady() only acts when all
1744 // providers are ready. In case there are no providers, we call it
1745 // to trigger removal of extensions that used to have an external source.
1746 if (external_extension_providers_.empty())
1747 OnExternalProviderReady();
[email protected]9f1087e2009-06-15 17:29:321748}
1749
[email protected]8e4560b62011-01-14 10:09:141750void ExtensionService::OnExternalProviderReady() {
[email protected]a29a517a2011-01-21 21:11:121751 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1752
1753 // An external provider has finished loading. We only take action
[email protected]8e4560b62011-01-14 10:09:141754 // if all of them are finished. So we check them first.
1755 ProviderCollection::const_iterator i;
1756 for (i = external_extension_providers_.begin();
1757 i != external_extension_providers_.end(); ++i) {
1758 ExternalExtensionProviderInterface* provider = i->get();
[email protected]a29a517a2011-01-21 21:11:121759 if (!provider->IsReady())
[email protected]8e4560b62011-01-14 10:09:141760 return;
[email protected]8e4560b62011-01-14 10:09:141761 }
1762
[email protected]a29a517a2011-01-21 21:11:121763 // All the providers are ready. Install any pending extensions.
1764 if (external_extension_url_added_ && updater()) {
1765 external_extension_url_added_ = false;
[email protected]8e4560b62011-01-14 10:09:141766 updater()->CheckNow();
1767 }
1768
1769 // Uninstall all the unclaimed extensions.
1770 scoped_ptr<ExtensionPrefs::ExtensionsInfo> extensions_info(
1771 extension_prefs_->GetInstalledExtensionsInfo());
1772 for (size_t i = 0; i < extensions_info->size(); ++i) {
1773 ExtensionInfo* info = extensions_info->at(i).get();
1774 if (Extension::IsExternalLocation(info->extension_location))
1775 CheckExternalUninstall(info->extension_id);
1776 }
[email protected]6c751e72010-11-23 10:11:101777}
1778
[email protected]a9f39a312010-12-23 22:14:271779void ExtensionService::UnloadExtension(
1780 const std::string& extension_id,
1781 UnloadedExtensionInfo::Reason reason) {
[email protected]27e469a2010-01-11 20:35:091782 // Make sure the extension gets deleted after we return from this function.
[email protected]9adb9692010-10-29 23:14:021783 scoped_refptr<const Extension> extension(
[email protected]8001df22011-04-28 19:59:471784 GetExtensionByIdInternal(extension_id, true, true, false));
[email protected]631cf822009-05-15 07:01:251785
[email protected]fa0c96732010-11-17 00:14:231786 // This method can be called via PostTask, so the extension may have been
1787 // unloaded by the time this runs.
[email protected]dd163fb02011-05-04 22:22:171788 if (!extension) {
1789 // In case the extension may have crashed/uninstalled. Allow the profile to
1790 // clean up its RequestContexts.
1791 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]fa0c96732010-11-17 00:14:231792 return;
[email protected]dd163fb02011-05-04 22:22:171793 }
[email protected]0c6da502009-08-14 22:32:391794
[email protected]1eb175082010-02-10 09:26:161795 // Keep information about the extension so that we can reload it later
1796 // even if it's not permanently installed.
1797 unloaded_extension_paths_[extension->id()] = extension->path();
1798
[email protected]f17dbd42010-08-16 23:21:101799 // Clean up if the extension is meant to be enabled after a reload.
1800 disabled_extension_paths_.erase(extension->id());
1801
[email protected]d7e9a862010-11-03 21:57:491802 // Clean up runtime data.
1803 extension_runtime_data_.erase(extension_id);
1804
[email protected]0c6da502009-08-14 22:32:391805 ExtensionList::iterator iter = std::find(disabled_extensions_.begin(),
1806 disabled_extensions_.end(),
1807 extension.get());
1808 if (iter != disabled_extensions_.end()) {
[email protected]a9f39a312010-12-23 22:14:271809 UnloadedExtensionInfo details(extension, reason);
1810 details.already_disabled = true;
[email protected]0c6da502009-08-14 22:32:391811 disabled_extensions_.erase(iter);
[email protected]866930682009-08-18 22:53:471812 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271813 chrome::NOTIFICATION_EXTENSION_UNLOADED,
[email protected]24e7a9d2009-11-04 11:11:341814 Source<Profile>(profile_),
[email protected]a9f39a312010-12-23 22:14:271815 Details<UnloadedExtensionInfo>(&details));
[email protected]dd163fb02011-05-04 22:22:171816 // Make sure the profile cleans up its RequestContexts when an already
1817 // disabled extension is unloaded (since they are also tracking the disabled
1818 // extensions).
1819 profile_->UnregisterExtensionWithRequestContexts(extension_id, reason);
[email protected]0c6da502009-08-14 22:32:391820 return;
1821 }
1822
1823 iter = std::find(extensions_.begin(), extensions_.end(), extension.get());
[email protected]894bb502009-05-21 22:39:571824
[email protected]631cf822009-05-15 07:01:251825 // Remove the extension from our list.
1826 extensions_.erase(iter);
1827
[email protected]a9f39a312010-12-23 22:14:271828 NotifyExtensionUnloaded(extension.get(), reason);
[email protected]631cf822009-05-15 07:01:251829}
1830
[email protected]eaa7dd182010-12-14 11:09:001831void ExtensionService::UnloadAllExtensions() {
[email protected]c8d407e2011-04-28 21:27:171832 profile_->GetExtensionSpecialStoragePolicy()->
1833 RevokeRightsForAllExtensions();
1834
[email protected]9f1087e2009-06-15 17:29:321835 extensions_.clear();
[email protected]cd500f72010-06-25 23:44:321836 disabled_extensions_.clear();
[email protected]bb7f40952011-01-13 00:21:201837 terminated_extension_ids_.clear();
1838 terminated_extensions_.clear();
[email protected]d7e9a862010-11-03 21:57:491839 extension_runtime_data_.clear();
[email protected]cd500f72010-06-25 23:44:321840
[email protected]c6e4a3412009-06-24 15:45:291841 // TODO(erikkay) should there be a notification for this? We can't use
1842 // EXTENSION_UNLOADED since that implies that the extension has been disabled
1843 // or uninstalled, and UnloadAll is just part of shutdown.
[email protected]9f1087e2009-06-15 17:29:321844}
1845
[email protected]eaa7dd182010-12-14 11:09:001846void ExtensionService::ReloadExtensions() {
[email protected]9f1087e2009-06-15 17:29:321847 UnloadAllExtensions();
1848 LoadAllExtensions();
1849}
1850
[email protected]eaa7dd182010-12-14 11:09:001851void ExtensionService::GarbageCollectExtensions() {
[email protected]39d9f62c2010-12-03 10:48:501852 if (extension_prefs_->pref_service()->ReadOnly())
[email protected]ba399672010-04-06 15:42:391853 return;
1854
[email protected]ca3dbf52010-05-19 22:27:061855 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
1856 extension_prefs_->GetInstalledExtensionsInfo());
1857
1858 std::map<std::string, FilePath> extension_paths;
1859 for (size_t i = 0; i < info->size(); ++i)
1860 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
1861
[email protected]14908b72011-04-20 06:54:361862 if (!BrowserThread::PostTask(
1863 BrowserThread::FILE, FROM_HERE,
1864 NewRunnableFunction(
1865 &extension_file_util::GarbageCollectExtensions,
1866 install_directory_,
1867 extension_paths)))
1868 NOTREACHED();
[email protected]f6ccd652010-11-17 00:48:341869
1870 // Also garbage-collect themes. We check |profile_| to be
1871 // defensive; in the future, we may call GarbageCollectExtensions()
1872 // from somewhere other than Init() (e.g., in a timer).
1873 if (profile_) {
[email protected]18280372011-03-22 18:05:221874 ThemeServiceFactory::GetForProfile(profile_)->RemoveUnusedThemes();
[email protected]f6ccd652010-11-17 00:48:341875 }
[email protected]3cf4f0992009-02-03 23:00:301876}
1877
[email protected]eaa7dd182010-12-14 11:09:001878void ExtensionService::OnLoadedInstalledExtensions() {
[email protected]93fd78f42009-07-10 16:43:171879 if (updater_.get()) {
1880 updater_->Start();
1881 }
[email protected]fa6a9102010-11-22 15:38:501882
1883 ready_ = true;
[email protected]e72e8eb82009-06-18 17:21:511884 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271885 chrome::NOTIFICATION_EXTENSIONS_READY,
[email protected]24e7a9d2009-11-04 11:11:341886 Source<Profile>(profile_),
[email protected]e72e8eb82009-06-18 17:21:511887 NotificationService::NoDetails());
1888}
1889
[email protected]49cd1572011-02-08 21:38:451890void ExtensionService::AddExtension(const Extension* extension) {
[email protected]ae09ca62009-08-21 19:46:461891 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:021892 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]9f1087e2009-06-15 17:29:321893
[email protected]c8d407e2011-04-28 21:27:171894 // TODO(jstritar): We may be able to get rid of this branch by overriding the
1895 // default extension state to DISABLED when the --disable-extensions flag
1896 // is set (http://crbug.com/29067).
1897 if (!extensions_enabled() &&
1898 !extension->is_theme() &&
1899 extension->location() != Extension::COMPONENT &&
1900 !Extension::IsExternalLocation(extension->location()))
1901 return;
1902
1903 SetBeingUpgraded(extension, false);
1904
[email protected]1eb175082010-02-10 09:26:161905 // The extension is now loaded, remove its data from unloaded extension map.
1906 unloaded_extension_paths_.erase(extension->id());
1907
[email protected]bb7f40952011-01-13 00:21:201908 // If a terminated extension is loaded, remove it from the terminated list.
1909 UntrackTerminatedExtension(extension->id());
1910
[email protected]f17dbd42010-08-16 23:21:101911 // If the extension was disabled for a reload, then enable it.
1912 if (disabled_extension_paths_.erase(extension->id()) > 0)
1913 EnableExtension(extension->id());
1914
[email protected]d728e002010-12-08 04:46:231915 // Check if the extension's privileges have changed and disable the
1916 // extension if necessary.
[email protected]8d888c12010-11-30 00:00:251917 DisableIfPrivilegeIncrease(extension);
[email protected]0c6da502009-08-14 22:32:391918
[email protected]36429da2011-07-11 20:25:181919 bool disabled = Extension::UserMayDisable(extension->location()) &&
1920 extension_prefs_->GetExtensionState(extension->id()) ==
1921 Extension::DISABLED;
1922 if (disabled) {
[email protected]c8d407e2011-04-28 21:27:171923 disabled_extensions_.push_back(scoped_extension);
1924 // TODO(aa): This seems dodgy. It seems that AddExtension() could get called
1925 // with a disabled extension for other reasons other than that an update was
1926 // disabled.
1927 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:271928 chrome::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
[email protected]c8d407e2011-04-28 21:27:171929 Source<Profile>(profile_),
1930 Details<const Extension>(extension));
1931 return;
[email protected]e72e8eb82009-06-18 17:21:511932 }
[email protected]aab98a52009-12-02 03:22:351933
[email protected]c8d407e2011-04-28 21:27:171934 extensions_.push_back(scoped_extension);
1935 NotifyExtensionLoaded(extension);
[email protected]aab98a52009-12-02 03:22:351936}
1937
[email protected]eaa7dd182010-12-14 11:09:001938void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) {
[email protected]8d888c12010-11-30 00:00:251939 // We keep track of all permissions the user has granted each extension.
1940 // This allows extensions to gracefully support backwards compatibility
1941 // by including unknown permissions in their manifests. When the user
1942 // installs the extension, only the recognized permissions are recorded.
1943 // When the unknown permissions become recognized (e.g., through browser
1944 // upgrade), we can prompt the user to accept these new permissions.
1945 // Extensions can also silently upgrade to less permissions, and then
1946 // silently upgrade to a version that adds these permissions back.
1947 //
1948 // For example, pretend that Chrome 10 includes a permission "omnibox"
1949 // for an API that adds suggestions to the omnibox. An extension can
1950 // maintain backwards compatibility while still having "omnibox" in the
1951 // manifest. If a user installs the extension on Chrome 9, the browser
1952 // will record the permissions it recognized, not including "omnibox."
1953 // When upgrading to Chrome 10, "omnibox" will be recognized and Chrome
1954 // will disable the extension and prompt the user to approve the increase
1955 // in privileges. The extension could then release a new version that
1956 // removes the "omnibox" permission. When the user upgrades, Chrome will
1957 // still remember that "omnibox" had been granted, so that if the
1958 // extension once again includes "omnibox" in an upgrade, the extension
1959 // can upgrade without requiring this user's approval.
1960 const Extension* old = GetExtensionByIdInternal(extension->id(),
[email protected]8001df22011-04-28 19:59:471961 true, true, false);
[email protected]8d888c12010-11-30 00:00:251962 bool is_extension_upgrade = old != NULL;
1963 bool is_privilege_increase = false;
1964
1965 // We only record the granted permissions for INTERNAL extensions, since
1966 // they can't silently increase privileges.
1967 if (extension->location() == Extension::INTERNAL) {
1968 // Add all the recognized permissions if the granted permissions list
1969 // hasn't been initialized yet.
[email protected]0d3e4a22011-06-23 19:02:521970 scoped_ptr<ExtensionPermissionSet> granted_permissions(
1971 extension_prefs_->GetGrantedPermissions(extension->id()));
1972 CHECK(granted_permissions.get());
[email protected]8d888c12010-11-30 00:00:251973
1974 // Here, we check if an extension's privileges have increased in a manner
1975 // that requires the user's approval. This could occur because the browser
1976 // upgraded and recognized additional privileges, or an extension upgrades
1977 // to a version that requires additional privileges.
[email protected]0d3e4a22011-06-23 19:02:521978 is_privilege_increase =
1979 granted_permissions->HasLessPrivilegesThan(extension->permission_set());
[email protected]8d888c12010-11-30 00:00:251980 }
1981
1982 if (is_extension_upgrade) {
[email protected]66e26872010-12-03 20:07:251983 // Other than for unpacked extensions, CrxInstaller should have guaranteed
1984 // that we aren't downgrading.
1985 if (extension->location() != Extension::LOAD)
1986 CHECK(extension->version()->CompareTo(*(old->version())) >= 0);
[email protected]8d888c12010-11-30 00:00:251987
1988 // Extensions get upgraded if the privileges are allowed to increase or
1989 // the privileges haven't increased.
1990 if (!is_privilege_increase) {
1991 SetBeingUpgraded(old, true);
1992 SetBeingUpgraded(extension, true);
1993 }
1994
1995 // To upgrade an extension in place, unload the old one and
1996 // then load the new one.
[email protected]a9f39a312010-12-23 22:14:271997 UnloadExtension(old->id(), UnloadedExtensionInfo::UPDATE);
[email protected]8d888c12010-11-30 00:00:251998 old = NULL;
1999 }
2000
2001 // Extension has changed permissions significantly. Disable it. A
2002 // notification should be sent by the caller.
2003 if (is_privilege_increase) {
[email protected]fe2dd7742011-04-19 22:52:492004 if (!extension_prefs_->DidExtensionEscalatePermissions(extension->id())) {
2005 RecordPermissionMessagesHistogram(
2006 extension, "Extensions.Permissions_AutoDisable");
2007 }
[email protected]b3317ad2011-04-28 23:46:002008 extension_prefs_->SetExtensionState(extension->id(), Extension::DISABLED);
[email protected]8d888c12010-11-30 00:00:252009 extension_prefs_->SetDidExtensionEscalatePermissions(extension, true);
2010 }
2011}
2012
[email protected]eaa7dd182010-12-14 11:09:002013void ExtensionService::UpdateActiveExtensionsInCrashReporter() {
[email protected]c8865962009-12-16 07:47:392014 std::set<std::string> extension_ids;
[email protected]aab98a52009-12-02 03:22:352015 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]2a500c1b2010-11-05 15:27:342016 if (!extensions_[i]->is_theme() &&
2017 extensions_[i]->location() != Extension::COMPONENT)
[email protected]c8865962009-12-16 07:47:392018 extension_ids.insert(extensions_[i]->id());
[email protected]aab98a52009-12-02 03:22:352019 }
2020
2021 child_process_logging::SetActiveExtensions(extension_ids);
[email protected]6014d672008-12-05 00:38:252022}
2023
[email protected]e49002a2011-07-13 03:40:442024void ExtensionService::OnLoadSingleExtension(const Extension* extension,
2025 bool prompt_for_plugins) {
[email protected]5fb889382011-06-03 00:29:202026 // If this is a new install of an extension with plugins, prompt the user
2027 // first.
[email protected]e49002a2011-07-13 03:40:442028 if (show_extensions_prompts_ && prompt_for_plugins &&
[email protected]5fb889382011-06-03 00:29:202029 !extension->plugins().empty() &&
2030 disabled_extension_paths_.find(extension->id()) ==
2031 disabled_extension_paths_.end()) {
2032 SimpleExtensionLoadPrompt* prompt = new SimpleExtensionLoadPrompt(
2033 profile_, weak_ptr_factory_.GetWeakPtr(), extension);
2034 prompt->ShowPrompt();
2035 return; // continues in SimpleExtensionLoadPrompt::InstallUI*
2036 }
[email protected]8266d662011-07-12 21:53:262037 OnExtensionInstalled(extension, false); // Not from web store.
[email protected]5fb889382011-06-03 00:29:202038}
2039
[email protected]8266d662011-07-12 21:53:262040void ExtensionService::OnExtensionInstalled(
2041 const Extension* extension, bool from_webstore) {
[email protected]a29a517a2011-01-21 21:11:122042 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]a8af9fdb2010-10-28 21:52:202043
[email protected]4416c5a2010-06-26 01:28:572044 // Ensure extension is deleted unless we transfer ownership.
[email protected]9adb9692010-10-29 23:14:022045 scoped_refptr<const Extension> scoped_extension(extension);
[email protected]c3cfb012011-04-06 22:07:352046 const std::string& id = extension->id();
[email protected]6cc7dbae2011-04-29 21:18:332047 bool initial_enable = IsExtensionEnabled(id);
[email protected]b2907fd2011-03-25 16:43:372048
2049 PendingExtensionInfo pending_extension_info;
[email protected]c3cfb012011-04-06 22:07:352050 if (pending_extension_manager()->GetById(id, &pending_extension_info)) {
2051 pending_extension_manager()->Remove(id);
[email protected]8ef78fd2010-08-19 17:14:322052
[email protected]483d1ff2011-03-03 17:12:022053 if (!pending_extension_info.ShouldAllowInstall(*extension)) {
[email protected]4416c5a2010-06-26 01:28:572054 LOG(WARNING)
[email protected]ae6cb3022011-04-04 20:11:542055 << "ShouldAllowInstall() returned false for "
[email protected]c3cfb012011-04-06 22:07:352056 << id << " of type " << extension->GetType()
[email protected]7fa19f82010-12-21 19:40:082057 << " and update URL " << extension->update_url().spec()
2058 << "; not installing";
[email protected]ae6cb3022011-04-04 20:11:542059
2060 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272061 chrome::NOTIFICATION_EXTENSION_INSTALL_NOT_ALLOWED,
[email protected]ae6cb3022011-04-04 20:11:542062 Source<Profile>(profile_),
2063 Details<const Extension>(extension));
2064
[email protected]4416c5a2010-06-26 01:28:572065 // Delete the extension directory since we're not going to
2066 // load it.
[email protected]14908b72011-04-20 06:54:362067 if (!BrowserThread::PostTask(
2068 BrowserThread::FILE, FROM_HERE,
2069 NewRunnableFunction(&extension_file_util::DeleteFile,
2070 extension->path(), true)))
2071 NOTREACHED();
[email protected]4416c5a2010-06-26 01:28:572072 return;
2073 }
[email protected]4416c5a2010-06-26 01:28:572074 } else {
[email protected]c3cfb012011-04-06 22:07:352075 // We explicitly want to re-enable an uninstalled external
2076 // extension; if we're here, that means the user is manually
2077 // installing the extension.
[email protected]6cc7dbae2011-04-29 21:18:332078 if (IsExternalExtensionUninstalled(id)) {
2079 initial_enable = true;
2080 }
[email protected]aa142702010-03-26 01:26:332081 }
2082
[email protected]07533022011-06-27 20:42:552083 // Do not record the install histograms for upgrades.
2084 if (!GetExtensionByIdInternal(extension->id(), true, true, false)) {
2085 UMA_HISTOGRAM_ENUMERATION("Extensions.InstallType",
2086 extension->GetType(), 100);
2087 RecordPermissionMessagesHistogram(
2088 extension, "Extensions.Permissions_Install");
2089 }
2090
[email protected]eda3c362010-11-12 08:08:232091 ShownSectionsHandler::OnExtensionInstalled(profile_->GetPrefs(), extension);
[email protected]4416c5a2010-06-26 01:28:572092 extension_prefs_->OnExtensionInstalled(
[email protected]8266d662011-07-12 21:53:262093 extension,
2094 initial_enable ? Extension::ENABLED : Extension::DISABLED,
2095 from_webstore);
[email protected]25b34332009-06-05 21:53:192096
[email protected]3aff9ad2011-04-01 20:26:482097 // Unpacked extensions default to allowing file access, but if that has been
2098 // overridden, don't reset the value.
2099 if (Extension::ShouldAlwaysAllowFileAccess(Extension::LOAD) &&
[email protected]c3cfb012011-04-06 22:07:352100 !extension_prefs_->HasAllowFileAccessSetting(id)) {
2101 extension_prefs_->SetAllowFileAccess(id, true);
[email protected]3aff9ad2011-04-01 20:26:482102 }
[email protected]92a5b1d2010-07-20 00:42:002103
[email protected]a3e61e82011-04-15 20:32:082104 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272105 chrome::NOTIFICATION_EXTENSION_INSTALLED,
[email protected]a3e61e82011-04-15 20:32:082106 Source<Profile>(profile_),
2107 Details<const Extension>(extension));
[email protected]7577a5c52009-07-30 06:21:582108
[email protected]49cd1572011-02-08 21:38:452109 // Transfer ownership of |extension| to AddExtension.
2110 AddExtension(scoped_extension);
[email protected]4a190632009-05-09 01:07:422111}
2112
[email protected]eaa7dd182010-12-14 11:09:002113const Extension* ExtensionService::GetExtensionByIdInternal(
[email protected]8001df22011-04-28 19:59:472114 const std::string& id, bool include_enabled, bool include_disabled,
2115 bool include_terminated) const {
[email protected]e957fe52009-06-23 16:51:052116 std::string lowercase_id = StringToLowerASCII(id);
[email protected]0c6da502009-08-14 22:32:392117 if (include_enabled) {
2118 for (ExtensionList::const_iterator iter = extensions_.begin();
2119 iter != extensions_.end(); ++iter) {
2120 if ((*iter)->id() == lowercase_id)
2121 return *iter;
2122 }
2123 }
2124 if (include_disabled) {
2125 for (ExtensionList::const_iterator iter = disabled_extensions_.begin();
2126 iter != disabled_extensions_.end(); ++iter) {
2127 if ((*iter)->id() == lowercase_id)
2128 return *iter;
2129 }
[email protected]ce5c4502009-05-06 16:46:112130 }
[email protected]8001df22011-04-28 19:59:472131 if (include_terminated) {
2132 for (ExtensionList::const_iterator iter = terminated_extensions_.begin();
2133 iter != terminated_extensions_.end(); ++iter) {
2134 if ((*iter)->id() == lowercase_id)
2135 return *iter;
2136 }
2137 }
[email protected]ce5c4502009-05-06 16:46:112138 return NULL;
2139}
2140
[email protected]bb7f40952011-01-13 00:21:202141void ExtensionService::TrackTerminatedExtension(const Extension* extension) {
2142 if (terminated_extension_ids_.insert(extension->id()).second)
2143 terminated_extensions_.push_back(make_scoped_refptr(extension));
[email protected]fa2416f2011-05-03 08:41:202144
2145 UnloadExtension(extension->id(), UnloadedExtensionInfo::DISABLE);
[email protected]bb7f40952011-01-13 00:21:202146}
2147
2148void ExtensionService::UntrackTerminatedExtension(const std::string& id) {
[email protected]fa2416f2011-05-03 08:41:202149 std::string lowercase_id = StringToLowerASCII(id);
2150 if (terminated_extension_ids_.erase(lowercase_id) <= 0)
[email protected]bb7f40952011-01-13 00:21:202151 return;
2152
[email protected]bb7f40952011-01-13 00:21:202153 for (ExtensionList::iterator iter = terminated_extensions_.begin();
2154 iter != terminated_extensions_.end(); ++iter) {
2155 if ((*iter)->id() == lowercase_id) {
2156 terminated_extensions_.erase(iter);
2157 return;
2158 }
2159 }
2160}
2161
[email protected]0dfe05c2011-02-23 23:03:362162const Extension* ExtensionService::GetTerminatedExtension(
[email protected]8001df22011-04-28 19:59:472163 const std::string& id) const {
2164 return GetExtensionByIdInternal(id, false, false, true);
2165}
2166
2167const Extension* ExtensionService::GetInstalledExtension(
2168 const std::string& id) const {
2169 return GetExtensionByIdInternal(id, true, true, true);
[email protected]0dfe05c2011-02-23 23:03:362170}
2171
[email protected]eaa7dd182010-12-14 11:09:002172const Extension* ExtensionService::GetWebStoreApp() {
[email protected]d3071992010-10-08 15:24:072173 return GetExtensionById(extension_misc::kWebStoreAppId, false);
2174}
2175
[email protected]eaa7dd182010-12-14 11:09:002176const Extension* ExtensionService::GetExtensionByURL(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572177 return url.scheme() != chrome::kExtensionScheme ? NULL :
2178 GetExtensionById(url.host(), false);
2179}
2180
[email protected]eaa7dd182010-12-14 11:09:002181const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) {
[email protected]a888b29e62010-04-01 13:38:572182 for (size_t i = 0; i < extensions_.size(); ++i) {
[email protected]cced75a2011-05-20 08:31:122183 if (extensions_[i]->web_extent().MatchesURL(url))
[email protected]a888b29e62010-04-01 13:38:572184 return extensions_[i];
2185 }
2186 return NULL;
[email protected]9f1087e2009-06-15 17:29:322187}
2188
[email protected]eaa7dd182010-12-14 11:09:002189bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) {
[email protected]056ad2a2011-07-12 02:13:552190 // Allow bindings for all packaged extensions.
2191 // Note that GetExtensionByURL may return an Extension for hosted apps
2192 // if the URL came from GetEffectiveURL.
2193 const Extension* extension = GetExtensionByURL(url);
2194 if (extension && extension->GetType() != Extension::TYPE_HOSTED_APP)
[email protected]583d45c12010-08-31 02:48:122195 return true;
2196
2197 // Allow bindings for all component, hosted apps.
[email protected]056ad2a2011-07-12 02:13:552198 if (!extension)
2199 extension = GetExtensionByWebExtent(url);
[email protected]583d45c12010-08-31 02:48:122200 return (extension && extension->location() == Extension::COMPONENT);
2201}
2202
[email protected]eaa7dd182010-12-14 11:09:002203const Extension* ExtensionService::GetExtensionByOverlappingWebExtent(
[email protected]cced75a2011-05-20 08:31:122204 const URLPatternSet& extent) {
[email protected]22c966c2010-06-26 06:35:022205 for (size_t i = 0; i < extensions_.size(); ++i) {
2206 if (extensions_[i]->web_extent().OverlapsWith(extent))
2207 return extensions_[i];
2208 }
2209
[email protected]6d2e60bd2010-06-03 22:37:392210 return NULL;
2211}
2212
[email protected]eaa7dd182010-12-14 11:09:002213const SkBitmap& ExtensionService::GetOmniboxIcon(
[email protected]b671760b2010-07-15 21:13:472214 const std::string& extension_id) {
2215 return omnibox_icon_manager_.GetIcon(extension_id);
2216}
2217
[email protected]eaa7dd182010-12-14 11:09:002218const SkBitmap& ExtensionService::GetOmniboxPopupIcon(
[email protected]29d0d4ac2010-09-08 21:10:312219 const std::string& extension_id) {
2220 return omnibox_popup_icon_manager_.GetIcon(extension_id);
2221}
2222
[email protected]eaa7dd182010-12-14 11:09:002223void ExtensionService::OnExternalExtensionFileFound(
[email protected]8ef78fd2010-08-19 17:14:322224 const std::string& id,
[email protected]8e4560b62011-01-14 10:09:142225 const Version* version,
[email protected]8ef78fd2010-08-19 17:14:322226 const FilePath& path,
2227 Extension::Location location) {
[email protected]ab22ba42011-01-14 16:36:382228 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2229 CHECK(Extension::IdIsValid(id));
[email protected]79c833b52011-04-05 18:31:012230 if (extension_prefs_->IsExternalExtensionUninstalled(id))
[email protected]683d0702010-12-06 16:25:572231 return;
[email protected]a8af9fdb2010-10-28 21:52:202232
[email protected]8e4560b62011-01-14 10:09:142233 DCHECK(version);
2234
[email protected]7577a5c52009-07-30 06:21:582235 // Before even bothering to unpack, check and see if we already have this
[email protected]4c967932009-07-31 01:15:492236 // version. This is important because these extensions are going to get
[email protected]7577a5c52009-07-30 06:21:582237 // installed on every startup.
[email protected]9adb9692010-10-29 23:14:022238 const Extension* existing = GetExtensionById(id, true);
[email protected]7577a5c52009-07-30 06:21:582239 if (existing) {
[email protected]8e4560b62011-01-14 10:09:142240 switch (existing->version()->CompareTo(*version)) {
[email protected]7577a5c52009-07-30 06:21:582241 case -1: // existing version is older, we should upgrade
2242 break;
2243 case 0: // existing version is same, do nothing
2244 return;
2245 case 1: // existing version is newer, uh-oh
2246 LOG(WARNING) << "Found external version of extension " << id
2247 << "that is older than current version. Current version "
2248 << "is: " << existing->VersionString() << ". New version "
2249 << "is: " << version << ". Keeping current version.";
2250 return;
2251 }
2252 }
2253
[email protected]b2907fd2011-03-25 16:43:372254 pending_extension_manager()->AddFromExternalFile(id, location);
[email protected]9c635f22010-12-02 09:36:362255
[email protected]14908b72011-04-20 06:54:362256 // no client (silent install)
2257 scoped_refptr<CrxInstaller> installer(MakeCrxInstaller(NULL));
[email protected]6dfbbf82010-03-12 23:09:162258 installer->set_install_source(location);
2259 installer->set_expected_id(id);
[email protected]cb0e50312011-05-09 15:03:072260 installer->set_expected_version(*version);
2261 installer->set_install_cause(extension_misc::INSTALL_CAUSE_EXTERNAL_FILE);
[email protected]6dfbbf82010-03-12 23:09:162262 installer->InstallCrx(path);
[email protected]7577a5c52009-07-30 06:21:582263}
2264
[email protected]eaa7dd182010-12-14 11:09:002265void ExtensionService::ReportExtensionLoadError(
[email protected]d11c8e92009-10-20 23:26:402266 const FilePath& extension_path,
2267 const std::string &error,
[email protected]432115822011-07-10 15:52:272268 int type,
[email protected]d11c8e92009-10-20 23:26:402269 bool be_noisy) {
2270 NotificationService* service = NotificationService::current();
2271 service->Notify(type,
[email protected]24e7a9d2009-11-04 11:11:342272 Source<Profile>(profile_),
[email protected]d11c8e92009-10-20 23:26:402273 Details<const std::string>(&error));
2274
[email protected]8a205c02011-02-04 20:41:332275 std::string path_str = UTF16ToUTF8(extension_path.LossyDisplayName());
[email protected]18d4b6c2010-09-21 03:21:042276 std::string message = base::StringPrintf(
2277 "Could not load extension from '%s'. %s",
2278 path_str.c_str(), error.c_str());
[email protected]d11c8e92009-10-20 23:26:402279 ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy);
2280}
2281
[email protected]eaa7dd182010-12-14 11:09:002282void ExtensionService::DidCreateRenderViewForBackgroundPage(
[email protected]406027c02010-09-27 08:03:182283 ExtensionHost* host) {
2284 OrphanedDevTools::iterator iter =
[email protected]2d2f6cfc2011-05-06 21:09:332285 orphaned_dev_tools_.find(host->extension_id());
[email protected]406027c02010-09-27 08:03:182286 if (iter == orphaned_dev_tools_.end())
2287 return;
2288
2289 DevToolsManager::GetInstance()->AttachClientHost(
2290 iter->second, host->render_view_host());
2291 orphaned_dev_tools_.erase(iter);
2292}
2293
[email protected]432115822011-07-10 15:52:272294void ExtensionService::Observe(int type,
[email protected]fa2416f2011-05-03 08:41:202295 const NotificationSource& source,
2296 const NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:272297 switch (type) {
2298 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: {
[email protected]bc535ee52010-08-31 18:40:322299 if (profile_ != Source<Profile>(source).ptr()->GetOriginalProfile())
2300 break;
[email protected]a4ed6282009-12-14 20:51:162301
[email protected]f128af42010-08-05 18:05:262302 ExtensionHost* host = Details<ExtensionHost>(details).ptr();
2303
[email protected]fa2416f2011-05-03 08:41:202304 // Mark the extension as terminated and Unload it. We want it to
2305 // be in a consistent state: either fully working or not loaded
2306 // at all, but never half-crashed. We do it in a PostTask so
2307 // that other handlers of this notification will still have
2308 // access to the Extension and ExtensionHost.
[email protected]14908b72011-04-20 06:54:362309 MessageLoop::current()->PostTask(
2310 FROM_HERE,
2311 method_factory_.NewRunnableMethod(
[email protected]fa2416f2011-05-03 08:41:202312 &ExtensionService::TrackTerminatedExtension,
2313 host->extension()));
[email protected]31f77262009-12-02 20:48:532314 break;
2315 }
[email protected]432115822011-07-10 15:52:272316 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: {
[email protected]77a6970c2011-04-23 16:58:562317 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
2318 // Valid extension function names, used to setup bindings in renderer.
2319 std::vector<std::string> function_names;
2320 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names);
2321 process->Send(new ExtensionMsg_SetFunctionNames(function_names));
[email protected]31f77262009-12-02 20:48:532322
[email protected]77a6970c2011-04-23 16:58:562323 // Scripting whitelist. This is modified by tests and must be communicated
2324 // to renderers.
2325 process->Send(new ExtensionMsg_SetScriptingWhitelist(
2326 *Extension::GetScriptingWhitelist()));
2327
2328 // Loaded extensions.
2329 for (size_t i = 0; i < extensions_.size(); ++i) {
2330 process->Send(new ExtensionMsg_Loaded(
2331 ExtensionMsg_Loaded_Params(extensions_[i])));
2332 }
2333 break;
2334 }
[email protected]432115822011-07-10 15:52:272335 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
[email protected]da5683db2011-04-23 17:12:212336 RenderProcessHost* process = Source<RenderProcessHost>(source).ptr();
2337 installed_app_hosts_.erase(process->id());
2338 break;
2339 }
[email protected]432115822011-07-10 15:52:272340 case chrome::NOTIFICATION_PREF_CHANGED: {
[email protected]aa96d3a2010-08-21 08:45:252341 std::string* pref_name = Details<std::string>(details).ptr();
[email protected]6c751e72010-11-23 10:11:102342 if (*pref_name == prefs::kExtensionInstallAllowList ||
2343 *pref_name == prefs::kExtensionInstallDenyList) {
2344 CheckAdminBlacklist();
[email protected]6c751e72010-11-23 10:11:102345 } else {
2346 NOTREACHED() << "Unexpected preference name.";
2347 }
[email protected]aa96d3a2010-08-21 08:45:252348 break;
2349 }
2350
[email protected]4814b512009-11-07 00:12:292351 default:
2352 NOTREACHED() << "Unexpected notification type.";
2353 }
2354}
2355
[email protected]eaa7dd182010-12-14 11:09:002356bool ExtensionService::HasApps() const {
[email protected]ec5b50d2010-10-09 16:35:182357 return !GetAppIds().empty();
2358}
[email protected]377011d2010-07-20 04:18:502359
[email protected]eaa7dd182010-12-14 11:09:002360ExtensionIdSet ExtensionService::GetAppIds() const {
[email protected]ec5b50d2010-10-09 16:35:182361 ExtensionIdSet result;
[email protected]377011d2010-07-20 04:18:502362 for (ExtensionList::const_iterator it = extensions_.begin();
2363 it != extensions_.end(); ++it) {
[email protected]ec5b50d2010-10-09 16:35:182364 if ((*it)->is_app() && (*it)->location() != Extension::COMPONENT)
2365 result.insert((*it)->id());
[email protected]377011d2010-07-20 04:18:502366 }
2367
[email protected]ec5b50d2010-10-09 16:35:182368 return result;
[email protected]377011d2010-07-20 04:18:502369}
[email protected]d7e9a862010-11-03 21:57:492370
[email protected]14908b72011-04-20 06:54:362371scoped_refptr<CrxInstaller> ExtensionService::MakeCrxInstaller(
2372 ExtensionInstallUI* client) {
2373 return new CrxInstaller(weak_ptr_factory_.GetWeakPtr(), client);
2374}
2375
[email protected]eaa7dd182010-12-14 11:09:002376bool ExtensionService::IsBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492377 return (extension->background_url().is_empty() ||
2378 extension_runtime_data_[extension->id()].background_page_ready);
2379}
2380
[email protected]eaa7dd182010-12-14 11:09:002381void ExtensionService::SetBackgroundPageReady(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492382 DCHECK(!extension->background_url().is_empty());
2383 extension_runtime_data_[extension->id()].background_page_ready = true;
2384 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:272385 chrome::NOTIFICATION_EXTENSION_BACKGROUND_PAGE_READY,
[email protected]d7e9a862010-11-03 21:57:492386 Source<const Extension>(extension),
2387 NotificationService::NoDetails());
2388}
2389
[email protected]eaa7dd182010-12-14 11:09:002390bool ExtensionService::IsBeingUpgraded(const Extension* extension) {
[email protected]d7e9a862010-11-03 21:57:492391 return extension_runtime_data_[extension->id()].being_upgraded;
2392}
2393
[email protected]eaa7dd182010-12-14 11:09:002394void ExtensionService::SetBeingUpgraded(const Extension* extension,
[email protected]d7e9a862010-11-03 21:57:492395 bool value) {
2396 extension_runtime_data_[extension->id()].being_upgraded = value;
2397}
[email protected]1bead0712010-11-27 17:41:532398
[email protected]eaa7dd182010-12-14 11:09:002399PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
[email protected]1bead0712010-11-27 17:41:532400 return &extension_runtime_data_[extension->id()].property_bag;
2401}
[email protected]cebc3dc2011-04-18 17:15:002402
2403void ExtensionService::RegisterNaClModule(const GURL& url,
2404 const std::string& mime_type) {
2405 NaClModuleInfo info;
2406 info.url = url;
2407 info.mime_type = mime_type;
2408
2409 DCHECK(FindNaClModule(url) == nacl_module_list_.end());
2410 nacl_module_list_.push_front(info);
2411}
2412
2413void ExtensionService::UnregisterNaClModule(const GURL& url) {
2414 NaClModuleInfoList::iterator iter = FindNaClModule(url);
2415 DCHECK(iter != nacl_module_list_.end());
2416 nacl_module_list_.erase(iter);
2417}
2418
[email protected]ed0ba002011-05-26 16:55:132419void ExtensionService::UpdatePluginListWithNaClModules() {
[email protected]300c0ea2011-07-15 23:04:332420 // An extension has been added which has a nacl_module component, which means
2421 // there is a MIME type that module wants to handle, so we need to add that
2422 // MIME type to plugins which handle NaCl modules in order to allow the
2423 // individual modules to handle these types.
[email protected]ed0ba002011-05-26 16:55:132424
[email protected]300c0ea2011-07-15 23:04:332425 DCHECK(nacl_module_list_.size() <= 1);
[email protected]ed0ba002011-05-26 16:55:132426
2427 const PepperPluginInfo* pepper_info = NULL;
2428 std::vector<PepperPluginInfo> plugins;
2429 PepperPluginRegistry::ComputeList(&plugins);
[email protected]300c0ea2011-07-15 23:04:332430
2431 // Search the entire plugin list for plugins that handle the NaCl MIME type.
2432 // There can be multiple plugins like this, for instance the internal NaCl
2433 // plugin and a plugin registered by --register-pepper-plugins during tests.
[email protected]ed0ba002011-05-26 16:55:132434 for (size_t i = 0; i < plugins.size(); ++i) {
[email protected]300c0ea2011-07-15 23:04:332435 pepper_info = &plugins[i];
2436 CHECK(pepper_info);
2437 std::vector<webkit::npapi::WebPluginMimeType>::const_iterator mime_iter;
2438 // Check each MIME type the plugins handle for the NaCl MIME type.
2439 for (mime_iter = pepper_info->mime_types.begin();
2440 mime_iter != pepper_info->mime_types.end(); ++mime_iter) {
2441 if (mime_iter->mime_type == kNaClPluginMimeType) {
2442 // This plugin handles "application/x-nacl".
2443
2444 webkit::npapi::PluginList::Singleton()->
2445 UnregisterInternalPlugin(pepper_info->path);
2446
2447 webkit::npapi::WebPluginInfo info = pepper_info->ToWebPluginInfo();
2448
2449 for (ExtensionService::NaClModuleInfoList::const_iterator iter =
2450 nacl_module_list_.begin();
2451 iter != nacl_module_list_.end(); ++iter) {
2452 // Add the MIME type specified in the extension to this NaCl plugin,
2453 // With an extra "nacl" argument to specify the location of the NaCl
2454 // manifest file.
2455 webkit::npapi::WebPluginMimeType mime_type_info;
2456 mime_type_info.mime_type = iter->mime_type;
2457 mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
2458 mime_type_info.additional_param_values.push_back(
2459 UTF8ToUTF16(iter->url.spec()));
2460 info.mime_types.push_back(mime_type_info);
2461 }
2462
2463 webkit::npapi::PluginList::Singleton()->RefreshPlugins();
2464 webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info);
2465 // This plugin has been modified, no need to check the rest of its
2466 // types, but continue checking other plugins.
2467 break;
2468 }
[email protected]ed0ba002011-05-26 16:55:132469 }
2470 }
[email protected]ed0ba002011-05-26 16:55:132471}
2472
[email protected]cebc3dc2011-04-18 17:15:002473ExtensionService::NaClModuleInfoList::iterator
2474 ExtensionService::FindNaClModule(const GURL& url) {
2475 for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
2476 iter != nacl_module_list_.end(); ++iter) {
2477 if (iter->url == url)
2478 return iter;
2479 }
2480 return nacl_module_list_.end();
2481}