1 // Copyright 2014 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/extension_system_impl.h"
7 #include "base/base_switches.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/strings/string_tokenizer.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/content_settings/cookie_settings.h"
15 #include "chrome/browser/extensions/blacklist.h"
16 #include "chrome/browser/extensions/component_loader.h"
17 #include "chrome/browser/extensions/error_console/error_console.h"
18 #include "chrome/browser/extensions/extension_error_reporter.h"
19 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/extensions/extension_system_factory.h"
21 #include "chrome/browser/extensions/extension_util.h"
22 #include "chrome/browser/extensions/extension_warning_badge_service.h"
23 #include "chrome/browser/extensions/extension_warning_set.h"
24 #include "chrome/browser/extensions/install_verifier.h"
25 #include "chrome/browser/extensions/navigation_observer.h"
26 #include "chrome/browser/extensions/standard_management_policy_provider.h"
27 #include "chrome/browser/extensions/state_store.h"
28 #include "chrome/browser/extensions/unpacked_installer.h"
29 #include "chrome/browser/extensions/updater/manifest_fetch_data.h"
30 #include "chrome/browser/extensions/user_script_master.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/profiles/profile_manager.h"
33 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
34 #include "chrome/common/chrome_switches.h"
35 #include "chrome/common/chrome_version_info.h"
36 #include "chrome/common/extensions/extension_file_util.h"
37 #include "chrome/common/extensions/features/feature_channel.h"
38 #include "chrome/common/extensions/manifest_url_handler.h"
39 #include "content/public/browser/browser_thread.h"
40 #include "content/public/browser/url_data_source.h"
41 #include "extensions/browser/content_verifier.h"
42 #include "extensions/browser/content_verifier_delegate.h"
43 #include "extensions/browser/event_router.h"
44 #include "extensions/browser/extension_pref_store.h"
45 #include "extensions/browser/extension_pref_value_map.h"
46 #include "extensions/browser/extension_pref_value_map_factory.h"
47 #include "extensions/browser/extension_prefs.h"
48 #include "extensions/browser/extension_registry.h"
49 #include "extensions/browser/info_map.h"
50 #include "extensions/browser/lazy_background_task_queue.h"
51 #include "extensions/browser/management_policy.h"
52 #include "extensions/browser/process_manager.h"
53 #include "extensions/browser/quota_service.h"
54 #include "extensions/browser/runtime_data.h"
55 #include "extensions/common/constants.h"
56 #include "extensions/common/extension.h"
57 #include "extensions/common/manifest.h"
58 #include "net/base/escape.h"
60 #if defined(ENABLE_NOTIFICATIONS)
61 #include "chrome/browser/notifications/desktop_notification_service.h"
62 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
63 #include "ui/message_center/notifier_settings.h"
66 #if defined(OS_CHROMEOS)
67 #include "chrome/browser/app_mode/app_mode_utils.h"
68 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
69 #include "chrome/browser/chromeos/login/user.h"
70 #include "chrome/browser/chromeos/login/user_manager.h"
71 #include "chrome/browser/chromeos/policy/device_local_account.h"
72 #include "chromeos/chromeos_switches.h"
73 #include "chromeos/login/login_state.h"
76 using content::BrowserThread;
78 namespace extensions {
81 // ExtensionSystemImpl::Shared
84 ExtensionSystemImpl::Shared::Shared(Profile* profile)
88 ExtensionSystemImpl::Shared::~Shared() {
91 void ExtensionSystemImpl::Shared::InitPrefs() {
92 lazy_background_task_queue_.reset(new LazyBackgroundTaskQueue(profile_));
93 event_router_.reset(new EventRouter(profile_, ExtensionPrefs::Get(profile_)));
94 // TODO(yoz): Remove once crbug.com/159265 is fixed.
95 #if defined(ENABLE_EXTENSIONS)
96 // Two state stores. The latter, which contains declarative rules, must be
97 // loaded immediately so that the rules are ready before we issue network
99 state_store_.reset(new StateStore(
101 profile_->GetPath().AppendASCII(extensions::kStateStoreName),
104 rules_store_.reset(new StateStore(
106 profile_->GetPath().AppendASCII(extensions::kRulesStoreName),
109 blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
111 standard_management_policy_provider_.reset(
112 new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
114 #if defined (OS_CHROMEOS)
115 const chromeos::User* user = chromeos::UserManager::Get()->GetActiveUser();
116 policy::DeviceLocalAccount::Type device_local_account_type;
117 if (user && policy::IsDeviceLocalAccountUser(user->email(),
118 &device_local_account_type)) {
119 device_local_account_management_policy_provider_.reset(
120 new chromeos::DeviceLocalAccountManagementPolicyProvider(
121 device_local_account_type));
123 #endif // defined (OS_CHROMEOS)
125 #endif // defined(ENABLE_EXTENSIONS)
128 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
129 // TODO(yoz): Remove once crbug.com/159265 is fixed.
130 #if defined(ENABLE_EXTENSIONS)
131 DCHECK(standard_management_policy_provider_.get());
132 management_policy_->RegisterProvider(
133 standard_management_policy_provider_.get());
135 #if defined (OS_CHROMEOS)
136 if (device_local_account_management_policy_provider_) {
137 management_policy_->RegisterProvider(
138 device_local_account_management_policy_provider_.get());
140 #endif // defined (OS_CHROMEOS)
142 management_policy_->RegisterProvider(install_verifier_.get());
144 #endif // defined(ENABLE_EXTENSIONS)
149 class ContentVerifierDelegateImpl : public ContentVerifierDelegate {
151 explicit ContentVerifierDelegateImpl(ExtensionService* service)
152 : service_(service->AsWeakPtr()) {}
154 virtual ~ContentVerifierDelegateImpl() {}
156 virtual bool ShouldBeVerified(const Extension& extension) OVERRIDE {
157 if (!extension.is_extension() && !extension.is_legacy_packaged_app())
159 if (!Manifest::IsAutoUpdateableLocation(extension.location()))
162 if (!ManifestURL::UpdatesFromGallery(&extension)) {
163 // It's possible that the webstore update url was overridden for testing
164 // so also consider extensions with the default (production) update url
165 // to be from the store as well.
166 GURL default_webstore_url = extension_urls::GetDefaultWebstoreUpdateUrl();
167 if (ManifestURL::GetUpdateURL(&extension) != default_webstore_url)
174 virtual const ContentVerifierKey& PublicKey() OVERRIDE {
175 static ContentVerifierKey key(
176 extension_misc::kWebstoreSignaturesPublicKey,
177 extension_misc::kWebstoreSignaturesPublicKeySize);
181 virtual GURL GetSignatureFetchUrl(const std::string& extension_id,
182 const base::Version& version) OVERRIDE {
183 // TODO(asargent) Factor out common code from the extension updater's
184 // ManifestFetchData class that can be shared for use here.
185 std::vector<std::string> parts;
186 parts.push_back("uc");
187 parts.push_back("installsource=signature");
188 parts.push_back("id=" + extension_id);
189 parts.push_back("v=" + version.GetString());
190 std::string x_value =
191 net::EscapeQueryParamValue(JoinString(parts, "&"), true);
192 std::string query = "response=redirect&x=" + x_value;
194 GURL base_url = extension_urls::GetWebstoreUpdateUrl();
195 GURL::Replacements replacements;
196 replacements.SetQuery(query.c_str(), url::Component(0, query.length()));
197 return base_url.ReplaceComponents(replacements);
200 virtual std::set<base::FilePath> GetBrowserImagePaths(
201 const extensions::Extension* extension) OVERRIDE {
202 return extension_file_util::GetBrowserImagePaths(extension);
205 virtual void VerifyFailed(const std::string& extension_id) OVERRIDE {
207 service_->DisableExtension(extension_id, Extension::DISABLE_CORRUPTED);
211 base::WeakPtr<ExtensionService> service_;
212 DISALLOW_COPY_AND_ASSIGN(ContentVerifierDelegateImpl);
217 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled) {
218 const CommandLine* command_line = CommandLine::ForCurrentProcess();
220 navigation_observer_.reset(new NavigationObserver(profile_));
222 bool allow_noisy_errors = !command_line->HasSwitch(switches::kNoErrorDialogs);
223 ExtensionErrorReporter::Init(allow_noisy_errors);
225 user_script_master_ = new UserScriptMaster(profile_);
227 // ExtensionService depends on RuntimeData.
228 runtime_data_.reset(new RuntimeData(ExtensionRegistry::Get(profile_)));
230 bool autoupdate_enabled = !profile_->IsGuestSession();
231 #if defined(OS_CHROMEOS)
232 if (!extensions_enabled)
233 autoupdate_enabled = false;
235 extension_service_.reset(new ExtensionService(
237 CommandLine::ForCurrentProcess(),
238 profile_->GetPath().AppendASCII(extensions::kInstallDirectoryName),
239 ExtensionPrefs::Get(profile_),
245 // These services must be registered before the ExtensionService tries to
246 // load any extensions.
248 install_verifier_.reset(
249 new InstallVerifier(ExtensionPrefs::Get(profile_), profile_));
250 install_verifier_->Init();
251 content_verifier_ = new ContentVerifier(
252 profile_, new ContentVerifierDelegateImpl(extension_service_.get()));
253 content_verifier_->Start();
254 info_map()->SetContentVerifier(content_verifier_.get());
256 management_policy_.reset(new ManagementPolicy);
257 RegisterManagementPolicyProviders();
260 bool skip_session_extensions = false;
261 #if defined(OS_CHROMEOS)
262 // Skip loading session extensions if we are not in a user session.
263 skip_session_extensions = !chromeos::LoginState::Get()->IsUserLoggedIn();
264 if (chrome::IsRunningInForcedAppMode()) {
265 extension_service_->component_loader()->
266 AddDefaultComponentExtensionsForKioskMode(skip_session_extensions);
268 extension_service_->component_loader()->AddDefaultComponentExtensions(
269 skip_session_extensions);
272 extension_service_->component_loader()->AddDefaultComponentExtensions(
273 skip_session_extensions);
275 if (command_line->HasSwitch(switches::kLoadComponentExtension)) {
276 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
277 switches::kLoadComponentExtension);
278 base::StringTokenizerT<CommandLine::StringType,
279 CommandLine::StringType::const_iterator> t(path_list,
280 FILE_PATH_LITERAL(","));
281 while (t.GetNext()) {
282 // Load the component extension manifest synchronously.
283 // Blocking the UI thread is acceptable here since
284 // this flag designated for developers.
285 base::ThreadRestrictions::ScopedAllowIO allow_io;
286 extension_service_->component_loader()->AddOrReplace(
287 base::FilePath(t.token()));
290 extension_service_->Init();
292 // Make the chrome://extension-icon/ resource available.
293 content::URLDataSource::Add(profile_, new ExtensionIconSource(profile_));
295 extension_warning_service_.reset(new ExtensionWarningService(profile_));
296 extension_warning_badge_service_.reset(
297 new ExtensionWarningBadgeService(profile_));
298 extension_warning_service_->AddObserver(
299 extension_warning_badge_service_.get());
300 error_console_.reset(new ErrorConsole(profile_));
301 quota_service_.reset(new QuotaService);
303 if (extensions_enabled) {
304 // Load any extensions specified with --load-extension.
305 // TODO(yoz): Seems like this should move into ExtensionService::Init.
306 // But maybe it's no longer important.
307 if (command_line->HasSwitch(switches::kLoadExtension)) {
308 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
309 switches::kLoadExtension);
310 base::StringTokenizerT<CommandLine::StringType,
311 CommandLine::StringType::const_iterator> t(path_list,
312 FILE_PATH_LITERAL(","));
313 while (t.GetNext()) {
314 std::string extension_id;
315 UnpackedInstaller::Create(extension_service_.get())->
316 LoadFromCommandLine(base::FilePath(t.token()), &extension_id);
322 void ExtensionSystemImpl::Shared::Shutdown() {
323 if (extension_warning_service_) {
324 extension_warning_service_->RemoveObserver(
325 extension_warning_badge_service_.get());
327 if (content_verifier_)
328 content_verifier_->Shutdown();
329 if (extension_service_)
330 extension_service_->Shutdown();
333 StateStore* ExtensionSystemImpl::Shared::state_store() {
334 return state_store_.get();
337 StateStore* ExtensionSystemImpl::Shared::rules_store() {
338 return rules_store_.get();
341 ExtensionService* ExtensionSystemImpl::Shared::extension_service() {
342 return extension_service_.get();
345 RuntimeData* ExtensionSystemImpl::Shared::runtime_data() {
346 return runtime_data_.get();
349 ManagementPolicy* ExtensionSystemImpl::Shared::management_policy() {
350 return management_policy_.get();
353 UserScriptMaster* ExtensionSystemImpl::Shared::user_script_master() {
354 return user_script_master_.get();
357 InfoMap* ExtensionSystemImpl::Shared::info_map() {
358 if (!extension_info_map_.get())
359 extension_info_map_ = new InfoMap();
360 return extension_info_map_.get();
363 LazyBackgroundTaskQueue*
364 ExtensionSystemImpl::Shared::lazy_background_task_queue() {
365 return lazy_background_task_queue_.get();
368 EventRouter* ExtensionSystemImpl::Shared::event_router() {
369 return event_router_.get();
372 ExtensionWarningService* ExtensionSystemImpl::Shared::warning_service() {
373 return extension_warning_service_.get();
376 Blacklist* ExtensionSystemImpl::Shared::blacklist() {
377 return blacklist_.get();
380 ErrorConsole* ExtensionSystemImpl::Shared::error_console() {
381 return error_console_.get();
384 InstallVerifier* ExtensionSystemImpl::Shared::install_verifier() {
385 return install_verifier_.get();
388 QuotaService* ExtensionSystemImpl::Shared::quota_service() {
389 return quota_service_.get();
392 ContentVerifier* ExtensionSystemImpl::Shared::content_verifier() {
393 return content_verifier_.get();
397 // ExtensionSystemImpl
400 ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile)
401 : profile_(profile) {
402 shared_ = ExtensionSystemSharedFactory::GetForBrowserContext(profile);
404 if (profile->IsOffTheRecord()) {
405 process_manager_.reset(ProcessManager::Create(profile));
407 shared_->InitPrefs();
411 ExtensionSystemImpl::~ExtensionSystemImpl() {
414 void ExtensionSystemImpl::Shutdown() {
415 process_manager_.reset();
418 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled) {
419 DCHECK(!profile_->IsOffTheRecord());
420 if (user_script_master() || extension_service())
421 return; // Already initialized.
423 // The InfoMap needs to be created before the ProcessManager.
426 process_manager_.reset(ProcessManager::Create(profile_));
428 shared_->Init(extensions_enabled);
431 ExtensionService* ExtensionSystemImpl::extension_service() {
432 return shared_->extension_service();
435 RuntimeData* ExtensionSystemImpl::runtime_data() {
436 return shared_->runtime_data();
439 ManagementPolicy* ExtensionSystemImpl::management_policy() {
440 return shared_->management_policy();
443 UserScriptMaster* ExtensionSystemImpl::user_script_master() {
444 return shared_->user_script_master();
447 ProcessManager* ExtensionSystemImpl::process_manager() {
448 return process_manager_.get();
451 StateStore* ExtensionSystemImpl::state_store() {
452 return shared_->state_store();
455 StateStore* ExtensionSystemImpl::rules_store() {
456 return shared_->rules_store();
459 InfoMap* ExtensionSystemImpl::info_map() { return shared_->info_map(); }
461 LazyBackgroundTaskQueue* ExtensionSystemImpl::lazy_background_task_queue() {
462 return shared_->lazy_background_task_queue();
465 EventRouter* ExtensionSystemImpl::event_router() {
466 return shared_->event_router();
469 ExtensionWarningService* ExtensionSystemImpl::warning_service() {
470 return shared_->warning_service();
473 Blacklist* ExtensionSystemImpl::blacklist() {
474 return shared_->blacklist();
477 const OneShotEvent& ExtensionSystemImpl::ready() const {
478 return shared_->ready();
481 ErrorConsole* ExtensionSystemImpl::error_console() {
482 return shared_->error_console();
485 InstallVerifier* ExtensionSystemImpl::install_verifier() {
486 return shared_->install_verifier();
489 QuotaService* ExtensionSystemImpl::quota_service() {
490 return shared_->quota_service();
493 ContentVerifier* ExtensionSystemImpl::content_verifier() {
494 return shared_->content_verifier();
497 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
498 const Extension* extension) {
499 base::Time install_time;
500 if (extension->location() != Manifest::COMPONENT) {
501 install_time = ExtensionPrefs::Get(profile_)->
502 GetInstallTime(extension->id());
504 bool incognito_enabled = util::IsIncognitoEnabled(extension->id(), profile_);
506 bool notifications_disabled = false;
507 #if defined(ENABLE_NOTIFICATIONS)
508 message_center::NotifierId notifier_id(
509 message_center::NotifierId::APPLICATION,
512 DesktopNotificationService* notification_service =
513 DesktopNotificationServiceFactory::GetForProfile(profile_);
514 notifications_disabled =
515 !notification_service->IsNotifierEnabled(notifier_id);
518 BrowserThread::PostTask(
519 BrowserThread::IO, FROM_HERE,
520 base::Bind(&InfoMap::AddExtension, info_map(),
521 make_scoped_refptr(extension), install_time,
522 incognito_enabled, notifications_disabled));
525 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
526 const std::string& extension_id,
527 const UnloadedExtensionInfo::Reason reason) {
528 BrowserThread::PostTask(
531 base::Bind(&InfoMap::RemoveExtension, info_map(), extension_id, reason));
534 } // namespace extensions