NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / extension_system_impl.cc
blob29ac1c6fd586c25dac54cf0814a6f2308271d8a0
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/strings/string_tokenizer.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/content_settings/cookie_settings.h"
14 #include "chrome/browser/extensions/blacklist.h"
15 #include "chrome/browser/extensions/component_loader.h"
16 #include "chrome/browser/extensions/error_console/error_console.h"
17 #include "chrome/browser/extensions/extension_error_reporter.h"
18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/extension_system_factory.h"
20 #include "chrome/browser/extensions/extension_util.h"
21 #include "chrome/browser/extensions/extension_warning_badge_service.h"
22 #include "chrome/browser/extensions/extension_warning_set.h"
23 #include "chrome/browser/extensions/install_verifier.h"
24 #include "chrome/browser/extensions/navigation_observer.h"
25 #include "chrome/browser/extensions/standard_management_policy_provider.h"
26 #include "chrome/browser/extensions/state_store.h"
27 #include "chrome/browser/extensions/unpacked_installer.h"
28 #include "chrome/browser/extensions/user_script_master.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/chrome_version_info.h"
34 #include "chrome/common/extensions/features/feature_channel.h"
35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/browser/url_data_source.h"
37 #include "extensions/browser/event_router.h"
38 #include "extensions/browser/extension_pref_store.h"
39 #include "extensions/browser/extension_pref_value_map.h"
40 #include "extensions/browser/extension_pref_value_map_factory.h"
41 #include "extensions/browser/extension_prefs.h"
42 #include "extensions/browser/extension_registry.h"
43 #include "extensions/browser/info_map.h"
44 #include "extensions/browser/lazy_background_task_queue.h"
45 #include "extensions/browser/management_policy.h"
46 #include "extensions/browser/process_manager.h"
47 #include "extensions/browser/quota_service.h"
48 #include "extensions/browser/runtime_data.h"
49 #include "extensions/common/constants.h"
50 #include "extensions/common/extension.h"
51 #include "extensions/common/manifest.h"
53 #if defined(ENABLE_NOTIFICATIONS)
54 #include "chrome/browser/notifications/desktop_notification_service.h"
55 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
56 #include "ui/message_center/notifier_settings.h"
57 #endif
59 #if defined(OS_CHROMEOS)
60 #include "chrome/browser/app_mode/app_mode_utils.h"
61 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
62 #include "chrome/browser/chromeos/login/user.h"
63 #include "chrome/browser/chromeos/login/user_manager.h"
64 #include "chrome/browser/chromeos/policy/device_local_account.h"
65 #include "chromeos/chromeos_switches.h"
66 #include "chromeos/login/login_state.h"
67 #endif
69 using content::BrowserThread;
71 namespace extensions {
74 // ExtensionSystemImpl::Shared
77 ExtensionSystemImpl::Shared::Shared(Profile* profile)
78 : profile_(profile) {
81 ExtensionSystemImpl::Shared::~Shared() {
84 void ExtensionSystemImpl::Shared::InitPrefs() {
85 lazy_background_task_queue_.reset(new LazyBackgroundTaskQueue(profile_));
86 event_router_.reset(new EventRouter(profile_, ExtensionPrefs::Get(profile_)));
87 // TODO(yoz): Remove once crbug.com/159265 is fixed.
88 #if defined(ENABLE_EXTENSIONS)
89 // Two state stores. The latter, which contains declarative rules, must be
90 // loaded immediately so that the rules are ready before we issue network
91 // requests.
92 state_store_.reset(new StateStore(
93 profile_,
94 profile_->GetPath().AppendASCII(extensions::kStateStoreName),
95 true));
97 rules_store_.reset(new StateStore(
98 profile_,
99 profile_->GetPath().AppendASCII(extensions::kRulesStoreName),
100 false));
102 blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
104 standard_management_policy_provider_.reset(
105 new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
107 #if defined (OS_CHROMEOS)
108 const chromeos::User* user = chromeos::UserManager::Get()->GetActiveUser();
109 policy::DeviceLocalAccount::Type device_local_account_type;
110 if (user && policy::IsDeviceLocalAccountUser(user->email(),
111 &device_local_account_type)) {
112 device_local_account_management_policy_provider_.reset(
113 new chromeos::DeviceLocalAccountManagementPolicyProvider(
114 device_local_account_type));
116 #endif // defined (OS_CHROMEOS)
118 #endif // defined(ENABLE_EXTENSIONS)
121 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
122 // TODO(yoz): Remove once crbug.com/159265 is fixed.
123 #if defined(ENABLE_EXTENSIONS)
124 DCHECK(standard_management_policy_provider_.get());
125 management_policy_->RegisterProvider(
126 standard_management_policy_provider_.get());
128 #if defined (OS_CHROMEOS)
129 if (device_local_account_management_policy_provider_) {
130 management_policy_->RegisterProvider(
131 device_local_account_management_policy_provider_.get());
133 #endif // defined (OS_CHROMEOS)
135 management_policy_->RegisterProvider(install_verifier_.get());
137 #endif // defined(ENABLE_EXTENSIONS)
140 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled) {
141 const CommandLine* command_line = CommandLine::ForCurrentProcess();
143 navigation_observer_.reset(new NavigationObserver(profile_));
145 bool allow_noisy_errors = !command_line->HasSwitch(switches::kNoErrorDialogs);
146 ExtensionErrorReporter::Init(allow_noisy_errors);
148 user_script_master_ = new UserScriptMaster(profile_);
150 // ExtensionService depends on RuntimeData.
151 runtime_data_.reset(new RuntimeData(ExtensionRegistry::Get(profile_)));
153 bool autoupdate_enabled = true;
154 #if defined(OS_CHROMEOS)
155 if (!extensions_enabled)
156 autoupdate_enabled = false;
157 else
158 autoupdate_enabled =
159 !command_line->HasSwitch(chromeos::switches::kGuestSession);
160 #endif
161 extension_service_.reset(new ExtensionService(
162 profile_,
163 CommandLine::ForCurrentProcess(),
164 profile_->GetPath().AppendASCII(extensions::kInstallDirectoryName),
165 ExtensionPrefs::Get(profile_),
166 blacklist_.get(),
167 autoupdate_enabled,
168 extensions_enabled,
169 &ready_));
171 // These services must be registered before the ExtensionService tries to
172 // load any extensions.
174 install_verifier_.reset(new InstallVerifier(ExtensionPrefs::Get(profile_),
175 profile_->GetRequestContext()));
176 install_verifier_->Init();
178 management_policy_.reset(new ManagementPolicy);
179 RegisterManagementPolicyProviders();
182 bool skip_session_extensions = false;
183 #if defined(OS_CHROMEOS)
184 // Skip loading session extensions if we are not in a user session.
185 skip_session_extensions = !chromeos::LoginState::Get()->IsUserLoggedIn();
186 if (chrome::IsRunningInForcedAppMode()) {
187 extension_service_->component_loader()->
188 AddDefaultComponentExtensionsForKioskMode(skip_session_extensions);
189 } else {
190 extension_service_->component_loader()->AddDefaultComponentExtensions(
191 skip_session_extensions);
193 #else
194 extension_service_->component_loader()->AddDefaultComponentExtensions(
195 skip_session_extensions);
196 #endif
197 if (command_line->HasSwitch(switches::kLoadComponentExtension)) {
198 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
199 switches::kLoadComponentExtension);
200 base::StringTokenizerT<CommandLine::StringType,
201 CommandLine::StringType::const_iterator> t(path_list,
202 FILE_PATH_LITERAL(","));
203 while (t.GetNext()) {
204 // Load the component extension manifest synchronously.
205 // Blocking the UI thread is acceptable here since
206 // this flag designated for developers.
207 base::ThreadRestrictions::ScopedAllowIO allow_io;
208 extension_service_->component_loader()->AddOrReplace(
209 base::FilePath(t.token()));
212 extension_service_->Init();
214 // Make the chrome://extension-icon/ resource available.
215 content::URLDataSource::Add(profile_, new ExtensionIconSource(profile_));
217 extension_warning_service_.reset(new ExtensionWarningService(profile_));
218 extension_warning_badge_service_.reset(
219 new ExtensionWarningBadgeService(profile_));
220 extension_warning_service_->AddObserver(
221 extension_warning_badge_service_.get());
222 error_console_.reset(new ErrorConsole(profile_, extension_service_.get()));
223 quota_service_.reset(new QuotaService);
225 if (extensions_enabled) {
226 // Load any extensions specified with --load-extension.
227 // TODO(yoz): Seems like this should move into ExtensionService::Init.
228 // But maybe it's no longer important.
229 if (command_line->HasSwitch(switches::kLoadExtension)) {
230 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
231 switches::kLoadExtension);
232 base::StringTokenizerT<CommandLine::StringType,
233 CommandLine::StringType::const_iterator> t(path_list,
234 FILE_PATH_LITERAL(","));
235 while (t.GetNext()) {
236 std::string extension_id;
237 UnpackedInstaller::Create(extension_service_.get())->
238 LoadFromCommandLine(base::FilePath(t.token()), &extension_id);
244 void ExtensionSystemImpl::Shared::Shutdown() {
245 if (extension_warning_service_) {
246 extension_warning_service_->RemoveObserver(
247 extension_warning_badge_service_.get());
249 if (extension_service_)
250 extension_service_->Shutdown();
253 StateStore* ExtensionSystemImpl::Shared::state_store() {
254 return state_store_.get();
257 StateStore* ExtensionSystemImpl::Shared::rules_store() {
258 return rules_store_.get();
261 ExtensionService* ExtensionSystemImpl::Shared::extension_service() {
262 return extension_service_.get();
265 RuntimeData* ExtensionSystemImpl::Shared::runtime_data() {
266 return runtime_data_.get();
269 ManagementPolicy* ExtensionSystemImpl::Shared::management_policy() {
270 return management_policy_.get();
273 UserScriptMaster* ExtensionSystemImpl::Shared::user_script_master() {
274 return user_script_master_.get();
277 InfoMap* ExtensionSystemImpl::Shared::info_map() {
278 if (!extension_info_map_.get())
279 extension_info_map_ = new InfoMap();
280 return extension_info_map_.get();
283 LazyBackgroundTaskQueue*
284 ExtensionSystemImpl::Shared::lazy_background_task_queue() {
285 return lazy_background_task_queue_.get();
288 EventRouter* ExtensionSystemImpl::Shared::event_router() {
289 return event_router_.get();
292 ExtensionWarningService* ExtensionSystemImpl::Shared::warning_service() {
293 return extension_warning_service_.get();
296 Blacklist* ExtensionSystemImpl::Shared::blacklist() {
297 return blacklist_.get();
300 ErrorConsole* ExtensionSystemImpl::Shared::error_console() {
301 return error_console_.get();
304 InstallVerifier* ExtensionSystemImpl::Shared::install_verifier() {
305 return install_verifier_.get();
308 QuotaService* ExtensionSystemImpl::Shared::quota_service() {
309 return quota_service_.get();
313 // ExtensionSystemImpl
316 ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile)
317 : profile_(profile) {
318 shared_ = ExtensionSystemSharedFactory::GetForBrowserContext(profile);
320 if (profile->IsOffTheRecord()) {
321 process_manager_.reset(ProcessManager::Create(profile));
322 } else {
323 shared_->InitPrefs();
327 ExtensionSystemImpl::~ExtensionSystemImpl() {
330 void ExtensionSystemImpl::Shutdown() {
331 process_manager_.reset();
334 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled) {
335 DCHECK(!profile_->IsOffTheRecord());
336 if (user_script_master() || extension_service())
337 return; // Already initialized.
339 // The InfoMap needs to be created before the ProcessManager.
340 shared_->info_map();
342 process_manager_.reset(ProcessManager::Create(profile_));
344 shared_->Init(extensions_enabled);
347 ExtensionService* ExtensionSystemImpl::extension_service() {
348 return shared_->extension_service();
351 RuntimeData* ExtensionSystemImpl::runtime_data() {
352 return shared_->runtime_data();
355 ManagementPolicy* ExtensionSystemImpl::management_policy() {
356 return shared_->management_policy();
359 UserScriptMaster* ExtensionSystemImpl::user_script_master() {
360 return shared_->user_script_master();
363 ProcessManager* ExtensionSystemImpl::process_manager() {
364 return process_manager_.get();
367 StateStore* ExtensionSystemImpl::state_store() {
368 return shared_->state_store();
371 StateStore* ExtensionSystemImpl::rules_store() {
372 return shared_->rules_store();
375 InfoMap* ExtensionSystemImpl::info_map() { return shared_->info_map(); }
377 LazyBackgroundTaskQueue* ExtensionSystemImpl::lazy_background_task_queue() {
378 return shared_->lazy_background_task_queue();
381 EventRouter* ExtensionSystemImpl::event_router() {
382 return shared_->event_router();
385 ExtensionWarningService* ExtensionSystemImpl::warning_service() {
386 return shared_->warning_service();
389 Blacklist* ExtensionSystemImpl::blacklist() {
390 return shared_->blacklist();
393 const OneShotEvent& ExtensionSystemImpl::ready() const {
394 return shared_->ready();
397 ErrorConsole* ExtensionSystemImpl::error_console() {
398 return shared_->error_console();
401 InstallVerifier* ExtensionSystemImpl::install_verifier() {
402 return shared_->install_verifier();
405 QuotaService* ExtensionSystemImpl::quota_service() {
406 return shared_->quota_service();
409 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
410 const Extension* extension) {
411 base::Time install_time;
412 if (extension->location() != Manifest::COMPONENT) {
413 install_time = ExtensionPrefs::Get(profile_)->
414 GetInstallTime(extension->id());
416 bool incognito_enabled = util::IsIncognitoEnabled(extension->id(), profile_);
418 bool notifications_disabled = false;
419 #if defined(ENABLE_NOTIFICATIONS)
420 message_center::NotifierId notifier_id(
421 message_center::NotifierId::APPLICATION,
422 extension->id());
424 DesktopNotificationService* notification_service =
425 DesktopNotificationServiceFactory::GetForProfile(profile_);
426 notifications_disabled =
427 !notification_service->IsNotifierEnabled(notifier_id);
428 #endif
430 BrowserThread::PostTask(
431 BrowserThread::IO, FROM_HERE,
432 base::Bind(&InfoMap::AddExtension, info_map(),
433 make_scoped_refptr(extension), install_time,
434 incognito_enabled, notifications_disabled));
437 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
438 const std::string& extension_id,
439 const UnloadedExtensionInfo::Reason reason) {
440 BrowserThread::PostTask(
441 BrowserThread::IO,
442 FROM_HERE,
443 base::Bind(&InfoMap::RemoveExtension, info_map(), extension_id, reason));
446 } // namespace extensions