OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <algorithm> | 5 #include <algorithm> |
6 #include <stack> | 6 #include <stack> |
7 | 7 |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "chrome/browser/drive/drive_uploader.h" | 11 #include "chrome/browser/drive/drive_uploader.h" |
12 #include "chrome/browser/drive/fake_drive_service.h" | 12 #include "chrome/browser/drive/fake_drive_service.h" |
13 #include "chrome/browser/drive/test_util.h" | 13 #include "chrome/browser/drive/test_util.h" |
14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants. h" | 14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants. h" |
15 #include "chrome/browser/sync_file_system/drive_backend/fake_drive_service_helpe r.h" | 15 #include "chrome/browser/sync_file_system/drive_backend/fake_drive_service_helpe r.h" |
16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h" | 16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h" |
17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" | 17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" |
18 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h" | 18 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h" |
19 #include "chrome/browser/sync_file_system/drive_backend/sync_worker.h" | |
19 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h" | 20 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h" |
20 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" | 21 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h" |
21 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h" | 22 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h" |
22 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" | 23 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h" |
23 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" | 24 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" |
24 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" | 25 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" |
25 #include "chrome/test/base/testing_profile.h" | 26 #include "chrome/test/base/testing_profile.h" |
26 #include "content/public/test/test_browser_thread.h" | 27 #include "content/public/test/test_browser_thread.h" |
27 #include "content/public/test/test_browser_thread_bundle.h" | 28 #include "content/public/test/test_browser_thread_bundle.h" |
28 #include "extensions/common/extension.h" | 29 #include "extensions/common/extension.h" |
29 #include "google_apis/drive/drive_api_parser.h" | 30 #include "google_apis/drive/drive_api_parser.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
31 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 32 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
32 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 33 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
33 #include "webkit/browser/fileapi/file_system_context.h" | 34 #include "webkit/browser/fileapi/file_system_context.h" |
34 | 35 |
35 #define FPL(a) FILE_PATH_LITERAL(a) | 36 #define FPL(a) FILE_PATH_LITERAL(a) |
36 | 37 |
37 namespace sync_file_system { | 38 namespace sync_file_system { |
38 namespace drive_backend { | 39 namespace drive_backend { |
39 | 40 |
40 typedef fileapi::FileSystemOperation::FileEntryList FileEntryList; | 41 typedef fileapi::FileSystemOperation::FileEntryList FileEntryList; |
41 | 42 |
42 namespace { | 43 namespace { |
43 | 44 |
44 void SetSyncStatus(const base::Closure& closure, | 45 template <typename T> |
45 SyncStatusCode* status_out, | 46 void SetValueAndCallClosure(const base::Closure& closure, |
46 SyncStatusCode status) { | 47 T* arg_out, |
47 *status_out = status; | 48 T arg) { |
49 *arg_out = base::internal::CallbackForward(arg); | |
48 closure.Run(); | 50 closure.Run(); |
49 } | 51 } |
50 | 52 |
51 void SetSyncStatusAndUrl(const base::Closure& closure, | 53 void SetSyncStatusAndUrl(const base::Closure& closure, |
52 SyncStatusCode* status_out, | 54 SyncStatusCode* status_out, |
53 fileapi::FileSystemURL* url_out, | 55 fileapi::FileSystemURL* url_out, |
54 SyncStatusCode status, | 56 SyncStatusCode status, |
55 const fileapi::FileSystemURL& url) { | 57 const fileapi::FileSystemURL& url) { |
56 *status_out = status; | 58 *status_out = status; |
57 *url_out = url; | 59 *url_out = url; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 } | 152 } |
151 | 153 |
152 fileapi::FileSystemURL CreateURL(const std::string& app_id, | 154 fileapi::FileSystemURL CreateURL(const std::string& app_id, |
153 const base::FilePath& path) { | 155 const base::FilePath& path) { |
154 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id); | 156 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id); |
155 return CreateSyncableFileSystemURL(origin, path); | 157 return CreateSyncableFileSystemURL(origin, path); |
156 } | 158 } |
157 | 159 |
158 bool GetAppRootFolderID(const std::string& app_id, | 160 bool GetAppRootFolderID(const std::string& app_id, |
159 std::string* folder_id) { | 161 std::string* folder_id) { |
162 base::RunLoop run_loop; | |
163 bool success = false; | |
160 FileTracker tracker; | 164 FileTracker tracker; |
161 if (!metadata_database()->FindAppRootTracker(app_id, &tracker)) | 165 PostTaskAndReplyWithResult( |
166 worker_task_runner_, | |
167 FROM_HERE, | |
168 base::Bind(&MetadataDatabase::FindAppRootTracker, | |
169 base::Unretained(metadata_database()), | |
170 app_id, | |
171 &tracker), | |
172 base::Bind(&SetValueAndCallClosure<bool>, | |
173 run_loop.QuitClosure(), | |
174 &success)); | |
175 run_loop.Run(); | |
176 if (!success) | |
162 return false; | 177 return false; |
163 *folder_id = tracker.file_id(); | 178 *folder_id = tracker.file_id(); |
164 return true; | 179 return true; |
165 } | 180 } |
166 | 181 |
167 std::string GetFileIDByPath(const std::string& app_id, | 182 std::string GetFileIDByPath(const std::string& app_id, |
168 const base::FilePath::StringType& path) { | 183 const base::FilePath::StringType& path) { |
169 return GetFileIDByPath(app_id, base::FilePath(path)); | 184 return GetFileIDByPath(app_id, base::FilePath(path)); |
170 } | 185 } |
171 | 186 |
172 std::string GetFileIDByPath(const std::string& app_id, | 187 std::string GetFileIDByPath(const std::string& app_id, |
173 const base::FilePath& path) { | 188 const base::FilePath& path) { |
189 base::RunLoop run_loop; | |
190 bool success = false; | |
174 FileTracker tracker; | 191 FileTracker tracker; |
175 base::FilePath result_path; | 192 base::FilePath result_path; |
176 base::FilePath normalized_path = path.NormalizePathSeparators(); | 193 base::FilePath normalized_path = path.NormalizePathSeparators(); |
177 EXPECT_TRUE(metadata_database()->FindNearestActiveAncestor( | 194 PostTaskAndReplyWithResult( |
178 app_id, normalized_path, &tracker, &result_path)); | 195 worker_task_runner_, |
196 FROM_HERE, | |
197 base::Bind(&MetadataDatabase::FindNearestActiveAncestor, | |
198 base::Unretained(metadata_database()), | |
199 app_id, | |
200 normalized_path, | |
201 &tracker, | |
202 &result_path), | |
203 base::Bind(&SetValueAndCallClosure<bool>, | |
204 run_loop.QuitClosure(), | |
205 &success)); | |
206 run_loop.Run(); | |
207 EXPECT_TRUE(success); | |
179 EXPECT_EQ(normalized_path, result_path); | 208 EXPECT_EQ(normalized_path, result_path); |
180 return tracker.file_id(); | 209 return tracker.file_id(); |
181 } | 210 } |
182 | 211 |
183 SyncStatusCode RegisterApp(const std::string& app_id) { | 212 SyncStatusCode RegisterApp(const std::string& app_id) { |
184 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id); | 213 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id); |
185 if (!ContainsKey(file_systems_, app_id)) { | 214 if (!ContainsKey(file_systems_, app_id)) { |
186 CannedSyncableFileSystem* file_system = new CannedSyncableFileSystem( | 215 CannedSyncableFileSystem* file_system = new CannedSyncableFileSystem( |
187 origin, in_memory_env_.get(), | 216 origin, in_memory_env_.get(), |
188 io_task_runner_.get(), file_task_runner_.get()); | 217 io_task_runner_.get(), file_task_runner_.get()); |
189 file_system->SetUp(CannedSyncableFileSystem::QUOTA_DISABLED); | 218 file_system->SetUp(CannedSyncableFileSystem::QUOTA_DISABLED); |
190 | 219 |
191 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 220 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
192 base::RunLoop run_loop; | 221 base::RunLoop run_loop; |
193 local_sync_service_->MaybeInitializeFileSystemContext( | 222 local_sync_service_->MaybeInitializeFileSystemContext( |
194 origin, file_system->file_system_context(), | 223 origin, file_system->file_system_context(), |
195 base::Bind(&SetSyncStatus, run_loop.QuitClosure(), &status)); | 224 base::Bind(&SetValueAndCallClosure<SyncStatusCode>, |
225 run_loop.QuitClosure(), &status)); | |
196 run_loop.Run(); | 226 run_loop.Run(); |
197 EXPECT_EQ(SYNC_STATUS_OK, status); | 227 EXPECT_EQ(SYNC_STATUS_OK, status); |
198 | 228 |
199 file_system->backend()->sync_context()-> | 229 file_system->backend()->sync_context()-> |
200 set_mock_notify_changes_duration_in_sec(0); | 230 set_mock_notify_changes_duration_in_sec(0); |
201 | 231 |
202 EXPECT_EQ(base::File::FILE_OK, file_system->OpenFileSystem()); | 232 EXPECT_EQ(base::File::FILE_OK, file_system->OpenFileSystem()); |
203 file_systems_[app_id] = file_system; | 233 file_systems_[app_id] = file_system; |
204 } | 234 } |
205 | 235 |
206 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 236 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
207 base::RunLoop run_loop; | 237 base::RunLoop run_loop; |
208 remote_sync_service_->RegisterOrigin( | 238 remote_sync_service_->RegisterOrigin( |
209 origin, | 239 origin, |
210 base::Bind(&SetSyncStatus, run_loop.QuitClosure(), &status)); | 240 base::Bind(&SetValueAndCallClosure<SyncStatusCode>, |
241 run_loop.QuitClosure(), &status)); | |
211 run_loop.Run(); | 242 run_loop.Run(); |
212 return status; | 243 return status; |
213 } | 244 } |
214 | 245 |
215 void AddLocalFolder(const std::string& app_id, | 246 void AddLocalFolder(const std::string& app_id, |
216 const base::FilePath::StringType& path) { | 247 const base::FilePath::StringType& path) { |
217 ASSERT_TRUE(ContainsKey(file_systems_, app_id)); | 248 ASSERT_TRUE(ContainsKey(file_systems_, app_id)); |
218 EXPECT_EQ(base::File::FILE_OK, | 249 EXPECT_EQ(base::File::FILE_OK, |
219 file_systems_[app_id]->CreateDirectory( | 250 file_systems_[app_id]->CreateDirectory( |
220 CreateURL(app_id, path))); | 251 CreateURL(app_id, path))); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 return remote_sync_status; | 333 return remote_sync_status; |
303 | 334 |
304 if (local_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC && | 335 if (local_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC && |
305 remote_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC) { | 336 remote_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC) { |
306 remote_sync_service_->PromoteDemotedChanges(); | 337 remote_sync_service_->PromoteDemotedChanges(); |
307 local_sync_service_->PromoteDemotedChanges(); | 338 local_sync_service_->PromoteDemotedChanges(); |
308 | 339 |
309 if (pending_remote_changes_ || pending_local_changes_) | 340 if (pending_remote_changes_ || pending_local_changes_) |
310 continue; | 341 continue; |
311 | 342 |
312 int64 largest_fetched_change_id = | 343 base::RunLoop run_loop; |
313 metadata_database()->GetLargestFetchedChangeID(); | 344 int64 largest_fetched_change_id = -1; |
345 PostTaskAndReplyWithResult( | |
346 worker_task_runner_, | |
347 FROM_HERE, | |
348 base::Bind(&MetadataDatabase::GetLargestFetchedChangeID, | |
349 base::Unretained(metadata_database())), | |
350 base::Bind(&SetValueAndCallClosure<int64>, | |
351 run_loop.QuitClosure(), | |
352 &largest_fetched_change_id)); | |
353 run_loop.Run(); | |
314 if (largest_fetched_change_id != GetLargestChangeID()) { | 354 if (largest_fetched_change_id != GetLargestChangeID()) { |
315 FetchRemoteChanges(); | 355 FetchRemoteChanges(); |
316 continue; | 356 continue; |
317 } | 357 } |
318 break; | 358 break; |
319 } | 359 } |
320 } | 360 } |
321 return SYNC_STATUS_OK; | 361 return SYNC_STATUS_OK; |
322 } | 362 } |
323 | 363 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 const base::FilePath::StringType& path) { | 519 const base::FilePath::StringType& path) { |
480 SCOPED_TRACE(testing::Message() << "Verifying local file: " | 520 SCOPED_TRACE(testing::Message() << "Verifying local file: " |
481 << "app_id = " << app_id | 521 << "app_id = " << app_id |
482 << ", path = " << path); | 522 << ", path = " << path); |
483 ASSERT_TRUE(ContainsKey(file_systems_, app_id)); | 523 ASSERT_TRUE(ContainsKey(file_systems_, app_id)); |
484 EXPECT_EQ(base::File::FILE_OK, | 524 EXPECT_EQ(base::File::FILE_OK, |
485 file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path))); | 525 file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path))); |
486 } | 526 } |
487 | 527 |
488 size_t CountMetadata() { | 528 size_t CountMetadata() { |
489 return metadata_database()->CountFileMetadata(); | 529 size_t count = 0; |
530 base::RunLoop run_loop; | |
531 PostTaskAndReplyWithResult( | |
532 worker_task_runner_, | |
533 FROM_HERE, | |
534 base::Bind(&MetadataDatabase::CountFileMetadata, | |
535 base::Unretained(metadata_database())), | |
536 base::Bind(&SetValueAndCallClosure<size_t>, | |
537 run_loop.QuitClosure(), | |
538 &count)); | |
539 run_loop.Run(); | |
540 return count; | |
490 } | 541 } |
491 | 542 |
492 size_t CountTracker() { | 543 size_t CountTracker() { |
493 return metadata_database()->CountFileTracker(); | 544 size_t count = 0; |
545 base::RunLoop run_loop; | |
546 PostTaskAndReplyWithResult( | |
547 worker_task_runner_, | |
548 FROM_HERE, | |
549 base::Bind(&MetadataDatabase::CountFileTracker, | |
550 base::Unretained(metadata_database())), | |
551 base::Bind(&SetValueAndCallClosure<size_t>, | |
552 run_loop.QuitClosure(), &count)); | |
553 run_loop.Run(); | |
554 return count; | |
494 } | 555 } |
495 | 556 |
496 drive::FakeDriveService* fake_drive_service() { | 557 drive::FakeDriveService* fake_drive_service() { |
497 return static_cast<drive::FakeDriveService*>( | 558 return static_cast<drive::FakeDriveService*>( |
498 remote_sync_service_->GetDriveService()); | 559 remote_sync_service_->GetDriveService()); |
499 } | 560 } |
500 | 561 |
501 FakeDriveServiceHelper* fake_drive_service_helper() { | 562 FakeDriveServiceHelper* fake_drive_service_helper() { |
502 return fake_drive_service_helper_.get(); | 563 return fake_drive_service_helper_.get(); |
503 } | 564 } |
504 | 565 |
566 // NOTE: Member functions of MetadataDatabase class must not be called | |
567 // directly through this method. Call them via PostTask. | |
505 MetadataDatabase* metadata_database() { | 568 MetadataDatabase* metadata_database() { |
nhiroki
2014/06/02 05:43:56
Can you make this 'private'?
peria
2014/06/02 05:57:30
Done.
| |
506 return remote_sync_service_->GetMetadataDatabase(); | 569 return remote_sync_service_->sync_worker_->GetMetadataDatabase(); |
507 } | 570 } |
508 | 571 |
509 private: | 572 private: |
510 content::TestBrowserThreadBundle thread_bundle_; | 573 content::TestBrowserThreadBundle thread_bundle_; |
511 | 574 |
512 base::ScopedTempDir base_dir_; | 575 base::ScopedTempDir base_dir_; |
513 scoped_ptr<leveldb::Env> in_memory_env_; | 576 scoped_ptr<leveldb::Env> in_memory_env_; |
514 TestingProfile profile_; | 577 TestingProfile profile_; |
515 | 578 |
516 scoped_ptr<SyncEngine> remote_sync_service_; | 579 scoped_ptr<SyncEngine> remote_sync_service_; |
(...skipping 1092 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1609 | 1672 |
1610 EXPECT_EQ(1u, CountApp()); | 1673 EXPECT_EQ(1u, CountApp()); |
1611 EXPECT_EQ(1u, CountLocalFile(app_id)); | 1674 EXPECT_EQ(1u, CountLocalFile(app_id)); |
1612 | 1675 |
1613 EXPECT_EQ(2u, CountMetadata()); | 1676 EXPECT_EQ(2u, CountMetadata()); |
1614 EXPECT_EQ(2u, CountTracker()); | 1677 EXPECT_EQ(2u, CountTracker()); |
1615 } | 1678 } |
1616 | 1679 |
1617 } // namespace drive_backend | 1680 } // namespace drive_backend |
1618 } // namespace sync_file_system | 1681 } // namespace sync_file_system |
OLD | NEW |