Chromium Code Reviews| 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" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // TODO(crbug.com/587857): This is an extremely small value, for development. | 17 // TODO(crbug.com/587857): This is an extremely small value, for development. |
| 18 // Replace it by something sensible and add a command line param to control it. | 18 // Replace it by something sensible and add a command line param to control it. |
| 19 const int kDefaultFetchingIntervalSeconds = 60; | 19 const int kDefaultFetchingIntervalSeconds = 60; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 | 82 |
| 83 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 83 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
| 84 observers_.RemoveObserver(observer); | 84 observers_.RemoveObserver(observer); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void NTPSnippetsService::OnFileReadDone(const std::string& json) { | 87 void NTPSnippetsService::OnFileReadDone(const std::string& json) { |
| 88 LoadFromJSONString(json); | 88 LoadFromJSONString(json); |
| 89 } | 89 } |
| 90 | 90 |
| 91 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { | 91 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { |
| 92 snippets_.clear(); | 92 scoped_ptr<base::Value> deserialized = base::JSONReader::Read(str); |
| 93 | |
| 94 JSONStringValueDeserializer deserializer(str); | |
|
Marc Treib
2016/02/23 12:57:54
Not sure what the point of this class is, it just
Bernhard Bauer
2016/02/23 13:42:21
¯\_(ツ)_/¯ It does return an error message and code
Marc Treib
2016/02/23 13:58:33
Which JSONReader also does, if you call ReadAndRet
| |
| 95 int error_code; | |
| 96 std::string error_message; | |
| 97 | |
| 98 scoped_ptr<base::Value> deserialized = | |
| 99 deserializer.Deserialize(&error_code, &error_message); | |
| 100 if (!deserialized) | 93 if (!deserialized) |
| 101 return false; | 94 return false; |
| 102 | 95 |
| 103 const base::DictionaryValue* top_dict = NULL; | 96 const base::DictionaryValue* top_dict = nullptr; |
| 104 if (!deserialized->GetAsDictionary(&top_dict)) | 97 if (!deserialized->GetAsDictionary(&top_dict)) |
| 105 return false; | 98 return false; |
| 106 | 99 |
| 107 const base::ListValue* list = NULL; | 100 const base::ListValue* list = nullptr; |
| 108 if (!top_dict->GetList("recos", &list)) | 101 if (!top_dict->GetList("recos", &list)) |
| 109 return false; | 102 return false; |
| 110 | 103 |
| 111 for (base::Value* const value : *list) { | 104 for (const base::Value* const value : *list) { |
| 112 const base::DictionaryValue* dict = NULL; | 105 const base::DictionaryValue* dict = nullptr; |
| 113 if (!value->GetAsDictionary(&dict)) | 106 if (!value->GetAsDictionary(&dict)) |
| 114 return false; | 107 return false; |
| 115 | 108 |
| 116 const base::DictionaryValue* content = NULL; | 109 const base::DictionaryValue* content = nullptr; |
| 117 if (!dict->GetDictionary("contentInfo", &content)) | 110 if (!dict->GetDictionary("contentInfo", &content)) |
| 118 return false; | 111 return false; |
| 119 scoped_ptr<NTPSnippet> snippet = | 112 scoped_ptr<NTPSnippet> snippet = |
| 120 NTPSnippet::NTPSnippetFromDictionary(*content); | 113 NTPSnippet::NTPSnippetFromDictionary(*content); |
| 121 if (!snippet) | 114 if (!snippet) |
| 122 return false; | 115 return false; |
| 123 snippets_.push_back(std::move(snippet)); | 116 |
| 117 // Check if we already have a snippet with the same URL. If so, replace it | |
| 118 // rather than adding a duplicate. | |
| 119 const GURL& url = snippet->url(); | |
| 120 auto it = std::find_if(snippets_.begin(), snippets_.end(), | |
| 121 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { | |
| 122 return old_snippet->url() == url; | |
| 123 }); | |
| 124 if (it != snippets_.end()) | |
| 125 *it = std::move(snippet); | |
| 126 else | |
|
Bernhard Bauer
2016/02/23 13:42:21
This kind of feels like it should go into a map...
Marc Treib
2016/02/23 13:58:33
Maybe? That would mess up the order though, and we
Bernhard Bauer
2016/02/23 14:48:28
Weeeellll... we could of course make this a std::s
Marc Treib
2016/02/23 15:05:09
Good point :)
| |
| 127 snippets_.push_back(std::move(snippet)); | |
| 124 } | 128 } |
| 125 loaded_ = true; | 129 loaded_ = true; |
| 126 | 130 |
| 127 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 131 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 128 NTPSnippetsServiceLoaded(this)); | 132 NTPSnippetsServiceLoaded(this)); |
| 129 return true; | 133 return true; |
| 130 } | 134 } |
| 131 | 135 |
| 132 void NTPSnippetsService::OnSnippetsDownloaded( | 136 void NTPSnippetsService::OnSnippetsDownloaded( |
| 133 const base::FilePath& download_path) { | 137 const base::FilePath& download_path) { |
| 134 base::PostTaskAndReplyWithResult( | 138 base::PostTaskAndReplyWithResult( |
| 135 file_task_runner_.get(), FROM_HERE, | 139 file_task_runner_.get(), FROM_HERE, |
| 136 base::Bind(&ReadFileToString, download_path), | 140 base::Bind(&ReadFileToString, download_path), |
| 137 base::Bind(&NTPSnippetsService::OnFileReadDone, | 141 base::Bind(&NTPSnippetsService::OnFileReadDone, |
| 138 weak_ptr_factory_.GetWeakPtr())); | 142 weak_ptr_factory_.GetWeakPtr())); |
| 139 } | 143 } |
| 140 | 144 |
| 141 } // namespace ntp_snippets | 145 } // namespace ntp_snippets |
| OLD | NEW |