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..3586aac33d675a103c322f35cdfc16b395ae8fd7 |
--- /dev/null |
+++ b/components/offline_pages/offline_page_model_query.cc |
@@ -0,0 +1,198 @@ |
+// 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 <algorithm> |
+#include <unordered_set> |
+ |
+#include "base/memory/ptr_util.h" |
+ |
+namespace offline_pages { |
+ |
+namespace { |
+ |
+std::vector<std::string> InvertNamespaces( |
+ ClientPolicyController* policy_controller, |
+ const std::vector<std::string>& namespaces_to_remove) { |
+ std::unordered_set<std::string> removal_set(namespaces_to_remove.begin(), |
+ namespaces_to_remove.end()); |
+ std::vector<std::string> remaining_namespaces; |
+ for (auto name_space : policy_controller->GetAllNamespaces()) { |
+ if (removal_set.count(name_space) == 0) |
+ remaining_namespaces.emplace_back(name_space); |
+ } |
+ |
+ return remaining_namespaces; |
+} |
+} |
+ |
+OfflinePageModelQueryBuilder::OfflinePageModelQueryBuilder() |
+ : query_(base::MakeUnique<OfflinePageModelQuery>()) {} |
+ |
+OfflinePageModelQueryBuilder::~OfflinePageModelQueryBuilder() = default; |
+ |
+OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetOfflinePageIds( |
+ const std::vector<int64_t>& ids) { |
+ query_->offline_ids_ = |
+ base::MakeUnique<std::set<int64_t>>(ids.begin(), ids.end()); |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetClientIds( |
+ const std::vector<ClientId>& ids) { |
+ query_->client_ids_ = |
+ base::MakeUnique<std::set<ClientId>>(ids.begin(), ids.end()); |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetUrls( |
+ const std::vector<GURL>& urls) { |
+ query_->urls_ = base::MakeUnique<std::set<GURL>>(urls.begin(), urls.end()); |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& |
+OfflinePageModelQueryBuilder::RequireSupportedByDownload( |
+ Requirement supported_by_download) { |
+ supported_by_download_ = supported_by_download; |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& |
+OfflinePageModelQueryBuilder::RequireShownAsRecentlyVisitedSite( |
+ Requirement recently_visited) { |
+ shown_as_recently_visited_site_ = recently_visited; |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& |
+OfflinePageModelQueryBuilder::RequireRestrictedToOriginalTab( |
+ Requirement original_tab) { |
+ restricted_to_original_tab_ = original_tab; |
+ return *this; |
+} |
+ |
+OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::AllowExpiredPages( |
+ bool allow_expired) { |
+ query_->allow_expired_ = allow_expired; |
+ return *this; |
+} |
+ |
+void OfflinePageModelQueryBuilder::IntersectWithNamespaces( |
+ ClientPolicyController* controller, |
+ const std::vector<std::string>& namespaces, |
+ bool invert) { |
+ std::vector<std::string> desired_namespaces = |
+ invert ? InvertNamespaces(controller, namespaces) : namespaces; |
+ if (!query_->restricted_to_namespaces_) { |
+ query_->restricted_to_namespaces_ = base::MakeUnique<std::set<std::string>>( |
+ desired_namespaces.begin(), desired_namespaces.end()); |
+ return; |
+ } |
+ |
+ auto new_policy_set = base::MakeUnique<std::set<std::string>>(); |
+ for (auto& name_space : desired_namespaces) { |
+ if (query_->restricted_to_namespaces_->count(name_space) > 0) |
+ new_policy_set->insert(name_space); |
+ } |
+ |
+ query_->restricted_to_namespaces_.swap(new_policy_set); |
+} |
+ |
+std::unique_ptr<OfflinePageModelQuery> OfflinePageModelQueryBuilder::Build( |
+ ClientPolicyController* controller) { |
+ if (supported_by_download_ != Requirement::Unset) { |
+ IntersectWithNamespaces( |
+ controller, controller->GetNamespacesSupportedByDownload(), |
+ supported_by_download_ == Requirement::OnlyNotMatching /* invert */); |
+ supported_by_download_ = Requirement::Unset; |
+ } |
+ if (shown_as_recently_visited_site_ != Requirement::Unset) { |
+ IntersectWithNamespaces( |
+ controller, controller->GetNamespacesShownAsRecentlyVisitedSite(), |
+ shown_as_recently_visited_site_ == |
+ Requirement::OnlyNotMatching /* invert */); |
+ shown_as_recently_visited_site_ = Requirement::Unset; |
+ } |
+ if (restricted_to_original_tab_ != Requirement::Unset) { |
+ IntersectWithNamespaces(controller, |
+ controller->GetNamespacesRestrictedToOriginalTab(), |
+ restricted_to_original_tab_ == |
+ Requirement::OnlyNotMatching /* invert */); |
+ restricted_to_original_tab_ = Requirement::Unset; |
+ } |
+ |
+ std::unique_ptr<OfflinePageModelQuery> returned_query; |
+ // Swaps the current query with an uninitialized one. |
+ 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::GetRestrictedToUrls(std::set<GURL>* ids_out) { |
+ if (!urls_) |
+ return false; |
+ |
+ *ids_out = *urls_; |
+ 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 |