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

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: CR comments, modified iOS NTPSnippetServiceFactory 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 std::string ReadFileToString(const base::FilePath& path) {
18 std::string data;
19 bool success = base::ReadFileToString(path, &data);
20 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.
21 return data;
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) {
61 LoadFromJSONString(json);
62 }
63
34 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { 64 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
35 JSONStringValueDeserializer deserializer(str); 65 JSONStringValueDeserializer deserializer(str);
36 int error_code; 66 int error_code;
37 std::string error_message; 67 std::string error_message;
38 68
39 scoped_ptr<base::Value> deserialized = 69 scoped_ptr<base::Value> deserialized =
40 deserializer.Deserialize(&error_code, &error_message); 70 deserializer.Deserialize(&error_code, &error_message);
41 if (!deserialized) 71 if (!deserialized)
42 return false; 72 return false;
43 73
44 const base::DictionaryValue* top_dict = NULL; 74 const base::DictionaryValue* top_dict = NULL;
45 if (!deserialized->GetAsDictionary(&top_dict)) 75 if (!deserialized->GetAsDictionary(&top_dict))
46 return false; 76 return false;
47 77
48 const base::ListValue* list = NULL; 78 const base::ListValue* list = NULL;
49 if (!top_dict->GetList("recos", &list)) 79 if (!top_dict->GetList("recos", &list))
50 return false; 80 return false;
51 81
52 for (base::Value* const value : *list) { 82 for (base::Value* const value : *list) {
53 const base::DictionaryValue* dict = NULL; 83 const base::DictionaryValue* dict = NULL;
54 if (!value->GetAsDictionary(&dict)) 84 if (!value->GetAsDictionary(&dict))
55 return false; 85 return false;
56 86
57 const base::DictionaryValue* content = NULL; 87 const base::DictionaryValue* content = NULL;
58 if (!dict->GetDictionary("contentInfo", &content)) 88 if (!dict->GetDictionary("contentInfo", &content))
59 return false; 89 return false;
60 std::unique_ptr<NTPSnippet> snippet = 90 scoped_ptr<NTPSnippet> snippet =
61 NTPSnippet::NTPSnippetFromDictionary(*content); 91 NTPSnippet::NTPSnippetFromDictionary(*content);
62 if (!snippet) 92 if (!snippet)
63 return false; 93 return false;
64 snippets_.push_back(std::move(snippet)); 94 snippets_.push_back(std::move(snippet));
65 } 95 }
66 loaded_ = true; 96 loaded_ = true;
97
67 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 98 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
68 NTPSnippetsServiceLoaded(this)); 99 NTPSnippetsServiceLoaded(this));
69 return true; 100 return true;
70 } 101 }
71 102
103 void NTPSnippetsService::OnSnippetsDownloaded(
104 const base::FilePath& download_path) {
105 base::PostTaskAndReplyWithResult(
106 file_task_runner_.get(), FROM_HERE,
107 base::Bind(&ReadFileToString, download_path),
108 base::Bind(&NTPSnippetsService::OnFileReadDone,
109 weak_ptr_factory_.GetWeakPtr()));
110 }
111
72 } // namespace ntp_snippets 112 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698