| 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 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 11 #include "base/location.h" | 12 #include "base/location.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 15 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "components/bookmarks/browser/bookmark_model.h" | 18 #include "components/bookmarks/browser/bookmark_model.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 DCHECK(success); | 67 DCHECK(success); |
| 67 for (const auto& file_path : paths_to_delete) { | 68 for (const auto& file_path : paths_to_delete) { |
| 68 // Make sure delete happens on the left of || so that it is always executed. | 69 // Make sure delete happens on the left of || so that it is always executed. |
| 69 *success = base::DeleteFile(file_path, false) || *success; | 70 *success = base::DeleteFile(file_path, false) || *success; |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 | 73 |
| 73 void EmptyDeleteCallback(OfflinePageModel::DeletePageResult /* result */) { | 74 void EmptyDeleteCallback(OfflinePageModel::DeletePageResult /* result */) { |
| 74 } | 75 } |
| 75 | 76 |
| 77 void FindPagesMissingArchiveFile( |
| 78 const std::vector<std::pair<int64, base::FilePath>>& id_path_pairs, |
| 79 std::vector<int64>* ids_of_pages_missing_archive_file) { |
| 80 DCHECK(ids_of_pages_missing_archive_file); |
| 81 |
| 82 for (const auto& id_path : id_path_pairs) { |
| 83 if (!base::PathExists(id_path.second) || |
| 84 base::DirectoryExists(id_path.second)) { |
| 85 ids_of_pages_missing_archive_file->push_back(id_path.first); |
| 86 } |
| 87 } |
| 88 } |
| 89 |
| 76 } // namespace | 90 } // namespace |
| 77 | 91 |
| 78 OfflinePageModel::OfflinePageModel( | 92 OfflinePageModel::OfflinePageModel( |
| 79 scoped_ptr<OfflinePageMetadataStore> store, | 93 scoped_ptr<OfflinePageMetadataStore> store, |
| 80 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 94 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
| 81 : store_(store.Pass()), | 95 : store_(store.Pass()), |
| 82 is_loaded_(false), | 96 is_loaded_(false), |
| 83 task_runner_(task_runner), | 97 task_runner_(task_runner), |
| 84 scoped_observer_(this), | 98 scoped_observer_(this), |
| 85 weak_ptr_factory_(this) { | 99 weak_ptr_factory_(this) { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 const GURL& offline_url) const { | 240 const GURL& offline_url) const { |
| 227 for (auto iter = offline_pages_.begin(); | 241 for (auto iter = offline_pages_.begin(); |
| 228 iter != offline_pages_.end(); | 242 iter != offline_pages_.end(); |
| 229 ++iter) { | 243 ++iter) { |
| 230 if (iter->second.GetOfflineURL() == offline_url) | 244 if (iter->second.GetOfflineURL() == offline_url) |
| 231 return &(iter->second); | 245 return &(iter->second); |
| 232 } | 246 } |
| 233 return nullptr; | 247 return nullptr; |
| 234 } | 248 } |
| 235 | 249 |
| 250 void OfflinePageModel::CheckForExternalFileDeletion() { |
| 251 DCHECK(is_loaded_); |
| 252 |
| 253 std::vector<std::pair<int64, base::FilePath>> id_path_pairs; |
| 254 for (const auto& id_page_pair : offline_pages_) { |
| 255 id_path_pairs.push_back( |
| 256 std::make_pair(id_page_pair.first, id_page_pair.second.file_path)); |
| 257 } |
| 258 |
| 259 std::vector<int64>* ids_of_pages_missing_archive_file = |
| 260 new std::vector<int64>(); |
| 261 task_runner_->PostTaskAndReply( |
| 262 FROM_HERE, base::Bind(&FindPagesMissingArchiveFile, id_path_pairs, |
| 263 ids_of_pages_missing_archive_file), |
| 264 base::Bind(&OfflinePageModel::OnFindPagesMissingArchiveFile, |
| 265 weak_ptr_factory_.GetWeakPtr(), |
| 266 base::Owned(ids_of_pages_missing_archive_file))); |
| 267 } |
| 268 |
| 236 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { | 269 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { |
| 237 return store_.get(); | 270 return store_.get(); |
| 238 } | 271 } |
| 239 | 272 |
| 240 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, | 273 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, |
| 241 int64 bookmark_id, | 274 int64 bookmark_id, |
| 242 const SavePageCallback& callback, | 275 const SavePageCallback& callback, |
| 243 OfflinePageArchiver* archiver, | 276 OfflinePageArchiver* archiver, |
| 244 ArchiverResult archiver_result, | 277 ArchiverResult archiver_result, |
| 245 const GURL& url, | 278 const GURL& url, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 if (!success) | 346 if (!success) |
| 314 return; | 347 return; |
| 315 | 348 |
| 316 // Schedule to do the final deletion. | 349 // Schedule to do the final deletion. |
| 317 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 350 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 318 FROM_HERE, | 351 FROM_HERE, |
| 319 base::Bind(&OfflinePageModel::FinalizePageDeletion, | 352 base::Bind(&OfflinePageModel::FinalizePageDeletion, |
| 320 weak_ptr_factory_.GetWeakPtr()), | 353 weak_ptr_factory_.GetWeakPtr()), |
| 321 kFinalDeletionDelay); | 354 kFinalDeletionDelay); |
| 322 | 355 |
| 323 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 356 FOR_EACH_OBSERVER( |
| 357 Observer, observers_, OfflinePageDeleted(offline_page_item.bookmark_id)); |
| 324 } | 358 } |
| 325 | 359 |
| 326 void OfflinePageModel::OnUndoOfflinePageDone( | 360 void OfflinePageModel::OnUndoOfflinePageDone( |
| 327 const OfflinePageItem& offline_page, bool success) { | 361 const OfflinePageItem& offline_page, bool success) { |
| 328 if (!success) | 362 if (!success) |
| 329 return; | 363 return; |
| 330 offline_pages_[offline_page.bookmark_id] = offline_page; | 364 offline_pages_[offline_page.bookmark_id] = offline_page; |
| 331 | 365 |
| 332 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 366 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
| 333 } | 367 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 // Run all the delayed tasks. | 437 // Run all the delayed tasks. |
| 404 for (const auto& delayed_task : delayed_tasks_) | 438 for (const auto& delayed_task : delayed_tasks_) |
| 405 delayed_task.Run(); | 439 delayed_task.Run(); |
| 406 delayed_tasks_.clear(); | 440 delayed_tasks_.clear(); |
| 407 | 441 |
| 408 // If there are pages that are marked for deletion, but not yet deleted and | 442 // If there are pages that are marked for deletion, but not yet deleted and |
| 409 // OfflinePageModel gets reloaded. Delete the pages now. | 443 // OfflinePageModel gets reloaded. Delete the pages now. |
| 410 FinalizePageDeletion(); | 444 FinalizePageDeletion(); |
| 411 | 445 |
| 412 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); | 446 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelLoaded(this)); |
| 447 |
| 448 CheckForExternalFileDeletion(); |
| 413 } | 449 } |
| 414 | 450 |
| 415 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, | 451 void OfflinePageModel::InformSavePageDone(const SavePageCallback& callback, |
| 416 SavePageResult result) { | 452 SavePageResult result) { |
| 417 UMA_HISTOGRAM_ENUMERATION( | 453 UMA_HISTOGRAM_ENUMERATION( |
| 418 "OfflinePages.SavePageResult", | 454 "OfflinePages.SavePageResult", |
| 419 static_cast<int>(result), | 455 static_cast<int>(result), |
| 420 static_cast<int>(SavePageResult::RESULT_COUNT)); | 456 static_cast<int>(SavePageResult::RESULT_COUNT)); |
| 421 callback.Run(result); | 457 callback.Run(result); |
| 422 } | 458 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 507 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, |
| 472 DeletePageResult result) { | 508 DeletePageResult result) { |
| 473 UMA_HISTOGRAM_ENUMERATION( | 509 UMA_HISTOGRAM_ENUMERATION( |
| 474 "OfflinePages.DeletePageResult", | 510 "OfflinePages.DeletePageResult", |
| 475 static_cast<int>(result), | 511 static_cast<int>(result), |
| 476 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 512 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
| 477 if (!callback.is_null()) | 513 if (!callback.is_null()) |
| 478 callback.Run(result); | 514 callback.Run(result); |
| 479 } | 515 } |
| 480 | 516 |
| 517 void OfflinePageModel::OnFindPagesMissingArchiveFile( |
| 518 const std::vector<int64>* ids_of_pages_missing_archive_file) { |
| 519 DCHECK(ids_of_pages_missing_archive_file); |
| 520 if (ids_of_pages_missing_archive_file->empty()) |
| 521 return; |
| 522 |
| 523 DeletePageCallback done_callback( |
| 524 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone, |
| 525 weak_ptr_factory_.GetWeakPtr(), |
| 526 *ids_of_pages_missing_archive_file)); |
| 527 |
| 528 store_->RemoveOfflinePages( |
| 529 *ids_of_pages_missing_archive_file, |
| 530 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, |
| 531 weak_ptr_factory_.GetWeakPtr(), |
| 532 *ids_of_pages_missing_archive_file, |
| 533 done_callback)); |
| 534 } |
| 535 |
| 536 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( |
| 537 const std::vector<int64>& bookmark_ids, |
| 538 OfflinePageModel::DeletePageResult /* result */) { |
| 539 for (int64 bookmark_id : bookmark_ids) { |
| 540 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); |
| 541 } |
| 542 } |
| 543 |
| 481 } // namespace offline_pages | 544 } // namespace offline_pages |
| OLD | NEW |