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"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/metrics/field_trial.h"
15 #include "base/path_service.h"
16 #include "base/strings/string_util.h"
17 #include "base/values.h"
18 #include "base/version.h"
19 #include "chrome/browser/app_mode/app_mode_utils.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/extensions/extension_service.h"
22 #include "chrome/browser/extensions/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/profiles/profile.h"
26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "extensions/browser/extension_system.h"
31 #include "extensions/browser/external_provider_interface.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/manifest.h"
34 #include "ui/base/l10n/l10n_util.h"
36 #if defined(OS_CHROMEOS)
37 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
38 #include "chrome/browser/chromeos/login/user.h"
39 #include "chrome/browser/chromeos/login/user_manager.h"
40 #include "chrome/browser/chromeos/policy/app_pack_updater.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"
45 #include "chrome/browser/extensions/default_apps.h"
49 #include "chrome/browser/extensions/external_registry_loader_win.h"
52 using content::BrowserThread
;
54 namespace extensions
{
56 // Constants for keeping track of extension preferences in a dictionary.
57 const char ExternalProviderImpl::kExternalCrx
[] = "external_crx";
58 const char ExternalProviderImpl::kExternalVersion
[] = "external_version";
59 const char ExternalProviderImpl::kExternalUpdateUrl
[] = "external_update_url";
60 const char ExternalProviderImpl::kSupportedLocales
[] = "supported_locales";
61 const char ExternalProviderImpl::kIsBookmarkApp
[] = "is_bookmark_app";
62 const char ExternalProviderImpl::kIsFromWebstore
[] = "is_from_webstore";
63 const char ExternalProviderImpl::kKeepIfPresent
[] = "keep_if_present";
65 ExternalProviderImpl::ExternalProviderImpl(
66 VisitorInterface
* service
,
67 const scoped_refptr
<ExternalLoader
>& loader
,
69 Manifest::Location crx_location
,
70 Manifest::Location download_location
,
72 : crx_location_(crx_location
),
73 download_location_(download_location
),
78 creation_flags_(creation_flags
),
79 auto_acknowledge_(false) {
83 ExternalProviderImpl::~ExternalProviderImpl() {
84 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
85 loader_
->OwnerShutdown();
88 void ExternalProviderImpl::VisitRegisteredExtension() {
89 // The loader will call back to SetPrefs.
90 loader_
->StartLoading();
93 void ExternalProviderImpl::SetPrefs(base::DictionaryValue
* prefs
) {
94 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
96 // Check if the service is still alive. It is possible that it went
97 // away while |loader_| was working on the FILE thread.
98 if (!service_
) return;
101 ready_
= true; // Queries for extensions are allowed from this point.
103 // Set of unsupported extensions that need to be deleted from prefs_.
104 std::set
<std::string
> unsupported_extensions
;
106 // Notify ExtensionService about all the extensions this provider has.
107 for (base::DictionaryValue::Iterator
i(*prefs_
); !i
.IsAtEnd(); i
.Advance()) {
108 const std::string
& extension_id
= i
.key();
109 const base::DictionaryValue
* extension
= NULL
;
111 if (!Extension::IdIsValid(extension_id
)) {
112 LOG(WARNING
) << "Malformed extension dictionary: key "
113 << extension_id
.c_str() << " is not a valid id.";
117 if (!i
.value().GetAsDictionary(&extension
)) {
118 LOG(WARNING
) << "Malformed extension dictionary: key "
119 << extension_id
.c_str()
120 << " has a value that is not a dictionary.";
124 base::FilePath::StringType external_crx
;
125 const base::Value
* external_version_value
= NULL
;
126 std::string external_version
;
127 std::string external_update_url
;
129 bool has_external_crx
= extension
->GetString(kExternalCrx
, &external_crx
);
131 bool has_external_version
= false;
132 if (extension
->Get(kExternalVersion
, &external_version_value
)) {
133 if (external_version_value
->IsType(base::Value::TYPE_STRING
)) {
134 external_version_value
->GetAsString(&external_version
);
135 has_external_version
= true;
137 LOG(WARNING
) << "Malformed extension dictionary for extension: "
138 << extension_id
.c_str() << ". " << kExternalVersion
139 << " value must be a string.";
144 bool has_external_update_url
= extension
->GetString(kExternalUpdateUrl
,
145 &external_update_url
);
146 if (has_external_crx
!= has_external_version
) {
147 LOG(WARNING
) << "Malformed extension dictionary for extension: "
148 << extension_id
.c_str() << ". " << kExternalCrx
149 << " and " << kExternalVersion
<< " must be used together.";
153 if (has_external_crx
== has_external_update_url
) {
154 LOG(WARNING
) << "Malformed extension dictionary for extension: "
155 << extension_id
.c_str() << ". Exactly one of the "
156 << "followng keys should be used: " << kExternalCrx
157 << ", " << kExternalUpdateUrl
<< ".";
161 // Check that extension supports current browser locale.
162 const base::ListValue
* supported_locales
= NULL
;
163 if (extension
->GetList(kSupportedLocales
, &supported_locales
)) {
164 std::vector
<std::string
> browser_locales
;
165 l10n_util::GetParentLocales(g_browser_process
->GetApplicationLocale(),
168 size_t num_locales
= supported_locales
->GetSize();
169 bool locale_supported
= false;
170 for (size_t j
= 0; j
< num_locales
; j
++) {
171 std::string current_locale
;
172 if (supported_locales
->GetString(j
, ¤t_locale
) &&
173 l10n_util::IsValidLocaleSyntax(current_locale
)) {
174 current_locale
= l10n_util::NormalizeLocale(current_locale
);
175 if (std::find(browser_locales
.begin(), browser_locales
.end(),
176 current_locale
) != browser_locales
.end()) {
177 locale_supported
= true;
181 LOG(WARNING
) << "Unrecognized locale '" << current_locale
182 << "' found as supported locale for extension: "
187 if (!locale_supported
) {
188 unsupported_extensions
.insert(extension_id
);
189 VLOG(1) << "Skip installing (or uninstall) external extension: "
190 << extension_id
<< " because the extension doesn't support "
191 << "the browser locale.";
196 int creation_flags
= creation_flags_
;
197 bool is_bookmark_app
;
198 if (extension
->GetBoolean(kIsBookmarkApp
, &is_bookmark_app
) &&
200 creation_flags
|= Extension::FROM_BOOKMARK
;
202 bool is_from_webstore
;
203 if (extension
->GetBoolean(kIsFromWebstore
, &is_from_webstore
) &&
205 creation_flags
|= Extension::FROM_WEBSTORE
;
207 bool keep_if_present
;
208 if (extension
->GetBoolean(kKeepIfPresent
, &keep_if_present
) &&
209 keep_if_present
&& profile_
) {
210 ExtensionServiceInterface
* extension_service
=
211 ExtensionSystem::Get(profile_
)->extension_service();
212 const Extension
* extension
= extension_service
?
213 extension_service
->GetExtensionById(extension_id
, true) : NULL
;
215 VLOG(1) << "Skip installing (or uninstall) external extension: "
216 << extension_id
<< " because the extension should be kept "
217 << "only if it is already installed.";
222 if (has_external_crx
) {
223 if (crx_location_
== Manifest::INVALID_LOCATION
) {
224 LOG(WARNING
) << "This provider does not support installing external "
225 << "extensions from crx files.";
228 if (external_crx
.find(base::FilePath::kParentDirectory
) !=
229 base::StringPiece::npos
) {
230 LOG(WARNING
) << "Path traversal not allowed in path: "
231 << external_crx
.c_str();
235 // If the path is relative, and the provider has a base path,
236 // build the absolute path to the crx file.
237 base::FilePath
path(external_crx
);
238 if (!path
.IsAbsolute()) {
239 base::FilePath base_path
= loader_
->GetBaseCrxFilePath();
240 if (base_path
.empty()) {
241 LOG(WARNING
) << "File path " << external_crx
.c_str()
242 << " is relative. An absolute path is required.";
245 path
= base_path
.Append(external_crx
);
248 Version
version(external_version
);
249 if (!version
.IsValid()) {
250 LOG(WARNING
) << "Malformed extension dictionary for extension: "
251 << extension_id
.c_str() << ". Invalid version string \""
252 << external_version
<< "\".";
255 service_
->OnExternalExtensionFileFound(extension_id
, &version
, path
,
256 crx_location_
, creation_flags
,
258 } else { // if (has_external_update_url)
259 CHECK(has_external_update_url
); // Checking of keys above ensures this.
260 if (download_location_
== Manifest::INVALID_LOCATION
) {
261 LOG(WARNING
) << "This provider does not support installing external "
262 << "extensions from update URLs.";
265 GURL
update_url(external_update_url
);
266 if (!update_url
.is_valid()) {
267 LOG(WARNING
) << "Malformed extension dictionary for extension: "
268 << extension_id
.c_str() << ". Key " << kExternalUpdateUrl
269 << " has value \"" << external_update_url
270 << "\", which is not a valid URL.";
273 service_
->OnExternalExtensionUpdateUrlFound(
274 extension_id
, update_url
, download_location_
, creation_flags
,
279 for (std::set
<std::string
>::iterator it
= unsupported_extensions
.begin();
280 it
!= unsupported_extensions
.end(); ++it
) {
281 // Remove extension for the list of know external extensions. The extension
282 // will be uninstalled later because provider doesn't provide it anymore.
283 prefs_
->Remove(*it
, NULL
);
286 service_
->OnExternalProviderReady(this);
289 void ExternalProviderImpl::ServiceShutdown() {
293 bool ExternalProviderImpl::IsReady() const {
297 bool ExternalProviderImpl::HasExtension(
298 const std::string
& id
) const {
299 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
302 return prefs_
->HasKey(id
);
305 bool ExternalProviderImpl::GetExtensionDetails(
306 const std::string
& id
, Manifest::Location
* location
,
307 scoped_ptr
<Version
>* version
) const {
308 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
311 base::DictionaryValue
* extension
= NULL
;
312 if (!prefs_
->GetDictionary(id
, &extension
))
315 Manifest::Location loc
= Manifest::INVALID_LOCATION
;
316 if (extension
->HasKey(kExternalUpdateUrl
)) {
317 loc
= download_location_
;
319 } else if (extension
->HasKey(kExternalCrx
)) {
322 std::string external_version
;
323 if (!extension
->GetString(kExternalVersion
, &external_version
))
327 version
->reset(new Version(external_version
));
330 NOTREACHED(); // Chrome should not allow prefs to get into this state.
341 void ExternalProviderImpl::CreateExternalProviders(
342 VisitorInterface
* service
,
344 ProviderCollection
* provider_list
) {
345 scoped_refptr
<ExternalLoader
> external_loader
;
346 extensions::Manifest::Location crx_location
= Manifest::INVALID_LOCATION
;
347 #if defined(OS_CHROMEOS)
348 policy::BrowserPolicyConnectorChromeOS
* connector
=
349 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
350 bool is_chrome_os_public_session
= false;
351 const chromeos::User
* user
=
352 chromeos::UserManager::Get()->GetUserByProfile(profile
);
353 policy::DeviceLocalAccount::Type account_type
;
354 if (user
&& policy::IsDeviceLocalAccountUser(user
->email(), &account_type
)) {
355 if (account_type
== policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION
)
356 is_chrome_os_public_session
= true;
357 policy::DeviceLocalAccountPolicyBroker
* broker
=
358 connector
->GetDeviceLocalAccountPolicyService()->GetBrokerForUser(
361 external_loader
= broker
->extension_loader();
362 crx_location
= Manifest::EXTERNAL_POLICY
;
367 external_loader
= new ExternalPolicyLoader(profile
);
370 external_loader
= new ExternalPolicyLoader(profile
);
373 // Policies are mandatory so they can't be skipped with command line flag.
374 if (external_loader
) {
375 provider_list
->push_back(
376 linked_ptr
<ExternalProviderInterface
>(
377 new ExternalProviderImpl(
382 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
383 Extension::NO_FLAGS
)));
386 // In tests don't install extensions from default external sources.
387 // It would only slowdown tests and make them flaky.
388 if (CommandLine::ForCurrentProcess()->HasSwitch(
389 switches::kDisableDefaultApps
))
392 // No external app install in app mode.
393 if (chrome::IsRunningInForcedAppMode())
396 // On Mac OS, items in /Library/... should be written by the superuser.
397 // Check that all components of the path are writable by root only.
398 ExternalPrefLoader::Options check_admin_permissions_on_mac
;
399 #if defined(OS_MACOSX)
400 check_admin_permissions_on_mac
=
401 ExternalPrefLoader::ENSURE_PATH_CONTROLLED_BY_ADMIN
;
403 check_admin_permissions_on_mac
= ExternalPrefLoader::NONE
;
406 bool is_chromeos_demo_session
= false;
407 int bundled_extension_creation_flags
= Extension::NO_FLAGS
;
408 #if defined(OS_CHROMEOS)
409 chromeos::UserManager
* user_manager
= chromeos::UserManager::Get();
410 is_chromeos_demo_session
=
411 user_manager
&& user_manager
->IsLoggedInAsDemoUser() &&
412 connector
->GetDeviceMode() == policy::DEVICE_MODE_RETAIL_KIOSK
;
413 bundled_extension_creation_flags
= Extension::FROM_WEBSTORE
|
414 Extension::WAS_INSTALLED_BY_DEFAULT
;
417 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
418 if (!profile
->IsManaged()) {
419 provider_list
->push_back(
420 linked_ptr
<ExternalProviderInterface
>(
421 new ExternalProviderImpl(
423 new ExternalPrefLoader(
424 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
,
425 ExternalPrefLoader::NONE
),
427 Manifest::EXTERNAL_PREF
,
428 Manifest::EXTERNAL_PREF_DOWNLOAD
,
429 bundled_extension_creation_flags
)));
433 #if defined(OS_CHROMEOS)
434 if (!is_chromeos_demo_session
&& !is_chrome_os_public_session
) {
435 int external_apps_path_id
= profile
->IsManaged() ?
436 chrome::DIR_MANAGED_USERS_DEFAULT_APPS
:
437 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
;
438 provider_list
->push_back(
439 linked_ptr
<ExternalProviderInterface
>(
440 new ExternalProviderImpl(
442 new ExternalPrefLoader(external_apps_path_id
,
443 ExternalPrefLoader::NONE
),
445 Manifest::EXTERNAL_PREF
,
446 Manifest::EXTERNAL_PREF_DOWNLOAD
,
447 bundled_extension_creation_flags
)));
450 policy::AppPackUpdater
* app_pack_updater
= connector
->GetAppPackUpdater();
451 if (is_chromeos_demo_session
&& app_pack_updater
&&
452 !app_pack_updater
->created_external_loader()) {
453 provider_list
->push_back(
454 linked_ptr
<ExternalProviderInterface
>(
455 new ExternalProviderImpl(
457 app_pack_updater
->CreateExternalLoader(),
459 Manifest::EXTERNAL_PREF
,
460 Manifest::INVALID_LOCATION
,
461 Extension::NO_FLAGS
)));
465 if (!profile
->IsManaged() && !is_chromeos_demo_session
) {
467 provider_list
->push_back(
468 linked_ptr
<ExternalProviderInterface
>(
469 new ExternalProviderImpl(
471 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS
,
472 check_admin_permissions_on_mac
),
474 Manifest::EXTERNAL_PREF
,
475 Manifest::EXTERNAL_PREF_DOWNLOAD
,
476 bundled_extension_creation_flags
)));
479 // Define a per-user source of external extensions.
480 // On Chrome OS, this serves as a source for OEM customization.
481 #if defined(OS_CHROMEOS)
482 if (!is_chrome_os_public_session
) {
484 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
485 provider_list
->push_back(
486 linked_ptr
<ExternalProviderInterface
>(
487 new ExternalProviderImpl(
489 new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS
,
490 ExternalPrefLoader::NONE
),
492 Manifest::EXTERNAL_PREF
,
493 Manifest::EXTERNAL_PREF_DOWNLOAD
,
494 Extension::NO_FLAGS
)));
496 #if defined(OS_CHROMEOS)
501 provider_list
->push_back(
502 linked_ptr
<ExternalProviderInterface
>(
503 new ExternalProviderImpl(
505 new ExternalRegistryLoader
,
507 Manifest::EXTERNAL_REGISTRY
,
508 Manifest::EXTERNAL_PREF_DOWNLOAD
,
509 Extension::NO_FLAGS
)));
512 #if !defined(OS_CHROMEOS)
513 // The default apps are installed as INTERNAL but use the external
514 // extension installer codeflow.
515 provider_list
->push_back(
516 linked_ptr
<ExternalProviderInterface
>(
517 new default_apps::Provider(
520 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS
,
521 ExternalPrefLoader::NONE
),
523 Manifest::INVALID_LOCATION
,
524 Extension::FROM_WEBSTORE
|
525 Extension::WAS_INSTALLED_BY_DEFAULT
)));
528 provider_list
->push_back(
529 linked_ptr
<ExternalProviderInterface
>(
530 new ExternalProviderImpl(
532 new ExternalComponentLoader(profile
),
534 Manifest::INVALID_LOCATION
,
535 Manifest::EXTERNAL_COMPONENT
,
536 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
)));
540 } // namespace extensions