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..3271af1b496d68392bd0f1e0c7c648247462e920 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 { |
+std::string ReadFileToString(const base::FilePath& path) { |
+ std::string data; |
+ bool success = base::ReadFileToString(path, &data); |
+ DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName(); |
noyau (Ping after 24h)
2016/02/17 20:19:47
Use DCHECK instead, as I think this will use the |
May
2016/02/19 14:49:01
Modified.
|
+ return data; |
+} |
+ |
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,10 @@ void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
observers_.RemoveObserver(observer); |
} |
+void NTPSnippetsService::OnFileReadDone(const std::string& json) { |
+ LoadFromJSONString(json); |
+} |
+ |
bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { |
JSONStringValueDeserializer deserializer(str); |
int error_code; |
@@ -57,16 +87,26 @@ 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) { |
+ base::PostTaskAndReplyWithResult( |
+ file_task_runner_.get(), FROM_HERE, |
+ base::Bind(&ReadFileToString, download_path), |
+ base::Bind(&NTPSnippetsService::OnFileReadDone, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
} // namespace ntp_snippets |