| 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/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/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "components/data_use_measurement/core/data_use_user_data.h" | 19 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 20 #include "components/google/core/browser/google_util.h" | 20 #include "components/google/core/browser/google_util.h" |
| 21 #include "components/ntp_tiles/constants.h" | 21 #include "components/ntp_tiles/constants.h" |
| 22 #include "components/ntp_tiles/field_trial.h" |
| 22 #include "components/ntp_tiles/pref_names.h" | 23 #include "components/ntp_tiles/pref_names.h" |
| 23 #include "components/ntp_tiles/switches.h" | 24 #include "components/ntp_tiles/switches.h" |
| 24 #include "components/pref_registry/pref_registry_syncable.h" | 25 #include "components/pref_registry/pref_registry_syncable.h" |
| 25 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
| 26 #include "components/search_engines/search_engine_type.h" | 27 #include "components/search_engines/search_engine_type.h" |
| 27 #include "components/search_engines/template_url_service.h" | 28 #include "components/search_engines/template_url_service.h" |
| 28 #include "components/variations/service/variations_service.h" | 29 #include "components/variations/service/variations_service.h" |
| 29 #include "components/variations/variations_associated_data.h" | 30 #include "components/variations/variations_associated_data.h" |
| 30 #include "net/base/load_flags.h" | 31 #include "net/base/load_flags.h" |
| 31 #include "net/http/http_status_code.h" | 32 #include "net/http/http_status_code.h" |
| 32 | 33 |
| 34 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 35 #include "base/json/json_reader.h" |
| 36 #include "components/grit/components_resources.h" |
| 37 #include "ui/base/resource/resource_bundle.h" |
| 38 #endif |
| 39 |
| 33 #if defined(OS_IOS) | 40 #if defined(OS_IOS) |
| 34 #include "components/ntp_tiles/country_code_ios.h" | 41 #include "components/ntp_tiles/country_code_ios.h" |
| 35 #endif | 42 #endif |
| 36 | 43 |
| 37 using net::URLFetcher; | 44 using net::URLFetcher; |
| 38 using variations::VariationsService; | 45 using variations::VariationsService; |
| 39 | 46 |
| 40 namespace ntp_tiles { | 47 namespace ntp_tiles { |
| 41 | 48 |
| 42 namespace { | 49 namespace { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 item->GetString("thumbnail_url", &thumbnail_url); | 125 item->GetString("thumbnail_url", &thumbnail_url); |
| 119 std::string large_icon_url; | 126 std::string large_icon_url; |
| 120 item->GetString("large_icon_url", &large_icon_url); | 127 item->GetString("large_icon_url", &large_icon_url); |
| 121 | 128 |
| 122 sites.emplace_back(title, GURL(url), GURL(favicon_url), | 129 sites.emplace_back(title, GURL(url), GURL(favicon_url), |
| 123 GURL(large_icon_url), GURL(thumbnail_url)); | 130 GURL(large_icon_url), GURL(thumbnail_url)); |
| 124 } | 131 } |
| 125 return sites; | 132 return sites; |
| 126 } | 133 } |
| 127 | 134 |
| 135 // This allows instantiation of PopularSites without registered prefs. |
| 136 // That case can occur during tests. |
| 137 PopularSites::SitesVector GetDefaultFromPrefs(const PrefService& prefs) { |
| 138 if (!ShouldShowPopularSites()) { |
| 139 return PopularSites::SitesVector(); |
| 140 } |
| 141 return ParseSiteList(*prefs.GetList(kPopularSitesJsonPref)); |
| 142 } |
| 143 |
| 144 // Creates the List of popular sites based on a snapshot available for mobile. |
| 145 std::unique_ptr<base::ListValue> DefaultPopularSites() { |
| 146 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 147 std::unique_ptr<base::ListValue> sites = |
| 148 base::ListValue::From(base::JSONReader().ReadToValue( |
| 149 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 150 IDR_DEFAULT_POPULAR_SITES_JSON))); |
| 151 DCHECK(sites); |
| 152 return sites; |
| 153 #endif |
| 154 return base::MakeUnique<base::ListValue>(); |
| 155 } |
| 156 |
| 128 } // namespace | 157 } // namespace |
| 129 | 158 |
| 130 PopularSites::Site::Site(const base::string16& title, | 159 PopularSites::Site::Site(const base::string16& title, |
| 131 const GURL& url, | 160 const GURL& url, |
| 132 const GURL& favicon_url, | 161 const GURL& favicon_url, |
| 133 const GURL& large_icon_url, | 162 const GURL& large_icon_url, |
| 134 const GURL& thumbnail_url) | 163 const GURL& thumbnail_url) |
| 135 : title(title), | 164 : title(title), |
| 136 url(url), | 165 url(url), |
| 137 favicon_url(favicon_url), | 166 favicon_url(favicon_url), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 151 const base::FilePath& directory, | 180 const base::FilePath& directory, |
| 152 ParseJSONCallback parse_json) | 181 ParseJSONCallback parse_json) |
| 153 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( | 182 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( |
| 154 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | 183 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), |
| 155 prefs_(prefs), | 184 prefs_(prefs), |
| 156 template_url_service_(template_url_service), | 185 template_url_service_(template_url_service), |
| 157 variations_(variations_service), | 186 variations_(variations_service), |
| 158 download_context_(download_context), | 187 download_context_(download_context), |
| 159 parse_json_(std::move(parse_json)), | 188 parse_json_(std::move(parse_json)), |
| 160 is_fallback_(false), | 189 is_fallback_(false), |
| 190 sites_(GetDefaultFromPrefs(*prefs_)), |
| 161 weak_ptr_factory_(this) { | 191 weak_ptr_factory_(this) { |
| 162 // If valid path provided, remove local files created by older versions. | 192 // If valid path provided, remove local files created by older versions. |
| 163 if (!directory.empty() && blocking_runner_) { | 193 if (!directory.empty() && blocking_runner_) { |
| 164 blocking_runner_->PostTask( | 194 blocking_runner_->PostTask( |
| 165 FROM_HERE, | 195 FROM_HERE, |
| 166 base::Bind(base::IgnoreResult(&base::DeleteFile), | 196 base::Bind(base::IgnoreResult(&base::DeleteFile), |
| 167 directory.AppendASCII(kPopularSitesLocalFilenameToCleanup), | 197 directory.AppendASCII(kPopularSitesLocalFilenameToCleanup), |
| 168 /*recursive=*/false)); | 198 /*recursive=*/false)); |
| 169 } | 199 } |
| 170 } | 200 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 187 pending_url_ = GetURLToFetch(); | 217 pending_url_ = GetURLToFetch(); |
| 188 const bool url_changed = | 218 const bool url_changed = |
| 189 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); | 219 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); |
| 190 | 220 |
| 191 // Download forced, or we need to download a new file. | 221 // Download forced, or we need to download a new file. |
| 192 if (force_download || download_time_is_future || | 222 if (force_download || download_time_is_future || |
| 193 (time_since_last_download > redownload_interval) || url_changed) { | 223 (time_since_last_download > redownload_interval) || url_changed) { |
| 194 FetchPopularSites(); | 224 FetchPopularSites(); |
| 195 return true; | 225 return true; |
| 196 } | 226 } |
| 197 | 227 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 } | 228 } |
| 209 | 229 |
| 210 const PopularSites::SitesVector& PopularSitesImpl::sites() const { | 230 const PopularSites::SitesVector& PopularSitesImpl::sites() const { |
| 211 return sites_; | 231 return sites_; |
| 212 } | 232 } |
| 213 | 233 |
| 214 GURL PopularSitesImpl::GetLastURLFetched() const { | 234 GURL PopularSitesImpl::GetLastURLFetched() const { |
| 215 return GURL(prefs_->GetString(kPopularSitesURLPref)); | 235 return GURL(prefs_->GetString(kPopularSitesURLPref)); |
| 216 } | 236 } |
| 217 | 237 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 user_prefs::PrefRegistrySyncable* user_prefs) { | 302 user_prefs::PrefRegistrySyncable* user_prefs) { |
| 283 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, | 303 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| 284 std::string()); | 304 std::string()); |
| 285 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 305 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, |
| 286 std::string()); | 306 std::string()); |
| 287 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, | 307 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, |
| 288 std::string()); | 308 std::string()); |
| 289 | 309 |
| 290 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 310 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
| 291 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); | 311 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); |
| 292 user_prefs->RegisterListPref(kPopularSitesJsonPref); | 312 user_prefs->RegisterListPref(kPopularSitesJsonPref, |
| 313 DefaultPopularSites().release()); |
| 293 } | 314 } |
| 294 | 315 |
| 295 void PopularSitesImpl::FetchPopularSites() { | 316 void PopularSitesImpl::FetchPopularSites() { |
| 296 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this); | 317 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this); |
| 297 data_use_measurement::DataUseUserData::AttachToFetcher( | 318 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 298 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES); | 319 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES); |
| 299 fetcher_->SetRequestContext(download_context_); | 320 fetcher_->SetRequestContext(download_context_); |
| 300 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 321 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 301 net::LOAD_DO_NOT_SAVE_COOKIES); | 322 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 302 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | 323 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, | 372 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, |
| 352 kPopularSitesDefaultVersion); | 373 kPopularSitesDefaultVersion); |
| 353 FetchPopularSites(); | 374 FetchPopularSites(); |
| 354 } else { | 375 } else { |
| 355 DLOG(WARNING) << "Download fallback site list failed"; | 376 DLOG(WARNING) << "Download fallback site list failed"; |
| 356 callback_.Run(false); | 377 callback_.Run(false); |
| 357 } | 378 } |
| 358 } | 379 } |
| 359 | 380 |
| 360 } // namespace ntp_tiles | 381 } // namespace ntp_tiles |
| OLD | NEW |