Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: components/offline_pages/offline_page_model_query.cc

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "components/offline_pages/offline_page_model_query.h"
6
7 #include <algorithm>
8 #include <unordered_set>
9
10 #include "base/memory/ptr_util.h"
11
12 namespace offline_pages {
13
14 using Requirement = OfflinePageModelQuery::Requirement;
15
16 OfflinePageModelQueryBuilder::OfflinePageModelQueryBuilder()
17 : offline_ids_(std::make_pair(Requirement::UNSET, std::vector<int64_t>())) {
18 }
19
20 OfflinePageModelQueryBuilder::~OfflinePageModelQueryBuilder() = default;
21
22 OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetOfflinePageIds(
23 Requirement requirement,
24 const std::vector<int64_t>& ids) {
25 offline_ids_ = std::make_pair(requirement, ids);
26 return *this;
27 }
28
29 OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetClientIds(
30 Requirement requirement,
31 const std::vector<ClientId>& ids) {
32 client_ids_ = std::make_pair(requirement, ids);
33 return *this;
34 }
35
36 OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetUrls(
37 Requirement requirement,
38 const std::vector<GURL>& urls) {
39 urls_ = std::make_pair(requirement, urls);
40 return *this;
41 }
42
43 OfflinePageModelQueryBuilder&
44 OfflinePageModelQueryBuilder::RequireSupportedByDownload(
45 Requirement supported_by_download) {
46 supported_by_download_ = supported_by_download;
47 return *this;
48 }
49
50 OfflinePageModelQueryBuilder&
51 OfflinePageModelQueryBuilder::RequireShownAsRecentlyVisitedSite(
52 Requirement recently_visited) {
53 shown_as_recently_visited_site_ = recently_visited;
54 return *this;
55 }
56
57 OfflinePageModelQueryBuilder&
58 OfflinePageModelQueryBuilder::RequireRestrictedToOriginalTab(
59 Requirement original_tab) {
60 restricted_to_original_tab_ = original_tab;
61 return *this;
62 }
63
64 std::unique_ptr<OfflinePageModelQuery> OfflinePageModelQueryBuilder::Build(
65 ClientPolicyController* controller) {
66 DCHECK(controller);
67
68 auto query = base::MakeUnique<OfflinePageModelQuery>();
69
70 query->urls_ = std::make_pair(
71 urls_.first, std::set<GURL>(urls_.second.begin(), urls_.second.end()));
72 urls_ = std::make_pair(Requirement::UNSET, std::vector<GURL>());
73 query->offline_ids_ = std::make_pair(
74 offline_ids_.first, std::set<int64_t>(offline_ids_.second.begin(),
75 offline_ids_.second.end()));
76 offline_ids_ = std::make_pair(Requirement::UNSET, std::vector<int64_t>());
77 query->client_ids_ = std::make_pair(
78 client_ids_.first,
79 std::set<ClientId>(client_ids_.second.begin(), client_ids_.second.end()));
80 client_ids_ = std::make_pair(Requirement::UNSET, std::vector<ClientId>());
81
82 std::vector<std::string> allowed_namespaces;
83 bool uses_namespace_restrictions = false;
84
85 for (auto& name_space : controller->GetAllNamespaces()) {
86 // If any exclusion requirements exist, and the namespace matches one of
87 // those excluded by policy, skip adding it to |allowed_namespaces|.
88 if ((supported_by_download_ == Requirement::EXCLUDE_MATCHING &&
89 controller->IsSupportedByDownload(name_space)) ||
90 (shown_as_recently_visited_site_ == Requirement::EXCLUDE_MATCHING &&
91 controller->IsShownAsRecentlyVisitedSite(name_space)) ||
92 (restricted_to_original_tab_ == Requirement::EXCLUDE_MATCHING &&
93 controller->IsRestrictedToOriginalTab(name_space))) {
94 // Skip namespaces that meet exclusion requirements.
95 uses_namespace_restrictions = true;
96 continue;
97 }
98
99 if ((supported_by_download_ == Requirement::INCLUDE_MATCHING &&
100 !controller->IsSupportedByDownload(name_space)) ||
101 (shown_as_recently_visited_site_ == Requirement::INCLUDE_MATCHING &&
102 !controller->IsShownAsRecentlyVisitedSite(name_space)) ||
103 (restricted_to_original_tab_ == Requirement::INCLUDE_MATCHING &&
104 !controller->IsRestrictedToOriginalTab(name_space))) {
105 // Skip namespaces that don't meet inclusion requirement.
106 uses_namespace_restrictions = true;
107 continue;
108 }
109
110 // Add namespace otherwise.
111 allowed_namespaces.emplace_back(name_space);
112 }
113
114 supported_by_download_ = Requirement::UNSET;
115 shown_as_recently_visited_site_ = Requirement::UNSET;
116 restricted_to_original_tab_ = Requirement::UNSET;
117
118 if (uses_namespace_restrictions) {
119 query->restricted_to_namespaces_ = base::MakeUnique<std::set<std::string>>(
120 allowed_namespaces.begin(), allowed_namespaces.end());
121 }
122
123 return query;
124 }
125
126 OfflinePageModelQuery::OfflinePageModelQuery() = default;
127 OfflinePageModelQuery::~OfflinePageModelQuery() = default;
128
129 std::pair<bool, std::set<std::string>>
130 OfflinePageModelQuery::GetRestrictedToNamespaces() const {
131 if (!restricted_to_namespaces_)
132 return std::make_pair(false, std::set<std::string>());
133
134 return std::make_pair(true, *restricted_to_namespaces_);
135 }
136
137 std::pair<Requirement, std::set<int64_t>>
138 OfflinePageModelQuery::GetRestrictedToOfflineIds() const {
139 if (offline_ids_.first == Requirement::UNSET)
140 return std::make_pair(Requirement::UNSET, std::set<int64_t>());
141
142 return offline_ids_;
143 }
144
145 std::pair<Requirement, std::set<ClientId>>
146 OfflinePageModelQuery::GetRestrictedToClientIds() const {
147 if (client_ids_.first == Requirement::UNSET)
148 return std::make_pair(Requirement::UNSET, std::set<ClientId>());
149
150 return client_ids_;
151 }
152
153 std::pair<Requirement, std::set<GURL>>
154 OfflinePageModelQuery::GetRestrictedToUrls() const {
155 if (urls_.first == Requirement::UNSET)
156 return std::make_pair(Requirement::UNSET, std::set<GURL>());
157
158 return urls_;
159 }
160
161 bool OfflinePageModelQuery::Matches(const OfflinePageItem& item) const {
162 switch (offline_ids_.first) {
163 case Requirement::UNSET:
164 break;
165 case Requirement::INCLUDE_MATCHING:
166 if (offline_ids_.second.count(item.offline_id) == 0)
167 return false;
168 break;
169 case Requirement::EXCLUDE_MATCHING:
170 if (offline_ids_.second.count(item.offline_id) > 0)
171 return false;
172 break;
173 }
174
175 switch (urls_.first) {
176 case Requirement::UNSET:
177 break;
178 case Requirement::INCLUDE_MATCHING:
179 if (urls_.second.count(item.url) == 0)
180 return false;
181 break;
182 case Requirement::EXCLUDE_MATCHING:
183 if (urls_.second.count(item.url) > 0)
184 return false;
185 break;
186 }
187
188 const ClientId& client_id = item.client_id;
189 if (restricted_to_namespaces_ &&
190 restricted_to_namespaces_->count(client_id.name_space) == 0) {
191 return false;
192 }
193
194 switch (client_ids_.first) {
195 case Requirement::UNSET:
196 break;
197 case Requirement::INCLUDE_MATCHING:
198 if (client_ids_.second.count(client_id) == 0)
199 return false;
200 break;
201 case Requirement::EXCLUDE_MATCHING:
202 if (client_ids_.second.count(client_id) > 0)
203 return false;
204 break;
205 }
206
207 return true;
208 }
209
210 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model_query.h ('k') | components/offline_pages/offline_page_model_query_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698