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 namespace { |
| 15 |
| 16 std::vector<std::string> InvertNamespaces( |
| 17 ClientPolicyController* policy_controller, |
| 18 const std::vector<std::string>& namespaces_to_remove) { |
| 19 std::unordered_set<std::string> removal_set(namespaces_to_remove.begin(), |
| 20 namespaces_to_remove.end()); |
| 21 std::vector<std::string> remaining_namespaces; |
| 22 for (auto name_space : policy_controller->GetAllNamespaces()) { |
| 23 if (removal_set.count(name_space) == 0) |
| 24 remaining_namespaces.emplace_back(name_space); |
| 25 } |
| 26 |
| 27 return remaining_namespaces; |
| 28 } |
| 29 } |
| 30 |
| 31 OfflinePageModelQueryBuilder::OfflinePageModelQueryBuilder() |
| 32 : query_(base::MakeUnique<OfflinePageModelQuery>()) {} |
| 33 |
| 34 OfflinePageModelQueryBuilder::~OfflinePageModelQueryBuilder() = default; |
| 35 |
| 36 OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetOfflinePageIds( |
| 37 const std::vector<int64_t>& ids) { |
| 38 query_->offline_ids_ = |
| 39 base::MakeUnique<std::set<int64_t>>(ids.begin(), ids.end()); |
| 40 return *this; |
| 41 } |
| 42 |
| 43 OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetClientIds( |
| 44 const std::vector<ClientId>& ids) { |
| 45 query_->client_ids_ = |
| 46 base::MakeUnique<std::set<ClientId>>(ids.begin(), ids.end()); |
| 47 return *this; |
| 48 } |
| 49 |
| 50 OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetUrls( |
| 51 const std::vector<GURL>& urls) { |
| 52 query_->urls_ = base::MakeUnique<std::set<GURL>>(urls.begin(), urls.end()); |
| 53 return *this; |
| 54 } |
| 55 |
| 56 OfflinePageModelQueryBuilder& |
| 57 OfflinePageModelQueryBuilder::RequireSupportedByDownload( |
| 58 Requirement supported_by_download) { |
| 59 supported_by_download_ = supported_by_download; |
| 60 return *this; |
| 61 } |
| 62 |
| 63 OfflinePageModelQueryBuilder& |
| 64 OfflinePageModelQueryBuilder::RequireShownAsRecentlyVisitedSite( |
| 65 Requirement recently_visited) { |
| 66 shown_as_recently_visited_site_ = recently_visited; |
| 67 return *this; |
| 68 } |
| 69 |
| 70 OfflinePageModelQueryBuilder& |
| 71 OfflinePageModelQueryBuilder::RequireRestrictedToOriginalTab( |
| 72 Requirement original_tab) { |
| 73 restricted_to_original_tab_ = original_tab; |
| 74 return *this; |
| 75 } |
| 76 |
| 77 OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::AllowExpiredPages( |
| 78 bool allow_expired) { |
| 79 query_->allow_expired_ = allow_expired; |
| 80 return *this; |
| 81 } |
| 82 |
| 83 void OfflinePageModelQueryBuilder::IntersectWithNamespaces( |
| 84 ClientPolicyController* controller, |
| 85 const std::vector<std::string>& namespaces, |
| 86 bool invert) { |
| 87 std::vector<std::string> desired_namespaces = |
| 88 invert ? InvertNamespaces(controller, namespaces) : namespaces; |
| 89 if (!query_->restricted_to_namespaces_) { |
| 90 query_->restricted_to_namespaces_ = base::MakeUnique<std::set<std::string>>( |
| 91 desired_namespaces.begin(), desired_namespaces.end()); |
| 92 return; |
| 93 } |
| 94 |
| 95 auto new_policy_set = base::MakeUnique<std::set<std::string>>(); |
| 96 for (auto& name_space : desired_namespaces) { |
| 97 if (query_->restricted_to_namespaces_->count(name_space) > 0) |
| 98 new_policy_set->insert(name_space); |
| 99 } |
| 100 |
| 101 query_->restricted_to_namespaces_.swap(new_policy_set); |
| 102 } |
| 103 |
| 104 std::unique_ptr<OfflinePageModelQuery> OfflinePageModelQueryBuilder::Build( |
| 105 ClientPolicyController* controller) { |
| 106 if (supported_by_download_ != Requirement::Unset) { |
| 107 IntersectWithNamespaces( |
| 108 controller, controller->GetNamespacesSupportedByDownload(), |
| 109 supported_by_download_ == Requirement::OnlyNotMatching /* invert */); |
| 110 supported_by_download_ = Requirement::Unset; |
| 111 } |
| 112 if (shown_as_recently_visited_site_ != Requirement::Unset) { |
| 113 IntersectWithNamespaces( |
| 114 controller, controller->GetNamespacesShownAsRecentlyVisitedSite(), |
| 115 shown_as_recently_visited_site_ == |
| 116 Requirement::OnlyNotMatching /* invert */); |
| 117 shown_as_recently_visited_site_ = Requirement::Unset; |
| 118 } |
| 119 if (restricted_to_original_tab_ != Requirement::Unset) { |
| 120 IntersectWithNamespaces(controller, |
| 121 controller->GetNamespacesRestrictedToOriginalTab(), |
| 122 restricted_to_original_tab_ == |
| 123 Requirement::OnlyNotMatching /* invert */); |
| 124 restricted_to_original_tab_ = Requirement::Unset; |
| 125 } |
| 126 |
| 127 std::unique_ptr<OfflinePageModelQuery> returned_query; |
| 128 // Swaps the current query with an uninitialized one. |
| 129 query_.swap(returned_query); |
| 130 query_ = base::MakeUnique<OfflinePageModelQuery>(); |
| 131 return returned_query; |
| 132 } |
| 133 |
| 134 OfflinePageModelQuery::OfflinePageModelQuery() = default; |
| 135 OfflinePageModelQuery::~OfflinePageModelQuery() = default; |
| 136 |
| 137 bool OfflinePageModelQuery::GetRestrictedToNamespaces( |
| 138 std::set<std::string>* namespaces_out) { |
| 139 if (!restricted_to_namespaces_) |
| 140 return false; |
| 141 |
| 142 *namespaces_out = *restricted_to_namespaces_; |
| 143 return true; |
| 144 } |
| 145 |
| 146 bool OfflinePageModelQuery::GetRestrictedToOfflineIds( |
| 147 std::set<int64_t>* ids_out) { |
| 148 if (!offline_ids_) |
| 149 return false; |
| 150 |
| 151 *ids_out = *offline_ids_; |
| 152 return true; |
| 153 } |
| 154 |
| 155 bool OfflinePageModelQuery::GetRestrictedToClientIds( |
| 156 std::set<ClientId>* ids_out) { |
| 157 if (!client_ids_) |
| 158 return false; |
| 159 |
| 160 *ids_out = *client_ids_; |
| 161 return true; |
| 162 } |
| 163 |
| 164 bool OfflinePageModelQuery::GetRestrictedToUrls(std::set<GURL>* ids_out) { |
| 165 if (!urls_) |
| 166 return false; |
| 167 |
| 168 *ids_out = *urls_; |
| 169 return true; |
| 170 } |
| 171 |
| 172 bool OfflinePageModelQuery::GetAllowExpired() { |
| 173 return allow_expired_; |
| 174 } |
| 175 |
| 176 bool OfflinePageModelQuery::Matches(const OfflinePageItem& item) { |
| 177 if (!allow_expired_ && item.IsExpired()) |
| 178 return false; |
| 179 |
| 180 if (offline_ids_ && offline_ids_->count(item.offline_id) == 0) |
| 181 return false; |
| 182 |
| 183 if (urls_ && urls_->count(item.url) == 0) |
| 184 return false; |
| 185 |
| 186 const ClientId& client_id = item.client_id; |
| 187 if (restricted_to_namespaces_ && |
| 188 restricted_to_namespaces_->count(client_id.name_space) == 0) { |
| 189 return false; |
| 190 } |
| 191 |
| 192 if (client_ids_ && client_ids_->count(client_id) == 0) |
| 193 return false; |
| 194 |
| 195 return true; |
| 196 } |
| 197 |
| 198 } // namespace offline_pages |
OLD | NEW |