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