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"
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/event_router.h"
18 #include "chrome/browser/extensions/extension_error_reporter.h"
19 #include "chrome/browser/extensions/extension_info_map.h"
20 #include "chrome/browser/extensions/extension_pref_store.h"
21 #include "chrome/browser/extensions/extension_pref_value_map.h"
22 #include "chrome/browser/extensions/extension_pref_value_map_factory.h"
23 #include "chrome/browser/extensions/extension_prefs.h"
24 #include "chrome/browser/extensions/extension_process_manager.h"
25 #include "chrome/browser/extensions/extension_service.h"
26 #include "chrome/browser/extensions/extension_system_factory.h"
27 #include "chrome/browser/extensions/extension_util.h"
28 #include "chrome/browser/extensions/extension_warning_badge_service.h"
29 #include "chrome/browser/extensions/extension_warning_set.h"
30 #include "chrome/browser/extensions/management_policy.h"
31 #include "chrome/browser/extensions/navigation_observer.h"
32 #include "chrome/browser/extensions/standard_management_policy_provider.h"
33 #include "chrome/browser/extensions/state_store.h"
34 #include "chrome/browser/extensions/unpacked_installer.h"
35 #include "chrome/browser/extensions/user_script_master.h"
36 #include "chrome/browser/profiles/profile.h"
37 #include "chrome/browser/profiles/profile_manager.h"
38 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
39 #include "chrome/common/chrome_switches.h"
40 #include "chrome/common/chrome_version_info.h"
41 #include "chrome/common/extensions/extension.h"
42 #include "chrome/common/extensions/features/feature_channel.h"
43 #include "content/public/browser/browser_thread.h"
44 #include "content/public/browser/url_data_source.h"
45 #include "extensions/browser/lazy_background_task_queue.h"
46 #include "extensions/common/constants.h"
47 #include "extensions/common/manifest.h"
49 #if defined(OS_CHROMEOS)
50 #include "chrome/browser/app_mode/app_mode_utils.h"
51 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
52 #include "chrome/browser/chromeos/login/user.h"
53 #include "chrome/browser/chromeos/login/user_manager.h"
54 #include "chrome/browser/chromeos/policy/device_local_account.h"
55 #include "chromeos/chromeos_switches.h"
56 #include "chromeos/login/login_state.h"
59 using content::BrowserThread;
61 namespace extensions {
67 ExtensionSystem::ExtensionSystem() {
68 // Only set if it hasn't already been set (e.g. by a test).
69 if (GetCurrentChannel() == GetDefaultChannel())
70 SetCurrentChannel(chrome::VersionInfo::GetChannel());
73 ExtensionSystem::~ExtensionSystem() {
77 ExtensionSystem* ExtensionSystem::Get(Profile* profile) {
78 return ExtensionSystemFactory::GetForProfile(profile);
82 ExtensionSystem* ExtensionSystem::GetForBrowserContext(
83 content::BrowserContext* profile) {
84 return ExtensionSystemFactory::GetForProfile(static_cast<Profile*>(profile));
88 // ExtensionSystemImpl::Shared
91 ExtensionSystemImpl::Shared::Shared(Profile* profile)
95 ExtensionSystemImpl::Shared::~Shared() {
98 void ExtensionSystemImpl::Shared::InitPrefs() {
99 lazy_background_task_queue_.reset(new LazyBackgroundTaskQueue(profile_));
100 event_router_.reset(new EventRouter(profile_, ExtensionPrefs::Get(profile_)));
101 // TODO(yoz): Remove once crbug.com/159265 is fixed.
102 #if defined(ENABLE_EXTENSIONS)
103 // Two state stores. The latter, which contains declarative rules, must be
104 // loaded immediately so that the rules are ready before we issue network
106 state_store_.reset(new StateStore(
108 profile_->GetPath().AppendASCII(extensions::kStateStoreName),
111 rules_store_.reset(new StateStore(
113 profile_->GetPath().AppendASCII(extensions::kRulesStoreName),
116 blacklist_.reset(new Blacklist(ExtensionPrefs::Get(profile_)));
118 standard_management_policy_provider_.reset(
119 new StandardManagementPolicyProvider(ExtensionPrefs::Get(profile_)));
121 #if defined (OS_CHROMEOS)
122 const chromeos::User* user = chromeos::UserManager::Get()->GetActiveUser();
123 policy::DeviceLocalAccount::Type device_local_account_type;
124 if (user && policy::IsDeviceLocalAccountUser(user->email(),
125 &device_local_account_type)) {
126 device_local_account_management_policy_provider_.reset(
127 new chromeos::DeviceLocalAccountManagementPolicyProvider(
128 device_local_account_type));
130 #endif // defined (OS_CHROMEOS)
132 #endif // defined(ENABLE_EXTENSIONS)
135 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
136 // TODO(yoz): Remove once crbug.com/159265 is fixed.
137 #if defined(ENABLE_EXTENSIONS)
138 DCHECK(standard_management_policy_provider_.get());
139 management_policy_->RegisterProvider(
140 standard_management_policy_provider_.get());
142 #if defined (OS_CHROMEOS)
143 if (device_local_account_management_policy_provider_) {
144 management_policy_->RegisterProvider(
145 device_local_account_management_policy_provider_.get());
147 #endif // defined (OS_CHROMEOS)
149 #endif // defined(ENABLE_EXTENSIONS)
152 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled) {
153 const CommandLine* command_line = CommandLine::ForCurrentProcess();
155 navigation_observer_.reset(new NavigationObserver(profile_));
157 bool allow_noisy_errors = !command_line->HasSwitch(switches::kNoErrorDialogs);
158 ExtensionErrorReporter::Init(allow_noisy_errors);
160 user_script_master_ = new UserScriptMaster(profile_);
162 bool autoupdate_enabled = true;
163 #if defined(OS_CHROMEOS)
164 if (!extensions_enabled)
165 autoupdate_enabled = false;
168 !command_line->HasSwitch(chromeos::switches::kGuestSession);
170 extension_service_.reset(new ExtensionService(
172 CommandLine::ForCurrentProcess(),
173 profile_->GetPath().AppendASCII(extensions::kInstallDirectoryName),
174 ExtensionPrefs::Get(profile_),
180 // These services must be registered before the ExtensionService tries to
181 // load any extensions.
183 management_policy_.reset(new ManagementPolicy);
184 RegisterManagementPolicyProviders();
187 bool skip_session_extensions = false;
188 #if defined(OS_CHROMEOS)
189 // Skip loading session extensions if we are not in a user session.
190 skip_session_extensions = !chromeos::LoginState::Get()->IsUserLoggedIn();
191 if (!chrome::IsRunningInForcedAppMode()) {
192 extension_service_->component_loader()->AddDefaultComponentExtensions(
193 skip_session_extensions);
196 extension_service_->component_loader()->AddDefaultComponentExtensions(
197 skip_session_extensions);
199 if (command_line->HasSwitch(switches::kLoadComponentExtension)) {
200 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
201 switches::kLoadComponentExtension);
202 base::StringTokenizerT<CommandLine::StringType,
203 CommandLine::StringType::const_iterator> t(path_list,
204 FILE_PATH_LITERAL(","));
205 while (t.GetNext()) {
206 // Load the component extension manifest synchronously.
207 // Blocking the UI thread is acceptable here since
208 // this flag designated for developers.
209 base::ThreadRestrictions::ScopedAllowIO allow_io;
210 extension_service_->component_loader()->AddOrReplace(
211 base::FilePath(t.token()));
214 extension_service_->Init();
216 if (extensions_enabled) {
217 // Load any extensions specified with --load-extension.
218 // TODO(yoz): Seems like this should move into ExtensionService::Init.
219 // But maybe it's no longer important.
220 if (command_line->HasSwitch(switches::kLoadExtension)) {
221 CommandLine::StringType path_list = command_line->GetSwitchValueNative(
222 switches::kLoadExtension);
223 base::StringTokenizerT<CommandLine::StringType,
224 CommandLine::StringType::const_iterator> t(path_list,
225 FILE_PATH_LITERAL(","));
226 while (t.GetNext()) {
227 std::string extension_id;
228 UnpackedInstaller::Create(extension_service_.get())->
229 LoadFromCommandLine(base::FilePath(t.token()), &extension_id);
234 // Make the chrome://extension-icon/ resource available.
235 content::URLDataSource::Add(profile_, new ExtensionIconSource(profile_));
237 extension_warning_service_.reset(new ExtensionWarningService(profile_));
238 extension_warning_badge_service_.reset(
239 new ExtensionWarningBadgeService(profile_));
240 extension_warning_service_->AddObserver(
241 extension_warning_badge_service_.get());
242 error_console_.reset(new ErrorConsole(profile_, extension_service_.get()));
245 void ExtensionSystemImpl::Shared::Shutdown() {
246 if (extension_warning_service_) {
247 extension_warning_service_->RemoveObserver(
248 extension_warning_badge_service_.get());
250 if (extension_service_)
251 extension_service_->Shutdown();
254 StateStore* ExtensionSystemImpl::Shared::state_store() {
255 return state_store_.get();
258 StateStore* ExtensionSystemImpl::Shared::rules_store() {
259 return rules_store_.get();
262 ExtensionService* ExtensionSystemImpl::Shared::extension_service() {
263 return extension_service_.get();
266 ManagementPolicy* ExtensionSystemImpl::Shared::management_policy() {
267 return management_policy_.get();
270 UserScriptMaster* ExtensionSystemImpl::Shared::user_script_master() {
271 return user_script_master_.get();
274 ExtensionInfoMap* ExtensionSystemImpl::Shared::info_map() {
275 if (!extension_info_map_.get())
276 extension_info_map_ = new ExtensionInfoMap();
277 return extension_info_map_.get();
280 LazyBackgroundTaskQueue*
281 ExtensionSystemImpl::Shared::lazy_background_task_queue() {
282 return lazy_background_task_queue_.get();
285 EventRouter* ExtensionSystemImpl::Shared::event_router() {
286 return event_router_.get();
289 ExtensionWarningService* ExtensionSystemImpl::Shared::warning_service() {
290 return extension_warning_service_.get();
293 Blacklist* ExtensionSystemImpl::Shared::blacklist() {
294 return blacklist_.get();
297 ErrorConsole* ExtensionSystemImpl::Shared::error_console() {
298 return error_console_.get();
302 // ExtensionSystemImpl
305 ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile)
306 : profile_(profile) {
307 shared_ = ExtensionSystemSharedFactory::GetForProfile(profile);
309 if (profile->IsOffTheRecord()) {
310 extension_process_manager_.reset(ExtensionProcessManager::Create(profile));
312 shared_->InitPrefs();
316 ExtensionSystemImpl::~ExtensionSystemImpl() {
319 void ExtensionSystemImpl::Shutdown() {
320 extension_process_manager_.reset();
323 void ExtensionSystemImpl::InitForRegularProfile(
324 bool extensions_enabled,
325 bool defer_background_creation) {
326 DCHECK(!profile_->IsOffTheRecord());
327 if (user_script_master() || extension_service())
328 return; // Already initialized.
330 // The ExtensionInfoMap needs to be created before the
331 // ExtensionProcessManager.
334 extension_process_manager_.reset(ExtensionProcessManager::Create(profile_));
336 extension_process_manager_->DeferBackgroundHostCreation(
337 defer_background_creation);
339 shared_->Init(extensions_enabled);
342 ExtensionService* ExtensionSystemImpl::extension_service() {
343 return shared_->extension_service();
346 ManagementPolicy* ExtensionSystemImpl::management_policy() {
347 return shared_->management_policy();
350 UserScriptMaster* ExtensionSystemImpl::user_script_master() {
351 return shared_->user_script_master();
354 ExtensionProcessManager* ExtensionSystemImpl::process_manager() {
355 return extension_process_manager_.get();
358 StateStore* ExtensionSystemImpl::state_store() {
359 return shared_->state_store();
362 StateStore* ExtensionSystemImpl::rules_store() {
363 return shared_->rules_store();
366 ExtensionInfoMap* ExtensionSystemImpl::info_map() {
367 return shared_->info_map();
370 LazyBackgroundTaskQueue* ExtensionSystemImpl::lazy_background_task_queue() {
371 return shared_->lazy_background_task_queue();
374 EventRouter* ExtensionSystemImpl::event_router() {
375 return shared_->event_router();
378 ExtensionWarningService* ExtensionSystemImpl::warning_service() {
379 return shared_->warning_service();
382 Blacklist* ExtensionSystemImpl::blacklist() {
383 return shared_->blacklist();
386 const OneShotEvent& ExtensionSystemImpl::ready() const {
387 return shared_->ready();
390 ErrorConsole* ExtensionSystemImpl::error_console() {
391 return shared_->error_console();
394 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
395 const Extension* extension) {
396 base::Time install_time;
397 if (extension->location() != Manifest::COMPONENT) {
398 install_time = ExtensionPrefs::Get(profile_)->
399 GetInstallTime(extension->id());
401 bool incognito_enabled =
402 extension_util::IsIncognitoEnabled(extension->id(), extension_service());
403 BrowserThread::PostTask(
404 BrowserThread::IO, FROM_HERE,
405 base::Bind(&ExtensionInfoMap::AddExtension, info_map(),
406 make_scoped_refptr(extension), install_time,
410 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
411 const std::string& extension_id,
412 const UnloadedExtensionInfo::Reason reason) {
413 BrowserThread::PostTask(
414 BrowserThread::IO, FROM_HERE,
415 base::Bind(&ExtensionInfoMap::RemoveExtension, info_map(),
416 extension_id, reason));
419 } // namespace extensions