| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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_IMPL_H_ | |
| 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_IMPL_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <set> | |
| 13 #include <string> | |
| 14 #include <utility> | |
| 15 #include <vector> | |
| 16 | |
| 17 #include "base/callback.h" | |
| 18 #include "base/files/file_path.h" | |
| 19 #include "base/gtest_prod_util.h" | |
| 20 #include "base/macros.h" | |
| 21 #include "base/memory/ref_counted.h" | |
| 22 #include "base/memory/scoped_vector.h" | |
| 23 #include "base/memory/weak_ptr.h" | |
| 24 #include "base/observer_list.h" | |
| 25 #include "base/optional.h" | |
| 26 #include "base/strings/string16.h" | |
| 27 #include "base/time/time.h" | |
| 28 #include "components/keyed_service/core/keyed_service.h" | |
| 29 #include "components/offline_pages/offline_page_archiver.h" | |
| 30 #include "components/offline_pages/offline_page_metadata_store.h" | |
| 31 #include "components/offline_pages/offline_page_model.h" | |
| 32 #include "components/offline_pages/offline_page_model_event_logger.h" | |
| 33 #include "components/offline_pages/offline_page_storage_manager.h" | |
| 34 #include "components/offline_pages/offline_page_types.h" | |
| 35 #include "components/offline_pages/offline_store_types.h" | |
| 36 | |
| 37 class GURL; | |
| 38 namespace base { | |
| 39 class Clock; | |
| 40 class SequencedTaskRunner; | |
| 41 class TimeTicks; | |
| 42 } // namespace base | |
| 43 | |
| 44 namespace offline_pages { | |
| 45 | |
| 46 struct ClientId; | |
| 47 struct OfflinePageItem; | |
| 48 | |
| 49 class ArchiveManager; | |
| 50 class ClientPolicyController; | |
| 51 class OfflinePageModelQuery; | |
| 52 class OfflinePageStorageManager; | |
| 53 | |
| 54 // Implementation of service for saving pages offline, storing the offline | |
| 55 // copy and metadata, and retrieving them upon request. | |
| 56 class OfflinePageModelImpl : public OfflinePageModel, public KeyedService { | |
| 57 public: | |
| 58 // All blocking calls/disk access will happen on the provided |task_runner|. | |
| 59 OfflinePageModelImpl( | |
| 60 std::unique_ptr<OfflinePageMetadataStore> store, | |
| 61 const base::FilePath& archives_dir, | |
| 62 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
| 63 ~OfflinePageModelImpl() override; | |
| 64 | |
| 65 // Implemented methods: | |
| 66 void AddObserver(Observer* observer) override; | |
| 67 void RemoveObserver(Observer* observer) override; | |
| 68 void SavePage(const SavePageParams& save_page_params, | |
| 69 std::unique_ptr<OfflinePageArchiver> archiver, | |
| 70 const SavePageCallback& callback) override; | |
| 71 void MarkPageAccessed(int64_t offline_id) override; | |
| 72 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | |
| 73 const DeletePageCallback& callback) override; | |
| 74 void DeletePagesByClientIds(const std::vector<ClientId>& client_ids, | |
| 75 const DeletePageCallback& callback) override; | |
| 76 | |
| 77 void GetPagesMatchingQuery( | |
| 78 std::unique_ptr<OfflinePageModelQuery> query, | |
| 79 const MultipleOfflinePageItemCallback& callback) override; | |
| 80 | |
| 81 void GetPagesByClientIds( | |
| 82 const std::vector<ClientId>& client_ids, | |
| 83 const MultipleOfflinePageItemCallback& callback) override; | |
| 84 | |
| 85 void DeleteCachedPagesByURLPredicate( | |
| 86 const UrlPredicate& predicate, | |
| 87 const DeletePageCallback& callback) override; | |
| 88 void CheckPagesExistOffline( | |
| 89 const std::set<GURL>& urls, | |
| 90 const CheckPagesExistOfflineCallback& callback) override; | |
| 91 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override; | |
| 92 void GetOfflineIdsForClientId( | |
| 93 const ClientId& client_id, | |
| 94 const MultipleOfflineIdCallback& callback) override; | |
| 95 void GetPageByOfflineId( | |
| 96 int64_t offline_id, | |
| 97 const SingleOfflinePageItemCallback& callback) override; | |
| 98 void GetPagesByURL( | |
| 99 const GURL& url, | |
| 100 URLSearchMode url_search_mode, | |
| 101 const MultipleOfflinePageItemCallback& callback) override; | |
| 102 ClientPolicyController* GetPolicyController() override; | |
| 103 | |
| 104 // Methods for testing only: | |
| 105 OfflinePageMetadataStore* GetStoreForTesting(); | |
| 106 void set_testing_clock(base::Clock* clock) { testing_clock_ = clock; } | |
| 107 | |
| 108 OfflinePageStorageManager* GetStorageManager(); | |
| 109 | |
| 110 bool is_loaded() const override; | |
| 111 | |
| 112 OfflineEventLogger* GetLogger() override; | |
| 113 | |
| 114 protected: | |
| 115 // Adding a protected constructor for testing-only purposes in | |
| 116 // offline_page_storage_manager_unittest.cc | |
| 117 OfflinePageModelImpl(); | |
| 118 | |
| 119 private: | |
| 120 FRIEND_TEST_ALL_PREFIXES(OfflinePageModelImplTest, MarkPageForDeletion); | |
| 121 | |
| 122 typedef ScopedVector<OfflinePageArchiver> PendingArchivers; | |
| 123 | |
| 124 // Callback for ensuring archive directory is created. | |
| 125 void OnEnsureArchivesDirCreatedDone(const base::TimeTicks& start_time); | |
| 126 | |
| 127 void GetPagesMatchingQueryWhenLoadDone( | |
| 128 std::unique_ptr<OfflinePageModelQuery> query, | |
| 129 const MultipleOfflinePageItemCallback& callback); | |
| 130 void GetOfflineIdsForClientIdWhenLoadDone( | |
| 131 const ClientId& client_id, | |
| 132 const MultipleOfflineIdCallback& callback) const; | |
| 133 const std::vector<int64_t> MaybeGetOfflineIdsForClientId( | |
| 134 const ClientId& client_id) const; | |
| 135 void GetPagesByURLWhenLoadDone( | |
| 136 const GURL& url, | |
| 137 URLSearchMode url_search_mode, | |
| 138 const MultipleOfflinePageItemCallback& callback) const; | |
| 139 void MarkPageAccessedWhenLoadDone(int64_t offline_id); | |
| 140 | |
| 141 // Check the consistency between metadata store and archives on disk, | |
| 142 // would delete the metadata entries which don't have an associated | |
| 143 // archive and the archives which doesn't have a metadata in the store. | |
| 144 // The expired pages (from previous versions) would also be cleared | |
| 145 // during this process. | |
| 146 void CheckMetadataConsistency(); | |
| 147 | |
| 148 // Callback for loading pages from the offline page metadata store. | |
| 149 void OnStoreInitialized(const base::TimeTicks& start_time, | |
| 150 int reset_attempts_left, | |
| 151 bool success); | |
| 152 void OnStoreResetDone(const base::TimeTicks& start_time, | |
| 153 int reset_attempts_left, | |
| 154 bool success); | |
| 155 void OnInitialGetOfflinePagesDone( | |
| 156 const base::TimeTicks& start_time, | |
| 157 const std::vector<OfflinePageItem>& offline_pages); | |
| 158 void FinalizeModelLoad(); | |
| 159 | |
| 160 // Steps for saving a page offline. | |
| 161 void OnCreateArchiveDone(const SavePageParams& save_page_params, | |
| 162 int64_t offline_id, | |
| 163 const base::Time& start_time, | |
| 164 const SavePageCallback& callback, | |
| 165 OfflinePageArchiver* archiver, | |
| 166 OfflinePageArchiver::ArchiverResult result, | |
| 167 const GURL& url, | |
| 168 const base::FilePath& file_path, | |
| 169 const base::string16& title, | |
| 170 int64_t file_size); | |
| 171 void OnAddOfflinePageDone(OfflinePageArchiver* archiver, | |
| 172 const SavePageCallback& callback, | |
| 173 const OfflinePageItem& offline_page, | |
| 174 ItemActionStatus status); | |
| 175 void InformSavePageDone(const SavePageCallback& callback, | |
| 176 SavePageResult result, | |
| 177 const ClientId& client_id, | |
| 178 int64_t offline_id); | |
| 179 void DeletePendingArchiver(OfflinePageArchiver* archiver); | |
| 180 | |
| 181 // Steps for deleting files and data for an offline page. | |
| 182 void OnDeleteArchiveFilesDone(const std::vector<int64_t>& offline_ids, | |
| 183 const DeletePageCallback& callback, | |
| 184 bool success); | |
| 185 void OnRemoveOfflinePagesDone( | |
| 186 const DeletePageCallback& callback, | |
| 187 std::unique_ptr<OfflinePagesUpdateResult> result); | |
| 188 void InformDeletePageDone(const DeletePageCallback& callback, | |
| 189 DeletePageResult result); | |
| 190 | |
| 191 void OnMarkPageAccesseDone(const OfflinePageItem& offline_page_item, | |
| 192 std::unique_ptr<OfflinePagesUpdateResult> result); | |
| 193 | |
| 194 // Callbacks for checking metadata consistency. | |
| 195 void CheckMetadataConsistencyForArchivePaths( | |
| 196 const std::set<base::FilePath>& archive_paths); | |
| 197 // Callbacks which would be called after orphaned archives are deleted. | |
| 198 // Orphaned archives are the files on disk which are not pointed to by any of | |
| 199 // the page entries in the metadata store. | |
| 200 void DeletePagesMissingArchiveFile( | |
| 201 const std::set<base::FilePath>& archive_paths); | |
| 202 void OnDeletePagesMissingArchiveFileDone( | |
| 203 const std::vector<int64_t>& offline_ids, | |
| 204 DeletePageResult result); | |
| 205 void DeleteOrphanedArchives(const std::set<base::FilePath>& archive_paths); | |
| 206 void OnDeleteOrphanedArchivesDone(const std::vector<base::FilePath>& archives, | |
| 207 bool success); | |
| 208 | |
| 209 // Callbacks for deleting pages with same URL when saving pages. | |
| 210 void DeleteExistingPagesWithSameURL(const OfflinePageItem& offline_page); | |
| 211 void OnPagesFoundWithSameURL(const OfflinePageItem& offline_page, | |
| 212 size_t pages_allowed, | |
| 213 const MultipleOfflinePageItemResult& items); | |
| 214 void OnDeleteOldPagesWithSameURL(DeletePageResult result); | |
| 215 | |
| 216 void CacheLoadedData(const std::vector<OfflinePageItem>& offline_pages); | |
| 217 | |
| 218 // Actually does the work of deleting, requires the model is loaded. | |
| 219 void DoDeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | |
| 220 const DeletePageCallback& callback); | |
| 221 | |
| 222 // Actually does the work of deleting, requires the model is loaded. | |
| 223 void DeletePages(const DeletePageCallback& callback, | |
| 224 const MultipleOfflinePageItemResult& items); | |
| 225 | |
| 226 void DoGetPagesByClientIds(const std::vector<ClientId>& client_ids, | |
| 227 const MultipleOfflinePageItemCallback& callback); | |
| 228 | |
| 229 // Similar to DoDeletePagesByOfflineId, does actual work of deleting, and | |
| 230 // requires that the model is loaded. | |
| 231 void DoDeleteCachedPagesByURLPredicate(const UrlPredicate& predicate, | |
| 232 const DeletePageCallback& callback); | |
| 233 | |
| 234 // Clears expired pages if there are any or we're running out of storage. | |
| 235 void ClearStorageIfNeeded( | |
| 236 const OfflinePageStorageManager::ClearStorageCallback& callback); | |
| 237 | |
| 238 // Callback completing storage clearing. | |
| 239 void OnStorageCleared(size_t cleared_page_count, | |
| 240 OfflinePageStorageManager::ClearStorageResult result); | |
| 241 | |
| 242 // Post task to clear storage. | |
| 243 void PostClearStorageIfNeededTask(bool delayed); | |
| 244 | |
| 245 // Check if |offline_page| should be removed on cache reset by user. | |
| 246 bool IsRemovedOnCacheReset(const OfflinePageItem& offline_page) const; | |
| 247 | |
| 248 void RunWhenLoaded(const base::Closure& job); | |
| 249 | |
| 250 base::Time GetCurrentTime() const; | |
| 251 | |
| 252 // Persistent store for offline page metadata. | |
| 253 std::unique_ptr<OfflinePageMetadataStore> store_; | |
| 254 | |
| 255 // Location where all of the archive files will be stored. | |
| 256 base::FilePath archives_dir_; | |
| 257 | |
| 258 // The observers. | |
| 259 base::ObserverList<Observer> observers_; | |
| 260 | |
| 261 bool is_loaded_; | |
| 262 | |
| 263 // In memory copy of the offline page metadata, keyed by offline IDs. | |
| 264 std::map<int64_t, OfflinePageItem> offline_pages_; | |
| 265 | |
| 266 // Pending archivers owned by this model. | |
| 267 PendingArchivers pending_archivers_; | |
| 268 | |
| 269 // Delayed tasks that should be invoked after the loading is done. | |
| 270 std::vector<base::Closure> delayed_tasks_; | |
| 271 | |
| 272 // Controller of the client policies. | |
| 273 std::unique_ptr<ClientPolicyController> policy_controller_; | |
| 274 | |
| 275 // Manager for the storage consumed by archives and responsible for | |
| 276 // automatic page clearing. | |
| 277 std::unique_ptr<OfflinePageStorageManager> storage_manager_; | |
| 278 | |
| 279 // Manager for the offline archive files and directory. | |
| 280 std::unique_ptr<ArchiveManager> archive_manager_; | |
| 281 | |
| 282 // Logger to facilitate recording of events. | |
| 283 OfflinePageModelEventLogger offline_event_logger_; | |
| 284 | |
| 285 // Clock for getting time in testing code. The setter is responsible to reset | |
| 286 // it once it is not longer needed. | |
| 287 base::Clock* testing_clock_; | |
| 288 | |
| 289 base::WeakPtrFactory<OfflinePageModelImpl> weak_ptr_factory_; | |
| 290 | |
| 291 DISALLOW_COPY_AND_ASSIGN(OfflinePageModelImpl); | |
| 292 }; | |
| 293 | |
| 294 } // namespace offline_pages | |
| 295 | |
| 296 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_IMPL_H_ | |
| OLD | NEW |