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
3 // found in the LICENSE file.
5 #include "sync/api/attachments/attachment_store.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/thread_task_runner_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"
15 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h"
21 void NoOpDropCallback(const AttachmentStore::Result
& result
) {
25 AttachmentStore::AttachmentStore(
26 const scoped_refptr
<AttachmentStoreFrontend
>& frontend
,
28 : frontend_(frontend
), component_(component
) {
31 AttachmentStore::~AttachmentStore() {
34 void AttachmentStore::Read(const AttachmentIdList
& ids
,
35 const ReadCallback
& callback
) {
36 frontend_
->Read(component_
, ids
, callback
);
39 void AttachmentStore::Write(const AttachmentList
& attachments
,
40 const WriteCallback
& callback
) {
41 frontend_
->Write(component_
, attachments
, callback
);
44 void AttachmentStore::Drop(const AttachmentIdList
& ids
,
45 const DropCallback
& callback
) {
46 frontend_
->DropReference(component_
, ids
, callback
);
49 void AttachmentStore::ReadMetadataById(const AttachmentIdList
& ids
,
50 const ReadMetadataCallback
& callback
) {
51 frontend_
->ReadMetadataById(component_
, ids
, callback
);
54 void AttachmentStore::ReadMetadata(const ReadMetadataCallback
& callback
) {
55 frontend_
->ReadMetadata(component_
, callback
);
58 scoped_ptr
<AttachmentStoreForSync
>
59 AttachmentStore::CreateAttachmentStoreForSync() const {
60 scoped_ptr
<AttachmentStoreForSync
> attachment_store_for_sync(
61 new AttachmentStoreForSync(frontend_
, component_
, SYNC
));
62 return attachment_store_for_sync
.Pass();
65 scoped_ptr
<AttachmentStore
> AttachmentStore::CreateInMemoryStore() {
66 // Both frontend and backend of attachment store will live on current thread.
67 scoped_refptr
<base::SingleThreadTaskRunner
> runner
;
68 if (base::ThreadTaskRunnerHandle::IsSet()) {
69 runner
= base::ThreadTaskRunnerHandle::Get();
71 // Dummy runner for tests that don't have MessageLoop.
72 base::MessageLoop loop
;
73 // This works because |runner| takes a ref to the proxy.
74 runner
= base::ThreadTaskRunnerHandle::Get();
76 scoped_ptr
<AttachmentStoreBackend
> backend(
77 new InMemoryAttachmentStore(runner
));
78 scoped_refptr
<AttachmentStoreFrontend
> frontend(
79 new AttachmentStoreFrontend(backend
.Pass(), runner
));
80 scoped_ptr
<AttachmentStore
> attachment_store(
81 new AttachmentStore(frontend
, MODEL_TYPE
));
82 return attachment_store
.Pass();
85 scoped_ptr
<AttachmentStore
> AttachmentStore::CreateOnDiskStore(
86 const base::FilePath
& path
,
87 const scoped_refptr
<base::SequencedTaskRunner
>& backend_task_runner
,
88 const InitCallback
& callback
) {
89 scoped_ptr
<OnDiskAttachmentStore
> backend(
90 new OnDiskAttachmentStore(base::ThreadTaskRunnerHandle::Get(), path
));
92 scoped_refptr
<AttachmentStoreFrontend
> frontend
=
93 new AttachmentStoreFrontend(backend
.Pass(), backend_task_runner
);
94 scoped_ptr
<AttachmentStore
> attachment_store(
95 new AttachmentStore(frontend
, MODEL_TYPE
));
96 frontend
->Init(callback
);
98 return attachment_store
.Pass();
101 scoped_ptr
<AttachmentStore
> AttachmentStore::CreateMockStoreForTest(
102 scoped_ptr
<AttachmentStoreBackend
> backend
) {
103 scoped_refptr
<base::SingleThreadTaskRunner
> runner
=
104 base::ThreadTaskRunnerHandle::Get();
105 scoped_refptr
<AttachmentStoreFrontend
> attachment_store_frontend(
106 new AttachmentStoreFrontend(backend
.Pass(), runner
));
107 scoped_ptr
<AttachmentStore
> attachment_store(
108 new AttachmentStore(attachment_store_frontend
, MODEL_TYPE
));
109 return attachment_store
.Pass();
112 AttachmentStoreForSync::AttachmentStoreForSync(
113 const scoped_refptr
<AttachmentStoreFrontend
>& frontend
,
114 Component consumer_component
,
115 Component sync_component
)
116 : AttachmentStore(frontend
, consumer_component
),
117 sync_component_(sync_component
) {
120 AttachmentStoreForSync::~AttachmentStoreForSync() {
123 void AttachmentStoreForSync::SetSyncReference(const AttachmentIdList
& ids
) {
124 frontend()->SetReference(sync_component_
, ids
);
127 void AttachmentStoreForSync::SetModelTypeReference(
128 const AttachmentIdList
& ids
) {
129 frontend()->SetReference(component(), ids
);
132 void AttachmentStoreForSync::DropSyncReference(const AttachmentIdList
& ids
) {
133 frontend()->DropReference(sync_component_
, ids
,
134 base::Bind(&NoOpDropCallback
));
137 void AttachmentStoreForSync::ReadMetadataForSync(
138 const ReadMetadataCallback
& callback
) {
139 frontend()->ReadMetadata(sync_component_
, callback
);
142 } // namespace syncer