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