summaryrefslogtreecommitdiffstats
path: root/sync/internal_api/public/attachments/attachment_service_proxy.h
blob: 107576b5c8e4af9c50dc062e4c8c8f6b2a436b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_
#define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h"
#include "base/task_runner.h"
#include "sync/api/attachments/attachment.h"
#include "sync/base/sync_export.h"
#include "sync/internal_api/public/attachments/attachment_service.h"

namespace syncer {

// AttachmentServiceProxy wraps an AttachmentService allowing multiple threads
// to share the wrapped AttachmentService and invoke its methods in the
// appropriate thread.
//
// Callbacks passed to methods on this class will be invoked in the same thread
// from which the method was called.
//
// This class does not own its wrapped AttachmentService object.  This class
// holds a WeakPtr to the wrapped object.  Once the the wrapped object is
// destroyed, method calls on this object will be no-ops.
//
// Users of this class should take care to destroy the wrapped object on the
// correct thread (wrapped_task_runner).
//
// This class is thread-safe and is designed to be passed by const-ref.
class SYNC_EXPORT AttachmentServiceProxy : public AttachmentService {
 public:
  // Default copy and assignment are welcome.

  // Construct an invalid AttachmentServiceProxy.
  AttachmentServiceProxy();

  // Construct an AttachmentServiceProxy that forwards calls to |wrapped| on the
  // |wrapped_task_runner| thread.
  //
  // Note, this object does not own |wrapped|.  When |wrapped| is destroyed,
  // calls to this object become no-ops.
  AttachmentServiceProxy(
      const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
      const base::WeakPtr<syncer::AttachmentService>& wrapped);

  ~AttachmentServiceProxy() override;

  void GetOrDownloadAttachments(const AttachmentIdList& attachment_ids,
                                const GetOrDownloadCallback& callback) override;
  void UploadAttachments(const AttachmentIdList& attachment_ids) override;

 protected:
  // Core does the work of proxying calls to AttachmentService methods from one
  // thread to another so AttachmentServiceProxy can be an easy-to-use,
  // non-ref-counted A ref-counted class.
  //
  // Callback from AttachmentService are proxied back using free functions
  // defined in the .cc file (ProxyFooCallback functions).
  //
  // Core is ref-counted because we want to allow AttachmentServiceProxy to be
  // copy-constructable while allowing for different implementations of Core
  // (e.g. one type of core might own the wrapped AttachmentService).
  //
  // Calls to objects of this class become no-ops once its wrapped object is
  // destroyed.
  class SYNC_EXPORT Core : public AttachmentService,
                           public base::RefCountedThreadSafe<Core> {
   public:
    // Construct an AttachmentServiceProxyCore that forwards calls to |wrapped|.
    explicit Core(const base::WeakPtr<syncer::AttachmentService>& wrapped);

    // AttachmentService implementation.
    void GetOrDownloadAttachments(
        const AttachmentIdList& attachment_ids,
        const GetOrDownloadCallback& callback) override;
    void UploadAttachments(const AttachmentIdList& attachment_ids) override;

   protected:
    ~Core() override;

   private:
    friend class base::RefCountedThreadSafe<Core>;

    base::WeakPtr<AttachmentService> wrapped_;

    DISALLOW_COPY_AND_ASSIGN(Core);
  };

  // Used in tests to create an AttachmentServiceProxy with a custom Core.
  AttachmentServiceProxy(
      const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
      const scoped_refptr<Core>& core);

 private:
  scoped_refptr<base::SequencedTaskRunner> wrapped_task_runner_;
  scoped_refptr<Core> core_;
};

}  // namespace syncer

#endif  // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_