| 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 GetAllPagesWithExpired( | |
| 93 const MultipleOfflinePageItemCallback& callback) override; | |
| 94 void GetOfflineIdsForClientId( | |
| 95 const ClientId& client_id, | |
| 96 const MultipleOfflineIdCallback& callback) override; | |
| 97 void GetPageByOfflineId( | |
| 98 int64_t offline_id, | |
| 99 const SingleOfflinePageItemCallback& callback) override; | |
| 100 void GetPagesByURL( | |
| 101 const GURL& url, | |
| 102 URLSearchMode url_search_mode, | |
| 103 const MultipleOfflinePageItemCallback& callback) override; | |
| 104 void ExpirePages(const std::vector<int64_t>& offline_ids, | |
| 105 const base::Time& expiration_time, | |
| 106 const base::Callback<void(bool)>& callback) override; | |
| 107 ClientPolicyController* GetPolicyController() override; | |
| 108 | |
| 109 // Methods for testing only: | |
| 110 OfflinePageMetadataStore* GetStoreForTesting(); | |
| 111 void set_testing_clock(base::Clock* clock) { testing_clock_ = clock; } | |
| 112 | |
| 113 OfflinePageStorageManager* GetStorageManager(); | |
| 114 | |
| 115 bool is_loaded() const override; | |
| 116 | |
| 117 OfflineEventLogger* GetLogger() override; | |
| 118 | |
| 119 protected: | |
| 120 // Adding a protected constructor for testing-only purposes in | |
| 121 // offline_page_storage_manager_unittest.cc | |
| 122 OfflinePageModelImpl(); | |
| 123 | |
| 124 private: | |
| 125 FRIEND_TEST_ALL_PREFIXES(OfflinePageModelImplTest, MarkPageForDeletion); | |
| 126 | |
| 127 typedef ScopedVector<OfflinePageArchiver> PendingArchivers; | |
| 128 | |
| 129 // Callback for ensuring archive directory is created. | |
| 130 void OnEnsureArchivesDirCreatedDone(const base::TimeTicks& start_time); | |
| 131 | |
| 132 void CheckPagesExistOfflineAfterLoadDone( | |
| 133 const std::set<GURL>& urls, | |
| 134 const CheckPagesExistOfflineCallback& callback); | |
| 135 void GetOfflineIdsForClientIdWhenLoadDone( | |
| 136 const ClientId& client_id, | |
| 137 const MultipleOfflineIdCallback& callback) const; | |
| 138 void GetPageByOfflineIdWhenLoadDone( | |
| 139 int64_t offline_id, | |
| 140 const SingleOfflinePageItemCallback& callback) const; | |
| 141 const std::vector<int64_t> MaybeGetOfflineIdsForClientId( | |
| 142 const ClientId& client_id) const; | |
| 143 void GetPagesByURLWhenLoadDone( | |
| 144 const GURL& url, | |
| 145 URLSearchMode url_search_mode, | |
| 146 const MultipleOfflinePageItemCallback& callback) const; | |
| 147 void MarkPageAccessedWhenLoadDone(int64_t offline_id); | |
| 148 | |
| 149 void CheckMetadataConsistency(); | |
| 150 | |
| 151 // Callback for loading pages from the offline page metadata store. | |
| 152 void OnStoreInitialized(const base::TimeTicks& start_time, | |
| 153 int reset_attempts_left, | |
| 154 bool success); | |
| 155 void OnStoreResetDone(const base::TimeTicks& start_time, | |
| 156 int reset_attempts_left, | |
| 157 bool success); | |
| 158 void OnInitialGetOfflinePagesDone( | |
| 159 const base::TimeTicks& start_time, | |
| 160 const std::vector<OfflinePageItem>& offline_pages); | |
| 161 void FinalizeModelLoad(); | |
| 162 | |
| 163 // Steps for saving a page offline. | |
| 164 void OnCreateArchiveDone(const SavePageParams& save_page_params, | |
| 165 int64_t offline_id, | |
| 166 const base::Time& start_time, | |
| 167 const SavePageCallback& callback, | |
| 168 OfflinePageArchiver* archiver, | |
| 169 OfflinePageArchiver::ArchiverResult result, | |
| 170 const GURL& url, | |
| 171 const base::FilePath& file_path, | |
| 172 const base::string16& title, | |
| 173 int64_t file_size); | |
| 174 void OnAddOfflinePageDone(OfflinePageArchiver* archiver, | |
| 175 const SavePageCallback& callback, | |
| 176 const OfflinePageItem& offline_page, | |
| 177 ItemActionStatus status); | |
| 178 void InformSavePageDone(const SavePageCallback& callback, | |
| 179 SavePageResult result, | |
| 180 const ClientId& client_id, | |
| 181 int64_t offline_id); | |
| 182 void DeletePendingArchiver(OfflinePageArchiver* archiver); | |
| 183 | |
| 184 // Steps for deleting files and data for an offline page. | |
| 185 void OnDeleteArchiveFilesDone(const std::vector<int64_t>& offline_ids, | |
| 186 const DeletePageCallback& callback, | |
| 187 bool success); | |
| 188 void OnRemoveOfflinePagesDone( | |
| 189 const DeletePageCallback& callback, | |
| 190 std::unique_ptr<OfflinePagesUpdateResult> result); | |
| 191 void InformDeletePageDone(const DeletePageCallback& callback, | |
| 192 DeletePageResult result); | |
| 193 | |
| 194 void OnMarkPageAccesseDone(const OfflinePageItem& offline_page_item, | |
| 195 std::unique_ptr<OfflinePagesUpdateResult> result); | |
| 196 | |
| 197 // Callbacks for checking metadata consistency. | |
| 198 void CheckMetadataConsistencyForArchivePaths( | |
| 199 const std::set<base::FilePath>& archive_paths); | |
| 200 // Callback called after headless archives deleted. Orphaned archives are | |
| 201 // archives files on disk which are not pointed to by any of the page items | |
| 202 // in metadata store. | |
| 203 void ExpirePagesMissingArchiveFile( | |
| 204 const std::set<base::FilePath>& archive_paths); | |
| 205 void OnExpirePagesMissingArchiveFileDone( | |
| 206 const std::vector<int64_t>& offline_ids, | |
| 207 bool success); | |
| 208 void DeleteOrphanedArchives(const std::set<base::FilePath>& archive_paths); | |
| 209 void OnDeleteOrphanedArchivesDone(const std::vector<base::FilePath>& archives, | |
| 210 bool success); | |
| 211 | |
| 212 // Callbacks for deleting pages with same URL when saving pages. | |
| 213 void DeleteExistingPagesWithSameURL(const OfflinePageItem& offline_page); | |
| 214 void OnPagesFoundWithSameURL(const OfflinePageItem& offline_page, | |
| 215 size_t pages_allowed, | |
| 216 const MultipleOfflinePageItemResult& items); | |
| 217 void OnDeleteOldPagesWithSameURL(DeletePageResult result); | |
| 218 | |
| 219 void CacheLoadedData(const std::vector<OfflinePageItem>& offline_pages); | |
| 220 | |
| 221 // Actually does the work of deleting, requires the model is loaded. | |
| 222 void DoDeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | |
| 223 const DeletePageCallback& callback); | |
| 224 | |
| 225 // Actually does the work of deleting, requires the model is loaded. | |
| 226 void DeletePages(const DeletePageCallback& callback, | |
| 227 const MultipleOfflinePageItemResult& items); | |
| 228 | |
| 229 void DoGetPagesByClientIds(const std::vector<ClientId>& client_ids, | |
| 230 const MultipleOfflinePageItemCallback& callback); | |
| 231 | |
| 232 // Similar to DoDeletePagesByOfflineId, does actual work of deleting, and | |
| 233 // requires that the model is loaded. | |
| 234 void DoDeleteCachedPagesByURLPredicate(const UrlPredicate& predicate, | |
| 235 const DeletePageCallback& callback); | |
| 236 | |
| 237 // Callback completing page expiration. | |
| 238 void OnExpirePageDone(const base::Time& expiration_time, | |
| 239 std::unique_ptr<OfflinePagesUpdateResult> result); | |
| 240 | |
| 241 // Clears expired pages if there are any. | |
| 242 void ClearStorageIfNeeded( | |
| 243 const OfflinePageStorageManager::ClearStorageCallback& callback); | |
| 244 | |
| 245 // Callback completing storage clearing. | |
| 246 void OnStorageCleared(size_t expired_page_count, | |
| 247 OfflinePageStorageManager::ClearStorageResult result); | |
| 248 | |
| 249 // Post task to clear storage. | |
| 250 void PostClearStorageIfNeededTask(bool delayed); | |
| 251 | |
| 252 // Check if |offline_page| should be removed on cache reset by user. | |
| 253 bool IsRemovedOnCacheReset(const OfflinePageItem& offline_page) const; | |
| 254 | |
| 255 void RunWhenLoaded(const base::Closure& job); | |
| 256 | |
| 257 base::Time GetCurrentTime() const; | |
| 258 | |
| 259 // Persistent store for offline page metadata. | |
| 260 std::unique_ptr<OfflinePageMetadataStore> store_; | |
| 261 | |
| 262 // Location where all of the archive files will be stored. | |
| 263 base::FilePath archives_dir_; | |
| 264 | |
| 265 // The observers. | |
| 266 base::ObserverList<Observer> observers_; | |
| 267 | |
| 268 bool is_loaded_; | |
| 269 | |
| 270 // In memory copy of the offline page metadata, keyed by offline IDs. | |
| 271 std::map<int64_t, OfflinePageItem> offline_pages_; | |
| 272 | |
| 273 // Pending archivers owned by this model. | |
| 274 PendingArchivers pending_archivers_; | |
| 275 | |
| 276 // Delayed tasks that should be invoked after the loading is done. | |
| 277 std::vector<base::Closure> delayed_tasks_; | |
| 278 | |
| 279 // Controller of the client policies. | |
| 280 std::unique_ptr<ClientPolicyController> policy_controller_; | |
| 281 | |
| 282 // Manager for the storage consumed by archives and responsible for | |
| 283 // automatic page clearing. | |
| 284 std::unique_ptr<OfflinePageStorageManager> storage_manager_; | |
| 285 | |
| 286 // Manager for the offline archive files and directory. | |
| 287 std::unique_ptr<ArchiveManager> archive_manager_; | |
| 288 | |
| 289 // Logger to facilitate recording of events. | |
| 290 OfflinePageModelEventLogger offline_event_logger_; | |
| 291 | |
| 292 // Clock for getting time in testing code. The setter is responsible to reset | |
| 293 // it once it is not longer needed. | |
| 294 base::Clock* testing_clock_; | |
| 295 | |
| 296 base::WeakPtrFactory<OfflinePageModelImpl> weak_ptr_factory_; | |
| 297 | |
| 298 DISALLOW_COPY_AND_ASSIGN(OfflinePageModelImpl); | |
| 299 }; | |
| 300 | |
| 301 } // namespace offline_pages | |
| 302 | |
| 303 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_IMPL_H_ | |
| OLD | NEW |