OLD | NEW |
---|---|
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/path_service.h" | |
11 #include "base/task_runner_util.h" | |
8 #include "base/values.h" | 12 #include "base/values.h" |
9 | 13 |
10 namespace ntp_snippets { | 14 namespace ntp_snippets { |
11 | 15 |
16 extern base::FilePath GetSnippetsSuggestionsPath(); | |
Bernhard Bauer
2016/02/08 18:19:27
Wait, what's this doing here? This seems like it s
May
2016/02/09 17:38:54
I removed this, since I changed the callback model
| |
17 | |
18 std::string ReadFileToString(const base::FilePath& path) { | |
19 std::string data; | |
20 bool success = base::ReadFileToString(path, &data); | |
21 if (!success) { | |
Bernhard Bauer
2016/02/08 18:19:26
No braces. Also, you could use DLOG_IF().
May
2016/02/09 17:38:54
Done.
| |
22 DLOG(ERROR) << "Error reading file " << path.LossyDisplayName(); | |
23 } | |
24 return data; | |
25 } | |
26 | |
12 NTPSnippetsService::NTPSnippetsService( | 27 NTPSnippetsService::NTPSnippetsService( |
13 const std::string& application_language_code) | 28 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
14 : loaded_(false), application_language_code_(application_language_code) {} | 29 const std::string& application_language_code, |
30 NTPSnippetsFetcher* snippets_fetcher) | |
31 : loaded_(false), | |
32 file_task_runner_(file_task_runner), | |
33 application_language_code_(application_language_code) { | |
34 snippets_fetcher_.reset(snippets_fetcher); | |
Bernhard Bauer
2016/02/08 18:19:27
You could directly initialize this in the initiali
May
2016/02/09 17:38:53
Done.
| |
35 snippets_fetcher_->AddObserver(this); | |
36 } | |
15 | 37 |
16 NTPSnippetsService::~NTPSnippetsService() {} | 38 NTPSnippetsService::~NTPSnippetsService() { |
39 snippets_fetcher_->RemoveObserver(this); | |
40 } | |
17 | 41 |
18 void NTPSnippetsService::Shutdown() { | 42 void NTPSnippetsService::Shutdown() { |
19 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 43 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
20 NTPSnippetsServiceShutdown(this)); | 44 NTPSnippetsServiceShutdown(this)); |
21 loaded_ = false; | 45 loaded_ = false; |
22 } | 46 } |
23 | 47 |
48 void NTPSnippetsService::FetchSnippets(bool overwrite) { | |
49 snippets_fetcher_->FetchSnippets(overwrite); | |
50 } | |
51 | |
24 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 52 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
25 observers_.AddObserver(observer); | 53 observers_.AddObserver(observer); |
26 if (loaded_) | 54 if (loaded_) |
27 observer->NTPSnippetsServiceLoaded(this); | 55 observer->NTPSnippetsServiceLoaded(this); |
28 } | 56 } |
29 | 57 |
30 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 58 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
31 observers_.RemoveObserver(observer); | 59 observers_.RemoveObserver(observer); |
32 } | 60 } |
33 | 61 |
62 void NTPSnippetsService::OnFileReadDone(std::string& json) { | |
63 LoadFromJSONString(json); | |
64 } | |
65 | |
34 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { | 66 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { |
35 JSONStringValueDeserializer deserializer(str); | 67 JSONStringValueDeserializer deserializer(str); |
36 int error_code; | 68 int error_code; |
37 std::string error_message; | 69 std::string error_message; |
38 | 70 |
39 scoped_ptr<base::Value> deserialized = | 71 scoped_ptr<base::Value> deserialized = |
40 deserializer.Deserialize(&error_code, &error_message); | 72 deserializer.Deserialize(&error_code, &error_message); |
41 if (!deserialized) | 73 if (!deserialized) { |
Bernhard Bauer
2016/02/08 18:19:26
Eh, undo please 😃
May
2016/02/09 17:38:54
Done.
| |
42 return false; | 74 return false; |
75 } | |
43 | 76 |
44 const base::DictionaryValue* top_dict = NULL; | 77 const base::DictionaryValue* top_dict = NULL; |
45 if (!deserialized->GetAsDictionary(&top_dict)) | 78 if (!deserialized->GetAsDictionary(&top_dict)) |
46 return false; | 79 return false; |
47 | 80 |
48 const base::ListValue* list = NULL; | 81 const base::ListValue* list = NULL; |
49 if (!top_dict->GetList("recos", &list)) | 82 if (!top_dict->GetList("recos", &list)) |
50 return false; | 83 return false; |
51 | 84 |
52 for (base::Value* const value : *list) { | 85 for (base::Value* const value : *list) { |
53 const base::DictionaryValue* dict = NULL; | 86 const base::DictionaryValue* dict = NULL; |
54 if (!value->GetAsDictionary(&dict)) | 87 if (!value->GetAsDictionary(&dict)) |
55 return false; | 88 return false; |
56 | 89 |
57 const base::DictionaryValue* content = NULL; | 90 const base::DictionaryValue* content = NULL; |
58 if (!dict->GetDictionary("contentInfo", &content)) | 91 if (!dict->GetDictionary("contentInfo", &content)) |
59 return false; | 92 return false; |
60 std::unique_ptr<NTPSnippet> snippet = | 93 std::unique_ptr<NTPSnippet> snippet = |
61 NTPSnippet::NTPSnippetFromDictionary(*content); | 94 NTPSnippet::NTPSnippetFromDictionary(*content); |
62 if (!snippet) | 95 if (!snippet) |
63 return false; | 96 return false; |
64 snippets_.push_back(std::move(snippet)); | 97 snippets_.push_back(std::move(snippet)); |
65 } | 98 } |
66 loaded_ = true; | 99 loaded_ = true; |
100 | |
67 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 101 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
68 NTPSnippetsServiceLoaded(this)); | 102 NTPSnippetsServiceLoaded(this)); |
69 return true; | 103 return true; |
70 } | 104 } |
71 | 105 |
106 // NTPSnippetsFetcher::Observer overrides: | |
107 void NTPSnippetsService::OnNTPSnippetsDownloaded() { | |
108 base::PostTaskAndReplyWithResult( | |
109 file_task_runner_.get(), FROM_HERE, | |
110 base::Bind(&ReadFileToString, GetSnippetsSuggestionsPath()), | |
111 base::Bind(&NTPSnippetsService::OnFileReadDone, base::Unretained(this))); | |
Marc Treib
2016/02/09 09:17:54
I don't think base::Unretained is safe here. You p
May
2016/02/09 17:38:53
Done.
| |
112 } | |
113 | |
72 } // namespace ntp_snippets | 114 } // namespace ntp_snippets |
OLD | NEW |