attachment_service_proxy.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// found in the LICENSE file.
4e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_
65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_
7e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
8e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/basictypes.h"
9e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/callback.h"
10e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/memory/scoped_ptr.h"
11e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/memory/weak_ptr.h"
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/sequenced_task_runner.h"
13e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/task_runner.h"
14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "sync/api/attachments/attachment.h"
15e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "sync/base/sync_export.h"
165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "sync/internal_api/public/attachments/attachment_service.h"
17e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
18e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochnamespace syncer {
19e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// AttachmentServiceProxy wraps an AttachmentService allowing multiple threads
21a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// to share the wrapped AttachmentService and invoke its methods in the
22a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// appropriate thread.
23e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//
24e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Callbacks passed to methods on this class will be invoked in the same thread
25e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// from which the method was called.
26e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//
27e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// This class does not own its wrapped AttachmentService object.  This class
28e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// holds a WeakPtr to the wrapped object.  Once the the wrapped object is
29e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// destroyed, method calls on this object will be no-ops.
30e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//
31a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Users of this class should take care to destroy the wrapped object on the
32a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// correct thread (wrapped_task_runner).
33e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch//
34a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// This class is thread-safe and is designed to be passed by const-ref.
35e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass SYNC_EXPORT AttachmentServiceProxy : public AttachmentService {
36e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch public:
37e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Default copy and assignment are welcome.
38e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
39e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Construct an invalid AttachmentServiceProxy.
40e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  AttachmentServiceProxy();
41e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
42e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Construct an AttachmentServiceProxy that forwards calls to |wrapped| on the
43e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // |wrapped_task_runner| thread.
44a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  //
45a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Note, this object does not own |wrapped|.  When |wrapped| is destroyed,
46a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // calls to this object become no-ops.
47e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  AttachmentServiceProxy(
48e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
49a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      const base::WeakPtr<syncer::AttachmentService>& wrapped);
50e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
51e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual ~AttachmentServiceProxy();
52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
53e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // AttachmentService implementation.
54a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  virtual void GetOrDownloadAttachments(
55a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      const AttachmentIdList& attachment_ids,
56a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      const GetOrDownloadCallback& callback) OVERRIDE;
57e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual void DropAttachments(const AttachmentIdList& attachment_ids,
58e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                               const DropCallback& callback) OVERRIDE;
59010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  virtual void StoreAttachments(const AttachmentList& attachment,
60010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                const StoreCallback& callback) OVERRIDE;
61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
62a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch protected:
63a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Core does the work of proxying calls to AttachmentService methods from one
64a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // thread to another so AttachmentServiceProxy can be an easy-to-use,
65a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // non-ref-counted A ref-counted class.
66a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  //
67a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Callback from AttachmentService are proxied back using free functions
68a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // defined in the .cc file (ProxyFooCallback functions).
69a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  //
70a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Core is ref-counted because we want to allow AttachmentServiceProxy to be
71a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // copy-constructable while allowing for different implementations of Core
72a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // (e.g. one type of core might own the wrapped AttachmentService).
73a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  //
74a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Calls to objects of this class become no-ops once its wrapped object is
75a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // destroyed.
76a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  class SYNC_EXPORT Core : public AttachmentService,
77a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                           public base::RefCountedThreadSafe<Core> {
78a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch   public:
79a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    // Construct an AttachmentServiceProxyCore that forwards calls to |wrapped|.
80a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    Core(const base::WeakPtr<syncer::AttachmentService>& wrapped);
81a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
82a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    // AttachmentService implementation.
83a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    virtual void GetOrDownloadAttachments(
84a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        const AttachmentIdList& attachment_ids,
85a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        const GetOrDownloadCallback& callback) OVERRIDE;
86a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    virtual void DropAttachments(const AttachmentIdList& attachment_ids,
87a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                 const DropCallback& callback) OVERRIDE;
88010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    virtual void StoreAttachments(const AttachmentList& attachment,
89010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                  const StoreCallback& callback) OVERRIDE;
90a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
91a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch   protected:
92a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    friend class base::RefCountedThreadSafe<Core>;
93a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    virtual ~Core();
94a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
95a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch   private:
96a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    base::WeakPtr<AttachmentService> wrapped_;
97a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
98a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    DISALLOW_COPY_AND_ASSIGN(Core);
99a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  };
100a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
101a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Used in tests to create an AttachmentServiceProxy with a custom Core.
102a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  AttachmentServiceProxy(
103a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
104a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      const scoped_refptr<Core>& core);
105a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
106e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch private:
107e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  scoped_refptr<base::SequencedTaskRunner> wrapped_task_runner_;
108a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_refptr<Core> core_;
109e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch};
110e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
111e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}  // namespace syncer
112e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif  // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_
114