Additional tracing and histogram in Extensions startup.
[chromium-blink-merge.git] / chrome / browser / extensions / external_provider_impl.cc
blobf3122881c0af2f3125e81b935d67ddd48c7b4fc1
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.
5 #include "chrome/browser/extensions/external_provider_impl.h"
7 #include <set>
8 #include <vector>
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/strings/string_util.h"
16 #include "base/trace_event/trace_event.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_management.h"
22 #include "chrome/browser/extensions/extension_service.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/profiles/profile.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "components/crx_file/id_util.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "extensions/browser/extension_system.h"
32 #include "extensions/browser/external_provider_interface.h"
33 #include "extensions/common/extension.h"
34 #include "extensions/common/manifest.h"
35 #include "ui/base/l10n/l10n_util.h"
37 #if defined(OS_CHROMEOS)
38 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
39 #include "chrome/browser/chromeos/customization/customization_document.h"
40 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
41 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
42 #include "chrome/browser/chromeos/policy/device_local_account.h"
43 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
44 #include "chrome/browser/chromeos/profiles/profile_helper.h"
45 #include "components/user_manager/user.h"
46 #else
47 #include "chrome/browser/extensions/default_apps.h"
48 #endif
50 #if defined(OS_WIN)
51 #include "chrome/browser/extensions/external_registry_loader_win.h"
52 #endif
54 using content::BrowserThread;
56 namespace extensions {
58 // Constants for keeping track of extension preferences in a dictionary.
59 const char ExternalProviderImpl::kInstallParam[] = "install_parameter";
60 const char ExternalProviderImpl::kExternalCrx[] = "external_crx";
61 const char ExternalProviderImpl::kExternalVersion[] = "external_version";
62 const char ExternalProviderImpl::kExternalUpdateUrl[] = "external_update_url";
63 const char ExternalProviderImpl::kIsBookmarkApp[] = "is_bookmark_app";
64 const char ExternalProviderImpl::kIsFromWebstore[] = "is_from_webstore";
65 const char ExternalProviderImpl::kKeepIfPresent[] = "keep_if_present";
66 const char ExternalProviderImpl::kWasInstalledByOem[] = "was_installed_by_oem";
67 const char ExternalProviderImpl::kSupportedLocales[] = "supported_locales";
68 const char ExternalProviderImpl::kMayBeUntrusted[] = "may_be_untrusted";
70 ExternalProviderImpl::ExternalProviderImpl(
71 VisitorInterface* service,
72 const scoped_refptr<ExternalLoader>& loader,
73 Profile* profile,
74 Manifest::Location crx_location,
75 Manifest::Location download_location,
76 int creation_flags)
77 : crx_location_(crx_location),
78 download_location_(download_location),
79 service_(service),
80 ready_(false),
81 loader_(loader),
82 profile_(profile),
83 creation_flags_(creation_flags),
84 auto_acknowledge_(false),
85 install_immediately_(false) {
86 loader_->Init(this);
89 ExternalProviderImpl::~ExternalProviderImpl() {
90 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
91 loader_->OwnerShutdown();
94 void ExternalProviderImpl::VisitRegisteredExtension() {
95 // The loader will call back to SetPrefs.
96 loader_->StartLoading();
99 void ExternalProviderImpl::SetPrefs(base::DictionaryValue* prefs) {
100 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
102 // Check if the service is still alive. It is possible that it went
103 // away while |loader_| was working on the FILE thread.
104 if (!service_) return;
106 prefs_.reset(prefs);
107 ready_ = true; // Queries for extensions are allowed from this point.
109 // Set of unsupported extensions that need to be deleted from prefs_.
110 std::set<std::string> unsupported_extensions;
112 // Notify ExtensionService about all the extensions this provider has.
113 for (base::DictionaryValue::Iterator i(*prefs_); !i.IsAtEnd(); i.Advance()) {
114 const std::string& extension_id = i.key();
115 const base::DictionaryValue* extension = NULL;
117 if (!crx_file::id_util::IdIsValid(extension_id)) {
118 LOG(WARNING) << "Malformed extension dictionary: key "
119 << extension_id.c_str() << " is not a valid id.";
120 continue;
123 if (!i.value().GetAsDictionary(&extension)) {
124 LOG(WARNING) << "Malformed extension dictionary: key "
125 << extension_id.c_str()
126 << " has a value that is not a dictionary.";
127 continue;
130 base::FilePath::StringType external_crx;
131 const base::Value* external_version_value = NULL;
132 std::string external_version;
133 std::string external_update_url;
135 bool has_external_crx = extension->GetString(kExternalCrx, &external_crx);
137 bool has_external_version = false;
138 if (extension->Get(kExternalVersion, &external_version_value)) {
139 if (external_version_value->IsType(base::Value::TYPE_STRING)) {
140 external_version_value->GetAsString(&external_version);
141 has_external_version = true;
142 } else {
143 LOG(WARNING) << "Malformed extension dictionary for extension: "
144 << extension_id.c_str() << ". " << kExternalVersion
145 << " value must be a string.";
146 continue;
150 bool has_external_update_url = extension->GetString(kExternalUpdateUrl,
151 &external_update_url);
152 if (has_external_crx != has_external_version) {
153 LOG(WARNING) << "Malformed extension dictionary for extension: "
154 << extension_id.c_str() << ". " << kExternalCrx
155 << " and " << kExternalVersion << " must be used together.";
156 continue;
159 if (has_external_crx == has_external_update_url) {
160 LOG(WARNING) << "Malformed extension dictionary for extension: "
161 << extension_id.c_str() << ". Exactly one of the "
162 << "followng keys should be used: " << kExternalCrx
163 << ", " << kExternalUpdateUrl << ".";
164 continue;
167 // Check that extension supports current browser locale.
168 const base::ListValue* supported_locales = NULL;
169 if (extension->GetList(kSupportedLocales, &supported_locales)) {
170 std::vector<std::string> browser_locales;
171 l10n_util::GetParentLocales(g_browser_process->GetApplicationLocale(),
172 &browser_locales);
174 size_t num_locales = supported_locales->GetSize();
175 bool locale_supported = false;
176 for (size_t j = 0; j < num_locales; j++) {
177 std::string current_locale;
178 if (supported_locales->GetString(j, &current_locale) &&
179 l10n_util::IsValidLocaleSyntax(current_locale)) {
180 current_locale = l10n_util::NormalizeLocale(current_locale);
181 if (std::find(browser_locales.begin(), browser_locales.end(),
182 current_locale) != browser_locales.end()) {
183 locale_supported = true;
184 break;
186 } else {
187 LOG(WARNING) << "Unrecognized locale '" << current_locale
188 << "' found as supported locale for extension: "
189 << extension_id;
193 if (!locale_supported) {
194 unsupported_extensions.insert(extension_id);
195 VLOG(1) << "Skip installing (or uninstall) external extension: "
196 << extension_id << " because the extension doesn't support "
197 << "the browser locale.";
198 continue;
202 int creation_flags = creation_flags_;
203 bool is_bookmark_app;
204 if (extension->GetBoolean(kIsBookmarkApp, &is_bookmark_app) &&
205 is_bookmark_app) {
206 creation_flags |= Extension::FROM_BOOKMARK;
208 bool is_from_webstore = false;
209 if (extension->GetBoolean(kIsFromWebstore, &is_from_webstore) &&
210 is_from_webstore) {
211 creation_flags |= Extension::FROM_WEBSTORE;
213 bool keep_if_present = false;
214 if (extension->GetBoolean(kKeepIfPresent, &keep_if_present) &&
215 keep_if_present && profile_) {
216 ExtensionServiceInterface* extension_service =
217 ExtensionSystem::Get(profile_)->extension_service();
218 const Extension* extension = extension_service ?
219 extension_service->GetExtensionById(extension_id, true) : NULL;
220 if (!extension) {
221 VLOG(1) << "Skip installing (or uninstall) external extension: "
222 << extension_id << " because the extension should be kept "
223 << "only if it is already installed.";
224 continue;
227 bool was_installed_by_oem = false;
228 if (extension->GetBoolean(kWasInstalledByOem, &was_installed_by_oem) &&
229 was_installed_by_oem) {
230 creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
232 bool may_be_untrusted = false;
233 if (extension->GetBoolean(kMayBeUntrusted, &may_be_untrusted) &&
234 may_be_untrusted) {
235 creation_flags |= Extension::MAY_BE_UNTRUSTED;
238 std::string install_parameter;
239 extension->GetString(kInstallParam, &install_parameter);
241 if (has_external_crx) {
242 if (crx_location_ == Manifest::INVALID_LOCATION) {
243 LOG(WARNING) << "This provider does not support installing external "
244 << "extensions from crx files.";
245 continue;
247 if (external_crx.find(base::FilePath::kParentDirectory) !=
248 base::StringPiece::npos) {
249 LOG(WARNING) << "Path traversal not allowed in path: "
250 << external_crx.c_str();
251 continue;
254 // If the path is relative, and the provider has a base path,
255 // build the absolute path to the crx file.
256 base::FilePath path(external_crx);
257 if (!path.IsAbsolute()) {
258 base::FilePath base_path = loader_->GetBaseCrxFilePath();
259 if (base_path.empty()) {
260 LOG(WARNING) << "File path " << external_crx.c_str()
261 << " is relative. An absolute path is required.";
262 continue;
264 path = base_path.Append(external_crx);
267 Version version(external_version);
268 if (!version.IsValid()) {
269 LOG(WARNING) << "Malformed extension dictionary for extension: "
270 << extension_id.c_str() << ". Invalid version string \""
271 << external_version << "\".";
272 continue;
274 service_->OnExternalExtensionFileFound(extension_id, &version, path,
275 crx_location_, creation_flags,
276 auto_acknowledge_,
277 install_immediately_);
278 } else { // if (has_external_update_url)
279 CHECK(has_external_update_url); // Checking of keys above ensures this.
280 if (download_location_ == Manifest::INVALID_LOCATION) {
281 LOG(WARNING) << "This provider does not support installing external "
282 << "extensions from update URLs.";
283 continue;
285 GURL update_url(external_update_url);
286 if (!update_url.is_valid()) {
287 LOG(WARNING) << "Malformed extension dictionary for extension: "
288 << extension_id.c_str() << ". Key " << kExternalUpdateUrl
289 << " has value \"" << external_update_url
290 << "\", which is not a valid URL.";
291 continue;
293 service_->OnExternalExtensionUpdateUrlFound(extension_id,
294 install_parameter,
295 update_url,
296 download_location_,
297 creation_flags,
298 auto_acknowledge_);
302 for (std::set<std::string>::iterator it = unsupported_extensions.begin();
303 it != unsupported_extensions.end(); ++it) {
304 // Remove extension for the list of know external extensions. The extension
305 // will be uninstalled later because provider doesn't provide it anymore.
306 prefs_->Remove(*it, NULL);
309 service_->OnExternalProviderReady(this);
312 void ExternalProviderImpl::ServiceShutdown() {
313 service_ = NULL;
316 bool ExternalProviderImpl::IsReady() const {
317 return ready_;
320 bool ExternalProviderImpl::HasExtension(
321 const std::string& id) const {
322 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
323 CHECK(prefs_.get());
324 CHECK(ready_);
325 return prefs_->HasKey(id);
328 bool ExternalProviderImpl::GetExtensionDetails(
329 const std::string& id, Manifest::Location* location,
330 scoped_ptr<Version>* version) const {
331 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
332 CHECK(prefs_.get());
333 CHECK(ready_);
334 base::DictionaryValue* extension = NULL;
335 if (!prefs_->GetDictionary(id, &extension))
336 return false;
338 Manifest::Location loc = Manifest::INVALID_LOCATION;
339 if (extension->HasKey(kExternalUpdateUrl)) {
340 loc = download_location_;
342 } else if (extension->HasKey(kExternalCrx)) {
343 loc = crx_location_;
345 std::string external_version;
346 if (!extension->GetString(kExternalVersion, &external_version))
347 return false;
349 if (version)
350 version->reset(new Version(external_version));
352 } else {
353 NOTREACHED(); // Chrome should not allow prefs to get into this state.
354 return false;
357 if (location)
358 *location = loc;
360 return true;
363 // static
364 void ExternalProviderImpl::CreateExternalProviders(
365 VisitorInterface* service,
366 Profile* profile,
367 ProviderCollection* provider_list) {
368 TRACE_EVENT0("browser,startup",
369 "ExternalProviderImpl::CreateExternalProviders");
370 scoped_refptr<ExternalLoader> external_loader;
371 scoped_refptr<ExternalLoader> external_recommended_loader;
372 extensions::Manifest::Location crx_location = Manifest::INVALID_LOCATION;
373 #if defined(OS_CHROMEOS)
374 policy::BrowserPolicyConnectorChromeOS* connector =
375 g_browser_process->platform_part()->browser_policy_connector_chromeos();
376 bool is_chrome_os_public_session = false;
377 const user_manager::User* user =
378 chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
379 policy::DeviceLocalAccount::Type account_type;
380 if (user &&
381 connector->IsEnterpriseManaged() &&
382 policy::IsDeviceLocalAccountUser(user->email(), &account_type)) {
383 if (account_type == policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION)
384 is_chrome_os_public_session = true;
385 policy::DeviceLocalAccountPolicyBroker* broker =
386 connector->GetDeviceLocalAccountPolicyService()->GetBrokerForUser(
387 user->email());
388 if (broker) {
389 external_loader = broker->extension_loader();
390 crx_location = Manifest::EXTERNAL_POLICY;
391 } else {
392 NOTREACHED();
394 } else {
395 external_loader = new ExternalPolicyLoader(
396 ExtensionManagementFactory::GetForBrowserContext(profile),
397 ExternalPolicyLoader::FORCED);
398 external_recommended_loader = new ExternalPolicyLoader(
399 ExtensionManagementFactory::GetForBrowserContext(profile),
400 ExternalPolicyLoader::RECOMMENDED);
402 #else
403 external_loader = new ExternalPolicyLoader(
404 ExtensionManagementFactory::GetForBrowserContext(profile),
405 ExternalPolicyLoader::FORCED);
406 external_recommended_loader = new ExternalPolicyLoader(
407 ExtensionManagementFactory::GetForBrowserContext(profile),
408 ExternalPolicyLoader::RECOMMENDED);
409 #endif
411 // Policies are mandatory so they can't be skipped with command line flag.
412 if (external_loader.get()) {
413 provider_list->push_back(
414 linked_ptr<ExternalProviderInterface>(
415 new ExternalProviderImpl(
416 service,
417 external_loader,
418 profile,
419 crx_location,
420 Manifest::EXTERNAL_POLICY_DOWNLOAD,
421 Extension::NO_FLAGS)));
424 // Load the KioskAppExternalProvider when running in kiosk mode.
425 if (chrome::IsRunningInForcedAppMode()) {
426 #if defined(OS_CHROMEOS)
427 chromeos::KioskAppManager* kiosk_app_manager =
428 chromeos::KioskAppManager::Get();
429 DCHECK(kiosk_app_manager);
430 if (kiosk_app_manager && !kiosk_app_manager->external_loader_created()) {
431 scoped_ptr<ExternalProviderImpl> kiosk_app_provider(
432 new ExternalProviderImpl(
433 service, kiosk_app_manager->CreateExternalLoader(), profile,
434 Manifest::EXTERNAL_PREF, Manifest::INVALID_LOCATION,
435 Extension::NO_FLAGS));
436 kiosk_app_provider->set_auto_acknowledge(true);
437 kiosk_app_provider->set_install_immediately(true);
438 provider_list->push_back(
439 linked_ptr<ExternalProviderInterface>(kiosk_app_provider.release()));
441 #endif
442 return;
445 // Extensions provided by recommended policies.
446 if (external_recommended_loader.get()) {
447 provider_list->push_back(linked_ptr<ExternalProviderInterface>(
448 new ExternalProviderImpl(service,
449 external_recommended_loader,
450 profile,
451 crx_location,
452 Manifest::EXTERNAL_PREF_DOWNLOAD,
453 Extension::NO_FLAGS)));
456 // In tests don't install extensions from default external sources.
457 // It would only slowdown tests and make them flaky.
458 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
459 switches::kDisableDefaultApps))
460 return;
462 // On Mac OS, items in /Library/... should be written by the superuser.
463 // Check that all components of the path are writable by root only.
464 ExternalPrefLoader::Options check_admin_permissions_on_mac;
465 #if defined(OS_MACOSX)
466 check_admin_permissions_on_mac =
467 ExternalPrefLoader::ENSURE_PATH_CONTROLLED_BY_ADMIN;
468 #else
469 check_admin_permissions_on_mac = ExternalPrefLoader::NONE;
470 #endif
472 #if !defined(OS_WIN)
473 int bundled_extension_creation_flags = Extension::NO_FLAGS;
474 #endif
475 #if defined(OS_CHROMEOS)
476 bundled_extension_creation_flags = Extension::FROM_WEBSTORE |
477 Extension::WAS_INSTALLED_BY_DEFAULT;
479 if (!is_chrome_os_public_session) {
480 int external_apps_path_id = profile->IsSupervised() ?
481 chrome::DIR_SUPERVISED_USERS_DEFAULT_APPS :
482 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS;
483 ExternalPrefLoader::Options pref_load_flags =
484 profile->IsNewProfile()
485 ? ExternalPrefLoader::DELAY_LOAD_UNTIL_PRIORITY_SYNC
486 : ExternalPrefLoader::NONE;
487 provider_list->push_back(
488 linked_ptr<ExternalProviderInterface>(new ExternalProviderImpl(
489 service, new ExternalPrefLoader(external_apps_path_id,
490 pref_load_flags, profile),
491 profile, Manifest::EXTERNAL_PREF, Manifest::EXTERNAL_PREF_DOWNLOAD,
492 bundled_extension_creation_flags)));
494 // OEM default apps.
495 int oem_extension_creation_flags =
496 bundled_extension_creation_flags | Extension::WAS_INSTALLED_BY_OEM;
497 chromeos::ServicesCustomizationDocument* customization =
498 chromeos::ServicesCustomizationDocument::GetInstance();
499 provider_list->push_back(linked_ptr<ExternalProviderInterface>(
500 new ExternalProviderImpl(service,
501 customization->CreateExternalLoader(profile),
502 profile,
503 Manifest::EXTERNAL_PREF,
504 Manifest::EXTERNAL_PREF_DOWNLOAD,
505 oem_extension_creation_flags)));
507 #elif defined(OS_LINUX)
508 if (!profile->IsLegacySupervised()) {
509 provider_list->push_back(
510 linked_ptr<ExternalProviderInterface>(
511 new ExternalProviderImpl(
512 service,
513 new ExternalPrefLoader(
514 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS,
515 ExternalPrefLoader::NONE,
516 NULL),
517 profile,
518 Manifest::EXTERNAL_PREF,
519 Manifest::EXTERNAL_PREF_DOWNLOAD,
520 bundled_extension_creation_flags)));
522 #endif
524 if (!profile->IsLegacySupervised()) {
525 #if defined(OS_WIN)
526 provider_list->push_back(
527 linked_ptr<ExternalProviderInterface>(
528 new ExternalProviderImpl(
529 service,
530 new ExternalRegistryLoader,
531 profile,
532 Manifest::EXTERNAL_REGISTRY,
533 Manifest::EXTERNAL_PREF_DOWNLOAD,
534 Extension::NO_FLAGS)));
535 #else
536 provider_list->push_back(
537 linked_ptr<ExternalProviderInterface>(
538 new ExternalProviderImpl(
539 service,
540 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS,
541 check_admin_permissions_on_mac,
542 NULL),
543 profile,
544 Manifest::EXTERNAL_PREF,
545 Manifest::EXTERNAL_PREF_DOWNLOAD,
546 bundled_extension_creation_flags)));
548 // Define a per-user source of external extensions.
549 #if defined(OS_MACOSX)
550 provider_list->push_back(
551 linked_ptr<ExternalProviderInterface>(
552 new ExternalProviderImpl(
553 service,
554 new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS,
555 ExternalPrefLoader::NONE,
556 NULL),
557 profile,
558 Manifest::EXTERNAL_PREF,
559 Manifest::EXTERNAL_PREF_DOWNLOAD,
560 Extension::NO_FLAGS)));
561 #endif
562 #endif
564 #if !defined(OS_CHROMEOS)
565 // The default apps are installed as INTERNAL but use the external
566 // extension installer codeflow.
567 provider_list->push_back(
568 linked_ptr<ExternalProviderInterface>(
569 new default_apps::Provider(
570 profile,
571 service,
572 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS,
573 ExternalPrefLoader::NONE,
574 NULL),
575 Manifest::INTERNAL,
576 Manifest::INTERNAL,
577 Extension::FROM_WEBSTORE |
578 Extension::WAS_INSTALLED_BY_DEFAULT)));
579 #endif
582 provider_list->push_back(
583 linked_ptr<ExternalProviderInterface>(
584 new ExternalProviderImpl(
585 service,
586 new ExternalComponentLoader(profile),
587 profile,
588 Manifest::INVALID_LOCATION,
589 Manifest::EXTERNAL_COMPONENT,
590 Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT)));
593 } // namespace extensions