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/internal_api/public/attachments/attachment_service_proxy.h"
6
7#include "base/bind.h"
8#include "base/message_loop/message_loop.h"
9#include "base/thread_task_runner_handle.h"
10
11namespace syncer {
12
13namespace {
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.
20void 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.
31void 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}  // namespace
39
40AttachmentServiceProxy::AttachmentServiceProxy() {
41}
42
43AttachmentServiceProxy::AttachmentServiceProxy(
44    const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
45    const base::WeakPtr<syncer::AttachmentService>& wrapped)
46    : wrapped_task_runner_(wrapped_task_runner), core_(new Core(wrapped)) {
47  DCHECK(wrapped_task_runner_.get());
48}
49
50AttachmentServiceProxy::AttachmentServiceProxy(
51    const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
52    const scoped_refptr<Core>& core)
53    : wrapped_task_runner_(wrapped_task_runner), core_(core) {
54  DCHECK(wrapped_task_runner_.get());
55  DCHECK(core_.get());
56}
57
58AttachmentServiceProxy::~AttachmentServiceProxy() {
59}
60
61AttachmentStore* AttachmentServiceProxy::GetStore() {
62  return NULL;
63}
64
65void AttachmentServiceProxy::GetOrDownloadAttachments(
66    const AttachmentIdList& attachment_ids,
67    const GetOrDownloadCallback& callback) {
68  DCHECK(wrapped_task_runner_.get());
69  GetOrDownloadCallback proxy_callback =
70      base::Bind(&ProxyGetOrDownloadCallback,
71                 base::ThreadTaskRunnerHandle::Get(),
72                 callback);
73  wrapped_task_runner_->PostTask(
74      FROM_HERE,
75      base::Bind(&AttachmentService::GetOrDownloadAttachments,
76                 core_,
77                 attachment_ids,
78                 proxy_callback));
79}
80
81void AttachmentServiceProxy::DropAttachments(
82    const AttachmentIdList& attachment_ids,
83    const DropCallback& callback) {
84  DCHECK(wrapped_task_runner_.get());
85  DropCallback proxy_callback = base::Bind(
86      &ProxyDropCallback, base::ThreadTaskRunnerHandle::Get(), callback);
87  wrapped_task_runner_->PostTask(FROM_HERE,
88                                 base::Bind(&AttachmentService::DropAttachments,
89                                            core_,
90                                            attachment_ids,
91                                            proxy_callback));
92}
93
94void AttachmentServiceProxy::UploadAttachments(
95    const AttachmentIdSet& attachment_ids) {
96  DCHECK(wrapped_task_runner_.get());
97  wrapped_task_runner_->PostTask(
98      FROM_HERE,
99      base::Bind(&AttachmentService::UploadAttachments, core_, attachment_ids));
100}
101
102AttachmentServiceProxy::Core::Core(
103    const base::WeakPtr<syncer::AttachmentService>& wrapped)
104    : wrapped_(wrapped) {
105}
106
107AttachmentServiceProxy::Core::~Core() {
108}
109
110AttachmentStore* AttachmentServiceProxy::Core::GetStore() {
111  return NULL;
112}
113
114void AttachmentServiceProxy::Core::GetOrDownloadAttachments(
115    const AttachmentIdList& attachment_ids,
116    const GetOrDownloadCallback& callback) {
117  if (!wrapped_) {
118    return;
119  }
120  wrapped_->GetOrDownloadAttachments(attachment_ids, callback);
121}
122
123void AttachmentServiceProxy::Core::DropAttachments(
124    const AttachmentIdList& attachment_ids,
125    const DropCallback& callback) {
126  if (!wrapped_) {
127    return;
128  }
129  wrapped_->DropAttachments(attachment_ids, callback);
130}
131
132void AttachmentServiceProxy::Core::UploadAttachments(
133    const AttachmentIdSet& attachment_ids) {
134  if (!wrapped_) {
135    return;
136  }
137  wrapped_->UploadAttachments(attachment_ids);
138}
139
140}  // namespace syncer
141