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_content_verifier_delegate.h"
16 #include "chrome/browser/extensions/component_loader.h"
17 #include "chrome/browser/extensions/extension_error_reporter.h"
18 #include "chrome/browser/extensions/extension_management.h"
19 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/extensions/extension_system_factory.h"
21 #include "chrome/browser/extensions/extension_util.h"
22 #include "chrome/browser/extensions/install_verifier.h"
23 #include "chrome/browser/extensions/navigation_observer.h"
24 #include "chrome/browser/extensions/shared_module_service.h"
25 #include "chrome/browser/extensions/shared_user_script_master.h"
26 #include "chrome/browser/extensions/state_store_notification_observer.h"
27 #include "chrome/browser/extensions/unpacked_installer.h"
28 #include "chrome/browser/profiles/profile.h"
29 #include "chrome/browser/profiles/profile_manager.h"
30 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
31 #include "chrome/common/chrome_switches.h"
32 #include "chrome/common/extensions/features/feature_channel.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/url_data_source.h"
35 #include "extensions/browser/content_verifier.h"
36 #include "extensions/browser/extension_pref_store.h"
37 #include "extensions/browser/extension_pref_value_map.h"
38 #include "extensions/browser/extension_pref_value_map_factory.h"
39 #include "extensions/browser/extension_prefs.h"
40 #include "extensions/browser/extension_registry.h"
41 #include "extensions/browser/info_map.h"
42 #include "extensions/browser/quota_service.h"
43 #include "extensions/browser/runtime_data.h"
44 #include "extensions/browser/state_store.h"
45 #include "extensions/common/constants.h"
47 #if defined(ENABLE_NOTIFICATIONS)
48 #include "chrome/browser/notifications/notifier_state_tracker.h"
49 #include "chrome/browser/notifications/notifier_state_tracker_factory.h"
50 #include "ui/message_center/notifier_settings.h"
53 #if defined(OS_CHROMEOS)
54 #include "chrome/browser/app_mode/app_mode_utils.h"
55 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
56 #include "chrome/browser/chromeos/policy/device_local_account.h"
57 #include "chromeos/chromeos_switches.h"
58 #include "chromeos/login/login_state.h"
59 #include "components/user_manager/user.h"
60 #include "components/user_manager/user_manager.h"
63 using content::BrowserThread
;
65 // Statistics are logged to UMA with this string as part of histogram name. They
66 // can all be found under Extensions.Database.Open.<client>. Changing this needs
67 // to synchronize with histograms.xml, AND will also become incompatible with
68 // older browsers still reporting the previous values.
69 const char kStateDatabaseUMAClientName
[] = "State";
70 const char kRulesDatabaseUMAClientName
[] = "Rules";
72 namespace extensions
{
75 // ExtensionSystemImpl::Shared
78 ExtensionSystemImpl::Shared::Shared(Profile
* profile
)
82 ExtensionSystemImpl::Shared::~Shared() {
85 void ExtensionSystemImpl::Shared::InitPrefs() {
86 // Two state stores. The latter, which contains declarative rules, must be
87 // loaded immediately so that the rules are ready before we issue network
89 state_store_
.reset(new StateStore(
90 profile_
, kStateDatabaseUMAClientName
,
91 profile_
->GetPath().AppendASCII(extensions::kStateStoreName
), true));
92 state_store_notification_observer_
.reset(
93 new StateStoreNotificationObserver(state_store_
.get()));
95 rules_store_
.reset(new StateStore(
96 profile_
, kRulesDatabaseUMAClientName
,
97 profile_
->GetPath().AppendASCII(extensions::kRulesStoreName
), false));
99 #if defined(OS_CHROMEOS)
100 const user_manager::User
* user
=
101 user_manager::UserManager::Get()->GetActiveUser();
102 policy::DeviceLocalAccount::Type device_local_account_type
;
103 if (user
&& policy::IsDeviceLocalAccountUser(user
->email(),
104 &device_local_account_type
)) {
105 device_local_account_management_policy_provider_
.reset(
106 new chromeos::DeviceLocalAccountManagementPolicyProvider(
107 device_local_account_type
));
109 #endif // defined(OS_CHROMEOS)
112 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
113 management_policy_
->RegisterProviders(
114 ExtensionManagementFactory::GetForBrowserContext(profile_
)
117 #if defined(OS_CHROMEOS)
118 if (device_local_account_management_policy_provider_
) {
119 management_policy_
->RegisterProvider(
120 device_local_account_management_policy_provider_
.get());
122 #endif // defined(OS_CHROMEOS)
124 management_policy_
->RegisterProvider(InstallVerifier::Get(profile_
));
127 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled
) {
128 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::Shared::Init");
129 const base::CommandLine
* command_line
=
130 base::CommandLine::ForCurrentProcess();
132 navigation_observer_
.reset(new NavigationObserver(profile_
));
134 bool allow_noisy_errors
= !command_line
->HasSwitch(switches::kNoErrorDialogs
);
135 ExtensionErrorReporter::Init(allow_noisy_errors
);
137 content_verifier_
= new ContentVerifier(
138 profile_
, new ChromeContentVerifierDelegate(profile_
));
140 shared_user_script_master_
.reset(new SharedUserScriptMaster(profile_
));
142 // ExtensionService depends on RuntimeData.
143 runtime_data_
.reset(new RuntimeData(ExtensionRegistry::Get(profile_
)));
145 bool autoupdate_enabled
= !profile_
->IsGuestSession() &&
146 !profile_
->IsSystemProfile();
147 #if defined(OS_CHROMEOS)
148 if (!extensions_enabled
)
149 autoupdate_enabled
= false;
150 #endif // defined(OS_CHROMEOS)
151 extension_service_
.reset(new ExtensionService(
152 profile_
, base::CommandLine::ForCurrentProcess(),
153 profile_
->GetPath().AppendASCII(extensions::kInstallDirectoryName
),
154 ExtensionPrefs::Get(profile_
), Blacklist::Get(profile_
),
155 autoupdate_enabled
, extensions_enabled
, &ready_
));
157 // These services must be registered before the ExtensionService tries to
158 // load any extensions.
160 InstallVerifier::Get(profile_
)->Init();
161 ContentVerifierDelegate::Mode mode
=
162 ChromeContentVerifierDelegate::GetDefaultMode();
163 #if defined(OS_CHROMEOS)
164 mode
= std::max(mode
, ContentVerifierDelegate::BOOTSTRAP
);
165 #endif // defined(OS_CHROMEOS)
166 if (mode
>= ContentVerifierDelegate::BOOTSTRAP
)
167 content_verifier_
->Start();
168 info_map()->SetContentVerifier(content_verifier_
.get());
170 management_policy_
.reset(new ManagementPolicy
);
171 RegisterManagementPolicyProviders();
174 bool skip_session_extensions
= false;
175 #if defined(OS_CHROMEOS)
176 // Skip loading session extensions if we are not in a user session.
177 skip_session_extensions
= !chromeos::LoginState::Get()->IsUserLoggedIn();
178 if (chrome::IsRunningInForcedAppMode()) {
179 extension_service_
->component_loader()->
180 AddDefaultComponentExtensionsForKioskMode(skip_session_extensions
);
182 extension_service_
->component_loader()->AddDefaultComponentExtensions(
183 skip_session_extensions
);
186 extension_service_
->component_loader()->AddDefaultComponentExtensions(
187 skip_session_extensions
);
189 if (command_line
->HasSwitch(switches::kLoadComponentExtension
)) {
190 base::CommandLine::StringType path_list
=
191 command_line
->GetSwitchValueNative(switches::kLoadComponentExtension
);
192 base::StringTokenizerT
<base::CommandLine::StringType
,
193 base::CommandLine::StringType::const_iterator
>
194 t(path_list
, FILE_PATH_LITERAL(","));
195 while (t
.GetNext()) {
196 // Load the component extension manifest synchronously.
197 // Blocking the UI thread is acceptable here since
198 // this flag designated for developers.
199 base::ThreadRestrictions::ScopedAllowIO allow_io
;
200 extension_service_
->component_loader()->AddOrReplace(
201 base::FilePath(t
.token()));
204 extension_service_
->Init();
206 // Make the chrome://extension-icon/ resource available.
207 content::URLDataSource::Add(profile_
, new ExtensionIconSource(profile_
));
209 quota_service_
.reset(new QuotaService
);
211 if (extensions_enabled
) {
212 // Load any extensions specified with --load-extension.
213 // TODO(yoz): Seems like this should move into ExtensionService::Init.
214 // But maybe it's no longer important.
215 if (command_line
->HasSwitch(switches::kLoadExtension
)) {
216 base::CommandLine::StringType path_list
=
217 command_line
->GetSwitchValueNative(switches::kLoadExtension
);
218 base::StringTokenizerT
<base::CommandLine::StringType
,
219 base::CommandLine::StringType::const_iterator
>
220 t(path_list
, FILE_PATH_LITERAL(","));
221 while (t
.GetNext()) {
222 std::string extension_id
;
223 UnpackedInstaller::Create(extension_service_
.get())->
224 LoadFromCommandLine(base::FilePath(t
.token()), &extension_id
);
230 void ExtensionSystemImpl::Shared::Shutdown() {
231 if (content_verifier_
.get())
232 content_verifier_
->Shutdown();
233 if (extension_service_
)
234 extension_service_
->Shutdown();
237 StateStore
* ExtensionSystemImpl::Shared::state_store() {
238 return state_store_
.get();
241 StateStore
* ExtensionSystemImpl::Shared::rules_store() {
242 return rules_store_
.get();
245 ExtensionService
* ExtensionSystemImpl::Shared::extension_service() {
246 return extension_service_
.get();
249 RuntimeData
* ExtensionSystemImpl::Shared::runtime_data() {
250 return runtime_data_
.get();
253 ManagementPolicy
* ExtensionSystemImpl::Shared::management_policy() {
254 return management_policy_
.get();
257 SharedUserScriptMaster
*
258 ExtensionSystemImpl::Shared::shared_user_script_master() {
259 return shared_user_script_master_
.get();
262 InfoMap
* ExtensionSystemImpl::Shared::info_map() {
263 if (!extension_info_map_
.get())
264 extension_info_map_
= new InfoMap();
265 return extension_info_map_
.get();
268 QuotaService
* ExtensionSystemImpl::Shared::quota_service() {
269 return quota_service_
.get();
272 ContentVerifier
* ExtensionSystemImpl::Shared::content_verifier() {
273 return content_verifier_
.get();
277 // ExtensionSystemImpl
280 ExtensionSystemImpl::ExtensionSystemImpl(Profile
* profile
)
281 : profile_(profile
) {
282 shared_
= ExtensionSystemSharedFactory::GetForBrowserContext(profile
);
284 if (!profile
->IsOffTheRecord()) {
285 shared_
->InitPrefs();
289 ExtensionSystemImpl::~ExtensionSystemImpl() {
292 void ExtensionSystemImpl::Shutdown() {
295 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled
) {
296 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::InitForRegularProfile");
297 DCHECK(!profile_
->IsOffTheRecord());
298 if (shared_user_script_master() || extension_service())
299 return; // Already initialized.
301 // The InfoMap needs to be created before the ProcessManager.
303 shared_
->Init(extensions_enabled
);
306 ExtensionService
* ExtensionSystemImpl::extension_service() {
307 return shared_
->extension_service();
310 RuntimeData
* ExtensionSystemImpl::runtime_data() {
311 return shared_
->runtime_data();
314 ManagementPolicy
* ExtensionSystemImpl::management_policy() {
315 return shared_
->management_policy();
318 SharedUserScriptMaster
* ExtensionSystemImpl::shared_user_script_master() {
319 return shared_
->shared_user_script_master();
322 StateStore
* ExtensionSystemImpl::state_store() {
323 return shared_
->state_store();
326 StateStore
* ExtensionSystemImpl::rules_store() {
327 return shared_
->rules_store();
330 InfoMap
* ExtensionSystemImpl::info_map() { return shared_
->info_map(); }
332 const OneShotEvent
& ExtensionSystemImpl::ready() const {
333 return shared_
->ready();
336 QuotaService
* ExtensionSystemImpl::quota_service() {
337 return shared_
->quota_service();
340 ContentVerifier
* ExtensionSystemImpl::content_verifier() {
341 return shared_
->content_verifier();
344 scoped_ptr
<ExtensionSet
> ExtensionSystemImpl::GetDependentExtensions(
345 const Extension
* extension
) {
346 return extension_service()->shared_module_service()->GetDependentExtensions(
350 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
351 const Extension
* extension
,
352 const base::Closure
& callback
) {
353 base::Time install_time
;
354 if (extension
->location() != Manifest::COMPONENT
) {
355 install_time
= ExtensionPrefs::Get(profile_
)->
356 GetInstallTime(extension
->id());
358 bool incognito_enabled
= util::IsIncognitoEnabled(extension
->id(), profile_
);
360 bool notifications_disabled
= false;
361 #if defined(ENABLE_NOTIFICATIONS)
362 message_center::NotifierId
notifier_id(
363 message_center::NotifierId::APPLICATION
,
366 NotifierStateTracker
* notifier_state_tracker
=
367 NotifierStateTrackerFactory::GetForProfile(profile_
);
368 notifications_disabled
=
369 !notifier_state_tracker
->IsNotifierEnabled(notifier_id
);
372 BrowserThread::PostTaskAndReply(
373 BrowserThread::IO
, FROM_HERE
,
374 base::Bind(&InfoMap::AddExtension
, info_map(),
375 make_scoped_refptr(extension
), install_time
, incognito_enabled
,
376 notifications_disabled
),
380 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
381 const std::string
& extension_id
,
382 const UnloadedExtensionInfo::Reason reason
) {
383 BrowserThread::PostTask(
386 base::Bind(&InfoMap::RemoveExtension
, info_map(), extension_id
, reason
));
389 } // namespace extensions