| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_IMPL_H_ |
| 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_IMPL_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <set> | 12 #include <set> |
| 13 #include <string> |
| 14 #include <utility> |
| 13 #include <vector> | 15 #include <vector> |
| 14 | 16 |
| 15 #include "base/callback.h" | 17 #include "base/callback.h" |
| 16 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
| 17 #include "base/gtest_prod_util.h" | 19 #include "base/gtest_prod_util.h" |
| 18 #include "base/macros.h" | 20 #include "base/macros.h" |
| 19 #include "base/memory/ref_counted.h" | 21 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_vector.h" | 22 #include "base/memory/scoped_vector.h" |
| 21 #include "base/memory/weak_ptr.h" | 23 #include "base/memory/weak_ptr.h" |
| 22 #include "base/observer_list.h" | 24 #include "base/observer_list.h" |
| 23 #include "base/optional.h" | 25 #include "base/optional.h" |
| 24 #include "base/supports_user_data.h" | |
| 25 #include "components/keyed_service/core/keyed_service.h" | 26 #include "components/keyed_service/core/keyed_service.h" |
| 26 #include "components/offline_pages/offline_page_archiver.h" | 27 #include "components/offline_pages/offline_page_archiver.h" |
| 27 #include "components/offline_pages/offline_page_metadata_store.h" | 28 #include "components/offline_pages/offline_page_metadata_store.h" |
| 29 #include "components/offline_pages/offline_page_model.h" |
| 28 #include "components/offline_pages/offline_page_storage_manager.h" | 30 #include "components/offline_pages/offline_page_storage_manager.h" |
| 29 #include "components/offline_pages/offline_page_types.h" | 31 #include "components/offline_pages/offline_page_types.h" |
| 30 | 32 |
| 31 class GURL; | 33 class GURL; |
| 32 namespace base { | 34 namespace base { |
| 33 class SequencedTaskRunner; | 35 class SequencedTaskRunner; |
| 34 class Time; | 36 class Time; |
| 35 class TimeDelta; | 37 class TimeDelta; |
| 36 class TimeTicks; | 38 class TimeTicks; |
| 37 } // namespace base | 39 } // namespace base |
| 38 | 40 |
| 39 namespace offline_pages { | 41 namespace offline_pages { |
| 40 | 42 |
| 41 static const char* const kBookmarkNamespace = "bookmark"; | |
| 42 static const int64_t kInvalidOfflineId = 0; | |
| 43 | |
| 44 struct ClientId; | 43 struct ClientId; |
| 45 struct OfflinePageItem; | 44 struct OfflinePageItem; |
| 46 | 45 |
| 47 class ArchiveManager; | 46 class ArchiveManager; |
| 48 class ClientPolicyController; | 47 class ClientPolicyController; |
| 49 class OfflinePageMetadataStore; | 48 class OfflinePageMetadataStore; |
| 50 class OfflinePageStorageManager; | 49 class OfflinePageStorageManager; |
| 51 | 50 |
| 52 // Service for saving pages offline, storing the offline copy and metadata, and | 51 // Implementation of service for saving pages offline, storing the offline |
| 53 // retrieving them upon request. | 52 // copy and metadata, and retrieving them upon request. |
| 54 // | 53 class OfflinePageModelImpl : public OfflinePageModel, public KeyedService { |
| 55 // Example usage: | |
| 56 // class ArchiverImpl : public OfflinePageArchiver { | |
| 57 // // This is a class that knows how to create archiver | |
| 58 // void CreateArchiver(...) override; | |
| 59 // ... | |
| 60 // } | |
| 61 // | |
| 62 // // In code using the OfflinePagesModel to save a page: | |
| 63 // std::unique_ptr<ArchiverImpl> archiver(new ArchiverImpl()); | |
| 64 // // Callback is of type SavePageCallback. | |
| 65 // model->SavePage(url, std::move(archiver), callback); | |
| 66 // | |
| 67 // TODO(fgorski): Things to describe: | |
| 68 // * how to cancel requests and what to expect | |
| 69 class OfflinePageModel : public KeyedService, | |
| 70 public base::SupportsUserData, | |
| 71 public OfflinePageStorageManager::Client { | |
| 72 public: | 54 public: |
| 73 // Observer of the OfflinePageModel. | 55 // All blocking calls/disk access will happen on the provided |task_runner|. |
| 74 class Observer { | 56 OfflinePageModelImpl( |
| 75 public: | 57 std::unique_ptr<OfflinePageMetadataStore> store, |
| 76 // Invoked when the model has finished loading. | 58 const base::FilePath& archives_dir, |
| 77 virtual void OfflinePageModelLoaded(OfflinePageModel* model) = 0; | 59 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 60 ~OfflinePageModelImpl() override; |
| 78 | 61 |
| 79 // Invoked when the model is being updated, due to adding, removing or | 62 // Implemented methods: |
| 80 // updating an offline page. | 63 void AddObserver(Observer* observer) override; |
| 81 virtual void OfflinePageModelChanged(OfflinePageModel* model) = 0; | 64 void RemoveObserver(Observer* observer) override; |
| 82 | |
| 83 // Invoked when an offline copy related to |offline_id| was deleted. | |
| 84 // In can be invoked as a result of |CheckForExternalFileDeletion|, if a | |
| 85 // deleted page is detected. | |
| 86 virtual void OfflinePageDeleted(int64_t offline_id, | |
| 87 const ClientId& client_id) = 0; | |
| 88 | |
| 89 protected: | |
| 90 virtual ~Observer() {} | |
| 91 }; | |
| 92 | |
| 93 using CheckPagesExistOfflineResult = | |
| 94 offline_pages::CheckPagesExistOfflineResult; | |
| 95 using MultipleOfflinePageItemResult = | |
| 96 offline_pages::MultipleOfflinePageItemResult; | |
| 97 using SingleOfflinePageItemResult = | |
| 98 offline_pages::SingleOfflinePageItemResult; | |
| 99 | |
| 100 //using DeletePageCallback = offline_pages::DeletePageCallback; | |
| 101 using DeletePageResult = offline_pages::DeletePageResult; | |
| 102 using SavePageResult = offline_pages::SavePageResult; | |
| 103 | |
| 104 // Generates a new offline id | |
| 105 static int64_t GenerateOfflineId(); | |
| 106 | |
| 107 // Returns true if an offline copy can be saved for the given URL. | |
| 108 static bool CanSavePage(const GURL& url); | |
| 109 | |
| 110 static base::TimeDelta GetFinalDeletionDelayForTesting(); | |
| 111 | |
| 112 // All blocking calls/disk access will happen on the provided |task_runner|. | |
| 113 OfflinePageModel(std::unique_ptr<OfflinePageMetadataStore> store, | |
| 114 const base::FilePath& archives_dir, | |
| 115 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
| 116 ~OfflinePageModel() override; | |
| 117 | |
| 118 void AddObserver(Observer* observer); | |
| 119 void RemoveObserver(Observer* observer); | |
| 120 | |
| 121 // Attempts to save a page addressed by |url| offline. Requires that the model | |
| 122 // is loaded. Generates a new offline id and returns it. | |
| 123 void SavePage(const GURL& url, | 65 void SavePage(const GURL& url, |
| 124 const ClientId& client_id, | 66 const ClientId& client_id, |
| 125 std::unique_ptr<OfflinePageArchiver> archiver, | 67 std::unique_ptr<OfflinePageArchiver> archiver, |
| 126 const SavePageCallback& callback); | 68 const SavePageCallback& callback) override; |
| 127 | 69 void MarkPageAccessed(int64_t offline_id) override; |
| 128 // Marks that the offline page related to the passed |offline_id| has been | |
| 129 // accessed. Its access info, including last access time and access count, | |
| 130 // will be updated. Requires that the model is loaded. | |
| 131 void MarkPageAccessed(int64_t offline_id); | |
| 132 | |
| 133 // Deletes an offline page related to the passed |offline_id|. | |
| 134 void DeletePageByOfflineId(int64_t offline_id, | |
| 135 const DeletePageCallback& callback); | |
| 136 | |
| 137 // Deletes offline pages related to the passed |offline_ids|. | |
| 138 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, | 70 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, |
| 139 const DeletePageCallback& callback) override; | 71 const DeletePageCallback& callback) override; |
| 140 | 72 void ClearAll(const base::Closure& callback) override; |
| 141 // Wipes out all the data by deleting all saved files and clearing the store. | |
| 142 void ClearAll(const base::Closure& callback); | |
| 143 | |
| 144 // Deletes offline pages matching the URL predicate. | |
| 145 void DeletePagesByURLPredicate(const UrlPredicate& predicate, | 73 void DeletePagesByURLPredicate(const UrlPredicate& predicate, |
| 146 const DeletePageCallback& callback); | 74 const DeletePageCallback& callback) override; |
| 147 | |
| 148 // Returns true via callback if there are offline pages in the given | |
| 149 // |name_space|. | |
| 150 void HasPages(const std::string& name_space, | 75 void HasPages(const std::string& name_space, |
| 151 const HasPagesCallback& callback); | 76 const HasPagesCallback& callback) override; |
| 152 | 77 void CheckPagesExistOffline( |
| 153 // Returns via callback all GURLs in |urls| that are equal to the online URL | 78 const std::set<GURL>& urls, |
| 154 // of any offline page. | 79 const CheckPagesExistOfflineCallback& callback) override; |
| 155 void CheckPagesExistOffline(const std::set<GURL>& urls, | |
| 156 const CheckPagesExistOfflineCallback& callback); | |
| 157 | |
| 158 // Gets all available offline pages. | |
| 159 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override; | 80 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override; |
| 160 | 81 void GetOfflineIdsForClientId( |
| 161 // Gets all offline ids where the offline page has the matching client id. | 82 const ClientId& client_id, |
| 162 void GetOfflineIdsForClientId(const ClientId& client_id, | 83 const MultipleOfflineIdCallback& callback) override; |
| 163 const MultipleOfflineIdCallback& callback); | |
| 164 | |
| 165 // Gets all offline ids where the offline page has the matching client id. | |
| 166 // Requires that the model is loaded. May not return matching IDs depending | |
| 167 // on the internal state of the model. | |
| 168 // | |
| 169 // This function is deprecated. Use |GetOfflineIdsForClientId| instead. | |
| 170 const std::vector<int64_t> MaybeGetOfflineIdsForClientId( | 84 const std::vector<int64_t> MaybeGetOfflineIdsForClientId( |
| 171 const ClientId& client_id) const; | 85 const ClientId& client_id) const override; |
| 172 | 86 void GetPageByOfflineId( |
| 173 // Returns zero or one offline pages associated with a specified |offline_id|. | 87 int64_t offline_id, |
| 174 void GetPageByOfflineId(int64_t offline_id, | 88 const SingleOfflinePageItemCallback& callback) override; |
| 175 const SingleOfflinePageItemCallback& callback); | 89 const OfflinePageItem* MaybeGetPageByOfflineId( |
| 176 | 90 int64_t offline_id) const override; |
| 177 // Returns an offline page associated with a specified |offline_id|. nullptr | 91 void GetPageByOfflineURL( |
| 178 // is returned if not found. | 92 const GURL& offline_url, |
| 179 const OfflinePageItem* MaybeGetPageByOfflineId(int64_t offline_id) const; | 93 const SingleOfflinePageItemCallback& callback) override; |
| 180 | |
| 181 // Returns the offline page that is stored under |offline_url|, if any. | |
| 182 void GetPageByOfflineURL(const GURL& offline_url, | |
| 183 const SingleOfflinePageItemCallback& callback); | |
| 184 | |
| 185 // Returns an offline page that is stored as |offline_url|. A nullptr is | |
| 186 // returned if not found. | |
| 187 // | |
| 188 // This function is deprecated, and may return |nullptr| even if a page | |
| 189 // exists, depending on the implementation details of OfflinePageModel. | |
| 190 // Use |GetPageByOfflineURL| instead. | |
| 191 const OfflinePageItem* MaybeGetPageByOfflineURL( | 94 const OfflinePageItem* MaybeGetPageByOfflineURL( |
| 192 const GURL& offline_url) const; | 95 const GURL& offline_url) const override; |
| 193 | 96 void GetPagesByOnlineURL( |
| 194 // Returns the offline pages that are stored under |online_url|. | 97 const GURL& online_url, |
| 195 void GetPagesByOnlineURL(const GURL& online_url, | 98 const MultipleOfflinePageItemCallback& callback) override; |
| 196 const MultipleOfflinePageItemCallback& callback); | 99 void GetBestPageForOnlineURL( |
| 197 | 100 const GURL& online_url, |
| 198 // Returns via callback an offline page saved for |online_url|, if any. The | 101 const SingleOfflinePageItemCallback callback) override; |
| 199 // best page is chosen based on creation date; a more recently created offline | |
| 200 // page will be preferred over an older one. This API function does not | |
| 201 // respect namespaces, as it is used to choose which page is rendered in a | |
| 202 // tab. Today all namespaces are treated equally for the purposes of this | |
| 203 // selection. | |
| 204 void GetBestPageForOnlineURL(const GURL& online_url, | |
| 205 const SingleOfflinePageItemCallback callback); | |
| 206 | |
| 207 // Returns an offline page saved for |online_url|. A nullptr is returned if | |
| 208 // not found. See |GetBestPageForOnlineURL| for selection criteria. | |
| 209 const OfflinePageItem* MaybeGetBestPageForOnlineURL( | 102 const OfflinePageItem* MaybeGetBestPageForOnlineURL( |
| 210 const GURL& online_url) const; | 103 const GURL& online_url) const override; |
| 211 | 104 void CheckForExternalFileDeletion() override; |
| 212 // Checks that all of the offline pages have corresponding offline copies. | |
| 213 // If a page is discovered to be missing an offline copy, its offline page | |
| 214 // metadata will be removed and |OfflinePageDeleted| will be sent to model | |
| 215 // observers. | |
| 216 void CheckForExternalFileDeletion(); | |
| 217 | |
| 218 // Marks pages as expired and removes their respective files from the archive | |
| 219 // directory. | |
| 220 void ExpirePages(const std::vector<int64_t>& offline_ids, | 105 void ExpirePages(const std::vector<int64_t>& offline_ids, |
| 221 const base::Time& expiration_time, | 106 const base::Time& expiration_time, |
| 222 const base::Callback<void(bool)>& callback) override; | 107 const base::Callback<void(bool)>& callback) override; |
| 223 | 108 ClientPolicyController* GetPolicyController() override; |
| 224 // Returns the policy controller. | |
| 225 ClientPolicyController* GetPolicyController(); | |
| 226 | 109 |
| 227 // Methods for testing only: | 110 // Methods for testing only: |
| 228 OfflinePageMetadataStore* GetStoreForTesting(); | 111 OfflinePageMetadataStore* GetStoreForTesting(); |
| 229 | 112 |
| 230 OfflinePageStorageManager* GetStorageManager(); | 113 OfflinePageStorageManager* GetStorageManager(); |
| 231 | 114 |
| 232 bool is_loaded() const; | 115 bool is_loaded() const override; |
| 233 | 116 |
| 234 protected: | 117 protected: |
| 235 // Adding a protected constructor for testing-only purposes in | 118 // Adding a protected constructor for testing-only purposes in |
| 236 // offline_page_storage_manager_unittest.cc | 119 // offline_page_storage_manager_unittest.cc |
| 237 OfflinePageModel(); | 120 OfflinePageModelImpl(); |
| 238 | 121 |
| 239 private: | 122 private: |
| 240 FRIEND_TEST_ALL_PREFIXES(OfflinePageModelTest, MarkPageForDeletion); | 123 FRIEND_TEST_ALL_PREFIXES(OfflinePageModelImplTest, MarkPageForDeletion); |
| 241 | 124 |
| 242 typedef ScopedVector<OfflinePageArchiver> PendingArchivers; | 125 typedef ScopedVector<OfflinePageArchiver> PendingArchivers; |
| 243 | 126 |
| 244 // Callback for ensuring archive directory is created. | 127 // Callback for ensuring archive directory is created. |
| 245 void OnEnsureArchivesDirCreatedDone(const base::TimeTicks& start_time); | 128 void OnEnsureArchivesDirCreatedDone(const base::TimeTicks& start_time); |
| 246 | 129 |
| 247 void GetAllPagesAfterLoadDone( | 130 void GetAllPagesAfterLoadDone( |
| 248 const MultipleOfflinePageItemCallback& callback); | 131 const MultipleOfflinePageItemCallback& callback); |
| 249 void CheckPagesExistOfflineAfterLoadDone( | 132 void CheckPagesExistOfflineAfterLoadDone( |
| 250 const std::set<GURL>& urls, | 133 const std::set<GURL>& urls, |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 // Controller of the client policies. | 256 // Controller of the client policies. |
| 374 std::unique_ptr<ClientPolicyController> policy_controller_; | 257 std::unique_ptr<ClientPolicyController> policy_controller_; |
| 375 | 258 |
| 376 // Manager for the storage consumed by archives and responsible for | 259 // Manager for the storage consumed by archives and responsible for |
| 377 // automatic page clearing. | 260 // automatic page clearing. |
| 378 std::unique_ptr<OfflinePageStorageManager> storage_manager_; | 261 std::unique_ptr<OfflinePageStorageManager> storage_manager_; |
| 379 | 262 |
| 380 // Manager for the offline archive files and directory. | 263 // Manager for the offline archive files and directory. |
| 381 std::unique_ptr<ArchiveManager> archive_manager_; | 264 std::unique_ptr<ArchiveManager> archive_manager_; |
| 382 | 265 |
| 383 base::WeakPtrFactory<OfflinePageModel> weak_ptr_factory_; | 266 base::WeakPtrFactory<OfflinePageModelImpl> weak_ptr_factory_; |
| 384 | 267 |
| 385 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel); | 268 DISALLOW_COPY_AND_ASSIGN(OfflinePageModelImpl); |
| 386 }; | 269 }; |
| 387 | 270 |
| 388 } // namespace offline_pages | 271 } // namespace offline_pages |
| 389 | 272 |
| 390 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ | 273 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_IMPL_H_ |
| OLD | NEW |