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