OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #include "chrome/browser/android/search_geolocation/search_geolocation_service.h
" |
| 6 |
| 7 #include "base/feature_list.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/android/search_geolocation/search_geolocation_disclosur
e_tab_helper.h" |
| 10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 13 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
| 14 #include "chrome/common/chrome_features.h" |
| 15 #include "chrome/common/pref_names.h" |
| 16 #include "components/content_settings/core/browser/content_settings_utils.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/content_settings/core/common/content_settings_types.h" |
| 19 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 20 #include "components/pref_registry/pref_registry_syncable.h" |
| 21 #include "components/prefs/pref_service.h" |
| 22 #include "components/search_engines/template_url.h" |
| 23 #include "components/search_engines/template_url_service.h" |
| 24 #include "url/gurl.h" |
| 25 #include "url/url_constants.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 const char kIsGoogleSearchEngineKey[] = "is_google_search_engine"; |
| 30 const char kCCTLDKey[] = "cctld"; |
| 31 const char kDSESettingKey[] = "dse_setting"; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 struct SearchGeolocationService::PrefValue { |
| 36 bool is_google_search_engine = false; |
| 37 url::Origin cctld; |
| 38 bool setting = false; |
| 39 }; |
| 40 |
| 41 // static |
| 42 SearchGeolocationService* |
| 43 SearchGeolocationService::Factory::GetForBrowserContext( |
| 44 content::BrowserContext* context) { |
| 45 return static_cast<SearchGeolocationService*>(GetInstance() |
| 46 ->GetServiceForBrowserContext(context, true)); |
| 47 } |
| 48 |
| 49 // static |
| 50 SearchGeolocationService::Factory* |
| 51 SearchGeolocationService::Factory::GetInstance() { |
| 52 return base::Singleton<SearchGeolocationService::Factory>::get(); |
| 53 } |
| 54 |
| 55 SearchGeolocationService::Factory::Factory() |
| 56 : BrowserContextKeyedServiceFactory( |
| 57 "SearchGeolocationService", |
| 58 BrowserContextDependencyManager::GetInstance()) {} |
| 59 |
| 60 SearchGeolocationService::Factory::~Factory() {} |
| 61 |
| 62 bool SearchGeolocationService::Factory::ServiceIsCreatedWithBrowserContext() |
| 63 const { |
| 64 return true; |
| 65 } |
| 66 |
| 67 KeyedService* SearchGeolocationService::Factory::BuildServiceInstanceFor( |
| 68 content::BrowserContext* context) const { |
| 69 return new SearchGeolocationService(Profile::FromBrowserContext(context)); |
| 70 } |
| 71 |
| 72 // static |
| 73 void SearchGeolocationService::RegisterProfilePrefs( |
| 74 user_prefs::PrefRegistrySyncable* registry) { |
| 75 registry->RegisterDictionaryPref(prefs::kGoogleDSEGeolocationSetting); |
| 76 } |
| 77 |
| 78 SearchGeolocationService::SearchGeolocationService(Profile* profile) |
| 79 : profile_(profile), |
| 80 pref_service_(profile_->GetPrefs()), |
| 81 host_content_settings_map_( |
| 82 HostContentSettingsMapFactory::GetForProfile(profile_)), |
| 83 template_url_service_( |
| 84 TemplateURLServiceFactory::GetForProfile(profile_)) { |
| 85 if (!UseConsistentSearchGeolocation()) |
| 86 return; |
| 87 |
| 88 template_url_service_->AddObserver(this); |
| 89 |
| 90 InitializeDSEGeolocationSettingIfNeeded(); |
| 91 |
| 92 // Make sure the setting is valid now. It's possible that the setting has |
| 93 // become invalid either by changes being made to enterprise policy, or while |
| 94 // the flag to enable consistent search geolocation was off. |
| 95 EnsureDSEGeolocationSettingIsValid(); |
| 96 } |
| 97 |
| 98 bool SearchGeolocationService::UseDSEGeolocationSetting( |
| 99 const url::Origin& requesting_origin) { |
| 100 if (!UseConsistentSearchGeolocation()) |
| 101 return false; |
| 102 |
| 103 if (profile_->IsOffTheRecord()) |
| 104 return false; |
| 105 |
| 106 if (requesting_origin.scheme() != url::kHttpsScheme) |
| 107 return false; |
| 108 |
| 109 return requesting_origin.IsSameOriginWith(GetDSECCTLD()); |
| 110 } |
| 111 |
| 112 bool SearchGeolocationService::GetDSEGeolocationSetting() { |
| 113 // Make sure the setting is valid, in case enterprise policy has changed. |
| 114 // TODO(benwells): Check if enterprise policy can change while Chrome is |
| 115 // running. If it can't this call is probably not needed. |
| 116 EnsureDSEGeolocationSettingIsValid(); |
| 117 |
| 118 return GetDSEGeolocationPref().setting; |
| 119 } |
| 120 |
| 121 void SearchGeolocationService::SetDSEGeolocationSetting(bool setting) { |
| 122 PrefValue pref = GetDSEGeolocationPref(); |
| 123 if (setting == pref.setting) |
| 124 return; |
| 125 |
| 126 // If the user cannot change their geolocation content setting (e.g. due to |
| 127 // enterprise policy), they also can't change this preference so just bail |
| 128 // out. |
| 129 if (!IsContentSettingUserSettable()) |
| 130 return; |
| 131 |
| 132 pref.setting = setting; |
| 133 SetDSEGeolocationPref(pref); |
| 134 |
| 135 ResetContentSetting(); |
| 136 } |
| 137 |
| 138 void SearchGeolocationService::Shutdown() { |
| 139 if (UseConsistentSearchGeolocation()) |
| 140 template_url_service_->RemoveObserver(this); |
| 141 } |
| 142 |
| 143 SearchGeolocationService::~SearchGeolocationService() {} |
| 144 |
| 145 void SearchGeolocationService::OnTemplateURLServiceChanged() { |
| 146 bool is_now_google_search_engine = IsGoogleSearchEngine(); |
| 147 PrefValue pref = GetDSEGeolocationPref(); |
| 148 ContentSetting content_setting = GetCurrentContentSetting(); |
| 149 |
| 150 if (is_now_google_search_engine) { |
| 151 if (content_setting == CONTENT_SETTING_BLOCK && pref.setting) { |
| 152 pref.setting = false; |
| 153 } else if (content_setting == CONTENT_SETTING_ALLOW && !pref.setting) { |
| 154 ResetContentSetting(); |
| 155 } |
| 156 } |
| 157 |
| 158 if (is_now_google_search_engine && !pref.is_google_search_engine && |
| 159 pref.setting) { |
| 160 SearchGeolocationDisclosureTabHelper::ResetDisclosure(profile_); |
| 161 } |
| 162 |
| 163 pref.is_google_search_engine = is_now_google_search_engine; |
| 164 pref.cctld = GetDSECCTLD(); |
| 165 SetDSEGeolocationPref(pref); |
| 166 } |
| 167 |
| 168 void SearchGeolocationService::InitializeDSEGeolocationSettingIfNeeded() { |
| 169 // Initialize the pref if it hasn't been initialized yet. |
| 170 if (!pref_service_->HasPrefPath(prefs::kGoogleDSEGeolocationSetting)) { |
| 171 ContentSetting content_setting = GetCurrentContentSetting(); |
| 172 |
| 173 PrefValue pref; |
| 174 pref.is_google_search_engine = IsGoogleSearchEngine(); |
| 175 pref.cctld = GetDSECCTLD(); |
| 176 pref.setting = content_setting != CONTENT_SETTING_BLOCK; |
| 177 SetDSEGeolocationPref(pref); |
| 178 |
| 179 SearchGeolocationDisclosureTabHelper::ResetDisclosure(profile_); |
| 180 } |
| 181 } |
| 182 |
| 183 void SearchGeolocationService::EnsureDSEGeolocationSettingIsValid() { |
| 184 PrefValue pref = GetDSEGeolocationPref(); |
| 185 ContentSetting content_setting = GetCurrentContentSetting(); |
| 186 bool new_setting = pref.setting; |
| 187 |
| 188 if (pref.setting && content_setting == CONTENT_SETTING_BLOCK) { |
| 189 new_setting = false; |
| 190 } else if (!pref.setting && content_setting == CONTENT_SETTING_ALLOW) { |
| 191 new_setting = true; |
| 192 } |
| 193 |
| 194 if (pref.setting != new_setting) { |
| 195 pref.setting = new_setting; |
| 196 SetDSEGeolocationPref(pref); |
| 197 } |
| 198 } |
| 199 |
| 200 bool SearchGeolocationService::IsGoogleSearchEngine() { |
| 201 const TemplateURL* template_url = |
| 202 template_url_service_->GetDefaultSearchProvider(); |
| 203 return template_url && |
| 204 template_url->HasGoogleBaseURLs(UIThreadSearchTermsData(profile_)); |
| 205 } |
| 206 |
| 207 url::Origin SearchGeolocationService::GetDSECCTLD() { |
| 208 if (!IsGoogleSearchEngine()) |
| 209 return url::Origin(); |
| 210 |
| 211 GURL origin_url(UIThreadSearchTermsData(profile_).GoogleBaseURLValue()); |
| 212 return url::Origin(origin_url); |
| 213 } |
| 214 |
| 215 SearchGeolocationService::PrefValue |
| 216 SearchGeolocationService::GetDSEGeolocationPref() { |
| 217 const base::DictionaryValue* dict = |
| 218 pref_service_->GetDictionary(prefs::kGoogleDSEGeolocationSetting); |
| 219 |
| 220 PrefValue pref; |
| 221 bool is_google_search_engine; |
| 222 std::string cctld_string; |
| 223 bool setting; |
| 224 if (dict->GetBoolean(kIsGoogleSearchEngineKey, &is_google_search_engine) && |
| 225 dict->GetString(kCCTLDKey, &cctld_string) && |
| 226 dict->GetBoolean(kDSESettingKey, &setting)) { |
| 227 pref.is_google_search_engine = is_google_search_engine; |
| 228 pref.cctld = url::Origin(GURL(cctld_string)); |
| 229 pref.setting = setting; |
| 230 } |
| 231 |
| 232 return pref; |
| 233 } |
| 234 |
| 235 void SearchGeolocationService::SetDSEGeolocationPref( |
| 236 const SearchGeolocationService::PrefValue& pref) { |
| 237 base::DictionaryValue dict; |
| 238 dict.SetBoolean(kIsGoogleSearchEngineKey, pref.is_google_search_engine); |
| 239 dict.SetString(kCCTLDKey, pref.cctld.Serialize()); |
| 240 dict.SetBoolean(kDSESettingKey, pref.setting); |
| 241 pref_service_->Set(prefs::kGoogleDSEGeolocationSetting, dict); |
| 242 } |
| 243 |
| 244 ContentSetting SearchGeolocationService::GetCurrentContentSetting() { |
| 245 url::Origin origin = GetDSECCTLD(); |
| 246 return host_content_settings_map_->GetContentSetting( |
| 247 origin.GetURL(), origin.GetURL(), CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 248 std::string()); |
| 249 } |
| 250 |
| 251 void SearchGeolocationService::ResetContentSetting() { |
| 252 url::Origin origin = GetDSECCTLD(); |
| 253 host_content_settings_map_->SetContentSettingDefaultScope( |
| 254 origin.GetURL(), origin.GetURL(), CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 255 std::string(), CONTENT_SETTING_DEFAULT); |
| 256 } |
| 257 |
| 258 bool SearchGeolocationService::IsContentSettingUserSettable() { |
| 259 content_settings::SettingInfo info; |
| 260 url::Origin origin = GetDSECCTLD(); |
| 261 std::unique_ptr<base::Value> value = |
| 262 host_content_settings_map_->GetWebsiteSetting( |
| 263 origin.GetURL(), origin.GetURL(), CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| 264 std::string(), &info); |
| 265 return info.source == content_settings::SETTING_SOURCE_USER; |
| 266 } |
| 267 |
| 268 bool SearchGeolocationService::UseConsistentSearchGeolocation() { |
| 269 return base::FeatureList::IsEnabled(features::kConsistentOmniboxGeolocation); |
| 270 } |
OLD | NEW |