category.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_CATEGORY_H_
  5. #define COMPONENTS_NTP_SNIPPETS_CATEGORY_H_
  6. #include <ostream>
  7. namespace ntp_snippets {
  8. // These are the categories that the client knows about.
  9. // The values before LOCAL_CATEGORIES_COUNT are the categories that are provided
  10. // locally on the device. Categories provided by the server (IDs strictly larger
  11. // than REMOTE_CATEGORIES_OFFSET) only need to be hard-coded here if they need
  12. // to be recognized by the client implementation.
  13. // NOTE: These are persisted, so don't reorder or remove values, and insert new
  14. // values only in the appropriate places marked below.
  15. // On Android builds, a Java counterpart will be generated for this enum.
  16. // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.ntp.snippets
  17. enum class KnownCategories {
  18. // Pages recently downloaded during normal navigation.
  19. RECENT_TABS_DEPRECATED,
  20. // Pages downloaded by the user for offline consumption.
  21. DOWNLOADS_DEPRECATED,
  22. // Recently used bookmarks.
  23. BOOKMARKS_DEPRECATED,
  24. // Physical Web page available in the vicinity.
  25. PHYSICAL_WEB_PAGES_DEPRECATED,
  26. // Pages recently browsed to on other devices.
  27. FOREIGN_TABS_DEPRECATED,
  28. // Pages from the user reading list.
  29. READING_LIST,
  30. // ****************** INSERT NEW LOCAL CATEGORIES HERE! ******************
  31. // Existing categories are persisted and they must never be removed. This may
  32. // happen implicitly, e.g. when an older version without some local category
  33. // is installed.
  34. // Follows the last local category.
  35. LOCAL_CATEGORIES_COUNT,
  36. // Remote categories come after this.
  37. REMOTE_CATEGORIES_OFFSET = 10000,
  38. // Articles for you.
  39. ARTICLES = 10001,
  40. // Categories 10002-10008 are defined on the server.
  41. // ****************** INSERT NEW REMOTE CATEGORIES HERE! ******************
  42. // Update the list on the server first. Here specify the ID explicitly.
  43. // Tracks the last known remote category
  44. LAST_KNOWN_REMOTE_CATEGORY = ARTICLES,
  45. };
  46. // A category groups ContentSuggestions which belong together. Use the
  47. // CategoryFactory to obtain instances.
  48. class Category {
  49. public:
  50. // An arbitrary but consistent ordering. Can be used to look up categories in
  51. // a std::map, but should not be used to order categories for other purposes.
  52. struct CompareByID;
  53. // Creates a category from a KnownCategory value. The passed |known_category|
  54. // must not be one of the special values (LOCAL_CATEGORIES_COUNT or
  55. // REMOTE_CATEGORIES_OFFSET).
  56. static Category FromKnownCategory(KnownCategories known_category);
  57. // Creates a category from a category identifier delivered by the server.
  58. // |remote_category| must be positive.
  59. static Category FromRemoteCategory(int remote_category);
  60. // Creates a category from an ID as returned by |id()|. |id| must be a
  61. // non-negative value. Callers should make sure this is a valid id (if in
  62. // doubt, call IsValidIDValue()).
  63. static Category FromIDValue(int id);
  64. // Verifies if |id| is a valid ID value. Only checks that the value is within
  65. // a valid range -- not that the system actually knows about the corresponding
  66. // category.
  67. static bool IsValidIDValue(int id);
  68. // Returns a non-negative identifier that is unique for the category and can
  69. // be converted back to a Category instance using
  70. // |CategoryFactory::FromIDValue(id)|.
  71. int id() const { return id_; }
  72. // Returns a remote category identifier. Do not call for non-remote
  73. // categories.
  74. int remote_id() const;
  75. // Returns whether this category matches the given |known_category|.
  76. bool IsKnownCategory(KnownCategories known_category) const;
  77. private:
  78. explicit Category(int id);
  79. int id_;
  80. // Allow copy and assignment.
  81. };
  82. bool operator==(const Category& left, const Category& right);
  83. bool operator!=(const Category& left, const Category& right);
  84. struct Category::CompareByID {
  85. bool operator()(const Category& left, const Category& right) const;
  86. };
  87. std::ostream& operator<<(std::ostream& os, const Category& obj);
  88. } // namespace ntp_snippets
  89. #endif // COMPONENTS_NTP_SNIPPETS_CATEGORY_H_