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