| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ | |
| 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <set> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/supports_user_data.h" | |
| 16 #include "components/offline_pages/offline_event_logger.h" | |
| 17 #include "components/offline_pages/offline_page_archiver.h" | |
| 18 #include "components/offline_pages/offline_page_model_query.h" | |
| 19 #include "components/offline_pages/offline_page_storage_manager.h" | |
| 20 #include "components/offline_pages/offline_page_types.h" | |
| 21 | |
| 22 class GURL; | |
| 23 namespace base { | |
| 24 class Time; | |
| 25 } // namespace base | |
| 26 | |
| 27 namespace offline_pages { | |
| 28 | |
| 29 struct ClientId; | |
| 30 | |
| 31 // Service for saving pages offline, storing the offline copy and metadata, and | |
| 32 // retrieving them upon request. | |
| 33 // | |
| 34 // Example usage: | |
| 35 // class ArchiverImpl : public OfflinePageArchiver { | |
| 36 // // This is a class that knows how to create archiver | |
| 37 // void CreateArchiver(...) override; | |
| 38 // ... | |
| 39 // } | |
| 40 // | |
| 41 // // In code using the OfflinePagesModel to save a page: | |
| 42 // std::unique_ptr<ArchiverImpl> archiver(new ArchiverImpl()); | |
| 43 // // Callback is of type SavePageCallback. | |
| 44 // model->SavePage(url, std::move(archiver), callback); | |
| 45 // | |
| 46 // TODO(fgorski): Things to describe: | |
| 47 // * how to cancel requests and what to expect | |
| 48 class OfflinePageModel : public base::SupportsUserData { | |
| 49 public: | |
| 50 // Controls how to search on differnt URLs for pages. | |
| 51 enum class URLSearchMode { | |
| 52 // Match against the last committed URL only. | |
| 53 SEARCH_BY_FINAL_URL_ONLY, | |
| 54 // Match against all stored URLs, including the last committed URL and | |
| 55 // the original request URL. | |
| 56 SEARCH_BY_ALL_URLS | |
| 57 }; | |
| 58 | |
| 59 // Describes the parameters to control how to save a page. | |
| 60 struct SavePageParams { | |
| 61 SavePageParams(); | |
| 62 SavePageParams(const SavePageParams& other); | |
| 63 | |
| 64 // The last committed URL of the page to save. | |
| 65 GURL url; | |
| 66 | |
| 67 // The identification used by the client. | |
| 68 ClientId client_id; | |
| 69 | |
| 70 // Used for the offline_id for the saved file if non-zero. If it is | |
| 71 // kInvalidOfflineId, a new, random ID will be generated. | |
| 72 int64_t proposed_offline_id; | |
| 73 | |
| 74 // The original URL of the page to save. Empty if no redirect occurs. | |
| 75 GURL original_url; | |
| 76 }; | |
| 77 | |
| 78 // Observer of the OfflinePageModel. | |
| 79 class Observer { | |
| 80 public: | |
| 81 // Invoked when the model has finished loading. | |
| 82 virtual void OfflinePageModelLoaded(OfflinePageModel* model) = 0; | |
| 83 | |
| 84 // Invoked when the model is being updated, due to adding, removing or | |
| 85 // updating an offline page. | |
| 86 virtual void OfflinePageModelChanged(OfflinePageModel* model) = 0; | |
| 87 | |
| 88 // Invoked when an offline copy related to |offline_id| was deleted. | |
| 89 virtual void OfflinePageDeleted(int64_t offline_id, | |
| 90 const ClientId& client_id) = 0; | |
| 91 | |
| 92 protected: | |
| 93 virtual ~Observer() = default; | |
| 94 }; | |
| 95 | |
| 96 using CheckPagesExistOfflineResult = | |
| 97 offline_pages::CheckPagesExistOfflineResult; | |
| 98 using MultipleOfflinePageItemResult = | |
| 99 offline_pages::MultipleOfflinePageItemResult; | |
| 100 using DeletePageResult = offline_pages::DeletePageResult; | |
| 101 using SavePageResult = offline_pages::SavePageResult; | |
| 102 | |
| 103 // Returns true if saving an offline page may be attempted for |url|. | |
| 104 static bool CanSaveURL(const GURL& url); | |
| 105 | |
| 106 OfflinePageModel(); | |
| 107 ~OfflinePageModel() override; | |
| 108 | |
| 109 virtual void AddObserver(Observer* observer) = 0; | |
| 110 virtual void RemoveObserver(Observer* observer) = 0; | |
| 111 | |
| 112 static const int64_t kInvalidOfflineId = 0; | |
| 113 | |
| 114 // Attempts to save a page offline per |save_page_params|. Requires that the | |
| 115 // model is loaded. Generates a new offline id or uses the proposed offline | |
| 116 // id in |save_page_params| and returns it. | |
| 117 virtual void SavePage(const SavePageParams& save_page_params, | |
| 118 std::unique_ptr<OfflinePageArchiver> archiver, | |
| 119 const SavePageCallback& callback) = 0; | |
| 120 | |
| 121 // Marks that the offline page related to the passed |offline_id| has been | |
| 122 // accessed. Its access info, including last access time and access count, | |
| 123 // will be updated. Requires that the model is loaded. | |
| 124 virtual void MarkPageAccessed(int64_t offline_id) = 0; | |
| 125 | |
| 126 // Deletes pages based on |offline_ids|. | |
| 127 virtual void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | |
| 128 const DeletePageCallback& callback) = 0; | |
| 129 | |
| 130 // Deletes all pages associated with any of |client_ids|. | |
| 131 virtual void DeletePagesByClientIds(const std::vector<ClientId>& client_ids, | |
| 132 const DeletePageCallback& callback) = 0; | |
| 133 | |
| 134 virtual void GetPagesMatchingQuery( | |
| 135 std::unique_ptr<OfflinePageModelQuery> query, | |
| 136 const MultipleOfflinePageItemCallback& callback) = 0; | |
| 137 | |
| 138 // Retrieves all pages associated with any of |client_ids|. | |
| 139 virtual void GetPagesByClientIds( | |
| 140 const std::vector<ClientId>& client_ids, | |
| 141 const MultipleOfflinePageItemCallback& callback) = 0; | |
| 142 | |
| 143 // Deletes cached offline pages matching the URL predicate. | |
| 144 virtual void DeleteCachedPagesByURLPredicate( | |
| 145 const UrlPredicate& predicate, | |
| 146 const DeletePageCallback& callback) = 0; | |
| 147 | |
| 148 // Returns via callback all GURLs in |urls| that are equal to the online URL | |
| 149 // of any offline page. | |
| 150 virtual void CheckPagesExistOffline( | |
| 151 const std::set<GURL>& urls, | |
| 152 const CheckPagesExistOfflineCallback& callback) = 0; | |
| 153 | |
| 154 // Gets all offline pages. | |
| 155 virtual void GetAllPages(const MultipleOfflinePageItemCallback& callback) = 0; | |
| 156 | |
| 157 // Gets all offline pages including expired ones. | |
| 158 virtual void GetAllPagesWithExpired( | |
| 159 const MultipleOfflinePageItemCallback& callback) = 0; | |
| 160 | |
| 161 // Gets all offline ids where the offline page has the matching client id. | |
| 162 virtual void GetOfflineIdsForClientId( | |
| 163 const ClientId& client_id, | |
| 164 const MultipleOfflineIdCallback& callback) = 0; | |
| 165 | |
| 166 // Returns zero or one offline pages associated with a specified |offline_id|. | |
| 167 virtual void GetPageByOfflineId( | |
| 168 int64_t offline_id, | |
| 169 const SingleOfflinePageItemCallback& callback) = 0; | |
| 170 | |
| 171 // Returns the offline pages that are related to |url|. |url_search_mode| | |
| 172 // controls how the url match is done. See URLSearchMode for more details. | |
| 173 virtual void GetPagesByURL( | |
| 174 const GURL& url, | |
| 175 URLSearchMode url_search_mode, | |
| 176 const MultipleOfflinePageItemCallback& callback) = 0; | |
| 177 | |
| 178 // Marks pages with |offline_ids| as expired and deletes the associated | |
| 179 // archive files. | |
| 180 virtual void ExpirePages(const std::vector<int64_t>& offline_ids, | |
| 181 const base::Time& expiration_time, | |
| 182 const base::Callback<void(bool)>& callback) = 0; | |
| 183 | |
| 184 // Returns the policy controller. | |
| 185 virtual ClientPolicyController* GetPolicyController() = 0; | |
| 186 | |
| 187 // TODO(dougarnett): Remove this and its uses. | |
| 188 virtual bool is_loaded() const = 0; | |
| 189 | |
| 190 // Returns the logger. Ownership is retained by the model. | |
| 191 virtual OfflineEventLogger* GetLogger() = 0; | |
| 192 }; | |
| 193 | |
| 194 } // namespace offline_pages | |
| 195 | |
| 196 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ | |
| OLD | NEW |