Index: components/offline_pages/offline_page_model_query.cc |
diff --git a/components/offline_pages/offline_page_model_query.cc b/components/offline_pages/offline_page_model_query.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bd191ee97a86a21e58e254303fac001b2e64a5b3 |
--- /dev/null |
+++ b/components/offline_pages/offline_page_model_query.cc |
@@ -0,0 +1,204 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/offline_pages/offline_page_model_query.h" |
+ |
+#include "base/memory/ptr_util.h" |
+ |
+namespace offline_pages { |
+ |
+OfflinePageModelQueryBuilder::OfflinePageModelQueryBuilder( |
+ ClientPolicyController* policy_controller) |
+ : query_(base::MakeUnique<OfflinePageModelQuery>()), |
+ policy_controller_(policy_controller) {} |
+ |
+OfflinePageModelQueryBuilder::~OfflinePageModelQueryBuilder() = default; |
+ |
+OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetOfflinePageIds( |
+ const std::vector<int64_t>& ids) { |
+ if (!query_->offline_ids_) { |
+ query_->offline_ids_ = |
+ base::MakeUnique<std::set<int64_t>>(ids.begin(), ids.end()); |
+ return *this; |
+ } |
+ |
+ auto new_id_set = base::MakeUnique<std::set<int64_t>>(); |
+ for (int64_t id : ids) { |
+ if (query_->offline_ids_->count(id) > 0) |
+ new_id_set->insert(id); |
+ } |
+ |
+ DCHECK(!new_id_set->empty()) << "Restricted query to zero offline page IDs"; |
+ |
+ query_->offline_ids_.swap(new_id_set); |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetClientIds( |
+ const std::vector<ClientId>& ids) { |
+ if (!query_->client_ids_) { |
+ query_->client_ids_ = |
+ base::MakeUnique<std::set<ClientId>>(ids.begin(), ids.end()); |
+ return *this; |
+ } |
+ |
+ auto new_id_set = base::MakeUnique<std::set<ClientId>>(); |
+ for (ClientId id : ids) { |
+ if (query_->client_ids_->count(id) > 0) |
+ new_id_set->insert(id); |
+ } |
+ |
+ DCHECK(!new_id_set->empty()) << "Restricted query to zero offline page IDs"; |
+ |
+ query_->client_ids_.swap(new_id_set); |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetUrls( |
+ const std::vector<GURL>& urls) { |
+ if (!query_->urls_) { |
+ query_->urls_ = base::MakeUnique<std::set<GURL>>(urls.begin(), urls.end()); |
+ return *this; |
+ } |
+ |
+ auto new_url_set = base::MakeUnique<std::set<GURL>>(); |
+ for (GURL url : urls) { |
+ if (query_->urls_->count(url) > 0) |
+ new_url_set->insert(url); |
+ } |
+ |
+ DCHECK(!new_url_set->empty()) << "Restricted query to zero URLs"; |
+ |
+ query_->urls_.swap(new_url_set); |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& |
+OfflinePageModelQueryBuilder::RequireSupportedByDownload( |
+ bool supported_by_download) { |
+ std::vector<std::string> supported = |
+ policy_controller_->GetNamespacesSupportedByDownload(); |
+ std::vector<std::string> namespaces = |
+ supported_by_download |
+ ? supported |
+ : policy_controller_->GetAllNamespacesExcept(supported); |
+ IntersectWithNamespaces(namespaces); |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& |
+OfflinePageModelQueryBuilder::RequireShownAsRecentlyVisitedSite( |
+ bool recently_visited) { |
+ std::vector<std::string> supported = |
+ policy_controller_->GetNamespacesShownAsRecentlyVisitedSite(); |
+ std::vector<std::string> namespaces = |
+ recently_visited ? supported |
+ : policy_controller_->GetAllNamespacesExcept(supported); |
+ IntersectWithNamespaces(namespaces); |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& |
+OfflinePageModelQueryBuilder::RequireRestrictedToOriginalTab( |
+ bool original_tab) { |
+ std::vector<std::string> supported = |
+ policy_controller_->GetNamespacesRestrictedToOriginalTab(); |
+ std::vector<std::string> namespaces = |
+ original_tab ? supported |
+ : policy_controller_->GetAllNamespacesExcept(supported); |
+ IntersectWithNamespaces(namespaces); |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::AllowExpiredPages( |
+ bool allow_expired) { |
+ query_->allow_expired_ = allow_expired; |
+ return *this; |
+} |
+ |
+void OfflinePageModelQueryBuilder::IntersectWithNamespaces( |
+ const std::vector<std::string>& namespaces) { |
+ if (!query_->restricted_to_namespaces_) { |
+ query_->restricted_to_namespaces_ = base::MakeUnique<std::set<std::string>>( |
+ namespaces.begin(), namespaces.end()); |
+ return; |
+ } |
+ |
+ auto new_policy_set = base::MakeUnique<std::set<std::string>>(); |
+ for (auto& name_space : namespaces) { |
+ if (query_->restricted_to_namespaces_->count(name_space) > 0) |
+ new_policy_set->insert(name_space); |
+ } |
+ |
+ DCHECK(!new_policy_set->empty()) |
+ << "Restricted query to zero valid policies."; |
+ |
+ query_->restricted_to_namespaces_.swap(new_policy_set); |
+} |
+ |
+std::unique_ptr<OfflinePageModelQuery> OfflinePageModelQueryBuilder::Build() { |
+ // Swaps the current query with an uninitialized one. |
+ std::unique_ptr<OfflinePageModelQuery> returned_query; |
+ query_.swap(returned_query); |
+ query_ = base::MakeUnique<OfflinePageModelQuery>(); |
+ return returned_query; |
+} |
+ |
+OfflinePageModelQuery::OfflinePageModelQuery() = default; |
+OfflinePageModelQuery::~OfflinePageModelQuery() = default; |
+ |
+bool OfflinePageModelQuery::GetRestrictedToNamespaces( |
+ std::set<std::string>* namespaces_out) { |
+ if (!restricted_to_namespaces_) |
+ return false; |
+ |
+ *namespaces_out = *restricted_to_namespaces_; |
+ return true; |
+} |
+ |
+bool OfflinePageModelQuery::GetRestrictedToOfflineIds( |
+ std::set<int64_t>* ids_out) { |
+ if (!offline_ids_) |
+ return false; |
+ |
+ *ids_out = *offline_ids_; |
+ return true; |
+} |
+ |
+bool OfflinePageModelQuery::GetRestrictedToClientIds( |
+ std::set<ClientId>* ids_out) { |
+ if (!client_ids_) |
+ return false; |
+ |
+ *ids_out = *client_ids_; |
+ return true; |
+} |
+ |
+bool OfflinePageModelQuery::GetAllowExpired() { |
+ return allow_expired_; |
+} |
+ |
+bool OfflinePageModelQuery::Matches(const OfflinePageItem& item) { |
+ if (!allow_expired_ && item.IsExpired()) |
+ return false; |
+ |
+ if (offline_ids_ && offline_ids_->count(item.offline_id) == 0) |
+ return false; |
+ |
+ if (urls_ && urls_->count(item.url) == 0) |
+ return false; |
+ |
+ const ClientId& client_id = item.client_id; |
+ if (restricted_to_namespaces_ && |
+ restricted_to_namespaces_->count(client_id.name_space) == 0) { |
+ return false; |
+ } |
+ |
+ if (client_ids_ && client_ids_->count(client_id) == 0) |
+ return false; |
+ |
+ return true; |
+} |
+ |
+} // namespace offline_pages |