| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/sync_file_system/local/local_file_sync_context.h" | 5 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 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/memory/ptr_util.h" |
| 12 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 14 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
| 15 #include "chrome/browser/sync_file_system/file_change.h" | 16 #include "chrome/browser/sync_file_system/file_change.h" |
| 16 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" | 17 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" |
| 17 #include "chrome/browser/sync_file_system/local/local_origin_change_observer.h" | 18 #include "chrome/browser/sync_file_system/local/local_origin_change_observer.h" |
| 18 #include "chrome/browser/sync_file_system/local/root_delete_helper.h" | 19 #include "chrome/browser/sync_file_system/local/root_delete_helper.h" |
| 19 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" | 20 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" |
| 20 #include "chrome/browser/sync_file_system/local/syncable_file_operation_runner.h
" | 21 #include "chrome/browser/sync_file_system/local/syncable_file_operation_runner.h
" |
| 21 #include "chrome/browser/sync_file_system/logger.h" | 22 #include "chrome/browser/sync_file_system/logger.h" |
| (...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 return; | 635 return; |
| 635 } | 636 } |
| 636 DCHECK(file_system_context); | 637 DCHECK(file_system_context); |
| 637 SyncFileSystemBackend* backend = | 638 SyncFileSystemBackend* backend = |
| 638 SyncFileSystemBackend::GetBackend(file_system_context); | 639 SyncFileSystemBackend::GetBackend(file_system_context); |
| 639 DCHECK(backend); | 640 DCHECK(backend); |
| 640 if (!backend->change_tracker()) { | 641 if (!backend->change_tracker()) { |
| 641 // Create and initialize LocalFileChangeTracker and call back this method | 642 // Create and initialize LocalFileChangeTracker and call back this method |
| 642 // later again. | 643 // later again. |
| 643 std::set<GURL>* origins_with_changes = new std::set<GURL>; | 644 std::set<GURL>* origins_with_changes = new std::set<GURL>; |
| 644 scoped_ptr<LocalFileChangeTracker>* tracker_ptr( | 645 std::unique_ptr<LocalFileChangeTracker>* tracker_ptr( |
| 645 new scoped_ptr<LocalFileChangeTracker>); | 646 new std::unique_ptr<LocalFileChangeTracker>); |
| 646 base::PostTaskAndReplyWithResult( | 647 base::PostTaskAndReplyWithResult( |
| 647 file_system_context->default_file_task_runner(), FROM_HERE, | 648 file_system_context->default_file_task_runner(), FROM_HERE, |
| 648 base::Bind(&LocalFileSyncContext::InitializeChangeTrackerOnFileThread, | 649 base::Bind(&LocalFileSyncContext::InitializeChangeTrackerOnFileThread, |
| 649 this, tracker_ptr, base::RetainedRef(file_system_context), | 650 this, tracker_ptr, base::RetainedRef(file_system_context), |
| 650 origins_with_changes), | 651 origins_with_changes), |
| 651 base::Bind(&LocalFileSyncContext::DidInitializeChangeTrackerOnIOThread, | 652 base::Bind(&LocalFileSyncContext::DidInitializeChangeTrackerOnIOThread, |
| 652 this, base::Owned(tracker_ptr), source_url, | 653 this, base::Owned(tracker_ptr), source_url, |
| 653 base::RetainedRef(file_system_context), | 654 base::RetainedRef(file_system_context), |
| 654 base::Owned(origins_with_changes))); | 655 base::Owned(origins_with_changes))); |
| 655 return; | 656 return; |
| 656 } | 657 } |
| 657 if (!operation_runner_) { | 658 if (!operation_runner_) { |
| 658 DCHECK(!sync_status_); | 659 DCHECK(!sync_status_); |
| 659 DCHECK(!timer_on_io_); | 660 DCHECK(!timer_on_io_); |
| 660 sync_status_.reset(new LocalFileSyncStatus); | 661 sync_status_.reset(new LocalFileSyncStatus); |
| 661 timer_on_io_.reset(new base::OneShotTimer); | 662 timer_on_io_.reset(new base::OneShotTimer); |
| 662 operation_runner_.reset(new SyncableFileOperationRunner( | 663 operation_runner_.reset(new SyncableFileOperationRunner( |
| 663 kMaxConcurrentSyncableOperation, | 664 kMaxConcurrentSyncableOperation, |
| 664 sync_status_.get())); | 665 sync_status_.get())); |
| 665 sync_status_->AddObserver(this); | 666 sync_status_->AddObserver(this); |
| 666 } | 667 } |
| 667 backend->set_sync_context(this); | 668 backend->set_sync_context(this); |
| 668 DidInitialize(source_url, file_system_context, | 669 DidInitialize(source_url, file_system_context, |
| 669 SYNC_STATUS_OK); | 670 SYNC_STATUS_OK); |
| 670 } | 671 } |
| 671 | 672 |
| 672 SyncStatusCode LocalFileSyncContext::InitializeChangeTrackerOnFileThread( | 673 SyncStatusCode LocalFileSyncContext::InitializeChangeTrackerOnFileThread( |
| 673 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, | 674 std::unique_ptr<LocalFileChangeTracker>* tracker_ptr, |
| 674 FileSystemContext* file_system_context, | 675 FileSystemContext* file_system_context, |
| 675 std::set<GURL>* origins_with_changes) { | 676 std::set<GURL>* origins_with_changes) { |
| 676 DCHECK(file_system_context); | 677 DCHECK(file_system_context); |
| 677 DCHECK(tracker_ptr); | 678 DCHECK(tracker_ptr); |
| 678 DCHECK(origins_with_changes); | 679 DCHECK(origins_with_changes); |
| 679 tracker_ptr->reset(new LocalFileChangeTracker( | 680 tracker_ptr->reset(new LocalFileChangeTracker( |
| 680 file_system_context->partition_path(), | 681 file_system_context->partition_path(), |
| 681 env_override_, | 682 env_override_, |
| 682 file_system_context->default_file_task_runner())); | 683 file_system_context->default_file_task_runner())); |
| 683 const SyncStatusCode status = (*tracker_ptr)->Initialize(file_system_context); | 684 const SyncStatusCode status = (*tracker_ptr)->Initialize(file_system_context); |
| 684 if (status != SYNC_STATUS_OK) | 685 if (status != SYNC_STATUS_OK) |
| 685 return status; | 686 return status; |
| 686 | 687 |
| 687 // Get all origins that have pending changes. | 688 // Get all origins that have pending changes. |
| 688 FileSystemURLQueue urls; | 689 FileSystemURLQueue urls; |
| 689 (*tracker_ptr)->GetNextChangedURLs(&urls, 0); | 690 (*tracker_ptr)->GetNextChangedURLs(&urls, 0); |
| 690 for (FileSystemURLQueue::iterator iter = urls.begin(); | 691 for (FileSystemURLQueue::iterator iter = urls.begin(); |
| 691 iter != urls.end(); ++iter) { | 692 iter != urls.end(); ++iter) { |
| 692 origins_with_changes->insert(iter->origin()); | 693 origins_with_changes->insert(iter->origin()); |
| 693 } | 694 } |
| 694 | 695 |
| 695 // Creates snapshot directory. | 696 // Creates snapshot directory. |
| 696 base::CreateDirectory(local_base_path_.Append(kSnapshotDir)); | 697 base::CreateDirectory(local_base_path_.Append(kSnapshotDir)); |
| 697 | 698 |
| 698 return status; | 699 return status; |
| 699 } | 700 } |
| 700 | 701 |
| 701 void LocalFileSyncContext::DidInitializeChangeTrackerOnIOThread( | 702 void LocalFileSyncContext::DidInitializeChangeTrackerOnIOThread( |
| 702 scoped_ptr<LocalFileChangeTracker>* tracker_ptr, | 703 std::unique_ptr<LocalFileChangeTracker>* tracker_ptr, |
| 703 const GURL& source_url, | 704 const GURL& source_url, |
| 704 FileSystemContext* file_system_context, | 705 FileSystemContext* file_system_context, |
| 705 std::set<GURL>* origins_with_changes, | 706 std::set<GURL>* origins_with_changes, |
| 706 SyncStatusCode status) { | 707 SyncStatusCode status) { |
| 707 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 708 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 708 DCHECK(file_system_context); | 709 DCHECK(file_system_context); |
| 709 DCHECK(origins_with_changes); | 710 DCHECK(origins_with_changes); |
| 710 if (shutdown_on_io_) | 711 if (shutdown_on_io_) |
| 711 status = SYNC_STATUS_ABORT; | 712 status = SYNC_STATUS_ABORT; |
| 712 if (status != SYNC_STATUS_OK) { | 713 if (status != SYNC_STATUS_OK) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 | 753 |
| 753 StatusCallbackQueue& callback_queue = | 754 StatusCallbackQueue& callback_queue = |
| 754 pending_initialize_callbacks_[file_system_context]; | 755 pending_initialize_callbacks_[file_system_context]; |
| 755 for (StatusCallbackQueue::iterator iter = callback_queue.begin(); | 756 for (StatusCallbackQueue::iterator iter = callback_queue.begin(); |
| 756 iter != callback_queue.end(); ++iter) { | 757 iter != callback_queue.end(); ++iter) { |
| 757 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status)); | 758 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status)); |
| 758 } | 759 } |
| 759 pending_initialize_callbacks_.erase(file_system_context); | 760 pending_initialize_callbacks_.erase(file_system_context); |
| 760 } | 761 } |
| 761 | 762 |
| 762 scoped_ptr<LocalFileSyncContext::FileSystemURLQueue> | 763 std::unique_ptr<LocalFileSyncContext::FileSystemURLQueue> |
| 763 LocalFileSyncContext::GetNextURLsForSyncOnFileThread( | 764 LocalFileSyncContext::GetNextURLsForSyncOnFileThread( |
| 764 FileSystemContext* file_system_context) { | 765 FileSystemContext* file_system_context) { |
| 765 DCHECK(file_system_context); | 766 DCHECK(file_system_context); |
| 766 DCHECK(file_system_context->default_file_task_runner()-> | 767 DCHECK(file_system_context->default_file_task_runner()-> |
| 767 RunsTasksOnCurrentThread()); | 768 RunsTasksOnCurrentThread()); |
| 768 SyncFileSystemBackend* backend = | 769 SyncFileSystemBackend* backend = |
| 769 SyncFileSystemBackend::GetBackend(file_system_context); | 770 SyncFileSystemBackend::GetBackend(file_system_context); |
| 770 DCHECK(backend); | 771 DCHECK(backend); |
| 771 DCHECK(backend->change_tracker()); | 772 DCHECK(backend->change_tracker()); |
| 772 scoped_ptr<FileSystemURLQueue> urls(new FileSystemURLQueue); | 773 std::unique_ptr<FileSystemURLQueue> urls(new FileSystemURLQueue); |
| 773 backend->change_tracker()->GetNextChangedURLs( | 774 backend->change_tracker()->GetNextChangedURLs( |
| 774 urls.get(), kMaxURLsToFetchForLocalSync); | 775 urls.get(), kMaxURLsToFetchForLocalSync); |
| 775 for (FileSystemURLQueue::iterator iter = urls->begin(); | 776 for (FileSystemURLQueue::iterator iter = urls->begin(); |
| 776 iter != urls->end(); ++iter) | 777 iter != urls->end(); ++iter) |
| 777 backend->change_tracker()->DemoteChangesForURL(*iter); | 778 backend->change_tracker()->DemoteChangesForURL(*iter); |
| 778 | 779 |
| 779 return urls; | 780 return urls; |
| 780 } | 781 } |
| 781 | 782 |
| 782 void LocalFileSyncContext::TryPrepareForLocalSync( | 783 void LocalFileSyncContext::TryPrepareForLocalSync( |
| 783 FileSystemContext* file_system_context, | 784 FileSystemContext* file_system_context, |
| 784 const LocalFileSyncInfoCallback& callback, | 785 const LocalFileSyncInfoCallback& callback, |
| 785 scoped_ptr<FileSystemURLQueue> urls) { | 786 std::unique_ptr<FileSystemURLQueue> urls) { |
| 786 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 787 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 787 DCHECK(urls); | 788 DCHECK(urls); |
| 788 | 789 |
| 789 if (shutdown_on_ui_) { | 790 if (shutdown_on_ui_) { |
| 790 callback.Run(SYNC_STATUS_ABORT, LocalFileSyncInfo(), storage::ScopedFile()); | 791 callback.Run(SYNC_STATUS_ABORT, LocalFileSyncInfo(), storage::ScopedFile()); |
| 791 return; | 792 return; |
| 792 } | 793 } |
| 793 | 794 |
| 794 if (urls->empty()) { | 795 if (urls->empty()) { |
| 795 callback.Run(SYNC_STATUS_NO_CHANGE_TO_SYNC, | 796 callback.Run(SYNC_STATUS_NO_CHANGE_TO_SYNC, |
| 796 LocalFileSyncInfo(), | 797 LocalFileSyncInfo(), |
| 797 storage::ScopedFile()); | 798 storage::ScopedFile()); |
| 798 return; | 799 return; |
| 799 } | 800 } |
| 800 | 801 |
| 801 const FileSystemURL url = urls->front(); | 802 const FileSystemURL url = urls->front(); |
| 802 urls->pop_front(); | 803 urls->pop_front(); |
| 803 | 804 |
| 804 PrepareForSync(file_system_context, url, SYNC_SNAPSHOT, | 805 PrepareForSync(file_system_context, url, SYNC_SNAPSHOT, |
| 805 base::Bind(&LocalFileSyncContext::DidTryPrepareForLocalSync, | 806 base::Bind(&LocalFileSyncContext::DidTryPrepareForLocalSync, |
| 806 this, base::RetainedRef(file_system_context), | 807 this, base::RetainedRef(file_system_context), |
| 807 base::Passed(&urls), callback)); | 808 base::Passed(&urls), callback)); |
| 808 } | 809 } |
| 809 | 810 |
| 810 void LocalFileSyncContext::DidTryPrepareForLocalSync( | 811 void LocalFileSyncContext::DidTryPrepareForLocalSync( |
| 811 FileSystemContext* file_system_context, | 812 FileSystemContext* file_system_context, |
| 812 scoped_ptr<FileSystemURLQueue> remaining_urls, | 813 std::unique_ptr<FileSystemURLQueue> remaining_urls, |
| 813 const LocalFileSyncInfoCallback& callback, | 814 const LocalFileSyncInfoCallback& callback, |
| 814 SyncStatusCode status, | 815 SyncStatusCode status, |
| 815 const LocalFileSyncInfo& sync_file_info, | 816 const LocalFileSyncInfo& sync_file_info, |
| 816 storage::ScopedFile snapshot) { | 817 storage::ScopedFile snapshot) { |
| 817 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 818 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 818 if (status != SYNC_STATUS_FILE_BUSY) { | 819 if (status != SYNC_STATUS_FILE_BUSY) { |
| 819 PromoteDemotedChangesForURLs(file_system_context, | 820 PromoteDemotedChangesForURLs(file_system_context, |
| 820 std::move(remaining_urls)); | 821 std::move(remaining_urls)); |
| 821 callback.Run(status, sync_file_info, std::move(snapshot)); | 822 callback.Run(status, sync_file_info, std::move(snapshot)); |
| 822 return; | 823 return; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 847 | 848 |
| 848 SyncFileSystemBackend* backend = | 849 SyncFileSystemBackend* backend = |
| 849 SyncFileSystemBackend::GetBackend(file_system_context); | 850 SyncFileSystemBackend::GetBackend(file_system_context); |
| 850 DCHECK(backend); | 851 DCHECK(backend); |
| 851 DCHECK(backend->change_tracker()); | 852 DCHECK(backend->change_tracker()); |
| 852 backend->change_tracker()->PromoteDemotedChangesForURL(url); | 853 backend->change_tracker()->PromoteDemotedChangesForURL(url); |
| 853 } | 854 } |
| 854 | 855 |
| 855 void LocalFileSyncContext::PromoteDemotedChangesForURLs( | 856 void LocalFileSyncContext::PromoteDemotedChangesForURLs( |
| 856 FileSystemContext* file_system_context, | 857 FileSystemContext* file_system_context, |
| 857 scoped_ptr<FileSystemURLQueue> urls) { | 858 std::unique_ptr<FileSystemURLQueue> urls) { |
| 858 DCHECK(file_system_context); | 859 DCHECK(file_system_context); |
| 859 if (!file_system_context->default_file_task_runner()-> | 860 if (!file_system_context->default_file_task_runner()-> |
| 860 RunsTasksOnCurrentThread()) { | 861 RunsTasksOnCurrentThread()) { |
| 861 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 862 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 862 if (shutdown_on_ui_) | 863 if (shutdown_on_ui_) |
| 863 return; | 864 return; |
| 864 file_system_context->default_file_task_runner()->PostTask( | 865 file_system_context->default_file_task_runner()->PostTask( |
| 865 FROM_HERE, | 866 FROM_HERE, |
| 866 base::Bind(&LocalFileSyncContext::PromoteDemotedChangesForURLs, this, | 867 base::Bind(&LocalFileSyncContext::PromoteDemotedChangesForURLs, this, |
| 867 base::RetainedRef(file_system_context), | 868 base::RetainedRef(file_system_context), |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 904 FileChangeList changes; | 905 FileChangeList changes; |
| 905 backend->change_tracker()->GetChangesForURL(url, &changes); | 906 backend->change_tracker()->GetChangesForURL(url, &changes); |
| 906 | 907 |
| 907 base::FilePath platform_path; | 908 base::FilePath platform_path; |
| 908 base::File::Info file_info; | 909 base::File::Info file_info; |
| 909 FileSystemFileUtil* file_util = | 910 FileSystemFileUtil* file_util = |
| 910 file_system_context->sandbox_delegate()->sync_file_util(); | 911 file_system_context->sandbox_delegate()->sync_file_util(); |
| 911 DCHECK(file_util); | 912 DCHECK(file_util); |
| 912 | 913 |
| 913 base::File::Error file_error = file_util->GetFileInfo( | 914 base::File::Error file_error = file_util->GetFileInfo( |
| 914 make_scoped_ptr( | 915 base::WrapUnique(new FileSystemOperationContext(file_system_context)) |
| 915 new FileSystemOperationContext(file_system_context)).get(), | 916 .get(), |
| 916 url, | 917 url, &file_info, &platform_path); |
| 917 &file_info, | |
| 918 &platform_path); | |
| 919 | 918 |
| 920 storage::ScopedFile snapshot; | 919 storage::ScopedFile snapshot; |
| 921 if (file_error == base::File::FILE_OK && sync_mode == SYNC_SNAPSHOT) { | 920 if (file_error == base::File::FILE_OK && sync_mode == SYNC_SNAPSHOT) { |
| 922 base::FilePath snapshot_path; | 921 base::FilePath snapshot_path; |
| 923 base::CreateTemporaryFileInDir(local_base_path_.Append(kSnapshotDir), | 922 base::CreateTemporaryFileInDir(local_base_path_.Append(kSnapshotDir), |
| 924 &snapshot_path); | 923 &snapshot_path); |
| 925 if (base::CopyFile(platform_path, snapshot_path)) { | 924 if (base::CopyFile(platform_path, snapshot_path)) { |
| 926 platform_path = snapshot_path; | 925 platform_path = snapshot_path; |
| 927 snapshot = | 926 snapshot = |
| 928 storage::ScopedFile(snapshot_path, | 927 storage::ScopedFile(snapshot_path, |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 return; | 1048 return; |
| 1050 } | 1049 } |
| 1051 | 1050 |
| 1052 FileSystemURL url_for_sync = CreateSyncableFileSystemURLForSync( | 1051 FileSystemURL url_for_sync = CreateSyncableFileSystemURLForSync( |
| 1053 file_system_context, dest_url); | 1052 file_system_context, dest_url); |
| 1054 file_system_context->operation_runner()->CopyInForeignFile( | 1053 file_system_context->operation_runner()->CopyInForeignFile( |
| 1055 local_path, url_for_sync, callback); | 1054 local_path, url_for_sync, callback); |
| 1056 } | 1055 } |
| 1057 | 1056 |
| 1058 } // namespace sync_file_system | 1057 } // namespace sync_file_system |
| OLD | NEW |