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.
4
5 #include "sync/api/attachments/attachment_service_proxy.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "sync/api/sync_data.h"
10
11 namespace syncer {
12
13 namespace {
14
15 // These ProxyFooCallback functions are used to invoke a callback in a specific
16 // thread.
17
18 // Invokes |callback| with |result| and |attachments| in the |task_runner|
19 // thread.
ProxyGetOrDownloadCallback(const scoped_refptr<base::SequencedTaskRunner> & task_runner,const AttachmentService::GetOrDownloadCallback & callback,const AttachmentService::GetOrDownloadResult & result,scoped_ptr<AttachmentMap> attachments)20 void ProxyGetOrDownloadCallback(
21 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
22 const AttachmentService::GetOrDownloadCallback& callback,
23 const AttachmentService::GetOrDownloadResult& result,
24 scoped_ptr<AttachmentMap> attachments) {
25 task_runner->PostTask(
26 FROM_HERE, base::Bind(callback, result, base::Passed(&attachments)));
27 }
28
29 // Invokes |callback| with |result| and |attachments| in the |task_runner|
30 // thread.
ProxyDropCallback(const scoped_refptr<base::SequencedTaskRunner> & task_runner,const AttachmentService::DropCallback & callback,const AttachmentService::DropResult & result)31 void ProxyDropCallback(
32 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
33 const AttachmentService::DropCallback& callback,
34 const AttachmentService::DropResult& result) {
35 task_runner->PostTask(FROM_HERE, base::Bind(callback, result));
36 }
37
38 // Invokes |callback| with |result| and |attachments| in the |task_runner|
39 // thread.
ProxyStoreCallback(const scoped_refptr<base::SequencedTaskRunner> & task_runner,const AttachmentService::StoreCallback & callback,const AttachmentService::StoreResult & result)40 void ProxyStoreCallback(
41 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
42 const AttachmentService::StoreCallback& callback,
43 const AttachmentService::StoreResult& result) {
44 task_runner->PostTask(FROM_HERE, base::Bind(callback, result));
45 }
46
47 } // namespace
48
AttachmentServiceProxy()49 AttachmentServiceProxy::AttachmentServiceProxy() {
50 }
51
AttachmentServiceProxy(const scoped_refptr<base::SequencedTaskRunner> & wrapped_task_runner,const base::WeakPtr<syncer::AttachmentService> & wrapped)52 AttachmentServiceProxy::AttachmentServiceProxy(
53 const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
54 const base::WeakPtr<syncer::AttachmentService>& wrapped)
55 : wrapped_task_runner_(wrapped_task_runner), core_(new Core(wrapped)) {
56 DCHECK(wrapped_task_runner_);
57 }
58
AttachmentServiceProxy(const scoped_refptr<base::SequencedTaskRunner> & wrapped_task_runner,const scoped_refptr<Core> & core)59 AttachmentServiceProxy::AttachmentServiceProxy(
60 const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
61 const scoped_refptr<Core>& core)
62 : wrapped_task_runner_(wrapped_task_runner), core_(core) {
63 DCHECK(wrapped_task_runner_);
64 DCHECK(core_);
65 }
66
~AttachmentServiceProxy()67 AttachmentServiceProxy::~AttachmentServiceProxy() {
68 }
69
GetOrDownloadAttachments(const AttachmentIdList & attachment_ids,const GetOrDownloadCallback & callback)70 void AttachmentServiceProxy::GetOrDownloadAttachments(
71 const AttachmentIdList& attachment_ids,
72 const GetOrDownloadCallback& callback) {
73 DCHECK(wrapped_task_runner_);
74 GetOrDownloadCallback proxy_callback = base::Bind(
75 &ProxyGetOrDownloadCallback, base::MessageLoopProxy::current(), callback);
76 wrapped_task_runner_->PostTask(
77 FROM_HERE,
78 base::Bind(&AttachmentService::GetOrDownloadAttachments,
79 core_,
80 attachment_ids,
81 proxy_callback));
82 }
83
DropAttachments(const AttachmentIdList & attachment_ids,const DropCallback & callback)84 void AttachmentServiceProxy::DropAttachments(
85 const AttachmentIdList& attachment_ids,
86 const DropCallback& callback) {
87 DCHECK(wrapped_task_runner_);
88 DropCallback proxy_callback = base::Bind(
89 &ProxyDropCallback, base::MessageLoopProxy::current(), callback);
90 wrapped_task_runner_->PostTask(FROM_HERE,
91 base::Bind(&AttachmentService::DropAttachments,
92 core_,
93 attachment_ids,
94 proxy_callback));
95 }
96
StoreAttachments(const AttachmentList & attachments,const StoreCallback & callback)97 void AttachmentServiceProxy::StoreAttachments(const AttachmentList& attachments,
98 const StoreCallback& callback) {
99 DCHECK(wrapped_task_runner_);
100 StoreCallback proxy_callback = base::Bind(
101 &ProxyStoreCallback, base::MessageLoopProxy::current(), callback);
102 wrapped_task_runner_->PostTask(
103 FROM_HERE,
104 base::Bind(&AttachmentService::StoreAttachments,
105 core_,
106 attachments,
107 proxy_callback));
108 }
109
OnSyncDataDelete(const SyncData & sync_data)110 void AttachmentServiceProxy::OnSyncDataDelete(const SyncData& sync_data) {
111 DCHECK(wrapped_task_runner_);
112 wrapped_task_runner_->PostTask(
113 FROM_HERE,
114 base::Bind(&AttachmentService::OnSyncDataDelete, core_, sync_data));
115 }
116
OnSyncDataUpdate(const AttachmentIdList & old_attachment_ids,const SyncData & updated_sync_data)117 void AttachmentServiceProxy::OnSyncDataUpdate(
118 const AttachmentIdList& old_attachment_ids,
119 const SyncData& updated_sync_data) {
120 DCHECK(wrapped_task_runner_);
121 wrapped_task_runner_->PostTask(
122 FROM_HERE,
123 base::Bind(&AttachmentService::OnSyncDataUpdate,
124 core_,
125 old_attachment_ids,
126 updated_sync_data));
127 }
128
Core(const base::WeakPtr<syncer::AttachmentService> & wrapped)129 AttachmentServiceProxy::Core::Core(
130 const base::WeakPtr<syncer::AttachmentService>& wrapped)
131 : wrapped_(wrapped) {
132 }
133
~Core()134 AttachmentServiceProxy::Core::~Core() {
135 }
136
GetOrDownloadAttachments(const AttachmentIdList & attachment_ids,const GetOrDownloadCallback & callback)137 void AttachmentServiceProxy::Core::GetOrDownloadAttachments(
138 const AttachmentIdList& attachment_ids,
139 const GetOrDownloadCallback& callback) {
140 if (!wrapped_) {
141 return;
142 }
143 wrapped_->GetOrDownloadAttachments(attachment_ids, callback);
144 }
145
DropAttachments(const AttachmentIdList & attachment_ids,const DropCallback & callback)146 void AttachmentServiceProxy::Core::DropAttachments(
147 const AttachmentIdList& attachment_ids,
148 const DropCallback& callback) {
149 if (!wrapped_) {
150 return;
151 }
152 wrapped_->DropAttachments(attachment_ids, callback);
153 }
154
StoreAttachments(const AttachmentList & attachments,const StoreCallback & callback)155 void AttachmentServiceProxy::Core::StoreAttachments(
156 const AttachmentList& attachments,
157 const StoreCallback& callback) {
158 if (!wrapped_) {
159 return;
160 }
161 wrapped_->StoreAttachments(attachments, callback);
162 }
163
OnSyncDataDelete(const SyncData & sync_data)164 void AttachmentServiceProxy::Core::OnSyncDataDelete(const SyncData& sync_data) {
165 if (!wrapped_) {
166 return;
167 }
168 wrapped_->OnSyncDataDelete(sync_data);
169 }
170
OnSyncDataUpdate(const AttachmentIdList & old_attachment_ids,const SyncData & updated_sync_data)171 void AttachmentServiceProxy::Core::OnSyncDataUpdate(
172 const AttachmentIdList& old_attachment_ids,
173 const SyncData& updated_sync_data) {
174 if (!wrapped_) {
175 return;
176 }
177 wrapped_->OnSyncDataUpdate(old_attachment_ids, updated_sync_data);
178 }
179
180 } // namespace syncer
181