Chromium Code Reviews| 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..6f8080ebad6b08785557c9ec23b761c476f494d5 |
| --- /dev/null |
| +++ b/components/offline_pages/offline_page_model_query.cc |
| @@ -0,0 +1,257 @@ |
| +// 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 { |
| + |
| +using Requirement = OfflinePageModelQuery::Requirement; |
| + |
| +namespace { |
| + |
| +void SubtractNamespaces(std::vector<std::string>* namespaces, |
| + const std::vector<std::string>& to_remove) { |
| + std::set<std::string> remove_set(to_remove.begin(), to_remove.end()); |
| + auto found = [remove_set](const std::string& name_space) { |
| + return remove_set.count(name_space) > 0; |
| + }; |
| + namespaces->erase( |
| + std::remove_if(namespaces->begin(), namespaces->end(), found), |
| + namespaces->end()); |
| +} |
| + |
| +void IntersectNamespaces(std::vector<std::string>* namespaces, |
| + const std::vector<std::string>& intersect_with) { |
| + std::set<std::string> intersect_set(intersect_with.begin(), |
| + intersect_with.end()); |
| + auto missing = [intersect_set](const std::string& name_space) { |
| + return intersect_set.count(name_space) == 0; |
| + }; |
| + namespaces->erase( |
| + std::remove_if(namespaces->begin(), namespaces->end(), missing), |
| + namespaces->end()); |
| +} |
| + |
| +} // namespace |
| + |
| +OfflinePageModelQueryBuilder::OfflinePageModelQueryBuilder() |
| + : offline_ids_(std::make_pair(Requirement::UNSET, std::vector<int64_t>())) { |
| +} |
| + |
| +OfflinePageModelQueryBuilder::~OfflinePageModelQueryBuilder() = default; |
| + |
| +OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetOfflinePageIds( |
| + Requirement requirement, |
| + const std::vector<int64_t>& ids) { |
| + offline_ids_ = std::make_pair(requirement, ids); |
| + return *this; |
| +} |
| + |
| +OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetClientIds( |
| + Requirement requirement, |
| + const std::vector<ClientId>& ids) { |
| + client_ids_ = std::make_pair(requirement, ids); |
| + return *this; |
| +} |
| + |
| +OfflinePageModelQueryBuilder& OfflinePageModelQueryBuilder::SetUrls( |
| + Requirement requirement, |
| + const std::vector<GURL>& urls) { |
| + urls_ = std::make_pair(requirement, urls); |
| + 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) { |
| + allow_expired_ = allow_expired; |
| + return *this; |
| +} |
| + |
| +std::unique_ptr<OfflinePageModelQuery> OfflinePageModelQueryBuilder::Build( |
| + ClientPolicyController* controller) { |
|
fgorski
2016/10/26 18:05:42
dcheck controller
dewittj
2016/10/27 22:49:18
Done.
|
| + auto query = base::MakeUnique<OfflinePageModelQuery>(); |
| + |
| + query->allow_expired_ = allow_expired_; |
| + query->urls_ = std::make_pair( |
| + urls_.first, std::set<GURL>(urls_.second.begin(), urls_.second.end())); |
| + urls_ = std::make_pair(Requirement::UNSET, std::vector<GURL>()); |
| + query->offline_ids_ = std::make_pair( |
| + offline_ids_.first, std::set<int64_t>(offline_ids_.second.begin(), |
| + offline_ids_.second.end())); |
| + offline_ids_ = std::make_pair(Requirement::UNSET, std::vector<int64_t>()); |
| + query->client_ids_ = std::make_pair( |
| + client_ids_.first, |
| + std::set<ClientId>(client_ids_.second.begin(), client_ids_.second.end())); |
| + client_ids_ = std::make_pair(Requirement::UNSET, std::vector<ClientId>()); |
| + |
| + std::vector<std::string> allowed_namespaces = controller->GetAllNamespaces(); |
|
fgorski
2016/10/26 18:05:42
this can wait until you know you have something in
dewittj
2016/10/27 22:49:18
Acknowledged.
|
| + |
| + std::vector<std::pair<Requirement, std::vector<std::string>>> op_list; |
| + if (supported_by_download_ != Requirement::UNSET) { |
| + op_list.emplace_back( |
| + std::make_pair(supported_by_download_, |
| + controller->GetNamespacesSupportedByDownload())); |
| + } |
| + if (shown_as_recently_visited_site_ != Requirement::UNSET) { |
| + op_list.emplace_back( |
| + std::make_pair(shown_as_recently_visited_site_, |
| + controller->GetNamespacesShownAsRecentlyVisitedSite())); |
| + } |
| + if (restricted_to_original_tab_ != Requirement::UNSET) { |
| + op_list.emplace_back( |
| + std::make_pair(restricted_to_original_tab_, |
| + controller->GetNamespacesRestrictedToOriginalTab())); |
| + } |
| + |
| + for (auto& requirement_namespace_pair : op_list) { |
| + Requirement r = requirement_namespace_pair.first; |
| + const std::vector<std::string>& namespaces = |
| + requirement_namespace_pair.second; |
| + |
| + switch (r) { |
| + case Requirement::UNSET: |
| + break; |
| + case Requirement::INCLUDE_MATCHING: |
|
fgorski
2016/10/26 18:46:02
suggestion split in 2 ifs. Please check if that pa
dewittj
2016/10/27 22:49:18
Done. I like that better, thanks!
|
| + IntersectNamespaces(&allowed_namespaces, namespaces); |
| + break; |
| + case Requirement::EXCLUDE_MATCHING: |
| + SubtractNamespaces(&allowed_namespaces, namespaces); |
| + break; |
| + } |
| + } |
| + |
| + supported_by_download_ = Requirement::UNSET; |
| + shown_as_recently_visited_site_ = Requirement::UNSET; |
| + restricted_to_original_tab_ = Requirement::UNSET; |
| + |
| + if (op_list.size() > 0) { |
| + query->restricted_to_namespaces_ = base::MakeUnique<std::set<std::string>>( |
| + allowed_namespaces.begin(), allowed_namespaces.end()); |
| + } |
| + |
| + return 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; |
| +} |
| + |
| +Requirement OfflinePageModelQuery::GetRestrictedToOfflineIds( |
| + std::set<int64_t>* ids_out) { |
| + if (offline_ids_.first == Requirement::UNSET) |
| + return Requirement::UNSET; |
| + |
| + *ids_out = offline_ids_.second; |
| + return offline_ids_.first; |
| +} |
| + |
| +Requirement OfflinePageModelQuery::GetRestrictedToClientIds( |
| + std::set<ClientId>* ids_out) { |
| + if (client_ids_.first == Requirement::UNSET) |
| + return Requirement::UNSET; |
| + |
| + *ids_out = client_ids_.second; |
| + return client_ids_.first; |
| +} |
| + |
| +Requirement OfflinePageModelQuery::GetRestrictedToUrls( |
| + std::set<GURL>* urls_out) { |
| + if (urls_.first == Requirement::UNSET) |
| + return Requirement::UNSET; |
| + |
| + *urls_out = urls_.second; |
| + return urls_.first; |
| +} |
| + |
| +bool OfflinePageModelQuery::GetAllowExpired() { |
| + return allow_expired_; |
| +} |
| + |
| +bool OfflinePageModelQuery::Matches(const OfflinePageItem& item) { |
| + if (!allow_expired_ && item.IsExpired()) |
| + return false; |
| + |
| + switch (offline_ids_.first) { |
| + case Requirement::UNSET: |
| + break; |
| + case Requirement::INCLUDE_MATCHING: |
| + if (offline_ids_.second.count(item.offline_id) == 0) |
| + return false; |
| + break; |
| + case Requirement::EXCLUDE_MATCHING: |
| + if (offline_ids_.second.count(item.offline_id) > 0) |
| + return false; |
| + break; |
| + } |
| + |
| + switch (urls_.first) { |
| + case Requirement::UNSET: |
| + break; |
| + case Requirement::INCLUDE_MATCHING: |
| + if (urls_.second.count(item.url) == 0) |
| + return false; |
| + break; |
| + case Requirement::EXCLUDE_MATCHING: |
| + if (urls_.second.count(item.url) > 0) |
| + return false; |
| + break; |
| + } |
| + |
| + const ClientId& client_id = item.client_id; |
| + if (restricted_to_namespaces_ && |
| + restricted_to_namespaces_->count(client_id.name_space) == 0) { |
| + return false; |
| + } |
| + |
| + switch (client_ids_.first) { |
| + case Requirement::UNSET: |
| + break; |
| + case Requirement::INCLUDE_MATCHING: |
| + if (client_ids_.second.count(client_id) == 0) |
| + return false; |
| + break; |
| + case Requirement::EXCLUDE_MATCHING: |
| + if (client_ids_.second.count(client_id) > 0) |
| + return false; |
| + break; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +} // namespace offline_pages |