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

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

Issue 2668943002: provide static popular sites for first run (Closed)
Patch Set: Revert test changes. Construction of PopularSites is always safe. Created 3 years, 10 months 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_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
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 PopularSites::SitesVector GetDefaultFromPrefs(const PrefService& prefs) {
132 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
133 if (command_line->HasSwitch(ntp_tiles::switches::kDisableNTPPopularSites)) {
sfiera 2017/02/13 10:58:01 This check is not sufficient; use field_trial.h.
fhorschig 2017/02/13 14:57:00 Done.
sfiera 2017/02/13 15:05:54 If the test is going to instantiate the class, it
fhorschig 2017/02/13 18:13:44 Done.
134 return PopularSites::SitesVector();
135 }
136 return ParseSiteList(*prefs.GetList(kPopularSitesJsonPref));
137 }
138
139 // Creates the List of popular sites based on a snapshot available for mobile.
140 std::unique_ptr<base::ListValue> DefaultPopularSites() {
141 #if defined(OS_IOS) || defined(OS_ANDROID)
sfiera 2017/02/13 10:58:01 Alphabetically, Android is before iOS. Just sayin'
fhorschig 2017/02/13 14:57:00 Done.
142 std::unique_ptr<base::ListValue> sites =
143 base::ListValue::From(base::JSONReader().ReadToValue(
144 ResourceBundle::GetSharedInstance().GetRawDataResource(
145 IDR_DEFAULT_POPULAR_SITES_JSON)));
146 DCHECK(sites);
147 return sites;
148 #endif
149 return base::MakeUnique<base::ListValue>();
150 }
151
128 } // namespace 152 } // namespace
129 153
130 PopularSites::Site::Site(const base::string16& title, 154 PopularSites::Site::Site(const base::string16& title,
131 const GURL& url, 155 const GURL& url,
132 const GURL& favicon_url, 156 const GURL& favicon_url,
133 const GURL& large_icon_url, 157 const GURL& large_icon_url,
134 const GURL& thumbnail_url) 158 const GURL& thumbnail_url)
135 : title(title), 159 : title(title),
136 url(url), 160 url(url),
137 favicon_url(favicon_url), 161 favicon_url(favicon_url),
(...skipping 13 matching lines...) Expand all
151 const base::FilePath& directory, 175 const base::FilePath& directory,
152 ParseJSONCallback parse_json) 176 ParseJSONCallback parse_json)
153 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( 177 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior(
154 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), 178 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)),
155 prefs_(prefs), 179 prefs_(prefs),
156 template_url_service_(template_url_service), 180 template_url_service_(template_url_service),
157 variations_(variations_service), 181 variations_(variations_service),
158 download_context_(download_context), 182 download_context_(download_context),
159 parse_json_(std::move(parse_json)), 183 parse_json_(std::move(parse_json)),
160 is_fallback_(false), 184 is_fallback_(false),
185 sites_(GetDefaultFromPrefs(*prefs_)),
161 weak_ptr_factory_(this) { 186 weak_ptr_factory_(this) {
162 // If valid path provided, remove local files created by older versions. 187 // If valid path provided, remove local files created by older versions.
163 if (!directory.empty() && blocking_runner_) { 188 if (!directory.empty() && blocking_runner_) {
164 blocking_runner_->PostTask( 189 blocking_runner_->PostTask(
165 FROM_HERE, 190 FROM_HERE,
166 base::Bind(base::IgnoreResult(&base::DeleteFile), 191 base::Bind(base::IgnoreResult(&base::DeleteFile),
167 directory.AppendASCII(kPopularSitesLocalFilenameToCleanup), 192 directory.AppendASCII(kPopularSitesLocalFilenameToCleanup),
168 /*recursive=*/false)); 193 /*recursive=*/false));
169 } 194 }
170 } 195 }
(...skipping 16 matching lines...) Expand all
187 pending_url_ = GetURLToFetch(); 212 pending_url_ = GetURLToFetch();
188 const bool url_changed = 213 const bool url_changed =
189 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); 214 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref);
190 215
191 // Download forced, or we need to download a new file. 216 // Download forced, or we need to download a new file.
192 if (force_download || download_time_is_future || 217 if (force_download || download_time_is_future ||
193 (time_since_last_download > redownload_interval) || url_changed) { 218 (time_since_last_download > redownload_interval) || url_changed) {
194 FetchPopularSites(); 219 FetchPopularSites();
195 return true; 220 return true;
196 } 221 }
197 222 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 } 223 }
209 224
210 const PopularSites::SitesVector& PopularSitesImpl::sites() const { 225 const PopularSites::SitesVector& PopularSitesImpl::sites() const {
211 return sites_; 226 return sites_;
212 } 227 }
213 228
214 GURL PopularSitesImpl::GetLastURLFetched() const { 229 GURL PopularSitesImpl::GetLastURLFetched() const {
215 return GURL(prefs_->GetString(kPopularSitesURLPref)); 230 return GURL(prefs_->GetString(kPopularSitesURLPref));
216 } 231 }
217 232
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 user_prefs::PrefRegistrySyncable* user_prefs) { 297 user_prefs::PrefRegistrySyncable* user_prefs) {
283 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, 298 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL,
284 std::string()); 299 std::string());
285 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, 300 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry,
286 std::string()); 301 std::string());
287 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, 302 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion,
288 std::string()); 303 std::string());
289 304
290 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); 305 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0);
291 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); 306 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string());
292 user_prefs->RegisterListPref(kPopularSitesJsonPref); 307 user_prefs->RegisterListPref(kPopularSitesJsonPref,
308 DefaultPopularSites().release());
293 } 309 }
294 310
295 void PopularSitesImpl::FetchPopularSites() { 311 void PopularSitesImpl::FetchPopularSites() {
296 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this); 312 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this);
297 data_use_measurement::DataUseUserData::AttachToFetcher( 313 data_use_measurement::DataUseUserData::AttachToFetcher(
298 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES); 314 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES);
299 fetcher_->SetRequestContext(download_context_); 315 fetcher_->SetRequestContext(download_context_);
300 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 316 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
301 net::LOAD_DO_NOT_SAVE_COOKIES); 317 net::LOAD_DO_NOT_SAVE_COOKIES);
302 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); 318 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, 367 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode,
352 kPopularSitesDefaultVersion); 368 kPopularSitesDefaultVersion);
353 FetchPopularSites(); 369 FetchPopularSites();
354 } else { 370 } else {
355 DLOG(WARNING) << "Download fallback site list failed"; 371 DLOG(WARNING) << "Download fallback site list failed";
356 callback_.Run(false); 372 callback_.Run(false);
357 } 373 }
358 } 374 }
359 375
360 } // namespace ntp_tiles 376 } // namespace ntp_tiles
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698