Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_system_impl.cc
blobb4ef0d9358568e5c40599f6932b797dc3fa445d1
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"
8 #include "base/bind.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/chrome_version_info.h"
33 #include "chrome/common/extensions/features/feature_channel.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "content/public/browser/url_data_source.h"
36 #include "extensions/browser/content_verifier.h"
37 #include "extensions/browser/extension_pref_store.h"
38 #include "extensions/browser/extension_pref_value_map.h"
39 #include "extensions/browser/extension_pref_value_map_factory.h"
40 #include "extensions/browser/extension_prefs.h"
41 #include "extensions/browser/extension_registry.h"
42 #include "extensions/browser/info_map.h"
43 #include "extensions/browser/quota_service.h"
44 #include "extensions/browser/runtime_data.h"
45 #include "extensions/browser/state_store.h"
46 #include "extensions/common/constants.h"
48 #if defined(ENABLE_NOTIFICATIONS)
49 #include "chrome/browser/notifications/desktop_notification_service.h"
50 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
51 #include "ui/message_center/notifier_settings.h"
52 #endif
54 #if defined(OS_CHROMEOS)
55 #include "chrome/browser/app_mode/app_mode_utils.h"
56 #include "chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.h"
57 #include "chrome/browser/chromeos/policy/device_local_account.h"
58 #include "chromeos/chromeos_switches.h"
59 #include "chromeos/login/login_state.h"
60 #include "components/user_manager/user.h"
61 #include "components/user_manager/user_manager.h"
62 #endif
64 using content::BrowserThread;
66 // Statistics are logged to UMA with this string as part of histogram name. They
67 // can all be found under Extensions.Database.Open.<client>. Changing this needs
68 // to synchronize with histograms.xml, AND will also become incompatible with
69 // older browsers still reporting the previous values.
70 const char kStateDatabaseUMAClientName[] = "State";
71 const char kRulesDatabaseUMAClientName[] = "Rules";
73 namespace extensions {
76 // ExtensionSystemImpl::Shared
79 ExtensionSystemImpl::Shared::Shared(Profile* profile)
80 : profile_(profile) {
83 ExtensionSystemImpl::Shared::~Shared() {
86 void ExtensionSystemImpl::Shared::InitPrefs() {
87 // Two state stores. The latter, which contains declarative rules, must be
88 // loaded immediately so that the rules are ready before we issue network
89 // requests.
90 state_store_.reset(new StateStore(
91 profile_, kStateDatabaseUMAClientName,
92 profile_->GetPath().AppendASCII(extensions::kStateStoreName), true));
93 state_store_notification_observer_.reset(
94 new StateStoreNotificationObserver(state_store_.get()));
96 rules_store_.reset(new StateStore(
97 profile_, kRulesDatabaseUMAClientName,
98 profile_->GetPath().AppendASCII(extensions::kRulesStoreName), false));
100 #if defined(OS_CHROMEOS)
101 const user_manager::User* user =
102 user_manager::UserManager::Get()->GetActiveUser();
103 policy::DeviceLocalAccount::Type device_local_account_type;
104 if (user && policy::IsDeviceLocalAccountUser(user->email(),
105 &device_local_account_type)) {
106 device_local_account_management_policy_provider_.reset(
107 new chromeos::DeviceLocalAccountManagementPolicyProvider(
108 device_local_account_type));
110 #endif // defined(OS_CHROMEOS)
113 void ExtensionSystemImpl::Shared::RegisterManagementPolicyProviders() {
114 management_policy_->RegisterProviders(
115 ExtensionManagementFactory::GetForBrowserContext(profile_)
116 ->GetProviders());
118 #if defined(OS_CHROMEOS)
119 if (device_local_account_management_policy_provider_) {
120 management_policy_->RegisterProvider(
121 device_local_account_management_policy_provider_.get());
123 #endif // defined(OS_CHROMEOS)
125 management_policy_->RegisterProvider(InstallVerifier::Get(profile_));
128 void ExtensionSystemImpl::Shared::Init(bool extensions_enabled) {
129 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::Shared::Init");
130 const base::CommandLine* command_line =
131 base::CommandLine::ForCurrentProcess();
133 navigation_observer_.reset(new NavigationObserver(profile_));
135 bool allow_noisy_errors = !command_line->HasSwitch(switches::kNoErrorDialogs);
136 ExtensionErrorReporter::Init(allow_noisy_errors);
138 shared_user_script_master_.reset(new SharedUserScriptMaster(profile_));
140 // ExtensionService depends on RuntimeData.
141 runtime_data_.reset(new RuntimeData(ExtensionRegistry::Get(profile_)));
143 bool autoupdate_enabled = !profile_->IsGuestSession() &&
144 !profile_->IsSystemProfile();
145 #if defined(OS_CHROMEOS)
146 if (!extensions_enabled)
147 autoupdate_enabled = false;
148 #endif // defined(OS_CHROMEOS)
149 extension_service_.reset(new ExtensionService(
150 profile_, base::CommandLine::ForCurrentProcess(),
151 profile_->GetPath().AppendASCII(extensions::kInstallDirectoryName),
152 ExtensionPrefs::Get(profile_), Blacklist::Get(profile_),
153 autoupdate_enabled, extensions_enabled, &ready_));
155 // These services must be registered before the ExtensionService tries to
156 // load any extensions.
158 InstallVerifier::Get(profile_)->Init();
159 content_verifier_ = new ContentVerifier(
160 profile_, new ChromeContentVerifierDelegate(profile_));
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);
181 } else {
182 extension_service_->component_loader()->AddDefaultComponentExtensions(
183 skip_session_extensions);
185 #else
186 extension_service_->component_loader()->AddDefaultComponentExtensions(
187 skip_session_extensions);
188 #endif
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.
302 shared_->info_map();
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(
347 extension);
350 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
351 const Extension* extension) {
352 base::Time install_time;
353 if (extension->location() != Manifest::COMPONENT) {
354 install_time = ExtensionPrefs::Get(profile_)->
355 GetInstallTime(extension->id());
357 bool incognito_enabled = util::IsIncognitoEnabled(extension->id(), profile_);
359 bool notifications_disabled = false;
360 #if defined(ENABLE_NOTIFICATIONS)
361 message_center::NotifierId notifier_id(
362 message_center::NotifierId::APPLICATION,
363 extension->id());
365 DesktopNotificationService* notification_service =
366 DesktopNotificationServiceFactory::GetForProfile(profile_);
367 notifications_disabled =
368 !notification_service->IsNotifierEnabled(notifier_id);
369 #endif
371 BrowserThread::PostTask(
372 BrowserThread::IO, FROM_HERE,
373 base::Bind(&InfoMap::AddExtension, info_map(),
374 make_scoped_refptr(extension), install_time,
375 incognito_enabled, notifications_disabled));
378 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
379 const std::string& extension_id,
380 const UnloadedExtensionInfo::Reason reason) {
381 BrowserThread::PostTask(
382 BrowserThread::IO,
383 FROM_HERE,
384 base::Bind(&InfoMap::RemoveExtension, info_map(), extension_id, reason));
387 } // namespace extensions