OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_CATEGORY_FACTORY_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_CATEGORY_FACTORY_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "components/ntp_snippets/content_suggestions_category.h" |
| 13 |
| 14 namespace ntp_snippets { |
| 15 |
| 16 // Creates and orders instances of ContentSuggestionsCategory. |
| 17 class ContentSuggestionsCategoryFactory { |
| 18 public: |
| 19 ContentSuggestionsCategoryFactory(); |
| 20 ~ContentSuggestionsCategoryFactory(); |
| 21 |
| 22 // Creates a category from a KnownSuggestionsCategory value. The passed |
| 23 // |known_category| must not be one of the special values |
| 24 // (LOCAL_CATEGORIES_COUNT or REMOTE_CATEGORIES_OFFSET). |
| 25 ContentSuggestionsCategory FromKnownCategory( |
| 26 KnownSuggestionsCategories known_category); |
| 27 |
| 28 // Creates a category from a category identifier delivered by the server. |
| 29 // |remote_category| must be positive. |
| 30 // Note that remote categories are ordered in the order in which they were |
| 31 // first created by calling this method. |
| 32 ContentSuggestionsCategory FromRemoteCategory(int remote_category); |
| 33 |
| 34 // Creates a category from an ID as returned by |
| 35 // |ContentSuggestionsCategory::id()|. |id| must be a non-negative value. |
| 36 ContentSuggestionsCategory FromIDValue(int id); |
| 37 |
| 38 // Compares the given categories according to a strict ordering, returning |
| 39 // true if and only if |left| is strictly less than |right|. |
| 40 // This method satisfies the "Compare" contract required by sort algorithms. |
| 41 // The order is determined as follows: All local categories go first, in a |
| 42 // specific order hard-coded in the |ContentSuggestionsCategoryFactory| |
| 43 // constructor. All remote categories follow in the order in which they were |
| 44 // first created through |FromRemoteCategory|. |
| 45 bool CompareCategories(const ContentSuggestionsCategory& left, |
| 46 const ContentSuggestionsCategory& right) const; |
| 47 |
| 48 private: |
| 49 bool CategoryExists(int id); |
| 50 void AddKnownCategory(KnownSuggestionsCategories known_category); |
| 51 ContentSuggestionsCategory InternalFromID(int id); |
| 52 |
| 53 // Stores all known categories in the order which is also returned by |
| 54 // |CompareCategories|. |
| 55 std::vector<ContentSuggestionsCategory> ordered_categories_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsCategoryFactory); |
| 58 }; |
| 59 |
| 60 } // namespace ntp_snippets |
| 61 |
| 62 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_CATEGORY_FACTORY_H_ |
OLD | NEW |