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

Unified Diff: components/ntp_tiles/popular_sites_impl.cc

Issue 2668943002: provide static popular sites for first run (Closed)
Patch Set: Add dependencies. Protect access with field_trial 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_tiles/popular_sites_impl.cc
diff --git a/components/ntp_tiles/popular_sites_impl.cc b/components/ntp_tiles/popular_sites_impl.cc
index ce69227f271e9376aba9c43dd4b9217778926d26..34fa0274a06fdae706fd592b9930bb656fe5d22b 100644
--- a/components/ntp_tiles/popular_sites_impl.cc
+++ b/components/ntp_tiles/popular_sites_impl.cc
@@ -19,6 +19,7 @@
#include "components/data_use_measurement/core/data_use_user_data.h"
#include "components/google/core/browser/google_util.h"
#include "components/ntp_tiles/constants.h"
+#include "components/ntp_tiles/field_trial.h"
#include "components/ntp_tiles/pref_names.h"
#include "components/ntp_tiles/switches.h"
#include "components/pref_registry/pref_registry_syncable.h"
@@ -30,6 +31,12 @@
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
+#if defined(OS_ANDROID) || defined(OS_IOS)
+#include "base/json/json_reader.h"
+#include "components/grit/components_resources.h"
+#include "ui/base/resource/resource_bundle.h"
+#endif
+
#if defined(OS_IOS)
#include "components/ntp_tiles/country_code_ios.h"
#endif
@@ -125,6 +132,28 @@ PopularSites::SitesVector ParseSiteList(const base::ListValue& list) {
return sites;
}
+// This allows instantiation of PopularSites without registered prefs.
+// That case can occur during tests.
+PopularSites::SitesVector GetDefaultFromPrefs(const PrefService& prefs) {
+ if (!ShouldShowPopularSites()) {
+ return PopularSites::SitesVector();
+ }
+ return ParseSiteList(*prefs.GetList(kPopularSitesJsonPref));
+}
+
+// Creates the List of popular sites based on a snapshot available for mobile.
+std::unique_ptr<base::ListValue> DefaultPopularSites() {
+#if defined(OS_ANDROID) || defined(OS_IOS)
+ std::unique_ptr<base::ListValue> sites =
+ base::ListValue::From(base::JSONReader().ReadToValue(
+ ResourceBundle::GetSharedInstance().GetRawDataResource(
+ IDR_DEFAULT_POPULAR_SITES_JSON)));
+ DCHECK(sites);
+ return sites;
+#endif
+ return base::MakeUnique<base::ListValue>();
+}
+
} // namespace
PopularSites::Site::Site(const base::string16& title,
@@ -158,6 +187,7 @@ PopularSitesImpl::PopularSitesImpl(
download_context_(download_context),
parse_json_(std::move(parse_json)),
is_fallback_(false),
+ sites_(GetDefaultFromPrefs(*prefs_)),
weak_ptr_factory_(this) {
// If valid path provided, remove local files created by older versions.
if (!directory.empty() && blocking_runner_) {
@@ -194,17 +224,7 @@ bool PopularSitesImpl::MaybeStartFetch(bool force_download,
FetchPopularSites();
return true;
}
-
- const base::ListValue* json = prefs_->GetList(kPopularSitesJsonPref);
- if (!json) {
- // Cache didn't exist.
- FetchPopularSites();
- return true;
- } else {
- // Note that we don't run the callback.
- sites_ = ParseSiteList(*json);
- return false;
- }
+ return false;
}
const PopularSites::SitesVector& PopularSitesImpl::sites() const {
@@ -289,7 +309,8 @@ void PopularSitesImpl::RegisterProfilePrefs(
user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0);
user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string());
- user_prefs->RegisterListPref(kPopularSitesJsonPref);
+ user_prefs->RegisterListPref(kPopularSitesJsonPref,
+ DefaultPopularSites().release());
}
void PopularSitesImpl::FetchPopularSites() {

Powered by Google App Engine
This is Rietveld 408576698