content_suggestion.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTION_H_
  5. #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTION_H_
  6. #include <cstdint>
  7. #include <memory>
  8. #include <string>
  9. #include "base/time/time.h"
  10. #include "components/ntp_snippets/category.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. #include "url/gurl.h"
  13. namespace ntp_snippets {
  14. // ReadingListSuggestionExtra contains additional data which is only available
  15. // for Reading List suggestions.
  16. struct ReadingListSuggestionExtra {
  17. // URL of the page whose favicon should be displayed for this suggestion.
  18. GURL favicon_page_url;
  19. };
  20. // Contains additional data for notification-worthy suggestions.
  21. struct NotificationExtra {
  22. // Deadline for showing notification. If the deadline is past, the
  23. // notification is no longer fresh and no notification should be sent. If the
  24. // deadline passes while a notification is up, it should be canceled.
  25. base::Time deadline;
  26. };
  27. // A content suggestion for the new tab page, which can be an article or an
  28. // offline page, for example.
  29. class ContentSuggestion {
  30. public:
  31. class ID {
  32. public:
  33. ID(Category category, const std::string& id_within_category)
  34. : category_(category), id_within_category_(id_within_category) {}
  35. Category category() const { return category_; }
  36. const std::string& id_within_category() const {
  37. return id_within_category_;
  38. }
  39. bool operator==(const ID& rhs) const;
  40. bool operator!=(const ID& rhs) const;
  41. private:
  42. Category category_;
  43. std::string id_within_category_;
  44. // Allow copy and assignment.
  45. };
  46. // Creates a new ContentSuggestion. The caller must ensure that the |id|
  47. // passed in here is unique application-wide.
  48. ContentSuggestion(const ID& id, const GURL& url);
  49. ContentSuggestion(Category category,
  50. const std::string& id_within_category,
  51. const GURL& url);
  52. ContentSuggestion(ContentSuggestion&&);
  53. ContentSuggestion& operator=(ContentSuggestion&&);
  54. ContentSuggestion(const ContentSuggestion&) = delete;
  55. ContentSuggestion& operator=(const ContentSuggestion&) = delete;
  56. ~ContentSuggestion();
  57. // An ID for identifying the suggestion. The ID is unique application-wide.
  58. const ID& id() const { return id_; }
  59. // The URL where the content referenced by the suggestion can be accessed.
  60. // This may be an AMP URL.
  61. const GURL& url() const { return url_; }
  62. // The URL of the page that links to a favicon that represents the suggestion.
  63. // Path is trimmed for the URL because the current favicon server backend
  64. // prefers it this way.
  65. GURL url_with_favicon() const {
  66. return url_with_favicon_.is_valid() ? GetFaviconDomain(url_with_favicon_)
  67. : GetFaviconDomain(url_);
  68. }
  69. void set_url_with_favicon(const GURL& url_with_favicon) {
  70. url_with_favicon_ = url_with_favicon;
  71. }
  72. // A URL for an image that represents the content of the suggestion.
  73. // Empty when an image is not available.
  74. GURL salient_image_url() const { return salient_image_url_; }
  75. void set_salient_image_url(const GURL& salient_image_url) {
  76. salient_image_url_ = salient_image_url;
  77. }
  78. static GURL GetFaviconDomain(const GURL& favicon_url);
  79. // Title of the suggestion.
  80. const std::u16string& title() const { return title_; }
  81. void set_title(const std::u16string& title) { title_ = title; }
  82. // Summary or relevant textual extract from the content.
  83. const std::u16string& snippet_text() const { return snippet_text_; }
  84. void set_snippet_text(const std::u16string& snippet_text) {
  85. snippet_text_ = snippet_text;
  86. }
  87. // The time when the content represented by this suggestion was published.
  88. const base::Time& publish_date() const { return publish_date_; }
  89. void set_publish_date(const base::Time& publish_date) {
  90. publish_date_ = publish_date;
  91. }
  92. // The name of the source/publisher of this suggestion.
  93. const std::u16string& publisher_name() const { return publisher_name_; }
  94. void set_publisher_name(const std::u16string& publisher_name) {
  95. publisher_name_ = publisher_name;
  96. }
  97. bool is_video_suggestion() const { return is_video_suggestion_; }
  98. void set_is_video_suggestion(bool is_video_suggestion) {
  99. is_video_suggestion_ = is_video_suggestion;
  100. }
  101. // TODO(pke): Remove the score from the ContentSuggestion class. The UI only
  102. // uses it to track user clicks (histogram data). Instead, the providers
  103. // should be informed about clicks and do appropriate logging themselves.
  104. // IMPORTANT: The score may simply be 0 for suggestions from providers which
  105. // cannot provide score values.
  106. float score() const { return score_; }
  107. void set_score(float score) { score_ = score; }
  108. // Extra information for reading list suggestions. Only available for
  109. // KnownCategories::READING_LIST suggestions.
  110. ReadingListSuggestionExtra* reading_list_suggestion_extra() const {
  111. return reading_list_suggestion_extra_.get();
  112. }
  113. void set_reading_list_suggestion_extra(
  114. std::unique_ptr<ReadingListSuggestionExtra>
  115. reading_list_suggestion_extra);
  116. // Extra information for notifications. When absent, no notification should be
  117. // sent for this suggestion. When present, a notification should be sent,
  118. // unless other factors disallow it (examples: the extra parameters say to;
  119. // notifications are disabled; Chrome is in the foreground).
  120. NotificationExtra* notification_extra() const {
  121. return notification_extra_.get();
  122. }
  123. void set_notification_extra(
  124. std::unique_ptr<NotificationExtra> notification_extra);
  125. const base::Time& fetch_date() const { return fetch_date_; }
  126. void set_fetch_date(const base::Time& fetch_date) {
  127. fetch_date_ = fetch_date;
  128. }
  129. const absl::optional<uint32_t>& optional_image_dominant_color() const {
  130. return image_dominant_color_;
  131. }
  132. void set_optional_image_dominant_color(
  133. const absl::optional<uint32_t>& optional_color_int) {
  134. image_dominant_color_ = optional_color_int;
  135. }
  136. private:
  137. ID id_;
  138. GURL url_;
  139. GURL url_with_favicon_;
  140. GURL salient_image_url_;
  141. std::u16string title_;
  142. std::u16string snippet_text_;
  143. base::Time publish_date_;
  144. std::u16string publisher_name_;
  145. float score_;
  146. std::unique_ptr<ReadingListSuggestionExtra> reading_list_suggestion_extra_;
  147. std::unique_ptr<NotificationExtra> notification_extra_;
  148. // The time when the remote suggestion was fetched from the server. This field
  149. // is only populated when the ContentSuggestion is created from a
  150. // RemoteSuggestion.
  151. base::Time fetch_date_;
  152. bool is_video_suggestion_;
  153. // Encoded as an Android @ColorInt.
  154. absl::optional<uint32_t> image_dominant_color_;
  155. };
  156. std::ostream& operator<<(std::ostream& os, const ContentSuggestion::ID& id);
  157. } // namespace ntp_snippets
  158. #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTION_H_