| 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 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 class DictionaryValue; | 17 class DictionaryValue; |
| 18 class ListValue; | 18 class ListValue; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace ntp_snippets { | 21 namespace ntp_snippets { |
| 22 | 22 |
| 23 class SnippetProto; |
| 24 |
| 23 struct SnippetSource { | 25 struct SnippetSource { |
| 24 SnippetSource(const GURL& url, | 26 SnippetSource(const GURL& url, |
| 25 const std::string& publisher_name, | 27 const std::string& publisher_name, |
| 26 const GURL& amp_url) | 28 const GURL& amp_url) |
| 27 : url(url), publisher_name(publisher_name), amp_url(amp_url) {} | 29 : url(url), publisher_name(publisher_name), amp_url(amp_url) {} |
| 28 GURL url; | 30 GURL url; |
| 29 std::string publisher_name; | 31 std::string publisher_name; |
| 30 GURL amp_url; | 32 GURL amp_url; |
| 31 }; | 33 }; |
| 32 | 34 |
| 33 class NTPSnippet { | 35 class NTPSnippet { |
| 34 public: | 36 public: |
| 35 using PtrVector = std::vector<std::unique_ptr<NTPSnippet>>; | 37 using PtrVector = std::vector<std::unique_ptr<NTPSnippet>>; |
| 36 | 38 |
| 37 // Creates a new snippet with the given |id|. | 39 // Creates a new snippet with the given |id|. |
| 40 // Public for testing only - create snippets using the Create* methods below. |
| 41 // TODO(treib): Make this private and add a CreateSnippetForTest? |
| 38 NTPSnippet(const std::string& id); | 42 NTPSnippet(const std::string& id); |
| 39 | 43 |
| 40 ~NTPSnippet(); | 44 ~NTPSnippet(); |
| 41 | 45 |
| 42 // Creates an NTPSnippet from a dictionary. Returns a null pointer if the | 46 // Creates an NTPSnippet from a dictionary. Returns a null pointer if the |
| 43 // dictionary doesn't contain at least a url. The keys in the dictionary are | 47 // dictionary doesn't correspond to a valid snippet. The keys in the |
| 44 // expected to be the same as the property name, with exceptions documented in | 48 // dictionary are expected to be the same as the property name, with |
| 45 // the property comment. | 49 // exceptions documented in the property comment. |
| 46 static std::unique_ptr<NTPSnippet> CreateFromDictionary( | 50 static std::unique_ptr<NTPSnippet> CreateFromDictionary( |
| 47 const base::DictionaryValue& dict); | 51 const base::DictionaryValue& dict); |
| 48 | 52 |
| 53 // Creates an NTPSnippet from a protocol buffer. Returns a null pointer if the |
| 54 // protocol buffer doesn't correspond to a valid snippet. |
| 55 static std::unique_ptr<NTPSnippet> CreateFromProto(const SnippetProto& proto); |
| 56 |
| 49 // Creates snippets from dictionary values in |list| and adds them to | 57 // Creates snippets from dictionary values in |list| and adds them to |
| 50 // |snippets|. Returns true on success, false if anything went wrong. | 58 // |snippets|. Returns true on success, false if anything went wrong. |
| 59 // TODO(treib): Move this to NTPSnippetsFetcher where it's used. |
| 51 static bool AddFromListValue(const base::ListValue& list, | 60 static bool AddFromListValue(const base::ListValue& list, |
| 52 PtrVector* snippets); | 61 PtrVector* snippets); |
| 53 | 62 |
| 54 std::unique_ptr<base::DictionaryValue> ToDictionary() const; | 63 // Creates a protocol buffer corresponding to this snippet, for persisting. |
| 64 SnippetProto ToProto() const; |
| 55 | 65 |
| 56 // A unique ID for identifying the snippet. If initialized by | 66 // A unique ID for identifying the snippet. If initialized by |
| 57 // CreateFromDictionary() the relevant key is 'url'. | 67 // CreateFromDictionary() the relevant key is 'url'. |
| 58 // TODO(treib): For now, the ID has to be a valid URL spec, otherwise | 68 // TODO(treib): For now, the ID has to be a valid URL spec, otherwise |
| 59 // fetching the salient image will fail. See TODO in ntp_snippets_service.cc. | 69 // fetching the salient image will fail. See TODO in ntp_snippets_service.cc. |
| 60 const std::string& id() const { return id_; } | 70 const std::string& id() const { return id_; } |
| 61 | 71 |
| 62 // Title of the snippet. | 72 // Title of the snippet. |
| 63 const std::string& title() const { return title_; } | 73 const std::string& title() const { return title_; } |
| 64 void set_title(const std::string& title) { title_ = title; } | 74 void set_title(const std::string& title) { title_ = title; } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 bool is_complete() const { | 115 bool is_complete() const { |
| 106 return !id().empty() && !sources().empty() && !title().empty() && | 116 return !id().empty() && !sources().empty() && !title().empty() && |
| 107 !snippet().empty() && salient_image_url().is_valid() && | 117 !snippet().empty() && salient_image_url().is_valid() && |
| 108 !publish_date().is_null() && !expiry_date().is_null() && | 118 !publish_date().is_null() && !expiry_date().is_null() && |
| 109 !best_source().publisher_name.empty(); | 119 !best_source().publisher_name.empty(); |
| 110 } | 120 } |
| 111 | 121 |
| 112 float score() const { return score_; } | 122 float score() const { return score_; } |
| 113 void set_score(float score) { score_ = score; } | 123 void set_score(float score) { score_ = score; } |
| 114 | 124 |
| 125 bool is_discarded() const { return is_discarded_; } |
| 126 void set_discarded(bool discarded) { is_discarded_ = discarded; } |
| 127 |
| 115 // Public for testing. | 128 // Public for testing. |
| 116 static base::Time TimeFromJsonString(const std::string& timestamp_str); | 129 static base::Time TimeFromJsonString(const std::string& timestamp_str); |
| 117 static std::string TimeToJsonString(const base::Time& time); | 130 static std::string TimeToJsonString(const base::Time& time); |
| 118 | 131 |
| 119 private: | 132 private: |
| 133 void FindBestSource(); |
| 134 |
| 120 std::string id_; | 135 std::string id_; |
| 121 std::string title_; | 136 std::string title_; |
| 122 GURL salient_image_url_; | 137 GURL salient_image_url_; |
| 123 std::string snippet_; | 138 std::string snippet_; |
| 124 base::Time publish_date_; | 139 base::Time publish_date_; |
| 125 base::Time expiry_date_; | 140 base::Time expiry_date_; |
| 126 float score_; | 141 float score_; |
| 142 bool is_discarded_; |
| 143 |
| 127 size_t best_source_index_; | 144 size_t best_source_index_; |
| 128 | 145 |
| 129 std::vector<SnippetSource> sources_; | 146 std::vector<SnippetSource> sources_; |
| 130 | 147 |
| 131 DISALLOW_COPY_AND_ASSIGN(NTPSnippet); | 148 DISALLOW_COPY_AND_ASSIGN(NTPSnippet); |
| 132 }; | 149 }; |
| 133 | 150 |
| 134 } // namespace ntp_snippets | 151 } // namespace ntp_snippets |
| 135 | 152 |
| 136 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ | 153 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPET_H_ |
| OLD | NEW |