Chromium Code Reviews| Index: components/offline_pages/offline_page_model_query.h |
| diff --git a/components/offline_pages/offline_page_model_query.h b/components/offline_pages/offline_page_model_query.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c664d78002230442046d2a18132470df008e864b |
| --- /dev/null |
| +++ b/components/offline_pages/offline_page_model_query.h |
| @@ -0,0 +1,123 @@ |
| +// 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. |
| + |
| +#ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_QUERY_H_ |
| +#define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_QUERY_H_ |
| + |
| +#include <set> |
| +#include <vector> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "components/offline_pages/client_policy_controller.h" |
| +#include "components/offline_pages/offline_page_item.h" |
| +#include "components/offline_pages/offline_page_types.h" |
| + |
| +namespace offline_pages { |
| + |
| +// Can be used by OfflinePageModel instances to direct a query of the model. |
| +class OfflinePageModelQuery { |
| + public: |
| + OfflinePageModelQuery(); |
| + virtual ~OfflinePageModelQuery(); |
| + |
| + bool GetRestrictedToOfflineIds(std::set<int64_t>* ids_out); |
| + bool GetRestrictedToNamespaces(std::set<std::string>* namespaces_out); |
| + bool GetRestrictedToClientIds(std::set<ClientId>* ids_out); |
| + bool GetRestrictedToUrls(std::set<GURL>* ids_out); |
| + bool GetAllowExpired(); |
| + |
| + // This is the workhorse function that is used by the in-memory offline page |
| + // model, given a page it will find out whether that page matches the query. |
| + bool Matches(const OfflinePageItem& page); |
| + |
| + private: |
| + friend class OfflinePageModelQueryBuilder; |
| + |
| + bool allow_expired_ = false; |
| + std::unique_ptr<std::set<int64_t>> offline_ids_; |
| + std::unique_ptr<std::set<GURL>> urls_; |
| + std::unique_ptr<std::set<std::string>> restricted_to_namespaces_; |
| + std::unique_ptr<std::set<ClientId>> client_ids_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(OfflinePageModelQuery); |
| +}; |
| + |
| +// Used to create an offline page model query. Set policy bits to limit by |
| +// namespaces. If the number of IDs or the number of namespaces are reduced to |
| +// zero, the builder will DCHECK. Call |Build| to generate an actual query. |
| +class OfflinePageModelQueryBuilder { |
| + public: |
| + enum class Requirement { |
| + Unset, |
|
Dmitry Titov
2016/10/21 20:21:09
Enum values should be either CAPITALIZED or kNamed
dewittj
2016/10/24 18:19:33
Done.
|
| + OnlyMatching, |
| + OnlyNotMatching, |
| + }; |
| + |
| + explicit OfflinePageModelQueryBuilder(); |
|
Dmitry Titov
2016/10/21 20:21:09
doesn't have to be explicit
dewittj
2016/10/24 18:19:33
Done.
|
| + ~OfflinePageModelQueryBuilder(); |
| + |
| + // Sets the offline page IDs that are valid for this request. If called |
| + // multiple times, overwrites previous offline page ID restrictions. |
| + OfflinePageModelQueryBuilder& SetOfflinePageIds( |
|
Dmitry Titov
2016/10/21 20:21:09
I wonder if those "Set" methods can also take Requ
dewittj
2016/10/24 18:19:33
Done. What do you think? If you like the new sha
|
| + const std::vector<int64_t>& ids); |
| + |
| + // Sets the client IDs that are valid for this request. If called |
| + // multiple times, overwrites previous client ID restrictions. |
| + OfflinePageModelQueryBuilder& SetClientIds(const std::vector<ClientId>& ids); |
| + |
| + // Sets the URLs that are valid for this request. If called multiple times, |
| + // overwrites previous URL restrictions. |
| + OfflinePageModelQueryBuilder& SetUrls(const std::vector<GURL>& urls); |
| + |
| + // Only include namespaces whose namespaces satisfy |
| + // ClientPolicyController::IsSupportedByDownload(|namespace|) == |
| + // |supported_by_download| |
| + // Multiple calls overwrite previous ones. |
| + OfflinePageModelQueryBuilder& RequireSupportedByDownload( |
| + Requirement supported_by_download); |
| + |
| + // Only include namespaces whose namespaces satisfy |
| + // ClientPolicyController::IsShownAsRecentlyVisitedSite(|namespace|) == |
| + // |recently_visited| |
| + // Multiple calls overwrite previous ones. |
| + OfflinePageModelQueryBuilder& RequireShownAsRecentlyVisitedSite( |
| + Requirement recently_visited); |
| + |
| + // Only include namespaces whose namespaces satisfy |
| + // ClientPolicyController::IsRestrictedToOriginalTab(|namespace|) == |
| + // |original_tab| |
| + // Multiple calls overwrite previous ones. |
| + OfflinePageModelQueryBuilder& RequireRestrictedToOriginalTab( |
| + Requirement original_tab); |
| + |
| + // Resets whether we return expired pages. If called multiple times the bit |
| + // is overwritten and |allow_expired| from the last call is saved. |
| + OfflinePageModelQueryBuilder& AllowExpiredPages(bool allow_expired); |
| + |
| + // Returns the built-up query based on the above APIs. This resets the |
| + // internal state. |
| + std::unique_ptr<OfflinePageModelQuery> Build( |
| + ClientPolicyController* controller); |
| + |
| + private: |
| + // Intersects the allowed namespaces in query_ with |namespaces|. If |
| + // |inverted| is true, intersects the allowed namespaces with all namespaces |
| + // except those provided in |namespaces|. |
| + void IntersectWithNamespaces(ClientPolicyController* controller, |
|
Dmitry Titov
2016/10/21 20:21:09
If I understand correctly, "intersect with inversi
dewittj
2016/10/24 18:19:33
Moved to RemoveNamespaces & IntersectNamespaces.
|
| + const std::vector<std::string>& namespaces, |
| + bool invert); |
| + |
| + // The in-progress query object. |
| + std::unique_ptr<OfflinePageModelQuery> query_; |
| + |
| + Requirement supported_by_download_ = Requirement::Unset; |
| + Requirement shown_as_recently_visited_site_ = Requirement::Unset; |
| + Requirement restricted_to_original_tab_ = Requirement::Unset; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(OfflinePageModelQueryBuilder); |
| +}; |
| + |
| +} // namespace offline_pages |
| + |
| +#endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_QUERY_H_ |