15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/external_provider_impl.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <set>
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/linked_ptr.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/field_trial.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/version.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/app_mode/app_mode_utils.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/browser_process.h"
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/extensions/extension_management.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/extensions/external_component_loader.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/external_policy_loader.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/external_pref_loader.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_paths.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
3003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "components/crx_file/id_util.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/browser/external_provider_interface.h"
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "extensions/common/extension.h"
353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "extensions/common/manifest.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/chromeos/customization_document.h"
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/chromeos/policy/app_pack_updater.h"
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/chromeos/policy/device_local_account.h"
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/chromeos/profiles/profile_helper.h"
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/user_manager/user.h"
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/user_manager/user_manager.h"
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#else
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/default_apps.h"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/external_registry_loader_win.h"
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Constants for keeping track of extension preferences in a dictionary.
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochconst char ExternalProviderImpl::kInstallParam[] = "install_parameter";
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char ExternalProviderImpl::kExternalCrx[] = "external_crx";
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char ExternalProviderImpl::kExternalVersion[] = "external_version";
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char ExternalProviderImpl::kExternalUpdateUrl[] = "external_update_url";
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char ExternalProviderImpl::kIsBookmarkApp[] = "is_bookmark_app";
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const char ExternalProviderImpl::kIsFromWebstore[] = "is_from_webstore";
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)const char ExternalProviderImpl::kKeepIfPresent[] = "keep_if_present";
69c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochconst char ExternalProviderImpl::kWasInstalledByOem[] = "was_installed_by_oem";
70c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochconst char ExternalProviderImpl::kSupportedLocales[] = "supported_locales";
7103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)const char ExternalProviderImpl::kMayBeUntrusted[] = "may_be_untrusted";
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)ExternalProviderImpl::ExternalProviderImpl(
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    VisitorInterface* service,
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const scoped_refptr<ExternalLoader>& loader,
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    Profile* profile,
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    Manifest::Location crx_location,
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    Manifest::Location download_location,
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    int creation_flags)
807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : crx_location_(crx_location),
817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      download_location_(download_location),
827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      service_(service),
837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      ready_(false),
847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      loader_(loader),
857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      profile_(profile),
867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      creation_flags_(creation_flags),
877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      auto_acknowledge_(false) {
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  loader_->Init(this);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ExternalProviderImpl::~ExternalProviderImpl() {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  loader_->OwnerShutdown();
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExternalProviderImpl::VisitRegisteredExtension() {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The loader will call back to SetPrefs.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  loader_->StartLoading();
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid ExternalProviderImpl::SetPrefs(base::DictionaryValue* prefs) {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check if the service is still alive. It is possible that it went
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // away while |loader_| was working on the FILE thread.
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!service_) return;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  prefs_.reset(prefs);
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ready_ = true;  // Queries for extensions are allowed from this point.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set of unsupported extensions that need to be deleted from prefs_.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::set<std::string> unsupported_extensions;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notify ExtensionService about all the extensions this provider has.
115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (base::DictionaryValue::Iterator i(*prefs_); !i.IsAtEnd(); i.Advance()) {
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& extension_id = i.key();
117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const base::DictionaryValue* extension = NULL;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    if (!crx_file::id_util::IdIsValid(extension_id)) {
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(WARNING) << "Malformed extension dictionary: key "
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << extension_id.c_str() << " is not a valid id.";
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      continue;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!i.value().GetAsDictionary(&extension)) {
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(WARNING) << "Malformed extension dictionary: key "
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << extension_id.c_str()
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << " has a value that is not a dictionary.";
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      continue;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::FilePath::StringType external_crx;
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::Value* external_version_value = NULL;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string external_version;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string external_update_url;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool has_external_crx = extension->GetString(kExternalCrx, &external_crx);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool has_external_version = false;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (extension->Get(kExternalVersion, &external_version_value)) {
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (external_version_value->IsType(base::Value::TYPE_STRING)) {
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        external_version_value->GetAsString(&external_version);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        has_external_version = true;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      } else {
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        LOG(WARNING) << "Malformed extension dictionary for extension: "
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << extension_id.c_str() << ". " << kExternalVersion
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << " value must be a string.";
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        continue;
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool has_external_update_url = extension->GetString(kExternalUpdateUrl,
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                        &external_update_url);
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (has_external_crx != has_external_version) {
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(WARNING) << "Malformed extension dictionary for extension: "
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << extension_id.c_str() << ".  " << kExternalCrx
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << " and " << kExternalVersion << " must be used together.";
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      continue;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (has_external_crx == has_external_update_url) {
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG(WARNING) << "Malformed extension dictionary for extension: "
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << extension_id.c_str() << ".  Exactly one of the "
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << "followng keys should be used: " << kExternalCrx
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   << ", " << kExternalUpdateUrl << ".";
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      continue;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Check that extension supports current browser locale.
170eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const base::ListValue* supported_locales = NULL;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (extension->GetList(kSupportedLocales, &supported_locales)) {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::vector<std::string> browser_locales;
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      l10n_util::GetParentLocales(g_browser_process->GetApplicationLocale(),
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  &browser_locales);
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      size_t num_locales = supported_locales->GetSize();
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool locale_supported = false;
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      for (size_t j = 0; j < num_locales; j++) {
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        std::string current_locale;
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (supported_locales->GetString(j, &current_locale) &&
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            l10n_util::IsValidLocaleSyntax(current_locale)) {
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          current_locale = l10n_util::NormalizeLocale(current_locale);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (std::find(browser_locales.begin(), browser_locales.end(),
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        current_locale) != browser_locales.end()) {
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            locale_supported = true;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            break;
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          }
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        } else {
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          LOG(WARNING) << "Unrecognized locale '" << current_locale
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       << "' found as supported locale for extension: "
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       << extension_id;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!locale_supported) {
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        unsupported_extensions.insert(extension_id);
197868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        VLOG(1) << "Skip installing (or uninstall) external extension: "
198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                << extension_id << " because the extension doesn't support "
199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                << "the browser locale.";
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        continue;
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int creation_flags = creation_flags_;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool is_bookmark_app;
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (extension->GetBoolean(kIsBookmarkApp, &is_bookmark_app) &&
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        is_bookmark_app) {
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      creation_flags |= Extension::FROM_BOOKMARK;
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
21003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    bool is_from_webstore = false;
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (extension->GetBoolean(kIsFromWebstore, &is_from_webstore) &&
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        is_from_webstore) {
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      creation_flags |= Extension::FROM_WEBSTORE;
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
21503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    bool keep_if_present = false;
216868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (extension->GetBoolean(kKeepIfPresent, &keep_if_present) &&
217868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        keep_if_present && profile_) {
218868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ExtensionServiceInterface* extension_service =
219868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          ExtensionSystem::Get(profile_)->extension_service();
220868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const Extension* extension = extension_service ?
221868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          extension_service->GetExtensionById(extension_id, true) : NULL;
222868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      if (!extension) {
223868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        VLOG(1) << "Skip installing (or uninstall) external extension: "
224868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                << extension_id << " because the extension should be kept "
225868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                << "only if it is already installed.";
226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        continue;
227868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      }
228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
22903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    bool was_installed_by_oem = false;
230c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    if (extension->GetBoolean(kWasInstalledByOem, &was_installed_by_oem) &&
231c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        was_installed_by_oem) {
232c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      creation_flags |= Extension::WAS_INSTALLED_BY_OEM;
233c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    }
23403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    bool may_be_untrusted = false;
23503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    if (extension->GetBoolean(kMayBeUntrusted, &may_be_untrusted) &&
23603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        may_be_untrusted) {
23703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      creation_flags |= Extension::MAY_BE_UNTRUSTED;
23803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    }
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
240effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    std::string install_parameter;
241effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    extension->GetString(kInstallParam, &install_parameter);
242effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (has_external_crx) {
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (crx_location_ == Manifest::INVALID_LOCATION) {
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        LOG(WARNING) << "This provider does not support installing external "
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << "extensions from crx files.";
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        continue;
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (external_crx.find(base::FilePath::kParentDirectory) !=
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          base::StringPiece::npos) {
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        LOG(WARNING) << "Path traversal not allowed in path: "
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << external_crx.c_str();
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        continue;
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // If the path is relative, and the provider has a base path,
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // build the absolute path to the crx file.
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::FilePath path(external_crx);
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!path.IsAbsolute()) {
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::FilePath base_path = loader_->GetBaseCrxFilePath();
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (base_path.empty()) {
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          LOG(WARNING) << "File path " << external_crx.c_str()
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       << " is relative.  An absolute path is required.";
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          continue;
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        path = base_path.Append(external_crx);
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Version version(external_version);
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!version.IsValid()) {
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        LOG(WARNING) << "Malformed extension dictionary for extension: "
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << extension_id.c_str() << ".  Invalid version string \""
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << external_version << "\".";
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        continue;
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      service_->OnExternalExtensionFileFound(extension_id, &version, path,
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             crx_location_, creation_flags,
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             auto_acknowledge_);
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {  // if (has_external_update_url)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      CHECK(has_external_update_url);  // Checking of keys above ensures this.
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (download_location_ == Manifest::INVALID_LOCATION) {
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        LOG(WARNING) << "This provider does not support installing external "
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << "extensions from update URLs.";
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        continue;
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GURL update_url(external_update_url);
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!update_url.is_valid()) {
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        LOG(WARNING) << "Malformed extension dictionary for extension: "
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << extension_id.c_str() << ".  Key " << kExternalUpdateUrl
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << " has value \"" << external_update_url
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     << "\", which is not a valid URL.";
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        continue;
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
294effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      service_->OnExternalExtensionUpdateUrlFound(extension_id,
295effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  install_parameter,
296effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  update_url,
297effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  download_location_,
298effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  creation_flags,
299effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                                  auto_acknowledge_);
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (std::set<std::string>::iterator it = unsupported_extensions.begin();
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       it != unsupported_extensions.end(); ++it) {
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Remove extension for the list of know external extensions. The extension
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // will be uninstalled later because provider doesn't provide it anymore.
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    prefs_->Remove(*it, NULL);
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  service_->OnExternalProviderReady(this);
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExternalProviderImpl::ServiceShutdown() {
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  service_ = NULL;
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExternalProviderImpl::IsReady() const {
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return ready_;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExternalProviderImpl::HasExtension(
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& id) const {
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(prefs_.get());
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(ready_);
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return prefs_->HasKey(id);
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ExternalProviderImpl::GetExtensionDetails(
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& id, Manifest::Location* location,
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<Version>* version) const {
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(prefs_.get());
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(ready_);
335eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  base::DictionaryValue* extension = NULL;
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!prefs_->GetDictionary(id, &extension))
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Manifest::Location loc = Manifest::INVALID_LOCATION;
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (extension->HasKey(kExternalUpdateUrl)) {
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    loc = download_location_;
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (extension->HasKey(kExternalCrx)) {
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    loc = crx_location_;
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string external_version;
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!extension->GetString(kExternalVersion, &external_version))
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (version)
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      version->reset(new Version(external_version));
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();  // Chrome should not allow prefs to get into this state.
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (location)
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    *location = loc;
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExternalProviderImpl::CreateExternalProviders(
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    VisitorInterface* service,
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Profile* profile,
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProviderCollection* provider_list) {
3698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  scoped_refptr<ExternalLoader> external_loader;
3708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  extensions::Manifest::Location crx_location = Manifest::INVALID_LOCATION;
3718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(OS_CHROMEOS)
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy::BrowserPolicyConnectorChromeOS* connector =
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      g_browser_process->platform_part()->browser_policy_connector_chromeos();
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool is_chrome_os_public_session = false;
3755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const user_manager::User* user =
376116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
3775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy::DeviceLocalAccount::Type account_type;
3785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (user && policy::IsDeviceLocalAccountUser(user->email(), &account_type)) {
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (account_type == policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION)
3805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      is_chrome_os_public_session = true;
3818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    policy::DeviceLocalAccountPolicyBroker* broker =
3825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        connector->GetDeviceLocalAccountPolicyService()->GetBrokerForUser(
3835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            user->email());
3848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (broker) {
3858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      external_loader = broker->extension_loader();
3868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      crx_location = Manifest::EXTERNAL_POLICY;
3878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    } else {
3888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      NOTREACHED();
3898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
3908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else {
3911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    external_loader = new ExternalPolicyLoader(
3921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        ExtensionManagementFactory::GetForBrowserContext(profile));
3938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#else
3951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  external_loader = new ExternalPolicyLoader(
3961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      ExtensionManagementFactory::GetForBrowserContext(profile));
3978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
3988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Policies are mandatory so they can't be skipped with command line flag.
4001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (external_loader.get()) {
4018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    provider_list->push_back(
4028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        linked_ptr<ExternalProviderInterface>(
4038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            new ExternalProviderImpl(
4048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                service,
4058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                external_loader,
4068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                profile,
4078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                crx_location,
4088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                Manifest::EXTERNAL_POLICY_DOWNLOAD,
4098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                Extension::NO_FLAGS)));
4108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
412116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Load the KioskAppExternalProvider when running in kiosk mode.
413116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (chrome::IsRunningInForcedAppMode()) {
414116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(OS_CHROMEOS)
415116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    chromeos::KioskAppManager* kiosk_app_manager =
416116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        chromeos::KioskAppManager::Get();
417116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    DCHECK(kiosk_app_manager);
418116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (kiosk_app_manager && !kiosk_app_manager->external_loader_created()) {
419116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      provider_list->push_back(linked_ptr<ExternalProviderInterface>(
420116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          new ExternalProviderImpl(service,
421116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                   kiosk_app_manager->CreateExternalLoader(),
422116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                   profile,
423116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                   Manifest::EXTERNAL_PREF,
424116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                   Manifest::INVALID_LOCATION,
425116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                   Extension::NO_FLAGS)));
426116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
427116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif
428116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
429116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
430116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // In tests don't install extensions from default external sources.
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It would only slowdown tests and make them flaky.
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      switches::kDisableDefaultApps))
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // On Mac OS, items in /Library/... should be written by the superuser.
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check that all components of the path are writable by root only.
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExternalPrefLoader::Options check_admin_permissions_on_mac;
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_MACOSX)
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  check_admin_permissions_on_mac =
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ExternalPrefLoader::ENSURE_PATH_CONTROLLED_BY_ADMIN;
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  check_admin_permissions_on_mac = ExternalPrefLoader::NONE;
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_chromeos_demo_session = false;
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int bundled_extension_creation_flags = Extension::NO_FLAGS;
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_CHROMEOS)
4506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  user_manager::UserManager* user_manager = user_manager::UserManager::Get();
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  is_chromeos_demo_session =
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      user_manager && user_manager->IsLoggedInAsDemoUser() &&
4535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      connector->GetDeviceMode() == policy::DEVICE_MODE_RETAIL_KIOSK;
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bundled_extension_creation_flags = Extension::FROM_WEBSTORE |
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Extension::WAS_INSTALLED_BY_DEFAULT;
45658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#endif
45758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
45868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
459f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!profile->IsSupervised()) {
46068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    provider_list->push_back(
46168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)        linked_ptr<ExternalProviderInterface>(
46268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)            new ExternalProviderImpl(
46368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                service,
46468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                new ExternalPrefLoader(
46568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                    chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS,
46668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                    ExternalPrefLoader::NONE),
46768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                profile,
46868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                Manifest::EXTERNAL_PREF,
46968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                Manifest::EXTERNAL_PREF_DOWNLOAD,
47068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)                bundled_extension_creation_flags)));
47168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
47268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif
47368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
47468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if defined(OS_CHROMEOS)
4755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!is_chromeos_demo_session && !is_chrome_os_public_session) {
476f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int external_apps_path_id = profile->IsSupervised() ?
477f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        chrome::DIR_SUPERVISED_USERS_DEFAULT_APPS :
478424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS;
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    provider_list->push_back(
48023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        linked_ptr<ExternalProviderInterface>(new ExternalProviderImpl(
48123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)            service,
48223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)            new ExternalPrefLoader(external_apps_path_id,
48323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                   ExternalPrefLoader::NONE),
48423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)            profile,
48523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)            Manifest::EXTERNAL_PREF,
48623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)            Manifest::EXTERNAL_PREF_DOWNLOAD,
48723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)            bundled_extension_creation_flags)));
488a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
489a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // OEM default apps.
49023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    int oem_extension_creation_flags =
49123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        bundled_extension_creation_flags | Extension::WAS_INSTALLED_BY_OEM;
492a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    chromeos::ServicesCustomizationDocument* customization =
493a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        chromeos::ServicesCustomizationDocument::GetInstance();
49423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    provider_list->push_back(linked_ptr<ExternalProviderInterface>(
49523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)        new ExternalProviderImpl(service,
49623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 customization->CreateExternalLoader(profile),
49723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 profile,
49823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 Manifest::EXTERNAL_PREF,
49923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 Manifest::EXTERNAL_PREF_DOWNLOAD,
50023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)                                 oem_extension_creation_flags)));
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
502424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  policy::AppPackUpdater* app_pack_updater = connector->GetAppPackUpdater();
504424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (is_chromeos_demo_session && app_pack_updater &&
505424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      !app_pack_updater->created_external_loader()) {
506424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    provider_list->push_back(
507424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        linked_ptr<ExternalProviderInterface>(
508424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          new ExternalProviderImpl(
509424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)              service,
510424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)              app_pack_updater->CreateExternalLoader(),
511424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)              profile,
512424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)              Manifest::EXTERNAL_PREF,
513424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)              Manifest::INVALID_LOCATION,
514424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)              Extension::NO_FLAGS)));
515424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
516424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#endif
517424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
518f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!profile->IsSupervised() && !is_chromeos_demo_session) {
5195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if !defined(OS_WIN)
520424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    provider_list->push_back(
521424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        linked_ptr<ExternalProviderInterface>(
522424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)            new ExternalProviderImpl(
523424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                service,
524424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS,
525424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                       check_admin_permissions_on_mac),
526424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                profile,
527424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                Manifest::EXTERNAL_PREF,
528424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                Manifest::EXTERNAL_PREF_DOWNLOAD,
529424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                bundled_extension_creation_flags)));
5305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Define a per-user source of external extensions.
533a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_MACOSX)
534a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    provider_list->push_back(
535a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        linked_ptr<ExternalProviderInterface>(
536a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            new ExternalProviderImpl(
537a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                service,
538a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS,
539a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                       ExternalPrefLoader::NONE),
540a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                profile,
541a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                Manifest::EXTERNAL_PREF,
542a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                Manifest::EXTERNAL_PREF_DOWNLOAD,
543a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                Extension::NO_FLAGS)));
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(OS_WIN)
5472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    provider_list->push_back(
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        linked_ptr<ExternalProviderInterface>(
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            new ExternalProviderImpl(
5502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                service,
5512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                new ExternalRegistryLoader,
552868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                profile,
5532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                Manifest::EXTERNAL_REGISTRY,
5545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                Manifest::EXTERNAL_PREF_DOWNLOAD,
5552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                Extension::NO_FLAGS)));
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(OS_CHROMEOS)
5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The default apps are installed as INTERNAL but use the external
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // extension installer codeflow.
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    provider_list->push_back(
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        linked_ptr<ExternalProviderInterface>(
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            new default_apps::Provider(
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                profile,
5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                service,
5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS,
5672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                       ExternalPrefLoader::NONE),
5682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                Manifest::INTERNAL,
569f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                Manifest::INTERNAL,
5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                Extension::FROM_WEBSTORE |
5712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    Extension::WAS_INSTALLED_BY_DEFAULT)));
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
574424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    provider_list->push_back(
57590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      linked_ptr<ExternalProviderInterface>(
57690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        new ExternalProviderImpl(
57790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            service,
5785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            new ExternalComponentLoader(profile),
579868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            profile,
58090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            Manifest::INVALID_LOCATION,
5814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            Manifest::EXTERNAL_COMPONENT,
58290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)            Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT)));
583424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace extensions
587