[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/extensions_service.h" |
| 6 | |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 7 | #include "base/basictypes.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 10 | #include "base/histogram.h" |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 11 | #include "base/string16.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 13 | #include "base/time.h" |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | 15730c4 | 2009-09-03 00:03:20 | [diff] [blame] | 15 | #include "chrome/browser/browser_process.h" |
[email protected] | dbb92e0d | 2009-08-20 16:18:21 | [diff] [blame] | 16 | #include "chrome/browser/chrome_thread.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 17 | #include "chrome/browser/debugger/devtools_manager.h" |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 18 | #include "chrome/browser/extensions/crx_installer.h" |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 19 | #include "chrome/browser/extensions/extension_accessibility_api.h" |
[email protected] | 840b0db | 2009-11-20 03:00:38 | [diff] [blame] | 20 | #include "chrome/browser/extensions/extension_bookmarks_module.h" |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_browser_event_router.h" |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_dom_ui.h" |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_history_api.h" |
[email protected] | b1748b1d8 | 2009-11-30 20:32:56 | [diff] [blame] | 24 | #include "chrome/browser/extensions/extension_host.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 25 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 26 | #include "chrome/browser/extensions/extension_updater.h" |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 27 | #include "chrome/browser/extensions/external_extension_provider.h" |
| 28 | #include "chrome/browser/extensions/external_pref_extension_provider.h" |
[email protected] | 052313b | 2010-02-19 09:43:08 | [diff] [blame] | 29 | #include "chrome/browser/pref_service.h" |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 30 | #include "chrome/browser/profile.h" |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 31 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 32 | #include "chrome/common/child_process_logging.h" |
[email protected] | e2eb4311 | 2009-05-29 21:19:54 | [diff] [blame] | 33 | #include "chrome/common/chrome_switches.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 34 | #include "chrome/common/extensions/extension.h" |
[email protected] | d7b36dc | 2009-10-29 21:47:40 | [diff] [blame] | 35 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 5b1a0e2 | 2009-05-26 19:00:58 | [diff] [blame] | 36 | #include "chrome/common/extensions/extension_error_reporter.h" |
[email protected] | 7c927b6 | 2010-02-24 09:54:13 | [diff] [blame] | 37 | #include "chrome/common/extensions/extension_file_util.h" |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 38 | #include "chrome/common/extensions/extension_l10n_util.h" |
[email protected] | 8289126 | 2008-12-24 00:21:26 | [diff] [blame] | 39 | #include "chrome/common/notification_service.h" |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 40 | #include "chrome/common/notification_type.h" |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 41 | #include "chrome/common/json_value_serializer.h" |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 42 | #include "chrome/common/pref_names.h" |
[email protected] | a5720987 | 2009-05-04 22:53:14 | [diff] [blame] | 43 | #include "chrome/common/url_constants.h" |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 44 | #include "webkit/database/database_tracker.h" |
| 45 | #include "webkit/database/database_util.h" |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 46 | |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 47 | #if defined(OS_WIN) |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 48 | #include "chrome/browser/extensions/external_registry_extension_provider_win.h" |
[email protected] | 79db623 | 2009-02-13 20:51:20 | [diff] [blame] | 49 | #endif |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 50 | |
[email protected] | 5ef47ec | 2010-01-28 05:58:05 | [diff] [blame] | 51 | using base::Time; |
| 52 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 53 | namespace errors = extension_manifest_errors; |
| 54 | |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 55 | namespace { |
| 56 | |
| 57 | // Helper class to collect the IDs of every extension listed in the prefs. |
| 58 | class InstalledExtensionSet { |
| 59 | public: |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 60 | explicit InstalledExtensionSet(ExtensionPrefs* prefs) { |
| 61 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( |
| 62 | ExtensionPrefs::CollectExtensionsInfo(prefs)); |
| 63 | |
| 64 | for (size_t i = 0; i < info->size(); ++i) { |
| 65 | std::string version; |
| 66 | const DictionaryValue* manifest = info->at(i)->extension_manifest.get(); |
| 67 | if (!manifest || |
| 68 | !manifest->GetString(extension_manifest_keys::kVersion, &version)) { |
| 69 | // Without a version, the extension is invalid. Ignoring it here will |
| 70 | // cause it to get garbage collected. |
| 71 | continue; |
| 72 | } |
| 73 | extensions_.insert(info->at(i)->extension_id); |
| 74 | versions_[info->at(i)->extension_id] = version; |
| 75 | } |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | const std::set<std::string>& extensions() { return extensions_; } |
[email protected] | 4559a7d | 2009-12-02 01:42:41 | [diff] [blame] | 79 | const std::map<std::string, std::string>& versions() { return versions_; } |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 80 | |
| 81 | private: |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 82 | std::set<std::string> extensions_; |
[email protected] | 4559a7d | 2009-12-02 01:42:41 | [diff] [blame] | 83 | std::map<std::string, std::string> versions_; |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 84 | }; |
| 85 | |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 86 | static bool ShouldReloadExtensionManifest(const ExtensionInfo& info) { |
| 87 | // Always reload LOAD extension manifests, because they can change on disk |
| 88 | // independent of the manifest in our prefs. |
| 89 | if (info.extension_location == Extension::LOAD) |
| 90 | return true; |
| 91 | |
| 92 | // Otherwise, reload the manifest it needs to be relocalized. |
| 93 | return extension_l10n_util::ShouldRelocalizeManifest(info); |
| 94 | } |
| 95 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 96 | } // namespace |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 97 | |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 98 | // ExtensionsService. |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 99 | |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 100 | const char* ExtensionsService::kInstallDirectoryName = "Extensions"; |
| 101 | const char* ExtensionsService::kCurrentVersionFileName = "Current Version"; |
[email protected] | 494c06e | 2009-07-25 01:06:42 | [diff] [blame] | 102 | |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 103 | // static |
| 104 | bool ExtensionsService::IsDownloadFromGallery(const GURL& download_url, |
| 105 | const GURL& referrer_url) { |
| 106 | if (StartsWithASCII(download_url.spec(), |
| 107 | extension_urls::kMiniGalleryDownloadPrefix, false) && |
| 108 | StartsWithASCII(referrer_url.spec(), |
| 109 | extension_urls::kMiniGalleryBrowsePrefix, false)) { |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | if (StartsWithASCII(download_url.spec(), |
| 114 | extension_urls::kGalleryDownloadPrefix, false) && |
| 115 | StartsWithASCII(referrer_url.spec(), |
| 116 | extension_urls::kGalleryBrowsePrefix, false)) { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
[email protected] | ac02528 | 2009-12-16 19:16:38 | [diff] [blame] | 123 | bool ExtensionsService::IsDownloadFromMiniGallery(const GURL& download_url) { |
| 124 | return StartsWithASCII(download_url.spec(), |
| 125 | extension_urls::kMiniGalleryDownloadPrefix, |
| 126 | false); // case_sensitive |
| 127 | } |
| 128 | |
[email protected] | 81e6378 | 2009-02-27 19:35:09 | [diff] [blame] | 129 | ExtensionsService::ExtensionsService(Profile* profile, |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 130 | const CommandLine* command_line, |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 131 | PrefService* prefs, |
| 132 | const FilePath& install_directory, |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 133 | bool autoupdate_enabled) |
[email protected] | 6ef635e4 | 2009-07-26 06:16:12 | [diff] [blame] | 134 | : profile_(profile), |
| 135 | extension_prefs_(new ExtensionPrefs(prefs, install_directory)), |
[email protected] | a9b00ac | 2009-06-25 21:03:23 | [diff] [blame] | 136 | install_directory_(install_directory), |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 137 | extensions_enabled_(true), |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 138 | show_extensions_prompts_(true), |
[email protected] | e0360f2c | 2009-12-07 22:34:31 | [diff] [blame] | 139 | ready_(false), |
| 140 | ALLOW_THIS_IN_INITIALIZER_LIST(toolbar_model_(this)) { |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 141 | // Figure out if extension installation should be enabled. |
[email protected] | 6d60703b | 2009-08-29 01:29:23 | [diff] [blame] | 142 | if (command_line->HasSwitch(switches::kDisableExtensions)) { |
| 143 | extensions_enabled_ = false; |
| 144 | } else if (profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions)) { |
| 145 | extensions_enabled_ = false; |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 146 | } |
[email protected] | 36a784c | 2009-06-23 06:21:08 | [diff] [blame] | 147 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 148 | registrar_.Add(this, NotificationType::EXTENSION_HOST_DID_STOP_LOADING, |
| 149 | NotificationService::AllSources()); |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 150 | registrar_.Add(this, NotificationType::EXTENSION_PROCESS_TERMINATED, |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 151 | Source<Profile>(profile_)); |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 152 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 153 | // Set up the ExtensionUpdater |
| 154 | if (autoupdate_enabled) { |
| 155 | int update_frequency = kDefaultUpdateFrequencySeconds; |
| 156 | if (command_line->HasSwitch(switches::kExtensionsUpdateFrequency)) { |
[email protected] | c4e52f0d | 2009-11-06 19:55:16 | [diff] [blame] | 157 | update_frequency = StringToInt(command_line->GetSwitchValueASCII( |
| 158 | switches::kExtensionsUpdateFrequency)); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 159 | } |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 160 | updater_ = new ExtensionUpdater(this, prefs, update_frequency); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 163 | backend_ = new ExtensionsServiceBackend(install_directory_); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | ExtensionsService::~ExtensionsService() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 167 | UnloadAllExtensions(); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 168 | if (updater_.get()) { |
| 169 | updater_->Stop(); |
| 170 | } |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 173 | void ExtensionsService::Init() { |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 174 | DCHECK(!ready_); |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 175 | DCHECK_EQ(extensions_.size(), 0u); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 176 | |
[email protected] | 95dd38f | 2009-10-20 20:09:15 | [diff] [blame] | 177 | // Hack: we need to ensure the ResourceDispatcherHost is ready before we load |
| 178 | // the first extension, because its members listen for loaded notifications. |
| 179 | g_browser_process->resource_dispatcher_host(); |
| 180 | |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 181 | // Start up the extension event routers. |
| 182 | ExtensionHistoryEventRouter::GetInstance()->ObserveProfile(profile_); |
[email protected] | 5cbe1e2 | 2010-01-30 01:18:56 | [diff] [blame] | 183 | ExtensionAccessibilityEventRouter::GetInstance()->ObserveProfile(profile_); |
[email protected] | de768a83 | 2009-10-30 05:25:01 | [diff] [blame] | 184 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 185 | LoadAllExtensions(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 186 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 187 | // TODO(erikkay) this should probably be deferred to a future point |
| 188 | // rather than running immediately at startup. |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 189 | CheckForExternalUpdates(); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 190 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 191 | // TODO(erikkay) this should probably be deferred as well. |
| 192 | GarbageCollectExtensions(); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 195 | void ExtensionsService::InstallExtension(const FilePath& extension_path) { |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 196 | scoped_refptr<CrxInstaller> installer( |
| 197 | new CrxInstaller(install_directory_, |
| 198 | this, // frontend |
| 199 | NULL)); // no client (silent install) |
| 200 | installer->set_allow_privilege_increase(true); |
| 201 | installer->InstallCrx(extension_path); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 204 | void ExtensionsService::UpdateExtension(const std::string& id, |
[email protected] | 5c851620 | 2010-03-18 21:43:34 | [diff] [blame] | 205 | const FilePath& extension_path, |
| 206 | const GURL& download_url) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 207 | if (!GetExtensionByIdInternal(id, true, true)) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 208 | LOG(WARNING) << "Will not update extension " << id << " because it is not " |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 209 | << "installed"; |
| 210 | return; |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 211 | } |
| 212 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 213 | scoped_refptr<CrxInstaller> installer( |
| 214 | new CrxInstaller(install_directory_, |
| 215 | this, // frontend |
| 216 | NULL)); // no client (silent install) |
| 217 | installer->set_expected_id(id); |
| 218 | installer->set_delete_source(true); |
[email protected] | 867a73e1 | 2010-03-19 20:45:46 | [diff] [blame] | 219 | installer->set_force_web_origin_to_download_url(true); |
[email protected] | 5c851620 | 2010-03-18 21:43:34 | [diff] [blame] | 220 | installer->set_original_url(download_url); |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 221 | installer->InstallCrx(extension_path); |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 224 | void ExtensionsService::ReloadExtension(const std::string& extension_id) { |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 225 | FilePath path; |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 226 | Extension* current_extension = GetExtensionById(extension_id, false); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 227 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 228 | // Unload the extension if it's loaded. It might not be loaded if it crashed. |
| 229 | if (current_extension) { |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 230 | // If the extension has an inspector open for its background page, detach |
| 231 | // the inspector and hang onto a cookie for it, so that we can reattach |
| 232 | // later. |
| 233 | ExtensionProcessManager* manager = profile_->GetExtensionProcessManager(); |
| 234 | ExtensionHost* host = manager->GetBackgroundHostForExtension( |
| 235 | current_extension); |
| 236 | if (host) { |
| 237 | // Look for an open inspector for the background page. |
| 238 | int devtools_cookie = DevToolsManager::GetInstance()->DetachClientHost( |
| 239 | host->render_view_host()); |
| 240 | if (devtools_cookie >= 0) |
| 241 | orphaned_dev_tools_[extension_id] = devtools_cookie; |
| 242 | } |
| 243 | |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 244 | path = current_extension->path(); |
| 245 | UnloadExtension(extension_id); |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 246 | } else { |
| 247 | path = unloaded_extension_paths_[extension_id]; |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 250 | // We should always be able to remember the extension's path. If it's not in |
| 251 | // the map, someone failed to update |unloaded_extension_paths_|. |
| 252 | CHECK(!path.empty()); |
[email protected] | b65272f | 2009-08-31 15:47:06 | [diff] [blame] | 253 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 254 | LoadExtension(path); |
[email protected] | 9cddd470 | 2009-07-27 22:09:40 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 257 | void ExtensionsService::UninstallExtension(const std::string& extension_id, |
| 258 | bool external_uninstall) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 259 | Extension* extension = GetExtensionByIdInternal(extension_id, true, true); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 260 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 261 | // Callers should not send us nonexistant extensions. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 262 | DCHECK(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 263 | |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 264 | extension_prefs_->OnExtensionUninstalled(extension, external_uninstall); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 265 | |
| 266 | // Tell the backend to start deleting installed extensions on the file thread. |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 267 | if (Extension::LOAD != extension->location()) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 268 | ChromeThread::PostTask( |
| 269 | ChromeThread::FILE, FROM_HERE, |
| 270 | NewRunnableFunction( |
| 271 | &extension_file_util::UninstallExtension, extension_id, |
| 272 | install_directory_)); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 275 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 276 | extension->GetChromeURLOverrides()); |
| 277 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 278 | UnloadExtension(extension_id); |
| 279 | } |
| 280 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 281 | void ExtensionsService::EnableExtension(const std::string& extension_id) { |
| 282 | Extension* extension = GetExtensionByIdInternal(extension_id, false, true); |
| 283 | if (!extension) { |
| 284 | NOTREACHED() << "Trying to enable an extension that isn't disabled."; |
| 285 | return; |
| 286 | } |
| 287 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 288 | extension_prefs_->SetExtensionState(extension, Extension::ENABLED); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 289 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 290 | // Move it over to the enabled list. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 291 | extensions_.push_back(extension); |
| 292 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 293 | disabled_extensions_.end(), |
| 294 | extension); |
| 295 | disabled_extensions_.erase(iter); |
| 296 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 297 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 298 | extension->GetChromeURLOverrides()); |
| 299 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 300 | NotifyExtensionLoaded(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 301 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 304 | void ExtensionsService::DisableExtension(const std::string& extension_id) { |
| 305 | Extension* extension = GetExtensionByIdInternal(extension_id, true, false); |
[email protected] | b2ba996 | 2009-12-10 20:10:15 | [diff] [blame] | 306 | // The extension may have been disabled already. |
| 307 | if (!extension) |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 308 | return; |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 309 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 310 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 311 | |
| 312 | // Move it over to the disabled list. |
| 313 | disabled_extensions_.push_back(extension); |
| 314 | ExtensionList::iterator iter = std::find(extensions_.begin(), |
| 315 | extensions_.end(), |
| 316 | extension); |
| 317 | extensions_.erase(iter); |
| 318 | |
| 319 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 320 | extension->GetChromeURLOverrides()); |
| 321 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 322 | NotifyExtensionUnloaded(extension); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 323 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 1784e83a | 2009-09-08 21:01:52 | [diff] [blame] | 324 | } |
| 325 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 326 | void ExtensionsService::LoadExtension(const FilePath& extension_path) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 327 | ChromeThread::PostTask( |
| 328 | ChromeThread::FILE, FROM_HERE, |
| 329 | NewRunnableMethod( |
| 330 | backend_.get(), |
| 331 | &ExtensionsServiceBackend::LoadSingleExtension, |
| 332 | extension_path, scoped_refptr<ExtensionsService>(this))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 335 | void ExtensionsService::LoadComponentExtensions() { |
| 336 | for (RegisteredComponentExtensions::iterator it = |
| 337 | component_extension_manifests_.begin(); |
| 338 | it != component_extension_manifests_.end(); ++it) { |
| 339 | JSONStringValueSerializer serializer(it->manifest); |
| 340 | scoped_ptr<Value> manifest(serializer.Deserialize(NULL)); |
[email protected] | 999731f | 2010-03-22 19:13:53 | [diff] [blame^] | 341 | if (!manifest.get()) { |
| 342 | NOTREACHED() << "Failed to retrieve manifest for extension"; |
| 343 | continue; |
| 344 | } |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 345 | |
| 346 | scoped_ptr<Extension> extension(new Extension(it->root_directory)); |
| 347 | extension->set_location(Extension::COMPONENT); |
| 348 | |
| 349 | std::string error; |
| 350 | if (!extension->InitFromValue( |
| 351 | *static_cast<DictionaryValue*>(manifest.get()), |
| 352 | true, // require key |
| 353 | &error)) { |
| 354 | NOTREACHED(); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | OnExtensionLoaded(extension.release(), false); // Don't allow privilege |
| 359 | // increase. |
| 360 | } |
| 361 | } |
| 362 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 363 | void ExtensionsService::LoadAllExtensions() { |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 364 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 365 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 366 | // Load any component extensions. |
| 367 | LoadComponentExtensions(); |
| 368 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 369 | // Load the previously installed extensions. |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 370 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( |
| 371 | ExtensionPrefs::CollectExtensionsInfo(extension_prefs_.get())); |
| 372 | |
| 373 | // If any extensions need localization, we bounce them all to the file thread |
| 374 | // for re-reading and localization. |
| 375 | for (size_t i = 0; i < info->size(); ++i) { |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 376 | if (ShouldReloadExtensionManifest(*info->at(i))) { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 377 | ChromeThread::PostTask( |
| 378 | ChromeThread::FILE, FROM_HERE, NewRunnableMethod( |
| 379 | backend_.get(), |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 380 | &ExtensionsServiceBackend::ReloadExtensionManifests, |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 381 | info.release(), // Callee takes ownership of the memory. |
| 382 | start_time, |
| 383 | scoped_refptr<ExtensionsService>(this))); |
| 384 | return; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | // Don't update prefs. |
| 389 | // Callee takes ownership of the memory. |
| 390 | ContinueLoadAllExtensions(info.release(), start_time, false); |
| 391 | } |
| 392 | |
| 393 | void ExtensionsService::ContinueLoadAllExtensions( |
| 394 | ExtensionPrefs::ExtensionsInfo* extensions_info, |
| 395 | base::TimeTicks start_time, |
| 396 | bool write_to_prefs) { |
| 397 | scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(extensions_info); |
| 398 | |
| 399 | for (size_t i = 0; i < info->size(); ++i) { |
| 400 | LoadInstalledExtension(*info->at(i), write_to_prefs); |
| 401 | } |
| 402 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 403 | OnLoadedInstalledExtensions(); |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 404 | |
| 405 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadAll", extensions_.size()); |
| 406 | UMA_HISTOGRAM_COUNTS_100("Extensions.Disabled", disabled_extensions_.size()); |
| 407 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 408 | UMA_HISTOGRAM_TIMES("Extensions.LoadAllTime", |
| 409 | base::TimeTicks::Now() - start_time); |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 410 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 411 | int user_script_count = 0; |
| 412 | int extension_count = 0; |
| 413 | int theme_count = 0; |
| 414 | int external_count = 0; |
| 415 | int page_action_count = 0; |
| 416 | int browser_action_count = 0; |
| 417 | ExtensionList::iterator ex; |
| 418 | for (ex = extensions_.begin(); ex != extensions_.end(); ++ex) { |
| 419 | // Don't count component extensions, since they are only extensions as an |
| 420 | // implementation detail. |
| 421 | if ((*ex)->location() == Extension::COMPONENT) |
| 422 | continue; |
| 423 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 424 | // Don't count unpacked extensions, since they're a developer-specific |
| 425 | // feature. |
| 426 | if ((*ex)->location() == Extension::LOAD) |
| 427 | continue; |
| 428 | |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 429 | if ((*ex)->IsTheme()) { |
| 430 | theme_count++; |
| 431 | } else if ((*ex)->converted_from_user_script()) { |
| 432 | user_script_count++; |
| 433 | } else { |
| 434 | extension_count++; |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 435 | } |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 436 | if (Extension::IsExternalLocation((*ex)->location())) { |
| 437 | external_count++; |
| 438 | } |
| 439 | if ((*ex)->page_action() != NULL) { |
| 440 | page_action_count++; |
| 441 | } |
| 442 | if ((*ex)->browser_action() != NULL) { |
| 443 | browser_action_count++; |
| 444 | } |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 445 | } |
[email protected] | 1952c7d | 2010-03-04 23:48:34 | [diff] [blame] | 446 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExtension", extension_count); |
| 447 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadUserScript", user_script_count); |
| 448 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadTheme", theme_count); |
| 449 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadExternal", external_count); |
| 450 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadPageAction", page_action_count); |
| 451 | UMA_HISTOGRAM_COUNTS_100("Extensions.LoadBrowserAction", |
| 452 | browser_action_count); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 455 | void ExtensionsService::LoadInstalledExtension(const ExtensionInfo& info, |
| 456 | bool write_to_prefs) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 457 | std::string error; |
| 458 | Extension* extension = NULL; |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 459 | if (info.extension_manifest.get()) { |
| 460 | scoped_ptr<Extension> tmp(new Extension(info.extension_path)); |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 461 | bool require_key = info.extension_location != Extension::LOAD; |
| 462 | if (tmp->InitFromValue(*info.extension_manifest, require_key, &error)) |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 463 | extension = tmp.release(); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 464 | } else { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 465 | error = errors::kManifestUnreadable; |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | if (!extension) { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 469 | ReportExtensionLoadError(info.extension_path, |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 470 | error, |
| 471 | NotificationType::EXTENSION_INSTALL_ERROR, |
| 472 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 473 | return; |
| 474 | } |
| 475 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 476 | extension->set_location(info.extension_location); |
| 477 | |
| 478 | if (write_to_prefs) |
| 479 | extension_prefs_->UpdateManifest(extension); |
| 480 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 481 | OnExtensionLoaded(extension, true); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 482 | |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 483 | if (info.extension_location == Extension::EXTERNAL_PREF || |
| 484 | info.extension_location == Extension::EXTERNAL_REGISTRY) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 485 | ChromeThread::PostTask( |
| 486 | ChromeThread::FILE, FROM_HERE, |
| 487 | NewRunnableMethod( |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 488 | backend_.get(), |
| 489 | &ExtensionsServiceBackend::CheckExternalUninstall, |
| 490 | scoped_refptr<ExtensionsService>(this), |
| 491 | info.extension_id, |
| 492 | info.extension_location)); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 493 | } |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 494 | } |
| 495 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 496 | void ExtensionsService::NotifyExtensionLoaded(Extension* extension) { |
| 497 | LOG(INFO) << "Sending EXTENSION_LOADED"; |
| 498 | |
| 499 | // The ChromeURLRequestContext needs to be first to know that the extension |
| 500 | // was loaded, otherwise a race can arise where a renderer that is created |
| 501 | // for the extension may try to load an extension URL with an extension id |
| 502 | // that the request context doesn't yet know about. |
| 503 | if (profile_ && !profile_->IsOffTheRecord()) { |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 504 | ChromeURLRequestContextGetter* context_getter = |
| 505 | static_cast<ChromeURLRequestContextGetter*>( |
| 506 | profile_->GetRequestContext()); |
| 507 | if (context_getter) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 508 | ChromeThread::PostTask( |
| 509 | ChromeThread::IO, FROM_HERE, |
| 510 | NewRunnableMethod( |
| 511 | context_getter, |
| 512 | &ChromeURLRequestContextGetter::OnNewExtensions, |
| 513 | extension->id(), |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 514 | new ChromeURLRequestContext::ExtensionInfo( |
| 515 | extension->path(), |
| 516 | extension->default_locale(), |
[email protected] | 867a73e1 | 2010-03-19 20:45:46 | [diff] [blame] | 517 | std::vector<URLPattern>(), |
[email protected] | 0ce3f598 | 2010-01-28 23:04:27 | [diff] [blame] | 518 | extension->api_permissions()))); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 519 | } |
[email protected] | 24b538a | 2010-02-27 01:22:44 | [diff] [blame] | 520 | |
| 521 | // Check if this permission requires unlimited storage quota |
| 522 | if (extension->HasApiPermission(Extension::kUnlimitedStoragePermission)) { |
| 523 | string16 origin_identifier = |
| 524 | webkit_database::DatabaseUtil::GetOriginIdentifier(extension->url()); |
| 525 | ChromeThread::PostTask( |
| 526 | ChromeThread::FILE, FROM_HERE, |
| 527 | NewRunnableMethod( |
| 528 | profile_->GetDatabaseTracker(), |
| 529 | &webkit_database::DatabaseTracker::SetOriginQuotaInMemory, |
| 530 | origin_identifier, |
| 531 | kint64max)); |
| 532 | } |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | NotificationService::current()->Notify( |
| 536 | NotificationType::EXTENSION_LOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 537 | Source<Profile>(profile_), |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 538 | Details<Extension>(extension)); |
| 539 | } |
| 540 | |
| 541 | void ExtensionsService::NotifyExtensionUnloaded(Extension* extension) { |
| 542 | LOG(INFO) << "Sending EXTENSION_UNLOADED"; |
| 543 | |
| 544 | NotificationService::current()->Notify( |
| 545 | NotificationType::EXTENSION_UNLOADED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 546 | Source<Profile>(profile_), |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 547 | Details<Extension>(extension)); |
| 548 | |
| 549 | if (profile_ && !profile_->IsOffTheRecord()) { |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 550 | ChromeURLRequestContextGetter* context_getter = |
| 551 | static_cast<ChromeURLRequestContextGetter*>( |
| 552 | profile_->GetRequestContext()); |
| 553 | if (context_getter) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 554 | ChromeThread::PostTask( |
| 555 | ChromeThread::IO, FROM_HERE, |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 556 | NewRunnableMethod( |
| 557 | context_getter, |
| 558 | &ChromeURLRequestContextGetter::OnUnloadedExtension, |
| 559 | extension->id())); |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
[email protected] | 6b75ec3 | 2009-08-14 06:37:18 | [diff] [blame] | 564 | void ExtensionsService::UpdateExtensionBlacklist( |
| 565 | const std::vector<std::string>& blacklist) { |
| 566 | // Use this set to indicate if an extension in the blacklist has been used. |
| 567 | std::set<std::string> blacklist_set; |
| 568 | for (unsigned int i = 0; i < blacklist.size(); ++i) { |
| 569 | if (Extension::IdIsValid(blacklist[i])) { |
| 570 | blacklist_set.insert(blacklist[i]); |
| 571 | } |
| 572 | } |
| 573 | extension_prefs_->UpdateBlacklist(blacklist_set); |
| 574 | std::vector<std::string> to_be_removed; |
| 575 | // Loop current extensions, unload installed extensions. |
| 576 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 577 | iter != extensions_.end(); ++iter) { |
| 578 | Extension* extension = (*iter); |
| 579 | if (blacklist_set.find(extension->id()) != blacklist_set.end()) { |
| 580 | to_be_removed.push_back(extension->id()); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // UnloadExtension will change the extensions_ list. So, we should |
| 585 | // call it outside the iterator loop. |
| 586 | for (unsigned int i = 0; i < to_be_removed.size(); ++i) { |
| 587 | UnloadExtension(to_be_removed[i]); |
| 588 | } |
| 589 | } |
| 590 | |
[email protected] | 5ef47ec | 2010-01-28 05:58:05 | [diff] [blame] | 591 | void ExtensionsService::SetLastPingDay(const std::string& extension_id, |
| 592 | const base::Time& time) { |
| 593 | extension_prefs_->SetLastPingDay(extension_id, time); |
| 594 | } |
| 595 | |
| 596 | base::Time ExtensionsService::LastPingDay(const std::string& extension_id) { |
| 597 | return extension_prefs_->LastPingDay(extension_id); |
| 598 | } |
| 599 | |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 600 | bool ExtensionsService::IsIncognitoEnabled(const Extension* extension) { |
| 601 | // If this is a component extension we always allow it to work in incognito |
| 602 | // mode. |
| 603 | if (extension->location() == Extension::COMPONENT) |
| 604 | return true; |
| 605 | |
| 606 | // Check the prefs. |
| 607 | return extension_prefs_->IsIncognitoEnabled(extension->id()); |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 608 | } |
[email protected] | 55a3569 | 2010-02-11 23:25:21 | [diff] [blame] | 609 | |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 610 | void ExtensionsService::SetIsIncognitoEnabled(Extension* extension, |
[email protected] | db7331a | 2010-02-25 22:10:50 | [diff] [blame] | 611 | bool enabled) { |
[email protected] | cb0ce1e02 | 2010-03-10 19:54:41 | [diff] [blame] | 612 | extension_prefs_->SetIsIncognitoEnabled(extension->id(), enabled); |
[email protected] | c1499f3d | 2010-03-05 00:33:24 | [diff] [blame] | 613 | |
| 614 | // Broadcast unloaded and loaded events to update browser state. |
| 615 | NotifyExtensionUnloaded(extension); |
| 616 | NotifyExtensionLoaded(extension); |
[email protected] | 55a3569 | 2010-02-11 23:25:21 | [diff] [blame] | 617 | } |
| 618 | |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 619 | void ExtensionsService::CheckForExternalUpdates() { |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 620 | // This installs or updates externally provided extensions. |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 621 | // TODO(aa): Why pass this list into the provider, why not just filter it |
| 622 | // later? |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 623 | std::set<std::string> killed_extensions; |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 624 | extension_prefs_->GetKilledExtensionIds(&killed_extensions); |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 625 | ChromeThread::PostTask( |
| 626 | ChromeThread::FILE, FROM_HERE, |
| 627 | NewRunnableMethod( |
| 628 | backend_.get(), &ExtensionsServiceBackend::CheckForExternalUpdates, |
| 629 | killed_extensions, scoped_refptr<ExtensionsService>(this))); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | void ExtensionsService::UnloadExtension(const std::string& extension_id) { |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 633 | // Make sure the extension gets deleted after we return from this function. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 634 | scoped_ptr<Extension> extension( |
| 635 | GetExtensionByIdInternal(extension_id, true, true)); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 636 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 637 | // Callers should not send us nonexistant extensions. |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 638 | CHECK(extension.get()); |
| 639 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 640 | // Keep information about the extension so that we can reload it later |
| 641 | // even if it's not permanently installed. |
| 642 | unloaded_extension_paths_[extension->id()] = extension->path(); |
| 643 | |
[email protected] | 86c008e8 | 2009-08-28 20:26:05 | [diff] [blame] | 644 | ExtensionDOMUI::UnregisterChromeURLOverrides(profile_, |
| 645 | extension->GetChromeURLOverrides()); |
| 646 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 647 | ExtensionList::iterator iter = std::find(disabled_extensions_.begin(), |
| 648 | disabled_extensions_.end(), |
| 649 | extension.get()); |
| 650 | if (iter != disabled_extensions_.end()) { |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 651 | disabled_extensions_.erase(iter); |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 652 | NotificationService::current()->Notify( |
| 653 | NotificationType::EXTENSION_UNLOADED_DISABLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 654 | Source<Profile>(profile_), |
[email protected] | 86693068 | 2009-08-18 22:53:47 | [diff] [blame] | 655 | Details<Extension>(extension.get())); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 656 | return; |
| 657 | } |
| 658 | |
| 659 | iter = std::find(extensions_.begin(), extensions_.end(), extension.get()); |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 660 | |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 661 | // Remove the extension from our list. |
| 662 | extensions_.erase(iter); |
| 663 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 664 | NotifyExtensionUnloaded(extension.get()); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 665 | UpdateActiveExtensionsInCrashReporter(); |
[email protected] | 631cf82 | 2009-05-15 07:01:25 | [diff] [blame] | 666 | } |
| 667 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 668 | void ExtensionsService::UnloadAllExtensions() { |
| 669 | ExtensionList::iterator iter; |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 670 | for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 671 | delete *iter; |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 672 | extensions_.clear(); |
[email protected] | c6e4a341 | 2009-06-24 15:45:29 | [diff] [blame] | 673 | |
| 674 | // TODO(erikkay) should there be a notification for this? We can't use |
| 675 | // EXTENSION_UNLOADED since that implies that the extension has been disabled |
| 676 | // or uninstalled, and UnloadAll is just part of shutdown. |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void ExtensionsService::ReloadExtensions() { |
| 680 | UnloadAllExtensions(); |
| 681 | LoadAllExtensions(); |
| 682 | } |
| 683 | |
| 684 | void ExtensionsService::GarbageCollectExtensions() { |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 685 | InstalledExtensionSet installed(extension_prefs_.get()); |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 686 | ChromeThread::PostTask( |
| 687 | ChromeThread::FILE, FROM_HERE, |
| 688 | NewRunnableFunction( |
| 689 | &extension_file_util::GarbageCollectExtensions, install_directory_, |
[email protected] | 4559a7d | 2009-12-02 01:42:41 | [diff] [blame] | 690 | installed.extensions(), installed.versions())); |
[email protected] | 3cf4f099 | 2009-02-03 23:00:30 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 693 | void ExtensionsService::OnLoadedInstalledExtensions() { |
[email protected] | e81dba3 | 2009-06-19 20:19:13 | [diff] [blame] | 694 | ready_ = true; |
[email protected] | 93fd78f4 | 2009-07-10 16:43:17 | [diff] [blame] | 695 | if (updater_.get()) { |
| 696 | updater_->Start(); |
| 697 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 698 | NotificationService::current()->Notify( |
| 699 | NotificationType::EXTENSIONS_READY, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 700 | Source<Profile>(profile_), |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 701 | NotificationService::NoDetails()); |
| 702 | } |
| 703 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 704 | void ExtensionsService::OnExtensionLoaded(Extension* extension, |
| 705 | bool allow_privilege_increase) { |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 706 | // Ensure extension is deleted unless we transfer ownership. |
| 707 | scoped_ptr<Extension> scoped_extension(extension); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 708 | |
[email protected] | 1eb17508 | 2010-02-10 09:26:16 | [diff] [blame] | 709 | // The extension is now loaded, remove its data from unloaded extension map. |
| 710 | unloaded_extension_paths_.erase(extension->id()); |
| 711 | |
[email protected] | ceefd3d | 2010-03-12 09:10:29 | [diff] [blame] | 712 | // TODO(aa): Need to re-evaluate this branch. Does this still make sense now |
| 713 | // that extensions are enabled by default? |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 714 | if (extensions_enabled() || |
| 715 | extension->IsTheme() || |
| 716 | extension->location() == Extension::LOAD || |
| 717 | Extension::IsExternalLocation(extension->location())) { |
| 718 | Extension* old = GetExtensionByIdInternal(extension->id(), true, true); |
| 719 | if (old) { |
| 720 | if (extension->version()->CompareTo(*(old->version())) > 0) { |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 721 | bool allow_silent_upgrade = |
| 722 | allow_privilege_increase || !Extension::IsPrivilegeIncrease( |
| 723 | old, extension); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 724 | |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 725 | // Extensions get upgraded if silent upgrades are allowed, otherwise |
| 726 | // they get disabled. |
| 727 | if (allow_silent_upgrade) { |
| 728 | old->set_being_upgraded(true); |
| 729 | extension->set_being_upgraded(true); |
| 730 | } |
| 731 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 732 | // To upgrade an extension in place, unload the old one and |
| 733 | // then load the new one. |
| 734 | UnloadExtension(old->id()); |
| 735 | old = NULL; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 736 | |
[email protected] | b24d831 | 2009-08-27 06:47:46 | [diff] [blame] | 737 | if (!allow_silent_upgrade) { |
[email protected] | 6d27a7b | 2009-12-18 23:25:45 | [diff] [blame] | 738 | // Extension has changed permissions significantly. Disable it. We |
| 739 | // send a notification below. |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 740 | extension_prefs_->SetExtensionState(extension, Extension::DISABLED); |
[email protected] | 7d84586 | 2010-01-04 21:28:57 | [diff] [blame] | 741 | extension_prefs_->SetShowInstallWarningOnEnable(extension, true); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 742 | } |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 743 | } else { |
| 744 | // We already have the extension of the same or older version. |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 745 | std::string error_message("Duplicate extension load attempt: "); |
| 746 | error_message += extension->id(); |
| 747 | LOG(WARNING) << error_message; |
| 748 | ReportExtensionLoadError(extension->path(), |
| 749 | error_message, |
| 750 | NotificationType::EXTENSION_OVERINSTALL_ERROR, |
| 751 | false); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 752 | return; |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 753 | } |
[email protected] | ba74f35 | 2009-06-11 18:54:45 | [diff] [blame] | 754 | } |
[email protected] | 86a27407 | 2009-06-11 02:06:45 | [diff] [blame] | 755 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 756 | switch (extension_prefs_->GetExtensionState(extension->id())) { |
| 757 | case Extension::ENABLED: |
| 758 | extensions_.push_back(scoped_extension.release()); |
| 759 | |
[email protected] | aeb53b3 | 2009-10-29 07:34:45 | [diff] [blame] | 760 | // We delay starting up the browser event router until at least one |
| 761 | // extension that needs it is loaded. |
| 762 | if (extension->HasApiPermission(Extension::kTabPermission)) { |
| 763 | ExtensionBrowserEventRouter::GetInstance()->Init(); |
| 764 | } |
[email protected] | 840b0db | 2009-11-20 03:00:38 | [diff] [blame] | 765 | if (extension->HasApiPermission(Extension::kBookmarkPermission)) { |
| 766 | ExtensionBookmarkEventRouter::GetSingleton()->Observe( |
| 767 | profile_->GetBookmarkModel()); |
| 768 | } |
[email protected] | aeb53b3 | 2009-10-29 07:34:45 | [diff] [blame] | 769 | |
[email protected] | 62d30f4 | 2009-10-01 22:36:06 | [diff] [blame] | 770 | NotifyExtensionLoaded(extension); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 771 | |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 772 | ExtensionDOMUI::RegisterChromeURLOverrides(profile_, |
| 773 | extension->GetChromeURLOverrides()); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 774 | break; |
| 775 | case Extension::DISABLED: |
[email protected] | 6d27a7b | 2009-12-18 23:25:45 | [diff] [blame] | 776 | disabled_extensions_.push_back(scoped_extension.release()); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 777 | NotificationService::current()->Notify( |
| 778 | NotificationType::EXTENSION_UPDATE_DISABLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 779 | Source<Profile>(profile_), |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 780 | Details<Extension>(extension)); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 781 | break; |
| 782 | default: |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 783 | NOTREACHED(); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 784 | break; |
[email protected] | 811f343 | 2009-07-25 19:38:21 | [diff] [blame] | 785 | } |
[email protected] | e72e8eb8 | 2009-06-18 17:21:51 | [diff] [blame] | 786 | } |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 787 | |
[email protected] | 1e8c93f | 2010-02-08 22:58:31 | [diff] [blame] | 788 | extension->set_being_upgraded(false); |
| 789 | |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 790 | UpdateActiveExtensionsInCrashReporter(); |
| 791 | } |
| 792 | |
| 793 | void ExtensionsService::UpdateActiveExtensionsInCrashReporter() { |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 794 | std::set<std::string> extension_ids; |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 795 | for (size_t i = 0; i < extensions_.size(); ++i) { |
| 796 | if (!extensions_[i]->IsTheme()) |
[email protected] | c886596 | 2009-12-16 07:47:39 | [diff] [blame] | 797 | extension_ids.insert(extensions_[i]->id()); |
[email protected] | aab98a5 | 2009-12-02 03:22:35 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | child_process_logging::SetActiveExtensions(extension_ids); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 801 | } |
| 802 | |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 803 | void ExtensionsService::OnExtensionInstalled(Extension* extension, |
| 804 | bool allow_privilege_increase) { |
[email protected] | b6ab96d | 2009-08-20 18:58:19 | [diff] [blame] | 805 | extension_prefs_->OnExtensionInstalled(extension); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 806 | |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 807 | // If the extension is a theme, tell the profile (and therefore ThemeProvider) |
| 808 | // to apply it. |
| 809 | if (extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 810 | NotificationService::current()->Notify( |
| 811 | NotificationType::THEME_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 812 | Source<Profile>(profile_), |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 813 | Details<Extension>(extension)); |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 814 | } else { |
| 815 | NotificationService::current()->Notify( |
| 816 | NotificationType::EXTENSION_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 817 | Source<Profile>(profile_), |
[email protected] | 9197f3b | 2009-06-02 00:49:27 | [diff] [blame] | 818 | Details<Extension>(extension)); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 819 | } |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 820 | |
| 821 | // Also load the extension. |
[email protected] | 2a40953 | 2009-08-28 19:39:44 | [diff] [blame] | 822 | OnExtensionLoaded(extension, allow_privilege_increase); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 823 | } |
| 824 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 825 | void ExtensionsService::OnExtensionOverinstallAttempted(const std::string& id) { |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 826 | Extension* extension = GetExtensionById(id, false); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 827 | if (extension && extension->IsTheme()) { |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 828 | NotificationService::current()->Notify( |
| 829 | NotificationType::THEME_INSTALLED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 830 | Source<Profile>(profile_), |
[email protected] | 9ceb0734 | 2009-07-26 04:09:23 | [diff] [blame] | 831 | Details<Extension>(extension)); |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 832 | } else { |
| 833 | NotificationService::current()->Notify( |
| 834 | NotificationType::NO_THEME_DETECTED, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 835 | Source<Profile>(profile_), |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 836 | NotificationService::NoDetails()); |
[email protected] | 4a19063 | 2009-05-09 01:07:42 | [diff] [blame] | 837 | } |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 838 | } |
| 839 | |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 840 | Extension* ExtensionsService::GetExtensionByIdInternal(const std::string& id, |
| 841 | bool include_enabled, |
| 842 | bool include_disabled) { |
[email protected] | e957fe5 | 2009-06-23 16:51:05 | [diff] [blame] | 843 | std::string lowercase_id = StringToLowerASCII(id); |
[email protected] | 0c6da50 | 2009-08-14 22:32:39 | [diff] [blame] | 844 | if (include_enabled) { |
| 845 | for (ExtensionList::const_iterator iter = extensions_.begin(); |
| 846 | iter != extensions_.end(); ++iter) { |
| 847 | if ((*iter)->id() == lowercase_id) |
| 848 | return *iter; |
| 849 | } |
| 850 | } |
| 851 | if (include_disabled) { |
| 852 | for (ExtensionList::const_iterator iter = disabled_extensions_.begin(); |
| 853 | iter != disabled_extensions_.end(); ++iter) { |
| 854 | if ((*iter)->id() == lowercase_id) |
| 855 | return *iter; |
| 856 | } |
[email protected] | ce5c450 | 2009-05-06 16:46:11 | [diff] [blame] | 857 | } |
| 858 | return NULL; |
| 859 | } |
| 860 | |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 861 | Extension* ExtensionsService::GetExtensionByURL(const GURL& url) { |
| 862 | std::string host = url.host(); |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 863 | return GetExtensionById(host, false); |
[email protected] | 9f1087e | 2009-06-15 17:29:32 | [diff] [blame] | 864 | } |
| 865 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 866 | void ExtensionsService::ClearProvidersForTesting() { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 867 | ChromeThread::PostTask( |
| 868 | ChromeThread::FILE, FROM_HERE, |
| 869 | NewRunnableMethod( |
| 870 | backend_.get(), &ExtensionsServiceBackend::ClearProvidersForTesting)); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | void ExtensionsService::SetProviderForTesting( |
| 874 | Extension::Location location, ExternalExtensionProvider* test_provider) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 875 | ChromeThread::PostTask( |
| 876 | ChromeThread::FILE, FROM_HERE, |
| 877 | NewRunnableMethod( |
| 878 | backend_.get(), &ExtensionsServiceBackend::SetProviderForTesting, |
| 879 | location, test_provider)); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 880 | } |
| 881 | |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 882 | void ExtensionsService::OnExternalExtensionFound(const std::string& id, |
| 883 | const std::string& version, |
| 884 | const FilePath& path, |
| 885 | Extension::Location location) { |
| 886 | // Before even bothering to unpack, check and see if we already have this |
[email protected] | 4c96793 | 2009-07-31 01:15:49 | [diff] [blame] | 887 | // version. This is important because these extensions are going to get |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 888 | // installed on every startup. |
[email protected] | 61b41161 | 2009-11-10 23:17:41 | [diff] [blame] | 889 | Extension* existing = GetExtensionById(id, true); |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 890 | scoped_ptr<Version> other(Version::GetVersionFromString(version)); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 891 | if (existing) { |
[email protected] | a3a63ff8 | 2009-08-04 06:44:11 | [diff] [blame] | 892 | switch (existing->version()->CompareTo(*other)) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 893 | case -1: // existing version is older, we should upgrade |
| 894 | break; |
| 895 | case 0: // existing version is same, do nothing |
| 896 | return; |
| 897 | case 1: // existing version is newer, uh-oh |
| 898 | LOG(WARNING) << "Found external version of extension " << id |
| 899 | << "that is older than current version. Current version " |
| 900 | << "is: " << existing->VersionString() << ". New version " |
| 901 | << "is: " << version << ". Keeping current version."; |
| 902 | return; |
| 903 | } |
| 904 | } |
| 905 | |
[email protected] | 6dfbbf8 | 2010-03-12 23:09:16 | [diff] [blame] | 906 | scoped_refptr<CrxInstaller> installer( |
| 907 | new CrxInstaller(install_directory_, |
| 908 | this, // frontend |
| 909 | NULL)); // no client (silent install) |
| 910 | installer->set_install_source(location); |
| 911 | installer->set_expected_id(id); |
| 912 | installer->set_allow_privilege_increase(true); |
| 913 | installer->InstallCrx(path); |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 914 | } |
| 915 | |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 916 | void ExtensionsService::ReportExtensionLoadError( |
| 917 | const FilePath& extension_path, |
| 918 | const std::string &error, |
| 919 | NotificationType type, |
| 920 | bool be_noisy) { |
| 921 | NotificationService* service = NotificationService::current(); |
| 922 | service->Notify(type, |
[email protected] | 24e7a9d | 2009-11-04 11:11:34 | [diff] [blame] | 923 | Source<Profile>(profile_), |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 924 | Details<const std::string>(&error)); |
| 925 | |
| 926 | // TODO(port): note that this isn't guaranteed to work properly on Linux. |
[email protected] | 99efb7b1 | 2009-12-18 02:39:16 | [diff] [blame] | 927 | std::string path_str = WideToUTF8(extension_path.ToWStringHack()); |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 928 | std::string message = StringPrintf("Could not load extension from '%s'. %s", |
| 929 | path_str.c_str(), error.c_str()); |
| 930 | ExtensionErrorReporter::GetInstance()->ReportError(message, be_noisy); |
| 931 | } |
| 932 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 933 | void ExtensionsService::Observe(NotificationType type, |
| 934 | const NotificationSource& source, |
| 935 | const NotificationDetails& details) { |
| 936 | switch (type.value) { |
| 937 | case NotificationType::EXTENSION_HOST_DID_STOP_LOADING: { |
| 938 | ExtensionHost* host = Details<ExtensionHost>(details).ptr(); |
| 939 | OrphanedDevTools::iterator iter = |
| 940 | orphaned_dev_tools_.find(host->extension()->id()); |
| 941 | if (iter == orphaned_dev_tools_.end()) |
| 942 | return; |
| 943 | |
| 944 | DevToolsManager::GetInstance()->AttachClientHost( |
| 945 | iter->second, host->render_view_host()); |
| 946 | orphaned_dev_tools_.erase(iter); |
| 947 | break; |
| 948 | } |
| 949 | |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 950 | case NotificationType::EXTENSION_PROCESS_TERMINATED: { |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 951 | DCHECK_EQ(profile_, Source<Profile>(source).ptr()); |
[email protected] | a4ed628 | 2009-12-14 20:51:16 | [diff] [blame] | 952 | |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 953 | // Unload the entire extension. We want it to be in a consistent state: |
| 954 | // either fully working or not loaded at all, but never half-crashed. |
[email protected] | 597cceb | 2010-03-22 16:27:01 | [diff] [blame] | 955 | UnloadExtension(Details<ExtensionHost>(details).ptr()->extension()->id()); |
[email protected] | 31f7726 | 2009-12-02 20:48:53 | [diff] [blame] | 956 | break; |
| 957 | } |
| 958 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 959 | default: |
| 960 | NOTREACHED() << "Unexpected notification type."; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 965 | // ExtensionsServicesBackend |
| 966 | |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 967 | ExtensionsServiceBackend::ExtensionsServiceBackend( |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 968 | const FilePath& install_directory) |
[email protected] | 0c7bc4b | 2009-05-30 01:47:08 | [diff] [blame] | 969 | : frontend_(NULL), |
| 970 | install_directory_(install_directory), |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 971 | alert_on_error_(false) { |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 972 | // TODO(aa): This ends up doing blocking IO on the UI thread because it reads |
| 973 | // pref data in the ctor and that is called on the UI thread. Would be better |
| 974 | // to re-read data each time we list external extensions, anyway. |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 975 | external_extension_providers_[Extension::EXTERNAL_PREF] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 976 | linked_ptr<ExternalExtensionProvider>( |
[email protected] | 27b985d | 2009-06-25 17:53:15 | [diff] [blame] | 977 | new ExternalPrefExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 978 | #if defined(OS_WIN) |
| 979 | external_extension_providers_[Extension::EXTERNAL_REGISTRY] = |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 980 | linked_ptr<ExternalExtensionProvider>( |
| 981 | new ExternalRegistryExtensionProvider()); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 982 | #endif |
| 983 | } |
| 984 | |
| 985 | ExtensionsServiceBackend::~ExtensionsServiceBackend() { |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 986 | } |
| 987 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 988 | void ExtensionsServiceBackend::LoadSingleExtension( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 989 | const FilePath& path_in, scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 990 | frontend_ = frontend; |
| 991 | |
| 992 | // Explicit UI loads are always noisy. |
| 993 | alert_on_error_ = true; |
| 994 | |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 995 | FilePath extension_path = path_in; |
[email protected] | f36fa4fb | 2009-06-19 18:23:50 | [diff] [blame] | 996 | file_util::AbsolutePath(&extension_path); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 997 | |
| 998 | LOG(INFO) << "Loading single extension from " << |
[email protected] | 99efb7b1 | 2009-12-18 02:39:16 | [diff] [blame] | 999 | extension_path.BaseName().value(); |
[email protected] | bf24d2c | 2009-02-24 23:07:45 | [diff] [blame] | 1000 | |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 1001 | std::string error; |
| 1002 | Extension* extension = extension_file_util::LoadExtension( |
| 1003 | extension_path, |
| 1004 | false, // Don't require id |
| 1005 | &error); |
| 1006 | |
| 1007 | if (!extension) { |
| 1008 | ReportExtensionLoadError(extension_path, error); |
| 1009 | return; |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 1010 | } |
[email protected] | ab6f2b2 | 2009-07-28 23:28:37 | [diff] [blame] | 1011 | |
| 1012 | extension->set_location(Extension::LOAD); |
[email protected] | e8c729a | 2010-03-09 19:55:19 | [diff] [blame] | 1013 | |
| 1014 | // Report this as an installed extension so that it gets remembered in the |
| 1015 | // prefs. |
| 1016 | ChromeThread::PostTask( |
| 1017 | ChromeThread::UI, FROM_HERE, |
| 1018 | NewRunnableMethod(frontend_, &ExtensionsService::OnExtensionInstalled, |
| 1019 | extension, true)); |
[email protected] | 0877fd9 | 2009-02-03 16:34:06 | [diff] [blame] | 1020 | } |
| 1021 | |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 1022 | void ExtensionsServiceBackend::ReportExtensionLoadError( |
[email protected] | cc5da33 | 2009-03-04 08:02:51 | [diff] [blame] | 1023 | const FilePath& extension_path, const std::string &error) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1024 | ChromeThread::PostTask( |
| 1025 | ChromeThread::UI, FROM_HERE, |
| 1026 | NewRunnableMethod( |
| 1027 | frontend_, |
[email protected] | d11c8e9 | 2009-10-20 23:26:40 | [diff] [blame] | 1028 | &ExtensionsService::ReportExtensionLoadError, extension_path, |
| 1029 | error, NotificationType::EXTENSION_INSTALL_ERROR, alert_on_error_)); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 1030 | } |
| 1031 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1032 | bool ExtensionsServiceBackend::LookupExternalExtension( |
| 1033 | const std::string& id, Version** version, Extension::Location* location) { |
| 1034 | scoped_ptr<Version> extension_version; |
| 1035 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 1036 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1037 | const ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1038 | extension_version.reset(provider->RegisteredVersion(id, location)); |
| 1039 | if (extension_version.get()) { |
| 1040 | if (version) |
| 1041 | *version = extension_version.release(); |
| 1042 | return true; |
| 1043 | } |
| 1044 | } |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1048 | // Some extensions will autoupdate themselves externally from Chrome. These |
| 1049 | // are typically part of some larger client application package. To support |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1050 | // these, the extension will register its location in the the preferences file |
| 1051 | // (and also, on Windows, in the registry) and this code will periodically |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1052 | // check that location for a .crx file, which it will then install locally if |
| 1053 | // a new version is available. |
| 1054 | void ExtensionsServiceBackend::CheckForExternalUpdates( |
[email protected] | 894bb50 | 2009-05-21 22:39:57 | [diff] [blame] | 1055 | std::set<std::string> ids_to_ignore, |
| 1056 | scoped_refptr<ExtensionsService> frontend) { |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1057 | // Note that this installation is intentionally silent (since it didn't |
| 1058 | // go through the front-end). Extensions that are registered in this |
| 1059 | // way are effectively considered 'pre-bundled', and so implicitly |
| 1060 | // trusted. In general, if something has HKLM or filesystem access, |
| 1061 | // they could install an extension manually themselves anyway. |
| 1062 | alert_on_error_ = false; |
| 1063 | frontend_ = frontend; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1064 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1065 | // Ask each external extension provider to give us a call back for each |
| 1066 | // extension they know about. See OnExternalExtensionFound. |
| 1067 | for (ProviderMap::const_iterator i = external_extension_providers_.begin(); |
| 1068 | i != external_extension_providers_.end(); ++i) { |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1069 | ExternalExtensionProvider* provider = i->second.get(); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1070 | provider->VisitRegisteredExtension(this, ids_to_ignore); |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1071 | } |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1072 | } |
| 1073 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1074 | void ExtensionsServiceBackend::CheckExternalUninstall( |
| 1075 | scoped_refptr<ExtensionsService> frontend, const std::string& id, |
| 1076 | Extension::Location location) { |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1077 | // Check if the providers know about this extension. |
| 1078 | ProviderMap::const_iterator i = external_extension_providers_.find(location); |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1079 | if (i == external_extension_providers_.end()) { |
| 1080 | NOTREACHED() << "CheckExternalUninstall called for non-external extension " |
| 1081 | << location; |
| 1082 | return; |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1083 | } |
[email protected] | 25b3433 | 2009-06-05 21:53:19 | [diff] [blame] | 1084 | |
[email protected] | ae09ca6 | 2009-08-21 19:46:46 | [diff] [blame] | 1085 | scoped_ptr<Version> version; |
| 1086 | version.reset(i->second->RegisteredVersion(id, NULL)); |
| 1087 | if (version.get()) |
| 1088 | return; // Yup, known extension, don't uninstall. |
| 1089 | |
| 1090 | // This is an external extension that we don't have registered. Uninstall. |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1091 | ChromeThread::PostTask( |
| 1092 | ChromeThread::UI, FROM_HERE, |
| 1093 | NewRunnableMethod( |
| 1094 | frontend.get(), &ExtensionsService::UninstallExtension, id, true)); |
[email protected] | b0beaa66 | 2009-02-26 00:04:15 | [diff] [blame] | 1095 | } |
| 1096 | |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1097 | void ExtensionsServiceBackend::ClearProvidersForTesting() { |
| 1098 | external_extension_providers_.clear(); |
| 1099 | } |
| 1100 | |
| 1101 | void ExtensionsServiceBackend::SetProviderForTesting( |
| 1102 | Extension::Location location, |
| 1103 | ExternalExtensionProvider* test_provider) { |
| 1104 | DCHECK(test_provider); |
[email protected] | da50530a | 2009-06-15 17:43:01 | [diff] [blame] | 1105 | external_extension_providers_[location] = |
| 1106 | linked_ptr<ExternalExtensionProvider>(test_provider); |
[email protected] | a1257b1 | 2009-06-12 02:51:34 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | void ExtensionsServiceBackend::OnExternalExtensionFound( |
[email protected] | 7577a5c5 | 2009-07-30 06:21:58 | [diff] [blame] | 1110 | const std::string& id, const Version* version, const FilePath& path, |
| 1111 | Extension::Location location) { |
[email protected] | 95d2919 | 2009-10-30 01:49:06 | [diff] [blame] | 1112 | ChromeThread::PostTask( |
| 1113 | ChromeThread::UI, FROM_HERE, |
| 1114 | NewRunnableMethod( |
| 1115 | frontend_, &ExtensionsService::OnExternalExtensionFound, id, |
| 1116 | version->GetString(), path, location)); |
[email protected] | cc65591 | 2009-01-29 23:19:19 | [diff] [blame] | 1117 | } |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1118 | |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 1119 | void ExtensionsServiceBackend::ReloadExtensionManifests( |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1120 | ExtensionPrefs::ExtensionsInfo* extensions_to_reload, |
| 1121 | base::TimeTicks start_time, |
| 1122 | scoped_refptr<ExtensionsService> frontend) { |
| 1123 | frontend_ = frontend; |
| 1124 | |
| 1125 | for (size_t i = 0; i < extensions_to_reload->size(); ++i) { |
| 1126 | ExtensionInfo* info = extensions_to_reload->at(i).get(); |
[email protected] | 2111b1a | 2010-03-12 18:12:44 | [diff] [blame] | 1127 | if (!ShouldReloadExtensionManifest(*info)) |
[email protected] | c6d474f8 | 2009-12-16 21:11:06 | [diff] [blame] | 1128 | continue; |
| 1129 | |
| 1130 | // We need to reload original manifest in order to localize properly. |
| 1131 | std::string error; |
| 1132 | scoped_ptr<Extension> extension(extension_file_util::LoadExtension( |
| 1133 | info->extension_path, false, &error)); |
| 1134 | |
| 1135 | if (extension.get()) |
| 1136 | extensions_to_reload->at(i)->extension_manifest.reset( |
| 1137 | static_cast<DictionaryValue*>( |
| 1138 | extension->manifest_value()->DeepCopy())); |
| 1139 | } |
| 1140 | |
| 1141 | // Finish installing on UI thread. |
| 1142 | ChromeThread::PostTask( |
| 1143 | ChromeThread::UI, FROM_HERE, |
| 1144 | NewRunnableMethod( |
| 1145 | frontend_, |
| 1146 | &ExtensionsService::ContinueLoadAllExtensions, |
| 1147 | extensions_to_reload, |
| 1148 | start_time, |
| 1149 | true)); |
| 1150 | } |