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