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

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

Issue 2668943002: provide static popular sites for first run (Closed)
Patch Set: Clean build/DEPS files 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/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
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 // Creates the list of popular sites based on a snapshot available for mobile.
136 std::unique_ptr<base::ListValue> DefaultPopularSites() {
137 #if defined(OS_ANDROID) || defined(OS_IOS)
138 std::unique_ptr<base::ListValue> sites =
139 base::ListValue::From(base::JSONReader().ReadToValue(
140 ResourceBundle::GetSharedInstance().GetRawDataResource(
141 IDR_DEFAULT_POPULAR_SITES_JSON)));
142 DCHECK(sites);
143 return sites;
144 #endif
145 return base::MakeUnique<base::ListValue>();
146 }
147
128 } // namespace 148 } // namespace
129 149
130 PopularSites::Site::Site(const base::string16& title, 150 PopularSites::Site::Site(const base::string16& title,
131 const GURL& url, 151 const GURL& url,
132 const GURL& favicon_url, 152 const GURL& favicon_url,
133 const GURL& large_icon_url, 153 const GURL& large_icon_url,
134 const GURL& thumbnail_url) 154 const GURL& thumbnail_url)
135 : title(title), 155 : title(title),
136 url(url), 156 url(url),
137 favicon_url(favicon_url), 157 favicon_url(favicon_url),
(...skipping 13 matching lines...) Expand all
151 const base::FilePath& directory, 171 const base::FilePath& directory,
152 ParseJSONCallback parse_json) 172 ParseJSONCallback parse_json)
153 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( 173 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior(
154 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), 174 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)),
155 prefs_(prefs), 175 prefs_(prefs),
156 template_url_service_(template_url_service), 176 template_url_service_(template_url_service),
157 variations_(variations_service), 177 variations_(variations_service),
158 download_context_(download_context), 178 download_context_(download_context),
159 parse_json_(std::move(parse_json)), 179 parse_json_(std::move(parse_json)),
160 is_fallback_(false), 180 is_fallback_(false),
181 sites_(ParseSiteList(*prefs->GetList(kPopularSitesJsonPref))),
161 weak_ptr_factory_(this) { 182 weak_ptr_factory_(this) {
162 // If valid path provided, remove local files created by older versions. 183 // If valid path provided, remove local files created by older versions.
163 if (!directory.empty() && blocking_runner_) { 184 if (!directory.empty() && blocking_runner_) {
164 blocking_runner_->PostTask( 185 blocking_runner_->PostTask(
165 FROM_HERE, 186 FROM_HERE,
166 base::Bind(base::IgnoreResult(&base::DeleteFile), 187 base::Bind(base::IgnoreResult(&base::DeleteFile),
167 directory.AppendASCII(kPopularSitesLocalFilenameToCleanup), 188 directory.AppendASCII(kPopularSitesLocalFilenameToCleanup),
168 /*recursive=*/false)); 189 /*recursive=*/false));
169 } 190 }
170 } 191 }
(...skipping 16 matching lines...) Expand all
187 pending_url_ = GetURLToFetch(); 208 pending_url_ = GetURLToFetch();
188 const bool url_changed = 209 const bool url_changed =
189 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); 210 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref);
190 211
191 // Download forced, or we need to download a new file. 212 // Download forced, or we need to download a new file.
192 if (force_download || download_time_is_future || 213 if (force_download || download_time_is_future ||
193 (time_since_last_download > redownload_interval) || url_changed) { 214 (time_since_last_download > redownload_interval) || url_changed) {
194 FetchPopularSites(); 215 FetchPopularSites();
195 return true; 216 return true;
196 } 217 }
197 218 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 } 219 }
209 220
210 const PopularSites::SitesVector& PopularSitesImpl::sites() const { 221 const PopularSites::SitesVector& PopularSitesImpl::sites() const {
211 return sites_; 222 return sites_;
212 } 223 }
213 224
214 GURL PopularSitesImpl::GetLastURLFetched() const { 225 GURL PopularSitesImpl::GetLastURLFetched() const {
215 return GURL(prefs_->GetString(kPopularSitesURLPref)); 226 return GURL(prefs_->GetString(kPopularSitesURLPref));
216 } 227 }
217 228
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 user_prefs::PrefRegistrySyncable* user_prefs) { 293 user_prefs::PrefRegistrySyncable* user_prefs) {
283 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, 294 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL,
284 std::string()); 295 std::string());
285 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, 296 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry,
286 std::string()); 297 std::string());
287 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, 298 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion,
288 std::string()); 299 std::string());
289 300
290 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); 301 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0);
291 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); 302 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string());
292 user_prefs->RegisterListPref(kPopularSitesJsonPref); 303 user_prefs->RegisterListPref(kPopularSitesJsonPref,
304 DefaultPopularSites().release());
293 } 305 }
294 306
295 void PopularSitesImpl::FetchPopularSites() { 307 void PopularSitesImpl::FetchPopularSites() {
296 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this); 308 fetcher_ = URLFetcher::Create(pending_url_, URLFetcher::GET, this);
297 data_use_measurement::DataUseUserData::AttachToFetcher( 309 data_use_measurement::DataUseUserData::AttachToFetcher(
298 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES); 310 fetcher_.get(), data_use_measurement::DataUseUserData::NTP_TILES);
299 fetcher_->SetRequestContext(download_context_); 311 fetcher_->SetRequestContext(download_context_);
300 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 312 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
301 net::LOAD_DO_NOT_SAVE_COOKIES); 313 net::LOAD_DO_NOT_SAVE_COOKIES);
302 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); 314 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, 363 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode,
352 kPopularSitesDefaultVersion); 364 kPopularSitesDefaultVersion);
353 FetchPopularSites(); 365 FetchPopularSites();
354 } else { 366 } else {
355 DLOG(WARNING) << "Download fallback site list failed"; 367 DLOG(WARNING) << "Download fallback site list failed";
356 callback_.Run(false); 368 callback_.Run(false);
357 } 369 }
358 } 370 }
359 371
360 } // namespace ntp_tiles 372 } // namespace ntp_tiles
OLDNEW
« no previous file with comments | « components/ntp_tiles/most_visited_sites_unittest.cc ('k') | components/ntp_tiles/popular_sites_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698