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

Unified Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1737563003: [NTP Snippets] Persist the set of snippets in a pref (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_merge
Patch Set: . Created 4 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_snippets/ntp_snippets_service.cc
diff --git a/components/ntp_snippets/ntp_snippets_service.cc b/components/ntp_snippets/ntp_snippets_service.cc
index e540f73b87e4975a19e02a392fbd943050282350..b3c5522c8b805982662674110f12d7a6637ba9ad 100644
--- a/components/ntp_snippets/ntp_snippets_service.cc
+++ b/components/ntp_snippets/ntp_snippets_service.cc
@@ -11,9 +11,14 @@
#include "base/path_service.h"
#include "base/task_runner_util.h"
#include "base/values.h"
+#include "components/pref_registry/pref_registry_syncable.h"
+#include "components/prefs/pref_service.h"
+#include "components/prefs/scoped_user_pref_update.h"
namespace {
+const char kSnippetsPref[] = "ntp_snippets.snippets";
Bernhard Bauer 2016/02/25 17:16:33 This should be defined in pref_names.{h,cc} files
Marc Treib 2016/02/26 17:06:12 Done.
+
// TODO(crbug.com/587857): This is an extremely small value, for development.
// Replace it by something sensible and add a command line param to control it.
const int kDefaultFetchingIntervalSeconds = 60;
@@ -30,11 +35,13 @@ bool ReadFileToString(const base::FilePath& path, std::string* data) {
namespace ntp_snippets {
NTPSnippetsService::NTPSnippetsService(
+ PrefService* pref_service,
scoped_refptr<base::SequencedTaskRunner> file_task_runner,
const std::string& application_language_code,
NTPSnippetsScheduler* scheduler,
scoped_ptr<NTPSnippetsFetcher> snippets_fetcher)
- : loaded_(false),
+ : pref_service_(pref_service),
+ loaded_(false),
file_task_runner_(file_task_runner),
application_language_code_(application_language_code),
scheduler_(scheduler),
@@ -47,11 +54,21 @@ NTPSnippetsService::NTPSnippetsService(
NTPSnippetsService::~NTPSnippetsService() {}
+// static
+void NTPSnippetsService::RegisterProfilePrefs(
+ user_prefs::PrefRegistrySyncable* registry) {
+ registry->RegisterListPref(kSnippetsPref);
+}
+
void NTPSnippetsService::Init(bool enabled) {
- // If enabled, get snippets immediately. If we've downloaded them before,
- // this will just read from disk.
- if (enabled)
- FetchSnippets(false);
+ // If enabled, get any existing snippets immediately from prefs.
+ if (enabled) {
+ LoadFromPrefs();
+
+ // If we don't have any snippets yet, start a fetch.
+ if (snippets_.empty())
+ FetchSnippets(false);
+ }
// The scheduler only exists on Android so far, it's null on other platforms.
if (!scheduler_)
@@ -83,12 +100,23 @@ void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
observers_.RemoveObserver(observer);
}
+void NTPSnippetsService::OnSnippetsDownloaded(
+ const base::FilePath& download_path) {
+ std::string* downloaded_data = new std::string;
+ base::PostTaskAndReplyWithResult(
+ file_task_runner_.get(), FROM_HERE,
+ base::Bind(&ReadFileToString, download_path, downloaded_data),
+ base::Bind(&NTPSnippetsService::OnFileReadDone,
+ weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data)));
+}
+
void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) {
if (!success)
return;
DCHECK(json);
LoadFromJSONString(*json);
+ StoreToPrefs();
}
bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
@@ -104,7 +132,11 @@ bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
if (!top_dict->GetList("recos", &list))
return false;
- for (const base::Value* const value : *list) {
+ return LoadFromJSONList(*list);
+}
+
+bool NTPSnippetsService::LoadFromJSONList(const base::ListValue& list) {
+ for (const base::Value* const value : list) {
const base::DictionaryValue* dict = nullptr;
if (!value->GetAsDictionary(&dict))
return false;
@@ -112,8 +144,7 @@ bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
const base::DictionaryValue* content = nullptr;
if (!dict->GetDictionary("contentInfo", &content))
return false;
- scoped_ptr<NTPSnippet> snippet =
- NTPSnippet::NTPSnippetFromDictionary(*content);
+ scoped_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromDictionary(*content);
if (!snippet)
return false;
@@ -136,14 +167,24 @@ bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
return true;
}
-void NTPSnippetsService::OnSnippetsDownloaded(
- const base::FilePath& download_path) {
- std::string* downloaded_data = new std::string;
- base::PostTaskAndReplyWithResult(
- file_task_runner_.get(), FROM_HERE,
- base::Bind(&ReadFileToString, download_path, downloaded_data),
- base::Bind(&NTPSnippetsService::OnFileReadDone,
- weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data)));
+bool NTPSnippetsService::LoadFromPrefs() {
+ // |pref_service_| can be null in tests.
+ if (!pref_service_)
+ return false;
Bernhard Bauer 2016/02/25 17:16:33 The return value seems unused.
Marc Treib 2016/02/26 17:06:12 I've removed the return value, and instead added a
+ return LoadFromJSONList(*pref_service_->GetList(kSnippetsPref));
+}
+
+void NTPSnippetsService::StoreToPrefs() {
+ // |pref_service_| can be null in tests.
+ if (!pref_service_)
+ return;
+ ListPrefUpdate update(pref_service_, kSnippetsPref);
+ update->Clear();
Bernhard Bauer 2016/02/25 17:16:33 Instead of doing this, just build the new list and
Marc Treib 2016/02/26 17:06:12 Done.
+ for (const auto& snippet : snippets_) {
+ scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
+ dict->Set("contentInfo", snippet->ToDictionary());
+ update->Append(std::move(dict));
+ }
}
} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698