| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/sync_file_system_service.h" | 5 #include "chrome/browser/sync_file_system/sync_file_system_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 SyncEventObserver* observer) { | 348 SyncEventObserver* observer) { |
| 349 observers_.RemoveObserver(observer); | 349 observers_.RemoveObserver(observer); |
| 350 } | 350 } |
| 351 | 351 |
| 352 LocalChangeProcessor* SyncFileSystemService::GetLocalChangeProcessor( | 352 LocalChangeProcessor* SyncFileSystemService::GetLocalChangeProcessor( |
| 353 const GURL& origin) { | 353 const GURL& origin) { |
| 354 return GetRemoteService(origin)->GetLocalChangeProcessor(); | 354 return GetRemoteService(origin)->GetLocalChangeProcessor(); |
| 355 } | 355 } |
| 356 | 356 |
| 357 void SyncFileSystemService::OnSyncIdle() { | 357 void SyncFileSystemService::OnSyncIdle() { |
| 358 if (promoting_demoted_changes_) |
| 359 return; |
| 360 promoting_demoted_changes_ = true; |
| 361 |
| 362 int* job_count = new int(1); |
| 363 base::Closure promote_completion_callback = |
| 364 base::Bind(&SyncFileSystemService::OnPromotionCompleted, |
| 365 AsWeakPtr(), base::Owned(job_count)); |
| 366 |
| 358 int64 remote_changes = 0; | 367 int64 remote_changes = 0; |
| 359 for (ScopedVector<SyncProcessRunner>::iterator iter = | 368 for (size_t i = 0; i < remote_sync_runners_.size(); ++i) |
| 360 remote_sync_runners_.begin(); | 369 remote_changes += remote_sync_runners_[i]->pending_changes(); |
| 361 iter != remote_sync_runners_.end(); ++iter) | 370 if (remote_changes == 0) { |
| 362 remote_changes += (*iter)->pending_changes(); | 371 ++*job_count; |
| 363 if (remote_changes == 0) | 372 local_service_->PromoteDemotedChanges(promote_completion_callback); |
| 364 local_service_->PromoteDemotedChanges(NoopClosure()); | 373 } |
| 365 | 374 |
| 366 int64 local_changes = 0; | 375 int64 local_changes = 0; |
| 367 for (ScopedVector<SyncProcessRunner>::iterator iter = | 376 for (size_t i = 0; i < local_sync_runners_.size(); ++i) |
| 368 local_sync_runners_.begin(); | 377 local_changes += local_sync_runners_[i]->pending_changes(); |
| 369 iter != local_sync_runners_.end(); ++iter) | |
| 370 local_changes += (*iter)->pending_changes(); | |
| 371 if (local_changes == 0) { | 378 if (local_changes == 0) { |
| 372 remote_service_->PromoteDemotedChanges(NoopClosure()); | 379 ++*job_count; |
| 380 remote_service_->PromoteDemotedChanges(promote_completion_callback); |
| 373 } | 381 } |
| 382 |
| 383 promote_completion_callback.Run(); |
| 384 } |
| 385 |
| 386 void SyncFileSystemService::OnPromotionCompleted(int* count) { |
| 387 if (--*count != 0) |
| 388 return; |
| 389 promoting_demoted_changes_ = false; |
| 390 CheckIfIdle(); |
| 391 } |
| 392 |
| 393 void SyncFileSystemService::CheckIfIdle() { |
| 394 if (promoting_demoted_changes_) |
| 395 return; |
| 396 |
| 397 for (size_t i = 0; i < remote_sync_runners_.size(); ++i) { |
| 398 SyncServiceState service_state = remote_sync_runners_[i]->GetServiceState(); |
| 399 if (service_state != SYNC_SERVICE_RUNNING && |
| 400 service_state != SYNC_SERVICE_TEMPORARY_UNAVAILABLE) |
| 401 continue; |
| 402 |
| 403 if (remote_sync_runners_[i]->pending_changes()) |
| 404 return; |
| 405 } |
| 406 |
| 407 for (size_t i = 0; i < local_sync_runners_.size(); ++i) { |
| 408 SyncServiceState service_state = local_sync_runners_[i]->GetServiceState(); |
| 409 if (service_state != SYNC_SERVICE_RUNNING && |
| 410 service_state != SYNC_SERVICE_TEMPORARY_UNAVAILABLE) |
| 411 continue; |
| 412 |
| 413 if (local_sync_runners_[i]->pending_changes()) |
| 414 return; |
| 415 } |
| 416 |
| 417 if (idle_callback_.is_null()) |
| 418 return; |
| 419 |
| 420 base::Closure callback = idle_callback_; |
| 421 idle_callback_.Reset(); |
| 422 callback.Run(); |
| 374 } | 423 } |
| 375 | 424 |
| 376 SyncServiceState SyncFileSystemService::GetSyncServiceState() { | 425 SyncServiceState SyncFileSystemService::GetSyncServiceState() { |
| 377 // For now we always query the state from the main RemoteFileSyncService. | 426 // For now we always query the state from the main RemoteFileSyncService. |
| 378 return RemoteStateToSyncServiceState(remote_service_->GetCurrentState()); | 427 return RemoteStateToSyncServiceState(remote_service_->GetCurrentState()); |
| 379 } | 428 } |
| 380 | 429 |
| 381 SyncFileSystemService* SyncFileSystemService::GetSyncService() { | 430 SyncFileSystemService* SyncFileSystemService::GetSyncService() { |
| 382 return this; | 431 return this; |
| 383 } | 432 } |
| 384 | 433 |
| 434 void SyncFileSystemService::CallOnIdleForTesting( |
| 435 const base::Closure& callback) { |
| 436 DCHECK(idle_callback_.is_null()); |
| 437 idle_callback_ = callback; |
| 438 CheckIfIdle(); |
| 439 } |
| 440 |
| 385 SyncFileSystemService::SyncFileSystemService(Profile* profile) | 441 SyncFileSystemService::SyncFileSystemService(Profile* profile) |
| 386 : profile_(profile), | 442 : profile_(profile), |
| 387 sync_enabled_(true) { | 443 sync_enabled_(true), |
| 444 promoting_demoted_changes_(false) { |
| 388 } | 445 } |
| 389 | 446 |
| 390 void SyncFileSystemService::Initialize( | 447 void SyncFileSystemService::Initialize( |
| 391 scoped_ptr<LocalFileSyncService> local_service, | 448 scoped_ptr<LocalFileSyncService> local_service, |
| 392 scoped_ptr<RemoteFileSyncService> remote_service) { | 449 scoped_ptr<RemoteFileSyncService> remote_service) { |
| 393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 450 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 394 DCHECK(local_service); | 451 DCHECK(local_service); |
| 395 DCHECK(remote_service); | 452 DCHECK(remote_service); |
| 396 DCHECK(profile_); | 453 DCHECK(profile_); |
| 397 | 454 |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 iter != remote_sync_runners_.end(); ++iter) | 747 iter != remote_sync_runners_.end(); ++iter) |
| 691 ((*iter)->*method)(); | 748 ((*iter)->*method)(); |
| 692 } | 749 } |
| 693 | 750 |
| 694 RemoteFileSyncService* SyncFileSystemService::GetRemoteService( | 751 RemoteFileSyncService* SyncFileSystemService::GetRemoteService( |
| 695 const GURL& origin) { | 752 const GURL& origin) { |
| 696 return remote_service_.get(); | 753 return remote_service_.get(); |
| 697 } | 754 } |
| 698 | 755 |
| 699 } // namespace sync_file_system | 756 } // namespace sync_file_system |
| OLD | NEW |