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