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 #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 OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::AllowExpiredPages( |
| 65 bool allow_expired) { |
| 66 allow_expired_ = allow_expired; |
| 67 return *this; |
| 68 } |
| 69 |
| 70 std::unique_ptr<OfflinePageModelQuery> OfflinePageModelQueryBuilder::Build( |
| 71 ClientPolicyController* controller) { |
| 72 DCHECK(controller); |
| 73 |
| 74 auto query = base::MakeUnique<OfflinePageModelQuery>(); |
| 75 |
| 76 query->allow_expired_ = allow_expired_; |
| 77 query->urls_ = std::make_pair( |
| 78 urls_.first, std::set<GURL>(urls_.second.begin(), urls_.second.end())); |
| 79 urls_ = std::make_pair(Requirement::UNSET, std::vector<GURL>()); |
| 80 query->offline_ids_ = std::make_pair( |
| 81 offline_ids_.first, std::set<int64_t>(offline_ids_.second.begin(), |
| 82 offline_ids_.second.end())); |
| 83 offline_ids_ = std::make_pair(Requirement::UNSET, std::vector<int64_t>()); |
| 84 query->client_ids_ = std::make_pair( |
| 85 client_ids_.first, |
| 86 std::set<ClientId>(client_ids_.second.begin(), client_ids_.second.end())); |
| 87 client_ids_ = std::make_pair(Requirement::UNSET, std::vector<ClientId>()); |
| 88 |
| 89 std::vector<std::string> allowed_namespaces; |
| 90 bool uses_namespace_restrictions = false; |
| 91 |
| 92 for (auto& name_space : controller->GetAllNamespaces()) { |
| 93 // If any exclusion requirements exist, and the namespace matches one of |
| 94 // those excluded by policy, skip adding it to |allowed_namespaces|. |
| 95 if ((supported_by_download_ == Requirement::EXCLUDE_MATCHING && |
| 96 controller->IsSupportedByDownload(name_space)) || |
| 97 (shown_as_recently_visited_site_ == Requirement::EXCLUDE_MATCHING && |
| 98 controller->IsShownAsRecentlyVisitedSite(name_space)) || |
| 99 (restricted_to_original_tab_ == Requirement::EXCLUDE_MATCHING && |
| 100 controller->IsRestrictedToOriginalTab(name_space))) { |
| 101 // Skip namespaces that meet exclusion requirements. |
| 102 uses_namespace_restrictions = true; |
| 103 continue; |
| 104 } |
| 105 |
| 106 if ((supported_by_download_ == Requirement::INCLUDE_MATCHING && |
| 107 !controller->IsSupportedByDownload(name_space)) || |
| 108 (shown_as_recently_visited_site_ == Requirement::INCLUDE_MATCHING && |
| 109 !controller->IsShownAsRecentlyVisitedSite(name_space)) || |
| 110 (restricted_to_original_tab_ == Requirement::INCLUDE_MATCHING && |
| 111 !controller->IsRestrictedToOriginalTab(name_space))) { |
| 112 // Skip namespaces that don't meet inclusion requirement. |
| 113 uses_namespace_restrictions = true; |
| 114 continue; |
| 115 } |
| 116 |
| 117 // Add namespace otherwise. |
| 118 allowed_namespaces.emplace_back(name_space); |
| 119 } |
| 120 |
| 121 supported_by_download_ = Requirement::UNSET; |
| 122 shown_as_recently_visited_site_ = Requirement::UNSET; |
| 123 restricted_to_original_tab_ = Requirement::UNSET; |
| 124 |
| 125 if (uses_namespace_restrictions) { |
| 126 query->restricted_to_namespaces_ = base::MakeUnique<std::set<std::string>>( |
| 127 allowed_namespaces.begin(), allowed_namespaces.end()); |
| 128 } |
| 129 |
| 130 return query; |
| 131 } |
| 132 |
| 133 OfflinePageModelQuery::OfflinePageModelQuery() = default; |
| 134 OfflinePageModelQuery::~OfflinePageModelQuery() = default; |
| 135 |
| 136 std::pair<bool, std::set<std::string>> |
| 137 OfflinePageModelQuery::GetRestrictedToNamespaces() const { |
| 138 if (!restricted_to_namespaces_) |
| 139 return std::make_pair(false, std::set<std::string>()); |
| 140 |
| 141 return std::make_pair(true, *restricted_to_namespaces_); |
| 142 } |
| 143 |
| 144 std::pair<Requirement, std::set<int64_t>> |
| 145 OfflinePageModelQuery::GetRestrictedToOfflineIds() const { |
| 146 if (offline_ids_.first == Requirement::UNSET) |
| 147 return std::make_pair(Requirement::UNSET, std::set<int64_t>()); |
| 148 |
| 149 return offline_ids_; |
| 150 } |
| 151 |
| 152 std::pair<Requirement, std::set<ClientId>> |
| 153 OfflinePageModelQuery::GetRestrictedToClientIds() const { |
| 154 if (client_ids_.first == Requirement::UNSET) |
| 155 return std::make_pair(Requirement::UNSET, std::set<ClientId>()); |
| 156 |
| 157 return client_ids_; |
| 158 } |
| 159 |
| 160 std::pair<Requirement, std::set<GURL>> |
| 161 OfflinePageModelQuery::GetRestrictedToUrls() const { |
| 162 if (urls_.first == Requirement::UNSET) |
| 163 return std::make_pair(Requirement::UNSET, std::set<GURL>()); |
| 164 |
| 165 return urls_; |
| 166 } |
| 167 |
| 168 bool OfflinePageModelQuery::GetAllowExpired() const { |
| 169 return allow_expired_; |
| 170 } |
| 171 |
| 172 bool OfflinePageModelQuery::Matches(const OfflinePageItem& item) const { |
| 173 if (!allow_expired_ && item.IsExpired()) |
| 174 return false; |
| 175 |
| 176 switch (offline_ids_.first) { |
| 177 case Requirement::UNSET: |
| 178 break; |
| 179 case Requirement::INCLUDE_MATCHING: |
| 180 if (offline_ids_.second.count(item.offline_id) == 0) |
| 181 return false; |
| 182 break; |
| 183 case Requirement::EXCLUDE_MATCHING: |
| 184 if (offline_ids_.second.count(item.offline_id) > 0) |
| 185 return false; |
| 186 break; |
| 187 } |
| 188 |
| 189 switch (urls_.first) { |
| 190 case Requirement::UNSET: |
| 191 break; |
| 192 case Requirement::INCLUDE_MATCHING: |
| 193 if (urls_.second.count(item.url) == 0) |
| 194 return false; |
| 195 break; |
| 196 case Requirement::EXCLUDE_MATCHING: |
| 197 if (urls_.second.count(item.url) > 0) |
| 198 return false; |
| 199 break; |
| 200 } |
| 201 |
| 202 const ClientId& client_id = item.client_id; |
| 203 if (restricted_to_namespaces_ && |
| 204 restricted_to_namespaces_->count(client_id.name_space) == 0) { |
| 205 return false; |
| 206 } |
| 207 |
| 208 switch (client_ids_.first) { |
| 209 case Requirement::UNSET: |
| 210 break; |
| 211 case Requirement::INCLUDE_MATCHING: |
| 212 if (client_ids_.second.count(client_id) == 0) |
| 213 return false; |
| 214 break; |
| 215 case Requirement::EXCLUDE_MATCHING: |
| 216 if (client_ids_.second.count(client_id) > 0) |
| 217 return false; |
| 218 break; |
| 219 } |
| 220 |
| 221 return true; |
| 222 } |
| 223 |
| 224 } // namespace offline_pages |
OLD | NEW |