Chromium Code Reviews| 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 "components/ntp_tiles/popular_sites_impl.h" | 5 #include "components/ntp_tiles/popular_sites_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/json/json_reader.h" | |
| 14 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "components/data_use_measurement/core/data_use_user_data.h" | 20 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 20 #include "components/google/core/browser/google_util.h" | 21 #include "components/google/core/browser/google_util.h" |
| 22 #include "components/grit/components_resources.h" | |
| 21 #include "components/ntp_tiles/constants.h" | 23 #include "components/ntp_tiles/constants.h" |
| 22 #include "components/ntp_tiles/pref_names.h" | 24 #include "components/ntp_tiles/pref_names.h" |
| 23 #include "components/ntp_tiles/switches.h" | 25 #include "components/ntp_tiles/switches.h" |
| 24 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
| 25 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
| 26 #include "components/search_engines/search_engine_type.h" | 28 #include "components/search_engines/search_engine_type.h" |
| 27 #include "components/search_engines/template_url_service.h" | 29 #include "components/search_engines/template_url_service.h" |
| 28 #include "components/variations/service/variations_service.h" | 30 #include "components/variations/service/variations_service.h" |
| 29 #include "components/variations/variations_associated_data.h" | 31 #include "components/variations/variations_associated_data.h" |
| 30 #include "net/base/load_flags.h" | 32 #include "net/base/load_flags.h" |
| 31 #include "net/http/http_status_code.h" | 33 #include "net/http/http_status_code.h" |
| 34 #include "ui/base/resource/resource_bundle.h" | |
| 32 | 35 |
| 33 #if defined(OS_IOS) | 36 #if defined(OS_IOS) |
| 34 #include "components/ntp_tiles/country_code_ios.h" | 37 #include "components/ntp_tiles/country_code_ios.h" |
| 35 #endif | 38 #endif |
| 36 | 39 |
| 37 using net::URLFetcher; | 40 using net::URLFetcher; |
| 38 using variations::VariationsService; | 41 using variations::VariationsService; |
| 39 | 42 |
| 40 namespace ntp_tiles { | 43 namespace ntp_tiles { |
| 41 | 44 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 item->GetString("thumbnail_url", &thumbnail_url); | 121 item->GetString("thumbnail_url", &thumbnail_url); |
| 119 std::string large_icon_url; | 122 std::string large_icon_url; |
| 120 item->GetString("large_icon_url", &large_icon_url); | 123 item->GetString("large_icon_url", &large_icon_url); |
| 121 | 124 |
| 122 sites.emplace_back(title, GURL(url), GURL(favicon_url), | 125 sites.emplace_back(title, GURL(url), GURL(favicon_url), |
| 123 GURL(large_icon_url), GURL(thumbnail_url)); | 126 GURL(large_icon_url), GURL(thumbnail_url)); |
| 124 } | 127 } |
| 125 return sites; | 128 return sites; |
| 126 } | 129 } |
| 127 | 130 |
| 131 base::ListValue* DefaultPopularSites() { | |
| 132 base::JSONReader json_reader; | |
| 133 std::unique_ptr<base::Value> value = json_reader.ReadToValue( | |
| 134 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 135 IDR_DEFAULT_POPULAR_SITES_JSON)); | |
| 136 if (!value) { | |
|
sfiera
2017/02/09 12:39:06
ListValue::From() accepts nullptr, returning nullp
fhorschig
2017/02/09 15:30:18
This is what I was looking for, thank you!
| |
| 137 NOTREACHED(); | |
| 138 return nullptr; | |
| 139 } | |
| 140 return base::ListValue::From(std::move(value)).release(); | |
|
sfiera
2017/02/09 12:39:06
Return a std::unique_ptr<> and have the caller rel
fhorschig
2017/02/09 15:30:18
Done.
| |
| 141 } | |
| 142 | |
| 128 } // namespace | 143 } // namespace |
| 129 | 144 |
| 130 PopularSites::Site::Site(const base::string16& title, | 145 PopularSites::Site::Site(const base::string16& title, |
| 131 const GURL& url, | 146 const GURL& url, |
| 132 const GURL& favicon_url, | 147 const GURL& favicon_url, |
| 133 const GURL& large_icon_url, | 148 const GURL& large_icon_url, |
| 134 const GURL& thumbnail_url) | 149 const GURL& thumbnail_url) |
| 135 : title(title), | 150 : title(title), |
| 136 url(url), | 151 url(url), |
| 137 favicon_url(favicon_url), | 152 favicon_url(favicon_url), |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 } | 184 } |
| 170 } | 185 } |
| 171 | 186 |
| 172 PopularSitesImpl::~PopularSitesImpl() {} | 187 PopularSitesImpl::~PopularSitesImpl() {} |
| 173 | 188 |
| 174 bool PopularSitesImpl::MaybeStartFetch(bool force_download, | 189 bool PopularSitesImpl::MaybeStartFetch(bool force_download, |
| 175 const FinishedCallback& callback) { | 190 const FinishedCallback& callback) { |
| 176 DCHECK(!callback_); | 191 DCHECK(!callback_); |
| 177 callback_ = callback; | 192 callback_ = callback; |
| 178 | 193 |
| 194 const base::ListValue* json = prefs_->GetList(kPopularSitesJsonPref); | |
| 195 // Assuming the prefs were cleared, there would be no popular sites. | |
|
sfiera
2017/02/09 12:39:06
I don't understand this comment.
- What do you me
fhorschig
2017/02/09 15:30:18
Thank you for the clarification, I was assuming th
| |
| 196 if (json) { | |
| 197 // Note that we don't run the callback. | |
| 198 sites_ = ParseSiteList(*json); | |
|
sfiera
2017/02/09 12:39:06
Any reason we wouldn't want to do this in the cons
fhorschig
2017/02/09 15:30:18
Doing this in the constructor sounds good.
Parsing
| |
| 199 } | |
| 200 | |
| 179 const base::Time last_download_time = base::Time::FromInternalValue( | 201 const base::Time last_download_time = base::Time::FromInternalValue( |
| 180 prefs_->GetInt64(kPopularSitesLastDownloadPref)); | 202 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
| 181 const base::TimeDelta time_since_last_download = | 203 const base::TimeDelta time_since_last_download = |
| 182 base::Time::Now() - last_download_time; | 204 base::Time::Now() - last_download_time; |
| 183 const base::TimeDelta redownload_interval = | 205 const base::TimeDelta redownload_interval = |
| 184 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 206 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
| 185 const bool download_time_is_future = base::Time::Now() < last_download_time; | 207 const bool download_time_is_future = base::Time::Now() < last_download_time; |
| 186 | 208 |
| 187 pending_url_ = GetURLToFetch(); | 209 pending_url_ = GetURLToFetch(); |
| 188 const bool url_changed = | 210 const bool url_changed = |
| 189 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); | 211 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); |
| 190 | 212 |
| 191 // Download forced, or we need to download a new file. | 213 // Cache empty, download forced, or we need to download a newer file. |
| 192 if (force_download || download_time_is_future || | 214 if (!json || force_download || download_time_is_future || |
| 193 (time_since_last_download > redownload_interval) || url_changed) { | 215 (time_since_last_download > redownload_interval) || url_changed) { |
| 194 FetchPopularSites(); | 216 FetchPopularSites(); |
| 195 return true; | 217 return true; |
| 196 } | 218 } |
| 197 | 219 return false; |
| 198 const base::ListValue* json = prefs_->GetList(kPopularSitesJsonPref); | |
| 199 if (!json) { | |
| 200 // Cache didn't exist. | |
| 201 FetchPopularSites(); | |
| 202 return true; | |
| 203 } else { | |
| 204 // Note that we don't run the callback. | |
| 205 sites_ = ParseSiteList(*json); | |
| 206 return false; | |
| 207 } | |
| 208 } | 220 } |
| 209 | 221 |
| 210 const PopularSites::SitesVector& PopularSitesImpl::sites() const { | 222 const PopularSites::SitesVector& PopularSitesImpl::sites() const { |
| 211 return sites_; | 223 return sites_; |
| 212 } | 224 } |
| 213 | 225 |
| 214 GURL PopularSitesImpl::GetLastURLFetched() const { | 226 GURL PopularSitesImpl::GetLastURLFetched() const { |
| 215 return GURL(prefs_->GetString(kPopularSitesURLPref)); | 227 return GURL(prefs_->GetString(kPopularSitesURLPref)); |
| 216 } | 228 } |
| 217 | 229 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 user_prefs::PrefRegistrySyncable* user_prefs) { | 294 user_prefs::PrefRegistrySyncable* user_prefs) { |
| 283 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, | 295 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| 284 std::string()); | 296 std::string()); |
| 285 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 297 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, |
| 286 std::string()); | 298 std::string()); |
| 287 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, | 299 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, |
| 288 std::string()); | 300 std::string()); |
| 289 | 301 |
| 290 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 302 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
| 291 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); | 303 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); |
| 292 user_prefs->RegisterListPref(kPopularSitesJsonPref); | 304 // RegisterListPref takes ownership of the default list. |
| 305 user_prefs->RegisterListPref(kPopularSitesJsonPref, DefaultPopularSites()); | |
| 293 } | 306 } |
| 294 | 307 |
| 295 void PopularSitesImpl::FetchPopularSites() { | 308 void PopularSitesImpl::FetchPopularSites() { |
| 296 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this); | 309 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this); |
| 297 data_use_measurement::DataUseUserData::AttachToFetcher( | 310 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 298 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES); | 311 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES); |
| 299 fetcher_->SetRequestContext(download_context_); | 312 fetcher_->SetRequestContext(download_context_); |
| 300 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 313 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 301 net::LOAD_DO_NOT_SAVE_COOKIES); | 314 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 302 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | 315 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, | 364 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, |
| 352 kPopularSitesDefaultVersion); | 365 kPopularSitesDefaultVersion); |
| 353 FetchPopularSites(); | 366 FetchPopularSites(); |
| 354 } else { | 367 } else { |
| 355 DLOG(WARNING) << "Download fallback site list failed"; | 368 DLOG(WARNING) << "Download fallback site list failed"; |
| 356 callback_.Run(false); | 369 callback_.Run(false); |
| 357 } | 370 } |
| 358 } | 371 } |
| 359 | 372 |
| 360 } // namespace ntp_tiles | 373 } // namespace ntp_tiles |
| OLD | NEW |