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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
7 #include "base/json/json_string_value_serializer.h" 9 #include "base/json/json_string_value_serializer.h"
10 #include "base/location.h"
11 #include "base/path_service.h"
12 #include "base/task_runner_util.h"
8 #include "base/values.h" 13 #include "base/values.h"
9 14
10 namespace ntp_snippets { 15 namespace ntp_snippets {
11 16
17 bool ReadFileToString(const base::FilePath& path, std::string* data) {
18 DCHECK(data);
19 bool success = base::ReadFileToString(path, data);
20 DLOG_IF(ERROR, !success) << "Error reading file " << path.LossyDisplayName();
21 return success;
22 }
23
12 NTPSnippetsService::NTPSnippetsService( 24 NTPSnippetsService::NTPSnippetsService(
13 const std::string& application_language_code) 25 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
14 : loaded_(false), application_language_code_(application_language_code) {} 26 const std::string& application_language_code,
27 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher)
28 : loaded_(false),
29 file_task_runner_(file_task_runner),
30 application_language_code_(application_language_code),
31 snippets_fetcher_(std::move(snippets_fetcher)),
32 weak_ptr_factory_(this) {
33 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback(
34 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded,
35 weak_ptr_factory_.GetWeakPtr()));
36 }
15 37
16 NTPSnippetsService::~NTPSnippetsService() {} 38 NTPSnippetsService::~NTPSnippetsService() {}
17 39
18 void NTPSnippetsService::Shutdown() { 40 void NTPSnippetsService::Shutdown() {
19 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 41 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
20 NTPSnippetsServiceShutdown(this)); 42 NTPSnippetsServiceShutdown(this));
21 loaded_ = false; 43 loaded_ = false;
22 } 44 }
23 45
46 void NTPSnippetsService::FetchSnippets(bool overwrite) {
47 snippets_fetcher_->FetchSnippets(overwrite);
48 }
49
24 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { 50 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) {
25 observers_.AddObserver(observer); 51 observers_.AddObserver(observer);
26 if (loaded_) 52 if (loaded_)
27 observer->NTPSnippetsServiceLoaded(this); 53 observer->NTPSnippetsServiceLoaded(this);
28 } 54 }
29 55
30 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { 56 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) {
31 observers_.RemoveObserver(observer); 57 observers_.RemoveObserver(observer);
32 } 58 }
33 59
60 void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) {
61 if (!success)
62 return;
63
64 DCHECK(json);
65 LoadFromJSONString(*json);
66 }
67
34 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { 68 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
35 JSONStringValueDeserializer deserializer(str); 69 JSONStringValueDeserializer deserializer(str);
36 int error_code; 70 int error_code;
37 std::string error_message; 71 std::string error_message;
38 72
39 scoped_ptr<base::Value> deserialized = 73 scoped_ptr<base::Value> deserialized =
40 deserializer.Deserialize(&error_code, &error_message); 74 deserializer.Deserialize(&error_code, &error_message);
41 if (!deserialized) 75 if (!deserialized)
42 return false; 76 return false;
43 77
44 const base::DictionaryValue* top_dict = NULL; 78 const base::DictionaryValue* top_dict = NULL;
45 if (!deserialized->GetAsDictionary(&top_dict)) 79 if (!deserialized->GetAsDictionary(&top_dict))
46 return false; 80 return false;
47 81
48 const base::ListValue* list = NULL; 82 const base::ListValue* list = NULL;
49 if (!top_dict->GetList("recos", &list)) 83 if (!top_dict->GetList("recos", &list))
50 return false; 84 return false;
51 85
52 for (base::Value* const value : *list) { 86 for (base::Value* const value : *list) {
53 const base::DictionaryValue* dict = NULL; 87 const base::DictionaryValue* dict = NULL;
54 if (!value->GetAsDictionary(&dict)) 88 if (!value->GetAsDictionary(&dict))
55 return false; 89 return false;
56 90
57 const base::DictionaryValue* content = NULL; 91 const base::DictionaryValue* content = NULL;
58 if (!dict->GetDictionary("contentInfo", &content)) 92 if (!dict->GetDictionary("contentInfo", &content))
59 return false; 93 return false;
60 std::unique_ptr<NTPSnippet> snippet = 94 scoped_ptr<NTPSnippet> snippet =
61 NTPSnippet::NTPSnippetFromDictionary(*content); 95 NTPSnippet::NTPSnippetFromDictionary(*content);
62 if (!snippet) 96 if (!snippet)
63 return false; 97 return false;
64 snippets_.push_back(std::move(snippet)); 98 snippets_.push_back(std::move(snippet));
65 } 99 }
66 loaded_ = true; 100 loaded_ = true;
101
67 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 102 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
68 NTPSnippetsServiceLoaded(this)); 103 NTPSnippetsServiceLoaded(this));
69 return true; 104 return true;
70 } 105 }
71 106
107 void NTPSnippetsService::OnSnippetsDownloaded(
108 const base::FilePath& download_path) {
109 std::string* downloaded_data = new std::string;
110 base::PostTaskAndReplyWithResult(
111 file_task_runner_.get(), FROM_HERE,
112 base::Bind(&ReadFileToString, download_path, downloaded_data),
113 base::Bind(&NTPSnippetsService::OnFileReadDone,
114 weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data)));
115 }
116
72 } // namespace ntp_snippets 117 } // namespace ntp_snippets
OLDNEW
« 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