| OLD | NEW |
| 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 "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/extensions/api/preference/preference_api.h" | 10 #include "chrome/browser/extensions/api/preference/preference_api.h" |
| 11 #include "chrome/browser/prefs/session_startup_pref.h" | 11 #include "chrome/browser/prefs/session_startup_pref.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "chrome/browser/search_engines/template_url_service_factory.h" | 14 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 15 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.
h" | 15 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.
h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
| 18 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 19 #include "extensions/browser/extension_prefs.h" | 19 #include "extensions/browser/extension_prefs.h" |
| 20 #include "extensions/browser/extension_prefs_factory.h" | 20 #include "extensions/browser/extension_prefs_factory.h" |
| 21 #include "extensions/common/error_utils.h" | 21 #include "extensions/common/error_utils.h" |
| 22 #include "extensions/common/manifest_constants.h" | 22 #include "extensions/common/manifest_constants.h" |
| 23 | 23 |
| 24 namespace extensions { | 24 namespace extensions { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 |
| 27 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsOverridesAPI> > | 28 base::LazyInstance<BrowserContextKeyedAPIFactory<SettingsOverridesAPI> > |
| 28 g_factory = LAZY_INSTANCE_INITIALIZER; | 29 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 29 | 30 |
| 30 const char kManyStartupPagesWarning[] = "* specifies more than 1 startup URL. " | 31 const char kManyStartupPagesWarning[] = "* specifies more than 1 startup URL. " |
| 31 "All but the first will be ignored."; | 32 "All but the first will be ignored."; |
| 32 | 33 |
| 33 using api::manifest_types::ChromeSettingsOverrides; | 34 using api::manifest_types::ChromeSettingsOverrides; |
| 34 | 35 |
| 36 std::string SubstituteInstallParam(std::string str, |
| 37 const std::string& install_parameter) { |
| 38 ReplaceSubstringsAfterOffset(&str, 0, "__PARAM__", install_parameter); |
| 39 return str; |
| 40 } |
| 41 |
| 35 TemplateURLData ConvertSearchProvider( | 42 TemplateURLData ConvertSearchProvider( |
| 36 const ChromeSettingsOverrides::Search_provider& search_provider) { | 43 const ChromeSettingsOverrides::Search_provider& search_provider, |
| 44 const std::string& install_parameter) { |
| 37 TemplateURLData data; | 45 TemplateURLData data; |
| 38 | 46 |
| 39 data.short_name = base::UTF8ToUTF16(search_provider.name); | 47 data.short_name = base::UTF8ToUTF16(search_provider.name); |
| 40 data.SetKeyword(base::UTF8ToUTF16(search_provider.keyword)); | 48 data.SetKeyword(base::UTF8ToUTF16(search_provider.keyword)); |
| 41 data.SetURL(search_provider.search_url); | 49 data.SetURL( |
| 42 if (search_provider.suggest_url) | 50 SubstituteInstallParam(search_provider.search_url, install_parameter)); |
| 43 data.suggestions_url = *search_provider.suggest_url; | 51 if (search_provider.suggest_url) { |
| 44 if (search_provider.instant_url) | 52 data.suggestions_url = |
| 45 data.instant_url = *search_provider.instant_url; | 53 SubstituteInstallParam(*search_provider.suggest_url, install_parameter); |
| 46 if (search_provider.image_url) | 54 } |
| 47 data.image_url = *search_provider.image_url; | 55 if (search_provider.instant_url) { |
| 56 data.instant_url = |
| 57 SubstituteInstallParam(*search_provider.instant_url, install_parameter); |
| 58 } |
| 59 if (search_provider.image_url) { |
| 60 data.image_url = |
| 61 SubstituteInstallParam(*search_provider.image_url, install_parameter); |
| 62 } |
| 48 if (search_provider.search_url_post_params) | 63 if (search_provider.search_url_post_params) |
| 49 data.search_url_post_params = *search_provider.search_url_post_params; | 64 data.search_url_post_params = *search_provider.search_url_post_params; |
| 50 if (search_provider.suggest_url_post_params) | 65 if (search_provider.suggest_url_post_params) |
| 51 data.suggestions_url_post_params = *search_provider.suggest_url_post_params; | 66 data.suggestions_url_post_params = *search_provider.suggest_url_post_params; |
| 52 if (search_provider.instant_url_post_params) | 67 if (search_provider.instant_url_post_params) |
| 53 data.instant_url_post_params = *search_provider.instant_url_post_params; | 68 data.instant_url_post_params = *search_provider.instant_url_post_params; |
| 54 if (search_provider.image_url_post_params) | 69 if (search_provider.image_url_post_params) |
| 55 data.image_url_post_params = *search_provider.image_url_post_params; | 70 data.image_url_post_params = *search_provider.image_url_post_params; |
| 56 data.favicon_url = GURL(search_provider.favicon_url); | 71 data.favicon_url = GURL( |
| 72 SubstituteInstallParam(search_provider.favicon_url, install_parameter)); |
| 57 data.show_in_default_list = true; | 73 data.show_in_default_list = true; |
| 58 data.safe_for_autoreplace = false; | 74 data.safe_for_autoreplace = false; |
| 59 data.input_encodings.push_back(search_provider.encoding); | 75 data.input_encodings.push_back(search_provider.encoding); |
| 60 data.date_created = base::Time(); | 76 data.date_created = base::Time(); |
| 61 data.last_modified = base::Time(); | 77 data.last_modified = base::Time(); |
| 62 data.prepopulate_id = 0; | 78 data.prepopulate_id = 0; |
| 63 if (search_provider.alternate_urls) { | 79 if (search_provider.alternate_urls) { |
| 64 for (size_t i = 0; i < search_provider.alternate_urls->size(); ++i) { | 80 for (size_t i = 0; i < search_provider.alternate_urls->size(); ++i) { |
| 65 if (!search_provider.alternate_urls->at(i).empty()) | 81 if (!search_provider.alternate_urls->at(i).empty()) |
| 66 data.alternate_urls.push_back(search_provider.alternate_urls->at(i)); | 82 data.alternate_urls.push_back(SubstituteInstallParam( |
| 83 search_provider.alternate_urls->at(i), install_parameter)); |
| 67 } | 84 } |
| 68 } | 85 } |
| 69 return data; | 86 return data; |
| 70 } | 87 } |
| 88 |
| 71 } // namespace | 89 } // namespace |
| 72 | 90 |
| 73 SettingsOverridesAPI::SettingsOverridesAPI(content::BrowserContext* context) | 91 SettingsOverridesAPI::SettingsOverridesAPI(content::BrowserContext* context) |
| 74 : profile_(Profile::FromBrowserContext(context)), | 92 : profile_(Profile::FromBrowserContext(context)), |
| 75 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)) { | 93 url_service_(TemplateURLServiceFactory::GetForProfile(profile_)) { |
| 76 DCHECK(profile_); | 94 DCHECK(profile_); |
| 77 registrar_.Add(this, | 95 registrar_.Add(this, |
| 78 chrome::NOTIFICATION_EXTENSION_LOADED, | 96 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 79 content::Source<Profile>(profile_)); | 97 content::Source<Profile>(profile_)); |
| 80 registrar_.Add(this, | 98 registrar_.Add(this, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 int type, | 130 int type, |
| 113 const content::NotificationSource& source, | 131 const content::NotificationSource& source, |
| 114 const content::NotificationDetails& details) { | 132 const content::NotificationDetails& details) { |
| 115 switch (type) { | 133 switch (type) { |
| 116 case chrome::NOTIFICATION_EXTENSION_LOADED: { | 134 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| 117 const Extension* extension = | 135 const Extension* extension = |
| 118 content::Details<const Extension>(details).ptr(); | 136 content::Details<const Extension>(details).ptr(); |
| 119 const SettingsOverrides* settings = | 137 const SettingsOverrides* settings = |
| 120 SettingsOverrides::Get(extension); | 138 SettingsOverrides::Get(extension); |
| 121 if (settings) { | 139 if (settings) { |
| 140 std::string install_parameter = |
| 141 ExtensionPrefs::Get(profile_)->GetInstallParam(extension->id()); |
| 122 if (settings->homepage) { | 142 if (settings->homepage) { |
| 123 SetPref(extension->id(), prefs::kHomePage, | 143 SetPref(extension->id(), |
| 124 new base::StringValue(settings->homepage->spec())); | 144 prefs::kHomePage, |
| 145 new base::StringValue(SubstituteInstallParam( |
| 146 settings->homepage->spec(), install_parameter))); |
| 125 SetPref(extension->id(), prefs::kHomePageIsNewTabPage, | 147 SetPref(extension->id(), prefs::kHomePageIsNewTabPage, |
| 126 new base::FundamentalValue(false)); | 148 new base::FundamentalValue(false)); |
| 127 } | 149 } |
| 128 if (!settings->startup_pages.empty()) { | 150 if (!settings->startup_pages.empty()) { |
| 129 SetPref(extension->id(), prefs::kRestoreOnStartup, | 151 SetPref(extension->id(), prefs::kRestoreOnStartup, |
| 130 new base::FundamentalValue( | 152 new base::FundamentalValue( |
| 131 SessionStartupPref::kPrefValueURLs)); | 153 SessionStartupPref::kPrefValueURLs)); |
| 132 if (settings->startup_pages.size() > 1) { | 154 if (settings->startup_pages.size() > 1) { |
| 133 VLOG(1) << extensions::ErrorUtils::FormatErrorMessage( | 155 VLOG(1) << extensions::ErrorUtils::FormatErrorMessage( |
| 134 kManyStartupPagesWarning, manifest_keys::kSettingsOverride); | 156 kManyStartupPagesWarning, manifest_keys::kSettingsOverride); |
| 135 } | 157 } |
| 136 scoped_ptr<base::ListValue> url_list(new base::ListValue); | 158 scoped_ptr<base::ListValue> url_list(new base::ListValue); |
| 137 url_list->Append( | 159 url_list->Append(new base::StringValue(SubstituteInstallParam( |
| 138 new base::StringValue(settings->startup_pages[0].spec())); | 160 settings->startup_pages[0].spec(), install_parameter))); |
| 139 SetPref(extension->id(), prefs::kURLsToRestoreOnStartup, | 161 SetPref(extension->id(), prefs::kURLsToRestoreOnStartup, |
| 140 url_list.release()); | 162 url_list.release()); |
| 141 } | 163 } |
| 142 if (settings->search_engine) { | 164 if (settings->search_engine) { |
| 143 SetPref(extension->id(), prefs::kDefaultSearchProviderEnabled, | 165 SetPref(extension->id(), prefs::kDefaultSearchProviderEnabled, |
| 144 new base::FundamentalValue(true)); | 166 new base::FundamentalValue(true)); |
| 145 DCHECK(url_service_); | 167 DCHECK(url_service_); |
| 146 if (url_service_->loaded()) { | 168 if (url_service_->loaded()) { |
| 147 RegisterSearchProvider(extension); | 169 RegisterSearchProvider(extension); |
| 148 } else { | 170 } else { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 void SettingsOverridesAPI::RegisterSearchProvider( | 228 void SettingsOverridesAPI::RegisterSearchProvider( |
| 207 const Extension* extension) const { | 229 const Extension* extension) const { |
| 208 DCHECK(url_service_); | 230 DCHECK(url_service_); |
| 209 DCHECK(extension); | 231 DCHECK(extension); |
| 210 const SettingsOverrides* settings = SettingsOverrides::Get(extension); | 232 const SettingsOverrides* settings = SettingsOverrides::Get(extension); |
| 211 DCHECK(settings); | 233 DCHECK(settings); |
| 212 DCHECK(settings->search_engine); | 234 DCHECK(settings->search_engine); |
| 213 scoped_ptr<AssociatedExtensionInfo> info(new AssociatedExtensionInfo); | 235 scoped_ptr<AssociatedExtensionInfo> info(new AssociatedExtensionInfo); |
| 214 info->extension_id = extension->id(); | 236 info->extension_id = extension->id(); |
| 215 info->wants_to_be_default_engine = settings->search_engine->is_default; | 237 info->wants_to_be_default_engine = settings->search_engine->is_default; |
| 216 info->install_time = | 238 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); |
| 217 ExtensionPrefs::Get(profile_)->GetInstallTime(extension->id()); | 239 info->install_time = prefs->GetInstallTime(extension->id()); |
| 218 TemplateURLData data = ConvertSearchProvider(*settings->search_engine); | 240 std::string install_parameter = prefs->GetInstallParam(extension->id()); |
| 241 TemplateURLData data = |
| 242 ConvertSearchProvider(*settings->search_engine, install_parameter); |
| 219 url_service_->AddExtensionControlledTURL(new TemplateURL(profile_, data), | 243 url_service_->AddExtensionControlledTURL(new TemplateURL(profile_, data), |
| 220 info.Pass()); | 244 info.Pass()); |
| 221 } | 245 } |
| 222 | 246 |
| 223 template <> | 247 template <> |
| 224 void BrowserContextKeyedAPIFactory< | 248 void BrowserContextKeyedAPIFactory< |
| 225 SettingsOverridesAPI>::DeclareFactoryDependencies() { | 249 SettingsOverridesAPI>::DeclareFactoryDependencies() { |
| 226 DependsOn(ExtensionPrefsFactory::GetInstance()); | 250 DependsOn(ExtensionPrefsFactory::GetInstance()); |
| 227 DependsOn(PreferenceAPI::GetFactoryInstance()); | 251 DependsOn(PreferenceAPI::GetFactoryInstance()); |
| 228 DependsOn(TemplateURLServiceFactory::GetInstance()); | 252 DependsOn(TemplateURLServiceFactory::GetInstance()); |
| 229 } | 253 } |
| 230 | 254 |
| 231 } // namespace extensions | 255 } // namespace extensions |
| OLD | NEW |