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/service_worker_manager.h"
47 #include "extensions/browser/state_store.h"
48 #include "extensions/common/constants.h"
50 #if defined(ENABLE_NOTIFICATIONS)
51 #include "chrome/browser/notifications/notifier_state_tracker.h"
52 #include "chrome/browser/notifications/notifier_state_tracker_factory.h"
53 #include "ui/message_center/notifier_settings.h"
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/policy/device_local_account.h"
60 #include "chromeos/chromeos_switches.h"
61 #include "chromeos/login/login_state.h"
62 #include "components/user_manager/user.h"
63 #include "components/user_manager/user_manager.h"
66 using content::BrowserThread
;
68 // Statistics are logged to UMA with this string as part of histogram name. They
69 // can all be found under Extensions.Database.Open.<client>. Changing this needs
70 // to synchronize with histograms.xml, AND will also become incompatible with
71 // older browsers still reporting the previous values.
72 const char kStateDatabaseUMAClientName
[] = "State";
73 const char kRulesDatabaseUMAClientName
[] = "Rules";
75 namespace extensions
{
78 // ExtensionSystemImpl::Shared
81 ExtensionSystemImpl::Shared::Shared(Profile
* profile
)
85 ExtensionSystemImpl::Shared::~Shared() {
88 void ExtensionSystemImpl::Shared::InitPrefs() {
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
92 state_store_
.reset(new StateStore(
93 profile_
, kStateDatabaseUMAClientName
,
94 profile_
->GetPath().AppendASCII(extensions::kStateStoreName
), true));
95 state_store_notification_observer_
.reset(
96 new StateStoreNotificationObserver(state_store_
.get()));
98 rules_store_
.reset(new StateStore(
99 profile_
, kRulesDatabaseUMAClientName
,
100 profile_
->GetPath().AppendASCII(extensions::kRulesStoreName
), false));
102 #if defined(OS_CHROMEOS)
103 const user_manager::User
* user
=
104 user_manager::UserManager::Get()->GetActiveUser();
105 policy::DeviceLocalAccount::Type device_local_account_type
;
106 if (user
&& policy::IsDeviceLocalAccountUser(user
->email(),
107 &device_local_account_type
)) {
108 device_local_account_management_policy_provider_
.reset(
109 new chromeos::DeviceLocalAccountManagementPolicyProvider(
110 device_local_account_type
));
112 #endif // defined(OS_CHROMEOS)
115 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
116 management_policy_
->RegisterProviders(
117 ExtensionManagementFactory::GetForBrowserContext(profile_
)
120 #if defined(OS_CHROMEOS)
121 if (device_local_account_management_policy_provider_
) {
122 management_policy_
->RegisterProvider(
123 device_local_account_management_policy_provider_
.get());
125 #endif // defined(OS_CHROMEOS)
127 management_policy_
->RegisterProvider(InstallVerifier::Get(profile_
));
130 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled
) {
131 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::Shared::Init");
132 const base::CommandLine
* command_line
=
133 base::CommandLine::ForCurrentProcess();
135 navigation_observer_
.reset(new NavigationObserver(profile_
));
137 bool allow_noisy_errors
= !command_line
->HasSwitch(switches::kNoErrorDialogs
);
138 ExtensionErrorReporter::Init(allow_noisy_errors
);
140 content_verifier_
= new ContentVerifier(
141 profile_
, new ChromeContentVerifierDelegate(profile_
));
143 service_worker_manager_
.reset(new ServiceWorkerManager(profile_
));
145 shared_user_script_master_
.reset(new SharedUserScriptMaster(profile_
));
147 // ExtensionService depends on RuntimeData.
148 runtime_data_
.reset(new RuntimeData(ExtensionRegistry::Get(profile_
)));
150 bool autoupdate_enabled
= !profile_
->IsGuestSession() &&
151 !profile_
->IsSystemProfile();
152 #if defined(OS_CHROMEOS)
153 if (!extensions_enabled
)
154 autoupdate_enabled
= false;
155 #endif // defined(OS_CHROMEOS)
156 extension_service_
.reset(new ExtensionService(
157 profile_
, base::CommandLine::ForCurrentProcess(),
158 profile_
->GetPath().AppendASCII(extensions::kInstallDirectoryName
),
159 ExtensionPrefs::Get(profile_
), Blacklist::Get(profile_
),
160 autoupdate_enabled
, extensions_enabled
, &ready_
));
162 // These services must be registered before the ExtensionService tries to
163 // load any extensions.
165 InstallVerifier::Get(profile_
)->Init();
166 ContentVerifierDelegate::Mode mode
=
167 ChromeContentVerifierDelegate::GetDefaultMode();
168 #if defined(OS_CHROMEOS)
169 mode
= std::max(mode
, ContentVerifierDelegate::BOOTSTRAP
);
170 #endif // defined(OS_CHROMEOS)
171 if (mode
>= ContentVerifierDelegate::BOOTSTRAP
)
172 content_verifier_
->Start();
173 info_map()->SetContentVerifier(content_verifier_
.get());
175 management_policy_
.reset(new ManagementPolicy
);
176 RegisterManagementPolicyProviders();
179 bool skip_session_extensions
= false;
180 #if defined(OS_CHROMEOS)
181 // Skip loading session extensions if we are not in a user session.
182 skip_session_extensions
= !chromeos::LoginState::Get()->IsUserLoggedIn();
183 if (chrome::IsRunningInForcedAppMode()) {
184 extension_service_
->component_loader()->
185 AddDefaultComponentExtensionsForKioskMode(skip_session_extensions
);
187 extension_service_
->component_loader()->AddDefaultComponentExtensions(
188 skip_session_extensions
);
191 extension_service_
->component_loader()->AddDefaultComponentExtensions(
192 skip_session_extensions
);
194 if (command_line
->HasSwitch(switches::kLoadComponentExtension
)) {
195 base::CommandLine::StringType path_list
=
196 command_line
->GetSwitchValueNative(switches::kLoadComponentExtension
);
197 base::StringTokenizerT
<base::CommandLine::StringType
,
198 base::CommandLine::StringType::const_iterator
>
199 t(path_list
, FILE_PATH_LITERAL(","));
200 while (t
.GetNext()) {
201 // Load the component extension manifest synchronously.
202 // Blocking the UI thread is acceptable here since
203 // this flag designated for developers.
204 base::ThreadRestrictions::ScopedAllowIO allow_io
;
205 extension_service_
->component_loader()->AddOrReplace(
206 base::FilePath(t
.token()));
210 app_sorting_
.reset(new ChromeAppSorting(profile_
));
212 extension_service_
->Init();
214 // Make sure ExtensionSyncService is created.
215 ExtensionSyncService::Get(profile_
);
217 // Make the chrome://extension-icon/ resource available.
218 content::URLDataSource::Add(profile_
, new ExtensionIconSource(profile_
));
220 quota_service_
.reset(new QuotaService
);
222 if (extensions_enabled
) {
223 // Load any extensions specified with --load-extension.
224 // TODO(yoz): Seems like this should move into ExtensionService::Init.
225 // But maybe it's no longer important.
226 if (command_line
->HasSwitch(switches::kLoadExtension
)) {
227 base::CommandLine::StringType path_list
=
228 command_line
->GetSwitchValueNative(switches::kLoadExtension
);
229 base::StringTokenizerT
<base::CommandLine::StringType
,
230 base::CommandLine::StringType::const_iterator
>
231 t(path_list
, FILE_PATH_LITERAL(","));
232 while (t
.GetNext()) {
233 std::string extension_id
;
234 UnpackedInstaller::Create(extension_service_
.get())->
235 LoadFromCommandLine(base::FilePath(t
.token()), &extension_id
);
241 void ExtensionSystemImpl::Shared::Shutdown() {
242 if (content_verifier_
.get())
243 content_verifier_
->Shutdown();
244 if (extension_service_
)
245 extension_service_
->Shutdown();
248 ServiceWorkerManager
* ExtensionSystemImpl::Shared::service_worker_manager() {
249 return service_worker_manager_
.get();
252 StateStore
* ExtensionSystemImpl::Shared::state_store() {
253 return state_store_
.get();
256 StateStore
* ExtensionSystemImpl::Shared::rules_store() {
257 return rules_store_
.get();
260 ExtensionService
* ExtensionSystemImpl::Shared::extension_service() {
261 return extension_service_
.get();
264 RuntimeData
* ExtensionSystemImpl::Shared::runtime_data() {
265 return runtime_data_
.get();
268 ManagementPolicy
* ExtensionSystemImpl::Shared::management_policy() {
269 return management_policy_
.get();
272 SharedUserScriptMaster
*
273 ExtensionSystemImpl::Shared::shared_user_script_master() {
274 return shared_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 QuotaService
* ExtensionSystemImpl::Shared::quota_service() {
284 return quota_service_
.get();
287 AppSorting
* ExtensionSystemImpl::Shared::app_sorting() {
288 return app_sorting_
.get();
291 ContentVerifier
* ExtensionSystemImpl::Shared::content_verifier() {
292 return content_verifier_
.get();
296 // ExtensionSystemImpl
299 ExtensionSystemImpl::ExtensionSystemImpl(Profile
* profile
)
300 : profile_(profile
) {
301 shared_
= ExtensionSystemSharedFactory::GetForBrowserContext(profile
);
303 if (!profile
->IsOffTheRecord()) {
304 shared_
->InitPrefs();
308 ExtensionSystemImpl::~ExtensionSystemImpl() {
311 void ExtensionSystemImpl::Shutdown() {
314 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled
) {
315 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::InitForRegularProfile");
316 DCHECK(!profile_
->IsOffTheRecord());
317 if (shared_user_script_master() || extension_service())
318 return; // Already initialized.
320 // The InfoMap needs to be created before the ProcessManager.
322 shared_
->Init(extensions_enabled
);
325 ExtensionService
* ExtensionSystemImpl::extension_service() {
326 return shared_
->extension_service();
329 RuntimeData
* ExtensionSystemImpl::runtime_data() {
330 return shared_
->runtime_data();
333 ManagementPolicy
* ExtensionSystemImpl::management_policy() {
334 return shared_
->management_policy();
337 ServiceWorkerManager
* ExtensionSystemImpl::service_worker_manager() {
338 return shared_
->service_worker_manager();
341 SharedUserScriptMaster
* ExtensionSystemImpl::shared_user_script_master() {
342 return shared_
->shared_user_script_master();
345 StateStore
* ExtensionSystemImpl::state_store() {
346 return shared_
->state_store();
349 StateStore
* ExtensionSystemImpl::rules_store() {
350 return shared_
->rules_store();
353 InfoMap
* ExtensionSystemImpl::info_map() { return shared_
->info_map(); }
355 const OneShotEvent
& ExtensionSystemImpl::ready() const {
356 return shared_
->ready();
359 QuotaService
* ExtensionSystemImpl::quota_service() {
360 return shared_
->quota_service();
363 AppSorting
* ExtensionSystemImpl::app_sorting() {
364 return shared_
->app_sorting();
367 ContentVerifier
* ExtensionSystemImpl::content_verifier() {
368 return shared_
->content_verifier();
371 scoped_ptr
<ExtensionSet
> ExtensionSystemImpl::GetDependentExtensions(
372 const Extension
* extension
) {
373 return extension_service()->shared_module_service()->GetDependentExtensions(
377 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
378 const Extension
* extension
,
379 const base::Closure
& callback
) {
380 base::Time install_time
;
381 if (extension
->location() != Manifest::COMPONENT
) {
382 install_time
= ExtensionPrefs::Get(profile_
)->
383 GetInstallTime(extension
->id());
385 bool incognito_enabled
= util::IsIncognitoEnabled(extension
->id(), profile_
);
387 bool notifications_disabled
= false;
388 #if defined(ENABLE_NOTIFICATIONS)
389 message_center::NotifierId
notifier_id(
390 message_center::NotifierId::APPLICATION
,
393 NotifierStateTracker
* notifier_state_tracker
=
394 NotifierStateTrackerFactory::GetForProfile(profile_
);
395 notifications_disabled
=
396 !notifier_state_tracker
->IsNotifierEnabled(notifier_id
);
399 BrowserThread::PostTaskAndReply(
400 BrowserThread::IO
, FROM_HERE
,
401 base::Bind(&InfoMap::AddExtension
, info_map(),
402 make_scoped_refptr(extension
), install_time
, incognito_enabled
,
403 notifications_disabled
),
407 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
408 const std::string
& extension_id
,
409 const UnloadedExtensionInfo::Reason reason
) {
410 BrowserThread::PostTask(
413 base::Bind(&InfoMap::RemoveExtension
, info_map(), extension_id
, reason
));
416 } // namespace extensions