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 std::unique_ptr<base::ListValue> DefaultPopularSites() { |
| 132 std::unique_ptr<base::ListValue> sites = |
| 133 base::ListValue::From(base::JSONReader().ReadToValue( |
| 134 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 135 IDR_DEFAULT_POPULAR_SITES_JSON))); |
| 136 DCHECK(sites); |
| 137 return sites; |
| 138 } |
| 139 |
128 } // namespace | 140 } // namespace |
129 | 141 |
130 PopularSites::Site::Site(const base::string16& title, | 142 PopularSites::Site::Site(const base::string16& title, |
131 const GURL& url, | 143 const GURL& url, |
132 const GURL& favicon_url, | 144 const GURL& favicon_url, |
133 const GURL& large_icon_url, | 145 const GURL& large_icon_url, |
134 const GURL& thumbnail_url) | 146 const GURL& thumbnail_url) |
135 : title(title), | 147 : title(title), |
136 url(url), | 148 url(url), |
137 favicon_url(favicon_url), | 149 favicon_url(favicon_url), |
(...skipping 13 matching lines...) Expand all Loading... |
151 const base::FilePath& directory, | 163 const base::FilePath& directory, |
152 ParseJSONCallback parse_json) | 164 ParseJSONCallback parse_json) |
153 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( | 165 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( |
154 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | 166 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), |
155 prefs_(prefs), | 167 prefs_(prefs), |
156 template_url_service_(template_url_service), | 168 template_url_service_(template_url_service), |
157 variations_(variations_service), | 169 variations_(variations_service), |
158 download_context_(download_context), | 170 download_context_(download_context), |
159 parse_json_(std::move(parse_json)), | 171 parse_json_(std::move(parse_json)), |
160 is_fallback_(false), | 172 is_fallback_(false), |
| 173 sites_(ParseSiteList(*prefs_->GetList(kPopularSitesJsonPref))), |
161 weak_ptr_factory_(this) { | 174 weak_ptr_factory_(this) { |
162 // If valid path provided, remove local files created by older versions. | 175 // If valid path provided, remove local files created by older versions. |
163 if (!directory.empty() && blocking_runner_) { | 176 if (!directory.empty() && blocking_runner_) { |
164 blocking_runner_->PostTask( | 177 blocking_runner_->PostTask( |
165 FROM_HERE, | 178 FROM_HERE, |
166 base::Bind(base::IgnoreResult(&base::DeleteFile), | 179 base::Bind(base::IgnoreResult(&base::DeleteFile), |
167 directory.AppendASCII(kPopularSitesLocalFilenameToCleanup), | 180 directory.AppendASCII(kPopularSitesLocalFilenameToCleanup), |
168 /*recursive=*/false)); | 181 /*recursive=*/false)); |
169 } | 182 } |
170 } | 183 } |
(...skipping 16 matching lines...) Expand all Loading... |
187 pending_url_ = GetURLToFetch(); | 200 pending_url_ = GetURLToFetch(); |
188 const bool url_changed = | 201 const bool url_changed = |
189 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); | 202 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); |
190 | 203 |
191 // Download forced, or we need to download a new file. | 204 // Download forced, or we need to download a new file. |
192 if (force_download || download_time_is_future || | 205 if (force_download || download_time_is_future || |
193 (time_since_last_download > redownload_interval) || url_changed) { | 206 (time_since_last_download > redownload_interval) || url_changed) { |
194 FetchPopularSites(); | 207 FetchPopularSites(); |
195 return true; | 208 return true; |
196 } | 209 } |
197 | 210 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 } | 211 } |
209 | 212 |
210 const PopularSites::SitesVector& PopularSitesImpl::sites() const { | 213 const PopularSites::SitesVector& PopularSitesImpl::sites() const { |
211 return sites_; | 214 return sites_; |
212 } | 215 } |
213 | 216 |
214 GURL PopularSitesImpl::GetLastURLFetched() const { | 217 GURL PopularSitesImpl::GetLastURLFetched() const { |
215 return GURL(prefs_->GetString(kPopularSitesURLPref)); | 218 return GURL(prefs_->GetString(kPopularSitesURLPref)); |
216 } | 219 } |
217 | 220 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 user_prefs::PrefRegistrySyncable* user_prefs) { | 285 user_prefs::PrefRegistrySyncable* user_prefs) { |
283 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, | 286 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, |
284 std::string()); | 287 std::string()); |
285 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 288 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, |
286 std::string()); | 289 std::string()); |
287 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, | 290 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, |
288 std::string()); | 291 std::string()); |
289 | 292 |
290 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 293 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
291 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); | 294 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); |
292 user_prefs->RegisterListPref(kPopularSitesJsonPref); | 295 user_prefs->RegisterListPref(kPopularSitesJsonPref, |
| 296 DefaultPopularSites().release()); |
293 } | 297 } |
294 | 298 |
295 void PopularSitesImpl::FetchPopularSites() { | 299 void PopularSitesImpl::FetchPopularSites() { |
296 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this); | 300 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this); |
297 data_use_measurement::DataUseUserData::AttachToFetcher( | 301 data_use_measurement::DataUseUserData::AttachToFetcher( |
298 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES); | 302 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES); |
299 fetcher_->SetRequestContext(download_context_); | 303 fetcher_->SetRequestContext(download_context_); |
300 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 304 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
301 net::LOAD_DO_NOT_SAVE_COOKIES); | 305 net::LOAD_DO_NOT_SAVE_COOKIES); |
302 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | 306 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, | 355 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, |
352 kPopularSitesDefaultVersion); | 356 kPopularSitesDefaultVersion); |
353 FetchPopularSites(); | 357 FetchPopularSites(); |
354 } else { | 358 } else { |
355 DLOG(WARNING) << "Download fallback site list failed"; | 359 DLOG(WARNING) << "Download fallback site list failed"; |
356 callback_.Run(false); | 360 callback_.Run(false); |
357 } | 361 } |
358 } | 362 } |
359 | 363 |
360 } // namespace ntp_tiles | 364 } // namespace ntp_tiles |
OLD | NEW |