OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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_H_ |
6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/gtest_prod_util.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
17 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
18 #include "base/scoped_observer.h" | 19 #include "base/scoped_observer.h" |
19 #include "components/bookmarks/browser/base_bookmark_model_observer.h" | 20 #include "components/bookmarks/browser/base_bookmark_model_observer.h" |
20 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
21 #include "components/offline_pages/offline_page_archiver.h" | 22 #include "components/offline_pages/offline_page_archiver.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 CANCELLED, | 92 CANCELLED, |
92 STORE_FAILURE, | 93 STORE_FAILURE, |
93 }; | 94 }; |
94 | 95 |
95 // Observer of the OfflinePageModel. | 96 // Observer of the OfflinePageModel. |
96 class Observer { | 97 class Observer { |
97 public: | 98 public: |
98 // Invoked when the model has finished loading. | 99 // Invoked when the model has finished loading. |
99 virtual void OfflinePageModelLoaded(OfflinePageModel* model) = 0; | 100 virtual void OfflinePageModelLoaded(OfflinePageModel* model) = 0; |
100 | 101 |
| 102 // Invoked when the model is being updated, due to adding, removing or |
| 103 // updating an offline page. |
| 104 virtual void OfflinePageModelChanged(OfflinePageModel* model) = 0; |
| 105 |
101 protected: | 106 protected: |
102 virtual ~Observer() {} | 107 virtual ~Observer() {} |
103 }; | 108 }; |
104 | 109 |
105 typedef base::Callback<void(SavePageResult)> SavePageCallback; | 110 typedef base::Callback<void(SavePageResult)> SavePageCallback; |
106 typedef base::Callback<void(DeletePageResult)> DeletePageCallback; | 111 typedef base::Callback<void(DeletePageResult)> DeletePageCallback; |
107 | 112 |
108 // All blocking calls/disk access will happen on the provided |task_runner|. | 113 // All blocking calls/disk access will happen on the provided |task_runner|. |
109 OfflinePageModel( | 114 OfflinePageModel( |
110 scoped_ptr<OfflinePageMetadataStore> store, | 115 scoped_ptr<OfflinePageMetadataStore> store, |
(...skipping 17 matching lines...) Expand all Loading... |
128 void SavePage(const GURL& url, | 133 void SavePage(const GURL& url, |
129 int64 bookmark_id, | 134 int64 bookmark_id, |
130 scoped_ptr<OfflinePageArchiver> archiver, | 135 scoped_ptr<OfflinePageArchiver> archiver, |
131 const SavePageCallback& callback); | 136 const SavePageCallback& callback); |
132 | 137 |
133 // Marks that the offline page related to the passed |bookmark_id| has been | 138 // Marks that the offline page related to the passed |bookmark_id| has been |
134 // accessed. Its access info, including last access time and access count, | 139 // accessed. Its access info, including last access time and access count, |
135 // will be updated. Requires that the model is loaded. | 140 // will be updated. Requires that the model is loaded. |
136 void MarkPageAccessed(int64 bookmark_id); | 141 void MarkPageAccessed(int64 bookmark_id); |
137 | 142 |
| 143 // Marks that the offline page related to the passed |bookmark_id| was going |
| 144 // to be deleted. The deletion will occur in a short while. The undo can be |
| 145 // done before this. Requires that the model is loaded. |
| 146 void MarkPageForDeletion(int64 bookmark_id, |
| 147 const DeletePageCallback& callback); |
| 148 |
138 // Deletes an offline page related to the passed |bookmark_id|. Requires that | 149 // Deletes an offline page related to the passed |bookmark_id|. Requires that |
139 // the model is loaded. | 150 // the model is loaded. |
140 void DeletePageByBookmarkId(int64 bookmark_id, | 151 void DeletePageByBookmarkId(int64 bookmark_id, |
141 const DeletePageCallback& callback); | 152 const DeletePageCallback& callback); |
142 | 153 |
143 // Deletes offline pages related to the passed |bookmark_ids|. Requires that | 154 // Deletes offline pages related to the passed |bookmark_ids|. Requires that |
144 // the model is loaded. | 155 // the model is loaded. |
145 void DeletePagesByBookmarkId(const std::vector<int64>& bookmark_ids, | 156 void DeletePagesByBookmarkId(const std::vector<int64>& bookmark_ids, |
146 const DeletePageCallback& callback); | 157 const DeletePageCallback& callback); |
147 | 158 |
| 159 void UndeletePage(int64 bookmark_id, const DeletePageCallback& callback); |
| 160 |
148 // Gets all available offline pages. Requires that the model is loaded. | 161 // Gets all available offline pages. Requires that the model is loaded. |
149 const std::vector<OfflinePageItem> GetAllPages() const; | 162 const std::vector<OfflinePageItem> GetAllPages() const; |
150 | 163 |
151 // Gets pages that should be removed to clean up storage. Requires that the | 164 // Gets pages that should be removed to clean up storage. Requires that the |
152 // model is loaded. | 165 // model is loaded. |
153 const std::vector<OfflinePageItem> GetPagesToCleanUp() const; | 166 const std::vector<OfflinePageItem> GetPagesToCleanUp() const; |
154 | 167 |
155 // Returns an offline page associated with a specified |bookmark_id|. nullptr | 168 // Returns an offline page associated with a specified |bookmark_id|. nullptr |
156 // is returned if not found. | 169 // is returned if not found. |
157 const OfflinePageItem* GetPageByBookmarkId(int64 bookmark_id) const; | 170 const OfflinePageItem* GetPageByBookmarkId(int64 bookmark_id) const; |
158 | 171 |
159 // Returns an offline page that is stored as |offline_url|. nullptr is | 172 // Returns an offline page that is stored as |offline_url|. nullptr is |
160 // returned if not found. | 173 // returned if not found. |
161 const OfflinePageItem* GetPageByOfflineURL(const GURL& offline_url) const; | 174 const OfflinePageItem* GetPageByOfflineURL(const GURL& offline_url) const; |
162 | 175 |
163 // Methods for testing only: | 176 // Methods for testing only: |
164 OfflinePageMetadataStore* GetStoreForTesting(); | 177 OfflinePageMetadataStore* GetStoreForTesting(); |
165 | 178 |
166 bool is_loaded() const { return is_loaded_; } | 179 bool is_loaded() const { return is_loaded_; } |
167 | 180 |
168 private: | 181 private: |
| 182 FRIEND_TEST_ALL_PREFIXES(OfflinePageModelTest, MarkPageForDeletion); |
| 183 |
169 typedef ScopedVector<OfflinePageArchiver> PendingArchivers; | 184 typedef ScopedVector<OfflinePageArchiver> PendingArchivers; |
170 | 185 |
171 // BaseBookmarkModelObserver: | 186 // BaseBookmarkModelObserver: |
172 void BookmarkModelChanged() override; | 187 void BookmarkModelChanged() override; |
| 188 void BookmarkNodeAdded(bookmarks::BookmarkModel* model, |
| 189 const bookmarks::BookmarkNode* parent, |
| 190 int index) override; |
173 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, | 191 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, |
174 const bookmarks::BookmarkNode* parent, | 192 const bookmarks::BookmarkNode* parent, |
175 int old_index, | 193 int old_index, |
176 const bookmarks::BookmarkNode* node, | 194 const bookmarks::BookmarkNode* node, |
177 const std::set<GURL>& removed_urls) override; | 195 const std::set<GURL>& removed_urls) override; |
178 | 196 |
179 // Callback for loading pages from the offline page metadata store. | 197 // Callback for loading pages from the offline page metadata store. |
180 void OnLoadDone(bool success, | 198 void OnLoadDone(bool success, |
181 const std::vector<OfflinePageItem>& offline_pages); | 199 const std::vector<OfflinePageItem>& offline_pages); |
182 | 200 |
(...skipping 18 matching lines...) Expand all Loading... |
201 void OnDeleteArchiveFilesDone( | 219 void OnDeleteArchiveFilesDone( |
202 const std::vector<int64>& bookmark_ids, | 220 const std::vector<int64>& bookmark_ids, |
203 const DeletePageCallback& callback, | 221 const DeletePageCallback& callback, |
204 const bool* success); | 222 const bool* success); |
205 void OnRemoveOfflinePagesDone(const std::vector<int64>& bookmark_ids, | 223 void OnRemoveOfflinePagesDone(const std::vector<int64>& bookmark_ids, |
206 const DeletePageCallback& callback, | 224 const DeletePageCallback& callback, |
207 bool success); | 225 bool success); |
208 void InformDeletePageDone(const DeletePageCallback& callback, | 226 void InformDeletePageDone(const DeletePageCallback& callback, |
209 DeletePageResult result); | 227 DeletePageResult result); |
210 | 228 |
211 void OnUpdateOfflinePageDone(const OfflinePageItem& offline_page_item, | 229 void OnMarkPageAccesseDone(const OfflinePageItem& offline_page_item, |
212 bool success); | 230 bool success); |
| 231 |
| 232 // Steps for marking an offline page for deletion that can be undo-ed. |
| 233 void OnMarkPageForDeletionDone(const OfflinePageItem& offline_page_item, |
| 234 const DeletePageCallback& callback, |
| 235 bool success); |
| 236 void FinalizePageDeletion(); |
| 237 |
| 238 // Steps for undoing a offline page deletion. |
| 239 void UndoPageDeletion(int64 bookmark_id); |
| 240 void OnUndoOfflinePageDone(const OfflinePageItem& offline_page, bool success); |
213 | 241 |
214 // Persistent store for offline page metadata. | 242 // Persistent store for offline page metadata. |
215 scoped_ptr<OfflinePageMetadataStore> store_; | 243 scoped_ptr<OfflinePageMetadataStore> store_; |
216 | 244 |
217 // The observers. | 245 // The observers. |
218 base::ObserverList<Observer> observers_; | 246 base::ObserverList<Observer> observers_; |
219 | 247 |
220 bool is_loaded_; | 248 bool is_loaded_; |
221 | 249 |
222 // In memory copy of the offline page metadata, keyed by bookmark IDs. | 250 // In memory copy of the offline page metadata, keyed by bookmark IDs. |
(...skipping 11 matching lines...) Expand all Loading... |
234 scoped_observer_; | 262 scoped_observer_; |
235 | 263 |
236 base::WeakPtrFactory<OfflinePageModel> weak_ptr_factory_; | 264 base::WeakPtrFactory<OfflinePageModel> weak_ptr_factory_; |
237 | 265 |
238 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel); | 266 DISALLOW_COPY_AND_ASSIGN(OfflinePageModel); |
239 }; | 267 }; |
240 | 268 |
241 } // namespace offline_pages | 269 } // namespace offline_pages |
242 | 270 |
243 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ | 271 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_MODEL_H_ |
OLD | NEW |