| 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_service.h" | 5 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h" |
| 6 | 6 |
| 7 #include "base/single_thread_task_runner.h" | 7 #include "base/single_thread_task_runner.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/sync_file_system/file_change.h" | 12 #include "chrome/browser/sync_file_system/file_change.h" |
| 13 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" | 13 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" |
| 14 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" | 14 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" |
| 15 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" | 15 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" |
| 16 #include "chrome/browser/sync_file_system/local_change_processor.h" | 16 #include "chrome/browser/sync_file_system/local_change_processor.h" |
| 17 #include "chrome/browser/sync_file_system/logger.h" | 17 #include "chrome/browser/sync_file_system/logger.h" |
| 18 #include "chrome/browser/sync_file_system/sync_file_metadata.h" | 18 #include "chrome/browser/sync_file_system/sync_file_metadata.h" |
| 19 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/site_instance.h" | 21 #include "content/public/browser/site_instance.h" |
| 22 #include "content/public/browser/storage_partition.h" | 22 #include "content/public/browser/storage_partition.h" |
| 23 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
| 24 #include "extensions/common/extension_set.h" | 24 #include "extensions/common/extension_set.h" |
| 25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 26 #include "webkit/browser/fileapi/file_system_context.h" | 26 #include "storage/browser/fileapi/file_system_context.h" |
| 27 #include "webkit/browser/fileapi/file_system_url.h" | 27 #include "storage/browser/fileapi/file_system_url.h" |
| 28 #include "webkit/common/blob/scoped_file.h" | 28 #include "storage/common/blob/scoped_file.h" |
| 29 | 29 |
| 30 using content::BrowserThread; | 30 using content::BrowserThread; |
| 31 using fileapi::FileSystemURL; | 31 using storage::FileSystemURL; |
| 32 | 32 |
| 33 namespace sync_file_system { | 33 namespace sync_file_system { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 void PrepareForProcessRemoteChangeCallbackAdapter( | 37 void PrepareForProcessRemoteChangeCallbackAdapter( |
| 38 const RemoteChangeProcessor::PrepareChangeCallback& callback, | 38 const RemoteChangeProcessor::PrepareChangeCallback& callback, |
| 39 SyncStatusCode status, | 39 SyncStatusCode status, |
| 40 const LocalFileSyncInfo& sync_file_info, | 40 const LocalFileSyncInfo& sync_file_info, |
| 41 webkit_blob::ScopedFile snapshot) { | 41 storage::ScopedFile snapshot) { |
| 42 callback.Run(status, sync_file_info.metadata, sync_file_info.changes); | 42 callback.Run(status, sync_file_info.metadata, sync_file_info.changes); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void InvokeCallbackOnNthInvocation(int* count, const base::Closure& callback) { | 45 void InvokeCallbackOnNthInvocation(int* count, const base::Closure& callback) { |
| 46 --*count; | 46 --*count; |
| 47 if (*count <= 0) | 47 if (*count <= 0) |
| 48 callback.Run(); | 48 callback.Run(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 } | 124 } |
| 125 | 125 |
| 126 void LocalFileSyncService::Shutdown() { | 126 void LocalFileSyncService::Shutdown() { |
| 127 sync_context_->RemoveOriginChangeObserver(this); | 127 sync_context_->RemoveOriginChangeObserver(this); |
| 128 sync_context_->ShutdownOnUIThread(); | 128 sync_context_->ShutdownOnUIThread(); |
| 129 profile_ = NULL; | 129 profile_ = NULL; |
| 130 } | 130 } |
| 131 | 131 |
| 132 void LocalFileSyncService::MaybeInitializeFileSystemContext( | 132 void LocalFileSyncService::MaybeInitializeFileSystemContext( |
| 133 const GURL& app_origin, | 133 const GURL& app_origin, |
| 134 fileapi::FileSystemContext* file_system_context, | 134 storage::FileSystemContext* file_system_context, |
| 135 const SyncStatusCallback& callback) { | 135 const SyncStatusCallback& callback) { |
| 136 sync_context_->MaybeInitializeFileSystemContext( | 136 sync_context_->MaybeInitializeFileSystemContext( |
| 137 app_origin, file_system_context, | 137 app_origin, file_system_context, |
| 138 base::Bind(&LocalFileSyncService::DidInitializeFileSystemContext, | 138 base::Bind(&LocalFileSyncService::DidInitializeFileSystemContext, |
| 139 AsWeakPtr(), app_origin, | 139 AsWeakPtr(), app_origin, |
| 140 make_scoped_refptr(file_system_context), callback)); | 140 make_scoped_refptr(file_system_context), callback)); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void LocalFileSyncService::AddChangeObserver(Observer* observer) { | 143 void LocalFileSyncService::AddChangeObserver(Observer* observer) { |
| 144 change_observers_.AddObserver(observer); | 144 change_observers_.AddObserver(observer); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 240 |
| 241 // The extension has been uninstalled and this method is called | 241 // The extension has been uninstalled and this method is called |
| 242 // before the remote changes for the origin are removed. | 242 // before the remote changes for the origin are removed. |
| 243 callback.Run(SYNC_STATUS_NO_CHANGE_TO_SYNC, | 243 callback.Run(SYNC_STATUS_NO_CHANGE_TO_SYNC, |
| 244 SyncFileMetadata(), FileChangeList()); | 244 SyncFileMetadata(), FileChangeList()); |
| 245 return; | 245 return; |
| 246 } | 246 } |
| 247 GURL site_url = | 247 GURL site_url = |
| 248 extensions::util::GetSiteForExtensionId(extension->id(), profile_); | 248 extensions::util::GetSiteForExtensionId(extension->id(), profile_); |
| 249 DCHECK(!site_url.is_empty()); | 249 DCHECK(!site_url.is_empty()); |
| 250 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 250 scoped_refptr<storage::FileSystemContext> file_system_context = |
| 251 content::BrowserContext::GetStoragePartitionForSite( | 251 content::BrowserContext::GetStoragePartitionForSite(profile_, site_url) |
| 252 profile_, site_url)->GetFileSystemContext(); | 252 ->GetFileSystemContext(); |
| 253 MaybeInitializeFileSystemContext( | 253 MaybeInitializeFileSystemContext( |
| 254 url.origin(), | 254 url.origin(), |
| 255 file_system_context.get(), | 255 file_system_context.get(), |
| 256 base::Bind(&LocalFileSyncService::DidInitializeForRemoteSync, | 256 base::Bind(&LocalFileSyncService::DidInitializeForRemoteSync, |
| 257 AsWeakPtr(), | 257 AsWeakPtr(), |
| 258 url, | 258 url, |
| 259 file_system_context, | 259 file_system_context, |
| 260 callback)); | 260 callback)); |
| 261 return; | 261 return; |
| 262 } | 262 } |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), | 350 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(), |
| 351 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO) | 351 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO) |
| 352 .get())), | 352 .get())), |
| 353 local_change_processor_(NULL) { | 353 local_change_processor_(NULL) { |
| 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 355 sync_context_->AddOriginChangeObserver(this); | 355 sync_context_->AddOriginChangeObserver(this); |
| 356 } | 356 } |
| 357 | 357 |
| 358 void LocalFileSyncService::DidInitializeFileSystemContext( | 358 void LocalFileSyncService::DidInitializeFileSystemContext( |
| 359 const GURL& app_origin, | 359 const GURL& app_origin, |
| 360 fileapi::FileSystemContext* file_system_context, | 360 storage::FileSystemContext* file_system_context, |
| 361 const SyncStatusCallback& callback, | 361 const SyncStatusCallback& callback, |
| 362 SyncStatusCode status) { | 362 SyncStatusCode status) { |
| 363 if (status != SYNC_STATUS_OK) { | 363 if (status != SYNC_STATUS_OK) { |
| 364 callback.Run(status); | 364 callback.Run(status); |
| 365 return; | 365 return; |
| 366 } | 366 } |
| 367 DCHECK(file_system_context); | 367 DCHECK(file_system_context); |
| 368 origin_to_contexts_[app_origin] = file_system_context; | 368 origin_to_contexts_[app_origin] = file_system_context; |
| 369 | 369 |
| 370 if (pending_origins_with_changes_.find(app_origin) != | 370 if (pending_origins_with_changes_.find(app_origin) != |
| 371 pending_origins_with_changes_.end()) { | 371 pending_origins_with_changes_.end()) { |
| 372 // We have remaining changes for the origin. | 372 // We have remaining changes for the origin. |
| 373 pending_origins_with_changes_.erase(app_origin); | 373 pending_origins_with_changes_.erase(app_origin); |
| 374 SyncFileSystemBackend* backend = | 374 SyncFileSystemBackend* backend = |
| 375 SyncFileSystemBackend::GetBackend(file_system_context); | 375 SyncFileSystemBackend::GetBackend(file_system_context); |
| 376 DCHECK(backend); | 376 DCHECK(backend); |
| 377 DCHECK(backend->change_tracker()); | 377 DCHECK(backend->change_tracker()); |
| 378 origin_change_map_.SetOriginChangeCount( | 378 origin_change_map_.SetOriginChangeCount( |
| 379 app_origin, backend->change_tracker()->num_changes()); | 379 app_origin, backend->change_tracker()->num_changes()); |
| 380 int64 num_changes = origin_change_map_.GetTotalChangeCount(); | 380 int64 num_changes = origin_change_map_.GetTotalChangeCount(); |
| 381 FOR_EACH_OBSERVER(Observer, change_observers_, | 381 FOR_EACH_OBSERVER(Observer, change_observers_, |
| 382 OnLocalChangeAvailable(num_changes)); | 382 OnLocalChangeAvailable(num_changes)); |
| 383 } | 383 } |
| 384 callback.Run(status); | 384 callback.Run(status); |
| 385 } | 385 } |
| 386 | 386 |
| 387 void LocalFileSyncService::DidInitializeForRemoteSync( | 387 void LocalFileSyncService::DidInitializeForRemoteSync( |
| 388 const FileSystemURL& url, | 388 const FileSystemURL& url, |
| 389 fileapi::FileSystemContext* file_system_context, | 389 storage::FileSystemContext* file_system_context, |
| 390 const PrepareChangeCallback& callback, | 390 const PrepareChangeCallback& callback, |
| 391 SyncStatusCode status) { | 391 SyncStatusCode status) { |
| 392 if (status != SYNC_STATUS_OK) { | 392 if (status != SYNC_STATUS_OK) { |
| 393 DVLOG(1) << "FileSystemContext initialization failed for remote sync:" | 393 DVLOG(1) << "FileSystemContext initialization failed for remote sync:" |
| 394 << url.DebugString() << " status=" << status | 394 << url.DebugString() << " status=" << status |
| 395 << " (" << SyncStatusCodeToString(status) << ")"; | 395 << " (" << SyncStatusCodeToString(status) << ")"; |
| 396 callback.Run(status, SyncFileMetadata(), FileChangeList()); | 396 callback.Run(status, SyncFileMetadata(), FileChangeList()); |
| 397 return; | 397 return; |
| 398 } | 398 } |
| 399 origin_to_contexts_[url.origin()] = file_system_context; | 399 origin_to_contexts_[url.origin()] = file_system_context; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 416 SyncStatusCode status) { | 416 SyncStatusCode status) { |
| 417 util::Log(logging::LOG_VERBOSE, FROM_HERE, | 417 util::Log(logging::LOG_VERBOSE, FROM_HERE, |
| 418 "[Remote -> Local] ApplyRemoteChange finished --> %s", | 418 "[Remote -> Local] ApplyRemoteChange finished --> %s", |
| 419 SyncStatusCodeToString(status)); | 419 SyncStatusCodeToString(status)); |
| 420 callback.Run(status); | 420 callback.Run(status); |
| 421 } | 421 } |
| 422 | 422 |
| 423 void LocalFileSyncService::DidGetFileForLocalSync( | 423 void LocalFileSyncService::DidGetFileForLocalSync( |
| 424 SyncStatusCode status, | 424 SyncStatusCode status, |
| 425 const LocalFileSyncInfo& sync_file_info, | 425 const LocalFileSyncInfo& sync_file_info, |
| 426 webkit_blob::ScopedFile snapshot) { | 426 storage::ScopedFile snapshot) { |
| 427 DCHECK(!local_sync_callback_.is_null()); | 427 DCHECK(!local_sync_callback_.is_null()); |
| 428 if (status != SYNC_STATUS_OK) { | 428 if (status != SYNC_STATUS_OK) { |
| 429 RunLocalSyncCallback(status, sync_file_info.url); | 429 RunLocalSyncCallback(status, sync_file_info.url); |
| 430 return; | 430 return; |
| 431 } | 431 } |
| 432 if (sync_file_info.changes.empty()) { | 432 if (sync_file_info.changes.empty()) { |
| 433 // There's a slight chance this could happen. | 433 // There's a slight chance this could happen. |
| 434 SyncFileCallback callback = local_sync_callback_; | 434 SyncFileCallback callback = local_sync_callback_; |
| 435 local_sync_callback_.Reset(); | 435 local_sync_callback_.Reset(); |
| 436 ProcessLocalChange(callback); | 436 ProcessLocalChange(callback); |
| 437 return; | 437 return; |
| 438 } | 438 } |
| 439 | 439 |
| 440 FileChange next_change = sync_file_info.changes.front(); | 440 FileChange next_change = sync_file_info.changes.front(); |
| 441 DVLOG(1) << "ProcessLocalChange: " << sync_file_info.url.DebugString() | 441 DVLOG(1) << "ProcessLocalChange: " << sync_file_info.url.DebugString() |
| 442 << " change:" << next_change.DebugString(); | 442 << " change:" << next_change.DebugString(); |
| 443 | 443 |
| 444 GetLocalChangeProcessor(sync_file_info.url)->ApplyLocalChange( | 444 GetLocalChangeProcessor(sync_file_info.url)->ApplyLocalChange( |
| 445 next_change, | 445 next_change, |
| 446 sync_file_info.local_file_path, | 446 sync_file_info.local_file_path, |
| 447 sync_file_info.metadata, | 447 sync_file_info.metadata, |
| 448 sync_file_info.url, | 448 sync_file_info.url, |
| 449 base::Bind(&LocalFileSyncService::ProcessNextChangeForURL, | 449 base::Bind(&LocalFileSyncService::ProcessNextChangeForURL, |
| 450 AsWeakPtr(), base::Passed(&snapshot), sync_file_info, | 450 AsWeakPtr(), base::Passed(&snapshot), sync_file_info, |
| 451 next_change, sync_file_info.changes.PopAndGetNewList())); | 451 next_change, sync_file_info.changes.PopAndGetNewList())); |
| 452 } | 452 } |
| 453 | 453 |
| 454 void LocalFileSyncService::ProcessNextChangeForURL( | 454 void LocalFileSyncService::ProcessNextChangeForURL( |
| 455 webkit_blob::ScopedFile snapshot, | 455 storage::ScopedFile snapshot, |
| 456 const LocalFileSyncInfo& sync_file_info, | 456 const LocalFileSyncInfo& sync_file_info, |
| 457 const FileChange& processed_change, | 457 const FileChange& processed_change, |
| 458 const FileChangeList& changes, | 458 const FileChangeList& changes, |
| 459 SyncStatusCode status) { | 459 SyncStatusCode status) { |
| 460 DVLOG(1) << "Processed one local change: " | 460 DVLOG(1) << "Processed one local change: " |
| 461 << sync_file_info.url.DebugString() | 461 << sync_file_info.url.DebugString() |
| 462 << " change:" << processed_change.DebugString() | 462 << " change:" << processed_change.DebugString() |
| 463 << " status:" << status; | 463 << " status:" << status; |
| 464 | 464 |
| 465 if (status == SYNC_STATUS_RETRY) { | 465 if (status == SYNC_STATUS_RETRY) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 | 503 |
| 504 LocalChangeProcessor* LocalFileSyncService::GetLocalChangeProcessor( | 504 LocalChangeProcessor* LocalFileSyncService::GetLocalChangeProcessor( |
| 505 const FileSystemURL& url) { | 505 const FileSystemURL& url) { |
| 506 if (!get_local_change_processor_.is_null()) | 506 if (!get_local_change_processor_.is_null()) |
| 507 return get_local_change_processor_.Run(url.origin()); | 507 return get_local_change_processor_.Run(url.origin()); |
| 508 DCHECK(local_change_processor_); | 508 DCHECK(local_change_processor_); |
| 509 return local_change_processor_; | 509 return local_change_processor_; |
| 510 } | 510 } |
| 511 | 511 |
| 512 } // namespace sync_file_system | 512 } // namespace sync_file_system |
| OLD | NEW |