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/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 if (!success) | |
21 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
| |
22 return data; | |
23 } | |
24 | |
12 NTPSnippetsService::NTPSnippetsService( | 25 NTPSnippetsService::NTPSnippetsService( |
13 const std::string& application_language_code) | 26 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
14 : loaded_(false), application_language_code_(application_language_code) {} | 27 const std::string& application_language_code, |
28 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher) | |
29 : loaded_(false), | |
30 file_task_runner_(file_task_runner), | |
31 application_language_code_(application_language_code), | |
32 snippets_fetcher_(std::move(snippets_fetcher)), | |
33 weak_ptr_factory_(this) { | |
34 snippets_fetcher_callback_ = snippets_fetcher_->AddCallback( | |
35 base::Bind(&NTPSnippetsService::OnSnippetsDownloaded, | |
36 weak_ptr_factory_.GetWeakPtr())); | |
37 } | |
15 | 38 |
16 NTPSnippetsService::~NTPSnippetsService() {} | 39 NTPSnippetsService::~NTPSnippetsService() {} |
17 | 40 |
18 void NTPSnippetsService::Shutdown() { | 41 void NTPSnippetsService::Shutdown() { |
19 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 42 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
20 NTPSnippetsServiceShutdown(this)); | 43 NTPSnippetsServiceShutdown(this)); |
21 loaded_ = false; | 44 loaded_ = false; |
22 } | 45 } |
23 | 46 |
47 void NTPSnippetsService::FetchSnippets(bool overwrite) { | |
48 snippets_fetcher_->FetchSnippets(overwrite); | |
49 } | |
50 | |
24 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 51 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
25 observers_.AddObserver(observer); | 52 observers_.AddObserver(observer); |
26 if (loaded_) | 53 if (loaded_) |
27 observer->NTPSnippetsServiceLoaded(this); | 54 observer->NTPSnippetsServiceLoaded(this); |
28 } | 55 } |
29 | 56 |
30 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 57 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
31 observers_.RemoveObserver(observer); | 58 observers_.RemoveObserver(observer); |
32 } | 59 } |
33 | 60 |
61 void NTPSnippetsService::OnFileReadDone(const std::string& json) { | |
62 LoadFromJSONString(json); | |
63 } | |
64 | |
34 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { | 65 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { |
35 JSONStringValueDeserializer deserializer(str); | 66 JSONStringValueDeserializer deserializer(str); |
36 int error_code; | 67 int error_code; |
37 std::string error_message; | 68 std::string error_message; |
38 | 69 |
39 scoped_ptr<base::Value> deserialized = | 70 scoped_ptr<base::Value> deserialized = |
40 deserializer.Deserialize(&error_code, &error_message); | 71 deserializer.Deserialize(&error_code, &error_message); |
41 if (!deserialized) | 72 if (!deserialized) |
42 return false; | 73 return false; |
43 | 74 |
44 const base::DictionaryValue* top_dict = NULL; | 75 const base::DictionaryValue* top_dict = NULL; |
45 if (!deserialized->GetAsDictionary(&top_dict)) | 76 if (!deserialized->GetAsDictionary(&top_dict)) |
46 return false; | 77 return false; |
47 | 78 |
48 const base::ListValue* list = NULL; | 79 const base::ListValue* list = NULL; |
49 if (!top_dict->GetList("recos", &list)) | 80 if (!top_dict->GetList("recos", &list)) |
50 return false; | 81 return false; |
51 | 82 |
52 for (base::Value* const value : *list) { | 83 for (base::Value* const value : *list) { |
53 const base::DictionaryValue* dict = NULL; | 84 const base::DictionaryValue* dict = NULL; |
54 if (!value->GetAsDictionary(&dict)) | 85 if (!value->GetAsDictionary(&dict)) |
55 return false; | 86 return false; |
56 | 87 |
57 const base::DictionaryValue* content = NULL; | 88 const base::DictionaryValue* content = NULL; |
58 if (!dict->GetDictionary("contentInfo", &content)) | 89 if (!dict->GetDictionary("contentInfo", &content)) |
59 return false; | 90 return false; |
60 std::unique_ptr<NTPSnippet> snippet = | 91 scoped_ptr<NTPSnippet> snippet = |
61 NTPSnippet::NTPSnippetFromDictionary(*content); | 92 NTPSnippet::NTPSnippetFromDictionary(*content); |
62 if (!snippet) | 93 if (!snippet) |
63 return false; | 94 return false; |
64 snippets_.push_back(std::move(snippet)); | 95 snippets_.push_back(std::move(snippet)); |
65 } | 96 } |
66 loaded_ = true; | 97 loaded_ = true; |
98 | |
67 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 99 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
68 NTPSnippetsServiceLoaded(this)); | 100 NTPSnippetsServiceLoaded(this)); |
69 return true; | 101 return true; |
70 } | 102 } |
71 | 103 |
104 void NTPSnippetsService::OnSnippetsDownloaded( | |
105 const base::FilePath& download_path) { | |
106 base::PostTaskAndReplyWithResult( | |
107 file_task_runner_.get(), FROM_HERE, | |
108 base::Bind(&ReadFileToString, download_path), | |
109 base::Bind(&NTPSnippetsService::OnFileReadDone, | |
110 weak_ptr_factory_.GetWeakPtr())); | |
111 } | |
112 | |
72 } // namespace ntp_snippets | 113 } // namespace ntp_snippets |
OLD | NEW |