| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/ntp_tiles/popular_sites.h" | 5 #include "components/ntp_tiles/popular_sites.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 std::string GetVariationCountry() { | 94 std::string GetVariationCountry() { |
| 95 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, | 95 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, |
| 96 "country"); | 96 "country"); |
| 97 } | 97 } |
| 98 | 98 |
| 99 std::string GetVariationVersion() { | 99 std::string GetVariationVersion() { |
| 100 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, | 100 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, |
| 101 "version"); | 101 "version"); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // Determine the country code to use. In order of precedence: | |
| 105 // - The explicit "override country" pref set by the user. | |
| 106 // - The country code from the field trial config (variation parameter). | |
| 107 // - The Google country code if Google is the default search engine (and the | |
| 108 // "--enable-ntp-search-engine-country-detection" switch is present). | |
| 109 // - The country provided by the VariationsService. | |
| 110 // - A default fallback. | |
| 111 std::string GetCountryToUse(const PrefService* prefs, | |
| 112 const TemplateURLService* template_url_service, | |
| 113 VariationsService* variations_service) { | |
| 114 std::string country_code = | |
| 115 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); | |
| 116 | |
| 117 if (country_code.empty()) | |
| 118 country_code = GetVariationCountry(); | |
| 119 | |
| 120 if (country_code.empty()) | |
| 121 country_code = GetDefaultSearchEngineCountryCode(template_url_service); | |
| 122 | |
| 123 if (country_code.empty() && variations_service) | |
| 124 country_code = variations_service->GetStoredPermanentCountry(); | |
| 125 | |
| 126 #if defined(OS_IOS) | |
| 127 if (country_code.empty()) | |
| 128 country_code = GetDeviceCountryCode(); | |
| 129 #endif | |
| 130 | |
| 131 if (country_code.empty()) | |
| 132 country_code = kPopularSitesDefaultCountryCode; | |
| 133 | |
| 134 return base::ToUpperASCII(country_code); | |
| 135 } | |
| 136 | |
| 137 // Determine the version to use. In order of precedence: | |
| 138 // - The explicit "override version" pref set by the user. | |
| 139 // - The version from the field trial config (variation parameter). | |
| 140 // - A default fallback. | |
| 141 std::string GetVersionToUse(const PrefService* prefs) { | |
| 142 std::string version = | |
| 143 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); | |
| 144 | |
| 145 if (version.empty()) | |
| 146 version = GetVariationVersion(); | |
| 147 | |
| 148 if (version.empty()) | |
| 149 version = kPopularSitesDefaultVersion; | |
| 150 | |
| 151 return version; | |
| 152 } | |
| 153 | |
| 154 // Must run on the blocking thread pool. | 104 // Must run on the blocking thread pool. |
| 155 bool WriteJsonToFile(const base::FilePath& local_path, | 105 bool WriteJsonToFile(const base::FilePath& local_path, |
| 156 const base::Value* json) { | 106 const base::Value* json) { |
| 157 std::string json_string; | 107 std::string json_string; |
| 158 return base::JSONWriter::Write(*json, &json_string) && | 108 return base::JSONWriter::Write(*json, &json_string) && |
| 159 base::ImportantFileWriter::WriteFileAtomically(local_path, | 109 base::ImportantFileWriter::WriteFileAtomically(local_path, |
| 160 json_string); | 110 json_string); |
| 161 } | 111 } |
| 162 | 112 |
| 163 } // namespace | 113 } // namespace |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 callback_ = callback; | 156 callback_ = callback; |
| 207 | 157 |
| 208 const base::Time last_download_time = base::Time::FromInternalValue( | 158 const base::Time last_download_time = base::Time::FromInternalValue( |
| 209 prefs_->GetInt64(kPopularSitesLastDownloadPref)); | 159 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
| 210 const base::TimeDelta time_since_last_download = | 160 const base::TimeDelta time_since_last_download = |
| 211 base::Time::Now() - last_download_time; | 161 base::Time::Now() - last_download_time; |
| 212 const base::TimeDelta redownload_interval = | 162 const base::TimeDelta redownload_interval = |
| 213 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 163 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
| 214 const bool download_time_is_future = base::Time::Now() < last_download_time; | 164 const bool download_time_is_future = base::Time::Now() < last_download_time; |
| 215 | 165 |
| 216 const std::string country = | 166 pending_url_ = GetURLToUse(); |
| 217 GetCountryToUse(prefs_, template_url_service_, variations_); | |
| 218 const std::string version = GetVersionToUse(prefs_); | |
| 219 | |
| 220 const GURL override_url = | |
| 221 GURL(prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); | |
| 222 pending_url_ = override_url.is_valid() ? override_url | |
| 223 : GetPopularSitesURL(country, version); | |
| 224 const bool url_changed = | 167 const bool url_changed = |
| 225 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); | 168 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); |
| 226 | 169 |
| 227 // No valid path to save to. Immediately post failure. | 170 // No valid path to save to. Immediately post failure. |
| 228 if (local_path_.empty()) { | 171 if (local_path_.empty()) { |
| 229 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 172 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 230 base::Bind(callback_, false)); | 173 base::Bind(callback_, false)); |
| 231 return; | 174 return; |
| 232 } | 175 } |
| 233 | 176 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 244 blocking_runner_.get(), FROM_HERE, | 187 blocking_runner_.get(), FROM_HERE, |
| 245 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), | 188 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), |
| 246 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), | 189 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), |
| 247 base::Passed(std::move(file_data)))); | 190 base::Passed(std::move(file_data)))); |
| 248 } | 191 } |
| 249 | 192 |
| 250 GURL PopularSites::LastURL() const { | 193 GURL PopularSites::LastURL() const { |
| 251 return GURL(prefs_->GetString(kPopularSitesURLPref)); | 194 return GURL(prefs_->GetString(kPopularSitesURLPref)); |
| 252 } | 195 } |
| 253 | 196 |
| 197 GURL PopularSites::GetURLToUse() { |
| 198 const std::string country = GetCountryToUse(); |
| 199 const std::string version = GetVersionToUse(); |
| 200 |
| 201 const GURL override_url = |
| 202 GURL(prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); |
| 203 return override_url.is_valid() ? override_url |
| 204 : GetPopularSitesURL(country, version); |
| 205 } |
| 206 |
| 207 // Determine the country code to use. In order of precedence: |
| 208 // - The explicit "override country" pref set by the user. |
| 209 // - The country code from the field trial config (variation parameter). |
| 210 // - The Google country code if Google is the default search engine (and the |
| 211 // "--enable-ntp-search-engine-country-detection" switch is present). |
| 212 // - The country provided by the VariationsService. |
| 213 // - A default fallback. |
| 214 std::string PopularSites::GetCountryToUse() { |
| 215 std::string country_code = |
| 216 prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); |
| 217 |
| 218 if (country_code.empty()) |
| 219 country_code = GetVariationCountry(); |
| 220 |
| 221 if (country_code.empty()) |
| 222 country_code = GetDefaultSearchEngineCountryCode(template_url_service_); |
| 223 |
| 224 if (country_code.empty() && variations_) |
| 225 country_code = variations_->GetStoredPermanentCountry(); |
| 226 |
| 227 #if defined(OS_IOS) |
| 228 if (country_code.empty()) |
| 229 country_code = GetDeviceCountryCode(); |
| 230 #endif |
| 231 |
| 232 if (country_code.empty()) |
| 233 country_code = kPopularSitesDefaultCountryCode; |
| 234 |
| 235 return base::ToUpperASCII(country_code); |
| 236 } |
| 237 |
| 238 // Determine the version to use. In order of precedence: |
| 239 // - The explicit "override version" pref set by the user. |
| 240 // - The version from the field trial config (variation parameter). |
| 241 // - A default fallback. |
| 242 std::string PopularSites::GetVersionToUse() { |
| 243 std::string version = |
| 244 prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
| 245 |
| 246 if (version.empty()) |
| 247 version = GetVariationVersion(); |
| 248 |
| 249 if (version.empty()) |
| 250 version = kPopularSitesDefaultVersion; |
| 251 |
| 252 return version; |
| 253 } |
| 254 |
| 254 // static | 255 // static |
| 255 void PopularSites::RegisterProfilePrefs( | 256 void PopularSites::RegisterProfilePrefs( |
| 256 user_prefs::PrefRegistrySyncable* user_prefs) { | 257 user_prefs::PrefRegistrySyncable* user_prefs) { |
| 257 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, | 258 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| 258 std::string()); | 259 std::string()); |
| 259 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 260 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, |
| 260 std::string()); | 261 std::string()); |
| 261 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, | 262 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, |
| 262 std::string()); | 263 std::string()); |
| 263 | 264 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, | 380 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, |
| 380 kPopularSitesDefaultVersion); | 381 kPopularSitesDefaultVersion); |
| 381 FetchPopularSites(); | 382 FetchPopularSites(); |
| 382 } else { | 383 } else { |
| 383 DLOG(WARNING) << "Download fallback site list failed"; | 384 DLOG(WARNING) << "Download fallback site list failed"; |
| 384 callback_.Run(false); | 385 callback_.Run(false); |
| 385 } | 386 } |
| 386 } | 387 } |
| 387 | 388 |
| 388 } // namespace ntp_tiles | 389 } // namespace ntp_tiles |
| OLD | NEW |