| 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_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_QUERY_H_ | |
| 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_QUERY_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "components/offline_pages/client_policy_controller.h" | |
| 13 #include "components/offline_pages/offline_page_item.h" | |
| 14 #include "components/offline_pages/offline_page_types.h" | |
| 15 | |
| 16 namespace offline_pages { | |
| 17 | |
| 18 // Can be used by OfflinePageModel instances to direct a query of the model. | |
| 19 class OfflinePageModelQuery { | |
| 20 public: | |
| 21 // A query can be constrained by several fields. This constraint can be | |
| 22 // either a positive or negative one (or no constraint): a page MUST have | |
| 23 // something, or a page MUST NOT have something to match a query. | |
| 24 enum class Requirement { | |
| 25 // No requirement. | |
| 26 UNSET, | |
| 27 // All returned pages will have one of the values specified in the query | |
| 28 // requirement. | |
| 29 INCLUDE_MATCHING, | |
| 30 // All returned pages will not have any of the values specified in the query | |
| 31 // requirement. | |
| 32 EXCLUDE_MATCHING, | |
| 33 }; | |
| 34 | |
| 35 OfflinePageModelQuery(); | |
| 36 virtual ~OfflinePageModelQuery(); | |
| 37 | |
| 38 std::pair<bool, std::set<std::string>> GetRestrictedToNamespaces() const; | |
| 39 std::pair<Requirement, std::set<int64_t>> GetRestrictedToOfflineIds() const; | |
| 40 std::pair<Requirement, std::set<ClientId>> GetRestrictedToClientIds() const; | |
| 41 std::pair<Requirement, std::set<GURL>> GetRestrictedToUrls() const; | |
| 42 | |
| 43 // This is the workhorse function that is used by the in-memory offline page | |
| 44 // model, given a page it will find out whether that page matches the query. | |
| 45 bool Matches(const OfflinePageItem& page) const; | |
| 46 | |
| 47 private: | |
| 48 friend class OfflinePageModelQueryBuilder; | |
| 49 | |
| 50 std::unique_ptr<std::set<std::string>> restricted_to_namespaces_; | |
| 51 | |
| 52 std::pair<Requirement, std::set<int64_t>> offline_ids_; | |
| 53 std::pair<Requirement, std::set<ClientId>> client_ids_; | |
| 54 std::pair<Requirement, std::set<GURL>> urls_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(OfflinePageModelQuery); | |
| 57 }; | |
| 58 | |
| 59 // Used to create an offline page model query. QueryBuilders without | |
| 60 // modifications create queries that allow all pages. | |
| 61 // Can restrict results by policies provided by |ClientPolicyController|, or by | |
| 62 // individual features of pages. Each restriction comes with a |Requirement| | |
| 63 // that can be used to specify whether the input restriction should include or | |
| 64 // exclude matching pages. | |
| 65 class OfflinePageModelQueryBuilder { | |
| 66 public: | |
| 67 using Requirement = OfflinePageModelQuery::Requirement; | |
| 68 | |
| 69 OfflinePageModelQueryBuilder(); | |
| 70 ~OfflinePageModelQueryBuilder(); | |
| 71 | |
| 72 // Sets the offline page IDs that are valid for this request. If called | |
| 73 // multiple times, overwrites previous offline page ID restrictions. | |
| 74 OfflinePageModelQueryBuilder& SetOfflinePageIds( | |
| 75 Requirement requirement, | |
| 76 const std::vector<int64_t>& ids); | |
| 77 | |
| 78 // Sets the client IDs that are valid for this request. If called multiple | |
| 79 // times, overwrites previous client ID restrictions. | |
| 80 OfflinePageModelQueryBuilder& SetClientIds(Requirement requirement, | |
| 81 const std::vector<ClientId>& ids); | |
| 82 | |
| 83 // Sets the URLs that are valid for this request. If called multiple times, | |
| 84 // overwrites previous URL restrictions. | |
| 85 OfflinePageModelQueryBuilder& SetUrls(Requirement requirement, | |
| 86 const std::vector<GURL>& urls); | |
| 87 | |
| 88 // Only include pages whose namespaces satisfy | |
| 89 // ClientPolicyController::IsSupportedByDownload(|namespace|) == | |
| 90 // |supported_by_download| | |
| 91 // Multiple calls overwrite previous ones. | |
| 92 OfflinePageModelQueryBuilder& RequireSupportedByDownload( | |
| 93 Requirement supported_by_download); | |
| 94 | |
| 95 // Only include pages whose namespaces satisfy | |
| 96 // ClientPolicyController::IsShownAsRecentlyVisitedSite(|namespace|) == | |
| 97 // |recently_visited| | |
| 98 // Multiple calls overwrite previous ones. | |
| 99 OfflinePageModelQueryBuilder& RequireShownAsRecentlyVisitedSite( | |
| 100 Requirement recently_visited); | |
| 101 | |
| 102 // Only include pages whose namespaces satisfy | |
| 103 // ClientPolicyController::IsRestrictedToOriginalTab(|namespace|) == | |
| 104 // |original_tab| | |
| 105 // Multiple calls overwrite previous ones. | |
| 106 OfflinePageModelQueryBuilder& RequireRestrictedToOriginalTab( | |
| 107 Requirement original_tab); | |
| 108 | |
| 109 // Builds the query using the namespace policies provided by |controller| | |
| 110 // This resets the internal state. |controller| should not be |nullptr|. | |
| 111 std::unique_ptr<OfflinePageModelQuery> Build( | |
| 112 ClientPolicyController* controller); | |
| 113 | |
| 114 private: | |
| 115 // Intersects the allowed namespaces in query_ with |namespaces|. If | |
| 116 // |inverted| is true, intersects the allowed namespaces with all namespaces | |
| 117 // except those provided in |namespaces|. | |
| 118 void IntersectWithNamespaces(ClientPolicyController* controller, | |
| 119 const std::vector<std::string>& namespaces, | |
| 120 Requirement match_requirement); | |
| 121 | |
| 122 std::pair<Requirement, std::vector<int64_t>> offline_ids_; | |
| 123 std::pair<Requirement, std::vector<ClientId>> client_ids_; | |
| 124 std::pair<Requirement, std::vector<GURL>> urls_; | |
| 125 | |
| 126 Requirement supported_by_download_ = Requirement::UNSET; | |
| 127 Requirement shown_as_recently_visited_site_ = Requirement::UNSET; | |
| 128 Requirement restricted_to_original_tab_ = Requirement::UNSET; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(OfflinePageModelQueryBuilder); | |
| 131 }; | |
| 132 | |
| 133 } // namespace offline_pages | |
| 134 | |
| 135 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_QUERY_H_ | |
| OLD | NEW |