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 #include "components/offline_pages/offline_page_model.h" | 5 #include "components/offline_pages/offline_page_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
14 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
15 #include "base/thread_task_runner_handle.h" | |
15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
16 #include "components/bookmarks/browser/bookmark_model.h" | 17 #include "components/bookmarks/browser/bookmark_model.h" |
17 #include "components/bookmarks/browser/bookmark_node.h" | 18 #include "components/bookmarks/browser/bookmark_node.h" |
18 #include "components/offline_pages/offline_page_item.h" | 19 #include "components/offline_pages/offline_page_item.h" |
19 #include "components/offline_pages/offline_page_metadata_store.h" | 20 #include "components/offline_pages/offline_page_metadata_store.h" |
20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
21 | 22 |
22 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; | 23 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; |
23 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; | 24 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; |
24 | 25 |
25 namespace offline_pages { | 26 namespace offline_pages { |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 // Threshold for how old offline copy of a page should be before we offer to | 30 // Threshold for how old offline copy of a page should be before we offer to |
30 // delete it to free up space. | 31 // delete it to free up space. |
31 const base::TimeDelta kPageCleanUpThreshold = base::TimeDelta::FromDays(30); | 32 const base::TimeDelta kPageCleanUpThreshold = base::TimeDelta::FromDays(30); |
32 | 33 |
34 // The delay for the final deletion to kick in after the page is marked for | |
35 // deletion. The value set here is a bit longer that the duration of the | |
36 // snackbar that offers undo. | |
37 const base::TimeDelta kFinalDeletionDelay = base::TimeDelta::FromSeconds(6); | |
38 | |
33 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { | 39 SavePageResult ToSavePageResult(ArchiverResult archiver_result) { |
34 SavePageResult result; | 40 SavePageResult result; |
35 switch (archiver_result) { | 41 switch (archiver_result) { |
36 case ArchiverResult::SUCCESSFULLY_CREATED: | 42 case ArchiverResult::SUCCESSFULLY_CREATED: |
37 result = SavePageResult::SUCCESS; | 43 result = SavePageResult::SUCCESS; |
38 break; | 44 break; |
39 case ArchiverResult::ERROR_DEVICE_FULL: | 45 case ArchiverResult::ERROR_DEVICE_FULL: |
40 result = SavePageResult::DEVICE_FULL; | 46 result = SavePageResult::DEVICE_FULL; |
41 break; | 47 break; |
42 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE: | 48 case ArchiverResult::ERROR_CONTENT_UNAVAILABLE: |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 if (iter == offline_pages_.end()) | 124 if (iter == offline_pages_.end()) |
119 return; | 125 return; |
120 | 126 |
121 // Make a copy of the cached item and update it. The cached item should only | 127 // Make a copy of the cached item and update it. The cached item should only |
122 // be updated upon the successful store operation. | 128 // be updated upon the successful store operation. |
123 OfflinePageItem offline_page_item = iter->second; | 129 OfflinePageItem offline_page_item = iter->second; |
124 offline_page_item.last_access_time = base::Time::Now(); | 130 offline_page_item.last_access_time = base::Time::Now(); |
125 offline_page_item.access_count++; | 131 offline_page_item.access_count++; |
126 store_->AddOrUpdateOfflinePage( | 132 store_->AddOrUpdateOfflinePage( |
127 offline_page_item, | 133 offline_page_item, |
128 base::Bind(&OfflinePageModel::OnUpdateOfflinePageDone, | 134 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, |
129 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | 135 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); |
130 } | 136 } |
131 | 137 |
138 void OfflinePageModel::MarkPageForDeletion( | |
139 int64 bookmark_id, | |
140 const DeletePageCallback& callback) { | |
141 DCHECK(is_loaded_); | |
142 auto iter = offline_pages_.find(bookmark_id); | |
143 if (iter == offline_pages_.end()) { | |
144 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); | |
145 return; | |
146 } | |
147 | |
148 // Make a copy of the cached item and update it. The cached item should only | |
149 // be updated upon the successful store operation. | |
150 OfflinePageItem offline_page_item = iter->second; | |
151 offline_page_item.MarkForDeletion(); | |
152 store_->AddOrUpdateOfflinePage( | |
153 offline_page_item, | |
154 base::Bind(&OfflinePageModel::OnMarkPageForDeletionDone, | |
155 weak_ptr_factory_.GetWeakPtr(), offline_page_item, callback)); | |
156 } | |
157 | |
132 void OfflinePageModel::DeletePageByBookmarkId( | 158 void OfflinePageModel::DeletePageByBookmarkId( |
133 int64 bookmark_id, | 159 int64 bookmark_id, |
134 const DeletePageCallback& callback) { | 160 const DeletePageCallback& callback) { |
135 DCHECK(is_loaded_); | 161 DCHECK(is_loaded_); |
136 std::vector<int64> bookmark_ids_to_delete; | 162 std::vector<int64> bookmark_ids_to_delete; |
137 bookmark_ids_to_delete.push_back(bookmark_id); | 163 bookmark_ids_to_delete.push_back(bookmark_id); |
138 DeletePagesByBookmarkId(bookmark_ids_to_delete, callback); | 164 DeletePagesByBookmarkId(bookmark_ids_to_delete, callback); |
139 } | 165 } |
140 | 166 |
141 void OfflinePageModel::DeletePagesByBookmarkId( | 167 void OfflinePageModel::DeletePagesByBookmarkId( |
(...skipping 21 matching lines...) Expand all Loading... | |
163 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, | 189 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, |
164 weak_ptr_factory_.GetWeakPtr(), | 190 weak_ptr_factory_.GetWeakPtr(), |
165 bookmark_ids, | 191 bookmark_ids, |
166 callback, | 192 callback, |
167 base::Owned(success))); | 193 base::Owned(success))); |
168 } | 194 } |
169 | 195 |
170 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { | 196 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { |
171 DCHECK(is_loaded_); | 197 DCHECK(is_loaded_); |
172 std::vector<OfflinePageItem> offline_pages; | 198 std::vector<OfflinePageItem> offline_pages; |
173 for (const auto& id_page_pair : offline_pages_) | 199 for (const auto& id_page_pair : offline_pages_) { |
200 if (id_page_pair.second.IsMarkedForDeletion()) | |
201 continue; | |
174 offline_pages.push_back(id_page_pair.second); | 202 offline_pages.push_back(id_page_pair.second); |
203 } | |
175 return offline_pages; | 204 return offline_pages; |
176 } | 205 } |
177 | 206 |
178 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const { | 207 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const { |
179 DCHECK(is_loaded_); | 208 DCHECK(is_loaded_); |
180 std::vector<OfflinePageItem> offline_pages; | 209 std::vector<OfflinePageItem> offline_pages; |
181 base::Time now = base::Time::Now(); | 210 base::Time now = base::Time::Now(); |
182 for (const auto& id_page_pair : offline_pages_) { | 211 for (const auto& id_page_pair : offline_pages_) { |
183 if (now - id_page_pair.second.creation_time > kPageCleanUpThreshold) | 212 if (!id_page_pair.second.IsMarkedForDeletion() && |
213 now - id_page_pair.second.creation_time > kPageCleanUpThreshold) { | |
184 offline_pages.push_back(id_page_pair.second); | 214 offline_pages.push_back(id_page_pair.second); |
215 } | |
185 } | 216 } |
186 return offline_pages; | 217 return offline_pages; |
187 } | 218 } |
188 | 219 |
189 const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId( | 220 const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId( |
190 int64 bookmark_id) const { | 221 int64 bookmark_id) const { |
191 const auto iter = offline_pages_.find(bookmark_id); | 222 const auto iter = offline_pages_.find(bookmark_id); |
192 return iter != offline_pages_.end() ? &(iter->second) : nullptr; | 223 return iter != offline_pages_.end() ? &(iter->second) : nullptr; |
193 } | 224 } |
194 | 225 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 result = SavePageResult::SUCCESS; | 281 result = SavePageResult::SUCCESS; |
251 UMA_HISTOGRAM_MEMORY_KB( | 282 UMA_HISTOGRAM_MEMORY_KB( |
252 "OfflinePages.PageSize", offline_page.file_size / 1024); | 283 "OfflinePages.PageSize", offline_page.file_size / 1024); |
253 } else { | 284 } else { |
254 result = SavePageResult::STORE_FAILURE; | 285 result = SavePageResult::STORE_FAILURE; |
255 } | 286 } |
256 InformSavePageDone(callback, result); | 287 InformSavePageDone(callback, result); |
257 DeletePendingArchiver(archiver); | 288 DeletePendingArchiver(archiver); |
258 } | 289 } |
259 | 290 |
260 void OfflinePageModel::OnUpdateOfflinePageDone( | 291 void OfflinePageModel::OnMarkPageAccesseDone( |
261 const OfflinePageItem& offline_page_item, bool success) { | 292 const OfflinePageItem& offline_page_item, bool success) { |
262 // Update the item in the cache only upon success. | 293 // Update the item in the cache only upon success. |
263 if (success) | 294 if (success) |
264 offline_pages_[offline_page_item.bookmark_id] = offline_page_item; | 295 offline_pages_[offline_page_item.bookmark_id] = offline_page_item; |
265 } | 296 } |
266 | 297 |
298 void OfflinePageModel::OnMarkPageForDeletionDone( | |
299 const OfflinePageItem& offline_page_item, | |
300 const DeletePageCallback& callback, | |
301 bool success) { | |
302 // Update the item in the cache only upon success. | |
303 if (success) | |
304 offline_pages_[offline_page_item.bookmark_id] = offline_page_item; | |
305 | |
306 InformDeletePageDone(callback, success ? DeletePageResult::SUCCESS | |
307 : DeletePageResult::STORE_FAILURE); | |
308 | |
309 if (!success) | |
310 return; | |
311 | |
312 // Schedule to do the final deletion. | |
313 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
314 FROM_HERE, | |
315 base::Bind(&OfflinePageModel::FinalizePageDeletion, | |
316 weak_ptr_factory_.GetWeakPtr()), | |
317 kFinalDeletionDelay); | |
318 } | |
319 | |
320 void OfflinePageModel::OnUndoOfflinePageDone( | |
321 const OfflinePageItem& offline_page, int64 old_bookmark_id, bool success) { | |
322 if (!success) | |
323 return; | |
324 offline_pages_.erase(old_bookmark_id); | |
325 offline_pages_[offline_page.bookmark_id] = offline_page; | |
326 } | |
327 | |
328 void OfflinePageModel::FinalizePageDeletion() { | |
329 std::vector<int64> bookmark_ids_pending_deletion; | |
330 for (const auto& id_page_pair : offline_pages_) { | |
331 if (!id_page_pair.second.IsMarkedForDeletion()) | |
332 continue; | |
333 bookmark_ids_pending_deletion.push_back(id_page_pair.second.bookmark_id); | |
334 } | |
335 DeletePagesByBookmarkId(bookmark_ids_pending_deletion, DeletePageCallback()); | |
336 } | |
337 | |
267 void OfflinePageModel::BookmarkModelChanged() { | 338 void OfflinePageModel::BookmarkModelChanged() { |
268 } | 339 } |
269 | 340 |
270 void OfflinePageModel::BookmarkNodeRemoved( | 341 void OfflinePageModel::BookmarkNodeRemoved( |
271 bookmarks::BookmarkModel* model, | 342 bookmarks::BookmarkModel* model, |
272 const bookmarks::BookmarkNode* parent, | 343 const bookmarks::BookmarkNode* parent, |
273 int old_index, | 344 int old_index, |
274 const bookmarks::BookmarkNode* node, | 345 const bookmarks::BookmarkNode* node, |
275 const std::set<GURL>& removed_urls) { | 346 const std::set<GURL>& removed_urls) { |
276 if (!is_loaded_) { | 347 if (!is_loaded_) { |
277 delayed_tasks_.push_back( | 348 delayed_tasks_.push_back( |
278 base::Bind(&OfflinePageModel::DeletePageByBookmarkId, | 349 base::Bind(&OfflinePageModel::MarkPageForDeletion, |
279 weak_ptr_factory_.GetWeakPtr(), | 350 weak_ptr_factory_.GetWeakPtr(), |
280 node->id(), | 351 node->id(), |
281 base::Bind(&EmptyDeleteCallback))); | 352 base::Bind(&EmptyDeleteCallback))); |
282 return; | 353 return; |
283 } | 354 } |
284 DeletePageByBookmarkId(node->id(), base::Bind(&EmptyDeleteCallback)); | 355 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); |
356 } | |
357 | |
358 void OfflinePageModel::BookmarkNodeRenumbered(bookmarks::BookmarkModel* model, | |
359 int64 old_bookmark_id, | |
360 int64 new_bookmark_id) { | |
361 // The old bookmark that is marked for deletion should still exist. | |
362 auto old_page_item_iter = offline_pages_.find(old_bookmark_id); | |
363 if (old_page_item_iter == offline_pages_.end() || | |
364 !old_page_item_iter->second.IsMarkedForDeletion()) { | |
365 NOTREACHED(); | |
366 return; | |
367 } | |
368 | |
369 // The new bookmark should not exist in our metadata record. | |
370 if (offline_pages_.find(new_bookmark_id) != offline_pages_.end()) { | |
371 NOTREACHED(); | |
372 return; | |
373 } | |
374 | |
375 // Write the old metadata with the new bookmark ID to the store. | |
376 OfflinePageItem new_page_item = old_page_item_iter->second; | |
377 new_page_item.bookmark_id = new_bookmark_id; | |
378 new_page_item.ClearMarkForDeletion(); | |
379 store_->AddOrUpdateOfflinePage( | |
380 new_page_item, | |
381 base::Bind(&OfflinePageModel::OnUndoOfflinePageDone, | |
382 weak_ptr_factory_.GetWeakPtr(), new_page_item, | |
383 old_bookmark_id)); | |
285 } | 384 } |
286 | 385 |
287 void OfflinePageModel::OnLoadDone( | 386 void OfflinePageModel::OnLoadDone( |
288 bool success, | 387 bool success, |
289 const std::vector<OfflinePageItem>& offline_pages) { | 388 const std::vector<OfflinePageItem>& offline_pages) { |
290 DCHECK(!is_loaded_); | 389 DCHECK(!is_loaded_); |
291 is_loaded_ = true; | 390 is_loaded_ = true; |
292 | 391 |
293 if (success) { | 392 if (success) { |
294 for (const auto& offline_page : offline_pages) | 393 for (const auto& offline_page : offline_pages) |
295 offline_pages_[offline_page.bookmark_id] = offline_page; | 394 offline_pages_[offline_page.bookmark_id] = offline_page; |
296 } | 395 } |
297 | 396 |
298 // Run all the delayed tasks. | 397 // Run all the delayed tasks. |
299 for (const auto& delayed_task : delayed_tasks_) | 398 for (const auto& delayed_task : delayed_tasks_) |
300 delayed_task.Run(); | 399 delayed_task.Run(); |
301 delayed_tasks_.clear(); | 400 delayed_tasks_.clear(); |
302 | 401 |
402 // If there are pages that are marked for deletion, but not yet deleted and | |
403 // OfflinePageModel gets reloaded. Delete the pages now. | |
fgorski
2015/09/25 21:23:57
Your first sentence does not conclude. How about:
jianli
2015/10/09 23:26:26
Done.
| |
404 FinalizePageDeletion(); | |
405 | |
303 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 406 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
304 } | 407 } |
305 | 408 |
306 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 409 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, |
307 SavePageResult result) { | 410 SavePageResult result) { |
308 UMA_HISTOGRAM_ENUMERATION( | 411 UMA_HISTOGRAM_ENUMERATION( |
309 "OfflinePages.SavePageResult", | 412 "OfflinePages.SavePageResult", |
310 static_cast<int>(result), | 413 static_cast<int>(result), |
311 static_cast<int>(SavePageResult::RESULT_COUNT)); | 414 static_cast<int>(SavePageResult::RESULT_COUNT)); |
312 callback.Run(result); | 415 callback.Run(result); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS | 461 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS |
359 : DeletePageResult::STORE_FAILURE); | 462 : DeletePageResult::STORE_FAILURE); |
360 } | 463 } |
361 | 464 |
362 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 465 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, |
363 DeletePageResult result) { | 466 DeletePageResult result) { |
364 UMA_HISTOGRAM_ENUMERATION( | 467 UMA_HISTOGRAM_ENUMERATION( |
365 "OfflinePages.DeletePageResult", | 468 "OfflinePages.DeletePageResult", |
366 static_cast<int>(result), | 469 static_cast<int>(result), |
367 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 470 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
368 callback.Run(result); | 471 if (!callback.is_null()) |
472 callback.Run(result); | |
369 } | 473 } |
370 | 474 |
371 } // namespace offline_pages | 475 } // namespace offline_pages |
OLD | NEW |