Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / extensions / extension_system_impl.cc
blobf54aae1cd0873f3a785133b293a8a1d2ec5c939c
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_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_system_factory.h"
22 #include "chrome/browser/extensions/extension_util.h"
23 #include "chrome/browser/extensions/install_verifier.h"
24 #include "chrome/browser/extensions/navigation_observer.h"
25 #include "chrome/browser/extensions/shared_module_service.h"
26 #include "chrome/browser/extensions/shared_user_script_master.h"
27 #include "chrome/browser/extensions/state_store_notification_observer.h"
28 #include "chrome/browser/extensions/unpacked_installer.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
32 #include "chrome/common/chrome_switches.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/notifier_state_tracker.h"
50 #include "chrome/browser/notifications/notifier_state_tracker_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 content_verifier_ = new ContentVerifier(
139 profile_, new ChromeContentVerifierDelegate(profile_));
141 shared_user_script_master_.reset(new SharedUserScriptMaster(profile_));
143 // ExtensionService depends on RuntimeData.
144 runtime_data_.reset(new RuntimeData(ExtensionRegistry::Get(profile_)));
146 bool autoupdate_enabled = !profile_->IsGuestSession() &&
147 !profile_->IsSystemProfile();
148 #if defined(OS_CHROMEOS)
149 if (!extensions_enabled)
150 autoupdate_enabled = false;
151 #endif // defined(OS_CHROMEOS)
152 extension_service_.reset(new ExtensionService(
153 profile_, base::CommandLine::ForCurrentProcess(),
154 profile_->GetPath().AppendASCII(extensions::kInstallDirectoryName),
155 ExtensionPrefs::Get(profile_), Blacklist::Get(profile_),
156 autoupdate_enabled, extensions_enabled, &ready_));
158 // These services must be registered before the ExtensionService tries to
159 // load any extensions.
161 InstallVerifier::Get(profile_)->Init();
162 ContentVerifierDelegate::Mode mode =
163 ChromeContentVerifierDelegate::GetDefaultMode();
164 #if defined(OS_CHROMEOS)
165 mode = std::max(mode, ContentVerifierDelegate::BOOTSTRAP);
166 #endif // defined(OS_CHROMEOS)
167 if (mode >= ContentVerifierDelegate::BOOTSTRAP)
168 content_verifier_->Start();
169 info_map()->SetContentVerifier(content_verifier_.get());
171 management_policy_.reset(new ManagementPolicy);
172 RegisterManagementPolicyProviders();
175 bool skip_session_extensions = false;
176 #if defined(OS_CHROMEOS)
177 // Skip loading session extensions if we are not in a user session.
178 skip_session_extensions = !chromeos::LoginState::Get()->IsUserLoggedIn();
179 if (chrome::IsRunningInForcedAppMode()) {
180 extension_service_->component_loader()->
181 AddDefaultComponentExtensionsForKioskMode(skip_session_extensions);
182 } else {
183 extension_service_->component_loader()->AddDefaultComponentExtensions(
184 skip_session_extensions);
186 #else
187 extension_service_->component_loader()->AddDefaultComponentExtensions(
188 skip_session_extensions);
189 #endif
190 if (command_line->HasSwitch(switches::kLoadComponentExtension)) {
191 base::CommandLine::StringType path_list =
192 command_line->GetSwitchValueNative(switches::kLoadComponentExtension);
193 base::StringTokenizerT<base::CommandLine::StringType,
194 base::CommandLine::StringType::const_iterator>
195 t(path_list, FILE_PATH_LITERAL(","));
196 while (t.GetNext()) {
197 // Load the component extension manifest synchronously.
198 // Blocking the UI thread is acceptable here since
199 // this flag designated for developers.
200 base::ThreadRestrictions::ScopedAllowIO allow_io;
201 extension_service_->component_loader()->AddOrReplace(
202 base::FilePath(t.token()));
206 app_sorting_.reset(new ChromeAppSorting(profile_));
208 extension_service_->Init();
210 // Make the chrome://extension-icon/ resource available.
211 content::URLDataSource::Add(profile_, new ExtensionIconSource(profile_));
213 quota_service_.reset(new QuotaService);
215 if (extensions_enabled) {
216 // Load any extensions specified with --load-extension.
217 // TODO(yoz): Seems like this should move into ExtensionService::Init.
218 // But maybe it's no longer important.
219 if (command_line->HasSwitch(switches::kLoadExtension)) {
220 base::CommandLine::StringType path_list =
221 command_line->GetSwitchValueNative(switches::kLoadExtension);
222 base::StringTokenizerT<base::CommandLine::StringType,
223 base::CommandLine::StringType::const_iterator>
224 t(path_list, FILE_PATH_LITERAL(","));
225 while (t.GetNext()) {
226 std::string extension_id;
227 UnpackedInstaller::Create(extension_service_.get())->
228 LoadFromCommandLine(base::FilePath(t.token()), &extension_id);
234 void ExtensionSystemImpl::Shared::Shutdown() {
235 if (content_verifier_.get())
236 content_verifier_->Shutdown();
237 if (extension_service_)
238 extension_service_->Shutdown();
241 StateStore* ExtensionSystemImpl::Shared::state_store() {
242 return state_store_.get();
245 StateStore* ExtensionSystemImpl::Shared::rules_store() {
246 return rules_store_.get();
249 ExtensionService* ExtensionSystemImpl::Shared::extension_service() {
250 return extension_service_.get();
253 RuntimeData* ExtensionSystemImpl::Shared::runtime_data() {
254 return runtime_data_.get();
257 ManagementPolicy* ExtensionSystemImpl::Shared::management_policy() {
258 return management_policy_.get();
261 SharedUserScriptMaster*
262 ExtensionSystemImpl::Shared::shared_user_script_master() {
263 return shared_user_script_master_.get();
266 InfoMap* ExtensionSystemImpl::Shared::info_map() {
267 if (!extension_info_map_.get())
268 extension_info_map_ = new InfoMap();
269 return extension_info_map_.get();
272 QuotaService* ExtensionSystemImpl::Shared::quota_service() {
273 return quota_service_.get();
276 AppSorting* ExtensionSystemImpl::Shared::app_sorting() {
277 return app_sorting_.get();
280 ContentVerifier* ExtensionSystemImpl::Shared::content_verifier() {
281 return content_verifier_.get();
285 // ExtensionSystemImpl
288 ExtensionSystemImpl::ExtensionSystemImpl(Profile* profile)
289 : profile_(profile) {
290 shared_ = ExtensionSystemSharedFactory::GetForBrowserContext(profile);
292 if (!profile->IsOffTheRecord()) {
293 shared_->InitPrefs();
297 ExtensionSystemImpl::~ExtensionSystemImpl() {
300 void ExtensionSystemImpl::Shutdown() {
303 void ExtensionSystemImpl::InitForRegularProfile(bool extensions_enabled) {
304 TRACE_EVENT0("browser,startup", "ExtensionSystemImpl::InitForRegularProfile");
305 DCHECK(!profile_->IsOffTheRecord());
306 if (shared_user_script_master() || extension_service())
307 return; // Already initialized.
309 // The InfoMap needs to be created before the ProcessManager.
310 shared_->info_map();
311 shared_->Init(extensions_enabled);
314 ExtensionService* ExtensionSystemImpl::extension_service() {
315 return shared_->extension_service();
318 RuntimeData* ExtensionSystemImpl::runtime_data() {
319 return shared_->runtime_data();
322 ManagementPolicy* ExtensionSystemImpl::management_policy() {
323 return shared_->management_policy();
326 SharedUserScriptMaster* ExtensionSystemImpl::shared_user_script_master() {
327 return shared_->shared_user_script_master();
330 StateStore* ExtensionSystemImpl::state_store() {
331 return shared_->state_store();
334 StateStore* ExtensionSystemImpl::rules_store() {
335 return shared_->rules_store();
338 InfoMap* ExtensionSystemImpl::info_map() { return shared_->info_map(); }
340 const OneShotEvent& ExtensionSystemImpl::ready() const {
341 return shared_->ready();
344 QuotaService* ExtensionSystemImpl::quota_service() {
345 return shared_->quota_service();
348 AppSorting* ExtensionSystemImpl::app_sorting() {
349 return shared_->app_sorting();
352 ContentVerifier* ExtensionSystemImpl::content_verifier() {
353 return shared_->content_verifier();
356 scoped_ptr<ExtensionSet> ExtensionSystemImpl::GetDependentExtensions(
357 const Extension* extension) {
358 return extension_service()->shared_module_service()->GetDependentExtensions(
359 extension);
362 void ExtensionSystemImpl::RegisterExtensionWithRequestContexts(
363 const Extension* extension,
364 const base::Closure& callback) {
365 base::Time install_time;
366 if (extension->location() != Manifest::COMPONENT) {
367 install_time = ExtensionPrefs::Get(profile_)->
368 GetInstallTime(extension->id());
370 bool incognito_enabled = util::IsIncognitoEnabled(extension->id(), profile_);
372 bool notifications_disabled = false;
373 #if defined(ENABLE_NOTIFICATIONS)
374 message_center::NotifierId notifier_id(
375 message_center::NotifierId::APPLICATION,
376 extension->id());
378 NotifierStateTracker* notifier_state_tracker =
379 NotifierStateTrackerFactory::GetForProfile(profile_);
380 notifications_disabled =
381 !notifier_state_tracker->IsNotifierEnabled(notifier_id);
382 #endif
384 BrowserThread::PostTaskAndReply(
385 BrowserThread::IO, FROM_HERE,
386 base::Bind(&InfoMap::AddExtension, info_map(),
387 make_scoped_refptr(extension), install_time, incognito_enabled,
388 notifications_disabled),
389 callback);
392 void ExtensionSystemImpl::UnregisterExtensionWithRequestContexts(
393 const std::string& extension_id,
394 const UnloadedExtensionInfo::Reason reason) {
395 BrowserThread::PostTask(
396 BrowserThread::IO,
397 FROM_HERE,
398 base::Bind(&InfoMap::RemoveExtension, info_map(), extension_id, reason));
401 } // namespace extensions