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..66a923fe1e367cdc84df1e3f5a7338e637c392e3 100644 |
--- a/components/ntp_snippets/ntp_snippets_service.cc |
+++ b/components/ntp_snippets/ntp_snippets_service.cc |
@@ -4,14 +4,37 @@ |
#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); |
+ if (!success) |
+ DLOG(ERROR) << "Error reading file " << path.LossyDisplayName(); |
noyau (Ping after 24h)
2016/02/23 14:23:45
I'm not sure this is correct. In a release build t
Marc Treib
2016/02/23 14:29:50
I'm fairly sure this works, but we do have DLOG_IF
Bernhard Bauer
2016/02/23 14:32:27
(Note that the DLOG isn't completely compiled out
blundell
2016/02/23 14:37:48
<drive-by> As is, this code seems dangerous: in th
May
2016/02/23 21:34:18
Good point. It's definitely a legit operation to f
May
2016/02/23 21:34:18
I initially had DLOG_IF, but I was only using the
|
+ 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 +44,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 +58,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 +88,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 |