| OLD | NEW |
| 1 // Copyright 2013 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/net/file_downloader.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/search_engines/template_url_service_factory.h" | 22 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 24 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
| 25 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 26 #include "components/google/core/browser/google_util.h" | 25 #include "components/google/core/browser/google_util.h" |
| 27 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
| 28 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
| 29 #include "components/search_engines/search_engine_type.h" | 28 #include "components/search_engines/search_engine_type.h" |
| 30 #include "components/search_engines/template_url_prepopulate_data.h" | 29 #include "components/search_engines/template_url_prepopulate_data.h" |
| 31 #include "components/search_engines/template_url_service.h" | 30 #include "components/search_engines/template_url_service.h" |
| 32 #include "components/variations/service/variations_service.h" | 31 #include "components/variations/service/variations_service.h" |
| 33 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 34 | 33 |
| 35 using content::BrowserThread; | 34 using content::BrowserThread; |
| 36 | 35 |
| 37 namespace { | 36 namespace { |
| 38 | 37 |
| 39 const char kPopularSitesURLFormat[] = "https://www.gstatic.com/chrome/ntp/%s"; | 38 const char kPopularSitesURLFormat[] = |
| 40 const char kPopularSitesServerFilenameFormat[] = "suggested_sites_%s_%s.json"; | 39 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; |
| 41 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 40 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 42 const char kPopularSitesDefaultVersion[] = "5"; | 41 const char kPopularSitesDefaultVersion[] = "5"; |
| 43 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; | 42 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; |
| 44 const int kPopularSitesRedownloadIntervalHours = 24; | 43 const int kPopularSitesRedownloadIntervalHours = 24; |
| 45 | 44 |
| 46 const char kPopularSitesLastDownloadPref[] = "popular_sites_last_download"; | 45 const char kPopularSitesLastDownloadPref[] = "popular_sites_last_download"; |
| 46 const char kPopularSitesCountryPref[] = "popular_sites_country"; |
| 47 const char kPopularSitesVersionPref[] = "popular_sites_version"; |
| 47 | 48 |
| 48 // Extract the country from the default search engine if the default search | 49 // Extract the country from the default search engine if the default search |
| 49 // engine is Google. | 50 // engine is Google. |
| 50 std::string GetDefaultSearchEngineCountryCode(Profile* profile) { | 51 std::string GetDefaultSearchEngineCountryCode(Profile* profile) { |
| 51 DCHECK(profile); | 52 DCHECK(profile); |
| 52 | 53 |
| 53 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 54 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 54 if (!cmd_line->HasSwitch(switches::kEnableNTPSearchEngineCountryDetection)) | 55 if (!cmd_line->HasSwitch(switches::kEnableNTPSearchEngineCountryDetection)) |
| 55 return std::string(); | 56 return std::string(); |
| 56 | 57 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 85 g_browser_process->variations_service(); | 86 g_browser_process->variations_service(); |
| 86 if (variations_service) | 87 if (variations_service) |
| 87 return variations_service->GetStoredPermanentCountry(); | 88 return variations_service->GetStoredPermanentCountry(); |
| 88 return std::string(); | 89 return std::string(); |
| 89 } | 90 } |
| 90 | 91 |
| 91 // Find out the country code of the user by using the Google country code if | 92 // Find out the country code of the user by using the Google country code if |
| 92 // Google is the default search engine set. If Google is not the default search | 93 // Google is the default search engine set. If Google is not the default search |
| 93 // engine use the country provided by VariationsService. Fallback to a default | 94 // engine use the country provided by VariationsService. Fallback to a default |
| 94 // if we can't make an educated guess. | 95 // if we can't make an educated guess. |
| 95 std::string GetCountryCode(Profile* profile) { | 96 std::string GetCountryToUse(Profile* profile, |
| 97 const std::string& override_country) { |
| 98 if (!override_country.empty()) |
| 99 return override_country; |
| 100 |
| 96 std::string country_code = GetDefaultSearchEngineCountryCode(profile); | 101 std::string country_code = GetDefaultSearchEngineCountryCode(profile); |
| 97 | 102 |
| 98 if (country_code.empty()) | 103 if (country_code.empty()) |
| 99 country_code = GetVariationsServiceCountry(); | 104 country_code = GetVariationsServiceCountry(); |
| 100 | 105 |
| 101 if (country_code.empty()) | 106 if (country_code.empty()) |
| 102 country_code = kPopularSitesDefaultCountryCode; | 107 country_code = kPopularSitesDefaultCountryCode; |
| 103 | 108 |
| 104 return base::ToUpperASCII(country_code); | 109 return base::ToUpperASCII(country_code); |
| 105 } | 110 } |
| 106 | 111 |
| 107 std::string GetPopularSitesServerFilename( | 112 std::string GetVersionToUse(const std::string& override_version) { |
| 108 Profile* profile, | 113 if (!override_version.empty()) |
| 109 const std::string& override_country, | 114 return override_version; |
| 110 const std::string& override_version) { | 115 return kPopularSitesDefaultVersion; |
| 111 std::string country = | |
| 112 !override_country.empty() ? override_country : GetCountryCode(profile); | |
| 113 std::string version = !override_version.empty() ? override_version | |
| 114 : kPopularSitesDefaultVersion; | |
| 115 return base::StringPrintf(kPopularSitesServerFilenameFormat, | |
| 116 country.c_str(), version.c_str()); | |
| 117 } | |
| 118 | |
| 119 GURL GetPopularSitesURL(Profile* profile, | |
| 120 const std::string& override_country, | |
| 121 const std::string& override_version) { | |
| 122 return GURL(base::StringPrintf( | |
| 123 kPopularSitesURLFormat, | |
| 124 GetPopularSitesServerFilename(profile, override_country, override_version) | |
| 125 .c_str())); | |
| 126 } | |
| 127 | |
| 128 GURL GetPopularSitesFallbackURL(Profile* profile) { | |
| 129 return GetPopularSitesURL(profile, kPopularSitesDefaultCountryCode, | |
| 130 kPopularSitesDefaultVersion); | |
| 131 } | 116 } |
| 132 | 117 |
| 133 base::FilePath GetPopularSitesPath() { | 118 base::FilePath GetPopularSitesPath() { |
| 134 base::FilePath dir; | 119 base::FilePath dir; |
| 135 PathService::Get(chrome::DIR_USER_DATA, &dir); | 120 PathService::Get(chrome::DIR_USER_DATA, &dir); |
| 136 return dir.AppendASCII(kPopularSitesLocalFilename); | 121 return dir.AppendASCII(kPopularSitesLocalFilename); |
| 137 } | 122 } |
| 138 | 123 |
| 139 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( | 124 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( |
| 140 const base::FilePath& path) { | 125 const base::FilePath& path) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 thumbnail_url(thumbnail_url) {} | 176 thumbnail_url(thumbnail_url) {} |
| 192 | 177 |
| 193 PopularSites::Site::~Site() {} | 178 PopularSites::Site::~Site() {} |
| 194 | 179 |
| 195 PopularSites::PopularSites(Profile* profile, | 180 PopularSites::PopularSites(Profile* profile, |
| 196 const std::string& override_country, | 181 const std::string& override_country, |
| 197 const std::string& override_version, | 182 const std::string& override_version, |
| 198 bool force_download, | 183 bool force_download, |
| 199 const FinishedCallback& callback) | 184 const FinishedCallback& callback) |
| 200 : PopularSites(profile, | 185 : PopularSites(profile, |
| 201 GetPopularSitesURL(profile, override_country, | 186 GetCountryToUse(profile, override_country), |
| 202 override_version), | 187 GetVersionToUse(override_version), |
| 188 GURL(), |
| 203 force_download, | 189 force_download, |
| 204 callback) {} | 190 callback) {} |
| 205 | 191 |
| 206 PopularSites::PopularSites(Profile* profile, | 192 PopularSites::PopularSites(Profile* profile, |
| 207 const GURL& url, | 193 const GURL& url, |
| 208 const FinishedCallback& callback) | 194 const FinishedCallback& callback) |
| 209 : PopularSites(profile, url, true, callback) {} | 195 : PopularSites(profile, std::string(), std::string(), url, true, callback) { |
| 196 } |
| 210 | 197 |
| 211 PopularSites::~PopularSites() {} | 198 PopularSites::~PopularSites() {} |
| 212 | 199 |
| 200 std::string PopularSites::GetCountry() const { |
| 201 return profile_->GetPrefs()->GetString(kPopularSitesCountryPref); |
| 202 } |
| 203 |
| 204 std::string PopularSites::GetVersion() const { |
| 205 return profile_->GetPrefs()->GetString(kPopularSitesVersionPref); |
| 206 } |
| 207 |
| 213 // static | 208 // static |
| 214 void PopularSites::RegisterProfilePrefs( | 209 void PopularSites::RegisterProfilePrefs( |
| 215 user_prefs::PrefRegistrySyncable* user_prefs) { | 210 user_prefs::PrefRegistrySyncable* user_prefs) { |
| 216 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 211 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
| 212 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); |
| 213 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); |
| 217 } | 214 } |
| 218 | 215 |
| 219 PopularSites::PopularSites(Profile* profile, | 216 PopularSites::PopularSites(Profile* profile, |
| 220 const GURL& url, | 217 const std::string& country, |
| 218 const std::string& version, |
| 219 const GURL& override_url, |
| 221 bool force_download, | 220 bool force_download, |
| 222 const FinishedCallback& callback) | 221 const FinishedCallback& callback) |
| 223 : callback_(callback), | 222 : callback_(callback), |
| 224 popular_sites_local_path_(GetPopularSitesPath()), | 223 pending_country_(country), |
| 224 pending_version_(version), |
| 225 local_path_(GetPopularSitesPath()), |
| 225 profile_(profile), | 226 profile_(profile), |
| 226 weak_ptr_factory_(this) { | 227 weak_ptr_factory_(this) { |
| 227 const base::Time last_download_time = base::Time::FromInternalValue( | 228 const base::Time last_download_time = base::Time::FromInternalValue( |
| 228 profile_->GetPrefs()->GetInt64(kPopularSitesLastDownloadPref)); | 229 profile_->GetPrefs()->GetInt64(kPopularSitesLastDownloadPref)); |
| 229 const base::TimeDelta time_since_last_download = | 230 const base::TimeDelta time_since_last_download = |
| 230 base::Time::Now() - last_download_time; | 231 base::Time::Now() - last_download_time; |
| 231 const base::TimeDelta redownload_interval = | 232 const base::TimeDelta redownload_interval = |
| 232 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 233 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
| 233 const bool download_time_is_future = base::Time::Now() < last_download_time; | 234 const bool download_time_is_future = base::Time::Now() < last_download_time; |
| 234 | 235 |
| 235 const bool should_redownload_if_exists = | 236 const bool should_redownload_if_exists = |
| 236 force_download || download_time_is_future || | 237 force_download || download_time_is_future || |
| 237 (time_since_last_download > redownload_interval); | 238 (time_since_last_download > redownload_interval); |
| 239 // TODO(treib): If country/version don't match what we have, we should |
| 240 // probably also redownload? |
| 238 | 241 |
| 239 FetchPopularSites(url, should_redownload_if_exists, false /* is_fallback */); | 242 FetchPopularSites( |
| 243 override_url.is_valid() ? override_url : GetPopularSitesURL(), |
| 244 should_redownload_if_exists, false /* is_fallback */); |
| 245 } |
| 246 |
| 247 GURL PopularSites::GetPopularSitesURL() const { |
| 248 return GURL(base::StringPrintf(kPopularSitesURLFormat, |
| 249 pending_country_.c_str(), |
| 250 pending_version_.c_str())); |
| 240 } | 251 } |
| 241 | 252 |
| 242 void PopularSites::FetchPopularSites(const GURL& url, | 253 void PopularSites::FetchPopularSites(const GURL& url, |
| 243 bool force_download, | 254 bool force_download, |
| 244 bool is_fallback) { | 255 bool is_fallback) { |
| 245 downloader_.reset( | 256 downloader_.reset(new FileDownloader( |
| 246 new FileDownloader(url, popular_sites_local_path_, force_download, | 257 url, local_path_, force_download, profile_->GetRequestContext(), |
| 247 profile_->GetRequestContext(), | 258 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), |
| 248 base::Bind(&PopularSites::OnDownloadDone, | 259 is_fallback))); |
| 249 base::Unretained(this), is_fallback))); | |
| 250 } | 260 } |
| 251 | 261 |
| 252 void PopularSites::OnDownloadDone(bool is_fallback, bool success) { | 262 void PopularSites::OnDownloadDone(bool is_fallback, |
| 263 FileDownloader::Result result) { |
| 253 downloader_.reset(); | 264 downloader_.reset(); |
| 254 if (success) { | 265 PrefService* prefs = profile_->GetPrefs(); |
| 255 profile_->GetPrefs()->SetInt64(kPopularSitesLastDownloadPref, | 266 switch (result) { |
| 256 base::Time::Now().ToInternalValue()); | 267 case FileDownloader::DOWNLOADED: |
| 257 ParseSiteList(popular_sites_local_path_); | 268 prefs->SetInt64(kPopularSitesLastDownloadPref, |
| 258 } else if (!is_fallback) { | 269 base::Time::Now().ToInternalValue()); |
| 259 DLOG(WARNING) << "Download country site list failed"; | 270 prefs->SetString(kPopularSitesCountryPref, pending_country_); |
| 260 // It is fine to force the download as Fallback is only triggered after a | 271 prefs->SetString(kPopularSitesVersionPref, pending_version_); |
| 261 // failed download. | 272 ParseSiteList(local_path_); |
| 262 FetchPopularSites(GetPopularSitesFallbackURL(profile_), | 273 break; |
| 263 true /* force_download */, true /* is_fallback */); | 274 case FileDownloader::EXISTS: |
| 264 } else { | 275 ParseSiteList(local_path_); |
| 265 DLOG(WARNING) << "Download fallback site list failed"; | 276 break; |
| 266 callback_.Run(false); | 277 case FileDownloader::FAILED: |
| 278 if (!is_fallback) { |
| 279 DLOG(WARNING) << "Download country site list failed"; |
| 280 pending_country_ = kPopularSitesDefaultCountryCode; |
| 281 pending_version_ = kPopularSitesDefaultVersion; |
| 282 // It is fine to force the download as Fallback is only triggered after |
| 283 // a failed download. |
| 284 FetchPopularSites(GetPopularSitesURL(), true /* force_download */, |
| 285 true /* is_fallback */); |
| 286 } else { |
| 287 DLOG(WARNING) << "Download fallback site list failed"; |
| 288 callback_.Run(false); |
| 289 } |
| 290 break; |
| 267 } | 291 } |
| 268 } | 292 } |
| 269 | 293 |
| 270 void PopularSites::ParseSiteList(const base::FilePath& path) { | 294 void PopularSites::ParseSiteList(const base::FilePath& path) { |
| 271 base::PostTaskAndReplyWithResult( | 295 base::PostTaskAndReplyWithResult( |
| 272 BrowserThread::GetBlockingPool() | 296 BrowserThread::GetBlockingPool() |
| 273 ->GetTaskRunnerWithShutdownBehavior( | 297 ->GetTaskRunnerWithShutdownBehavior( |
| 274 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) | 298 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) |
| 275 .get(), | 299 .get(), |
| 276 FROM_HERE, base::Bind(&ReadAndParseJsonFile, path), | 300 FROM_HERE, base::Bind(&ReadAndParseJsonFile, path), |
| 277 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); | 301 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); |
| 278 } | 302 } |
| 279 | 303 |
| 280 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { | 304 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { |
| 281 if (sites) | 305 if (sites) |
| 282 sites_.swap(*sites); | 306 sites_.swap(*sites); |
| 283 else | 307 else |
| 284 sites_.clear(); | 308 sites_.clear(); |
| 285 callback_.Run(!!sites); | 309 callback_.Run(!!sites); |
| 286 } | 310 } |
| OLD | NEW |