Revert 270694 "A bunch of remaining parts of extension content v..."
[chromium-blink-merge.git] / chrome / browser / extensions / extension_system_impl.cc
blobdab00948c4b3f9a7fda1d3412e252cc6f975b17f
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"
8 #include "base/bind.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/user_script_master.h"
30 #include "chrome/browser/profiles/profile.h"
31 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/chrome_version_info.h"
35 #include "chrome/common/extensions/features/feature_channel.h"
36 #include "chrome/common/extensions/manifest_url_handler.h"
37 #include "content/public/browser/browser_thread.h"
38 #include "content/public/browser/url_data_source.h"
39 #include "extensions/browser/content_verifier.h"
40 #include "extensions/browser/content_verifier_delegate.h"
41 #include "extensions/browser/event_router.h"
42 #include "extensions/browser/extension_pref_store.h"
43 #include "extensions/browser/extension_pref_value_map.h"
44 #include "extensions/browser/extension_pref_value_map_factory.h"
45 #include "extensions/browser/extension_prefs.h"
46 #include "extensions/browser/extension_registry.h"
47 #include "extensions/browser/info_map.h"
48 #include "extensions/browser/lazy_background_task_queue.h"
49 #include "extensions/browser/management_policy.h"
50 #include "extensions/browser/process_manager.h"
51 #include "extensions/browser/quota_service.h"
52 #include "extensions/browser/runtime_data.h"
53 #include "extensions/common/constants.h"
54 #include "extensions/common/extension.h"
55 #include "extensions/common/manifest.h"
57 #if defined(ENABLE_NOTIFICATIONS)
58 #include "chrome/browser/notifications/desktop_notification_service.h"
59 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
60 #include "ui/message_center/notifier_settings.h"
61 #endif
63 #if defined(OS_CHROMEOS)
64 #include "chrome/browser/app_mode/app_mode_utils.h"
65 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
66 #include "chrome/browser/chromeos/login/user.h"
67 #include "chrome/browser/chromeos/login/user_manager.h"
68 #include "chrome/browser/chromeos/policy/device_local_account.h"
69 #include "chromeos/chromeos_switches.h"
70 #include "chromeos/login/login_state.h"
71 #endif
73 using content::BrowserThread;
75 namespace extensions {
78 // ExtensionSystemImpl::Shared
81 ExtensionSystemImpl::Shared::Shared(Profile* profile)
82 : profile_(profile) {
85 ExtensionSystemImpl::Shared::~Shared() {
88 void ExtensionSystemImpl::Shared::InitPrefs() {
89 lazy_background_task_queue_.reset(new LazyBackgroundTaskQueue(profile_));
90 event_router_.reset(new EventRouter(profile_, ExtensionPrefs::Get(profile_)));
91 // TODO(yoz): Remove once crbug.com/159265 is fixed.
92 #if defined(ENABLE_EXTENSIONS)
93 // Two state stores. The latter, which contains declarative rules, must be
94 // loaded immediately so that the rules are ready before we issue network
95 // requests.
96 state_store_.reset(new StateStore(
97 profile_,
98 profile_->GetPath().AppendASCII(extensions::kStateStoreName),
99 true));
101 rules_store_.reset(new StateStore(
102 profile_,
103 profile_->GetPath().AppendASCII(extensions::kRulesStoreName),
104 false));
106 blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
108 standard_management_policy_provider_.reset(
109 new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
111 #if defined (OS_CHROMEOS)
112 const chromeos::User* user = chromeos::UserManager::Get()->GetActiveUser();
113 policy::DeviceLocalAccount::Type device_local_account_type;
114 if (user && policy::IsDeviceLocalAccountUser(user->email(),
115 &device_local_account_type)) {
116 device_local_account_management_policy_provider_.reset(
117 new chromeos::DeviceLocalAccountManagementPolicyProvider(
118 device_local_account_type));
120 #endif // defined (OS_CHROMEOS)
122 #endif // defined(ENABLE_EXTENSIONS)
125 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
126 // TODO(yoz): Remove once crbug.com/159265 is fixed.
127 #if defined(ENABLE_EXTENSIONS)
128 DCHECK(standard_management_policy_provider_.get());
129 management_policy_->RegisterProvider(
130 standard_management_policy_provider_.get());
132 #if defined (OS_CHROMEOS)
133 if (device_local_account_management_policy_provider_) {
134 management_policy_->RegisterProvider(
135 device_local_account_management_policy_provider_.get());
137 #endif // defined (OS_CHROMEOS)
139 management_policy_->RegisterProvider(install_verifier_.get());
141 #endif // defined(ENABLE_EXTENSIONS)
144 namespace {
146 class ContentVerifierDelegateImpl : public ContentVerifierDelegate {
147 public:
148 explicit ContentVerifierDelegateImpl(ExtensionService* service)
149 : service_(service->AsWeakPtr()) {}
151 virtual ~ContentVerifierDelegateImpl() {}
153 virtual bool ShouldBeVerified(const Extension& extension) OVERRIDE {
154 return ((extension.is_extension() || extension.is_legacy_packaged_app()) &&
155 ManifestURL::UpdatesFromGallery(&extension) &&
156 Manifest::IsAutoUpdateableLocation(extension.location()));
159 virtual const ContentVerifierKey& PublicKey() OVERRIDE {
160 static ContentVerifierKey key(
161 extension_misc::kWebstoreSignaturesPublicKey,
162 extension_misc::kWebstoreSignaturesPublicKeySize);
163 return key;
166 virtual GURL GetSignatureFetchUrl(const std::string& extension_id,
167 const base::Version& version) OVERRIDE {
168 return GURL();
171 virtual void VerifyFailed(const std::string& extension_id) OVERRIDE {
172 if (service_)
173 service_->DisableExtension(extension_id, Extension::DISABLE_CORRUPTED);
176 private:
177 base::WeakPtr<ExtensionService> service_;
178 DISALLOW_COPY_AND_ASSIGN(ContentVerifierDelegateImpl);
181 } // namespace
183 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled) {
184 const CommandLine* command_line = CommandLine::ForCurrentProcess();
186 navigation_observer_.reset(new NavigationObserver(profile_));
188 bool allow_noisy_errors = !command_line->HasSwitch(switches::kNoErrorDialogs);
189 ExtensionErrorReporter::Init(allow_noisy_errors);
191 user_script_master_ = new UserScriptMaster(profile_);
193 // ExtensionService depends on RuntimeData.
194 runtime_data_.reset(new RuntimeData(ExtensionRegistry::Get(profile_)));
196 bool autoupdate_enabled = !profile_->IsGuestSession();
197 #if defined(OS_CHROMEOS)
198 if (!extensions_enabled)
199 autoupdate_enabled = false;
200 #endif
201 extension_service_.reset(new ExtensionService(
202 profile_,
203 CommandLine::ForCurrentProcess(),
204 profile_->GetPath().AppendASCII(extensions::kInstallDirectoryName),
205 ExtensionPrefs::Get(profile_),
206 blacklist_.get(),
207 autoupdate_enabled,
208 extensions_enabled,
209 &ready_));
211 // These services must be registered before the ExtensionService tries to
212 // load any extensions.
214 install_verifier_.reset(
215 new InstallVerifier(ExtensionPrefs::Get(profile_), profile_));
216 install_verifier_->Init();
217 content_verifier_ = new ContentVerifier(
218 profile_, new ContentVerifierDelegateImpl(extension_service_.get()));
219 content_verifier_->Start();
220 info_map()->SetContentVerifier(content_verifier_.get());
222 management_policy_.reset(new ManagementPolicy);
223 RegisterManagementPolicyProviders();
226 bool skip_session_extensions = false;
227 #if defined(OS_CHROMEOS)
228 // Skip loading session extensions if we are not in a user session.
229 skip_session_extensions = !chromeos::LoginState::Get()->IsUserLoggedIn();
230 if (chrome::IsRunningInForcedAppMode()) {
231 extension_service_->component_loader()->
232 AddDefaultComponentExtensionsForKioskMode(skip_session_extensions);
233 } else {
234 extension_service_->component_loader()->AddDefaultComponentExtensions(
235 skip_session_extensions);
237 #else
238 extension_service_->component_loader()->AddDefaultComponentExtensions(
239 skip_session_extensions);
240 #endif
241 if (command_line->HasSwitch(switches::kLoadComponentExtension)) {
242 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
243 switches::kLoadComponentExtension);
244 base::StringTokenizerT<CommandLine::StringType,
245 CommandLine::StringType::const_iterator> t(path_list,
246 FILE_PATH_LITERAL(","));
247 while (t.GetNext()) {
248 // Load the component extension manifest synchronously.
249 // Blocking the UI thread is acceptable here since
250 // this flag designated for developers.
251 base::ThreadRestrictions::ScopedAllowIO allow_io;
252 extension_service_->component_loader()->AddOrReplace(
253 base::FilePath(t.token()));
256 extension_service_->Init();
258 // Make the chrome://extension-icon/ resource available.
259 content::URLDataSource::Add(profile_, new ExtensionIconSource(profile_));
261 extension_warning_service_.reset(new ExtensionWarningService(profile_));
262 extension_warning_badge_service_.reset(
263 new ExtensionWarningBadgeService(profile_));
264 extension_warning_service_->AddObserver(
265 extension_warning_badge_service_.get());
266 error_console_.reset(new ErrorConsole(profile_));
267 quota_service_.reset(new QuotaService);
269 if (extensions_enabled) {
270 // Load any extensions specified with --load-extension.
271 // TODO(yoz): Seems like this should move into ExtensionService::Init.
272 // But maybe it's no longer important.
273 if (command_line->HasSwitch(switches::kLoadExtension)) {
274 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
275 switches::kLoadExtension);
276 base::StringTokenizerT<CommandLine::StringType,
277 CommandLine::StringType::const_iterator> t(path_list,
278 FILE_PATH_LITERAL(","));
279 while (t.GetNext()) {
280 std::string extension_id;
281 UnpackedInstaller::Create(extension_service_.get())->
282 LoadFromCommandLine(base::FilePath(t.token()), &extension_id);
288 void ExtensionSystemImpl::Shared::Shutdown() {
289 if (extension_warning_service_) {
290 extension_warning_service_->RemoveObserver(
291 extension_warning_badge_service_.get());
293 if (content_verifier_)
294 content_verifier_->Shutdown();
295 if (extension_service_)
296 extension_service_->Shutdown();
299 StateStore* ExtensionSystemImpl::Shared::state_store() {
300 return state_store_.get();
303 StateStore* ExtensionSystemImpl::Shared::rules_store() {
304 return rules_store_.get();
307 ExtensionService* ExtensionSystemImpl::Shared::extension_service() {
308 return extension_service_.get();
311 RuntimeData* ExtensionSystemImpl::Shared::runtime_data() {
312 return runtime_data_.get();
315 ManagementPolicy* ExtensionSystemImpl::Shared::management_policy() {
316 return management_policy_.get();
319 UserScriptMaster* ExtensionSystemImpl::Shared::user_script_master() {
320 return user_script_master_.get();
323 InfoMap* ExtensionSystemImpl::Shared::info_map() {
324 if (!extension_info_map_.get())
325 extension_info_map_ = new InfoMap();
326 return extension_info_map_.get();
329 LazyBackgroundTaskQueue*
330 ExtensionSystemImpl::Shared::lazy_background_task_queue() {
331 return lazy_background_task_queue_.get();
334 EventRouter* ExtensionSystemImpl::Shared::event_router() {
335 return event_router_.get();
338 ExtensionWarningService* ExtensionSystemImpl::Shared::warning_service() {
339 return extension_warning_service_.get();
342 Blacklist* ExtensionSystemImpl::Shared::blacklist() {
343 return blacklist_.get();
346 ErrorConsole* ExtensionSystemImpl::Shared::error_console() {
347 return error_console_.get();
350 InstallVerifier* ExtensionSystemImpl::Shared::install_verifier() {
351 return install_verifier_.get();
354 QuotaService* ExtensionSystemImpl::Shared::quota_service() {
355 return quota_service_.get();
358 ContentVerifier* ExtensionSystemImpl::Shared::content_verifier() {
359 return content_verifier_.get();
363 // ExtensionSystemImpl
366 ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile)
367 : profile_(profile) {
368 shared_ = ExtensionSystemSharedFactory::GetForBrowserContext(profile);
370 if (profile->IsOffTheRecord()) {
371 process_manager_.reset(ProcessManager::Create(profile));
372 } else {
373 shared_->InitPrefs();
377 ExtensionSystemImpl::~ExtensionSystemImpl() {
380 void ExtensionSystemImpl::Shutdown() {
381 process_manager_.reset();
384 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled) {
385 DCHECK(!profile_->IsOffTheRecord());
386 if (user_script_master() || extension_service())
387 return; // Already initialized.
389 // The InfoMap needs to be created before the ProcessManager.
390 shared_->info_map();
392 process_manager_.reset(ProcessManager::Create(profile_));
394 shared_->Init(extensions_enabled);
397 ExtensionService* ExtensionSystemImpl::extension_service() {
398 return shared_->extension_service();
401 RuntimeData* ExtensionSystemImpl::runtime_data() {
402 return shared_->runtime_data();
405 ManagementPolicy* ExtensionSystemImpl::management_policy() {
406 return shared_->management_policy();
409 UserScriptMaster* ExtensionSystemImpl::user_script_master() {
410 return shared_->user_script_master();
413 ProcessManager* ExtensionSystemImpl::process_manager() {
414 return process_manager_.get();
417 StateStore* ExtensionSystemImpl::state_store() {
418 return shared_->state_store();
421 StateStore* ExtensionSystemImpl::rules_store() {
422 return shared_->rules_store();
425 InfoMap* ExtensionSystemImpl::info_map() { return shared_->info_map(); }
427 LazyBackgroundTaskQueue* ExtensionSystemImpl::lazy_background_task_queue() {
428 return shared_->lazy_background_task_queue();
431 EventRouter* ExtensionSystemImpl::event_router() {
432 return shared_->event_router();
435 ExtensionWarningService* ExtensionSystemImpl::warning_service() {
436 return shared_->warning_service();
439 Blacklist* ExtensionSystemImpl::blacklist() {
440 return shared_->blacklist();
443 const OneShotEvent& ExtensionSystemImpl::ready() const {
444 return shared_->ready();
447 ErrorConsole* ExtensionSystemImpl::error_console() {
448 return shared_->error_console();
451 InstallVerifier* ExtensionSystemImpl::install_verifier() {
452 return shared_->install_verifier();
455 QuotaService* ExtensionSystemImpl::quota_service() {
456 return shared_->quota_service();
459 ContentVerifier* ExtensionSystemImpl::content_verifier() {
460 return shared_->content_verifier();
463 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
464 const Extension* extension) {
465 base::Time install_time;
466 if (extension->location() != Manifest::COMPONENT) {
467 install_time = ExtensionPrefs::Get(profile_)->
468 GetInstallTime(extension->id());
470 bool incognito_enabled = util::IsIncognitoEnabled(extension->id(), profile_);
472 bool notifications_disabled = false;
473 #if defined(ENABLE_NOTIFICATIONS)
474 message_center::NotifierId notifier_id(
475 message_center::NotifierId::APPLICATION,
476 extension->id());
478 DesktopNotificationService* notification_service =
479 DesktopNotificationServiceFactory::GetForProfile(profile_);
480 notifications_disabled =
481 !notification_service->IsNotifierEnabled(notifier_id);
482 #endif
484 BrowserThread::PostTask(
485 BrowserThread::IO, FROM_HERE,
486 base::Bind(&InfoMap::AddExtension, info_map(),
487 make_scoped_refptr(extension), install_time,
488 incognito_enabled, notifications_disabled));
491 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
492 const std::string& extension_id,
493 const UnloadedExtensionInfo::Reason reason) {
494 BrowserThread::PostTask(
495 BrowserThread::IO,
496 FROM_HERE,
497 base::Bind(&InfoMap::RemoveExtension, info_map(), extension_id, reason));
500 } // namespace extensions