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/extension_system.h"
23 #include "chrome/browser/extensions/external_component_loader.h"
24 #include "chrome/browser/extensions/external_policy_loader.h"
25 #include "chrome/browser/extensions/external_pref_loader.h"
26 #include "chrome/browser/extensions/external_provider_interface.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/common/chrome_paths.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/extensions/extension.h"
31 #include "chrome/common/pref_names.h"
32 #include "content/public/browser/browser_thread.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/external_pref_cache_loader.h"
38 #include "chrome/browser/chromeos/login/user_manager.h"
39 #include "chrome/browser/chromeos/policy/app_pack_updater.h"
40 #include "chrome/browser/policy/browser_policy_connector.h"
42 #include "chrome/browser/extensions/default_apps.h"
46 #include "chrome/browser/extensions/external_registry_loader_win.h"
49 using content::BrowserThread
;
51 namespace extensions
{
53 // Constants for keeping track of extension preferences in a dictionary.
54 const char ExternalProviderImpl::kExternalCrx
[] = "external_crx";
55 const char ExternalProviderImpl::kExternalVersion
[] = "external_version";
56 const char ExternalProviderImpl::kExternalUpdateUrl
[] = "external_update_url";
57 const char ExternalProviderImpl::kSupportedLocales
[] = "supported_locales";
58 const char ExternalProviderImpl::kIsBookmarkApp
[] = "is_bookmark_app";
59 const char ExternalProviderImpl::kIsFromWebstore
[] = "is_from_webstore";
60 const char ExternalProviderImpl::kKeepIfPresent
[] = "keep_if_present";
62 ExternalProviderImpl::ExternalProviderImpl(VisitorInterface
* service
,
63 ExternalLoader
* loader
,
65 Manifest::Location crx_location
,
66 Manifest::Location download_location
,
68 : crx_location_(crx_location
),
69 download_location_(download_location
),
74 creation_flags_(creation_flags
),
75 auto_acknowledge_(false) {
79 ExternalProviderImpl::~ExternalProviderImpl() {
80 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
81 loader_
->OwnerShutdown();
84 void ExternalProviderImpl::VisitRegisteredExtension() {
85 // The loader will call back to SetPrefs.
86 loader_
->StartLoading();
89 void ExternalProviderImpl::SetPrefs(base::DictionaryValue
* prefs
) {
90 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
92 // Check if the service is still alive. It is possible that it went
93 // away while |loader_| was working on the FILE thread.
94 if (!service_
) return;
97 ready_
= true; // Queries for extensions are allowed from this point.
99 // Set of unsupported extensions that need to be deleted from prefs_.
100 std::set
<std::string
> unsupported_extensions
;
102 // Notify ExtensionService about all the extensions this provider has.
103 for (base::DictionaryValue::Iterator
i(*prefs_
); !i
.IsAtEnd(); i
.Advance()) {
104 const std::string
& extension_id
= i
.key();
105 const base::DictionaryValue
* extension
= NULL
;
107 if (!Extension::IdIsValid(extension_id
)) {
108 LOG(WARNING
) << "Malformed extension dictionary: key "
109 << extension_id
.c_str() << " is not a valid id.";
113 if (!i
.value().GetAsDictionary(&extension
)) {
114 LOG(WARNING
) << "Malformed extension dictionary: key "
115 << extension_id
.c_str()
116 << " has a value that is not a dictionary.";
120 base::FilePath::StringType external_crx
;
121 const Value
* external_version_value
= NULL
;
122 std::string external_version
;
123 std::string external_update_url
;
125 bool has_external_crx
= extension
->GetString(kExternalCrx
, &external_crx
);
127 bool has_external_version
= false;
128 if (extension
->Get(kExternalVersion
, &external_version_value
)) {
129 if (external_version_value
->IsType(Value::TYPE_STRING
)) {
130 external_version_value
->GetAsString(&external_version
);
131 has_external_version
= true;
133 LOG(WARNING
) << "Malformed extension dictionary for extension: "
134 << extension_id
.c_str() << ". " << kExternalVersion
135 << " value must be a string.";
140 bool has_external_update_url
= extension
->GetString(kExternalUpdateUrl
,
141 &external_update_url
);
142 if (has_external_crx
!= has_external_version
) {
143 LOG(WARNING
) << "Malformed extension dictionary for extension: "
144 << extension_id
.c_str() << ". " << kExternalCrx
145 << " and " << kExternalVersion
<< " must be used together.";
149 if (has_external_crx
== has_external_update_url
) {
150 LOG(WARNING
) << "Malformed extension dictionary for extension: "
151 << extension_id
.c_str() << ". Exactly one of the "
152 << "followng keys should be used: " << kExternalCrx
153 << ", " << kExternalUpdateUrl
<< ".";
157 // Check that extension supports current browser locale.
158 const base::ListValue
* supported_locales
= NULL
;
159 if (extension
->GetList(kSupportedLocales
, &supported_locales
)) {
160 std::vector
<std::string
> browser_locales
;
161 l10n_util::GetParentLocales(g_browser_process
->GetApplicationLocale(),
164 size_t num_locales
= supported_locales
->GetSize();
165 bool locale_supported
= false;
166 for (size_t j
= 0; j
< num_locales
; j
++) {
167 std::string current_locale
;
168 if (supported_locales
->GetString(j
, ¤t_locale
) &&
169 l10n_util::IsValidLocaleSyntax(current_locale
)) {
170 current_locale
= l10n_util::NormalizeLocale(current_locale
);
171 if (std::find(browser_locales
.begin(), browser_locales
.end(),
172 current_locale
) != browser_locales
.end()) {
173 locale_supported
= true;
177 LOG(WARNING
) << "Unrecognized locale '" << current_locale
178 << "' found as supported locale for extension: "
183 if (!locale_supported
) {
184 unsupported_extensions
.insert(extension_id
);
185 VLOG(1) << "Skip installing (or uninstall) external extension: "
186 << extension_id
<< " because the extension doesn't support "
187 << "the browser locale.";
192 int creation_flags
= creation_flags_
;
193 bool is_bookmark_app
;
194 if (extension
->GetBoolean(kIsBookmarkApp
, &is_bookmark_app
) &&
196 creation_flags
|= Extension::FROM_BOOKMARK
;
198 bool is_from_webstore
;
199 if (extension
->GetBoolean(kIsFromWebstore
, &is_from_webstore
) &&
201 creation_flags
|= Extension::FROM_WEBSTORE
;
203 bool keep_if_present
;
204 if (extension
->GetBoolean(kKeepIfPresent
, &keep_if_present
) &&
205 keep_if_present
&& profile_
) {
206 ExtensionServiceInterface
* extension_service
=
207 ExtensionSystem::Get(profile_
)->extension_service();
208 const Extension
* extension
= extension_service
?
209 extension_service
->GetExtensionById(extension_id
, true) : NULL
;
211 VLOG(1) << "Skip installing (or uninstall) external extension: "
212 << extension_id
<< " because the extension should be kept "
213 << "only if it is already installed.";
218 if (has_external_crx
) {
219 if (crx_location_
== Manifest::INVALID_LOCATION
) {
220 LOG(WARNING
) << "This provider does not support installing external "
221 << "extensions from crx files.";
224 if (external_crx
.find(base::FilePath::kParentDirectory
) !=
225 base::StringPiece::npos
) {
226 LOG(WARNING
) << "Path traversal not allowed in path: "
227 << external_crx
.c_str();
231 // If the path is relative, and the provider has a base path,
232 // build the absolute path to the crx file.
233 base::FilePath
path(external_crx
);
234 if (!path
.IsAbsolute()) {
235 base::FilePath base_path
= loader_
->GetBaseCrxFilePath();
236 if (base_path
.empty()) {
237 LOG(WARNING
) << "File path " << external_crx
.c_str()
238 << " is relative. An absolute path is required.";
241 path
= base_path
.Append(external_crx
);
244 Version
version(external_version
);
245 if (!version
.IsValid()) {
246 LOG(WARNING
) << "Malformed extension dictionary for extension: "
247 << extension_id
.c_str() << ". Invalid version string \""
248 << external_version
<< "\".";
251 service_
->OnExternalExtensionFileFound(extension_id
, &version
, path
,
252 crx_location_
, creation_flags
,
254 } else { // if (has_external_update_url)
255 CHECK(has_external_update_url
); // Checking of keys above ensures this.
256 if (download_location_
== Manifest::INVALID_LOCATION
) {
257 LOG(WARNING
) << "This provider does not support installing external "
258 << "extensions from update URLs.";
261 GURL
update_url(external_update_url
);
262 if (!update_url
.is_valid()) {
263 LOG(WARNING
) << "Malformed extension dictionary for extension: "
264 << extension_id
.c_str() << ". Key " << kExternalUpdateUrl
265 << " has value \"" << external_update_url
266 << "\", which is not a valid URL.";
269 service_
->OnExternalExtensionUpdateUrlFound(
270 extension_id
, update_url
, download_location_
);
274 for (std::set
<std::string
>::iterator it
= unsupported_extensions
.begin();
275 it
!= unsupported_extensions
.end(); ++it
) {
276 // Remove extension for the list of know external extensions. The extension
277 // will be uninstalled later because provider doesn't provide it anymore.
278 prefs_
->Remove(*it
, NULL
);
281 service_
->OnExternalProviderReady(this);
284 void ExternalProviderImpl::ServiceShutdown() {
288 bool ExternalProviderImpl::IsReady() const {
292 bool ExternalProviderImpl::HasExtension(
293 const std::string
& id
) const {
294 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
297 return prefs_
->HasKey(id
);
300 bool ExternalProviderImpl::GetExtensionDetails(
301 const std::string
& id
, Manifest::Location
* location
,
302 scoped_ptr
<Version
>* version
) const {
303 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
306 base::DictionaryValue
* extension
= NULL
;
307 if (!prefs_
->GetDictionary(id
, &extension
))
310 Manifest::Location loc
= Manifest::INVALID_LOCATION
;
311 if (extension
->HasKey(kExternalUpdateUrl
)) {
312 loc
= download_location_
;
314 } else if (extension
->HasKey(kExternalCrx
)) {
317 std::string external_version
;
318 if (!extension
->GetString(kExternalVersion
, &external_version
))
322 version
->reset(new Version(external_version
));
325 NOTREACHED(); // Chrome should not allow prefs to get into this state.
336 void ExternalProviderImpl::CreateExternalProviders(
337 VisitorInterface
* service
,
339 ProviderCollection
* provider_list
) {
340 // Policies are mandatory so they can't be skipped with command line flag.
341 provider_list
->push_back(
342 linked_ptr
<ExternalProviderInterface
>(
343 new ExternalProviderImpl(
345 new ExternalPolicyLoader(profile
),
347 Manifest::INVALID_LOCATION
,
348 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
349 Extension::NO_FLAGS
)));
351 // In tests don't install extensions from default external sources.
352 // It would only slowdown tests and make them flaky.
353 if (CommandLine::ForCurrentProcess()->HasSwitch(
354 switches::kDisableDefaultApps
))
357 // No external app install in app mode.
358 if (chrome::IsRunningInForcedAppMode())
361 // On Mac OS, items in /Library/... should be written by the superuser.
362 // Check that all components of the path are writable by root only.
363 ExternalPrefLoader::Options check_admin_permissions_on_mac
;
364 #if defined(OS_MACOSX)
365 check_admin_permissions_on_mac
=
366 ExternalPrefLoader::ENSURE_PATH_CONTROLLED_BY_ADMIN
;
368 check_admin_permissions_on_mac
= ExternalPrefLoader::NONE
;
371 bool is_chromeos_demo_session
= false;
372 int bundled_extension_creation_flags
= Extension::NO_FLAGS
;
373 #if defined(OS_CHROMEOS)
374 chromeos::UserManager
* user_manager
= chromeos::UserManager::Get();
375 is_chromeos_demo_session
=
376 user_manager
&& user_manager
->IsLoggedInAsDemoUser() &&
377 g_browser_process
->browser_policy_connector()->GetDeviceMode() ==
378 policy::DEVICE_MODE_RETAIL_KIOSK
;
379 bundled_extension_creation_flags
= Extension::FROM_WEBSTORE
|
380 Extension::WAS_INSTALLED_BY_DEFAULT
;
383 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
384 if (!profile
->IsManaged()) {
385 provider_list
->push_back(
386 linked_ptr
<ExternalProviderInterface
>(
387 new ExternalProviderImpl(
389 new ExternalPrefLoader(
390 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
,
391 ExternalPrefLoader::NONE
),
393 Manifest::EXTERNAL_PREF
,
394 Manifest::EXTERNAL_PREF_DOWNLOAD
,
395 bundled_extension_creation_flags
)));
399 #if defined(OS_CHROMEOS)
400 if (!is_chromeos_demo_session
) {
401 int external_apps_path_id
= profile
->IsManaged() ?
402 chrome::DIR_MANAGED_USERS_DEFAULT_APPS
:
403 chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS
;
404 provider_list
->push_back(
405 linked_ptr
<ExternalProviderInterface
>(
406 new ExternalProviderImpl(
408 new chromeos::ExternalPrefCacheLoader(external_apps_path_id
),
410 Manifest::EXTERNAL_PREF
,
411 Manifest::EXTERNAL_PREF_DOWNLOAD
,
412 bundled_extension_creation_flags
)));
415 policy::AppPackUpdater
* app_pack_updater
=
416 g_browser_process
->browser_policy_connector()->GetAppPackUpdater();
417 if (is_chromeos_demo_session
&& app_pack_updater
&&
418 !app_pack_updater
->created_external_loader()) {
419 provider_list
->push_back(
420 linked_ptr
<ExternalProviderInterface
>(
421 new ExternalProviderImpl(
423 app_pack_updater
->CreateExternalLoader(),
425 Manifest::EXTERNAL_PREF
,
426 Manifest::INVALID_LOCATION
,
427 Extension::NO_FLAGS
)));
431 if (!profile
->IsManaged() && !is_chromeos_demo_session
) {
432 provider_list
->push_back(
433 linked_ptr
<ExternalProviderInterface
>(
434 new ExternalProviderImpl(
436 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS
,
437 check_admin_permissions_on_mac
),
439 Manifest::EXTERNAL_PREF
,
440 Manifest::EXTERNAL_PREF_DOWNLOAD
,
441 bundled_extension_creation_flags
)));
443 #if defined(OS_CHROMEOS) || defined (OS_MACOSX)
444 // Define a per-user source of external extensions.
445 // On Chrome OS, this serves as a source for OEM customization.
446 provider_list
->push_back(
447 linked_ptr
<ExternalProviderInterface
>(
448 new ExternalProviderImpl(
450 new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS
,
451 ExternalPrefLoader::NONE
),
453 Manifest::EXTERNAL_PREF
,
454 Manifest::EXTERNAL_PREF_DOWNLOAD
,
455 Extension::NO_FLAGS
)));
459 provider_list
->push_back(
460 linked_ptr
<ExternalProviderInterface
>(
461 new ExternalProviderImpl(
463 new ExternalRegistryLoader
,
465 Manifest::EXTERNAL_REGISTRY
,
466 Manifest::INVALID_LOCATION
,
467 Extension::NO_FLAGS
)));
470 #if !defined(OS_CHROMEOS)
471 // The default apps are installed as INTERNAL but use the external
472 // extension installer codeflow.
473 provider_list
->push_back(
474 linked_ptr
<ExternalProviderInterface
>(
475 new default_apps::Provider(
478 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS
,
479 ExternalPrefLoader::NONE
),
481 Manifest::INVALID_LOCATION
,
482 Extension::FROM_WEBSTORE
|
483 Extension::WAS_INSTALLED_BY_DEFAULT
)));
486 provider_list
->push_back(
487 linked_ptr
<ExternalProviderInterface
>(
488 new ExternalProviderImpl(
490 new ExternalComponentLoader(),
492 Manifest::INVALID_LOCATION
,
493 Manifest::EXTERNAL_POLICY_DOWNLOAD
,
494 Extension::FROM_WEBSTORE
| Extension::WAS_INSTALLED_BY_DEFAULT
)));
498 } // namespace extensions