• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_
6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_
7 
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/task_runner.h"
14 #include "sync/api/attachments/attachment.h"
15 #include "sync/base/sync_export.h"
16 #include "sync/internal_api/public/attachments/attachment_service.h"
17 
18 namespace syncer {
19 
20 // AttachmentServiceProxy wraps an AttachmentService allowing multiple threads
21 // to share the wrapped AttachmentService and invoke its methods in the
22 // appropriate thread.
23 //
24 // Callbacks passed to methods on this class will be invoked in the same thread
25 // from which the method was called.
26 //
27 // This class does not own its wrapped AttachmentService object.  This class
28 // holds a WeakPtr to the wrapped object.  Once the the wrapped object is
29 // destroyed, method calls on this object will be no-ops.
30 //
31 // Users of this class should take care to destroy the wrapped object on the
32 // correct thread (wrapped_task_runner).
33 //
34 // This class is thread-safe and is designed to be passed by const-ref.
35 class SYNC_EXPORT AttachmentServiceProxy : public AttachmentService {
36  public:
37   // Default copy and assignment are welcome.
38 
39   // Construct an invalid AttachmentServiceProxy.
40   AttachmentServiceProxy();
41 
42   // Construct an AttachmentServiceProxy that forwards calls to |wrapped| on the
43   // |wrapped_task_runner| thread.
44   //
45   // Note, this object does not own |wrapped|.  When |wrapped| is destroyed,
46   // calls to this object become no-ops.
47   AttachmentServiceProxy(
48       const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
49       const base::WeakPtr<syncer::AttachmentService>& wrapped);
50 
51   virtual ~AttachmentServiceProxy();
52 
53   // AttachmentService implementation.
54   //
55   // GetStore always returns NULL.
56   virtual AttachmentStore* GetStore() OVERRIDE;
57   virtual void GetOrDownloadAttachments(
58       const AttachmentIdList& attachment_ids,
59       const GetOrDownloadCallback& callback) OVERRIDE;
60   virtual void DropAttachments(const AttachmentIdList& attachment_ids,
61                                const DropCallback& callback) OVERRIDE;
62   virtual void UploadAttachments(
63       const AttachmentIdSet& attachment_ids) OVERRIDE;
64 
65  protected:
66   // Core does the work of proxying calls to AttachmentService methods from one
67   // thread to another so AttachmentServiceProxy can be an easy-to-use,
68   // non-ref-counted A ref-counted class.
69   //
70   // Callback from AttachmentService are proxied back using free functions
71   // defined in the .cc file (ProxyFooCallback functions).
72   //
73   // Core is ref-counted because we want to allow AttachmentServiceProxy to be
74   // copy-constructable while allowing for different implementations of Core
75   // (e.g. one type of core might own the wrapped AttachmentService).
76   //
77   // Calls to objects of this class become no-ops once its wrapped object is
78   // destroyed.
79   class SYNC_EXPORT Core : public AttachmentService,
80                            public base::RefCountedThreadSafe<Core> {
81    public:
82     // Construct an AttachmentServiceProxyCore that forwards calls to |wrapped|.
83     Core(const base::WeakPtr<syncer::AttachmentService>& wrapped);
84 
85     // AttachmentService implementation.
86     virtual AttachmentStore* GetStore() OVERRIDE;
87     virtual void GetOrDownloadAttachments(
88         const AttachmentIdList& attachment_ids,
89         const GetOrDownloadCallback& callback) OVERRIDE;
90     virtual void DropAttachments(const AttachmentIdList& attachment_ids,
91                                  const DropCallback& callback) OVERRIDE;
92     virtual void UploadAttachments(
93         const AttachmentIdSet& attachment_ids) OVERRIDE;
94 
95    protected:
96     friend class base::RefCountedThreadSafe<Core>;
97     virtual ~Core();
98 
99    private:
100     base::WeakPtr<AttachmentService> wrapped_;
101 
102     DISALLOW_COPY_AND_ASSIGN(Core);
103   };
104 
105   // Used in tests to create an AttachmentServiceProxy with a custom Core.
106   AttachmentServiceProxy(
107       const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
108       const scoped_refptr<Core>& core);
109 
110  private:
111   scoped_refptr<base::SequencedTaskRunner> wrapped_task_runner_;
112   scoped_refptr<Core> core_;
113 };
114 
115 }  // namespace syncer
116 
117 #endif  // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_
118