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