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