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

Unified Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1677073002: Fetch snippets from ChromeReader and show them on the NTP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments 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 f3c0670f7af808486dbfd6cd80572c73bfc971f5..149ab4a866ab13b47e801e59ce2b7a087006e97f 100644
--- a/components/ntp_snippets/ntp_snippets_service.cc
+++ b/components/ntp_snippets/ntp_snippets_service.cc
@@ -4,14 +4,36 @@
#include "components/ntp_snippets/ntp_snippets_service.h"
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
#include "base/json/json_string_value_serializer.h"
+#include "base/location.h"
+#include "base/path_service.h"
+#include "base/task_runner_util.h"
#include "base/values.h"
namespace ntp_snippets {
+bool ReadFileToString(const base::FilePath& path, std::string* data) {
+ DCHECK(data);
+ bool success = base::ReadFileToString(path, data);
+ DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName();
+ return success;
+}
+
NTPSnippetsService::NTPSnippetsService(
- const std::string& application_language_code)
- : loaded_(false), application_language_code_(application_language_code) {}
+ scoped_refptr<base::SequencedTaskRunner> file_task_runner,
+ const std::string& application_language_code,
+ scoped_ptr<NTPSnippetsFetcher> snippets_fetcher)
+ : loaded_(false),
+ file_task_runner_(file_task_runner),
+ application_language_code_(application_language_code),
+ snippets_fetcher_(std::move(snippets_fetcher)),
+ weak_ptr_factory_(this) {
+ snippets_fetcher_callback_ = snippets_fetcher_->AddCallback(
+ base::Bind(&NTPSnippetsService::OnSnippetsDownloaded,
+ weak_ptr_factory_.GetWeakPtr()));
+}
NTPSnippetsService::~NTPSnippetsService() {}
@@ -21,6 +43,10 @@ void NTPSnippetsService::Shutdown() {
loaded_ = false;
}
+void NTPSnippetsService::FetchSnippets(bool overwrite) {
+ snippets_fetcher_->FetchSnippets(overwrite);
+}
+
void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) {
observers_.AddObserver(observer);
if (loaded_)
@@ -31,6 +57,14 @@ void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
observers_.RemoveObserver(observer);
}
+void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) {
+ if (!success)
+ return;
+
+ DCHECK(json);
+ LoadFromJSONString(*json);
+}
+
bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
JSONStringValueDeserializer deserializer(str);
int error_code;
@@ -57,16 +91,27 @@ bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
const base::DictionaryValue* content = NULL;
if (!dict->GetDictionary("contentInfo", &content))
return false;
- std::unique_ptr<NTPSnippet> snippet =
+ scoped_ptr<NTPSnippet> snippet =
NTPSnippet::NTPSnippetFromDictionary(*content);
if (!snippet)
return false;
snippets_.push_back(std::move(snippet));
}
loaded_ = true;
+
FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
NTPSnippetsServiceLoaded(this));
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)));
+}
+
} // namespace ntp_snippets
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698