Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_system.cc
blob80ef73657ddb1303dfe8e05e28956ea460d28e31
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/extension_system.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_pref_store.h"
19 #include "chrome/browser/extensions/extension_pref_value_map.h"
20 #include "chrome/browser/extensions/extension_pref_value_map_factory.h"
21 #include "chrome/browser/extensions/extension_prefs.h"
22 #include "chrome/browser/extensions/extension_service.h"
23 #include "chrome/browser/extensions/extension_system_factory.h"
24 #include "chrome/browser/extensions/extension_util.h"
25 #include "chrome/browser/extensions/extension_warning_badge_service.h"
26 #include "chrome/browser/extensions/extension_warning_set.h"
27 #include "chrome/browser/extensions/install_verifier.h"
28 #include "chrome/browser/extensions/navigation_observer.h"
29 #include "chrome/browser/extensions/standard_management_policy_provider.h"
30 #include "chrome/browser/extensions/state_store.h"
31 #include "chrome/browser/extensions/unpacked_installer.h"
32 #include "chrome/browser/extensions/user_script_master.h"
33 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/browser/profiles/profile_manager.h"
35 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
36 #include "chrome/common/chrome_switches.h"
37 #include "chrome/common/chrome_version_info.h"
38 #include "chrome/common/extensions/features/feature_channel.h"
39 #include "content/public/browser/browser_thread.h"
40 #include "content/public/browser/url_data_source.h"
41 #include "extensions/browser/event_router.h"
42 #include "extensions/browser/info_map.h"
43 #include "extensions/browser/lazy_background_task_queue.h"
44 #include "extensions/browser/management_policy.h"
45 #include "extensions/browser/process_manager.h"
46 #include "extensions/common/constants.h"
47 #include "extensions/common/extension.h"
48 #include "extensions/common/manifest.h"
50 #if defined(ENABLE_NOTIFICATIONS)
51 #include "chrome/browser/notifications/desktop_notification_service.h"
52 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
53 #include "ui/message_center/notifier_settings.h"
54 #endif
56 #if defined(OS_CHROMEOS)
57 #include "chrome/browser/app_mode/app_mode_utils.h"
58 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
59 #include "chrome/browser/chromeos/login/user.h"
60 #include "chrome/browser/chromeos/login/user_manager.h"
61 #include "chrome/browser/chromeos/policy/device_local_account.h"
62 #include "chromeos/chromeos_switches.h"
63 #include "chromeos/login/login_state.h"
64 #endif
66 using content::BrowserThread;
68 namespace extensions {
71 // ExtensionSystem
74 ExtensionSystem::ExtensionSystem() {
75 // Only set if it hasn't already been set (e.g. by a test).
76 if (GetCurrentChannel() == GetDefaultChannel())
77 SetCurrentChannel(chrome::VersionInfo::GetChannel());
80 ExtensionSystem::~ExtensionSystem() {
83 // static
84 ExtensionSystem* ExtensionSystem::Get(Profile* profile) {
85 return ExtensionSystemFactory::GetForProfile(profile);
88 // static
89 ExtensionSystem* ExtensionSystem::GetForBrowserContext(
90 content::BrowserContext* profile) {
91 return ExtensionSystemFactory::GetForProfile(static_cast<Profile*>(profile));
95 // ExtensionSystemImpl::Shared
98 ExtensionSystemImpl::Shared::Shared(Profile* profile)
99 : profile_(profile) {
102 ExtensionSystemImpl::Shared::~Shared() {
105 void ExtensionSystemImpl::Shared::InitPrefs() {
106 lazy_background_task_queue_.reset(new LazyBackgroundTaskQueue(profile_));
107 event_router_.reset(new EventRouter(profile_, ExtensionPrefs::Get(profile_)));
108 // TODO(yoz): Remove once crbug.com/159265 is fixed.
109 #if defined(ENABLE_EXTENSIONS)
110 // Two state stores. The latter, which contains declarative rules, must be
111 // loaded immediately so that the rules are ready before we issue network
112 // requests.
113 state_store_.reset(new StateStore(
114 profile_,
115 profile_->GetPath().AppendASCII(extensions::kStateStoreName),
116 true));
118 rules_store_.reset(new StateStore(
119 profile_,
120 profile_->GetPath().AppendASCII(extensions::kRulesStoreName),
121 false));
123 blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
125 standard_management_policy_provider_.reset(
126 new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
128 #if defined (OS_CHROMEOS)
129 const chromeos::User* user = chromeos::UserManager::Get()->GetActiveUser();
130 policy::DeviceLocalAccount::Type device_local_account_type;
131 if (user && policy::IsDeviceLocalAccountUser(user->email(),
132 &device_local_account_type)) {
133 device_local_account_management_policy_provider_.reset(
134 new chromeos::DeviceLocalAccountManagementPolicyProvider(
135 device_local_account_type));
137 #endif // defined (OS_CHROMEOS)
139 #endif // defined(ENABLE_EXTENSIONS)
142 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
143 // TODO(yoz): Remove once crbug.com/159265 is fixed.
144 #if defined(ENABLE_EXTENSIONS)
145 DCHECK(standard_management_policy_provider_.get());
146 management_policy_->RegisterProvider(
147 standard_management_policy_provider_.get());
149 #if defined (OS_CHROMEOS)
150 if (device_local_account_management_policy_provider_) {
151 management_policy_->RegisterProvider(
152 device_local_account_management_policy_provider_.get());
154 #endif // defined (OS_CHROMEOS)
156 management_policy_->RegisterProvider(install_verifier_.get());
158 #endif // defined(ENABLE_EXTENSIONS)
161 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled) {
162 const CommandLine* command_line = CommandLine::ForCurrentProcess();
164 navigation_observer_.reset(new NavigationObserver(profile_));
166 bool allow_noisy_errors = !command_line->HasSwitch(switches::kNoErrorDialogs);
167 ExtensionErrorReporter::Init(allow_noisy_errors);
169 user_script_master_ = new UserScriptMaster(profile_);
171 bool autoupdate_enabled = true;
172 #if defined(OS_CHROMEOS)
173 if (!extensions_enabled)
174 autoupdate_enabled = false;
175 else
176 autoupdate_enabled =
177 !command_line->HasSwitch(chromeos::switches::kGuestSession);
178 #endif
179 extension_service_.reset(new ExtensionService(
180 profile_,
181 CommandLine::ForCurrentProcess(),
182 profile_->GetPath().AppendASCII(extensions::kInstallDirectoryName),
183 ExtensionPrefs::Get(profile_),
184 blacklist_.get(),
185 autoupdate_enabled,
186 extensions_enabled,
187 &ready_));
189 // These services must be registered before the ExtensionService tries to
190 // load any extensions.
192 install_verifier_.reset(new InstallVerifier(ExtensionPrefs::Get(profile_),
193 profile_->GetRequestContext()));
194 install_verifier_->Init();
196 management_policy_.reset(new ManagementPolicy);
197 RegisterManagementPolicyProviders();
200 bool skip_session_extensions = false;
201 #if defined(OS_CHROMEOS)
202 // Skip loading session extensions if we are not in a user session.
203 skip_session_extensions = !chromeos::LoginState::Get()->IsUserLoggedIn();
204 if (chrome::IsRunningInForcedAppMode()) {
205 extension_service_->component_loader()->
206 AddDefaultComponentExtensionsForKioskMode(skip_session_extensions);
207 } else {
208 extension_service_->component_loader()->AddDefaultComponentExtensions(
209 skip_session_extensions);
211 #else
212 extension_service_->component_loader()->AddDefaultComponentExtensions(
213 skip_session_extensions);
214 #endif
215 if (command_line->HasSwitch(switches::kLoadComponentExtension)) {
216 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
217 switches::kLoadComponentExtension);
218 base::StringTokenizerT<CommandLine::StringType,
219 CommandLine::StringType::const_iterator> t(path_list,
220 FILE_PATH_LITERAL(","));
221 while (t.GetNext()) {
222 // Load the component extension manifest synchronously.
223 // Blocking the UI thread is acceptable here since
224 // this flag designated for developers.
225 base::ThreadRestrictions::ScopedAllowIO allow_io;
226 extension_service_->component_loader()->AddOrReplace(
227 base::FilePath(t.token()));
230 extension_service_->Init();
232 // Make the chrome://extension-icon/ resource available.
233 content::URLDataSource::Add(profile_, new ExtensionIconSource(profile_));
235 extension_warning_service_.reset(new ExtensionWarningService(profile_));
236 extension_warning_badge_service_.reset(
237 new ExtensionWarningBadgeService(profile_));
238 extension_warning_service_->AddObserver(
239 extension_warning_badge_service_.get());
240 error_console_.reset(new ErrorConsole(profile_, extension_service_.get()));
242 if (extensions_enabled) {
243 // Load any extensions specified with --load-extension.
244 // TODO(yoz): Seems like this should move into ExtensionService::Init.
245 // But maybe it's no longer important.
246 if (command_line->HasSwitch(switches::kLoadExtension)) {
247 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
248 switches::kLoadExtension);
249 base::StringTokenizerT<CommandLine::StringType,
250 CommandLine::StringType::const_iterator> t(path_list,
251 FILE_PATH_LITERAL(","));
252 while (t.GetNext()) {
253 std::string extension_id;
254 UnpackedInstaller::Create(extension_service_.get())->
255 LoadFromCommandLine(base::FilePath(t.token()), &extension_id);
261 void ExtensionSystemImpl::Shared::Shutdown() {
262 if (extension_warning_service_) {
263 extension_warning_service_->RemoveObserver(
264 extension_warning_badge_service_.get());
266 if (extension_service_)
267 extension_service_->Shutdown();
270 StateStore* ExtensionSystemImpl::Shared::state_store() {
271 return state_store_.get();
274 StateStore* ExtensionSystemImpl::Shared::rules_store() {
275 return rules_store_.get();
278 ExtensionService* ExtensionSystemImpl::Shared::extension_service() {
279 return extension_service_.get();
282 ManagementPolicy* ExtensionSystemImpl::Shared::management_policy() {
283 return management_policy_.get();
286 UserScriptMaster* ExtensionSystemImpl::Shared::user_script_master() {
287 return user_script_master_.get();
290 InfoMap* ExtensionSystemImpl::Shared::info_map() {
291 if (!extension_info_map_.get())
292 extension_info_map_ = new InfoMap();
293 return extension_info_map_.get();
296 LazyBackgroundTaskQueue*
297 ExtensionSystemImpl::Shared::lazy_background_task_queue() {
298 return lazy_background_task_queue_.get();
301 EventRouter* ExtensionSystemImpl::Shared::event_router() {
302 return event_router_.get();
305 ExtensionWarningService* ExtensionSystemImpl::Shared::warning_service() {
306 return extension_warning_service_.get();
309 Blacklist* ExtensionSystemImpl::Shared::blacklist() {
310 return blacklist_.get();
313 ErrorConsole* ExtensionSystemImpl::Shared::error_console() {
314 return error_console_.get();
317 InstallVerifier* ExtensionSystemImpl::Shared::install_verifier() {
318 return install_verifier_.get();
322 // ExtensionSystemImpl
325 ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile)
326 : profile_(profile) {
327 shared_ = ExtensionSystemSharedFactory::GetForProfile(profile);
329 if (profile->IsOffTheRecord()) {
330 process_manager_.reset(ProcessManager::Create(profile));
331 } else {
332 shared_->InitPrefs();
336 ExtensionSystemImpl::~ExtensionSystemImpl() {
339 void ExtensionSystemImpl::Shutdown() {
340 process_manager_.reset();
343 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled) {
344 DCHECK(!profile_->IsOffTheRecord());
345 if (user_script_master() || extension_service())
346 return; // Already initialized.
348 // The InfoMap needs to be created before the ProcessManager.
349 shared_->info_map();
351 process_manager_.reset(ProcessManager::Create(profile_));
353 shared_->Init(extensions_enabled);
356 ExtensionService* ExtensionSystemImpl::extension_service() {
357 return shared_->extension_service();
360 ManagementPolicy* ExtensionSystemImpl::management_policy() {
361 return shared_->management_policy();
364 UserScriptMaster* ExtensionSystemImpl::user_script_master() {
365 return shared_->user_script_master();
368 ProcessManager* ExtensionSystemImpl::process_manager() {
369 return process_manager_.get();
372 StateStore* ExtensionSystemImpl::state_store() {
373 return shared_->state_store();
376 StateStore* ExtensionSystemImpl::rules_store() {
377 return shared_->rules_store();
380 InfoMap* ExtensionSystemImpl::info_map() { return shared_->info_map(); }
382 LazyBackgroundTaskQueue* ExtensionSystemImpl::lazy_background_task_queue() {
383 return shared_->lazy_background_task_queue();
386 EventRouter* ExtensionSystemImpl::event_router() {
387 return shared_->event_router();
390 ExtensionWarningService* ExtensionSystemImpl::warning_service() {
391 return shared_->warning_service();
394 Blacklist* ExtensionSystemImpl::blacklist() {
395 return shared_->blacklist();
398 const OneShotEvent& ExtensionSystemImpl::ready() const {
399 return shared_->ready();
402 ErrorConsole* ExtensionSystemImpl::error_console() {
403 return shared_->error_console();
406 InstallVerifier* ExtensionSystemImpl::install_verifier() {
407 return shared_->install_verifier();
410 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
411 const Extension* extension) {
412 base::Time install_time;
413 if (extension->location() != Manifest::COMPONENT) {
414 install_time = ExtensionPrefs::Get(profile_)->
415 GetInstallTime(extension->id());
417 bool incognito_enabled =
418 extension_util::IsIncognitoEnabled(extension->id(), extension_service());
420 bool notifications_disabled = false;
421 #if defined(ENABLE_NOTIFICATIONS)
422 message_center::NotifierId notifier_id(
423 message_center::NotifierId::APPLICATION,
424 extension->id());
426 DesktopNotificationService* notification_service =
427 DesktopNotificationServiceFactory::GetForProfile(profile_);
428 notifications_disabled =
429 !notification_service->IsNotifierEnabled(notifier_id);
430 #endif
432 BrowserThread::PostTask(
433 BrowserThread::IO, FROM_HERE,
434 base::Bind(&InfoMap::AddExtension, info_map(),
435 make_scoped_refptr(extension), install_time,
436 incognito_enabled, notifications_disabled));
439 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
440 const std::string& extension_id,
441 const UnloadedExtensionInfo::Reason reason) {
442 BrowserThread::PostTask(
443 BrowserThread::IO,
444 FROM_HERE,
445 base::Bind(&InfoMap::RemoveExtension, info_map(), extension_id, reason));
448 } // namespace extensions