| 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 "sync/api/attachments/attachment_store.h" | 5 #include "sync/api/attachments/attachment_store.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "sync/internal_api/public/attachments/attachment_store_handle.h" | 13 #include "sync/internal_api/public/attachments/attachment_store_frontend.h" |
| 14 #include "sync/internal_api/public/attachments/in_memory_attachment_store.h" | 14 #include "sync/internal_api/public/attachments/in_memory_attachment_store.h" |
| 15 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h" | 15 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h" |
| 16 | 16 |
| 17 namespace syncer { | 17 namespace syncer { |
| 18 | 18 |
| 19 AttachmentStore::AttachmentStore() {} | 19 AttachmentStore::AttachmentStore( |
| 20 AttachmentStore::~AttachmentStore() {} | 20 const scoped_refptr<AttachmentStoreFrontend>& frontend, |
| 21 AttachmentReferrer referrer) |
| 22 : frontend_(frontend), referrer_(referrer) { |
| 23 } |
| 21 | 24 |
| 22 scoped_refptr<AttachmentStore> AttachmentStore::CreateInMemoryStore() { | 25 AttachmentStore::~AttachmentStore() { |
| 26 } |
| 27 |
| 28 void AttachmentStore::Read(const AttachmentIdList& ids, |
| 29 const ReadCallback& callback) { |
| 30 frontend_->Read(ids, callback); |
| 31 } |
| 32 |
| 33 void AttachmentStore::Write(const AttachmentList& attachments, |
| 34 const WriteCallback& callback) { |
| 35 frontend_->Write(referrer_, attachments, callback); |
| 36 } |
| 37 |
| 38 void AttachmentStore::Drop(const AttachmentIdList& ids, |
| 39 const DropCallback& callback) { |
| 40 frontend_->Drop(referrer_, ids, callback); |
| 41 } |
| 42 |
| 43 void AttachmentStore::ReadMetadata(const AttachmentIdList& ids, |
| 44 const ReadMetadataCallback& callback) { |
| 45 frontend_->ReadMetadata(ids, callback); |
| 46 } |
| 47 |
| 48 void AttachmentStore::ReadAllMetadata(const ReadMetadataCallback& callback) { |
| 49 frontend_->ReadAllMetadata(referrer_, callback); |
| 50 } |
| 51 |
| 52 scoped_ptr<AttachmentStore> AttachmentStore::CreateAttachmentStoreForSync() { |
| 53 scoped_ptr<AttachmentStore> attachment_store( |
| 54 new AttachmentStore(frontend_, SYNC)); |
| 55 return attachment_store.Pass(); |
| 56 } |
| 57 |
| 58 scoped_ptr<AttachmentStore> AttachmentStore::CreateInMemoryStore() { |
| 23 // Both frontend and backend of attachment store will live on current thread. | 59 // Both frontend and backend of attachment store will live on current thread. |
| 24 scoped_refptr<base::SingleThreadTaskRunner> runner; | 60 scoped_refptr<base::SingleThreadTaskRunner> runner; |
| 25 if (base::ThreadTaskRunnerHandle::IsSet()) { | 61 if (base::ThreadTaskRunnerHandle::IsSet()) { |
| 26 runner = base::ThreadTaskRunnerHandle::Get(); | 62 runner = base::ThreadTaskRunnerHandle::Get(); |
| 27 } else { | 63 } else { |
| 28 // Dummy runner for tests that don't have MessageLoop. | 64 // Dummy runner for tests that don't have MessageLoop. |
| 29 base::MessageLoop loop; | 65 base::MessageLoop loop; |
| 30 // This works because |runner| takes a ref to the proxy. | 66 // This works because |runner| takes a ref to the proxy. |
| 31 runner = base::ThreadTaskRunnerHandle::Get(); | 67 runner = base::ThreadTaskRunnerHandle::Get(); |
| 32 } | 68 } |
| 33 scoped_ptr<AttachmentStoreBackend> backend( | 69 scoped_ptr<AttachmentStoreBackend> backend( |
| 34 new InMemoryAttachmentStore(runner)); | 70 new InMemoryAttachmentStore(runner)); |
| 35 return scoped_refptr<AttachmentStore>( | 71 scoped_refptr<AttachmentStoreFrontend> frontend( |
| 36 new AttachmentStoreHandle(backend.Pass(), runner)); | 72 new AttachmentStoreFrontend(backend.Pass(), runner)); |
| 73 scoped_ptr<AttachmentStore> attachment_store( |
| 74 new AttachmentStore(frontend, MODEL_TYPE)); |
| 75 return attachment_store.Pass(); |
| 37 } | 76 } |
| 38 | 77 |
| 39 scoped_refptr<AttachmentStore> AttachmentStore::CreateOnDiskStore( | 78 scoped_ptr<AttachmentStore> AttachmentStore::CreateOnDiskStore( |
| 40 const base::FilePath& path, | 79 const base::FilePath& path, |
| 41 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner, | 80 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner, |
| 42 const InitCallback& callback) { | 81 const InitCallback& callback) { |
| 43 scoped_ptr<OnDiskAttachmentStore> backend( | 82 scoped_ptr<OnDiskAttachmentStore> backend( |
| 44 new OnDiskAttachmentStore(base::ThreadTaskRunnerHandle::Get(), path)); | 83 new OnDiskAttachmentStore(base::ThreadTaskRunnerHandle::Get(), path)); |
| 45 | 84 |
| 46 scoped_refptr<AttachmentStore> attachment_store = | 85 scoped_refptr<AttachmentStoreFrontend> frontend = |
| 47 new AttachmentStoreHandle(backend.Pass(), backend_task_runner); | 86 new AttachmentStoreFrontend(backend.Pass(), backend_task_runner); |
| 48 attachment_store->Init(callback); | 87 scoped_ptr<AttachmentStore> attachment_store( |
| 88 new AttachmentStore(frontend, MODEL_TYPE)); |
| 89 frontend->Init(callback); |
| 49 | 90 |
| 50 return attachment_store; | 91 return attachment_store.Pass(); |
| 51 } | 92 } |
| 52 | 93 |
| 53 AttachmentStoreBackend::AttachmentStoreBackend( | 94 scoped_ptr<AttachmentStore> AttachmentStore::CreateMockStoreForTest( |
| 54 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner) | 95 scoped_ptr<AttachmentStoreBackend> backend) { |
| 55 : callback_task_runner_(callback_task_runner) { | 96 scoped_refptr<base::SingleThreadTaskRunner> runner = |
| 56 } | 97 base::ThreadTaskRunnerHandle::Get(); |
| 57 | 98 scoped_refptr<AttachmentStoreFrontend> attachment_store_frontend( |
| 58 AttachmentStoreBackend::~AttachmentStoreBackend() { | 99 new AttachmentStoreFrontend(backend.Pass(), runner)); |
| 59 } | 100 scoped_ptr<AttachmentStore> attachment_store( |
| 60 | 101 new AttachmentStore(attachment_store_frontend, MODEL_TYPE)); |
| 61 void AttachmentStoreBackend::PostCallback(const base::Closure& callback) { | 102 return attachment_store.Pass(); |
| 62 callback_task_runner_->PostTask(FROM_HERE, callback); | |
| 63 } | 103 } |
| 64 | 104 |
| 65 } // namespace syncer | 105 } // namespace syncer |
| OLD | NEW |