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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 | 491 |
492 // Fire the callback on UI thread. | 492 // Fire the callback on UI thread. |
493 ui_task_runner_->PostTask(FROM_HERE, | 493 ui_task_runner_->PostTask(FROM_HERE, |
494 base::Bind(callback, | 494 base::Bind(callback, |
495 SYNC_STATUS_OK, | 495 SYNC_STATUS_OK, |
496 !changes.empty())); | 496 !changes.empty())); |
497 } | 497 } |
498 | 498 |
499 void LocalFileSyncContext::PromoteDemotedChanges( | 499 void LocalFileSyncContext::PromoteDemotedChanges( |
500 const GURL& origin, | 500 const GURL& origin, |
501 fileapi::FileSystemContext* file_system_context) { | 501 fileapi::FileSystemContext* file_system_context, |
| 502 const base::Closure& callback) { |
502 // This is initially called on UI thread and to be relayed to FILE thread. | 503 // This is initially called on UI thread and to be relayed to FILE thread. |
503 DCHECK(file_system_context); | 504 DCHECK(file_system_context); |
504 if (!file_system_context->default_file_task_runner()-> | 505 if (!file_system_context->default_file_task_runner()-> |
505 RunsTasksOnCurrentThread()) { | 506 RunsTasksOnCurrentThread()) { |
506 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 507 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
507 file_system_context->default_file_task_runner()->PostTask( | 508 file_system_context->default_file_task_runner()->PostTask( |
508 FROM_HERE, | 509 FROM_HERE, |
509 base::Bind(&LocalFileSyncContext::PromoteDemotedChanges, | 510 base::Bind(&LocalFileSyncContext::PromoteDemotedChanges, |
510 this, origin, make_scoped_refptr(file_system_context))); | 511 this, origin, make_scoped_refptr(file_system_context), |
| 512 callback)); |
511 return; | 513 return; |
512 } | 514 } |
513 | 515 |
514 SyncFileSystemBackend* backend = | 516 SyncFileSystemBackend* backend = |
515 SyncFileSystemBackend::GetBackend(file_system_context); | 517 SyncFileSystemBackend::GetBackend(file_system_context); |
516 DCHECK(backend); | 518 DCHECK(backend); |
517 DCHECK(backend->change_tracker()); | 519 DCHECK(backend->change_tracker()); |
518 if (!backend->change_tracker()->PromoteDemotedChanges()) | 520 if (!backend->change_tracker()->PromoteDemotedChanges()) { |
| 521 ui_task_runner_->PostTask(FROM_HERE, callback); |
519 return; | 522 return; |
| 523 } |
520 | 524 |
521 io_task_runner_->PostTask( | 525 io_task_runner_->PostTask( |
522 FROM_HERE, | 526 FROM_HERE, |
523 base::Bind(&LocalFileSyncContext::UpdateChangesForOrigin, | 527 base::Bind(&LocalFileSyncContext::UpdateChangesForOrigin, |
524 this, origin)); | 528 this, origin, callback)); |
525 } | 529 } |
526 | 530 |
527 void LocalFileSyncContext::UpdateChangesForOrigin(const GURL& origin) { | 531 void LocalFileSyncContext::UpdateChangesForOrigin( |
| 532 const GURL& origin, |
| 533 const base::Closure& callback) { |
528 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 534 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
529 if (shutdown_on_io_) | 535 if (shutdown_on_io_) |
530 return; | 536 return; |
531 origins_with_pending_changes_.insert(origin); | 537 origins_with_pending_changes_.insert(origin); |
532 ScheduleNotifyChangesUpdatedOnIOThread(); | 538 ScheduleNotifyChangesUpdatedOnIOThread(callback); |
533 } | 539 } |
534 | 540 |
535 void LocalFileSyncContext::AddOriginChangeObserver( | 541 void LocalFileSyncContext::AddOriginChangeObserver( |
536 LocalOriginChangeObserver* observer) { | 542 LocalOriginChangeObserver* observer) { |
537 origin_change_observers_.AddObserver(observer); | 543 origin_change_observers_.AddObserver(observer); |
538 } | 544 } |
539 | 545 |
540 void LocalFileSyncContext::RemoveOriginChangeObserver( | 546 void LocalFileSyncContext::RemoveOriginChangeObserver( |
541 LocalOriginChangeObserver* observer) { | 547 LocalOriginChangeObserver* observer) { |
542 origin_change_observers_.RemoveObserver(observer); | 548 origin_change_observers_.RemoveObserver(observer); |
543 } | 549 } |
544 | 550 |
545 base::WeakPtr<SyncableFileOperationRunner> | 551 base::WeakPtr<SyncableFileOperationRunner> |
546 LocalFileSyncContext::operation_runner() const { | 552 LocalFileSyncContext::operation_runner() const { |
547 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 553 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
548 if (operation_runner_) | 554 if (operation_runner_) |
549 return operation_runner_->AsWeakPtr(); | 555 return operation_runner_->AsWeakPtr(); |
550 return base::WeakPtr<SyncableFileOperationRunner>(); | 556 return base::WeakPtr<SyncableFileOperationRunner>(); |
551 } | 557 } |
552 | 558 |
553 LocalFileSyncStatus* LocalFileSyncContext::sync_status() const { | 559 LocalFileSyncStatus* LocalFileSyncContext::sync_status() const { |
554 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 560 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
555 return sync_status_.get(); | 561 return sync_status_.get(); |
556 } | 562 } |
557 | 563 |
558 void LocalFileSyncContext::OnSyncEnabled(const FileSystemURL& url) { | 564 void LocalFileSyncContext::OnSyncEnabled(const FileSystemURL& url) { |
559 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 565 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
560 if (shutdown_on_io_) | 566 if (shutdown_on_io_) |
561 return; | 567 return; |
562 UpdateChangesForOrigin(url.origin()); | 568 UpdateChangesForOrigin(url.origin(), NoopClosure()); |
563 if (url_syncable_callback_.is_null() || | 569 if (url_syncable_callback_.is_null() || |
564 sync_status()->IsWriting(url_waiting_sync_on_io_)) { | 570 sync_status()->IsWriting(url_waiting_sync_on_io_)) { |
565 return; | 571 return; |
566 } | 572 } |
567 // TODO(kinuko): may want to check how many pending tasks we have. | 573 // TODO(kinuko): may want to check how many pending tasks we have. |
568 ui_task_runner_->PostTask(FROM_HERE, url_syncable_callback_); | 574 ui_task_runner_->PostTask(FROM_HERE, url_syncable_callback_); |
569 url_syncable_callback_.Reset(); | 575 url_syncable_callback_.Reset(); |
570 } | 576 } |
571 | 577 |
572 void LocalFileSyncContext::OnWriteEnabled(const FileSystemURL& url) { | 578 void LocalFileSyncContext::OnWriteEnabled(const FileSystemURL& url) { |
573 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 579 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
574 // Nothing to do for now. | 580 // Nothing to do for now. |
575 } | 581 } |
576 | 582 |
577 LocalFileSyncContext::~LocalFileSyncContext() { | 583 LocalFileSyncContext::~LocalFileSyncContext() { |
578 } | 584 } |
579 | 585 |
580 void LocalFileSyncContext::ScheduleNotifyChangesUpdatedOnIOThread() { | 586 void LocalFileSyncContext::ScheduleNotifyChangesUpdatedOnIOThread( |
| 587 const base::Closure& callback) { |
581 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 588 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
582 if (shutdown_on_io_) | 589 if (shutdown_on_io_) |
583 return; | 590 return; |
584 if (base::Time::Now() > last_notified_changes_ + NotifyChangesDuration()) { | 591 if (base::Time::Now() > last_notified_changes_ + NotifyChangesDuration()) { |
585 NotifyAvailableChangesOnIOThread(); | 592 NotifyAvailableChangesOnIOThread(callback); |
586 } else if (!timer_on_io_->IsRunning()) { | 593 } else if (!timer_on_io_->IsRunning()) { |
587 timer_on_io_->Start( | 594 timer_on_io_->Start( |
588 FROM_HERE, NotifyChangesDuration(), this, | 595 FROM_HERE, NotifyChangesDuration(), |
589 &LocalFileSyncContext::NotifyAvailableChangesOnIOThread); | 596 base::Bind(&LocalFileSyncContext::NotifyAvailableChangesOnIOThread, |
| 597 base::Unretained(this), callback)); |
590 } | 598 } |
591 } | 599 } |
592 | 600 |
593 void LocalFileSyncContext::NotifyAvailableChangesOnIOThread() { | 601 void LocalFileSyncContext::NotifyAvailableChangesOnIOThread( |
| 602 const base::Closure& callback) { |
594 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 603 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
595 if (shutdown_on_io_) | 604 if (shutdown_on_io_) |
596 return; | 605 return; |
597 ui_task_runner_->PostTask( | 606 ui_task_runner_->PostTask( |
598 FROM_HERE, | 607 FROM_HERE, |
599 base::Bind(&LocalFileSyncContext::NotifyAvailableChanges, | 608 base::Bind(&LocalFileSyncContext::NotifyAvailableChanges, |
600 this, origins_with_pending_changes_)); | 609 this, origins_with_pending_changes_, callback)); |
601 last_notified_changes_ = base::Time::Now(); | 610 last_notified_changes_ = base::Time::Now(); |
602 origins_with_pending_changes_.clear(); | 611 origins_with_pending_changes_.clear(); |
603 } | 612 } |
604 | 613 |
605 void LocalFileSyncContext::NotifyAvailableChanges( | 614 void LocalFileSyncContext::NotifyAvailableChanges( |
606 const std::set<GURL>& origins) { | 615 const std::set<GURL>& origins, |
| 616 const base::Closure& callback) { |
607 FOR_EACH_OBSERVER(LocalOriginChangeObserver, origin_change_observers_, | 617 FOR_EACH_OBSERVER(LocalOriginChangeObserver, origin_change_observers_, |
608 OnChangesAvailableInOrigins(origins)); | 618 OnChangesAvailableInOrigins(origins)); |
| 619 callback.Run(); |
609 } | 620 } |
610 | 621 |
611 void LocalFileSyncContext::ShutdownOnIOThread() { | 622 void LocalFileSyncContext::ShutdownOnIOThread() { |
612 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 623 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
613 shutdown_on_io_ = true; | 624 shutdown_on_io_ = true; |
614 operation_runner_.reset(); | 625 operation_runner_.reset(); |
615 root_delete_helper_.reset(); | 626 root_delete_helper_.reset(); |
616 sync_status_.reset(); | 627 sync_status_.reset(); |
617 timer_on_io_.reset(); | 628 timer_on_io_.reset(); |
618 } | 629 } |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 return; | 726 return; |
716 } | 727 } |
717 | 728 |
718 SyncFileSystemBackend* backend = | 729 SyncFileSystemBackend* backend = |
719 SyncFileSystemBackend::GetBackend(file_system_context); | 730 SyncFileSystemBackend::GetBackend(file_system_context); |
720 DCHECK(backend); | 731 DCHECK(backend); |
721 backend->SetLocalFileChangeTracker(tracker_ptr->Pass()); | 732 backend->SetLocalFileChangeTracker(tracker_ptr->Pass()); |
722 | 733 |
723 origins_with_pending_changes_.insert(origins_with_changes->begin(), | 734 origins_with_pending_changes_.insert(origins_with_changes->begin(), |
724 origins_with_changes->end()); | 735 origins_with_changes->end()); |
725 ScheduleNotifyChangesUpdatedOnIOThread(); | 736 ScheduleNotifyChangesUpdatedOnIOThread(NoopClosure()); |
726 | 737 |
727 InitializeFileSystemContextOnIOThread(source_url, file_system_context, | 738 InitializeFileSystemContextOnIOThread(source_url, file_system_context, |
728 GURL(), std::string(), | 739 GURL(), std::string(), |
729 base::File::FILE_OK); | 740 base::File::FILE_OK); |
730 } | 741 } |
731 | 742 |
732 void LocalFileSyncContext::DidInitialize( | 743 void LocalFileSyncContext::DidInitialize( |
733 const GURL& source_url, | 744 const GURL& source_url, |
734 FileSystemContext* file_system_context, | 745 FileSystemContext* file_system_context, |
735 SyncStatusCode status) { | 746 SyncStatusCode status) { |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 return; | 943 return; |
933 sync_status()->EndSyncing(url); | 944 sync_status()->EndSyncing(url); |
934 | 945 |
935 if (for_snapshot_sync) { | 946 if (for_snapshot_sync) { |
936 // The caller will hold shared lock on this one. | 947 // The caller will hold shared lock on this one. |
937 sync_status()->StartWriting(url); | 948 sync_status()->StartWriting(url); |
938 return; | 949 return; |
939 } | 950 } |
940 | 951 |
941 // Since a sync has finished the number of changes must have been updated. | 952 // Since a sync has finished the number of changes must have been updated. |
942 UpdateChangesForOrigin(url.origin()); | 953 UpdateChangesForOrigin(url.origin(), NoopClosure()); |
943 } | 954 } |
944 | 955 |
945 void LocalFileSyncContext::FinalizeSnapshotSyncOnIOThread( | 956 void LocalFileSyncContext::FinalizeSnapshotSyncOnIOThread( |
946 const FileSystemURL& url) { | 957 const FileSystemURL& url) { |
947 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 958 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
948 if (shutdown_on_io_) | 959 if (shutdown_on_io_) |
949 return; | 960 return; |
950 sync_status()->EndWriting(url); | 961 sync_status()->EndWriting(url); |
951 | 962 |
952 // Since a sync has finished the number of changes must have been updated. | 963 // Since a sync has finished the number of changes must have been updated. |
953 UpdateChangesForOrigin(url.origin()); | 964 UpdateChangesForOrigin(url.origin(), NoopClosure()); |
954 } | 965 } |
955 | 966 |
956 void LocalFileSyncContext::DidApplyRemoteChange( | 967 void LocalFileSyncContext::DidApplyRemoteChange( |
957 const FileSystemURL& url, | 968 const FileSystemURL& url, |
958 const SyncStatusCallback& callback_on_ui, | 969 const SyncStatusCallback& callback_on_ui, |
959 base::File::Error file_error) { | 970 base::File::Error file_error) { |
960 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 971 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
961 root_delete_helper_.reset(); | 972 root_delete_helper_.reset(); |
962 ui_task_runner_->PostTask( | 973 ui_task_runner_->PostTask( |
963 FROM_HERE, | 974 FROM_HERE, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
998 return; | 1009 return; |
999 } | 1010 } |
1000 | 1011 |
1001 FileSystemURL url_for_sync = CreateSyncableFileSystemURLForSync( | 1012 FileSystemURL url_for_sync = CreateSyncableFileSystemURLForSync( |
1002 file_system_context, dest_url); | 1013 file_system_context, dest_url); |
1003 file_system_context->operation_runner()->CopyInForeignFile( | 1014 file_system_context->operation_runner()->CopyInForeignFile( |
1004 local_path, url_for_sync, callback); | 1015 local_path, url_for_sync, callback); |
1005 } | 1016 } |
1006 | 1017 |
1007 } // namespace sync_file_system | 1018 } // namespace sync_file_system |
OLD | NEW |