Chromium Code Reviews| 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" | 26 #include "base/supports_user_data.h" |
| 25 #include "components/keyed_service/core/keyed_service.h" | 27 #include "components/keyed_service/core/keyed_service.h" |
| 26 #include "components/offline_pages/offline_page_archiver.h" | 28 #include "components/offline_pages/offline_page_archiver.h" |
| 27 #include "components/offline_pages/offline_page_metadata_store.h" | 29 #include "components/offline_pages/offline_page_metadata_store.h" |
| 30 #include "components/offline_pages/offline_page_model.h" | |
| 28 #include "components/offline_pages/offline_page_storage_manager.h" | 31 #include "components/offline_pages/offline_page_storage_manager.h" |
| 29 #include "components/offline_pages/offline_page_types.h" | 32 #include "components/offline_pages/offline_page_types.h" |
| 30 | 33 |
| 31 class GURL; | 34 class GURL; |
| 32 namespace base { | 35 namespace base { |
| 33 class SequencedTaskRunner; | 36 class SequencedTaskRunner; |
| 34 class Time; | 37 class Time; |
| 35 class TimeDelta; | 38 class TimeDelta; |
| 36 class TimeTicks; | 39 class TimeTicks; |
| 37 } // namespace base | 40 } // namespace base |
| 38 | 41 |
| 39 namespace offline_pages { | 42 namespace offline_pages { |
| 40 | 43 |
| 41 static const char* const kBookmarkNamespace = "bookmark"; | |
| 42 static const int64_t kInvalidOfflineId = 0; | |
| 43 | |
| 44 struct ClientId; | 44 struct ClientId; |
| 45 struct OfflinePageItem; | 45 struct OfflinePageItem; |
| 46 | 46 |
| 47 class ArchiveManager; | 47 class ArchiveManager; |
| 48 class ClientPolicyController; | 48 class ClientPolicyController; |
| 49 class OfflinePageMetadataStore; | 49 class OfflinePageMetadataStore; |
| 50 class OfflinePageStorageManager; | 50 class OfflinePageStorageManager; |
| 51 | 51 |
| 52 // Service for saving pages offline, storing the offline copy and metadata, and | 52 // Implementation of service for saving pages offline, storing the offline |
| 53 // retrieving them upon request. | 53 // copy and metadata, and retrieving them upon request. |
| 54 // | 54 class OfflinePageModelImpl : public OfflinePageModel { |
| 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: | 55 public: |
| 73 // Observer of the OfflinePageModel. | 56 // All blocking calls/disk access will happen on the provided |task_runner|. |
| 74 class Observer { | 57 OfflinePageModelImpl( |
| 75 public: | 58 std::unique_ptr<OfflinePageMetadataStore> store, |
| 76 // Invoked when the model has finished loading. | 59 const base::FilePath& archives_dir, |
| 77 virtual void OfflinePageModelLoaded(OfflinePageModel* model) = 0; | 60 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 61 ~OfflinePageModelImpl() override; | |
| 78 | 62 |
| 79 // Invoked when the model is being updated, due to adding, removing or | 63 // Implemented methods: |
| 80 // updating an offline page. | 64 void AddObserver(Observer* observer) override; |
| 81 virtual void OfflinePageModelChanged(OfflinePageModel* model) = 0; | 65 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, | 66 void SavePage(const GURL& url, |
| 124 const ClientId& client_id, | 67 const ClientId& client_id, |
| 125 std::unique_ptr<OfflinePageArchiver> archiver, | 68 std::unique_ptr<OfflinePageArchiver> archiver, |
| 126 const SavePageCallback& callback); | 69 const SavePageCallback& callback) override; |
| 127 | 70 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, | |
|
romax
2016/05/26 01:26:48
Why is this removed? I think this is some kind of
dougarnett
2016/05/26 16:03:22
I was not able to find any implementation nor usag
| |
| 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, | 71 void DeletePagesByOfflineId(const std::vector<int64_t>& offline_ids, |
| 139 const DeletePageCallback& callback) override; | 72 const DeletePageCallback& callback) override; |
| 140 | |
| 141 // Wipes out all the data by deleting all saved files and clearing the store. | |
| 142 void ClearAll(const base::Closure& callback); | 73 void ClearAll(const base::Closure& callback); |
| 143 | |
| 144 // Deletes offline pages matching the URL predicate. | |
| 145 void DeletePagesByURLPredicate(const UrlPredicate& predicate, | 74 void DeletePagesByURLPredicate(const UrlPredicate& predicate, |
| 146 const DeletePageCallback& callback); | 75 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, | 76 void HasPages(const std::string& name_space, |
| 151 const HasPagesCallback& callback); | 77 const HasPagesCallback& callback) override; |
| 152 | 78 void CheckPagesExistOffline( |
| 153 // Returns via callback all GURLs in |urls| that are equal to the online URL | 79 const std::set<GURL>& urls, |
| 154 // of any offline page. | 80 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; | 81 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override; |
| 160 | 82 void GetOfflineIdsForClientId( |
| 161 // Gets all offline ids where the offline page has the matching client id. | 83 const ClientId& client_id, |
| 162 void GetOfflineIdsForClientId(const ClientId& client_id, | 84 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( | 85 const std::vector<int64_t> MaybeGetOfflineIdsForClientId( |
| 171 const ClientId& client_id) const; | 86 const ClientId& client_id) const override; |
| 172 | 87 void GetPageByOfflineId( |
| 173 // Returns zero or one offline pages associated with a specified |offline_id|. | 88 int64_t offline_id, |
| 174 void GetPageByOfflineId(int64_t offline_id, | 89 const SingleOfflinePageItemCallback& callback) override; |
| 175 const SingleOfflinePageItemCallback& callback); | 90 const OfflinePageItem* MaybeGetPageByOfflineId( |
| 176 | 91 int64_t offline_id) const override; |
| 177 // Returns an offline page associated with a specified |offline_id|. nullptr | 92 void GetPageByOfflineURL( |
| 178 // is returned if not found. | 93 const GURL& offline_url, |
| 179 const OfflinePageItem* MaybeGetPageByOfflineId(int64_t offline_id) const; | 94 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( | 95 const OfflinePageItem* MaybeGetPageByOfflineURL( |
| 192 const GURL& offline_url) const; | 96 const GURL& offline_url) const override; |
| 193 | 97 void GetPagesByOnlineURL( |
| 194 // Returns the offline pages that are stored under |online_url|. | 98 const GURL& online_url, |
| 195 void GetPagesByOnlineURL(const GURL& online_url, | 99 const MultipleOfflinePageItemCallback& callback) override; |
| 196 const MultipleOfflinePageItemCallback& callback); | 100 void GetBestPageForOnlineURL( |
| 197 | 101 const GURL& online_url, |
| 198 // Returns via callback an offline page saved for |online_url|, if any. The | 102 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( | 103 const OfflinePageItem* MaybeGetBestPageForOnlineURL( |
| 210 const GURL& online_url) const; | 104 const GURL& online_url) const override; |
| 211 | 105 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, | 106 void ExpirePages(const std::vector<int64_t>& offline_ids, |
| 221 const base::Time& expiration_time); | 107 const base::Time& expiration_time) override; |
| 222 | 108 ClientPolicyController* GetPolicyController() override; |
| 223 // Returns the policy controller. | |
| 224 ClientPolicyController* GetPolicyController(); | |
| 225 | 109 |
| 226 // Methods for testing only: | 110 // Methods for testing only: |
| 227 OfflinePageMetadataStore* GetStoreForTesting(); | 111 OfflinePageMetadataStore* GetStoreForTesting(); |
| 228 | 112 |
| 229 OfflinePageStorageManager* GetStorageManager(); | 113 OfflinePageStorageManager* GetStorageManager(); |
| 230 | 114 |
| 231 bool is_loaded() const; | 115 bool is_loaded() const override; |
| 232 | 116 |
| 233 protected: | 117 protected: |
| 234 // Adding a protected constructor for testing-only purposes in | 118 // Adding a protected constructor for testing-only purposes in |
| 235 // offline_page_storage_manager_unittest.cc | 119 // offline_page_storage_manager_unittest.cc |
| 236 OfflinePageModel(); | 120 OfflinePageModelImpl(); |
| 237 | 121 |
| 238 private: | 122 private: |
| 239 FRIEND_TEST_ALL_PREFIXES(OfflinePageModelTest, MarkPageForDeletion); | 123 FRIEND_TEST_ALL_PREFIXES(OfflinePageModelImplTest, MarkPageForDeletion); |
| 240 | 124 |
| 241 typedef ScopedVector<OfflinePageArchiver> PendingArchivers; | 125 typedef ScopedVector<OfflinePageArchiver> PendingArchivers; |
| 242 | 126 |
| 243 // Callback for ensuring archive directory is created. | 127 // Callback for ensuring archive directory is created. |
| 244 void OnEnsureArchivesDirCreatedDone(const base::TimeTicks& start_time); | 128 void OnEnsureArchivesDirCreatedDone(const base::TimeTicks& start_time); |
| 245 | 129 |
| 246 void GetAllPagesAfterLoadDone( | 130 void GetAllPagesAfterLoadDone( |
| 247 const MultipleOfflinePageItemCallback& callback); | 131 const MultipleOfflinePageItemCallback& callback); |
| 248 void CheckPagesExistOfflineAfterLoadDone( | 132 void CheckPagesExistOfflineAfterLoadDone( |
| 249 const std::set<GURL>& urls, | 133 const std::set<GURL>& urls, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 364 // Controller of the client policies. | 248 // Controller of the client policies. |
| 365 std::unique_ptr<ClientPolicyController> policy_controller_; | 249 std::unique_ptr<ClientPolicyController> policy_controller_; |
| 366 | 250 |
| 367 // Manager for the storage consumed by archives and responsible for | 251 // Manager for the storage consumed by archives and responsible for |
| 368 // automatic page clearing. | 252 // automatic page clearing. |
| 369 std::unique_ptr<OfflinePageStorageManager> storage_manager_; | 253 std::unique_ptr<OfflinePageStorageManager> storage_manager_; |
| 370 | 254 |
| 371 // Manager for the offline archive files and directory. | 255 // Manager for the offline archive files and directory. |
| 372 std::unique_ptr<ArchiveManager> archive_manager_; | 256 std::unique_ptr<ArchiveManager> archive_manager_; |
| 373 | 257 |
| 374 base::WeakPtrFactory<OfflinePageModel> weak_ptr_factory_; | 258 base::WeakPtrFactory<OfflinePageModelImpl> weak_ptr_factory_; |
| 375 | 259 |
| 376 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel); | 260 DISALLOW_COPY_AND_ASSIGN(OfflinePageModelImpl); |
| 377 }; | 261 }; |
| 378 | 262 |
| 379 } // namespace offline_pages | 263 } // namespace offline_pages |
| 380 | 264 |
| 381 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ | 265 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_IMPL_H_ |
| OLD | NEW |