OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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_CATEGORY_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_CATEGORY_H_ |
6 #define COMPONENTS_NTP_SNIPPETS_CATEGORY_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_CATEGORY_H_ |
7 | 7 |
8 #include <ostream> | 8 #include <ostream> |
9 | 9 |
10 namespace ntp_snippets { | 10 namespace ntp_snippets { |
11 | 11 |
12 class CategoryFactory; | |
13 | |
14 // These are the categories that the client knows about. | 12 // These are the categories that the client knows about. |
15 // The values before LOCAL_CATEGORIES_COUNT are the categories that are provided | 13 // The values before LOCAL_CATEGORIES_COUNT are the categories that are provided |
16 // locally on the device. Categories provided by the server (IDs strictly larger | 14 // locally on the device. Categories provided by the server (IDs strictly larger |
17 // than REMOTE_CATEGORIES_OFFSET) only need to be hard-coded here if they need | 15 // than REMOTE_CATEGORIES_OFFSET) only need to be hard-coded here if they need |
18 // to be recognized by the client implementation. | 16 // to be recognized by the client implementation. |
19 // NOTE: These are persisted, so don't reorder or remove values, and insert new | 17 // NOTE: These are persisted, so don't reorder or remove values, and insert new |
20 // values only in the appropriate places marked below. | 18 // values only in the appropriate places marked below. |
21 // On Android builds, a Java counterpart will be generated for this enum. | 19 // On Android builds, a Java counterpart will be generated for this enum. |
22 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.ntp.snippets | 20 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.ntp.snippets |
23 enum class KnownCategories { | 21 enum class KnownCategories { |
(...skipping 28 matching lines...) Expand all Loading... |
52 }; | 50 }; |
53 | 51 |
54 // A category groups ContentSuggestions which belong together. Use the | 52 // A category groups ContentSuggestions which belong together. Use the |
55 // CategoryFactory to obtain instances. | 53 // CategoryFactory to obtain instances. |
56 class Category { | 54 class Category { |
57 public: | 55 public: |
58 // An arbitrary but consistent ordering. Can be used to look up categories in | 56 // An arbitrary but consistent ordering. Can be used to look up categories in |
59 // a std::map, but should not be used to order categories for other purposes. | 57 // a std::map, but should not be used to order categories for other purposes. |
60 struct CompareByID; | 58 struct CompareByID; |
61 | 59 |
| 60 // Creates a category from a KnownCategory value. The passed |known_category| |
| 61 // must not be one of the special values (LOCAL_CATEGORIES_COUNT or |
| 62 // REMOTE_CATEGORIES_OFFSET). |
| 63 static Category FromKnownCategory(KnownCategories known_category); |
| 64 |
| 65 // Creates a category from a category identifier delivered by the server. |
| 66 // |remote_category| must be positive. |
| 67 static Category FromRemoteCategory(int remote_category); |
| 68 |
| 69 // Creates a category from an ID as returned by |id()|. |id| must be a |
| 70 // non-negative value. |
| 71 static Category FromIDValue(int id); |
| 72 |
62 // Returns a non-negative identifier that is unique for the category and can | 73 // Returns a non-negative identifier that is unique for the category and can |
63 // be converted back to a Category instance using | 74 // be converted back to a Category instance using |
64 // |CategoryFactory::FromIDValue(id)|. | 75 // |CategoryFactory::FromIDValue(id)|. |
65 int id() const { return id_; } | 76 int id() const { return id_; } |
66 | 77 |
67 // Returns whether this category matches the given |known_category|. | 78 // Returns whether this category matches the given |known_category|. |
68 bool IsKnownCategory(KnownCategories known_category) const; | 79 bool IsKnownCategory(KnownCategories known_category) const; |
69 | 80 |
70 private: | 81 private: |
71 friend class CategoryFactory; | |
72 | |
73 explicit Category(int id); | 82 explicit Category(int id); |
74 | 83 |
75 int id_; | 84 int id_; |
76 | 85 |
77 // Allow copy and assignment. | 86 // Allow copy and assignment. |
78 }; | 87 }; |
79 | 88 |
80 bool operator==(const Category& left, const Category& right); | 89 bool operator==(const Category& left, const Category& right); |
81 | 90 |
82 bool operator!=(const Category& left, const Category& right); | 91 bool operator!=(const Category& left, const Category& right); |
83 | 92 |
84 struct Category::CompareByID { | 93 struct Category::CompareByID { |
85 bool operator()(const Category& left, const Category& right) const; | 94 bool operator()(const Category& left, const Category& right) const; |
86 }; | 95 }; |
87 | 96 |
88 std::ostream& operator<<(std::ostream& os, const Category& obj); | 97 std::ostream& operator<<(std::ostream& os, const Category& obj); |
89 | 98 |
90 } // namespace ntp_snippets | 99 } // namespace ntp_snippets |
91 | 100 |
92 #endif // COMPONENTS_NTP_SNIPPETS_CATEGORY_H_ | 101 #endif // COMPONENTS_NTP_SNIPPETS_CATEGORY_H_ |
OLD | NEW |