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