| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" |
| 18 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 19 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h" | 22 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h" |
| 22 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" | 23 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h" |
| 23 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" | 24 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" |
| 24 #include "chrome/browser/sync_file_system/sync_file_metadata.h" | 25 #include "chrome/browser/sync_file_system/sync_file_metadata.h" |
| 25 #include "chrome/browser/sync_file_system/sync_status_code.h" | 26 #include "chrome/browser/sync_file_system/sync_status_code.h" |
| 26 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" | 27 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" |
| 27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 } | 100 } |
| 100 | 101 |
| 101 SyncStatusCode PrepareForSync(FileSystemContext* file_system_context, | 102 SyncStatusCode PrepareForSync(FileSystemContext* file_system_context, |
| 102 const FileSystemURL& url, | 103 const FileSystemURL& url, |
| 103 LocalFileSyncContext::SyncMode sync_mode, | 104 LocalFileSyncContext::SyncMode sync_mode, |
| 104 SyncFileMetadata* metadata, | 105 SyncFileMetadata* metadata, |
| 105 FileChangeList* changes, | 106 FileChangeList* changes, |
| 106 storage::ScopedFile* snapshot) { | 107 storage::ScopedFile* snapshot) { |
| 107 StartPrepareForSync(file_system_context, url, sync_mode, | 108 StartPrepareForSync(file_system_context, url, sync_mode, |
| 108 metadata, changes, snapshot); | 109 metadata, changes, snapshot); |
| 109 base::MessageLoop::current()->Run(); | 110 base::RunLoop().Run(); |
| 110 return status_; | 111 return status_; |
| 111 } | 112 } |
| 112 | 113 |
| 113 base::Closure GetPrepareForSyncClosure( | 114 base::Closure GetPrepareForSyncClosure( |
| 114 FileSystemContext* file_system_context, | 115 FileSystemContext* file_system_context, |
| 115 const FileSystemURL& url, | 116 const FileSystemURL& url, |
| 116 LocalFileSyncContext::SyncMode sync_mode, | 117 LocalFileSyncContext::SyncMode sync_mode, |
| 117 SyncFileMetadata* metadata, | 118 SyncFileMetadata* metadata, |
| 118 FileChangeList* changes, | 119 FileChangeList* changes, |
| 119 storage::ScopedFile* snapshot) { | 120 storage::ScopedFile* snapshot) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 LocalFileSyncContext::SYNC_EXCLUSIVE, | 156 LocalFileSyncContext::SYNC_EXCLUSIVE, |
| 156 &metadata, &changes, nullptr)); | 157 &metadata, &changes, nullptr)); |
| 157 EXPECT_EQ(expected_file_type, metadata.file_type); | 158 EXPECT_EQ(expected_file_type, metadata.file_type); |
| 158 | 159 |
| 159 status_ = SYNC_STATUS_UNKNOWN; | 160 status_ = SYNC_STATUS_UNKNOWN; |
| 160 sync_context_->ApplyRemoteChange( | 161 sync_context_->ApplyRemoteChange( |
| 161 file_system_context, change, local_path, url, | 162 file_system_context, change, local_path, url, |
| 162 base::Bind(&LocalFileSyncContextTest::DidApplyRemoteChange, | 163 base::Bind(&LocalFileSyncContextTest::DidApplyRemoteChange, |
| 163 base::Unretained(this), | 164 base::Unretained(this), |
| 164 base::RetainedRef(file_system_context), url)); | 165 base::RetainedRef(file_system_context), url)); |
| 165 base::MessageLoop::current()->Run(); | 166 base::RunLoop().Run(); |
| 166 return status_; | 167 return status_; |
| 167 } | 168 } |
| 168 | 169 |
| 169 void DidApplyRemoteChange(FileSystemContext* file_system_context, | 170 void DidApplyRemoteChange(FileSystemContext* file_system_context, |
| 170 const FileSystemURL& url, | 171 const FileSystemURL& url, |
| 171 SyncStatusCode status) { | 172 SyncStatusCode status) { |
| 172 status_ = status; | 173 status_ = status; |
| 173 sync_context_->FinalizeExclusiveSync( | 174 sync_context_->FinalizeExclusiveSync( |
| 174 file_system_context, url, | 175 file_system_context, url, |
| 175 status == SYNC_STATUS_OK /* clear_local_changes */, | 176 status == SYNC_STATUS_OK /* clear_local_changes */, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 190 } | 191 } |
| 191 ASSERT_TRUE(io_task_runner_->RunsTasksOnCurrentThread()); | 192 ASSERT_TRUE(io_task_runner_->RunsTasksOnCurrentThread()); |
| 192 file_error_ = base::File::FILE_ERROR_FAILED; | 193 file_error_ = base::File::FILE_ERROR_FAILED; |
| 193 file_system->operation_runner()->Truncate( | 194 file_system->operation_runner()->Truncate( |
| 194 url, 1, base::Bind(&LocalFileSyncContextTest::DidModifyFile, | 195 url, 1, base::Bind(&LocalFileSyncContextTest::DidModifyFile, |
| 195 base::Unretained(this))); | 196 base::Unretained(this))); |
| 196 } | 197 } |
| 197 | 198 |
| 198 base::File::Error WaitUntilModifyFileIsDone() { | 199 base::File::Error WaitUntilModifyFileIsDone() { |
| 199 while (!async_modify_finished_) | 200 while (!async_modify_finished_) |
| 200 base::MessageLoop::current()->RunUntilIdle(); | 201 base::RunLoop().RunUntilIdle(); |
| 201 return file_error_; | 202 return file_error_; |
| 202 } | 203 } |
| 203 | 204 |
| 204 void DidModifyFile(base::File::Error error) { | 205 void DidModifyFile(base::File::Error error) { |
| 205 if (!ui_task_runner_->RunsTasksOnCurrentThread()) { | 206 if (!ui_task_runner_->RunsTasksOnCurrentThread()) { |
| 206 ASSERT_TRUE(io_task_runner_->RunsTasksOnCurrentThread()); | 207 ASSERT_TRUE(io_task_runner_->RunsTasksOnCurrentThread()); |
| 207 ui_task_runner_->PostTask( | 208 ui_task_runner_->PostTask( |
| 208 FROM_HERE, | 209 FROM_HERE, |
| 209 base::Bind(&LocalFileSyncContextTest::DidModifyFile, | 210 base::Bind(&LocalFileSyncContextTest::DidModifyFile, |
| 210 base::Unretained(this), error)); | 211 base::Unretained(this), error)); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 EXPECT_EQ(1U, changes.size()); | 320 EXPECT_EQ(1U, changes.size()); |
| 320 EXPECT_TRUE(changes.list().back().IsFile()); | 321 EXPECT_TRUE(changes.list().back().IsFile()); |
| 321 EXPECT_TRUE(changes.list().back().IsAddOrUpdate()); | 322 EXPECT_TRUE(changes.list().back().IsAddOrUpdate()); |
| 322 | 323 |
| 323 StartModifyFileOnIOThread(&file_system, kFile); | 324 StartModifyFileOnIOThread(&file_system, kFile); |
| 324 | 325 |
| 325 if (sync_mode == LocalFileSyncContext::SYNC_SNAPSHOT) { | 326 if (sync_mode == LocalFileSyncContext::SYNC_SNAPSHOT) { |
| 326 // Write should succeed. | 327 // Write should succeed. |
| 327 EXPECT_EQ(base::File::FILE_OK, WaitUntilModifyFileIsDone()); | 328 EXPECT_EQ(base::File::FILE_OK, WaitUntilModifyFileIsDone()); |
| 328 } else { | 329 } else { |
| 329 base::MessageLoop::current()->RunUntilIdle(); | 330 base::RunLoop().RunUntilIdle(); |
| 330 EXPECT_FALSE(async_modify_finished_); | 331 EXPECT_FALSE(async_modify_finished_); |
| 331 } | 332 } |
| 332 | 333 |
| 333 SimulateFinishSync(file_system.file_system_context(), kFile, | 334 SimulateFinishSync(file_system.file_system_context(), kFile, |
| 334 SYNC_STATUS_OK, sync_mode); | 335 SYNC_STATUS_OK, sync_mode); |
| 335 | 336 |
| 336 EXPECT_EQ(base::File::FILE_OK, WaitUntilModifyFileIsDone()); | 337 EXPECT_EQ(base::File::FILE_OK, WaitUntilModifyFileIsDone()); |
| 337 | 338 |
| 338 // Sync succeeded, but the other change that was made during or | 339 // Sync succeeded, but the other change that was made during or |
| 339 // after sync is recorded. | 340 // after sync is recorded. |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 sync_context_->RegisterURLForWaitingSync( | 577 sync_context_->RegisterURLForWaitingSync( |
| 577 kURL1, GetPrepareForSyncClosure(file_system.file_system_context(), kURL1, | 578 kURL1, GetPrepareForSyncClosure(file_system.file_system_context(), kURL1, |
| 578 LocalFileSyncContext::SYNC_EXCLUSIVE, | 579 LocalFileSyncContext::SYNC_EXCLUSIVE, |
| 579 &metadata, &changes, nullptr)); | 580 &metadata, &changes, nullptr)); |
| 580 | 581 |
| 581 // Wait for the completion. | 582 // Wait for the completion. |
| 582 EXPECT_EQ(base::File::FILE_OK, WaitUntilModifyFileIsDone()); | 583 EXPECT_EQ(base::File::FILE_OK, WaitUntilModifyFileIsDone()); |
| 583 | 584 |
| 584 // The PrepareForSync must have been started; wait until DidPrepareForSync | 585 // The PrepareForSync must have been started; wait until DidPrepareForSync |
| 585 // is done. | 586 // is done. |
| 586 base::MessageLoop::current()->Run(); | 587 base::RunLoop().Run(); |
| 587 ASSERT_FALSE(has_inflight_prepare_for_sync_); | 588 ASSERT_FALSE(has_inflight_prepare_for_sync_); |
| 588 | 589 |
| 589 // Now PrepareForSync should have run and returned OK. | 590 // Now PrepareForSync should have run and returned OK. |
| 590 EXPECT_EQ(SYNC_STATUS_OK, status_); | 591 EXPECT_EQ(SYNC_STATUS_OK, status_); |
| 591 EXPECT_EQ(1U, changes.size()); | 592 EXPECT_EQ(1U, changes.size()); |
| 592 EXPECT_TRUE(changes.list().back().IsFile()); | 593 EXPECT_TRUE(changes.list().back().IsFile()); |
| 593 EXPECT_TRUE(changes.list().back().IsAddOrUpdate()); | 594 EXPECT_TRUE(changes.list().back().IsAddOrUpdate()); |
| 594 EXPECT_EQ(SYNC_FILE_TYPE_FILE, metadata.file_type); | 595 EXPECT_EQ(SYNC_FILE_TYPE_FILE, metadata.file_type); |
| 595 EXPECT_EQ(1, metadata.size); | 596 EXPECT_EQ(1, metadata.size); |
| 596 | 597 |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 954 | 955 |
| 955 // Make sure kDir and kFile are created by ApplyRemoteChange. | 956 // Make sure kDir and kFile are created by ApplyRemoteChange. |
| 956 EXPECT_EQ(base::File::FILE_OK, file_system.FileExists(kFile)); | 957 EXPECT_EQ(base::File::FILE_OK, file_system.FileExists(kFile)); |
| 957 EXPECT_EQ(base::File::FILE_OK, file_system.DirectoryExists(kDir)); | 958 EXPECT_EQ(base::File::FILE_OK, file_system.DirectoryExists(kDir)); |
| 958 | 959 |
| 959 sync_context_->ShutdownOnUIThread(); | 960 sync_context_->ShutdownOnUIThread(); |
| 960 file_system.TearDown(); | 961 file_system.TearDown(); |
| 961 } | 962 } |
| 962 | 963 |
| 963 } // namespace sync_file_system | 964 } // namespace sync_file_system |
| OLD | NEW |