Chromium Code Reviews| 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 "chrome/browser/android/ntp/popular_sites.h" | 5 #include "chrome/browser/android/ntp/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" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/profiles/profile.h" | |
| 22 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 23 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
| 24 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 25 #include "components/google/core/browser/google_util.h" | 23 #include "components/google/core/browser/google_util.h" |
| 26 #include "components/pref_registry/pref_registry_syncable.h" | 24 #include "components/pref_registry/pref_registry_syncable.h" |
| 27 #include "components/prefs/pref_service.h" | 25 #include "components/prefs/pref_service.h" |
| 28 #include "components/search_engines/search_engine_type.h" | 26 #include "components/search_engines/search_engine_type.h" |
| 29 #include "components/search_engines/template_url_prepopulate_data.h" | 27 #include "components/search_engines/template_url_prepopulate_data.h" |
| 30 #include "components/search_engines/template_url_service.h" | 28 #include "components/search_engines/template_url_service.h" |
| 31 #include "components/variations/service/variations_service.h" | 29 #include "components/variations/service/variations_service.h" |
| 32 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 33 | 31 |
| 34 using content::BrowserThread; | 32 using content::BrowserThread; |
| 35 | 33 |
| 36 namespace { | 34 namespace { |
| 37 | 35 |
| 38 const char kPopularSitesURLFormat[] = | 36 const char kPopularSitesURLFormat[] = |
| 39 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; | 37 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; |
| 40 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 38 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 41 const char kPopularSitesDefaultVersion[] = "5"; | 39 const char kPopularSitesDefaultVersion[] = "5"; |
| 42 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; | 40 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; |
| 43 const int kPopularSitesRedownloadIntervalHours = 24; | 41 const int kPopularSitesRedownloadIntervalHours = 24; |
| 44 | 42 |
| 45 const char kPopularSitesLastDownloadPref[] = "popular_sites_last_download"; | 43 const char kPopularSitesLastDownloadPref[] = "popular_sites_last_download"; |
| 46 const char kPopularSitesCountryPref[] = "popular_sites_country"; | 44 const char kPopularSitesCountryPref[] = "popular_sites_country"; |
| 47 const char kPopularSitesVersionPref[] = "popular_sites_version"; | 45 const char kPopularSitesVersionPref[] = "popular_sites_version"; |
| 48 | 46 |
| 49 // Extract the country from the default search engine if the default search | 47 // Extract the country from the default search engine if the default search |
| 50 // engine is Google. | 48 // engine is Google. |
| 51 std::string GetDefaultSearchEngineCountryCode(Profile* profile) { | 49 std::string GetDefaultSearchEngineCountryCode( |
| 52 DCHECK(profile); | 50 const TemplateURLService* template_url_service) { |
| 51 DCHECK(template_url_service); | |
| 53 | 52 |
| 54 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 53 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 55 if (!cmd_line->HasSwitch(switches::kEnableNTPSearchEngineCountryDetection)) | 54 if (!cmd_line->HasSwitch(switches::kEnableNTPSearchEngineCountryDetection)) |
| 56 return std::string(); | 55 return std::string(); |
| 57 | 56 |
| 58 const TemplateURLService* template_url_service = | |
| 59 TemplateURLServiceFactory::GetForProfile(profile); | |
| 60 DCHECK(template_url_service); | |
| 61 | |
| 62 const TemplateURL* default_provider = | 57 const TemplateURL* default_provider = |
| 63 template_url_service->GetDefaultSearchProvider(); | 58 template_url_service->GetDefaultSearchProvider(); |
| 64 // It's possible to not have a default provider in the case that the default | 59 // It's possible to not have a default provider in the case that the default |
| 65 // search engine is defined by policy. | 60 // search engine is defined by policy. |
| 66 if (default_provider) { | 61 if (default_provider) { |
| 67 bool is_google_search_engine = | 62 bool is_google_search_engine = |
| 68 TemplateURLPrepopulateData::GetEngineType( | 63 TemplateURLPrepopulateData::GetEngineType( |
| 69 *default_provider, template_url_service->search_terms_data()) == | 64 *default_provider, template_url_service->search_terms_data()) == |
| 70 SearchEngineType::SEARCH_ENGINE_GOOGLE; | 65 SearchEngineType::SEARCH_ENGINE_GOOGLE; |
| 71 | 66 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 86 g_browser_process->variations_service(); | 81 g_browser_process->variations_service(); |
| 87 if (variations_service) | 82 if (variations_service) |
| 88 return variations_service->GetStoredPermanentCountry(); | 83 return variations_service->GetStoredPermanentCountry(); |
| 89 return std::string(); | 84 return std::string(); |
| 90 } | 85 } |
| 91 | 86 |
| 92 // Find out the country code of the user by using the Google country code if | 87 // Find out the country code of the user by using the Google country code if |
| 93 // Google is the default search engine set. If Google is not the default search | 88 // Google is the default search engine set. If Google is not the default search |
| 94 // engine use the country provided by VariationsService. Fallback to a default | 89 // engine use the country provided by VariationsService. Fallback to a default |
| 95 // if we can't make an educated guess. | 90 // if we can't make an educated guess. |
| 96 std::string GetCountryToUse(Profile* profile, | 91 std::string GetCountryToUse(const TemplateURLService* template_url_service, |
| 97 const std::string& override_country) { | 92 const std::string& override_country) { |
| 98 if (!override_country.empty()) | 93 if (!override_country.empty()) |
| 99 return override_country; | 94 return override_country; |
| 100 | 95 |
| 101 std::string country_code = GetDefaultSearchEngineCountryCode(profile); | 96 std::string country_code = GetDefaultSearchEngineCountryCode( |
| 97 template_url_service); | |
| 102 | 98 |
| 103 if (country_code.empty()) | 99 if (country_code.empty()) |
| 104 country_code = GetVariationsServiceCountry(); | 100 country_code = GetVariationsServiceCountry(); |
| 105 | 101 |
| 106 if (country_code.empty()) | 102 if (country_code.empty()) |
| 107 country_code = kPopularSitesDefaultCountryCode; | 103 country_code = kPopularSitesDefaultCountryCode; |
| 108 | 104 |
| 109 return base::ToUpperASCII(country_code); | 105 return base::ToUpperASCII(country_code); |
| 110 } | 106 } |
| 111 | 107 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 : title(title), | 168 : title(title), |
| 173 url(url), | 169 url(url), |
| 174 favicon_url(favicon_url), | 170 favicon_url(favicon_url), |
| 175 large_icon_url(large_icon_url), | 171 large_icon_url(large_icon_url), |
| 176 thumbnail_url(thumbnail_url) {} | 172 thumbnail_url(thumbnail_url) {} |
| 177 | 173 |
| 178 PopularSites::Site::Site(const Site& other) = default; | 174 PopularSites::Site::Site(const Site& other) = default; |
| 179 | 175 |
| 180 PopularSites::Site::~Site() {} | 176 PopularSites::Site::~Site() {} |
| 181 | 177 |
| 182 PopularSites::PopularSites(Profile* profile, | 178 PopularSites::PopularSites(PrefService* prefs, |
| 179 const TemplateURLService* template_url_service, | |
| 180 net::URLRequestContextGetter* download_context, | |
| 183 const std::string& override_country, | 181 const std::string& override_country, |
| 184 const std::string& override_version, | 182 const std::string& override_version, |
| 185 bool force_download, | 183 bool force_download, |
| 186 const FinishedCallback& callback) | 184 const FinishedCallback& callback) |
| 187 : PopularSites(profile, | 185 : PopularSites(prefs, |
| 188 GetCountryToUse(profile, override_country), | 186 template_url_service, |
| 187 download_context, | |
| 188 GetCountryToUse(template_url_service, override_country), | |
| 189 GetVersionToUse(override_version), | 189 GetVersionToUse(override_version), |
| 190 GURL(), | 190 GURL(), |
| 191 force_download, | 191 force_download, |
| 192 callback) {} | 192 callback) {} |
| 193 | 193 |
| 194 PopularSites::PopularSites(Profile* profile, | 194 PopularSites::PopularSites(PrefService* prefs, |
| 195 const TemplateURLService* template_url_service, | |
| 196 net::URLRequestContextGetter* download_context, | |
| 195 const GURL& url, | 197 const GURL& url, |
| 196 const FinishedCallback& callback) | 198 const FinishedCallback& callback) |
| 197 : PopularSites(profile, std::string(), std::string(), url, true, callback) { | 199 : PopularSites(prefs, |
| 200 template_url_service, | |
| 201 download_context, | |
| 202 std::string(), | |
| 203 std::string(), | |
| 204 url, | |
| 205 true, | |
| 206 callback) { | |
| 198 } | 207 } |
|
Bernhard Bauer
2016/04/28 09:24:10
Can you move this to the previous line, so it's co
sfiera
2016/04/28 12:07:46
Done.
| |
| 199 | 208 |
| 200 PopularSites::~PopularSites() {} | 209 PopularSites::~PopularSites() {} |
| 201 | 210 |
| 202 std::string PopularSites::GetCountry() const { | 211 std::string PopularSites::GetCountry() const { |
| 203 return profile_->GetPrefs()->GetString(kPopularSitesCountryPref); | 212 return prefs_->GetString(kPopularSitesCountryPref); |
| 204 } | 213 } |
| 205 | 214 |
| 206 std::string PopularSites::GetVersion() const { | 215 std::string PopularSites::GetVersion() const { |
| 207 return profile_->GetPrefs()->GetString(kPopularSitesVersionPref); | 216 return prefs_->GetString(kPopularSitesVersionPref); |
| 208 } | 217 } |
| 209 | 218 |
| 210 // static | 219 // static |
| 211 void PopularSites::RegisterProfilePrefs( | 220 void PopularSites::RegisterProfilePrefs( |
| 212 user_prefs::PrefRegistrySyncable* user_prefs) { | 221 user_prefs::PrefRegistrySyncable* user_prefs) { |
| 213 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 222 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
| 214 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); | 223 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); |
| 215 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); | 224 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); |
| 216 } | 225 } |
| 217 | 226 |
| 218 PopularSites::PopularSites(Profile* profile, | 227 PopularSites::PopularSites(PrefService* prefs, |
| 228 const TemplateURLService* template_url_service, | |
| 229 net::URLRequestContextGetter* download_context, | |
| 219 const std::string& country, | 230 const std::string& country, |
| 220 const std::string& version, | 231 const std::string& version, |
| 221 const GURL& override_url, | 232 const GURL& override_url, |
| 222 bool force_download, | 233 bool force_download, |
| 223 const FinishedCallback& callback) | 234 const FinishedCallback& callback) |
| 224 : callback_(callback), | 235 : callback_(callback), |
| 225 pending_country_(country), | 236 pending_country_(country), |
| 226 pending_version_(version), | 237 pending_version_(version), |
| 227 local_path_(GetPopularSitesPath()), | 238 local_path_(GetPopularSitesPath()), |
| 228 profile_(profile), | 239 prefs_(prefs), |
| 240 template_url_service_(template_url_service), | |
| 241 download_context_(download_context), | |
| 229 weak_ptr_factory_(this) { | 242 weak_ptr_factory_(this) { |
| 230 const base::Time last_download_time = base::Time::FromInternalValue( | 243 const base::Time last_download_time = base::Time::FromInternalValue( |
| 231 profile_->GetPrefs()->GetInt64(kPopularSitesLastDownloadPref)); | 244 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
| 232 const base::TimeDelta time_since_last_download = | 245 const base::TimeDelta time_since_last_download = |
| 233 base::Time::Now() - last_download_time; | 246 base::Time::Now() - last_download_time; |
| 234 const base::TimeDelta redownload_interval = | 247 const base::TimeDelta redownload_interval = |
| 235 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 248 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
| 236 const bool download_time_is_future = base::Time::Now() < last_download_time; | 249 const bool download_time_is_future = base::Time::Now() < last_download_time; |
| 237 | 250 |
| 238 const bool should_redownload_if_exists = | 251 const bool should_redownload_if_exists = |
| 239 force_download || download_time_is_future || | 252 force_download || download_time_is_future || |
| 240 (time_since_last_download > redownload_interval); | 253 (time_since_last_download > redownload_interval); |
| 241 // TODO(treib): If country/version don't match what we have, we should | 254 // TODO(treib): If country/version don't match what we have, we should |
| 242 // probably also redownload? | 255 // probably also redownload? |
| 243 | 256 |
| 244 FetchPopularSites( | 257 FetchPopularSites( |
| 245 override_url.is_valid() ? override_url : GetPopularSitesURL(), | 258 override_url.is_valid() ? override_url : GetPopularSitesURL(), |
| 246 should_redownload_if_exists, false /* is_fallback */); | 259 should_redownload_if_exists, false /* is_fallback */); |
| 247 } | 260 } |
| 248 | 261 |
| 249 GURL PopularSites::GetPopularSitesURL() const { | 262 GURL PopularSites::GetPopularSitesURL() const { |
| 250 return GURL(base::StringPrintf(kPopularSitesURLFormat, | 263 return GURL(base::StringPrintf(kPopularSitesURLFormat, |
| 251 pending_country_.c_str(), | 264 pending_country_.c_str(), |
| 252 pending_version_.c_str())); | 265 pending_version_.c_str())); |
| 253 } | 266 } |
| 254 | 267 |
| 255 void PopularSites::FetchPopularSites(const GURL& url, | 268 void PopularSites::FetchPopularSites(const GURL& url, |
| 256 bool force_download, | 269 bool force_download, |
| 257 bool is_fallback) { | 270 bool is_fallback) { |
| 258 downloader_.reset(new FileDownloader( | 271 downloader_.reset(new FileDownloader( |
| 259 url, local_path_, force_download, profile_->GetRequestContext(), | 272 url, local_path_, force_download, download_context_, |
| 260 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), | 273 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), |
| 261 is_fallback))); | 274 is_fallback))); |
| 262 } | 275 } |
| 263 | 276 |
| 264 void PopularSites::OnDownloadDone(bool is_fallback, | 277 void PopularSites::OnDownloadDone(bool is_fallback, |
| 265 FileDownloader::Result result) { | 278 FileDownloader::Result result) { |
| 266 downloader_.reset(); | 279 downloader_.reset(); |
| 267 PrefService* prefs = profile_->GetPrefs(); | |
| 268 switch (result) { | 280 switch (result) { |
| 269 case FileDownloader::DOWNLOADED: | 281 case FileDownloader::DOWNLOADED: |
| 270 prefs->SetInt64(kPopularSitesLastDownloadPref, | 282 prefs_->SetInt64(kPopularSitesLastDownloadPref, |
| 271 base::Time::Now().ToInternalValue()); | 283 base::Time::Now().ToInternalValue()); |
| 272 prefs->SetString(kPopularSitesCountryPref, pending_country_); | 284 prefs_->SetString(kPopularSitesCountryPref, pending_country_); |
| 273 prefs->SetString(kPopularSitesVersionPref, pending_version_); | 285 prefs_->SetString(kPopularSitesVersionPref, pending_version_); |
| 274 ParseSiteList(local_path_); | 286 ParseSiteList(local_path_); |
| 275 break; | 287 break; |
| 276 case FileDownloader::EXISTS: | 288 case FileDownloader::EXISTS: |
| 277 ParseSiteList(local_path_); | 289 ParseSiteList(local_path_); |
| 278 break; | 290 break; |
| 279 case FileDownloader::FAILED: | 291 case FileDownloader::FAILED: |
| 280 if (!is_fallback) { | 292 if (!is_fallback) { |
| 281 DLOG(WARNING) << "Download country site list failed"; | 293 DLOG(WARNING) << "Download country site list failed"; |
| 282 pending_country_ = kPopularSitesDefaultCountryCode; | 294 pending_country_ = kPopularSitesDefaultCountryCode; |
| 283 pending_version_ = kPopularSitesDefaultVersion; | 295 pending_version_ = kPopularSitesDefaultVersion; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 303 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); | 315 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); |
| 304 } | 316 } |
| 305 | 317 |
| 306 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { | 318 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { |
| 307 if (sites) | 319 if (sites) |
| 308 sites_.swap(*sites); | 320 sites_.swap(*sites); |
| 309 else | 321 else |
| 310 sites_.clear(); | 322 sites_.clear(); |
| 311 callback_.Run(!!sites); | 323 callback_.Run(!!sites); |
| 312 } | 324 } |
| OLD | NEW |