Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: chrome/browser/extensions/api/settings_overrides/settings_overrides_api.cc

Issue 2479113002: Make extensions DSE persistent in browser prefs (Closed)
Patch Set: Added DSE tests Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/settings_overrides/settings_overrides_ap i.h" 5 #include "chrome/browser/extensions/api/settings_overrides/settings_overrides_ap i.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <memory>
8 #include <utility> 9 #include <utility>
9 10
10 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
11 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
12 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/extensions/api/preference/preference_api.h" 14 #include "chrome/browser/extensions/api/preference/preference_api.h"
14 #include "chrome/browser/prefs/session_startup_pref.h" 15 #include "chrome/browser/prefs/session_startup_pref.h"
15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search_engines/template_url_service_factory.h" 17 #include "chrome/browser/search_engines/template_url_service_factory.h"
17 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler. h" 18 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler. h"
18 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
19 #include "components/search_engines/search_engines_pref_names.h" 20 #include "components/search_engines/search_engines_pref_names.h"
20 #include "components/search_engines/template_url.h" 21 #include "components/search_engines/template_url.h"
22 #include "components/search_engines/template_url_data_util.h"
21 #include "components/search_engines/template_url_prepopulate_data.h" 23 #include "components/search_engines/template_url_prepopulate_data.h"
22 #include "extensions/browser/extension_prefs.h" 24 #include "extensions/browser/extension_prefs.h"
23 #include "extensions/browser/extension_prefs_factory.h" 25 #include "extensions/browser/extension_prefs_factory.h"
24 #include "extensions/browser/extension_registry.h" 26 #include "extensions/browser/extension_registry.h"
25 #include "extensions/common/error_utils.h" 27 #include "extensions/common/error_utils.h"
26 #include "extensions/common/manifest_constants.h" 28 #include "extensions/common/manifest_constants.h"
27 29
28 namespace extensions { 30 namespace extensions {
29 31
30 namespace { 32 namespace {
31 33
32 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsOverridesAPI> > 34 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsOverridesAPI> >
33 g_factory = LAZY_INSTANCE_INITIALIZER; 35 g_factory = LAZY_INSTANCE_INITIALIZER;
34 36
35 const char kManyStartupPagesWarning[] = "* specifies more than 1 startup URL. " 37 const char kManyStartupPagesWarning[] = "* specifies more than 1 startup URL. "
36 "All but the first will be ignored."; 38 "All but the first will be ignored.";
37 39
38 using api::manifest_types::ChromeSettingsOverrides; 40 using api::manifest_types::ChromeSettingsOverrides;
39 41
40 std::string SubstituteInstallParam(std::string str, 42 std::string SubstituteInstallParam(std::string str,
41 const std::string& install_parameter) { 43 const std::string& install_parameter) {
42 base::ReplaceSubstringsAfterOffset(&str, 0, "__PARAM__", install_parameter); 44 base::ReplaceSubstringsAfterOffset(&str, 0, "__PARAM__", install_parameter);
43 return str; 45 return str;
44 } 46 }
45 47
46 // Find the prepopulated search engine with the given id.
47 std::unique_ptr<TemplateURLData> GetPrepopulatedSearchProvider(
48 PrefService* prefs,
49 int prepopulated_id) {
50 size_t default_index;
51 std::vector<std::unique_ptr<TemplateURLData>> engines =
52 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, &default_index);
53 for (auto& engine : engines) {
54 if (engine->prepopulate_id == prepopulated_id)
55 return std::move(engine);
56 }
57 return nullptr;
58 }
59
60 std::unique_ptr<TemplateURLData> ConvertSearchProvider( 48 std::unique_ptr<TemplateURLData> ConvertSearchProvider(
61 PrefService* prefs, 49 PrefService* prefs,
62 const ChromeSettingsOverrides::Search_provider& search_provider, 50 const ChromeSettingsOverrides::Search_provider& search_provider,
63 const std::string& install_parameter) { 51 const std::string& install_parameter) {
64 std::unique_ptr<TemplateURLData> data; 52 std::unique_ptr<TemplateURLData> data;
65 if (search_provider.prepopulated_id) { 53 if (search_provider.prepopulated_id) {
66 data = 54 data = TemplateURLPrepopulateData::GetPrepopulatedEngine(
67 GetPrepopulatedSearchProvider(prefs, *search_provider.prepopulated_id); 55 prefs, *search_provider.prepopulated_id);
68 if (!data) { 56 if (!data) {
69 VLOG(1) << "Settings Overrides API can't recognize prepopulated_id=" 57 VLOG(1) << "Settings Overrides API can't recognize prepopulated_id="
70 << *search_provider.prepopulated_id; 58 << *search_provider.prepopulated_id;
71 } 59 }
72 } 60 }
73 61
74 if (!data) 62 if (!data)
75 data = base::MakeUnique<TemplateURLData>(); 63 data = base::MakeUnique<TemplateURLData>();
76 64
77 if (search_provider.name) 65 if (search_provider.name)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 SettingsOverridesAPI::~SettingsOverridesAPI() { 124 SettingsOverridesAPI::~SettingsOverridesAPI() {
137 } 125 }
138 126
139 BrowserContextKeyedAPIFactory<SettingsOverridesAPI>* 127 BrowserContextKeyedAPIFactory<SettingsOverridesAPI>*
140 SettingsOverridesAPI::GetFactoryInstance() { 128 SettingsOverridesAPI::GetFactoryInstance() {
141 return g_factory.Pointer(); 129 return g_factory.Pointer();
142 } 130 }
143 131
144 void SettingsOverridesAPI::SetPref(const std::string& extension_id, 132 void SettingsOverridesAPI::SetPref(const std::string& extension_id,
145 const std::string& pref_key, 133 const std::string& pref_key,
146 base::Value* value) { 134 std::unique_ptr<base::Value> value) const {
147 PreferenceAPI* prefs = PreferenceAPI::Get(profile_); 135 PreferenceAPI* prefs = PreferenceAPI::Get(profile_);
148 if (!prefs) 136 if (!prefs)
149 return; // Expected in unit tests. 137 return; // Expected in unit tests.
150 prefs->SetExtensionControlledPref(extension_id, 138 prefs->SetExtensionControlledPref(
151 pref_key, 139 extension_id, pref_key, kExtensionPrefsScopeRegular, value.release());
152 kExtensionPrefsScopeRegular,
153 value);
154 } 140 }
155 141
156 void SettingsOverridesAPI::UnsetPref(const std::string& extension_id, 142 void SettingsOverridesAPI::UnsetPref(const std::string& extension_id,
157 const std::string& pref_key) { 143 const std::string& pref_key) const {
158 PreferenceAPI* prefs = PreferenceAPI::Get(profile_); 144 PreferenceAPI* prefs = PreferenceAPI::Get(profile_);
159 if (!prefs) 145 if (!prefs)
160 return; // Expected in unit tests. 146 return; // Expected in unit tests.
161 prefs->RemoveExtensionControlledPref( 147 prefs->RemoveExtensionControlledPref(
162 extension_id, 148 extension_id,
163 pref_key, 149 pref_key,
164 kExtensionPrefsScopeRegular); 150 kExtensionPrefsScopeRegular);
165 } 151 }
166 152
167 void SettingsOverridesAPI::OnExtensionLoaded( 153 void SettingsOverridesAPI::OnExtensionLoaded(
168 content::BrowserContext* browser_context, 154 content::BrowserContext* browser_context,
169 const Extension* extension) { 155 const Extension* extension) {
170 const SettingsOverrides* settings = SettingsOverrides::Get(extension); 156 const SettingsOverrides* settings = SettingsOverrides::Get(extension);
171 if (settings) { 157 if (settings) {
172 std::string install_parameter = 158 std::string install_parameter =
173 ExtensionPrefs::Get(profile_)->GetInstallParam(extension->id()); 159 ExtensionPrefs::Get(profile_)->GetInstallParam(extension->id());
174 if (settings->homepage) { 160 if (settings->homepage) {
175 SetPref(extension->id(), 161 SetPref(extension->id(), prefs::kHomePage,
176 prefs::kHomePage, 162 base::MakeUnique<base::StringValue>(SubstituteInstallParam(
177 new base::StringValue(SubstituteInstallParam(
178 settings->homepage->spec(), install_parameter))); 163 settings->homepage->spec(), install_parameter)));
179 SetPref(extension->id(), 164 SetPref(extension->id(), prefs::kHomePageIsNewTabPage,
180 prefs::kHomePageIsNewTabPage, 165 base::MakeUnique<base::FundamentalValue>(false));
181 new base::FundamentalValue(false));
182 } 166 }
183 if (!settings->startup_pages.empty()) { 167 if (!settings->startup_pages.empty()) {
184 SetPref(extension->id(), 168 // kPrefValueURLs variable defined below is workaround for undefined
185 prefs::kRestoreOnStartup, 169 // symbol SessionStartupPref::kPrefValueURLs link error on mac.
vasilii 2016/11/30 14:00:02 I don't understand it. Now everything compiles fin
Alexander Yashkin 2016/12/05 18:16:48 It was a problem with ODR rule applying for Sessio
186 new base::FundamentalValue(SessionStartupPref::kPrefValueURLs)); 170 const int kPrefValueURLs = SessionStartupPref::kPrefValueURLs;
171 SetPref(extension->id(), prefs::kRestoreOnStartup,
172 base::MakeUnique<base::FundamentalValue>(kPrefValueURLs));
187 if (settings->startup_pages.size() > 1) { 173 if (settings->startup_pages.size() > 1) {
188 VLOG(1) << extensions::ErrorUtils::FormatErrorMessage( 174 VLOG(1) << extensions::ErrorUtils::FormatErrorMessage(
189 kManyStartupPagesWarning, 175 kManyStartupPagesWarning,
190 manifest_keys::kSettingsOverride); 176 manifest_keys::kSettingsOverride);
191 } 177 }
192 std::unique_ptr<base::ListValue> url_list(new base::ListValue); 178 std::unique_ptr<base::ListValue> url_list(new base::ListValue);
193 url_list->AppendString(SubstituteInstallParam( 179 url_list->AppendString(SubstituteInstallParam(
194 settings->startup_pages[0].spec(), install_parameter)); 180 settings->startup_pages[0].spec(), install_parameter));
195 SetPref( 181 SetPref(extension->id(), prefs::kURLsToRestoreOnStartup,
196 extension->id(), prefs::kURLsToRestoreOnStartup, url_list.release()); 182 std::move(url_list));
197 } 183 }
198 if (settings->search_engine) { 184 if (settings->search_engine) {
199 // Bring the preference to the correct state. Before this code set it 185 // Bring the preference to the correct state. Before this code set it
200 // to "true" for all search engines. Thus, we should overwrite it for 186 // to "true" for all search engines. Thus, we should overwrite it for
201 // all search engines. 187 // all search engines.
202 if (settings->search_engine->is_default) { 188 if (settings->search_engine->is_default) {
203 SetPref(extension->id(), 189 SetPref(extension->id(), prefs::kDefaultSearchProviderEnabled,
204 prefs::kDefaultSearchProviderEnabled, 190 base::MakeUnique<base::FundamentalValue>(true));
205 new base::FundamentalValue(true));
206 } else { 191 } else {
207 UnsetPref(extension->id(), prefs::kDefaultSearchProviderEnabled); 192 UnsetPref(extension->id(), prefs::kDefaultSearchProviderEnabled);
208 } 193 }
209 DCHECK(url_service_); 194 DCHECK(url_service_);
210 if (url_service_->loaded()) { 195 if (url_service_->loaded()) {
211 RegisterSearchProvider(extension); 196 RegisterSearchProvider(extension);
212 } else { 197 } else {
213 if (!template_url_sub_) { 198 if (!template_url_sub_) {
214 template_url_sub_ = url_service_->RegisterOnLoadedCallback( 199 template_url_sub_ = url_service_->RegisterOnLoadedCallback(
215 base::Bind(&SettingsOverridesAPI::OnTemplateURLsLoaded, 200 base::Bind(&SettingsOverridesAPI::OnTemplateURLsLoaded,
(...skipping 13 matching lines...) Expand all
229 if (settings) { 214 if (settings) {
230 if (settings->homepage) { 215 if (settings->homepage) {
231 UnsetPref(extension->id(), prefs::kHomePage); 216 UnsetPref(extension->id(), prefs::kHomePage);
232 UnsetPref(extension->id(), prefs::kHomePageIsNewTabPage); 217 UnsetPref(extension->id(), prefs::kHomePageIsNewTabPage);
233 } 218 }
234 if (!settings->startup_pages.empty()) { 219 if (!settings->startup_pages.empty()) {
235 UnsetPref(extension->id(), prefs::kRestoreOnStartup); 220 UnsetPref(extension->id(), prefs::kRestoreOnStartup);
236 UnsetPref(extension->id(), prefs::kURLsToRestoreOnStartup); 221 UnsetPref(extension->id(), prefs::kURLsToRestoreOnStartup);
237 } 222 }
238 if (settings->search_engine) { 223 if (settings->search_engine) {
224 if (settings->search_engine->is_default) {
225 // Current extension can be overriding DSE.
226 UnsetPref(extension->id(),
227 DefaultSearchManager::kDefaultSearchProviderDataPrefName);
228 }
239 DCHECK(url_service_); 229 DCHECK(url_service_);
240 if (url_service_->loaded()) { 230 if (url_service_->loaded()) {
241 url_service_->RemoveExtensionControlledTURL( 231 url_service_->RemoveExtensionControlledTURL(
242 extension->id(), TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); 232 extension->id(), TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION);
243 } else { 233 } else {
244 pending_extensions_.erase(extension); 234 pending_extensions_.erase(extension);
245 } 235 }
246 } 236 }
247 } 237 }
248 } 238 }
(...skipping 25 matching lines...) Expand all
274 264
275 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); 265 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
276 info->install_time = prefs->GetInstallTime(extension->id()); 266 info->install_time = prefs->GetInstallTime(extension->id());
277 std::string install_parameter = prefs->GetInstallParam(extension->id()); 267 std::string install_parameter = prefs->GetInstallParam(extension->id());
278 std::unique_ptr<TemplateURLData> data = ConvertSearchProvider( 268 std::unique_ptr<TemplateURLData> data = ConvertSearchProvider(
279 profile_->GetPrefs(), *settings->search_engine, install_parameter); 269 profile_->GetPrefs(), *settings->search_engine, install_parameter);
280 auto turl = base::MakeUnique<TemplateURL>( 270 auto turl = base::MakeUnique<TemplateURL>(
281 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); 271 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION);
282 272
283 url_service_->AddExtensionControlledTURL(std::move(turl), std::move(info)); 273 url_service_->AddExtensionControlledTURL(std::move(turl), std::move(info));
274
275 if (settings->search_engine->is_default) {
276 // Override current DSE pref to have extension overriden value.
277 SetPref(extension->id(),
278 DefaultSearchManager::kDefaultSearchProviderDataPrefName,
279 TemplateURLDataToDictionary(*data));
280 }
284 } 281 }
285 282
286 template <> 283 template <>
287 void BrowserContextKeyedAPIFactory< 284 void BrowserContextKeyedAPIFactory<
288 SettingsOverridesAPI>::DeclareFactoryDependencies() { 285 SettingsOverridesAPI>::DeclareFactoryDependencies() {
289 DependsOn(ExtensionPrefsFactory::GetInstance()); 286 DependsOn(ExtensionPrefsFactory::GetInstance());
290 DependsOn(PreferenceAPI::GetFactoryInstance()); 287 DependsOn(PreferenceAPI::GetFactoryInstance());
291 DependsOn(TemplateURLServiceFactory::GetInstance()); 288 DependsOn(TemplateURLServiceFactory::GetInstance());
292 } 289 }
293 290
294 } // namespace extensions 291 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698