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