Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / external_component_loader.cc
blobecfc18bd44fc9a0558e92dbc6f11fa79f8c52fa4
1 // Copyright (c) 2013 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/external_component_loader.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_change_registrar.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/values.h"
11 #include "chrome/browser/bookmarks/enhanced_bookmarks_features.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/external_provider_impl.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/extensions/extension_constants.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/user_prefs/pref_registry_syncable.h"
18 #include "content/public/browser/browser_thread.h"
20 namespace extensions {
22 #if defined(OS_CHROMEOS)
23 namespace {
24 // Table mapping language codes to the extension ids of high-quality
25 // speech synthesis extensions. See the comment in StartLoading() for more.
26 struct LangToExtensionId {
27 const char* lang;
28 const char* extension_id;
30 LangToExtensionId kLangToExtensionIdTable[] = {
31 { "en-US", extension_misc::kHighQuality_en_US_ExtensionId }
33 } // anonymous namespace
34 #endif // defined(OS_CHROMEOS)
36 ExternalComponentLoader::ExternalComponentLoader(Profile* profile)
37 : profile_(profile) {
38 #if defined(OS_CHROMEOS)
39 pref_change_registrar_.reset(new PrefChangeRegistrar());
40 pref_change_registrar_->Init(profile->GetPrefs());
41 pref_change_registrar_->Add(
42 prefs::kHighQualitySpeechSynthesisLanguages,
43 base::Bind(&ExternalComponentLoader::StartLoading, AsWeakPtr()));
45 for (size_t i = 0; i < arraysize(kLangToExtensionIdTable); ++i) {
46 lang_to_extension_id_map_[kLangToExtensionIdTable[i].lang] =
47 kLangToExtensionIdTable[i].extension_id;
49 #endif
52 ExternalComponentLoader::~ExternalComponentLoader() {}
54 void ExternalComponentLoader::StartLoading() {
55 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
56 prefs_.reset(new base::DictionaryValue());
57 std::string appId = extension_misc::kInAppPaymentsSupportAppId;
58 prefs_->SetString(appId + ".external_update_url",
59 extension_urls::GetWebstoreUpdateUrl().spec());
61 if (CommandLine::ForCurrentProcess()->
62 GetSwitchValueASCII(switches::kEnableEnhancedBookmarks) != "0") {
63 std::string ext_id = GetEnhancedBookmarksExtensionId();
64 if (!ext_id.empty()) {
65 prefs_->SetString(ext_id + ".external_update_url",
66 extension_urls::GetWebstoreUpdateUrl().spec());
70 #if defined(OS_CHROMEOS)
71 // Chrome OS comes with medium-quality speech synthesis extensions built-in.
72 // When the user speaks a certain threshold of utterances in the same
73 // session, we set a preference indicating that high quality speech is
74 // enabled for that language. Here, we check the preference and prepare
75 // the list of external extensions to be installed based on that.
76 PrefService* pref_service = profile_->GetPrefs();
77 const base::ListValue* languages =
78 pref_service->GetList(prefs::kHighQualitySpeechSynthesisLanguages);
79 for (size_t i = 0; i < languages->GetSize(); ++i) {
80 std::string lang;
81 if (!languages->GetString(i, &lang))
82 continue;
84 base::hash_map<std::string, std::string>::iterator iter =
85 lang_to_extension_id_map_.find(lang);
86 if (iter == lang_to_extension_id_map_.end())
87 continue;
89 std::string extension_id = iter->second;
90 base::DictionaryValue* extension = new base::DictionaryValue();
91 prefs_->Set(extension_id, extension);
92 extension->SetString(ExternalProviderImpl::kExternalUpdateUrl,
93 extension_urls::GetWebstoreUpdateUrl().spec());
94 base::ListValue* supported_locales = new base::ListValue();
95 supported_locales->AppendString(g_browser_process->GetApplicationLocale());
96 extension->Set(ExternalProviderImpl::kSupportedLocales, supported_locales);
97 extension->SetBoolean(ExternalProviderImpl::kIsFromWebstore, true);
99 #endif // defined(OS_CHROMEOS)
101 LoadFinished();
104 // static
105 void ExternalComponentLoader::RegisterProfilePrefs(
106 user_prefs::PrefRegistrySyncable* registry) {
107 #if defined(OS_CHROMEOS)
108 registry->RegisterListPref(prefs::kHighQualitySpeechSynthesisLanguages,
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
110 #endif
113 } // namespace extensions