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

Side by Side Diff: components/ntp_tiles/popular_sites.cc

Issue 2570783003: [Popular Sites] Split PopularSites interface and PopularSitesImpl (Closed)
Patch Set: Split PopularSitesImpl. Created 4 years 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 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 "components/ntp_tiles/popular_sites.h" 5 #include "components/ntp_tiles/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"
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 : title(title), 170 : title(title),
171 url(url), 171 url(url),
172 favicon_url(favicon_url), 172 favicon_url(favicon_url),
173 large_icon_url(large_icon_url), 173 large_icon_url(large_icon_url),
174 thumbnail_url(thumbnail_url) {} 174 thumbnail_url(thumbnail_url) {}
175 175
176 PopularSites::Site::Site(const Site& other) = default; 176 PopularSites::Site::Site(const Site& other) = default;
177 177
178 PopularSites::Site::~Site() {} 178 PopularSites::Site::~Site() {}
179 179
180 PopularSites::PopularSites( 180 PopularSitesImpl::PopularSitesImpl(
181 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, 181 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool,
182 PrefService* prefs, 182 PrefService* prefs,
183 const TemplateURLService* template_url_service, 183 const TemplateURLService* template_url_service,
184 VariationsService* variations_service, 184 VariationsService* variations_service,
185 net::URLRequestContextGetter* download_context, 185 net::URLRequestContextGetter* download_context,
186 const base::FilePath& directory, 186 const base::FilePath& directory,
187 ParseJSONCallback parse_json) 187 ParseJSONCallback parse_json)
188 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( 188 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior(
189 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), 189 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)),
190 prefs_(prefs), 190 prefs_(prefs),
191 template_url_service_(template_url_service), 191 template_url_service_(template_url_service),
192 variations_(variations_service), 192 variations_(variations_service),
193 download_context_(download_context), 193 download_context_(download_context),
194 local_path_(directory.empty() 194 local_path_(directory.empty()
195 ? base::FilePath() 195 ? base::FilePath()
196 : directory.AppendASCII(kPopularSitesLocalFilename)), 196 : directory.AppendASCII(kPopularSitesLocalFilename)),
197 parse_json_(std::move(parse_json)), 197 parse_json_(std::move(parse_json)),
198 is_fallback_(false), 198 is_fallback_(false),
199 weak_ptr_factory_(this) {} 199 weak_ptr_factory_(this) {}
200 200
201 PopularSites::~PopularSites() {} 201 PopularSitesImpl::~PopularSitesImpl() {}
202 202
203 void PopularSites::StartFetch(bool force_download, 203 void PopularSitesImpl::StartFetch(bool force_download,
204 const FinishedCallback& callback) { 204 const FinishedCallback& callback) {
205 DCHECK(!callback_); 205 DCHECK(!callback_);
206 callback_ = callback; 206 callback_ = callback;
207 207
208 const base::Time last_download_time = base::Time::FromInternalValue( 208 const base::Time last_download_time = base::Time::FromInternalValue(
209 prefs_->GetInt64(kPopularSitesLastDownloadPref)); 209 prefs_->GetInt64(kPopularSitesLastDownloadPref));
210 const base::TimeDelta time_since_last_download = 210 const base::TimeDelta time_since_last_download =
211 base::Time::Now() - last_download_time; 211 base::Time::Now() - last_download_time;
212 const base::TimeDelta redownload_interval = 212 const base::TimeDelta redownload_interval =
213 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); 213 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours);
214 const bool download_time_is_future = base::Time::Now() < last_download_time; 214 const bool download_time_is_future = base::Time::Now() < last_download_time;
(...skipping 21 matching lines...) Expand all
236 (time_since_last_download > redownload_interval) || url_changed) { 236 (time_since_last_download > redownload_interval) || url_changed) {
237 FetchPopularSites(); 237 FetchPopularSites();
238 return; 238 return;
239 } 239 }
240 240
241 std::unique_ptr<std::string> file_data(new std::string); 241 std::unique_ptr<std::string> file_data(new std::string);
242 std::string* file_data_ptr = file_data.get(); 242 std::string* file_data_ptr = file_data.get();
243 base::PostTaskAndReplyWithResult( 243 base::PostTaskAndReplyWithResult(
244 blocking_runner_.get(), FROM_HERE, 244 blocking_runner_.get(), FROM_HERE,
245 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), 245 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr),
246 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), 246 base::Bind(&PopularSitesImpl::OnReadFileDone,
247 weak_ptr_factory_.GetWeakPtr(),
247 base::Passed(std::move(file_data)))); 248 base::Passed(std::move(file_data))));
248 } 249 }
249 250
250 GURL PopularSites::LastURL() const { 251 const PopularSites::SitesVector& PopularSitesImpl::sites() const {
252 return sites_;
253 }
254
255 GURL PopularSitesImpl::LastURL() const {
251 return GURL(prefs_->GetString(kPopularSitesURLPref)); 256 return GURL(prefs_->GetString(kPopularSitesURLPref));
252 } 257 }
253 258
254 // static 259 // static
255 void PopularSites::RegisterProfilePrefs( 260 void PopularSitesImpl::RegisterProfilePrefs(
256 user_prefs::PrefRegistrySyncable* user_prefs) { 261 user_prefs::PrefRegistrySyncable* user_prefs) {
257 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, 262 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL,
258 std::string()); 263 std::string());
259 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, 264 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry,
260 std::string()); 265 std::string());
261 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, 266 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion,
262 std::string()); 267 std::string());
263 268
264 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); 269 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0);
265 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); 270 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string());
266 } 271 }
267 272
268 void PopularSites::OnReadFileDone(std::unique_ptr<std::string> data, 273 void PopularSitesImpl::OnReadFileDone(std::unique_ptr<std::string> data,
269 bool success) { 274 bool success) {
270 if (success) { 275 if (success) {
271 auto json = base::JSONReader::Read(*data, base::JSON_ALLOW_TRAILING_COMMAS); 276 auto json = base::JSONReader::Read(*data, base::JSON_ALLOW_TRAILING_COMMAS);
272 if (json) { 277 if (json) {
273 ParseSiteList(std::move(json)); 278 ParseSiteList(std::move(json));
274 } else { 279 } else {
275 OnJsonParseFailed("previously-fetched JSON was no longer parseable"); 280 OnJsonParseFailed("previously-fetched JSON was no longer parseable");
276 } 281 }
277 } else { 282 } else {
278 // File didn't exist, or couldn't be read for some other reason. 283 // File didn't exist, or couldn't be read for some other reason.
279 FetchPopularSites(); 284 FetchPopularSites();
280 } 285 }
281 } 286 }
282 287
283 void PopularSites::FetchPopularSites() { 288 void PopularSitesImpl::FetchPopularSites() {
284 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this); 289 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this);
285 data_use_measurement::DataUseUserData::AttachToFetcher( 290 data_use_measurement::DataUseUserData::AttachToFetcher(
286 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES); 291 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES);
287 fetcher_->SetRequestContext(download_context_); 292 fetcher_->SetRequestContext(download_context_);
288 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 293 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
289 net::LOAD_DO_NOT_SAVE_COOKIES); 294 net::LOAD_DO_NOT_SAVE_COOKIES);
290 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); 295 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1);
291 fetcher_->Start(); 296 fetcher_->Start();
292 } 297 }
293 298
294 void PopularSites::OnURLFetchComplete(const net::URLFetcher* source) { 299 void PopularSitesImpl::OnURLFetchComplete(const net::URLFetcher* source) {
295 DCHECK_EQ(fetcher_.get(), source); 300 DCHECK_EQ(fetcher_.get(), source);
296 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); 301 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_);
297 302
298 std::string json_string; 303 std::string json_string;
299 if (!(source->GetStatus().is_success() && 304 if (!(source->GetStatus().is_success() &&
300 source->GetResponseCode() == net::HTTP_OK && 305 source->GetResponseCode() == net::HTTP_OK &&
301 source->GetResponseAsString(&json_string))) { 306 source->GetResponseAsString(&json_string))) {
302 OnDownloadFailed(); 307 OnDownloadFailed();
303 return; 308 return;
304 } 309 }
305 310
306 parse_json_.Run( 311 parse_json_.Run(json_string, base::Bind(&PopularSitesImpl::OnJsonParsed,
307 json_string, 312 weak_ptr_factory_.GetWeakPtr()),
308 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), 313 base::Bind(&PopularSitesImpl::OnJsonParseFailed,
309 base::Bind(&PopularSites::OnJsonParseFailed, 314 weak_ptr_factory_.GetWeakPtr()));
310 weak_ptr_factory_.GetWeakPtr()));
311 } 315 }
312 316
313 void PopularSites::OnJsonParsed(std::unique_ptr<base::Value> json) { 317 void PopularSitesImpl::OnJsonParsed(std::unique_ptr<base::Value> json) {
314 const base::Value* json_ptr = json.get(); 318 const base::Value* json_ptr = json.get();
315 base::PostTaskAndReplyWithResult( 319 base::PostTaskAndReplyWithResult(
316 blocking_runner_.get(), FROM_HERE, 320 blocking_runner_.get(), FROM_HERE,
317 base::Bind(&WriteJsonToFile, local_path_, json_ptr), 321 base::Bind(&WriteJsonToFile, local_path_, json_ptr),
318 base::Bind(&PopularSites::OnFileWriteDone, weak_ptr_factory_.GetWeakPtr(), 322 base::Bind(&PopularSitesImpl::OnFileWriteDone,
323 weak_ptr_factory_.GetWeakPtr(),
319 base::Passed(std::move(json)))); 324 base::Passed(std::move(json))));
320 } 325 }
321 326
322 void PopularSites::OnJsonParseFailed(const std::string& error_message) { 327 void PopularSitesImpl::OnJsonParseFailed(const std::string& error_message) {
323 DLOG(WARNING) << "JSON parsing failed: " << error_message; 328 DLOG(WARNING) << "JSON parsing failed: " << error_message;
324 OnDownloadFailed(); 329 OnDownloadFailed();
325 } 330 }
326 331
327 void PopularSites::OnFileWriteDone(std::unique_ptr<base::Value> json, 332 void PopularSitesImpl::OnFileWriteDone(std::unique_ptr<base::Value> json,
328 bool success) { 333 bool success) {
329 if (success) { 334 if (success) {
330 prefs_->SetInt64(kPopularSitesLastDownloadPref, 335 prefs_->SetInt64(kPopularSitesLastDownloadPref,
331 base::Time::Now().ToInternalValue()); 336 base::Time::Now().ToInternalValue());
332 prefs_->SetString(kPopularSitesURLPref, pending_url_.spec()); 337 prefs_->SetString(kPopularSitesURLPref, pending_url_.spec());
333 ParseSiteList(std::move(json)); 338 ParseSiteList(std::move(json));
334 } else { 339 } else {
335 DLOG(WARNING) << "Could not write file to " 340 DLOG(WARNING) << "Could not write file to "
336 << local_path_.LossyDisplayName(); 341 << local_path_.LossyDisplayName();
337 OnDownloadFailed(); 342 OnDownloadFailed();
338 } 343 }
339 } 344 }
340 345
341 void PopularSites::ParseSiteList(std::unique_ptr<base::Value> json) { 346 void PopularSitesImpl::ParseSiteList(std::unique_ptr<base::Value> json) {
342 base::ListValue* list = nullptr; 347 base::ListValue* list = nullptr;
343 if (!json || !json->GetAsList(&list)) { 348 if (!json || !json->GetAsList(&list)) {
344 DLOG(WARNING) << "JSON is not a list"; 349 DLOG(WARNING) << "JSON is not a list";
345 sites_.clear(); 350 sites_.clear();
346 callback_.Run(false); 351 callback_.Run(false);
347 return; 352 return;
348 } 353 }
349 354
350 std::vector<PopularSites::Site> sites; 355 SitesVector sites;
351 for (size_t i = 0; i < list->GetSize(); i++) { 356 for (size_t i = 0; i < list->GetSize(); i++) {
352 base::DictionaryValue* item; 357 base::DictionaryValue* item;
353 if (!list->GetDictionary(i, &item)) 358 if (!list->GetDictionary(i, &item))
354 continue; 359 continue;
355 base::string16 title; 360 base::string16 title;
356 std::string url; 361 std::string url;
357 if (!item->GetString("title", &title) || !item->GetString("url", &url)) 362 if (!item->GetString("title", &title) || !item->GetString("url", &url))
358 continue; 363 continue;
359 std::string favicon_url; 364 std::string favicon_url;
360 item->GetString("favicon_url", &favicon_url); 365 item->GetString("favicon_url", &favicon_url);
361 std::string thumbnail_url; 366 std::string thumbnail_url;
362 item->GetString("thumbnail_url", &thumbnail_url); 367 item->GetString("thumbnail_url", &thumbnail_url);
363 std::string large_icon_url; 368 std::string large_icon_url;
364 item->GetString("large_icon_url", &large_icon_url); 369 item->GetString("large_icon_url", &large_icon_url);
365 370
366 sites.push_back(PopularSites::Site(title, GURL(url), GURL(favicon_url), 371 sites.push_back(PopularSitesImpl::Site(title, GURL(url), GURL(favicon_url),
367 GURL(large_icon_url), 372 GURL(large_icon_url),
368 GURL(thumbnail_url))); 373 GURL(thumbnail_url)));
369 } 374 }
370 375
371 sites_.swap(sites); 376 sites_.swap(sites);
372 callback_.Run(true); 377 callback_.Run(true);
373 } 378 }
374 379
375 void PopularSites::OnDownloadFailed() { 380 void PopularSitesImpl::OnDownloadFailed() {
376 if (!is_fallback_) { 381 if (!is_fallback_) {
377 DLOG(WARNING) << "Download country site list failed"; 382 DLOG(WARNING) << "Download country site list failed";
378 is_fallback_ = true; 383 is_fallback_ = true;
379 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, 384 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode,
380 kPopularSitesDefaultVersion); 385 kPopularSitesDefaultVersion);
381 FetchPopularSites(); 386 FetchPopularSites();
382 } else { 387 } else {
383 DLOG(WARNING) << "Download fallback site list failed"; 388 DLOG(WARNING) << "Download fallback site list failed";
384 callback_.Run(false); 389 callback_.Run(false);
385 } 390 }
386 } 391 }
387 392
388 } // namespace ntp_tiles 393 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698