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 "base/trace_event/trace_event.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/chrome_app_sorting.h"
16 #include "chrome/browser/extensions/chrome_content_verifier_delegate.h"
17 #include "chrome/browser/extensions/component_loader.h"
18 #include "chrome/browser/extensions/extension_error_reporter.h"
19 #include "chrome/browser/extensions/extension_management.h"
20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/extensions/extension_sync_service.h"
22 #include "chrome/browser/extensions/extension_system_factory.h"
23 #include "chrome/browser/extensions/extension_util.h"
24 #include "chrome/browser/extensions/install_verifier.h"
25 #include "chrome/browser/extensions/navigation_observer.h"
26 #include "chrome/browser/extensions/shared_module_service.h"
27 #include "chrome/browser/extensions/shared_user_script_master.h"
28 #include "chrome/browser/extensions/state_store_notification_observer.h"
29 #include "chrome/browser/extensions/unpacked_installer.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/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/content_verifier.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/quota_service.h"
45 #include "extensions/browser/runtime_data.h"
46 #include "extensions/browser/state_store.h"
47 #include "extensions/common/constants.h"
49 #if defined(ENABLE_NOTIFICATIONS)
50 #include "chrome/browser/notifications/notifier_state_tracker.h"
51 #include "chrome/browser/notifications/notifier_state_tracker_factory.h"
52 #include "ui/message_center/notifier_settings.h"
55 #if defined(OS_CHROMEOS)
56 #include "chrome/browser/app_mode/app_mode_utils.h"
57 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
58 #include "chrome/browser/chromeos/policy/device_local_account.h"
59 #include "chromeos/chromeos_switches.h"
60 #include "chromeos/login/login_state.h"
61 #include "components/user_manager/user.h"
62 #include "components/user_manager/user_manager.h"
65 using content::BrowserThread
;
67 // Statistics are logged to UMA with this string as part of histogram name. They
68 // can all be found under Extensions.Database.Open.<client>. Changing this needs
69 // to synchronize with histograms.xml, AND will also become incompatible with
70 // older browsers still reporting the previous values.
71 const char kStateDatabaseUMAClientName
[] = "State";
72 const char kRulesDatabaseUMAClientName
[] = "Rules";
74 namespace extensions
{
77 // ExtensionSystemImpl::Shared
80 ExtensionSystemImpl::Shared::Shared(Profile
* profile
)
84 ExtensionSystemImpl::Shared::~Shared() {
87 void ExtensionSystemImpl::Shared::InitPrefs() {
88 // Two state stores. The latter, which contains declarative rules, must be
89 // loaded immediately so that the rules are ready before we issue network
91 state_store_
.reset(new StateStore(
92 profile_
, kStateDatabaseUMAClientName
,
93 profile_
->GetPath().AppendASCII(extensions::kStateStoreName
), true));
94 state_store_notification_observer_
.reset(
95 new StateStoreNotificationObserver(state_store_
.get()));
97 rules_store_
.reset(new StateStore(
98 profile_
, kRulesDatabaseUMAClientName
,
99 profile_
->GetPath().AppendASCII(extensions::kRulesStoreName
), false));
101 #if defined(OS_CHROMEOS)
102 const user_manager::User
* user
=
103 user_manager::UserManager::Get()->GetActiveUser();
104 policy::DeviceLocalAccount::Type device_local_account_type
;
105 if (user
&& policy::IsDeviceLocalAccountUser(user
->email(),
106 &device_local_account_type
)) {
107 device_local_account_management_policy_provider_
.reset(
108 new chromeos::DeviceLocalAccountManagementPolicyProvider(
109 device_local_account_type
));
111 #endif // defined(OS_CHROMEOS)
114 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
115 management_policy_
->RegisterProviders(
116 ExtensionManagementFactory::GetForBrowserContext(profile_
)
119 #if defined(OS_CHROMEOS)
120 if (device_local_account_management_policy_provider_
) {
121 management_policy_
->RegisterProvider(
122 device_local_account_management_policy_provider_
.get());
124 #endif // defined(OS_CHROMEOS)
126 management_policy_
->RegisterProvider(InstallVerifier::Get(profile_
));
129 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled
) {
130 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::Shared::Init");
131 const base::CommandLine
* command_line
=
132 base::CommandLine::ForCurrentProcess();
134 navigation_observer_
.reset(new NavigationObserver(profile_
));
136 bool allow_noisy_errors
= !command_line
->HasSwitch(switches::kNoErrorDialogs
);
137 ExtensionErrorReporter::Init(allow_noisy_errors
);
139 content_verifier_
= new ContentVerifier(
140 profile_
, new ChromeContentVerifierDelegate(profile_
));
142 shared_user_script_master_
.reset(new SharedUserScriptMaster(profile_
));
144 // ExtensionService depends on RuntimeData.
145 runtime_data_
.reset(new RuntimeData(ExtensionRegistry::Get(profile_
)));
147 bool autoupdate_enabled
= !profile_
->IsGuestSession() &&
148 !profile_
->IsSystemProfile();
149 #if defined(OS_CHROMEOS)
150 if (!extensions_enabled
)
151 autoupdate_enabled
= false;
152 #endif // defined(OS_CHROMEOS)
153 extension_service_
.reset(new ExtensionService(
154 profile_
, base::CommandLine::ForCurrentProcess(),
155 profile_
->GetPath().AppendASCII(extensions::kInstallDirectoryName
),
156 ExtensionPrefs::Get(profile_
), Blacklist::Get(profile_
),
157 autoupdate_enabled
, extensions_enabled
, &ready_
));
159 // These services must be registered before the ExtensionService tries to
160 // load any extensions.
162 InstallVerifier::Get(profile_
)->Init();
163 ContentVerifierDelegate::Mode mode
=
164 ChromeContentVerifierDelegate::GetDefaultMode();
165 #if defined(OS_CHROMEOS)
166 mode
= std::max(mode
, ContentVerifierDelegate::BOOTSTRAP
);
167 #endif // defined(OS_CHROMEOS)
168 if (mode
>= ContentVerifierDelegate::BOOTSTRAP
)
169 content_verifier_
->Start();
170 info_map()->SetContentVerifier(content_verifier_
.get());
172 management_policy_
.reset(new ManagementPolicy
);
173 RegisterManagementPolicyProviders();
176 bool skip_session_extensions
= false;
177 #if defined(OS_CHROMEOS)
178 // Skip loading session extensions if we are not in a user session.
179 skip_session_extensions
= !chromeos::LoginState::Get()->IsUserLoggedIn();
180 if (chrome::IsRunningInForcedAppMode()) {
181 extension_service_
->component_loader()->
182 AddDefaultComponentExtensionsForKioskMode(skip_session_extensions
);
184 extension_service_
->component_loader()->AddDefaultComponentExtensions(
185 skip_session_extensions
);
188 extension_service_
->component_loader()->AddDefaultComponentExtensions(
189 skip_session_extensions
);
191 if (command_line
->HasSwitch(switches::kLoadComponentExtension
)) {
192 base::CommandLine::StringType path_list
=
193 command_line
->GetSwitchValueNative(switches::kLoadComponentExtension
);
194 base::StringTokenizerT
<base::CommandLine::StringType
,
195 base::CommandLine::StringType::const_iterator
>
196 t(path_list
, FILE_PATH_LITERAL(","));
197 while (t
.GetNext()) {
198 // Load the component extension manifest synchronously.
199 // Blocking the UI thread is acceptable here since
200 // this flag designated for developers.
201 base::ThreadRestrictions::ScopedAllowIO allow_io
;
202 extension_service_
->component_loader()->AddOrReplace(
203 base::FilePath(t
.token()));
207 app_sorting_
.reset(new ChromeAppSorting(profile_
));
209 extension_service_
->Init();
211 // Make sure ExtensionSyncService is created.
212 ExtensionSyncService::Get(profile_
);
214 // Make the chrome://extension-icon/ resource available.
215 content::URLDataSource::Add(profile_
, new ExtensionIconSource(profile_
));
217 quota_service_
.reset(new QuotaService
);
219 if (extensions_enabled
) {
220 // Load any extensions specified with --load-extension.
221 // TODO(yoz): Seems like this should move into ExtensionService::Init.
222 // But maybe it's no longer important.
223 if (command_line
->HasSwitch(switches::kLoadExtension
)) {
224 base::CommandLine::StringType path_list
=
225 command_line
->GetSwitchValueNative(switches::kLoadExtension
);
226 base::StringTokenizerT
<base::CommandLine::StringType
,
227 base::CommandLine::StringType::const_iterator
>
228 t(path_list
, FILE_PATH_LITERAL(","));
229 while (t
.GetNext()) {
230 std::string extension_id
;
231 UnpackedInstaller::Create(extension_service_
.get())->
232 LoadFromCommandLine(base::FilePath(t
.token()), &extension_id
);
238 void ExtensionSystemImpl::Shared::Shutdown() {
239 if (content_verifier_
.get())
240 content_verifier_
->Shutdown();
241 if (extension_service_
)
242 extension_service_
->Shutdown();
245 StateStore
* ExtensionSystemImpl::Shared::state_store() {
246 return state_store_
.get();
249 StateStore
* ExtensionSystemImpl::Shared::rules_store() {
250 return rules_store_
.get();
253 ExtensionService
* ExtensionSystemImpl::Shared::extension_service() {
254 return extension_service_
.get();
257 RuntimeData
* ExtensionSystemImpl::Shared::runtime_data() {
258 return runtime_data_
.get();
261 ManagementPolicy
* ExtensionSystemImpl::Shared::management_policy() {
262 return management_policy_
.get();
265 SharedUserScriptMaster
*
266 ExtensionSystemImpl::Shared::shared_user_script_master() {
267 return shared_user_script_master_
.get();
270 InfoMap
* ExtensionSystemImpl::Shared::info_map() {
271 if (!extension_info_map_
.get())
272 extension_info_map_
= new InfoMap();
273 return extension_info_map_
.get();
276 QuotaService
* ExtensionSystemImpl::Shared::quota_service() {
277 return quota_service_
.get();
280 AppSorting
* ExtensionSystemImpl::Shared::app_sorting() {
281 return app_sorting_
.get();
284 ContentVerifier
* ExtensionSystemImpl::Shared::content_verifier() {
285 return content_verifier_
.get();
289 // ExtensionSystemImpl
292 ExtensionSystemImpl::ExtensionSystemImpl(Profile
* profile
)
293 : profile_(profile
) {
294 shared_
= ExtensionSystemSharedFactory::GetForBrowserContext(profile
);
296 if (!profile
->IsOffTheRecord()) {
297 shared_
->InitPrefs();
301 ExtensionSystemImpl::~ExtensionSystemImpl() {
304 void ExtensionSystemImpl::Shutdown() {
307 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled
) {
308 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::InitForRegularProfile");
309 DCHECK(!profile_
->IsOffTheRecord());
310 if (shared_user_script_master() || extension_service())
311 return; // Already initialized.
313 // The InfoMap needs to be created before the ProcessManager.
315 shared_
->Init(extensions_enabled
);
318 ExtensionService
* ExtensionSystemImpl::extension_service() {
319 return shared_
->extension_service();
322 RuntimeData
* ExtensionSystemImpl::runtime_data() {
323 return shared_
->runtime_data();
326 ManagementPolicy
* ExtensionSystemImpl::management_policy() {
327 return shared_
->management_policy();
330 SharedUserScriptMaster
* ExtensionSystemImpl::shared_user_script_master() {
331 return shared_
->shared_user_script_master();
334 StateStore
* ExtensionSystemImpl::state_store() {
335 return shared_
->state_store();
338 StateStore
* ExtensionSystemImpl::rules_store() {
339 return shared_
->rules_store();
342 InfoMap
* ExtensionSystemImpl::info_map() { return shared_
->info_map(); }
344 const OneShotEvent
& ExtensionSystemImpl::ready() const {
345 return shared_
->ready();
348 QuotaService
* ExtensionSystemImpl::quota_service() {
349 return shared_
->quota_service();
352 AppSorting
* ExtensionSystemImpl::app_sorting() {
353 return shared_
->app_sorting();
356 ContentVerifier
* ExtensionSystemImpl::content_verifier() {
357 return shared_
->content_verifier();
360 scoped_ptr
<ExtensionSet
> ExtensionSystemImpl::GetDependentExtensions(
361 const Extension
* extension
) {
362 return extension_service()->shared_module_service()->GetDependentExtensions(
366 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
367 const Extension
* extension
,
368 const base::Closure
& callback
) {
369 base::Time install_time
;
370 if (extension
->location() != Manifest::COMPONENT
) {
371 install_time
= ExtensionPrefs::Get(profile_
)->
372 GetInstallTime(extension
->id());
374 bool incognito_enabled
= util::IsIncognitoEnabled(extension
->id(), profile_
);
376 bool notifications_disabled
= false;
377 #if defined(ENABLE_NOTIFICATIONS)
378 message_center::NotifierId
notifier_id(
379 message_center::NotifierId::APPLICATION
,
382 NotifierStateTracker
* notifier_state_tracker
=
383 NotifierStateTrackerFactory::GetForProfile(profile_
);
384 notifications_disabled
=
385 !notifier_state_tracker
->IsNotifierEnabled(notifier_id
);
388 BrowserThread::PostTaskAndReply(
389 BrowserThread::IO
, FROM_HERE
,
390 base::Bind(&InfoMap::AddExtension
, info_map(),
391 make_scoped_refptr(extension
), install_time
, incognito_enabled
,
392 notifications_disabled
),
396 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
397 const std::string
& extension_id
,
398 const UnloadedExtensionInfo::Reason reason
) {
399 BrowserThread::PostTask(
402 base::Bind(&InfoMap::RemoveExtension
, info_map(), extension_id
, reason
));
405 } // namespace extensions