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, |
198 } | 200 template_url_service, |
| 201 download_context, |
| 202 std::string(), |
| 203 std::string(), |
| 204 url, |
| 205 true, |
| 206 callback) {} |
199 | 207 |
200 PopularSites::~PopularSites() {} | 208 PopularSites::~PopularSites() {} |
201 | 209 |
202 std::string PopularSites::GetCountry() const { | 210 std::string PopularSites::GetCountry() const { |
203 return profile_->GetPrefs()->GetString(kPopularSitesCountryPref); | 211 return prefs_->GetString(kPopularSitesCountryPref); |
204 } | 212 } |
205 | 213 |
206 std::string PopularSites::GetVersion() const { | 214 std::string PopularSites::GetVersion() const { |
207 return profile_->GetPrefs()->GetString(kPopularSitesVersionPref); | 215 return prefs_->GetString(kPopularSitesVersionPref); |
208 } | 216 } |
209 | 217 |
210 // static | 218 // static |
211 void PopularSites::RegisterProfilePrefs( | 219 void PopularSites::RegisterProfilePrefs( |
212 user_prefs::PrefRegistrySyncable* user_prefs) { | 220 user_prefs::PrefRegistrySyncable* user_prefs) { |
213 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 221 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
214 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); | 222 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); |
215 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); | 223 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); |
216 } | 224 } |
217 | 225 |
218 PopularSites::PopularSites(Profile* profile, | 226 PopularSites::PopularSites(PrefService* prefs, |
| 227 const TemplateURLService* template_url_service, |
| 228 net::URLRequestContextGetter* download_context, |
219 const std::string& country, | 229 const std::string& country, |
220 const std::string& version, | 230 const std::string& version, |
221 const GURL& override_url, | 231 const GURL& override_url, |
222 bool force_download, | 232 bool force_download, |
223 const FinishedCallback& callback) | 233 const FinishedCallback& callback) |
224 : callback_(callback), | 234 : callback_(callback), |
225 pending_country_(country), | 235 pending_country_(country), |
226 pending_version_(version), | 236 pending_version_(version), |
227 local_path_(GetPopularSitesPath()), | 237 local_path_(GetPopularSitesPath()), |
228 profile_(profile), | 238 prefs_(prefs), |
| 239 template_url_service_(template_url_service), |
| 240 download_context_(download_context), |
229 weak_ptr_factory_(this) { | 241 weak_ptr_factory_(this) { |
230 const base::Time last_download_time = base::Time::FromInternalValue( | 242 const base::Time last_download_time = base::Time::FromInternalValue( |
231 profile_->GetPrefs()->GetInt64(kPopularSitesLastDownloadPref)); | 243 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
232 const base::TimeDelta time_since_last_download = | 244 const base::TimeDelta time_since_last_download = |
233 base::Time::Now() - last_download_time; | 245 base::Time::Now() - last_download_time; |
234 const base::TimeDelta redownload_interval = | 246 const base::TimeDelta redownload_interval = |
235 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 247 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
236 const bool download_time_is_future = base::Time::Now() < last_download_time; | 248 const bool download_time_is_future = base::Time::Now() < last_download_time; |
237 | 249 |
238 const bool should_redownload_if_exists = | 250 const bool should_redownload_if_exists = |
239 force_download || download_time_is_future || | 251 force_download || download_time_is_future || |
240 (time_since_last_download > redownload_interval); | 252 (time_since_last_download > redownload_interval); |
241 // TODO(treib): If country/version don't match what we have, we should | 253 // TODO(treib): If country/version don't match what we have, we should |
242 // probably also redownload? | 254 // probably also redownload? |
243 | 255 |
244 FetchPopularSites( | 256 FetchPopularSites( |
245 override_url.is_valid() ? override_url : GetPopularSitesURL(), | 257 override_url.is_valid() ? override_url : GetPopularSitesURL(), |
246 should_redownload_if_exists, false /* is_fallback */); | 258 should_redownload_if_exists, false /* is_fallback */); |
247 } | 259 } |
248 | 260 |
249 GURL PopularSites::GetPopularSitesURL() const { | 261 GURL PopularSites::GetPopularSitesURL() const { |
250 return GURL(base::StringPrintf(kPopularSitesURLFormat, | 262 return GURL(base::StringPrintf(kPopularSitesURLFormat, |
251 pending_country_.c_str(), | 263 pending_country_.c_str(), |
252 pending_version_.c_str())); | 264 pending_version_.c_str())); |
253 } | 265 } |
254 | 266 |
255 void PopularSites::FetchPopularSites(const GURL& url, | 267 void PopularSites::FetchPopularSites(const GURL& url, |
256 bool force_download, | 268 bool force_download, |
257 bool is_fallback) { | 269 bool is_fallback) { |
258 downloader_.reset(new FileDownloader( | 270 downloader_.reset(new FileDownloader( |
259 url, local_path_, force_download, profile_->GetRequestContext(), | 271 url, local_path_, force_download, download_context_, |
260 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), | 272 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), |
261 is_fallback))); | 273 is_fallback))); |
262 } | 274 } |
263 | 275 |
264 void PopularSites::OnDownloadDone(bool is_fallback, | 276 void PopularSites::OnDownloadDone(bool is_fallback, |
265 FileDownloader::Result result) { | 277 FileDownloader::Result result) { |
266 downloader_.reset(); | 278 downloader_.reset(); |
267 PrefService* prefs = profile_->GetPrefs(); | |
268 switch (result) { | 279 switch (result) { |
269 case FileDownloader::DOWNLOADED: | 280 case FileDownloader::DOWNLOADED: |
270 prefs->SetInt64(kPopularSitesLastDownloadPref, | 281 prefs_->SetInt64(kPopularSitesLastDownloadPref, |
271 base::Time::Now().ToInternalValue()); | 282 base::Time::Now().ToInternalValue()); |
272 prefs->SetString(kPopularSitesCountryPref, pending_country_); | 283 prefs_->SetString(kPopularSitesCountryPref, pending_country_); |
273 prefs->SetString(kPopularSitesVersionPref, pending_version_); | 284 prefs_->SetString(kPopularSitesVersionPref, pending_version_); |
274 ParseSiteList(local_path_); | 285 ParseSiteList(local_path_); |
275 break; | 286 break; |
276 case FileDownloader::EXISTS: | 287 case FileDownloader::EXISTS: |
277 ParseSiteList(local_path_); | 288 ParseSiteList(local_path_); |
278 break; | 289 break; |
279 case FileDownloader::FAILED: | 290 case FileDownloader::FAILED: |
280 if (!is_fallback) { | 291 if (!is_fallback) { |
281 DLOG(WARNING) << "Download country site list failed"; | 292 DLOG(WARNING) << "Download country site list failed"; |
282 pending_country_ = kPopularSitesDefaultCountryCode; | 293 pending_country_ = kPopularSitesDefaultCountryCode; |
283 pending_version_ = kPopularSitesDefaultVersion; | 294 pending_version_ = kPopularSitesDefaultVersion; |
(...skipping 19 matching lines...) Expand all Loading... |
303 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); | 314 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); |
304 } | 315 } |
305 | 316 |
306 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { | 317 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { |
307 if (sites) | 318 if (sites) |
308 sites_.swap(*sites); | 319 sites_.swap(*sites); |
309 else | 320 else |
310 sites_.clear(); | 321 sites_.clear(); |
311 callback_.Run(!!sites); | 322 callback_.Run(!!sites); |
312 } | 323 } |
OLD | NEW |