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

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
« no previous file with comments | « chrome/browser/android/ntp/popular_sites.h ('k') | chrome/browser/net/file_downloader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
Bernhard Bauer 2016/04/01 17:40:48 Is there a reason you're updating the copyright to
Marc Treib 2016/04/06 11:39:38 Yup: The file was created in 2015 :) The "2013" m
Bernhard Bauer 2016/04/06 15:41:54 FWIW, the response I have received in the past fro
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 GetCountryCode(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 GetVersion(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,
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 GetCountryCode(profile, override_country),
202 override_version), 193 GetVersion(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
213 // static 206 // static
214 void PopularSites::RegisterProfilePrefs( 207 void PopularSites::RegisterProfilePrefs(
215 user_prefs::PrefRegistrySyncable* user_prefs) { 208 user_prefs::PrefRegistrySyncable* user_prefs) {
216 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); 209 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0);
210 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string());
211 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string());
217 } 212 }
218 213
219 PopularSites::PopularSites(Profile* profile, 214 PopularSites::PopularSites(Profile* profile,
220 const GURL& url, 215 const std::string& country,
216 const std::string& version,
217 const GURL& override_url,
221 bool force_download, 218 bool force_download,
222 const FinishedCallback& callback) 219 const FinishedCallback& callback)
223 : callback_(callback), 220 : callback_(callback),
224 popular_sites_local_path_(GetPopularSitesPath()), 221 country_(country),
222 version_(version),
223 local_path_(GetPopularSitesPath()),
225 profile_(profile), 224 profile_(profile),
226 weak_ptr_factory_(this) { 225 weak_ptr_factory_(this) {
227 const base::Time last_download_time = base::Time::FromInternalValue( 226 const base::Time last_download_time = base::Time::FromInternalValue(
228 profile_->GetPrefs()->GetInt64(kPopularSitesLastDownloadPref)); 227 profile_->GetPrefs()->GetInt64(kPopularSitesLastDownloadPref));
229 const base::TimeDelta time_since_last_download = 228 const base::TimeDelta time_since_last_download =
230 base::Time::Now() - last_download_time; 229 base::Time::Now() - last_download_time;
231 const base::TimeDelta redownload_interval = 230 const base::TimeDelta redownload_interval =
232 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); 231 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours);
233 const bool download_time_is_future = base::Time::Now() < last_download_time; 232 const bool download_time_is_future = base::Time::Now() < last_download_time;
234 233
235 const bool should_redownload_if_exists = 234 const bool should_redownload_if_exists =
236 force_download || download_time_is_future || 235 force_download || download_time_is_future ||
237 (time_since_last_download > redownload_interval); 236 (time_since_last_download > redownload_interval);
237 // TODO(treib): If country/version don't match what we have, we should
238 // probably also redownload?
238 239
239 FetchPopularSites(url, should_redownload_if_exists, false /* is_fallback */); 240 FetchPopularSites(override_url.is_valid()
241 ? override_url
242 : GetPopularSitesURL(country_, version_),
243 should_redownload_if_exists, false /* is_fallback */);
240 } 244 }
241 245
242 void PopularSites::FetchPopularSites(const GURL& url, 246 void PopularSites::FetchPopularSites(const GURL& url,
243 bool force_download, 247 bool force_download,
244 bool is_fallback) { 248 bool is_fallback) {
245 downloader_.reset( 249 downloader_.reset(new FileDownloader(
246 new FileDownloader(url, popular_sites_local_path_, force_download, 250 url, local_path_, force_download, profile_->GetRequestContext(),
247 profile_->GetRequestContext(), 251 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this),
248 base::Bind(&PopularSites::OnDownloadDone, 252 is_fallback)));
249 base::Unretained(this), is_fallback)));
250 } 253 }
251 254
252 void PopularSites::OnDownloadDone(bool is_fallback, bool success) { 255 void PopularSites::OnDownloadDone(bool is_fallback,
256 FileDownloader::Result result) {
253 downloader_.reset(); 257 downloader_.reset();
254 if (success) { 258 PrefService* prefs = profile_->GetPrefs();
255 profile_->GetPrefs()->SetInt64(kPopularSitesLastDownloadPref, 259 switch (result) {
256 base::Time::Now().ToInternalValue()); 260 case FileDownloader::DOWNLOADED:
257 ParseSiteList(popular_sites_local_path_); 261 prefs->SetInt64(kPopularSitesLastDownloadPref,
258 } else if (!is_fallback) { 262 base::Time::Now().ToInternalValue());
259 DLOG(WARNING) << "Download country site list failed"; 263 prefs->SetString(kPopularSitesCountryPref, country_);
260 // It is fine to force the download as Fallback is only triggered after a 264 prefs->SetString(kPopularSitesVersionPref, version_);
261 // failed download. 265 ParseSiteList(local_path_);
262 FetchPopularSites(GetPopularSitesFallbackURL(profile_), 266 break;
263 true /* force_download */, true /* is_fallback */); 267 case FileDownloader::EXISTS:
264 } else { 268 // If we loaded an existing file, then its country/version might not
265 DLOG(WARNING) << "Download fallback site list failed"; 269 // match what |country_| and |version_| say, so load the correct values.
266 callback_.Run(false); 270 country_ = prefs->GetString(kPopularSitesCountryPref);
271 version_ = prefs->GetString(kPopularSitesVersionPref);
272 ParseSiteList(local_path_);
273 break;
274 case FileDownloader::FAILED:
275 if (!is_fallback) {
276 DLOG(WARNING) << "Download country site list failed";
277 country_ = kPopularSitesDefaultCountryCode;
278 version_ = kPopularSitesDefaultVersion;
279 // It is fine to force the download as Fallback is only triggered after
280 // a failed download.
281 FetchPopularSites(GetPopularSitesURL(country_, version_),
282 true /* force_download */, true /* is_fallback */);
283 } else {
284 DLOG(WARNING) << "Download fallback site list failed";
285 callback_.Run(false);
286 }
287 break;
267 } 288 }
268 } 289 }
269 290
270 void PopularSites::ParseSiteList(const base::FilePath& path) { 291 void PopularSites::ParseSiteList(const base::FilePath& path) {
271 base::PostTaskAndReplyWithResult( 292 base::PostTaskAndReplyWithResult(
272 BrowserThread::GetBlockingPool() 293 BrowserThread::GetBlockingPool()
273 ->GetTaskRunnerWithShutdownBehavior( 294 ->GetTaskRunnerWithShutdownBehavior(
274 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) 295 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)
275 .get(), 296 .get(),
276 FROM_HERE, base::Bind(&ReadAndParseJsonFile, path), 297 FROM_HERE, base::Bind(&ReadAndParseJsonFile, path),
277 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); 298 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()));
278 } 299 }
279 300
280 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { 301 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) {
281 if (sites) 302 if (sites)
282 sites_.swap(*sites); 303 sites_.swap(*sites);
283 else 304 else
284 sites_.clear(); 305 sites_.clear();
285 callback_.Run(!!sites); 306 callback_.Run(!!sites);
286 } 307 }
OLDNEW
« no previous file with comments | « chrome/browser/android/ntp/popular_sites.h ('k') | chrome/browser/net/file_downloader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698