• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/extensions/external_provider_impl.h"
6 
7 #include <set>
8 #include <vector>
9 
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/linked_ptr.h"
14 #include "base/metrics/field_trial.h"
15 #include "base/path_service.h"
16 #include "base/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/profiles/profile.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h"
30 #include "content/public/browser/browser_thread.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"
35 
36 #if defined(OS_CHROMEOS)
37 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
38 #include "chrome/browser/chromeos/extensions/external_pref_cache_loader.h"
39 #include "chrome/browser/chromeos/login/user.h"
40 #include "chrome/browser/chromeos/login/user_manager.h"
41 #include "chrome/browser/chromeos/policy/app_pack_updater.h"
42 #include "chrome/browser/chromeos/policy/device_local_account.h"
43 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
44 #include "chrome/browser/policy/browser_policy_connector.h"
45 #else
46 #include "chrome/browser/extensions/default_apps.h"
47 #endif
48 
49 #if defined(OS_WIN)
50 #include "chrome/browser/extensions/external_registry_loader_win.h"
51 #endif
52 
53 using content::BrowserThread;
54 
55 namespace extensions {
56 
57 // Constants for keeping track of extension preferences in a dictionary.
58 const char ExternalProviderImpl::kExternalCrx[] = "external_crx";
59 const char ExternalProviderImpl::kExternalVersion[] = "external_version";
60 const char ExternalProviderImpl::kExternalUpdateUrl[] = "external_update_url";
61 const char ExternalProviderImpl::kSupportedLocales[] = "supported_locales";
62 const char ExternalProviderImpl::kIsBookmarkApp[] = "is_bookmark_app";
63 const char ExternalProviderImpl::kIsFromWebstore[] = "is_from_webstore";
64 const char ExternalProviderImpl::kKeepIfPresent[] = "keep_if_present";
65 
ExternalProviderImpl(VisitorInterface * service,const scoped_refptr<ExternalLoader> & loader,Profile * profile,Manifest::Location crx_location,Manifest::Location download_location,int creation_flags)66 ExternalProviderImpl::ExternalProviderImpl(
67     VisitorInterface* service,
68     const scoped_refptr<ExternalLoader>& loader,
69     Profile* profile,
70     Manifest::Location crx_location,
71     Manifest::Location download_location,
72     int creation_flags)
73     : crx_location_(crx_location),
74       download_location_(download_location),
75       service_(service),
76       ready_(false),
77       loader_(loader),
78       profile_(profile),
79       creation_flags_(creation_flags),
80       auto_acknowledge_(false) {
81   loader_->Init(this);
82 }
83 
~ExternalProviderImpl()84 ExternalProviderImpl::~ExternalProviderImpl() {
85   CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86   loader_->OwnerShutdown();
87 }
88 
VisitRegisteredExtension()89 void ExternalProviderImpl::VisitRegisteredExtension() {
90   // The loader will call back to SetPrefs.
91   loader_->StartLoading();
92 }
93 
SetPrefs(base::DictionaryValue * prefs)94 void ExternalProviderImpl::SetPrefs(base::DictionaryValue* prefs) {
95   CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
96 
97   // Check if the service is still alive. It is possible that it went
98   // away while |loader_| was working on the FILE thread.
99   if (!service_) return;
100 
101   prefs_.reset(prefs);
102   ready_ = true;  // Queries for extensions are allowed from this point.
103 
104   // Set of unsupported extensions that need to be deleted from prefs_.
105   std::set<std::string> unsupported_extensions;
106 
107   // Notify ExtensionService about all the extensions this provider has.
108   for (base::DictionaryValue::Iterator i(*prefs_); !i.IsAtEnd(); i.Advance()) {
109     const std::string& extension_id = i.key();
110     const base::DictionaryValue* extension = NULL;
111 
112     if (!Extension::IdIsValid(extension_id)) {
113       LOG(WARNING) << "Malformed extension dictionary: key "
114                    << extension_id.c_str() << " is not a valid id.";
115       continue;
116     }
117 
118     if (!i.value().GetAsDictionary(&extension)) {
119       LOG(WARNING) << "Malformed extension dictionary: key "
120                    << extension_id.c_str()
121                    << " has a value that is not a dictionary.";
122       continue;
123     }
124 
125     base::FilePath::StringType external_crx;
126     const Value* external_version_value = NULL;
127     std::string external_version;
128     std::string external_update_url;
129 
130     bool has_external_crx = extension->GetString(kExternalCrx, &external_crx);
131 
132     bool has_external_version = false;
133     if (extension->Get(kExternalVersion, &external_version_value)) {
134       if (external_version_value->IsType(Value::TYPE_STRING)) {
135         external_version_value->GetAsString(&external_version);
136         has_external_version = true;
137       } else {
138         LOG(WARNING) << "Malformed extension dictionary for extension: "
139                      << extension_id.c_str() << ". " << kExternalVersion
140                      << " value must be a string.";
141         continue;
142       }
143     }
144 
145     bool has_external_update_url = extension->GetString(kExternalUpdateUrl,
146                                                         &external_update_url);
147     if (has_external_crx != has_external_version) {
148       LOG(WARNING) << "Malformed extension dictionary for extension: "
149                    << extension_id.c_str() << ".  " << kExternalCrx
150                    << " and " << kExternalVersion << " must be used together.";
151       continue;
152     }
153 
154     if (has_external_crx == has_external_update_url) {
155       LOG(WARNING) << "Malformed extension dictionary for extension: "
156                    << extension_id.c_str() << ".  Exactly one of the "
157                    << "followng keys should be used: " << kExternalCrx
158                    << ", " << kExternalUpdateUrl << ".";
159       continue;
160     }
161 
162     // Check that extension supports current browser locale.
163     const base::ListValue* supported_locales = NULL;
164     if (extension->GetList(kSupportedLocales, &supported_locales)) {
165       std::vector<std::string> browser_locales;
166       l10n_util::GetParentLocales(g_browser_process->GetApplicationLocale(),
167                                   &browser_locales);
168 
169       size_t num_locales = supported_locales->GetSize();
170       bool locale_supported = false;
171       for (size_t j = 0; j < num_locales; j++) {
172         std::string current_locale;
173         if (supported_locales->GetString(j, &current_locale) &&
174             l10n_util::IsValidLocaleSyntax(current_locale)) {
175           current_locale = l10n_util::NormalizeLocale(current_locale);
176           if (std::find(browser_locales.begin(), browser_locales.end(),
177                         current_locale) != browser_locales.end()) {
178             locale_supported = true;
179             break;
180           }
181         } else {
182           LOG(WARNING) << "Unrecognized locale '" << current_locale
183                        << "' found as supported locale for extension: "
184                        << extension_id;
185         }
186       }
187 
188       if (!locale_supported) {
189         unsupported_extensions.insert(extension_id);
190         VLOG(1) << "Skip installing (or uninstall) external extension: "
191                 << extension_id << " because the extension doesn't support "
192                 << "the browser locale.";
193         continue;
194       }
195     }
196 
197     int creation_flags = creation_flags_;
198     bool is_bookmark_app;
199     if (extension->GetBoolean(kIsBookmarkApp, &is_bookmark_app) &&
200         is_bookmark_app) {
201       creation_flags |= Extension::FROM_BOOKMARK;
202     }
203     bool is_from_webstore;
204     if (extension->GetBoolean(kIsFromWebstore, &is_from_webstore) &&
205         is_from_webstore) {
206       creation_flags |= Extension::FROM_WEBSTORE;
207     }
208     bool keep_if_present;
209     if (extension->GetBoolean(kKeepIfPresent, &keep_if_present) &&
210         keep_if_present && profile_) {
211       ExtensionServiceInterface* extension_service =
212           ExtensionSystem::Get(profile_)->extension_service();
213       const Extension* extension = extension_service ?
214           extension_service->GetExtensionById(extension_id, true) : NULL;
215       if (!extension) {
216         VLOG(1) << "Skip installing (or uninstall) external extension: "
217                 << extension_id << " because the extension should be kept "
218                 << "only if it is already installed.";
219         continue;
220       }
221     }
222 
223     if (has_external_crx) {
224       if (crx_location_ == Manifest::INVALID_LOCATION) {
225         LOG(WARNING) << "This provider does not support installing external "
226                      << "extensions from crx files.";
227         continue;
228       }
229       if (external_crx.find(base::FilePath::kParentDirectory) !=
230           base::StringPiece::npos) {
231         LOG(WARNING) << "Path traversal not allowed in path: "
232                      << external_crx.c_str();
233         continue;
234       }
235 
236       // If the path is relative, and the provider has a base path,
237       // build the absolute path to the crx file.
238       base::FilePath path(external_crx);
239       if (!path.IsAbsolute()) {
240         base::FilePath base_path = loader_->GetBaseCrxFilePath();
241         if (base_path.empty()) {
242           LOG(WARNING) << "File path " << external_crx.c_str()
243                        << " is relative.  An absolute path is required.";
244           continue;
245         }
246         path = base_path.Append(external_crx);
247       }
248 
249       Version version(external_version);
250       if (!version.IsValid()) {
251         LOG(WARNING) << "Malformed extension dictionary for extension: "
252                      << extension_id.c_str() << ".  Invalid version string \""
253                      << external_version << "\".";
254         continue;
255       }
256       service_->OnExternalExtensionFileFound(extension_id, &version, path,
257                                              crx_location_, creation_flags,
258                                              auto_acknowledge_);
259     } else {  // if (has_external_update_url)
260       CHECK(has_external_update_url);  // Checking of keys above ensures this.
261       if (download_location_ == Manifest::INVALID_LOCATION) {
262         LOG(WARNING) << "This provider does not support installing external "
263                      << "extensions from update URLs.";
264         continue;
265       }
266       GURL update_url(external_update_url);
267       if (!update_url.is_valid()) {
268         LOG(WARNING) << "Malformed extension dictionary for extension: "
269                      << extension_id.c_str() << ".  Key " << kExternalUpdateUrl
270                      << " has value \"" << external_update_url
271                      << "\", which is not a valid URL.";
272         continue;
273       }
274       service_->OnExternalExtensionUpdateUrlFound(
275           extension_id, update_url, download_location_, creation_flags,
276           auto_acknowledge_);
277     }
278   }
279 
280   for (std::set<std::string>::iterator it = unsupported_extensions.begin();
281        it != unsupported_extensions.end(); ++it) {
282     // Remove extension for the list of know external extensions. The extension
283     // will be uninstalled later because provider doesn't provide it anymore.
284     prefs_->Remove(*it, NULL);
285   }
286 
287   service_->OnExternalProviderReady(this);
288 }
289 
ServiceShutdown()290 void ExternalProviderImpl::ServiceShutdown() {
291   service_ = NULL;
292 }
293 
IsReady() const294 bool ExternalProviderImpl::IsReady() const {
295   return ready_;
296 }
297 
HasExtension(const std::string & id) const298 bool ExternalProviderImpl::HasExtension(
299     const std::string& id) const {
300   CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
301   CHECK(prefs_.get());
302   CHECK(ready_);
303   return prefs_->HasKey(id);
304 }
305 
GetExtensionDetails(const std::string & id,Manifest::Location * location,scoped_ptr<Version> * version) const306 bool ExternalProviderImpl::GetExtensionDetails(
307     const std::string& id, Manifest::Location* location,
308     scoped_ptr<Version>* version) const {
309   CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
310   CHECK(prefs_.get());
311   CHECK(ready_);
312   base::DictionaryValue* extension = NULL;
313   if (!prefs_->GetDictionary(id, &extension))
314     return false;
315 
316   Manifest::Location loc = Manifest::INVALID_LOCATION;
317   if (extension->HasKey(kExternalUpdateUrl)) {
318     loc = download_location_;
319 
320   } else if (extension->HasKey(kExternalCrx)) {
321     loc = crx_location_;
322 
323     std::string external_version;
324     if (!extension->GetString(kExternalVersion, &external_version))
325       return false;
326 
327     if (version)
328       version->reset(new Version(external_version));
329 
330   } else {
331     NOTREACHED();  // Chrome should not allow prefs to get into this state.
332     return false;
333   }
334 
335   if (location)
336     *location = loc;
337 
338   return true;
339 }
340 
341 // static
CreateExternalProviders(VisitorInterface * service,Profile * profile,ProviderCollection * provider_list)342 void ExternalProviderImpl::CreateExternalProviders(
343     VisitorInterface* service,
344     Profile* profile,
345     ProviderCollection* provider_list) {
346   scoped_refptr<ExternalLoader> external_loader;
347   extensions::Manifest::Location crx_location = Manifest::INVALID_LOCATION;
348 #if defined(OS_CHROMEOS)
349   bool is_chrome_os_public_session = false;
350   const chromeos::User* user =
351       chromeos::UserManager::Get()->GetUserByProfile(profile);
352   policy::DeviceLocalAccount::Type account_type;
353   if (user && policy::IsDeviceLocalAccountUser(user->email(), &account_type)) {
354     if (account_type == policy::DeviceLocalAccount::TYPE_PUBLIC_SESSION)
355       is_chrome_os_public_session = true;
356     policy::DeviceLocalAccountPolicyBroker* broker =
357         g_browser_process->browser_policy_connector()->
358             GetDeviceLocalAccountPolicyService()->
359                 GetBrokerForUser(user->email());
360     if (broker) {
361       external_loader = broker->extension_loader();
362       crx_location = Manifest::EXTERNAL_POLICY;
363     } else {
364       NOTREACHED();
365     }
366   } else {
367     external_loader = new ExternalPolicyLoader(profile);
368   }
369 #else
370   external_loader = new ExternalPolicyLoader(profile);
371 #endif
372 
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(
378                 service,
379                 external_loader,
380                 profile,
381                 crx_location,
382                 Manifest::EXTERNAL_POLICY_DOWNLOAD,
383                 Extension::NO_FLAGS)));
384   }
385 
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))
390     return;
391 
392   // No external app install in app mode.
393   if (chrome::IsRunningInForcedAppMode())
394     return;
395 
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;
402 #else
403   check_admin_permissions_on_mac = ExternalPrefLoader::NONE;
404 #endif
405 
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       g_browser_process->browser_policy_connector()->GetDeviceMode() ==
413           policy::DEVICE_MODE_RETAIL_KIOSK;
414   bundled_extension_creation_flags = Extension::FROM_WEBSTORE |
415       Extension::WAS_INSTALLED_BY_DEFAULT;
416 #endif
417 
418 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
419   if (!profile->IsManaged()) {
420     provider_list->push_back(
421         linked_ptr<ExternalProviderInterface>(
422             new ExternalProviderImpl(
423                 service,
424                 new ExternalPrefLoader(
425                     chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS,
426                     ExternalPrefLoader::NONE),
427                 profile,
428                 Manifest::EXTERNAL_PREF,
429                 Manifest::EXTERNAL_PREF_DOWNLOAD,
430                 bundled_extension_creation_flags)));
431   }
432 #endif
433 
434 #if defined(OS_CHROMEOS)
435   if (!is_chromeos_demo_session && !is_chrome_os_public_session) {
436     int external_apps_path_id = profile->IsManaged() ?
437         chrome::DIR_MANAGED_USERS_DEFAULT_APPS :
438         chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS;
439     provider_list->push_back(
440         linked_ptr<ExternalProviderInterface>(
441             new ExternalProviderImpl(
442                 service,
443                 new chromeos::ExternalPrefCacheLoader(
444                     external_apps_path_id, profile),
445                 profile,
446                 Manifest::EXTERNAL_PREF,
447                 Manifest::EXTERNAL_PREF_DOWNLOAD,
448                 bundled_extension_creation_flags)));
449   }
450 
451   policy::AppPackUpdater* app_pack_updater =
452       g_browser_process->browser_policy_connector()->GetAppPackUpdater();
453   if (is_chromeos_demo_session && app_pack_updater &&
454       !app_pack_updater->created_external_loader()) {
455     provider_list->push_back(
456         linked_ptr<ExternalProviderInterface>(
457           new ExternalProviderImpl(
458               service,
459               app_pack_updater->CreateExternalLoader(),
460               profile,
461               Manifest::EXTERNAL_PREF,
462               Manifest::INVALID_LOCATION,
463               Extension::NO_FLAGS)));
464   }
465 #endif
466 
467   if (!profile->IsManaged() && !is_chromeos_demo_session) {
468     provider_list->push_back(
469         linked_ptr<ExternalProviderInterface>(
470             new ExternalProviderImpl(
471                 service,
472                 new ExternalPrefLoader(chrome::DIR_EXTERNAL_EXTENSIONS,
473                                        check_admin_permissions_on_mac),
474                 profile,
475                 Manifest::EXTERNAL_PREF,
476                 Manifest::EXTERNAL_PREF_DOWNLOAD,
477                 bundled_extension_creation_flags)));
478 
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) {
483 #endif
484 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
485       provider_list->push_back(
486           linked_ptr<ExternalProviderInterface>(
487               new ExternalProviderImpl(
488                   service,
489                   new ExternalPrefLoader(chrome::DIR_USER_EXTERNAL_EXTENSIONS,
490                                          ExternalPrefLoader::NONE),
491                   profile,
492                   Manifest::EXTERNAL_PREF,
493                   Manifest::EXTERNAL_PREF_DOWNLOAD,
494                   Extension::NO_FLAGS)));
495 #endif
496 #if defined(OS_CHROMEOS)
497     }
498 #endif
499 
500 #if defined(OS_WIN)
501     provider_list->push_back(
502         linked_ptr<ExternalProviderInterface>(
503             new ExternalProviderImpl(
504                 service,
505                 new ExternalRegistryLoader,
506                 profile,
507                 Manifest::EXTERNAL_REGISTRY,
508                 Manifest::EXTERNAL_PREF_DOWNLOAD,
509                 Extension::NO_FLAGS)));
510 #endif
511 
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(
518                 profile,
519                 service,
520                 new ExternalPrefLoader(chrome::DIR_DEFAULT_APPS,
521                                        ExternalPrefLoader::NONE),
522                 Manifest::INTERNAL,
523                 Manifest::INVALID_LOCATION,
524                 Extension::FROM_WEBSTORE |
525                     Extension::WAS_INSTALLED_BY_DEFAULT)));
526 #endif
527 
528     provider_list->push_back(
529       linked_ptr<ExternalProviderInterface>(
530         new ExternalProviderImpl(
531             service,
532             new ExternalComponentLoader(),
533             profile,
534             Manifest::INVALID_LOCATION,
535             Manifest::EXTERNAL_COMPONENT,
536             Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT)));
537   }
538 }
539 
540 }  // namespace extensions
541