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.last_access_time > kPageCleanUpThreshold) | 212 if (!id_page_pair.second.IsMarkedForDeletion() && |
213 now - id_page_pair.second.last_access_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, bool success) { | |
322 if (!success) | |
323 return; | |
324 offline_pages_[offline_page.bookmark_id] = offline_page; | |
fgorski
2015/10/12 20:59:52
you are missing a callback that will tell the call
jianli
2015/10/12 23:33:34
We don't need to fire a callback since it has alre
| |
325 } | |
326 | |
327 void OfflinePageModel::FinalizePageDeletion() { | |
328 std::vector<int64> bookmark_ids_pending_deletion; | |
329 for (const auto& id_page_pair : offline_pages_) { | |
330 if (!id_page_pair.second.IsMarkedForDeletion()) | |
331 continue; | |
332 bookmark_ids_pending_deletion.push_back(id_page_pair.second.bookmark_id); | |
333 } | |
334 DeletePagesByBookmarkId(bookmark_ids_pending_deletion, DeletePageCallback()); | |
335 } | |
336 | |
267 void OfflinePageModel::BookmarkModelChanged() { | 337 void OfflinePageModel::BookmarkModelChanged() { |
268 } | 338 } |
269 | 339 |
340 void OfflinePageModel::BookmarkNodeAdded(bookmarks::BookmarkModel* model, | |
341 const bookmarks::BookmarkNode* parent, | |
342 int index) { | |
343 const bookmarks::BookmarkNode* node = parent->GetChild(index); | |
344 DCHECK(node); | |
345 | |
346 auto iter = offline_pages_.find(node->id()); | |
347 if (iter == offline_pages_.end()) | |
348 return; | |
349 | |
350 OfflinePageItem offline_page_item = iter->second; | |
351 if (!offline_page_item.IsMarkedForDeletion()) | |
352 return; | |
353 | |
354 // Clear the flag to bring it back. | |
355 // Make a copy of the cached item and update it. The cached item should only | |
356 // be updated upon the successful store operation. | |
357 offline_page_item.ClearMarkForDeletion(); | |
358 store_->AddOrUpdateOfflinePage( | |
359 offline_page_item, | |
360 base::Bind(&OfflinePageModel::OnUndoOfflinePageDone, | |
361 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | |
362 } | |
363 | |
270 void OfflinePageModel::BookmarkNodeRemoved( | 364 void OfflinePageModel::BookmarkNodeRemoved( |
271 bookmarks::BookmarkModel* model, | 365 bookmarks::BookmarkModel* model, |
272 const bookmarks::BookmarkNode* parent, | 366 const bookmarks::BookmarkNode* parent, |
273 int old_index, | 367 int old_index, |
274 const bookmarks::BookmarkNode* node, | 368 const bookmarks::BookmarkNode* node, |
275 const std::set<GURL>& removed_urls) { | 369 const std::set<GURL>& removed_urls) { |
276 if (!is_loaded_) { | 370 if (!is_loaded_) { |
277 delayed_tasks_.push_back( | 371 delayed_tasks_.push_back( |
278 base::Bind(&OfflinePageModel::DeletePageByBookmarkId, | 372 base::Bind(&OfflinePageModel::MarkPageForDeletion, |
279 weak_ptr_factory_.GetWeakPtr(), | 373 weak_ptr_factory_.GetWeakPtr(), |
280 node->id(), | 374 node->id(), |
281 base::Bind(&EmptyDeleteCallback))); | 375 base::Bind(&EmptyDeleteCallback))); |
282 return; | 376 return; |
283 } | 377 } |
284 DeletePageByBookmarkId(node->id(), base::Bind(&EmptyDeleteCallback)); | 378 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); |
285 } | 379 } |
286 | 380 |
287 void OfflinePageModel::OnLoadDone( | 381 void OfflinePageModel::OnLoadDone( |
288 bool success, | 382 bool success, |
289 const std::vector<OfflinePageItem>& offline_pages) { | 383 const std::vector<OfflinePageItem>& offline_pages) { |
290 DCHECK(!is_loaded_); | 384 DCHECK(!is_loaded_); |
291 is_loaded_ = true; | 385 is_loaded_ = true; |
292 | 386 |
293 if (success) { | 387 if (success) { |
294 for (const auto& offline_page : offline_pages) | 388 for (const auto& offline_page : offline_pages) |
295 offline_pages_[offline_page.bookmark_id] = offline_page; | 389 offline_pages_[offline_page.bookmark_id] = offline_page; |
296 } | 390 } |
297 | 391 |
298 // Run all the delayed tasks. | 392 // Run all the delayed tasks. |
299 for (const auto& delayed_task : delayed_tasks_) | 393 for (const auto& delayed_task : delayed_tasks_) |
300 delayed_task.Run(); | 394 delayed_task.Run(); |
301 delayed_tasks_.clear(); | 395 delayed_tasks_.clear(); |
302 | 396 |
397 // If there are pages that are marked for deletion, but not yet deleted and | |
398 // OfflinePageModel gets reloaded. Delete the pages now. | |
399 FinalizePageDeletion(); | |
400 | |
303 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 401 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
304 } | 402 } |
305 | 403 |
306 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 404 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, |
307 SavePageResult result) { | 405 SavePageResult result) { |
308 UMA_HISTOGRAM_ENUMERATION( | 406 UMA_HISTOGRAM_ENUMERATION( |
309 "OfflinePages.SavePageResult", | 407 "OfflinePages.SavePageResult", |
310 static_cast<int>(result), | 408 static_cast<int>(result), |
311 static_cast<int>(SavePageResult::RESULT_COUNT)); | 409 static_cast<int>(SavePageResult::RESULT_COUNT)); |
312 callback.Run(result); | 410 callback.Run(result); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS | 456 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS |
359 : DeletePageResult::STORE_FAILURE); | 457 : DeletePageResult::STORE_FAILURE); |
360 } | 458 } |
361 | 459 |
362 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 460 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, |
363 DeletePageResult result) { | 461 DeletePageResult result) { |
364 UMA_HISTOGRAM_ENUMERATION( | 462 UMA_HISTOGRAM_ENUMERATION( |
365 "OfflinePages.DeletePageResult", | 463 "OfflinePages.DeletePageResult", |
366 static_cast<int>(result), | 464 static_cast<int>(result), |
367 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 465 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
368 callback.Run(result); | 466 if (!callback.is_null()) |
467 callback.Run(result); | |
369 } | 468 } |
370 | 469 |
371 } // namespace offline_pages | 470 } // namespace offline_pages |
OLD | NEW |