external_provider_impl.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 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/external_provider_impl.h" 6 7#include "base/command_line.h" 8#include "base/file_path.h" 9#include "base/logging.h" 10#include "base/memory/linked_ptr.h" 11#include "base/metrics/field_trial.h" 12#include "base/path_service.h" 13#include "base/string_util.h" 14#include "base/values.h" 15#include "base/version.h" 16#include "chrome/browser/browser_process.h" 17#include "chrome/browser/extensions/extension_service.h" 18#include "chrome/browser/extensions/external_policy_loader.h" 19#include "chrome/browser/extensions/external_pref_loader.h" 20#include "chrome/browser/extensions/external_provider_interface.h" 21#include "chrome/browser/profiles/profile.h" 22#include "chrome/common/chrome_paths.h" 23#include "chrome/common/chrome_switches.h" 24#include "chrome/common/pref_names.h" 25#include "content/public/browser/browser_thread.h" 26#include "ui/base/l10n/l10n_util.h" 27 28#if defined(OS_CHROMEOS) 29#include "chrome/browser/chromeos/login/user_manager.h" 30#include "chrome/browser/policy/app_pack_updater.h" 31#include "chrome/browser/policy/browser_policy_connector.h" 32#endif 33 34#if !defined(OS_CHROMEOS) 35#include "chrome/browser/extensions/default_apps.h" 36#endif 37 38#if defined(OS_WIN) 39#include "chrome/browser/extensions/external_registry_loader_win.h" 40#endif 41 42using content::BrowserThread; 43 44namespace extensions { 45 46// Constants for keeping track of extension preferences in a dictionary. 47const char ExternalProviderImpl::kExternalCrx[] = "external_crx"; 48const char ExternalProviderImpl::kExternalVersion[] = 49 "external_version"; 50const char ExternalProviderImpl::kExternalUpdateUrl[] = 51 "external_update_url"; 52const char ExternalProviderImpl::kSupportedLocales[] = 53 "supported_locales"; 54const char ExternalProviderImpl::kIsBookmarkApp[] = 55 "is_bookmark_app"; 56 57ExternalProviderImpl::ExternalProviderImpl( 58 VisitorInterface* service, 59 ExternalLoader* loader, 60 Extension::Location crx_location, 61 Extension::Location download_location, 62 int creation_flags) 63 : crx_location_(crx_location), 64 download_location_(download_location), 65 service_(service), 66 prefs_(NULL), 67 ready_(false), 68 loader_(loader), 69 creation_flags_(creation_flags), 70 auto_acknowledge_(false) { 71 loader_->Init(this); 72} 73 74ExternalProviderImpl::~ExternalProviderImpl() { 75 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 76 loader_->OwnerShutdown(); 77} 78 79void ExternalProviderImpl::VisitRegisteredExtension() { 80 // The loader will call back to SetPrefs. 81 loader_->StartLoading(); 82} 83 84void ExternalProviderImpl::SetPrefs(DictionaryValue* prefs) { 85 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 86 87 // Check if the service is still alive. It is possible that it went 88 // away while |loader_| was working on the FILE thread. 89 if (!service_) return; 90 91 prefs_.reset(prefs); 92 ready_ = true; // Queries for extensions are allowed from this point. 93 94 // Set of unsupported extensions that need to be deleted from prefs_. 95 std::set<std::string> unsupported_extensions; 96 97 // Notify ExtensionService about all the extensions this provider has. 98 for (DictionaryValue::key_iterator i = prefs_->begin_keys(); 99 i != prefs_->end_keys(); ++i) { 100 const std::string& extension_id = *i; 101 DictionaryValue* extension; 102 103 if (!Extension::IdIsValid(extension_id)) { 104 LOG(WARNING) << "Malformed extension dictionary: key " 105 << extension_id.c_str() << " is not a valid id."; 106 continue; 107 } 108 109 if (!prefs_->GetDictionaryWithoutPathExpansion(extension_id, &extension)) { 110 LOG(WARNING) << "Malformed extension dictionary: key " 111 << extension_id.c_str() 112 << " has a value that is not a dictionary."; 113 continue; 114 } 115 116 FilePath::StringType external_crx; 117 Value* external_version_value; 118 std::string external_version; 119 std::string external_update_url; 120 121 bool has_external_crx = extension->GetString(kExternalCrx, &external_crx); 122 123 bool has_external_version = false; 124 if (extension->Get(kExternalVersion, &external_version_value)) { 125 if (external_version_value->IsType(Value::TYPE_STRING)) { 126 external_version_value->GetAsString(&external_version); 127 has_external_version = true; 128 } else { 129 LOG(WARNING) << "Malformed extension dictionary for extension: " 130 << extension_id.c_str() << ". " << kExternalVersion 131 << " value must be a string."; 132 continue; 133 } 134 } 135 136 bool has_external_update_url = extension->GetString(kExternalUpdateUrl, 137 &external_update_url); 138 if (has_external_crx != has_external_version) { 139 LOG(WARNING) << "Malformed extension dictionary for extension: " 140 << extension_id.c_str() << ". " << kExternalCrx 141 << " and " << kExternalVersion << " must be used together."; 142 continue; 143 } 144 145 if (has_external_crx == has_external_update_url) { 146 LOG(WARNING) << "Malformed extension dictionary for extension: " 147 << extension_id.c_str() << ". Exactly one of the " 148 << "followng keys should be used: " << kExternalCrx 149 << ", " << kExternalUpdateUrl << "."; 150 continue; 151 } 152 153 // Check that extension supports current browser locale. 154 ListValue* supported_locales = NULL; 155 if (extension->GetList(kSupportedLocales, &supported_locales)) { 156 std::vector<std::string> browser_locales; 157 l10n_util::GetParentLocales(g_browser_process->GetApplicationLocale(), 158 &browser_locales); 159 160 size_t num_locales = supported_locales->GetSize(); 161 bool locale_supported = false; 162 for (size_t j = 0; j < num_locales; j++) { 163 std::string current_locale; 164 if (supported_locales->GetString(j, ¤t_locale) && 165 l10n_util::IsValidLocaleSyntax(current_locale)) { 166 current_locale = l10n_util::NormalizeLocale(current_locale); 167 if (std::find(browser_locales.begin(), browser_locales.end(), 168 current_locale) != browser_locales.end()) { 169 locale_supported = true; 170 break; 171 } 172 } else { 173 LOG(WARNING) << "Unrecognized locale '" << current_locale 174 << "' found as supported locale for extension: " 175 << extension_id; 176 } 177 } 178 179 if (!locale_supported) { 180 unsupported_extensions.insert(extension_id); 181 LOG(INFO) << "Skip installing (or uninstall) external extension: " 182 << extension_id << " because the extension doesn't support " 183 << "the browser locale."; 184 continue; 185 } 186 } 187 188 int creation_flags = creation_flags_; 189 bool is_bookmark_app; 190 if (extension->GetBoolean(kIsBookmarkApp, &is_bookmark_app) && 191 is_bookmark_app) { 192 creation_flags |= Extension::FROM_BOOKMARK; 193 } 194 195 if (has_external_crx) { 196 if (crx_location_ == Extension::INVALID) { 197 LOG(WARNING) << "This provider does not support installing external " 198 << "extensions from crx files."; 199 continue; 200 } 201 if (external_crx.find(FilePath::kParentDirectory) != 202 base::StringPiece::npos) { 203 LOG(WARNING) << "Path traversal not allowed in path: " 204 << external_crx.c_str(); 205 continue; 206 } 207 208 // If the path is relative, and the provider has a base path, 209 // build the absolute path to the crx file. 210 FilePath path(external_crx); 211 if (!path.IsAbsolute()) { 212 FilePath base_path = loader_->GetBaseCrxFilePath(); 213 if (base_path.empty()) { 214 LOG(WARNING) << "File path " << external_crx.c_str() 215 << " is relative. An absolute path is required."; 216 continue; 217 } 218 path = base_path.Append(external_crx); 219 } 220 221 Version version(external_version); 222 if (!version.IsValid()) { 223 LOG(WARNING) << "Malformed extension dictionary for extension: " 224 << extension_id.c_str() << ". Invalid version string \"" 225 << external_version << "\"."; 226 continue; 227 } 228 service_->OnExternalExtensionFileFound(extension_id, &version, path, 229 crx_location_, creation_flags, 230 auto_acknowledge_); 231 } else { // if (has_external_update_url) 232 CHECK(has_external_update_url); // Checking of keys above ensures this. 233 if (download_location_ == Extension::INVALID) { 234 LOG(WARNING) << "This provider does not support installing external " 235 << "extensions from update URLs."; 236 continue; 237 } 238 GURL update_url(external_update_url); 239 if (!update_url.is_valid()) { 240 LOG(WARNING) << "Malformed extension dictionary for extension: " 241 << extension_id.c_str() << ". Key " << kExternalUpdateUrl 242 << " has value \"" << external_update_url 243 << "\", which is not a valid URL."; 244 continue; 245 } 246 service_->OnExternalExtensionUpdateUrlFound( 247 extension_id, update_url, download_location_); 248 } 249 } 250 251 for (std::set<std::string>::iterator it = unsupported_extensions.begin(); 252 it != unsupported_extensions.end(); ++it) { 253 // Remove extension for the list of know external extensions. The extension 254 // will be uninstalled later because provider doesn't provide it anymore. 255 prefs_->Remove(*it, NULL); 256 } 257 258 service_->OnExternalProviderReady(this); 259} 260 261void ExternalProviderImpl::ServiceShutdown() { 262 service_ = NULL; 263} 264 265bool ExternalProviderImpl::IsReady() const { 266 return ready_; 267} 268 269bool ExternalProviderImpl::HasExtension( 270 const std::string& id) const { 271 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 272 CHECK(prefs_.get()); 273 CHECK(ready_); 274 return prefs_->HasKey(id); 275} 276 277bool ExternalProviderImpl::GetExtensionDetails( 278 const std::string& id, Extension::Location* location, 279 scoped_ptr<Version>* version) const { 280 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 281 CHECK(prefs_.get()); 282 CHECK(ready_); 283 DictionaryValue* extension = NULL; 284 if (!prefs_->GetDictionary(id, &extension)) 285 return false; 286 287 Extension::Location loc = Extension::INVALID; 288 if (extension->HasKey(kExternalUpdateUrl)) { 289 loc = download_location_; 290 291 } else if (extension->HasKey(kExternalCrx)) { 292 loc = crx_location_; 293 294 std::string external_version; 295 if (!extension->GetString(kExternalVersion, &external_version)) 296 return false; 297 298 if (version) 299 version->reset(new Version(external_version)); 300 301 } else { 302 NOTREACHED(); // Chrome should not allow prefs to get into this state. 303 return false; 304 } 305 306 if (location) 307 *location = loc; 308 309 return true; 310} 311 312// static 313void ExternalProviderImpl::CreateExternalProviders( 314 VisitorInterface* service, 315 Profile* profile, 316 ProviderCollection* provider_list) { 317 // Policies are mandatory so they can't be skipped with command line flag. 318 provider_list->push_back( 319 linked_ptr<ExternalProviderInterface>( 320 new ExternalProviderImpl( 321 service, 322 new ExternalPolicyLoader(profile), 323 Extension::INVALID, 324 Extension::EXTERNAL_POLICY_DOWNLOAD, 325 Extension::NO_FLAGS))); 326 327 // In tests don't install extensions from default external sources. 328 // It would only slowdown tests and make them flaky. 329 if (CommandLine::ForCurrentProcess()->HasSwitch( 330 switches::kDisableDefaultApps)) 331 return; 332 333 // On Mac OS, items in /Library/... should be written by the superuser. 334 // Check that all components of the path are writable by root only. 335 ExternalPrefLoader::Options check_admin_permissions_on_mac; 336#if defined(OS_MACOSX) 337 check_admin_permissions_on_mac = 338 ExternalPrefLoader::ENSURE_PATH_CONTROLLED_BY_ADMIN; 339#else 340 check_admin_permissions_on_mac = ExternalPrefLoader::NONE; 341#endif 342 343 bool is_chromeos_demo_session = false; 344 int bundled_extension_creation_flags = Extension::NO_FLAGS; 345#if defined(OS_CHROMEOS) 346 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); 347 is_chromeos_demo_session = 348 user_manager && user_manager->IsLoggedInAsDemoUser() && 349 g_browser_process->browser_policy_connector()->GetDeviceMode() == 350 policy::DEVICE_MODE_KIOSK; 351 bundled_extension_creation_flags = Extension::FROM_WEBSTORE | 352 Extension::WAS_INSTALLED_BY_DEFAULT; 353#endif 354 355 if (!is_chromeos_demo_session) { 356 provider_list->push_back( 357 linked_ptr<ExternalProviderInterface>( 358 new ExternalProviderImpl( 359 service, 360 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS, 361 check_admin_permissions_on_mac), 362 Extension::EXTERNAL_PREF, 363 Extension::EXTERNAL_PREF_DOWNLOAD, 364 bundled_extension_creation_flags))); 365 } 366 367#if defined(OS_CHROMEOS) || defined (OS_MACOSX) 368 // Define a per-user source of external extensions. 369 // On Chrome OS, this serves as a source for OEM customization. 370 provider_list->push_back( 371 linked_ptr<ExternalProviderInterface>( 372 new ExternalProviderImpl( 373 service, 374 new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS, 375 ExternalPrefLoader::NONE), 376 Extension::EXTERNAL_PREF, 377 Extension::EXTERNAL_PREF_DOWNLOAD, 378 Extension::NO_FLAGS))); 379#endif 380#if defined(OS_WIN) 381 provider_list->push_back( 382 linked_ptr<ExternalProviderInterface>( 383 new ExternalProviderImpl( 384 service, 385 new ExternalRegistryLoader, 386 Extension::EXTERNAL_REGISTRY, 387 Extension::INVALID, 388 Extension::NO_FLAGS))); 389#endif 390 391#if defined(OS_LINUX) 392 provider_list->push_back( 393 linked_ptr<ExternalProviderInterface>( 394 new ExternalProviderImpl( 395 service, 396 new ExternalPrefLoader(chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS, 397 ExternalPrefLoader::NONE), 398 Extension::EXTERNAL_PREF, 399 Extension::EXTERNAL_PREF_DOWNLOAD, 400 bundled_extension_creation_flags))); 401#endif 402 403#if !defined(OS_CHROMEOS) 404 // The default apps are installed as INTERNAL but use the external 405 // extension installer codeflow. 406 provider_list->push_back( 407 linked_ptr<ExternalProviderInterface>( 408 new default_apps::Provider( 409 profile, 410 service, 411 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS, 412 ExternalPrefLoader::NONE), 413 Extension::INTERNAL, 414 Extension::INVALID, 415 Extension::FROM_WEBSTORE | 416 Extension::WAS_INSTALLED_BY_DEFAULT))); 417#endif 418 419#if defined(OS_CHROMEOS) 420 policy::BrowserPolicyConnector* connector = 421 g_browser_process->browser_policy_connector(); 422 if (is_chromeos_demo_session && connector->GetAppPackUpdater()) { 423 provider_list->push_back( 424 linked_ptr<ExternalProviderInterface>( 425 new ExternalProviderImpl( 426 service, 427 connector->GetAppPackUpdater()->CreateExternalLoader(), 428 Extension::EXTERNAL_PREF, 429 Extension::INVALID, 430 Extension::NO_FLAGS))); 431 } 432#endif 433} 434 435} // namespace extensions 436