[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/extensions_service.h" |
| 6 | |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 7 | #include "base/basictypes.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 10 | #include "base/histogram.h" |
[email protected] | cd500f7 | 2010-06-25 23:44:32 | [diff] [blame] | 11 | #include "base/stl_util-inl.h" |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 12 | #include "base/string16.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 13 | #include "base/string_number_conversions.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 14 | #include "base/string_util.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 15 | #include "base/time.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 16 | #include "base/values.h" |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 17 | #include "base/version.h" |
[email protected] | 15730c4 | 2009-09-03 00:03:20 | [diff] [blame] | 18 | #include "chrome/browser/browser_process.h" |
[email protected] | dbb92e0d | 2009-08-20 16:18:21 | [diff] [blame] | 19 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 20 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 21 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_accessibility_api.h" |
[email protected] | 840b0db | 2009-11-20 03:00:38 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_bookmarks_module.h" |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 24 | #include "chrome/browser/extensions/extension_browser_event_router.h" |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 25 | #include "chrome/browser/extensions/extension_cookies_api.h" |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 26 | #include "chrome/browser/extensions/extension_data_deleter.h" |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 27 | #include "chrome/browser/extensions/extension_dom_ui.h" |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 28 | #include "chrome/browser/extensions/extension_error_reporter.h" |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 29 | #include "chrome/browser/extensions/extension_history_api.h" |
[email protected] | b1748b1d8 | 2009-11-30 20:32:56 | [diff] [blame] | 30 | #include "chrome/browser/extensions/extension_host.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 31 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 32 | #include "chrome/browser/extensions/extension_updater.h" |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 33 | #include "chrome/browser/extensions/external_extension_provider.h" |
| 34 | #include "chrome/browser/extensions/external_pref_extension_provider.h" |
[email protected] | 56ad379 | 2010-05-28 17:45:33 | [diff] [blame] | 35 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | 052313b | 2010-02-19 09:43:08 | [diff] [blame] | 36 | #include "chrome/browser/pref_service.h" |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 37 | #include "chrome/browser/profile.h" |
[email protected] | 56ad379 | 2010-05-28 17:45:33 | [diff] [blame] | 38 | #include "chrome/browser/search_engines/template_url_model.h" |
[email protected] | 11edd1e | 2010-07-21 00:14:50 | [diff] [blame] | 39 | #include "chrome/browser/sync/glue/extension_util.h" |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 40 | #include "chrome/common/child_process_logging.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 41 | #include "chrome/common/chrome_switches.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 42 | #include "chrome/common/extensions/extension.h" |
[email protected] | d7b36dc | 2009-10-29 21:47:40 | [diff] [blame] | 43 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 44 | #include "chrome/common/extensions/extension_error_utils.h" |
[email protected] | 7c927b6 | 2010-02-24 09:54:13 | [diff] [blame] | 45 | #include "chrome/common/extensions/extension_file_util.h" |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 46 | #include "chrome/common/extensions/extension_l10n_util.h" |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 47 | #include "chrome/common/notification_service.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 48 | #include "chrome/common/notification_type.h" |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 49 | #include "chrome/common/json_value_serializer.h" |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 50 | #include "chrome/common/pref_names.h" |
[email protected] | a5720987 | 2009-05-04 22:53:14 | [diff] [blame] | 51 | #include "chrome/common/url_constants.h" |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 52 | #include "googleurl/src/gurl.h" |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 53 | #include "webkit/database/database_tracker.h" |
| 54 | #include "webkit/database/database_util.h" |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 55 | |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 56 | #if defined(OS_WIN) |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 57 | #include "chrome/browser/extensions/external_registry_extension_provider_win.h" |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 58 | #endif |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 59 | |
[email protected] | 5ef47ec | 2010-01-28 05:58:05 | [diff] [blame] | 60 | using base::Time; |
| 61 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 62 | namespace errors = extension_manifest_errors; |
| 63 | |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 64 | namespace { |
| 65 | |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 66 | static bool ShouldReloadExtensionManifest(const ExtensionInfo& info) { |
| 67 | // Always reload LOAD extension manifests, because they can change on disk |
| 68 | // independent of the manifest in our prefs. |
| 69 | if (info.extension_location == Extension::LOAD) |
| 70 | return true; |
| 71 | |
| 72 | // Otherwise, reload the manifest it needs to be relocalized. |
| 73 | return extension_l10n_util::ShouldRelocalizeManifest(info); |
| 74 | } |
| 75 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 76 | } // namespace |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 77 | |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 78 | PendingExtensionInfo::PendingExtensionInfo(const GURL& update_url, |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 79 | bool is_theme, |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 80 | bool install_silently, |
| 81 | bool enable_on_install, |
| 82 | bool enable_incognito_on_install) |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 83 | : update_url(update_url), |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 84 | is_theme(is_theme), |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 85 | install_silently(install_silently), |
| 86 | enable_on_install(enable_on_install), |
| 87 | enable_incognito_on_install(enable_incognito_on_install) {} |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 88 | |
| 89 | PendingExtensionInfo::PendingExtensionInfo() |
| 90 | : update_url(), |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 91 | is_theme(false), |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 92 | install_silently(false), |
| 93 | enable_on_install(false), |
| 94 | enable_incognito_on_install(false) {} |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 95 | |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 96 | // ExtensionsService. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 97 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 98 | const char* ExtensionsService::kInstallDirectoryName = "Extensions"; |
| 99 | const char* ExtensionsService::kCurrentVersionFileName = "Current Version"; |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 100 | |
[email protected] | 334e04a | 2010-06-24 23:34:44 | [diff] [blame] | 101 | bool ExtensionsService::IsGalleryDownloadURL(const GURL& download_url) { |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 102 | if (StartsWithASCII(download_url.spec(), |
[email protected] | 334e04a | 2010-06-24 23:34:44 | [diff] [blame] | 103 | extension_urls::kMiniGalleryDownloadPrefix, false)) |
| 104 | return true; |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 105 | |
[email protected] | 334e04a | 2010-06-24 23:34:44 | [diff] [blame] | 106 | GURL gallery_download_prefix(extension_urls::kGalleryDownloadPrefix); |
| 107 | if (download_url.host() == gallery_download_prefix.host() && |
| 108 | StartsWithASCII(download_url.path(), |
| 109 | gallery_download_prefix.path(), false)) |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 110 | return true; |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 111 | |
[email protected] | 473ff6e | 2010-05-12 15:31:55 | [diff] [blame] | 112 | // Allow command line gallery url to be referrer for the gallery downloads. |
| 113 | std::string command_line_gallery_url = |
| 114 | CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
[email protected] | 2bce5e1 | 2010-05-13 01:18:07 | [diff] [blame] | 115 | switches::kAppsGalleryURL); |
[email protected] | 473ff6e | 2010-05-12 15:31:55 | [diff] [blame] | 116 | if (!command_line_gallery_url.empty() && |
| 117 | StartsWithASCII(download_url.spec(), |
[email protected] | 334e04a | 2010-06-24 23:34:44 | [diff] [blame] | 118 | extension_urls::kGalleryDownloadPrefix, false)) |
[email protected] | 473ff6e | 2010-05-12 15:31:55 | [diff] [blame] | 119 | return true; |
[email protected] | 334e04a | 2010-06-24 23:34:44 | [diff] [blame] | 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | // static |
| 125 | bool ExtensionsService::IsDownloadFromGallery(const GURL& download_url, |
| 126 | const GURL& referrer_url) { |
| 127 | if (!IsGalleryDownloadURL(download_url)) |
| 128 | return false; |
| 129 | |
| 130 | if (StartsWithASCII(referrer_url.spec(), |
| 131 | extension_urls::kMiniGalleryBrowsePrefix, false)) |
| 132 | return true; |
| 133 | |
| 134 | if (StartsWithASCII(referrer_url.spec(), |
| 135 | Extension::ChromeStoreURL(), false)) |
| 136 | return true; |
| 137 | |
| 138 | // Allow command line gallery url to be referrer for the gallery downloads. |
| 139 | std::string command_line_gallery_url = |
| 140 | CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 141 | switches::kAppsGalleryURL); |
| 142 | if (!command_line_gallery_url.empty() && |
| 143 | StartsWithASCII(referrer_url.spec(), |
| 144 | command_line_gallery_url, false)) |
| 145 | return true; |
[email protected] | 473ff6e | 2010-05-12 15:31:55 | [diff] [blame] | 146 | |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
[email protected] | ac02528 | 2009-12-16 19:16:38 | [diff] [blame] | 150 | bool ExtensionsService::IsDownloadFromMiniGallery(const GURL& download_url) { |
| 151 | return StartsWithASCII(download_url.spec(), |
| 152 | extension_urls::kMiniGalleryDownloadPrefix, |
| 153 | false); // case_sensitive |
| 154 | } |
| 155 | |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 156 | ExtensionsService::ExtensionsService(Profile* profile, |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 157 | const CommandLine* command_line, |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 158 | PrefService* prefs, |
| 159 | const FilePath& install_directory, |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 160 | bool autoupdate_enabled) |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 161 | : profile_(profile), |
| 162 | extension_prefs_(new ExtensionPrefs(prefs, install_directory)), |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 163 | install_directory_(install_directory), |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 164 | extensions_enabled_(true), |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 165 | show_extensions_prompts_(true), |
[email protected] | e0360f2c | 2009-12-07 22:34:31 | [diff] [blame] | 166 | ready_(false), |
| 167 | ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)) { |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 168 | // Figure out if extension installation should be enabled. |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 169 | if (command_line->HasSwitch(switches::kDisableExtensions)) { |
| 170 | extensions_enabled_ = false; |
| 171 | } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) { |
| 172 | extensions_enabled_ = false; |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 173 | } |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 174 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 175 | registrar_.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING, |
| 176 | NotificationService::AllSources()); |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 177 | registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED, |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 178 | Source<Profile>(profile_)); |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 179 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 180 | // Set up the ExtensionUpdater |
| 181 | if (autoupdate_enabled) { |
| 182 | int update_frequency = kDefaultUpdateFrequencySeconds; |
| 183 | if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) { |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 184 | base::StringToInt(command_line->GetSwitchValueASCII( |
| 185 | switches::kExtensionsUpdateFrequency), |
| 186 | &update_frequency); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 187 | } |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 188 | updater_ = new ExtensionUpdater(this, prefs, update_frequency); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 191 | backend_ = new ExtensionsServiceBackend(install_directory_); |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 192 | |
| 193 | // Use monochrome icons for omnibox icons. |
| 194 | omnibox_icon_manager_.set_monochrome(true); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | ExtensionsService::~ExtensionsService() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 198 | UnloadAllExtensions(); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 199 | if (updater_.get()) { |
| 200 | updater_->Stop(); |
| 201 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | c5ae74ab | 2010-04-15 18:14:37 | [diff] [blame] | 204 | void ExtensionsService::InitEventRouters() { |
| 205 | ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_); |
| 206 | ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_); |
[email protected] | 56ee015 | 2010-06-16 01:54:42 | [diff] [blame] | 207 | ExtensionBrowserEventRouter::GetInstance()->Init(profile_); |
[email protected] | c5ae74ab | 2010-04-15 18:14:37 | [diff] [blame] | 208 | ExtensionBookmarkEventRouter::GetSingleton()->Observe( |
| 209 | profile_->GetBookmarkModel()); |
[email protected] | 2c5e1e1 | 2010-06-10 13:14:44 | [diff] [blame] | 210 | ExtensionCookiesEventRouter::GetInstance()->Init(); |
[email protected] | c5ae74ab | 2010-04-15 18:14:37 | [diff] [blame] | 211 | } |
| 212 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 213 | void ExtensionsService::Init() { |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 214 | DCHECK(!ready_); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 215 | DCHECK_EQ(extensions_.size(), 0u); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 216 | |
[email protected] | 95dd38f | 2009-10-20 20:09:15 | [diff] [blame] | 217 | // Hack: we need to ensure the ResourceDispatcherHost is ready before we load |
| 218 | // the first extension, because its members listen for loaded notifications. |
| 219 | g_browser_process->resource_dispatcher_host(); |
| 220 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 221 | LoadAllExtensions(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 222 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 223 | // TODO(erikkay) this should probably be deferred to a future point |
| 224 | // rather than running immediately at startup. |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 225 | CheckForExternalUpdates(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 226 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 227 | // TODO(erikkay) this should probably be deferred as well. |
| 228 | GarbageCollectExtensions(); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 229 | } |
| 230 | |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 231 | void ExtensionsService::InstallExtension(const FilePath& extension_path) { |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 232 | scoped_refptr<CrxInstaller> installer( |
| 233 | new CrxInstaller(install_directory_, |
| 234 | this, // frontend |
| 235 | NULL)); // no client (silent install) |
| 236 | installer->set_allow_privilege_increase(true); |
| 237 | installer->InstallCrx(extension_path); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 240 | namespace { |
| 241 | // TODO(akalin): Put this somewhere where both crx_installer.cc and |
| 242 | // this file can use it. |
| 243 | void DeleteFileHelper(const FilePath& path, bool recursive) { |
| 244 | DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 245 | file_util::Delete(path, recursive); |
| 246 | } |
| 247 | } // namespace |
| 248 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 249 | void ExtensionsService::UpdateExtension(const std::string& id, |
[email protected] | 5c851620 | 2010-03-18 21:43:34 | [diff] [blame] | 250 | const FilePath& extension_path, |
| 251 | const GURL& download_url) { |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 252 | PendingExtensionMap::const_iterator it = pending_extensions_.find(id); |
| 253 | if ((it == pending_extensions_.end()) && |
| 254 | !GetExtensionByIdInternal(id, true, true)) { |
| 255 | LOG(WARNING) << "Will not update extension " << id |
| 256 | << " because it is not installed or pending"; |
| 257 | // Delete extension_path since we're not creating a CrxInstaller |
| 258 | // that would do it for us. |
| 259 | ChromeThread::PostTask( |
| 260 | ChromeThread::FILE, FROM_HERE, |
| 261 | NewRunnableFunction(&DeleteFileHelper, extension_path, false)); |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 262 | return; |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 263 | } |
| 264 | |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 265 | // We want a silent install only for non-pending extensions and |
| 266 | // pending extensions that have install_silently set. |
| 267 | ExtensionInstallUI* client = |
| 268 | ((it == pending_extensions_.end()) || it->second.install_silently) ? |
| 269 | NULL : new ExtensionInstallUI(profile_); |
| 270 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 271 | scoped_refptr<CrxInstaller> installer( |
| 272 | new CrxInstaller(install_directory_, |
| 273 | this, // frontend |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 274 | client)); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 275 | installer->set_expected_id(id); |
| 276 | installer->set_delete_source(true); |
[email protected] | 9f72aa0 | 2010-06-25 10:01:05 | [diff] [blame] | 277 | installer->set_limit_web_extent_to_download_host(true); |
[email protected] | 5c851620 | 2010-03-18 21:43:34 | [diff] [blame] | 278 | installer->set_original_url(download_url); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 279 | installer->InstallCrx(extension_path); |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 280 | } |
| 281 | |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 282 | void ExtensionsService::AddPendingExtension( |
| 283 | const std::string& id, const GURL& update_url, |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 284 | bool is_theme, bool install_silently, |
| 285 | bool enable_on_install, bool enable_incognito_on_install) { |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 286 | if (GetExtensionByIdInternal(id, true, true)) { |
[email protected] | efee9f26 | 2010-03-29 21:26:25 | [diff] [blame] | 287 | LOG(DFATAL) << "Trying to add pending extension " << id |
| 288 | << " which already exists"; |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 289 | return; |
| 290 | } |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 291 | AddPendingExtensionInternal( |
| 292 | id, update_url, is_theme, install_silently, |
| 293 | enable_on_install, enable_incognito_on_install); |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void ExtensionsService::AddPendingExtensionInternal( |
| 297 | const std::string& id, const GURL& update_url, |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 298 | bool is_theme, bool install_silently, |
| 299 | bool enable_on_install, bool enable_incognito_on_install) { |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 300 | pending_extensions_[id] = |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 301 | PendingExtensionInfo(update_url, is_theme, install_silently, |
| 302 | enable_on_install, enable_incognito_on_install); |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 305 | void ExtensionsService::ReloadExtension(const std::string& extension_id) { |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 306 | FilePath path; |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 307 | Extension* current_extension = GetExtensionById(extension_id, false); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 308 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 309 | // Unload the extension if it's loaded. It might not be loaded if it crashed. |
| 310 | if (current_extension) { |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 311 | // If the extension has an inspector open for its background page, detach |
| 312 | // the inspector and hang onto a cookie for it, so that we can reattach |
| 313 | // later. |
| 314 | ExtensionProcessManager* manager = profile_->GetExtensionProcessManager(); |
| 315 | ExtensionHost* host = manager->GetBackgroundHostForExtension( |
| 316 | current_extension); |
| 317 | if (host) { |
| 318 | // Look for an open inspector for the background page. |
| 319 | int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost( |
| 320 | host->render_view_host()); |
| 321 | if (devtools_cookie >= 0) |
| 322 | orphaned_dev_tools_[extension_id] = devtools_cookie; |
| 323 | } |
| 324 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 325 | path = current_extension->path(); |
| 326 | UnloadExtension(extension_id); |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 327 | } else { |
| 328 | path = unloaded_extension_paths_[extension_id]; |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | e6090e4 | 2010-03-23 22:44:08 | [diff] [blame] | 331 | // Check the installed extensions to see if what we're reloading was already |
| 332 | // installed. |
| 333 | scoped_ptr<ExtensionInfo> installed_extension( |
| 334 | extension_prefs_->GetInstalledExtensionInfo(extension_id)); |
| 335 | if (installed_extension.get() && |
| 336 | installed_extension->extension_manifest.get()) { |
| 337 | LoadInstalledExtension(*installed_extension, false); |
| 338 | } else { |
| 339 | // We should always be able to remember the extension's path. If it's not in |
| 340 | // the map, someone failed to update |unloaded_extension_paths_|. |
| 341 | CHECK(!path.empty()); |
| 342 | LoadExtension(path); |
| 343 | } |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 346 | void ExtensionsService::UninstallExtension(const std::string& extension_id, |
| 347 | bool external_uninstall) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 348 | Extension* extension = GetExtensionByIdInternal(extension_id, true, true); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 349 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 350 | // Callers should not send us nonexistant extensions. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 351 | DCHECK(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 352 | |
[email protected] | 831aa21 | 2010-03-26 13:55:19 | [diff] [blame] | 353 | // Get hold of information we need after unloading, since the extension |
| 354 | // pointer will be invalid then. |
| 355 | GURL extension_url(extension->url()); |
| 356 | Extension::Location location(extension->location()); |
| 357 | |
| 358 | // Also copy the extension identifier since the reference might have been |
| 359 | // obtained via Extension::id(). |
| 360 | std::string extension_id_copy(extension_id); |
| 361 | |
[email protected] | 56ad379 | 2010-05-28 17:45:33 | [diff] [blame] | 362 | if (profile_->GetTemplateURLModel()) |
| 363 | profile_->GetTemplateURLModel()->UnregisterExtensionKeyword(extension); |
| 364 | |
[email protected] | 831aa21 | 2010-03-26 13:55:19 | [diff] [blame] | 365 | // Unload before doing more cleanup to ensure that nothing is hanging on to |
| 366 | // any of these resources. |
| 367 | UnloadExtension(extension_id); |
| 368 | |
| 369 | extension_prefs_->OnExtensionUninstalled(extension_id_copy, location, |
| 370 | external_uninstall); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 371 | |
| 372 | // Tell the backend to start deleting installed extensions on the file thread. |
[email protected] | 831aa21 | 2010-03-26 13:55:19 | [diff] [blame] | 373 | if (Extension::LOAD != location) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 374 | ChromeThread::PostTask( |
| 375 | ChromeThread::FILE, FROM_HERE, |
| 376 | NewRunnableFunction( |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 377 | &extension_file_util::UninstallExtension, |
| 378 | install_directory_, |
| 379 | extension_id_copy)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 380 | } |
| 381 | |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 382 | ClearExtensionData(extension_url); |
| 383 | } |
| 384 | |
| 385 | void ExtensionsService::ClearExtensionData(const GURL& extension_url) { |
| 386 | scoped_refptr<ExtensionDataDeleter> deleter( |
| 387 | new ExtensionDataDeleter(profile_, extension_url)); |
| 388 | deleter->StartDeleting(); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 389 | } |
| 390 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 391 | void ExtensionsService::EnableExtension(const std::string& extension_id) { |
| 392 | Extension* extension = GetExtensionByIdInternal(extension_id, false, true); |
| 393 | if (!extension) { |
| 394 | NOTREACHED() << "Trying to enable an extension that isn't disabled."; |
| 395 | return; |
| 396 | } |
| 397 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 398 | extension_prefs_->SetExtensionState(extension, Extension::ENABLED); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 399 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 400 | // Move it over to the enabled list. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 401 | extensions_.push_back(extension); |
| 402 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 403 | disabled_extensions_.end(), |
| 404 | extension); |
| 405 | disabled_extensions_.erase(iter); |
| 406 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 407 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 408 | extension->GetChromeURLOverrides()); |
| 409 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 410 | NotifyExtensionLoaded(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 411 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 412 | } |
| 413 | |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 414 | void ExtensionsService::DisableExtension(const std::string& extension_id) { |
| 415 | Extension* extension = GetExtensionByIdInternal(extension_id, true, false); |
[email protected] | b2ba996 | 2009-12-10 20:10:15 | [diff] [blame] | 416 | // The extension may have been disabled already. |
| 417 | if (!extension) |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 418 | return; |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 419 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 420 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 421 | |
| 422 | // Move it over to the disabled list. |
| 423 | disabled_extensions_.push_back(extension); |
| 424 | ExtensionList::iterator iter = std::find(extensions_.begin(), |
| 425 | extensions_.end(), |
| 426 | extension); |
| 427 | extensions_.erase(iter); |
| 428 | |
| 429 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 430 | extension->GetChromeURLOverrides()); |
| 431 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 432 | NotifyExtensionUnloaded(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 433 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 434 | } |
| 435 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 436 | void ExtensionsService::LoadExtension(const FilePath& extension_path) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 437 | ChromeThread::PostTask( |
| 438 | ChromeThread::FILE, FROM_HERE, |
| 439 | NewRunnableMethod( |
| 440 | backend_.get(), |
| 441 | &ExtensionsServiceBackend::LoadSingleExtension, |
| 442 | extension_path, scoped_refptr<ExtensionsService>(this))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 445 | void ExtensionsService::LoadComponentExtensions() { |
| 446 | for (RegisteredComponentExtensions::iterator it = |
| 447 | component_extension_manifests_.begin(); |
| 448 | it != component_extension_manifests_.end(); ++it) { |
| 449 | JSONStringValueSerializer serializer(it->manifest); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 450 | scoped_ptr<Value> manifest(serializer.Deserialize(NULL, NULL)); |
[email protected] | 999731f | 2010-03-22 19:13:53 | [diff] [blame] | 451 | if (!manifest.get()) { |
[email protected] | 960ef8ae | 2010-05-06 17:12:29 | [diff] [blame] | 452 | DLOG(ERROR) << "Failed to retrieve manifest for extension"; |
[email protected] | 999731f | 2010-03-22 19:13:53 | [diff] [blame] | 453 | continue; |
| 454 | } |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 455 | |
| 456 | scoped_ptr<Extension> extension(new Extension(it->root_directory)); |
| 457 | extension->set_location(Extension::COMPONENT); |
| 458 | |
| 459 | std::string error; |
| 460 | if (!extension->InitFromValue( |
| 461 | *static_cast<DictionaryValue*>(manifest.get()), |
| 462 | true, // require key |
| 463 | &error)) { |
[email protected] | 4fdbc149 | 2010-07-01 01:20:59 | [diff] [blame] | 464 | NOTREACHED() << error; |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
| 468 | OnExtensionLoaded(extension.release(), false); // Don't allow privilege |
| 469 | // increase. |
| 470 | } |
| 471 | } |
| 472 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 473 | void ExtensionsService::LoadAllExtensions() { |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 474 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 475 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 476 | // Load any component extensions. |
| 477 | LoadComponentExtensions(); |
| 478 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 479 | // Load the previously installed extensions. |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 480 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( |
[email protected] | e6090e4 | 2010-03-23 22:44:08 | [diff] [blame] | 481 | extension_prefs_->GetInstalledExtensionsInfo()); |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 482 | |
| 483 | // If any extensions need localization, we bounce them all to the file thread |
| 484 | // for re-reading and localization. |
| 485 | for (size_t i = 0; i < info->size(); ++i) { |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 486 | if (ShouldReloadExtensionManifest(*info->at(i))) { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 487 | ChromeThread::PostTask( |
| 488 | ChromeThread::FILE, FROM_HERE, NewRunnableMethod( |
| 489 | backend_.get(), |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 490 | &ExtensionsServiceBackend::ReloadExtensionManifests, |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 491 | info.release(), // Callee takes ownership of the memory. |
| 492 | start_time, |
| 493 | scoped_refptr<ExtensionsService>(this))); |
| 494 | return; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // Don't update prefs. |
| 499 | // Callee takes ownership of the memory. |
| 500 | ContinueLoadAllExtensions(info.release(), start_time, false); |
| 501 | } |
| 502 | |
| 503 | void ExtensionsService::ContinueLoadAllExtensions( |
| 504 | ExtensionPrefs::ExtensionsInfo* extensions_info, |
| 505 | base::TimeTicks start_time, |
| 506 | bool write_to_prefs) { |
| 507 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(extensions_info); |
| 508 | |
| 509 | for (size_t i = 0; i < info->size(); ++i) { |
| 510 | LoadInstalledExtension(*info->at(i), write_to_prefs); |
| 511 | } |
| 512 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 513 | OnLoadedInstalledExtensions(); |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 514 | |
| 515 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size()); |
| 516 | UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size()); |
| 517 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 518 | UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime", |
| 519 | base::TimeTicks::Now() - start_time); |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 520 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 521 | int user_script_count = 0; |
| 522 | int extension_count = 0; |
| 523 | int theme_count = 0; |
| 524 | int external_count = 0; |
| 525 | int page_action_count = 0; |
| 526 | int browser_action_count = 0; |
| 527 | ExtensionList::iterator ex; |
| 528 | for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) { |
| 529 | // Don't count component extensions, since they are only extensions as an |
| 530 | // implementation detail. |
| 531 | if ((*ex)->location() == Extension::COMPONENT) |
| 532 | continue; |
| 533 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 534 | // Don't count unpacked extensions, since they're a developer-specific |
| 535 | // feature. |
| 536 | if ((*ex)->location() == Extension::LOAD) |
| 537 | continue; |
| 538 | |
[email protected] | 3ba0fd3 | 2010-06-19 05:39:10 | [diff] [blame] | 539 | if ((*ex)->is_theme()) { |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 540 | theme_count++; |
| 541 | } else if ((*ex)->converted_from_user_script()) { |
| 542 | user_script_count++; |
| 543 | } else { |
| 544 | extension_count++; |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 545 | } |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 546 | if (Extension::IsExternalLocation((*ex)->location())) { |
| 547 | external_count++; |
| 548 | } |
| 549 | if ((*ex)->page_action() != NULL) { |
| 550 | page_action_count++; |
| 551 | } |
| 552 | if ((*ex)->browser_action() != NULL) { |
| 553 | browser_action_count++; |
| 554 | } |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 555 | } |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 556 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count); |
| 557 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count); |
| 558 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count); |
| 559 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count); |
| 560 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count); |
| 561 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction", |
| 562 | browser_action_count); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 563 | } |
| 564 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 565 | void ExtensionsService::LoadInstalledExtension(const ExtensionInfo& info, |
| 566 | bool write_to_prefs) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 567 | std::string error; |
| 568 | Extension* extension = NULL; |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 569 | if (info.extension_manifest.get()) { |
| 570 | scoped_ptr<Extension> tmp(new Extension(info.extension_path)); |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 571 | bool require_key = info.extension_location != Extension::LOAD; |
| 572 | if (tmp->InitFromValue(*info.extension_manifest, require_key, &error)) |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 573 | extension = tmp.release(); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 574 | } else { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 575 | error = errors::kManifestUnreadable; |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | if (!extension) { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 579 | ReportExtensionLoadError(info.extension_path, |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 580 | error, |
| 581 | NotificationType::EXTENSION_INSTALL_ERROR, |
| 582 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 586 | extension->set_location(info.extension_location); |
| 587 | |
| 588 | if (write_to_prefs) |
| 589 | extension_prefs_->UpdateManifest(extension); |
| 590 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 591 | OnExtensionLoaded(extension, true); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 592 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 593 | if (info.extension_location == Extension::EXTERNAL_PREF || |
| 594 | info.extension_location == Extension::EXTERNAL_REGISTRY) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 595 | ChromeThread::PostTask( |
| 596 | ChromeThread::FILE, FROM_HERE, |
| 597 | NewRunnableMethod( |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 598 | backend_.get(), |
| 599 | &ExtensionsServiceBackend::CheckExternalUninstall, |
| 600 | scoped_refptr<ExtensionsService>(this), |
| 601 | info.extension_id, |
| 602 | info.extension_location)); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 603 | } |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 604 | } |
| 605 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 606 | void ExtensionsService::NotifyExtensionLoaded(Extension* extension) { |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 607 | // The ChromeURLRequestContexts need to be first to know that the extension |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 608 | // was loaded, otherwise a race can arise where a renderer that is created |
| 609 | // for the extension may try to load an extension URL with an extension id |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 610 | // that the request context doesn't yet know about. The profile is responsible |
| 611 | // for ensuring its URLRequestContexts appropriately discover the loaded |
| 612 | // extension. |
| 613 | if (profile_) { |
| 614 | profile_->RegisterExtensionWithRequestContexts(extension); |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 615 | |
| 616 | // Check if this permission requires unlimited storage quota |
| 617 | if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission)) { |
| 618 | string16 origin_identifier = |
| 619 | webkit_database::DatabaseUtil::GetOriginIdentifier(extension->url()); |
| 620 | ChromeThread::PostTask( |
| 621 | ChromeThread::FILE, FROM_HERE, |
| 622 | NewRunnableMethod( |
| 623 | profile_->GetDatabaseTracker(), |
| 624 | &webkit_database::DatabaseTracker::SetOriginQuotaInMemory, |
| 625 | origin_identifier, |
| 626 | kint64max)); |
| 627 | } |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 628 | } |
| 629 | |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 630 | LOG(INFO) << "Sending EXTENSION_LOADED"; |
| 631 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 632 | NotificationService::current()->Notify( |
| 633 | NotificationType::EXTENSION_LOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 634 | Source<Profile>(profile_), |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 635 | Details<Extension>(extension)); |
| 636 | } |
| 637 | |
| 638 | void ExtensionsService::NotifyExtensionUnloaded(Extension* extension) { |
| 639 | LOG(INFO) << "Sending EXTENSION_UNLOADED"; |
| 640 | |
| 641 | NotificationService::current()->Notify( |
| 642 | NotificationType::EXTENSION_UNLOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 643 | Source<Profile>(profile_), |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 644 | Details<Extension>(extension)); |
| 645 | |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 646 | if (profile_) { |
| 647 | profile_->UnregisterExtensionWithRequestContexts(extension); |
| 648 | |
| 649 | // Check if this permission required unlimited storage quota, reset its |
| 650 | // in-memory quota. |
| 651 | if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission)) { |
| 652 | string16 origin_identifier = |
| 653 | webkit_database::DatabaseUtil::GetOriginIdentifier(extension->url()); |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 654 | ChromeThread::PostTask( |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 655 | ChromeThread::FILE, FROM_HERE, |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 656 | NewRunnableMethod( |
[email protected] | 57a777f7 | 2010-03-31 01:09:42 | [diff] [blame] | 657 | profile_->GetDatabaseTracker(), |
| 658 | &webkit_database::DatabaseTracker::ResetOriginQuotaInMemory, |
| 659 | origin_identifier)); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 664 | void ExtensionsService::UpdateExtensionBlacklist( |
| 665 | const std::vector<std::string>& blacklist) { |
| 666 | // Use this set to indicate if an extension in the blacklist has been used. |
| 667 | std::set<std::string> blacklist_set; |
| 668 | for (unsigned int i = 0; i < blacklist.size(); ++i) { |
| 669 | if (Extension::IdIsValid(blacklist[i])) { |
| 670 | blacklist_set.insert(blacklist[i]); |
| 671 | } |
| 672 | } |
| 673 | extension_prefs_->UpdateBlacklist(blacklist_set); |
| 674 | std::vector<std::string> to_be_removed; |
| 675 | // Loop current extensions, unload installed extensions. |
| 676 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 677 | iter != extensions_.end(); ++iter) { |
| 678 | Extension* extension = (*iter); |
| 679 | if (blacklist_set.find(extension->id()) != blacklist_set.end()) { |
| 680 | to_be_removed.push_back(extension->id()); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | // UnloadExtension will change the extensions_ list. So, we should |
| 685 | // call it outside the iterator loop. |
| 686 | for (unsigned int i = 0; i < to_be_removed.size(); ++i) { |
| 687 | UnloadExtension(to_be_removed[i]); |
| 688 | } |
| 689 | } |
| 690 | |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 691 | bool ExtensionsService::IsIncognitoEnabled(const Extension* extension) { |
| 692 | // If this is a component extension we always allow it to work in incognito |
| 693 | // mode. |
| 694 | if (extension->location() == Extension::COMPONENT) |
| 695 | return true; |
| 696 | |
| 697 | // Check the prefs. |
| 698 | return extension_prefs_->IsIncognitoEnabled(extension->id()); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 699 | } |
[email protected] | 55a3569 | 2010-02-11 23:25:21 | [diff] [blame] | 700 | |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 701 | void ExtensionsService::SetIsIncognitoEnabled(Extension* extension, |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 702 | bool enabled) { |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 703 | extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled); |
[email protected] | c1499f3d | 2010-03-05 00:33:24 | [diff] [blame] | 704 | |
[email protected] | 568f33d | 2010-08-04 17:06:41 | [diff] [blame] | 705 | // Broadcast unloaded and loaded events to update browser state. Only bother |
| 706 | // if the extension is actually enabled, since there is no UI otherwise. |
| 707 | bool is_enabled = std::find(extensions_.begin(), extensions_.end(), |
| 708 | extension) != extensions_.end(); |
| 709 | if (is_enabled) { |
| 710 | NotifyExtensionUnloaded(extension); |
| 711 | NotifyExtensionLoaded(extension); |
| 712 | } |
[email protected] | 55a3569 | 2010-02-11 23:25:21 | [diff] [blame] | 713 | } |
| 714 | |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 715 | bool ExtensionsService::AllowFileAccess(const Extension* extension) { |
| 716 | return (CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | 334e04a | 2010-06-24 23:34:44 | [diff] [blame] | 717 | switches::kDisableExtensionsFileAccessCheck) || |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 718 | extension_prefs_->AllowFileAccess(extension->id())); |
| 719 | } |
| 720 | |
| 721 | void ExtensionsService::SetAllowFileAccess(Extension* extension, bool allow) { |
| 722 | extension_prefs_->SetAllowFileAccess(extension->id(), allow); |
| 723 | NotificationService::current()->Notify( |
| 724 | NotificationType::EXTENSION_USER_SCRIPTS_UPDATED, |
| 725 | Source<Profile>(profile_), |
| 726 | Details<Extension>(extension)); |
| 727 | } |
| 728 | |
| 729 | bool ExtensionsService::CanExecuteScriptOnHost(Extension* extension, |
| 730 | const GURL& url, |
| 731 | std::string* error) const { |
| 732 | // No extensions are allowed to execute script on the gallery because that |
| 733 | // would allow extensions to manipulate their own install pages. |
[email protected] | 638d45d | 2010-07-09 17:35:08 | [diff] [blame] | 734 | if (url.host() == GURL(Extension::ChromeStoreURL()).host() |
| 735 | && !CommandLine::ForCurrentProcess()->HasSwitch( |
| 736 | switches::kAllowScriptingGallery)) { |
[email protected] | 05c8218 | 2010-06-24 17:49:08 | [diff] [blame] | 737 | if (error) |
| 738 | *error = errors::kCannotScriptGallery; |
| 739 | return false; |
| 740 | } |
| 741 | |
| 742 | if (extension->HasHostPermission(url)) |
| 743 | return true; |
| 744 | |
| 745 | if (error) { |
| 746 | *error = ExtensionErrorUtils::FormatErrorMessage(errors::kCannotAccessPage, |
| 747 | url.spec()); |
| 748 | } |
| 749 | |
| 750 | return false; |
| 751 | } |
| 752 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 753 | void ExtensionsService::CheckForExternalUpdates() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 754 | // This installs or updates externally provided extensions. |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 755 | // TODO(aa): Why pass this list into the provider, why not just filter it |
| 756 | // later? |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 757 | std::set<std::string> killed_extensions; |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 758 | extension_prefs_->GetKilledExtensionIds(&killed_extensions); |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 759 | ChromeThread::PostTask( |
| 760 | ChromeThread::FILE, FROM_HERE, |
| 761 | NewRunnableMethod( |
| 762 | backend_.get(), &ExtensionsServiceBackend::CheckForExternalUpdates, |
| 763 | killed_extensions, scoped_refptr<ExtensionsService>(this))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void ExtensionsService::UnloadExtension(const std::string& extension_id) { |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 767 | // Make sure the extension gets deleted after we return from this function. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 768 | scoped_ptr<Extension> extension( |
| 769 | GetExtensionByIdInternal(extension_id, true, true)); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 770 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 771 | // Callers should not send us nonexistant extensions. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 772 | CHECK(extension.get()); |
| 773 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 774 | // Keep information about the extension so that we can reload it later |
| 775 | // even if it's not permanently installed. |
| 776 | unloaded_extension_paths_[extension->id()] = extension->path(); |
| 777 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 778 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 779 | extension->GetChromeURLOverrides()); |
| 780 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 781 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 782 | disabled_extensions_.end(), |
| 783 | extension.get()); |
| 784 | if (iter != disabled_extensions_.end()) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 785 | disabled_extensions_.erase(iter); |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 786 | NotificationService::current()->Notify( |
| 787 | NotificationType::EXTENSION_UNLOADED_DISABLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 788 | Source<Profile>(profile_), |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 789 | Details<Extension>(extension.get())); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 790 | return; |
| 791 | } |
| 792 | |
| 793 | iter = std::find(extensions_.begin(), extensions_.end(), extension.get()); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 794 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 795 | // Remove the extension from our list. |
| 796 | extensions_.erase(iter); |
| 797 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 798 | NotifyExtensionUnloaded(extension.get()); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 799 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 800 | } |
| 801 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 802 | void ExtensionsService::UnloadAllExtensions() { |
[email protected] | cd500f7 | 2010-06-25 23:44:32 | [diff] [blame] | 803 | STLDeleteContainerPointers(extensions_.begin(), extensions_.end()); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 804 | extensions_.clear(); |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 805 | |
[email protected] | cd500f7 | 2010-06-25 23:44:32 | [diff] [blame] | 806 | STLDeleteContainerPointers(disabled_extensions_.begin(), |
| 807 | disabled_extensions_.end()); |
| 808 | disabled_extensions_.clear(); |
| 809 | |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 810 | // TODO(erikkay) should there be a notification for this? We can't use |
| 811 | // EXTENSION_UNLOADED since that implies that the extension has been disabled |
| 812 | // or uninstalled, and UnloadAll is just part of shutdown. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | void ExtensionsService::ReloadExtensions() { |
| 816 | UnloadAllExtensions(); |
| 817 | LoadAllExtensions(); |
| 818 | } |
| 819 | |
| 820 | void ExtensionsService::GarbageCollectExtensions() { |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 821 | if (extension_prefs_->pref_service()->read_only()) |
| 822 | return; |
| 823 | |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 824 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( |
| 825 | extension_prefs_->GetInstalledExtensionsInfo()); |
| 826 | |
| 827 | std::map<std::string, FilePath> extension_paths; |
| 828 | for (size_t i = 0; i < info->size(); ++i) |
| 829 | extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path; |
| 830 | |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 831 | ChromeThread::PostTask( |
| 832 | ChromeThread::FILE, FROM_HERE, |
| 833 | NewRunnableFunction( |
| 834 | &extension_file_util::GarbageCollectExtensions, install_directory_, |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 835 | extension_paths)); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 836 | } |
| 837 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 838 | void ExtensionsService::OnLoadedInstalledExtensions() { |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 839 | ready_ = true; |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 840 | if (updater_.get()) { |
| 841 | updater_->Start(); |
| 842 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 843 | NotificationService::current()->Notify( |
| 844 | NotificationType::EXTENSIONS_READY, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 845 | Source<Profile>(profile_), |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 846 | NotificationService::NoDetails()); |
| 847 | } |
| 848 | |
[email protected] | 6d2e60bd | 2010-06-03 22:37:39 | [diff] [blame] | 849 | void ExtensionsService::OnExtensionLoaded(Extension* extension, |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 850 | bool allow_privilege_increase) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 851 | // Ensure extension is deleted unless we transfer ownership. |
| 852 | scoped_ptr<Extension> scoped_extension(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 853 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 854 | // The extension is now loaded, remove its data from unloaded extension map. |
| 855 | unloaded_extension_paths_.erase(extension->id()); |
| 856 | |
[email protected] | ceefd3d | 2010-03-12 09:10:29 | [diff] [blame] | 857 | // TODO(aa): Need to re-evaluate this branch. Does this still make sense now |
| 858 | // that extensions are enabled by default? |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 859 | if (extensions_enabled() || |
[email protected] | 3ba0fd3 | 2010-06-19 05:39:10 | [diff] [blame] | 860 | extension->is_theme() || |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 861 | extension->location() == Extension::LOAD || |
| 862 | Extension::IsExternalLocation(extension->location())) { |
| 863 | Extension* old = GetExtensionByIdInternal(extension->id(), true, true); |
| 864 | if (old) { |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 865 | // CrxInstaller should have guaranteed that we aren't downgrading. |
| 866 | CHECK(extension->version()->CompareTo(*(old->version())) >= 0); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 867 | |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 868 | bool allow_silent_upgrade = |
| 869 | allow_privilege_increase || !Extension::IsPrivilegeIncrease( |
| 870 | old, extension); |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 871 | |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 872 | // Extensions get upgraded if silent upgrades are allowed, otherwise |
| 873 | // they get disabled. |
| 874 | if (allow_silent_upgrade) { |
| 875 | old->set_being_upgraded(true); |
| 876 | extension->set_being_upgraded(true); |
| 877 | } |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 878 | |
[email protected] | ca3dbf5 | 2010-05-19 22:27:06 | [diff] [blame] | 879 | // To upgrade an extension in place, unload the old one and |
| 880 | // then load the new one. |
| 881 | UnloadExtension(old->id()); |
| 882 | old = NULL; |
| 883 | |
| 884 | if (!allow_silent_upgrade) { |
| 885 | // Extension has changed permissions significantly. Disable it. We |
| 886 | // send a notification below. |
| 887 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
| 888 | extension_prefs_->SetDidExtensionEscalatePermissions(extension, true); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 889 | } |
[email protected] | ba74f35 | 2009-06-11 18:54:45 | [diff] [blame] | 890 | } |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 891 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 892 | switch (extension_prefs_->GetExtensionState(extension->id())) { |
| 893 | case Extension::ENABLED: |
| 894 | extensions_.push_back(scoped_extension.release()); |
| 895 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 896 | NotifyExtensionLoaded(extension); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 897 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 898 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 899 | extension->GetChromeURLOverrides()); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 900 | break; |
| 901 | case Extension::DISABLED: |
[email protected] | 6d27a7b | 2009-12-18 23:25:45 | [diff] [blame] | 902 | disabled_extensions_.push_back(scoped_extension.release()); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 903 | NotificationService::current()->Notify( |
| 904 | NotificationType::EXTENSION_UPDATE_DISABLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 905 | Source<Profile>(profile_), |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 906 | Details<Extension>(extension)); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 907 | break; |
| 908 | default: |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 909 | NOTREACHED(); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 910 | break; |
[email protected] | 811f343 | 2009-07-25 19:38:21 | [diff] [blame] | 911 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 912 | } |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 913 | |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 914 | extension->set_being_upgraded(false); |
| 915 | |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 916 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 0b004da | 2010-07-02 17:54:31 | [diff] [blame] | 917 | |
| 918 | if (profile_->GetTemplateURLModel()) |
| 919 | profile_->GetTemplateURLModel()->RegisterExtensionKeyword(extension); |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 920 | |
| 921 | // Load the icon for omnibox-enabled extensions so it will be ready to display |
| 922 | // in the URL bar. |
| 923 | if (!extension->omnibox_keyword().empty()) |
| 924 | omnibox_icon_manager_.LoadIcon(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | void ExtensionsService::UpdateActiveExtensionsInCrashReporter() { |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 928 | std::set<std::string> extension_ids; |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 929 | for (size_t i = 0; i < extensions_.size(); ++i) { |
[email protected] | 3ba0fd3 | 2010-06-19 05:39:10 | [diff] [blame] | 930 | if (!extensions_[i]->is_theme()) |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 931 | extension_ids.insert(extensions_[i]->id()); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | child_process_logging::SetActiveExtensions(extension_ids); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 935 | } |
| 936 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 937 | void ExtensionsService::OnExtensionInstalled(Extension* extension, |
| 938 | bool allow_privilege_increase) { |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 939 | // Ensure extension is deleted unless we transfer ownership. |
| 940 | scoped_ptr<Extension> scoped_extension(extension); |
| 941 | Extension::State initial_state = Extension::DISABLED; |
| 942 | bool initial_enable_incognito = false; |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 943 | PendingExtensionMap::iterator it = |
| 944 | pending_extensions_.find(extension->id()); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 945 | if (it != pending_extensions_.end()) { |
[email protected] | 11edd1e | 2010-07-21 00:14:50 | [diff] [blame] | 946 | PendingExtensionInfo pending_extension_info = it->second; |
| 947 | pending_extensions_.erase(it); |
| 948 | it = pending_extensions_.end(); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 949 | // Set initial state from pending extension data. |
[email protected] | 11edd1e | 2010-07-21 00:14:50 | [diff] [blame] | 950 | if (pending_extension_info.is_theme != extension->is_theme()) { |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 951 | LOG(WARNING) |
| 952 | << "Not installing pending extension " << extension->id() |
| 953 | << " with is_theme = " << extension->is_theme() |
[email protected] | 11edd1e | 2010-07-21 00:14:50 | [diff] [blame] | 954 | << "; expected is_theme = " << pending_extension_info.is_theme; |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 955 | // Delete the extension directory since we're not going to |
| 956 | // load it. |
| 957 | ChromeThread::PostTask( |
| 958 | ChromeThread::FILE, FROM_HERE, |
| 959 | NewRunnableFunction(&DeleteFileHelper, extension->path(), true)); |
| 960 | return; |
| 961 | } |
[email protected] | 11edd1e | 2010-07-21 00:14:50 | [diff] [blame] | 962 | if (!extension->is_theme() && |
| 963 | !browser_sync::IsExtensionSyncable(*extension)) { |
| 964 | // We're an extension installed via sync that is unsyncable, |
| 965 | // i.e. we may have been syncable previously. We block these |
| 966 | // installs. We'll have to update the clause above if we decide |
| 967 | // to sync other extension-like things, like apps or user |
| 968 | // scripts. |
| 969 | // |
| 970 | // Note that this creates a small window where a user who tries |
| 971 | // to download/install an extension that is simultaneously |
| 972 | // installed via sync (and blocked) will find his download |
| 973 | // blocked. |
| 974 | // |
| 975 | // TODO(akalin): Remove this check once we've put in UI to |
| 976 | // approve synced extensions. |
| 977 | LOG(WARNING) |
| 978 | << "Not installing non-syncable extension " << extension->id(); |
| 979 | // Delete the extension directory since we're not going to |
| 980 | // load it. |
| 981 | ChromeThread::PostTask( |
| 982 | ChromeThread::FILE, FROM_HERE, |
| 983 | NewRunnableFunction(&DeleteFileHelper, extension->path(), true)); |
| 984 | return; |
| 985 | } |
| 986 | if (pending_extension_info.is_theme) { |
| 987 | DCHECK(pending_extension_info.enable_on_install); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 988 | initial_state = Extension::ENABLED; |
[email protected] | 11edd1e | 2010-07-21 00:14:50 | [diff] [blame] | 989 | DCHECK(!pending_extension_info.enable_incognito_on_install); |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 990 | initial_enable_incognito = false; |
| 991 | } else { |
| 992 | initial_state = |
[email protected] | 11edd1e | 2010-07-21 00:14:50 | [diff] [blame] | 993 | pending_extension_info.enable_on_install ? |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 994 | Extension::ENABLED : Extension::DISABLED; |
| 995 | initial_enable_incognito = |
[email protected] | 11edd1e | 2010-07-21 00:14:50 | [diff] [blame] | 996 | pending_extension_info.enable_incognito_on_install; |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 997 | } |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 998 | } else { |
[email protected] | dbec379 | 2010-08-10 00:08:45 | [diff] [blame^] | 999 | // Make sure we preserve enabled/disabled states. |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1000 | Extension::State existing_state = |
| 1001 | extension_prefs_->GetExtensionState(extension->id()); |
| 1002 | initial_state = |
| 1003 | (existing_state == Extension::DISABLED) ? |
| 1004 | Extension::DISABLED : Extension::ENABLED; |
[email protected] | dbec379 | 2010-08-10 00:08:45 | [diff] [blame^] | 1005 | initial_enable_incognito = |
| 1006 | extension_prefs_->IsIncognitoEnabled(extension->id()); |
[email protected] | aa14270 | 2010-03-26 01:26:33 | [diff] [blame] | 1007 | } |
| 1008 | |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1009 | extension_prefs_->OnExtensionInstalled( |
| 1010 | extension, initial_state, initial_enable_incognito); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1011 | |
[email protected] | 92a5b1d | 2010-07-20 00:42:00 | [diff] [blame] | 1012 | // Unpacked extensions start off with file access since they are a developer |
| 1013 | // feature. |
| 1014 | if (extension->location() == Extension::LOAD) |
| 1015 | extension_prefs_->SetAllowFileAccess(extension->id(), true); |
| 1016 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 1017 | // If the extension is a theme, tell the profile (and therefore ThemeProvider) |
| 1018 | // to apply it. |
[email protected] | 3ba0fd3 | 2010-06-19 05:39:10 | [diff] [blame] | 1019 | if (extension->is_theme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 1020 | NotificationService::current()->Notify( |
| 1021 | NotificationType::THEME_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1022 | Source<Profile>(profile_), |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 1023 | Details<Extension>(extension)); |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 1024 | } else { |
| 1025 | NotificationService::current()->Notify( |
| 1026 | NotificationType::EXTENSION_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1027 | Source<Profile>(profile_), |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 1028 | Details<Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 1029 | } |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1030 | |
[email protected] | 4416c5a | 2010-06-26 01:28:57 | [diff] [blame] | 1031 | // Transfer ownership of |extension| to OnExtensionLoaded. |
| 1032 | OnExtensionLoaded(scoped_extension.release(), allow_privilege_increase); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 1033 | } |
| 1034 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1035 | Extension* ExtensionsService::GetExtensionByIdInternal(const std::string& id, |
| 1036 | bool include_enabled, |
| 1037 | bool include_disabled) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 1038 | std::string lowercase_id = StringToLowerASCII(id); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 1039 | if (include_enabled) { |
| 1040 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 1041 | iter != extensions_.end(); ++iter) { |
| 1042 | if ((*iter)->id() == lowercase_id) |
| 1043 | return *iter; |
| 1044 | } |
| 1045 | } |
| 1046 | if (include_disabled) { |
| 1047 | for (ExtensionList::const_iterator iter = disabled_extensions_.begin(); |
| 1048 | iter != disabled_extensions_.end(); ++iter) { |
| 1049 | if ((*iter)->id() == lowercase_id) |
| 1050 | return *iter; |
| 1051 | } |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 1052 | } |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 1056 | Extension* ExtensionsService::GetExtensionByURL(const GURL& url) { |
[email protected] | a888b29e6 | 2010-04-01 13:38:57 | [diff] [blame] | 1057 | return url.scheme() != chrome::kExtensionScheme ? NULL : |
| 1058 | GetExtensionById(url.host(), false); |
| 1059 | } |
| 1060 | |
| 1061 | Extension* ExtensionsService::GetExtensionByWebExtent(const GURL& url) { |
| 1062 | for (size_t i = 0; i < extensions_.size(); ++i) { |
| 1063 | if (extensions_[i]->web_extent().ContainsURL(url)) |
| 1064 | return extensions_[i]; |
| 1065 | } |
| 1066 | return NULL; |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 1067 | } |
| 1068 | |
[email protected] | 6d2e60bd | 2010-06-03 22:37:39 | [diff] [blame] | 1069 | Extension* ExtensionsService::GetExtensionByOverlappingWebExtent( |
[email protected] | 9f72aa0 | 2010-06-25 10:01:05 | [diff] [blame] | 1070 | const ExtensionExtent& extent) { |
[email protected] | 22c966c | 2010-06-26 06:35:02 | [diff] [blame] | 1071 | for (size_t i = 0; i < extensions_.size(); ++i) { |
| 1072 | if (extensions_[i]->web_extent().OverlapsWith(extent)) |
| 1073 | return extensions_[i]; |
| 1074 | } |
| 1075 | |
[email protected] | 6d2e60bd | 2010-06-03 22:37:39 | [diff] [blame] | 1076 | return NULL; |
| 1077 | } |
| 1078 | |
[email protected] | b671760b | 2010-07-15 21:13:47 | [diff] [blame] | 1079 | const SkBitmap& ExtensionsService::GetOmniboxIcon( |
| 1080 | const std::string& extension_id) { |
| 1081 | return omnibox_icon_manager_.GetIcon(extension_id); |
| 1082 | } |
| 1083 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1084 | void ExtensionsService::ClearProvidersForTesting() { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1085 | ChromeThread::PostTask( |
| 1086 | ChromeThread::FILE, FROM_HERE, |
| 1087 | NewRunnableMethod( |
| 1088 | backend_.get(), &ExtensionsServiceBackend::ClearProvidersForTesting)); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | void ExtensionsService::SetProviderForTesting( |
| 1092 | Extension::Location location, ExternalExtensionProvider* test_provider) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1093 | ChromeThread::PostTask( |
| 1094 | ChromeThread::FILE, FROM_HERE, |
| 1095 | NewRunnableMethod( |
| 1096 | backend_.get(), &ExtensionsServiceBackend::SetProviderForTesting, |
| 1097 | location, test_provider)); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1098 | } |
| 1099 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1100 | void ExtensionsService::OnExternalExtensionFound(const std::string& id, |
| 1101 | const std::string& version, |
| 1102 | const FilePath& path, |
| 1103 | Extension::Location location) { |
| 1104 | // Before even bothering to unpack, check and see if we already have this |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 1105 | // version. This is important because these extensions are going to get |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1106 | // installed on every startup. |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 1107 | Extension* existing = GetExtensionById(id, true); |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 1108 | scoped_ptr<Version> other(Version::GetVersionFromString(version)); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1109 | if (existing) { |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 1110 | switch (existing->version()->CompareTo(*other)) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1111 | case -1: // existing version is older, we should upgrade |
| 1112 | break; |
| 1113 | case 0: // existing version is same, do nothing |
| 1114 | return; |
| 1115 | case 1: // existing version is newer, uh-oh |
| 1116 | LOG(WARNING) << "Found external version of extension " << id |
| 1117 | << "that is older than current version. Current version " |
| 1118 | << "is: " << existing->VersionString() << ". New version " |
| 1119 | << "is: " << version << ". Keeping current version."; |
| 1120 | return; |
| 1121 | } |
| 1122 | } |
| 1123 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 1124 | scoped_refptr<CrxInstaller> installer( |
| 1125 | new CrxInstaller(install_directory_, |
| 1126 | this, // frontend |
| 1127 | NULL)); // no client (silent install) |
| 1128 | installer->set_install_source(location); |
| 1129 | installer->set_expected_id(id); |
| 1130 | installer->set_allow_privilege_increase(true); |
| 1131 | installer->InstallCrx(path); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1132 | } |
| 1133 | |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1134 | void ExtensionsService::ReportExtensionLoadError( |
| 1135 | const FilePath& extension_path, |
| 1136 | const std::string &error, |
| 1137 | NotificationType type, |
| 1138 | bool be_noisy) { |
| 1139 | NotificationService* service = NotificationService::current(); |
| 1140 | service->Notify(type, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 1141 | Source<Profile>(profile_), |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1142 | Details<const std::string>(&error)); |
| 1143 | |
| 1144 | // TODO(port): note that this isn't guaranteed to work properly on Linux. |
[email protected] | 99efb7b1 | 2009-12-18 02:39:16 | [diff] [blame] | 1145 | std::string path_str = WideToUTF8(extension_path.ToWStringHack()); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1146 | std::string message = StringPrintf("Could not load extension from '%s'. %s", |
| 1147 | path_str.c_str(), error.c_str()); |
| 1148 | ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy); |
| 1149 | } |
| 1150 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 1151 | void ExtensionsService::Observe(NotificationType type, |
| 1152 | const NotificationSource& source, |
| 1153 | const NotificationDetails& details) { |
| 1154 | switch (type.value) { |
| 1155 | case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: { |
| 1156 | ExtensionHost* host = Details<ExtensionHost>(details).ptr(); |
| 1157 | OrphanedDevTools::iterator iter = |
| 1158 | orphaned_dev_tools_.find(host->extension()->id()); |
| 1159 | if (iter == orphaned_dev_tools_.end()) |
| 1160 | return; |
| 1161 | |
| 1162 | DevToolsManager::GetInstance()->AttachClientHost( |
| 1163 | iter->second, host->render_view_host()); |
| 1164 | orphaned_dev_tools_.erase(iter); |
| 1165 | break; |
| 1166 | } |
| 1167 | |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 1168 | case NotificationType::EXTENSION_PROCESS_TERMINATED: { |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 1169 | DCHECK_EQ(profile_, Source<Profile>(source).ptr()); |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 1170 | |
[email protected] | f128af4 | 2010-08-05 18:05:26 | [diff] [blame] | 1171 | ExtensionHost* host = Details<ExtensionHost>(details).ptr(); |
| 1172 | |
| 1173 | // TODO(rafaelw): Remove this check and ExtensionHost::recently_deleted(). |
| 1174 | // This is only here to help track down crbug.com/49114. |
| 1175 | ExtensionHost::HostPointerList::iterator iter = |
| 1176 | ExtensionHost::recently_deleted()->begin(); |
| 1177 | for (; iter != ExtensionHost::recently_deleted()->end(); iter++) { |
| 1178 | if (*iter == host) { |
| 1179 | CHECK(host->GetURL().spec().size() + 2 != 0); |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
| 1183 | if (iter == ExtensionHost::recently_deleted()->end()) |
| 1184 | CHECK(host->GetURL().spec().size() + 1 != 0); |
| 1185 | |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 1186 | // Unload the entire extension. We want it to be in a consistent state: |
| 1187 | // either fully working or not loaded at all, but never half-crashed. |
[email protected] | f128af4 | 2010-08-05 18:05:26 | [diff] [blame] | 1188 | UnloadExtension(host->extension()->id()); |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 1189 | break; |
| 1190 | } |
| 1191 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 1192 | default: |
| 1193 | NOTREACHED() << "Unexpected notification type."; |
| 1194 | } |
| 1195 | } |
| 1196 | |
[email protected] | 377011d | 2010-07-20 04:18:50 | [diff] [blame] | 1197 | bool ExtensionsService::HasApps() { |
| 1198 | if (!extensions_enabled_) |
| 1199 | return false; |
| 1200 | |
| 1201 | for (ExtensionList::const_iterator it = extensions_.begin(); |
| 1202 | it != extensions_.end(); ++it) { |
| 1203 | if ((*it)->is_app()) |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
| 1207 | return false; |
| 1208 | } |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 1209 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 1210 | // ExtensionsServicesBackend |
| 1211 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 1212 | ExtensionsServiceBackend::ExtensionsServiceBackend( |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1213 | const FilePath& install_directory) |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 1214 | : frontend_(NULL), |
| 1215 | install_directory_(install_directory), |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1216 | alert_on_error_(false) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1217 | // TODO(aa): This ends up doing blocking IO on the UI thread because it reads |
| 1218 | // pref data in the ctor and that is called on the UI thread. Would be better |
| 1219 | // to re-read data each time we list external extensions, anyway. |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1220 | external_extension_providers_[Extension::EXTERNAL_PREF] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1221 | linked_ptr<ExternalExtensionProvider>( |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 1222 | new ExternalPrefExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1223 | #if defined(OS_WIN) |
| 1224 | external_extension_providers_[Extension::EXTERNAL_REGISTRY] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1225 | linked_ptr<ExternalExtensionProvider>( |
| 1226 | new ExternalRegistryExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1227 | #endif |
| 1228 | } |
| 1229 | |
| 1230 | ExtensionsServiceBackend::~ExtensionsServiceBackend() { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 1231 | } |
| 1232 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1233 | void ExtensionsServiceBackend::LoadSingleExtension( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 1234 | const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1235 | frontend_ = frontend; |
| 1236 | |
| 1237 | // Explicit UI loads are always noisy. |
| 1238 | alert_on_error_ = true; |
| 1239 | |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 1240 | FilePath extension_path = path_in; |
[email protected] | f36fa4fb | 2009-06-19 18:23:50 | [diff] [blame] | 1241 | file_util::AbsolutePath(&extension_path); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 1242 | |
| 1243 | LOG(INFO) << "Loading single extension from " << |
[email protected] | 99efb7b1 | 2009-12-18 02:39:16 | [diff] [blame] | 1244 | extension_path.BaseName().value(); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 1245 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 1246 | std::string error; |
| 1247 | Extension* extension = extension_file_util::LoadExtension( |
| 1248 | extension_path, |
| 1249 | false, // Don't require id |
| 1250 | &error); |
| 1251 | |
| 1252 | if (!extension) { |
| 1253 | ReportExtensionLoadError(extension_path, error); |
| 1254 | return; |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 1255 | } |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 1256 | |
| 1257 | extension->set_location(Extension::LOAD); |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 1258 | |
| 1259 | // Report this as an installed extension so that it gets remembered in the |
| 1260 | // prefs. |
| 1261 | ChromeThread::PostTask( |
| 1262 | ChromeThread::UI, FROM_HERE, |
| 1263 | NewRunnableMethod(frontend_, &ExtensionsService::OnExtensionInstalled, |
| 1264 | extension, true)); |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 1265 | } |
| 1266 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 1267 | void ExtensionsServiceBackend::ReportExtensionLoadError( |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 1268 | const FilePath& extension_path, const std::string &error) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1269 | ChromeThread::PostTask( |
| 1270 | ChromeThread::UI, FROM_HERE, |
| 1271 | NewRunnableMethod( |
| 1272 | frontend_, |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1273 | &ExtensionsService::ReportExtensionLoadError, extension_path, |
| 1274 | error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 1275 | } |
| 1276 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1277 | bool ExtensionsServiceBackend::LookupExternalExtension( |
| 1278 | const std::string& id, Version** version, Extension::Location* location) { |
| 1279 | scoped_ptr<Version> extension_version; |
| 1280 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 1281 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1282 | const ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1283 | extension_version.reset(provider->RegisteredVersion(id, location)); |
| 1284 | if (extension_version.get()) { |
| 1285 | if (version) |
| 1286 | *version = extension_version.release(); |
| 1287 | return true; |
| 1288 | } |
| 1289 | } |
| 1290 | return false; |
| 1291 | } |
| 1292 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1293 | // Some extensions will autoupdate themselves externally from Chrome. These |
| 1294 | // are typically part of some larger client application package. To support |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1295 | // these, the extension will register its location in the the preferences file |
| 1296 | // (and also, on Windows, in the registry) and this code will periodically |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1297 | // check that location for a .crx file, which it will then install locally if |
| 1298 | // a new version is available. |
| 1299 | void ExtensionsServiceBackend::CheckForExternalUpdates( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 1300 | std::set<std::string> ids_to_ignore, |
| 1301 | scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1302 | // Note that this installation is intentionally silent (since it didn't |
| 1303 | // go through the front-end). Extensions that are registered in this |
| 1304 | // way are effectively considered 'pre-bundled', and so implicitly |
| 1305 | // trusted. In general, if something has HKLM or filesystem access, |
| 1306 | // they could install an extension manually themselves anyway. |
| 1307 | alert_on_error_ = false; |
| 1308 | frontend_ = frontend; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1309 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1310 | // Ask each external extension provider to give us a call back for each |
| 1311 | // extension they know about. See OnExternalExtensionFound. |
| 1312 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 1313 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1314 | ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1315 | provider->VisitRegisteredExtension(this, ids_to_ignore); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1316 | } |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1317 | } |
| 1318 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1319 | void ExtensionsServiceBackend::CheckExternalUninstall( |
| 1320 | scoped_refptr<ExtensionsService> frontend, const std::string& id, |
| 1321 | Extension::Location location) { |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1322 | // Check if the providers know about this extension. |
| 1323 | ProviderMap::const_iterator i = external_extension_providers_.find(location); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1324 | if (i == external_extension_providers_.end()) { |
| 1325 | NOTREACHED() << "CheckExternalUninstall called for non-external extension " |
| 1326 | << location; |
| 1327 | return; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1328 | } |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1329 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1330 | scoped_ptr<Version> version; |
| 1331 | version.reset(i->second->RegisteredVersion(id, NULL)); |
| 1332 | if (version.get()) |
| 1333 | return; // Yup, known extension, don't uninstall. |
| 1334 | |
| 1335 | // This is an external extension that we don't have registered. Uninstall. |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1336 | ChromeThread::PostTask( |
| 1337 | ChromeThread::UI, FROM_HERE, |
| 1338 | NewRunnableMethod( |
| 1339 | frontend.get(), &ExtensionsService::UninstallExtension, id, true)); |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1340 | } |
| 1341 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1342 | void ExtensionsServiceBackend::ClearProvidersForTesting() { |
| 1343 | external_extension_providers_.clear(); |
| 1344 | } |
| 1345 | |
| 1346 | void ExtensionsServiceBackend::SetProviderForTesting( |
| 1347 | Extension::Location location, |
| 1348 | ExternalExtensionProvider* test_provider) { |
| 1349 | DCHECK(test_provider); |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1350 | external_extension_providers_[location] = |
| 1351 | linked_ptr<ExternalExtensionProvider>(test_provider); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | void ExtensionsServiceBackend::OnExternalExtensionFound( |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1355 | const std::string& id, const Version* version, const FilePath& path, |
| 1356 | Extension::Location location) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1357 | ChromeThread::PostTask( |
| 1358 | ChromeThread::UI, FROM_HERE, |
| 1359 | NewRunnableMethod( |
| 1360 | frontend_, &ExtensionsService::OnExternalExtensionFound, id, |
| 1361 | version->GetString(), path, location)); |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 1362 | } |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1363 | |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 1364 | void ExtensionsServiceBackend::ReloadExtensionManifests( |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1365 | ExtensionPrefs::ExtensionsInfo* extensions_to_reload, |
| 1366 | base::TimeTicks start_time, |
| 1367 | scoped_refptr<ExtensionsService> frontend) { |
| 1368 | frontend_ = frontend; |
| 1369 | |
| 1370 | for (size_t i = 0; i < extensions_to_reload->size(); ++i) { |
| 1371 | ExtensionInfo* info = extensions_to_reload->at(i).get(); |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 1372 | if (!ShouldReloadExtensionManifest(*info)) |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1373 | continue; |
| 1374 | |
| 1375 | // We need to reload original manifest in order to localize properly. |
| 1376 | std::string error; |
| 1377 | scoped_ptr<Extension> extension(extension_file_util::LoadExtension( |
| 1378 | info->extension_path, false, &error)); |
| 1379 | |
| 1380 | if (extension.get()) |
| 1381 | extensions_to_reload->at(i)->extension_manifest.reset( |
| 1382 | static_cast<DictionaryValue*>( |
| 1383 | extension->manifest_value()->DeepCopy())); |
| 1384 | } |
| 1385 | |
| 1386 | // Finish installing on UI thread. |
| 1387 | ChromeThread::PostTask( |
| 1388 | ChromeThread::UI, FROM_HERE, |
| 1389 | NewRunnableMethod( |
| 1390 | frontend_, |
| 1391 | &ExtensionsService::ContinueLoadAllExtensions, |
| 1392 | extensions_to_reload, |
| 1393 | start_time, |
| 1394 | true)); |
| 1395 | } |