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.flags = static_cast<OfflinePageItem::Flags>( | |
fgorski
2015/09/25 20:30:50
create MarkForDeletion method in OfflinePageItem
jianli
2015/09/25 20:52:35
Done.
| |
152 static_cast<int>(offline_page_item.flags) | | |
153 OfflinePageItem::MARKED_FOR_DELETION); | |
154 store_->AddOrUpdateOfflinePage( | |
155 offline_page_item, | |
156 base::Bind(&OfflinePageModel::OnMarkPageForDeletionDone, | |
157 weak_ptr_factory_.GetWeakPtr(), offline_page_item, callback)); | |
158 } | |
159 | |
132 void OfflinePageModel::DeletePageByBookmarkId( | 160 void OfflinePageModel::DeletePageByBookmarkId( |
133 int64 bookmark_id, | 161 int64 bookmark_id, |
134 const DeletePageCallback& callback) { | 162 const DeletePageCallback& callback) { |
135 DCHECK(is_loaded_); | 163 DCHECK(is_loaded_); |
136 std::vector<int64> bookmark_ids_to_delete; | 164 std::vector<int64> bookmark_ids_to_delete; |
137 bookmark_ids_to_delete.push_back(bookmark_id); | 165 bookmark_ids_to_delete.push_back(bookmark_id); |
138 DeletePagesByBookmarkId(bookmark_ids_to_delete, callback); | 166 DeletePagesByBookmarkId(bookmark_ids_to_delete, callback); |
139 } | 167 } |
140 | 168 |
141 void OfflinePageModel::DeletePagesByBookmarkId( | 169 void OfflinePageModel::DeletePagesByBookmarkId( |
(...skipping 21 matching lines...) Expand all Loading... | |
163 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, | 191 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, |
164 weak_ptr_factory_.GetWeakPtr(), | 192 weak_ptr_factory_.GetWeakPtr(), |
165 bookmark_ids, | 193 bookmark_ids, |
166 callback, | 194 callback, |
167 base::Owned(success))); | 195 base::Owned(success))); |
168 } | 196 } |
169 | 197 |
170 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { | 198 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { |
171 DCHECK(is_loaded_); | 199 DCHECK(is_loaded_); |
172 std::vector<OfflinePageItem> offline_pages; | 200 std::vector<OfflinePageItem> offline_pages; |
173 for (const auto& id_page_pair : offline_pages_) | 201 for (const auto& id_page_pair : offline_pages_) { |
202 if (id_page_pair.second.IsMarkedForDeletion()) | |
203 continue; | |
174 offline_pages.push_back(id_page_pair.second); | 204 offline_pages.push_back(id_page_pair.second); |
205 } | |
175 return offline_pages; | 206 return offline_pages; |
176 } | 207 } |
177 | 208 |
178 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const { | 209 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const { |
179 DCHECK(is_loaded_); | 210 DCHECK(is_loaded_); |
180 std::vector<OfflinePageItem> offline_pages; | 211 std::vector<OfflinePageItem> offline_pages; |
181 base::Time now = base::Time::Now(); | 212 base::Time now = base::Time::Now(); |
182 for (const auto& id_page_pair : offline_pages_) { | 213 for (const auto& id_page_pair : offline_pages_) { |
183 if (now - id_page_pair.second.creation_time > kPageCleanUpThreshold) | 214 if (now - id_page_pair.second.creation_time > kPageCleanUpThreshold) |
184 offline_pages.push_back(id_page_pair.second); | 215 offline_pages.push_back(id_page_pair.second); |
(...skipping 65 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 // No need to report UMA for the undo operation. | |
fgorski
2015/09/25 20:30:50
Why?
jianli
2015/09/25 20:52:35
I meant not do report the UMA like in OnAddOffline
| |
327 } | |
328 | |
329 void OfflinePageModel::FinalizePageDeletion() { | |
330 std::vector<int64> bookmark_ids_pending_deletion; | |
331 for (const auto& id_page_pair : offline_pages_) { | |
332 if (!id_page_pair.second.IsMarkedForDeletion()) | |
333 continue; | |
334 bookmark_ids_pending_deletion.push_back(id_page_pair.second.bookmark_id); | |
335 } | |
336 DeletePagesByBookmarkId(bookmark_ids_pending_deletion, DeletePageCallback()); | |
337 } | |
338 | |
267 void OfflinePageModel::BookmarkModelChanged() { | 339 void OfflinePageModel::BookmarkModelChanged() { |
268 } | 340 } |
269 | 341 |
270 void OfflinePageModel::BookmarkNodeRemoved( | 342 void OfflinePageModel::BookmarkNodeRemoved( |
271 bookmarks::BookmarkModel* model, | 343 bookmarks::BookmarkModel* model, |
272 const bookmarks::BookmarkNode* parent, | 344 const bookmarks::BookmarkNode* parent, |
273 int old_index, | 345 int old_index, |
274 const bookmarks::BookmarkNode* node, | 346 const bookmarks::BookmarkNode* node, |
275 const std::set<GURL>& removed_urls) { | 347 const std::set<GURL>& removed_urls) { |
276 if (!is_loaded_) { | 348 if (!is_loaded_) { |
277 delayed_tasks_.push_back( | 349 delayed_tasks_.push_back( |
278 base::Bind(&OfflinePageModel::DeletePageByBookmarkId, | 350 base::Bind(&OfflinePageModel::MarkPageForDeletion, |
279 weak_ptr_factory_.GetWeakPtr(), | 351 weak_ptr_factory_.GetWeakPtr(), |
280 node->id(), | 352 node->id(), |
281 base::Bind(&EmptyDeleteCallback))); | 353 base::Bind(&EmptyDeleteCallback))); |
282 return; | 354 return; |
283 } | 355 } |
284 DeletePageByBookmarkId(node->id(), base::Bind(&EmptyDeleteCallback)); | 356 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); |
357 } | |
358 | |
359 void OfflinePageModel::BookmarkNodeRenumbered(bookmarks::BookmarkModel* model, | |
360 int64 old_bookmark_id, | |
361 int64 new_bookmark_id) { | |
362 // The old bookmark that is marked for deletion should still exist. | |
363 auto old_page_item_iter = offline_pages_.find(old_bookmark_id); | |
364 if (old_page_item_iter == offline_pages_.end() || | |
365 !old_page_item_iter->second.IsMarkedForDeletion()) { | |
366 NOTREACHED(); | |
367 return; | |
368 } | |
369 | |
370 // The new bookmark should not exist in our metadata record. | |
371 if (offline_pages_.find(new_bookmark_id) != offline_pages_.end()) { | |
372 NOTREACHED(); | |
373 return; | |
374 } | |
375 | |
376 // Write the old metadata with the new bookmark ID to the store. | |
377 OfflinePageItem new_page_item = old_page_item_iter->second; | |
378 new_page_item.bookmark_id = new_bookmark_id; | |
379 new_page_item.ClearMarkForDeletion(); | |
380 store_->AddOrUpdateOfflinePage( | |
381 new_page_item, | |
382 base::Bind(&OfflinePageModel::OnUndoOfflinePageDone, | |
383 weak_ptr_factory_.GetWeakPtr(), new_page_item, | |
384 old_bookmark_id)); | |
285 } | 385 } |
286 | 386 |
287 void OfflinePageModel::OnLoadDone( | 387 void OfflinePageModel::OnLoadDone( |
288 bool success, | 388 bool success, |
289 const std::vector<OfflinePageItem>& offline_pages) { | 389 const std::vector<OfflinePageItem>& offline_pages) { |
290 DCHECK(!is_loaded_); | 390 DCHECK(!is_loaded_); |
291 is_loaded_ = true; | 391 is_loaded_ = true; |
292 | 392 |
293 if (success) { | 393 if (success) { |
294 for (const auto& offline_page : offline_pages) | 394 for (const auto& offline_page : offline_pages) |
295 offline_pages_[offline_page.bookmark_id] = offline_page; | 395 offline_pages_[offline_page.bookmark_id] = offline_page; |
296 } | 396 } |
297 | 397 |
298 // Run all the delayed tasks. | 398 // Run all the delayed tasks. |
299 for (const auto& delayed_task : delayed_tasks_) | 399 for (const auto& delayed_task : delayed_tasks_) |
300 delayed_task.Run(); | 400 delayed_task.Run(); |
301 delayed_tasks_.clear(); | 401 delayed_tasks_.clear(); |
302 | 402 |
403 // If there're left over pages that are marked for deletion, but not yet | |
fgorski
2015/09/25 20:30:50
there are pages
jianli
2015/09/25 20:52:36
Done.
| |
404 // deleted and Chrome restarts. Do it now. | |
fgorski
2015/09/25 20:30:50
and service is restarted, delete the pages now.
jianli
2015/09/25 20:52:35
Done.
| |
405 FinalizePageDeletion(); | |
406 | |
303 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 407 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
304 } | 408 } |
305 | 409 |
306 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 410 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, |
307 SavePageResult result) { | 411 SavePageResult result) { |
308 UMA_HISTOGRAM_ENUMERATION( | 412 UMA_HISTOGRAM_ENUMERATION( |
309 "OfflinePages.SavePageResult", | 413 "OfflinePages.SavePageResult", |
310 static_cast<int>(result), | 414 static_cast<int>(result), |
311 static_cast<int>(SavePageResult::RESULT_COUNT)); | 415 static_cast<int>(SavePageResult::RESULT_COUNT)); |
312 callback.Run(result); | 416 callback.Run(result); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS | 462 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS |
359 : DeletePageResult::STORE_FAILURE); | 463 : DeletePageResult::STORE_FAILURE); |
360 } | 464 } |
361 | 465 |
362 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 466 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, |
363 DeletePageResult result) { | 467 DeletePageResult result) { |
364 UMA_HISTOGRAM_ENUMERATION( | 468 UMA_HISTOGRAM_ENUMERATION( |
365 "OfflinePages.DeletePageResult", | 469 "OfflinePages.DeletePageResult", |
366 static_cast<int>(result), | 470 static_cast<int>(result), |
367 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 471 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
368 callback.Run(result); | 472 if (!callback.is_null()) |
473 callback.Run(result); | |
369 } | 474 } |
370 | 475 |
371 } // namespace offline_pages | 476 } // namespace offline_pages |
OLD | NEW |