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 const { |
| 54 scoped_ptr<AttachmentStore> attachment_store( |
| 55 new AttachmentStore(frontend_, SYNC)); |
| 56 return attachment_store.Pass(); |
| 57 } |
| 58 |
| 59 scoped_ptr<AttachmentStore> AttachmentStore::CreateInMemoryStore() { |
23 // Both frontend and backend of attachment store will live on current thread. | 60 // Both frontend and backend of attachment store will live on current thread. |
24 scoped_refptr<base::SingleThreadTaskRunner> runner; | 61 scoped_refptr<base::SingleThreadTaskRunner> runner; |
25 if (base::ThreadTaskRunnerHandle::IsSet()) { | 62 if (base::ThreadTaskRunnerHandle::IsSet()) { |
26 runner = base::ThreadTaskRunnerHandle::Get(); | 63 runner = base::ThreadTaskRunnerHandle::Get(); |
27 } else { | 64 } else { |
28 // Dummy runner for tests that don't have MessageLoop. | 65 // Dummy runner for tests that don't have MessageLoop. |
29 base::MessageLoop loop; | 66 base::MessageLoop loop; |
30 // This works because |runner| takes a ref to the proxy. | 67 // This works because |runner| takes a ref to the proxy. |
31 runner = base::ThreadTaskRunnerHandle::Get(); | 68 runner = base::ThreadTaskRunnerHandle::Get(); |
32 } | 69 } |
33 scoped_ptr<AttachmentStoreBackend> backend( | 70 scoped_ptr<AttachmentStoreBackend> backend( |
34 new InMemoryAttachmentStore(runner)); | 71 new InMemoryAttachmentStore(runner)); |
35 return scoped_refptr<AttachmentStore>( | 72 scoped_refptr<AttachmentStoreFrontend> frontend( |
36 new AttachmentStoreHandle(backend.Pass(), runner)); | 73 new AttachmentStoreFrontend(backend.Pass(), runner)); |
| 74 scoped_ptr<AttachmentStore> attachment_store( |
| 75 new AttachmentStore(frontend, MODEL_TYPE)); |
| 76 return attachment_store.Pass(); |
37 } | 77 } |
38 | 78 |
39 scoped_refptr<AttachmentStore> AttachmentStore::CreateOnDiskStore( | 79 scoped_ptr<AttachmentStore> AttachmentStore::CreateOnDiskStore( |
40 const base::FilePath& path, | 80 const base::FilePath& path, |
41 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner, | 81 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner, |
42 const InitCallback& callback) { | 82 const InitCallback& callback) { |
43 scoped_ptr<OnDiskAttachmentStore> backend( | 83 scoped_ptr<OnDiskAttachmentStore> backend( |
44 new OnDiskAttachmentStore(base::ThreadTaskRunnerHandle::Get(), path)); | 84 new OnDiskAttachmentStore(base::ThreadTaskRunnerHandle::Get(), path)); |
45 | 85 |
46 scoped_refptr<AttachmentStore> attachment_store = | 86 scoped_refptr<AttachmentStoreFrontend> frontend = |
47 new AttachmentStoreHandle(backend.Pass(), backend_task_runner); | 87 new AttachmentStoreFrontend(backend.Pass(), backend_task_runner); |
48 attachment_store->Init(callback); | 88 scoped_ptr<AttachmentStore> attachment_store( |
| 89 new AttachmentStore(frontend, MODEL_TYPE)); |
| 90 frontend->Init(callback); |
49 | 91 |
50 return attachment_store; | 92 return attachment_store.Pass(); |
51 } | 93 } |
52 | 94 |
53 AttachmentStoreBackend::AttachmentStoreBackend( | 95 scoped_ptr<AttachmentStore> AttachmentStore::CreateMockStoreForTest( |
54 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner) | 96 scoped_ptr<AttachmentStoreBackend> backend) { |
55 : callback_task_runner_(callback_task_runner) { | 97 scoped_refptr<base::SingleThreadTaskRunner> runner = |
56 } | 98 base::ThreadTaskRunnerHandle::Get(); |
57 | 99 scoped_refptr<AttachmentStoreFrontend> attachment_store_frontend( |
58 AttachmentStoreBackend::~AttachmentStoreBackend() { | 100 new AttachmentStoreFrontend(backend.Pass(), runner)); |
59 } | 101 scoped_ptr<AttachmentStore> attachment_store( |
60 | 102 new AttachmentStore(attachment_store_frontend, MODEL_TYPE)); |
61 void AttachmentStoreBackend::PostCallback(const base::Closure& callback) { | 103 return attachment_store.Pass(); |
62 callback_task_runner_->PostTask(FROM_HERE, callback); | |
63 } | 104 } |
64 | 105 |
65 } // namespace syncer | 106 } // namespace syncer |
OLD | NEW |