Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: chrome/browser/android/ntp/popular_sites.cc

Issue 1851803002: [NTP Popular Sites] Store country/version in prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 } 116 }
118 117
119 GURL GetPopularSitesURL(Profile* profile, 118 GURL GetPopularSitesURL(const std::string& country,
Bernhard Bauer 2016/04/07 12:24:28 This now always gets called with |pending_country_
Marc Treib 2016/04/07 13:38:39 Done.
120 const std::string& override_country, 119 const std::string& version) {
121 const std::string& override_version) { 120 return GURL(base::StringPrintf(kPopularSitesURLFormat, country.c_str(),
122 return GURL(base::StringPrintf( 121 version.c_str()));
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 } 122 }
132 123
133 base::FilePath GetPopularSitesPath() { 124 base::FilePath GetPopularSitesPath() {
134 base::FilePath dir; 125 base::FilePath dir;
135 PathService::Get(chrome::DIR_USER_DATA, &dir); 126 PathService::Get(chrome::DIR_USER_DATA, &dir);
136 return dir.AppendASCII(kPopularSitesLocalFilename); 127 return dir.AppendASCII(kPopularSitesLocalFilename);
137 } 128 }
138 129
139 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( 130 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile(
140 const base::FilePath& path) { 131 const base::FilePath& path) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 thumbnail_url(thumbnail_url) {} 182 thumbnail_url(thumbnail_url) {}
192 183
193 PopularSites::Site::~Site() {} 184 PopularSites::Site::~Site() {}
194 185
195 PopularSites::PopularSites(Profile* profile, 186 PopularSites::PopularSites(Profile* profile,
196 const std::string& override_country, 187 const std::string& override_country,
197 const std::string& override_version, 188 const std::string& override_version,
198 bool force_download, 189 bool force_download,
199 const FinishedCallback& callback) 190 const FinishedCallback& callback)
200 : PopularSites(profile, 191 : PopularSites(profile,
201 GetPopularSitesURL(profile, override_country, 192 GetCountryToUse(profile, override_country),
202 override_version), 193 GetVersionToUse(override_version),
194 GURL(),
203 force_download, 195 force_download,
204 callback) {} 196 callback) {}
205 197
206 PopularSites::PopularSites(Profile* profile, 198 PopularSites::PopularSites(Profile* profile,
207 const GURL& url, 199 const GURL& url,
208 const FinishedCallback& callback) 200 const FinishedCallback& callback)
209 : PopularSites(profile, url, true, callback) {} 201 : PopularSites(profile, std::string(), std::string(), url, true, callback) {
202 }
210 203
211 PopularSites::~PopularSites() {} 204 PopularSites::~PopularSites() {}
212 205
206 std::string PopularSites::GetCountry() const {
207 return profile_->GetPrefs()->GetString(kPopularSitesCountryPref);
208 }
209
210 std::string PopularSites::GetVersion() const {
211 return profile_->GetPrefs()->GetString(kPopularSitesVersionPref);
212 }
213
213 // static 214 // static
214 void PopularSites::RegisterProfilePrefs( 215 void PopularSites::RegisterProfilePrefs(
215 user_prefs::PrefRegistrySyncable* user_prefs) { 216 user_prefs::PrefRegistrySyncable* user_prefs) {
216 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); 217 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0);
218 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string());
219 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string());
217 } 220 }
218 221
219 PopularSites::PopularSites(Profile* profile, 222 PopularSites::PopularSites(Profile* profile,
220 const GURL& url, 223 const std::string& country,
224 const std::string& version,
225 const GURL& override_url,
221 bool force_download, 226 bool force_download,
222 const FinishedCallback& callback) 227 const FinishedCallback& callback)
223 : callback_(callback), 228 : callback_(callback),
224 popular_sites_local_path_(GetPopularSitesPath()), 229 pending_country_(country),
230 pending_version_(version),
231 local_path_(GetPopularSitesPath()),
225 profile_(profile), 232 profile_(profile),
226 weak_ptr_factory_(this) { 233 weak_ptr_factory_(this) {
227 const base::Time last_download_time = base::Time::FromInternalValue( 234 const base::Time last_download_time = base::Time::FromInternalValue(
228 profile_->GetPrefs()->GetInt64(kPopularSitesLastDownloadPref)); 235 profile_->GetPrefs()->GetInt64(kPopularSitesLastDownloadPref));
229 const base::TimeDelta time_since_last_download = 236 const base::TimeDelta time_since_last_download =
230 base::Time::Now() - last_download_time; 237 base::Time::Now() - last_download_time;
231 const base::TimeDelta redownload_interval = 238 const base::TimeDelta redownload_interval =
232 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); 239 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours);
233 const bool download_time_is_future = base::Time::Now() < last_download_time; 240 const bool download_time_is_future = base::Time::Now() < last_download_time;
234 241
235 const bool should_redownload_if_exists = 242 const bool should_redownload_if_exists =
236 force_download || download_time_is_future || 243 force_download || download_time_is_future ||
237 (time_since_last_download > redownload_interval); 244 (time_since_last_download > redownload_interval);
245 // TODO(treib): If country/version don't match what we have, we should
246 // probably also redownload?
238 247
239 FetchPopularSites(url, should_redownload_if_exists, false /* is_fallback */); 248 FetchPopularSites(
249 override_url.is_valid()
250 ? override_url
251 : GetPopularSitesURL(pending_country_, pending_version_),
252 should_redownload_if_exists, false /* is_fallback */);
240 } 253 }
241 254
242 void PopularSites::FetchPopularSites(const GURL& url, 255 void PopularSites::FetchPopularSites(const GURL& url,
243 bool force_download, 256 bool force_download,
244 bool is_fallback) { 257 bool is_fallback) {
245 downloader_.reset( 258 downloader_.reset(new FileDownloader(
246 new FileDownloader(url, popular_sites_local_path_, force_download, 259 url, local_path_, force_download, profile_->GetRequestContext(),
247 profile_->GetRequestContext(), 260 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this),
248 base::Bind(&PopularSites::OnDownloadDone, 261 is_fallback)));
249 base::Unretained(this), is_fallback)));
250 } 262 }
251 263
252 void PopularSites::OnDownloadDone(bool is_fallback, bool success) { 264 void PopularSites::OnDownloadDone(bool is_fallback,
265 FileDownloader::Result result) {
253 downloader_.reset(); 266 downloader_.reset();
254 if (success) { 267 PrefService* prefs = profile_->GetPrefs();
255 profile_->GetPrefs()->SetInt64(kPopularSitesLastDownloadPref, 268 switch (result) {
256 base::Time::Now().ToInternalValue()); 269 case FileDownloader::DOWNLOADED:
257 ParseSiteList(popular_sites_local_path_); 270 prefs->SetInt64(kPopularSitesLastDownloadPref,
258 } else if (!is_fallback) { 271 base::Time::Now().ToInternalValue());
259 DLOG(WARNING) << "Download country site list failed"; 272 prefs->SetString(kPopularSitesCountryPref, pending_country_);
260 // It is fine to force the download as Fallback is only triggered after a 273 prefs->SetString(kPopularSitesVersionPref, pending_version_);
261 // failed download. 274 ParseSiteList(local_path_);
262 FetchPopularSites(GetPopularSitesFallbackURL(profile_), 275 break;
263 true /* force_download */, true /* is_fallback */); 276 case FileDownloader::EXISTS:
264 } else { 277 ParseSiteList(local_path_);
265 DLOG(WARNING) << "Download fallback site list failed"; 278 break;
266 callback_.Run(false); 279 case FileDownloader::FAILED:
280 if (!is_fallback) {
281 DLOG(WARNING) << "Download country site list failed";
282 pending_country_ = kPopularSitesDefaultCountryCode;
283 pending_version_ = kPopularSitesDefaultVersion;
284 // It is fine to force the download as Fallback is only triggered after
285 // a failed download.
286 FetchPopularSites(
287 GetPopularSitesURL(pending_country_, pending_version_),
288 true /* force_download */, true /* is_fallback */);
289 } else {
290 DLOG(WARNING) << "Download fallback site list failed";
291 callback_.Run(false);
292 }
293 break;
267 } 294 }
268 } 295 }
269 296
270 void PopularSites::ParseSiteList(const base::FilePath& path) { 297 void PopularSites::ParseSiteList(const base::FilePath& path) {
271 base::PostTaskAndReplyWithResult( 298 base::PostTaskAndReplyWithResult(
272 BrowserThread::GetBlockingPool() 299 BrowserThread::GetBlockingPool()
273 ->GetTaskRunnerWithShutdownBehavior( 300 ->GetTaskRunnerWithShutdownBehavior(
274 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) 301 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)
275 .get(), 302 .get(),
276 FROM_HERE, base::Bind(&ReadAndParseJsonFile, path), 303 FROM_HERE, base::Bind(&ReadAndParseJsonFile, path),
277 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); 304 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()));
278 } 305 }
279 306
280 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { 307 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) {
281 if (sites) 308 if (sites)
282 sites_.swap(*sites); 309 sites_.swap(*sites);
283 else 310 else
284 sites_.clear(); 311 sites_.clear();
285 callback_.Run(!!sites); 312 callback_.Run(!!sites);
286 } 313 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698