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 bool GetAllowExpired() const; | |
44 | |
45 // This is the workhorse function that is used by the in-memory offline page | |
46 // model, given a page it will find out whether that page matches the query. | |
47 bool Matches(const OfflinePageItem& page) const; | |
48 | |
49 private: | |
50 friend class OfflinePageModelQueryBuilder; | |
51 | |
52 bool allow_expired_ = false; | |
53 | |
54 std::unique_ptr<std::set<std::string>> restricted_to_namespaces_; | |
55 | |
56 std::pair<Requirement, std::set<int64_t>> offline_ids_; | |
57 std::pair<Requirement, std::set<ClientId>> client_ids_; | |
58 std::pair<Requirement, std::set<GURL>> urls_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(OfflinePageModelQuery); | |
61 }; | |
62 | |
63 // Used to create an offline page model query. QueryBuilders without | |
64 // modifications create queries that allow all pages that are not expired. | |
65 // Can restrict results by policies provided by |ClientPolicyController|, or by | |
66 // individual features of pages. Each restriction comes with a |Requirement| | |
67 // that can be used to specify whether the input restriction should include or | |
68 // exclude matching pages. | |
69 class OfflinePageModelQueryBuilder { | |
70 public: | |
71 using Requirement = OfflinePageModelQuery::Requirement; | |
72 | |
73 OfflinePageModelQueryBuilder(); | |
74 ~OfflinePageModelQueryBuilder(); | |
75 | |
76 // Sets the offline page IDs that are valid for this request. If called | |
77 // multiple times, overwrites previous offline page ID restrictions. | |
78 OfflinePageModelQueryBuilder& SetOfflinePageIds( | |
79 Requirement requirement, | |
80 const std::vector<int64_t>& ids); | |
81 | |
82 // Sets the client IDs that are valid for this request. If called multiple | |
83 // times, overwrites previous client ID restrictions. | |
84 OfflinePageModelQueryBuilder& SetClientIds(Requirement requirement, | |
85 const std::vector<ClientId>& ids); | |
86 | |
87 // Sets the URLs that are valid for this request. If called multiple times, | |
88 // overwrites previous URL restrictions. | |
89 OfflinePageModelQueryBuilder& SetUrls(Requirement requirement, | |
90 const std::vector<GURL>& urls); | |
91 | |
92 // Only include pages whose namespaces satisfy | |
93 // ClientPolicyController::IsSupportedByDownload(|namespace|) == | |
94 // |supported_by_download| | |
95 // Multiple calls overwrite previous ones. | |
96 OfflinePageModelQueryBuilder& RequireSupportedByDownload( | |
97 Requirement supported_by_download); | |
98 | |
99 // Only include pages whose namespaces satisfy | |
100 // ClientPolicyController::IsShownAsRecentlyVisitedSite(|namespace|) == | |
101 // |recently_visited| | |
102 // Multiple calls overwrite previous ones. | |
103 OfflinePageModelQueryBuilder& RequireShownAsRecentlyVisitedSite( | |
104 Requirement recently_visited); | |
105 | |
106 // Only include pages whose namespaces satisfy | |
107 // ClientPolicyController::IsRestrictedToOriginalTab(|namespace|) == | |
108 // |original_tab| | |
109 // Multiple calls overwrite previous ones. | |
110 OfflinePageModelQueryBuilder& RequireRestrictedToOriginalTab( | |
111 Requirement original_tab); | |
112 | |
113 // Resets whether we return expired pages. If called multiple times the bit | |
114 // is overwritten and |allow_expired| from the last call is saved. | |
115 OfflinePageModelQueryBuilder& AllowExpiredPages(bool allow_expired); | |
116 | |
117 // Builds the query using the namespace policies provided by |controller| | |
118 // This resets the internal state. |controller| should not be |nullptr|. | |
119 std::unique_ptr<OfflinePageModelQuery> Build( | |
120 ClientPolicyController* controller); | |
121 | |
122 private: | |
123 // Intersects the allowed namespaces in query_ with |namespaces|. If | |
124 // |inverted| is true, intersects the allowed namespaces with all namespaces | |
125 // except those provided in |namespaces|. | |
126 void IntersectWithNamespaces(ClientPolicyController* controller, | |
127 const std::vector<std::string>& namespaces, | |
128 Requirement match_requirement); | |
129 | |
130 std::pair<Requirement, std::vector<int64_t>> offline_ids_; | |
131 std::pair<Requirement, std::vector<ClientId>> client_ids_; | |
132 std::pair<Requirement, std::vector<GURL>> urls_; | |
133 | |
134 Requirement supported_by_download_ = Requirement::UNSET; | |
135 Requirement shown_as_recently_visited_site_ = Requirement::UNSET; | |
136 Requirement restricted_to_original_tab_ = Requirement::UNSET; | |
137 | |
138 bool allow_expired_ = false; | |
139 | |
140 DISALLOW_COPY_AND_ASSIGN(OfflinePageModelQueryBuilder); | |
141 }; | |
142 | |
143 } // namespace offline_pages | |
144 | |
145 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_QUERY_H_ | |
OLD | NEW |