OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/single_thread_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/sequenced_task_runner_handle.h" |
19 #include "mojo/public/cpp/bindings/associated_group.h" | 19 #include "mojo/public/cpp/bindings/associated_group.h" |
20 #include "mojo/public/cpp/bindings/associated_group_controller.h" | 20 #include "mojo/public/cpp/bindings/associated_group_controller.h" |
21 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 21 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
22 #include "mojo/public/cpp/bindings/connection_error_callback.h" | 22 #include "mojo/public/cpp/bindings/connection_error_callback.h" |
23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" | 23 #include "mojo/public/cpp/bindings/interface_endpoint_client.h" |
24 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" | 24 #include "mojo/public/cpp/bindings/lib/control_message_proxy.h" |
25 #include "mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h" | 25 #include "mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h" |
26 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" | 26 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" |
27 | 27 |
28 namespace mojo { | 28 namespace mojo { |
29 | 29 |
30 class MessageReceiver; | 30 class MessageReceiver; |
31 | 31 |
32 // Represents the implementation side of an associated interface. It is similar | 32 // Represents the implementation side of an associated interface. It is similar |
33 // to Binding, except that it doesn't own a message pipe handle. | 33 // to Binding, except that it doesn't own a message pipe handle. |
34 // | 34 // |
35 // When you bind this class to a request, optionally you can specify a | 35 // When you bind this class to a request, optionally you can specify a |
36 // base::SingleThreadTaskRunner. This task runner must belong to the same | 36 // base::SequencedTaskRunner. This task runner must belong to the same |
37 // thread. It will be used to dispatch incoming method calls and connection | 37 // thread. It will be used to dispatch incoming method calls and connection |
38 // error notification. It is useful when you attach multiple task runners to a | 38 // error notification. It is useful when you attach multiple task runners to a |
39 // single thread for the purposes of task scheduling. Please note that incoming | 39 // single thread for the purposes of task scheduling. Please note that incoming |
40 // synchrounous method calls may not be run from this task runner, when they | 40 // synchrounous method calls may not be run from this task runner, when they |
41 // reenter outgoing synchrounous calls on the same thread. | 41 // reenter outgoing synchrounous calls on the same thread. |
42 template <typename Interface, | 42 template <typename Interface, |
43 typename ImplRefTraits = RawPtrImplRefTraits<Interface>> | 43 typename ImplRefTraits = RawPtrImplRefTraits<Interface>> |
44 class AssociatedBinding { | 44 class AssociatedBinding { |
45 public: | 45 public: |
46 using ImplPointerType = typename ImplRefTraits::PointerType; | 46 using ImplPointerType = typename ImplRefTraits::PointerType; |
47 | 47 |
48 // Constructs an incomplete associated binding that will use the | 48 // Constructs an incomplete associated binding that will use the |
49 // implementation |impl|. It may be completed with a subsequent call to the | 49 // implementation |impl|. It may be completed with a subsequent call to the |
50 // |Bind| method. Does not take ownership of |impl|, which must outlive this | 50 // |Bind| method. Does not take ownership of |impl|, which must outlive this |
51 // object. | 51 // object. |
52 explicit AssociatedBinding(ImplPointerType impl) { stub_.set_sink(impl); } | 52 explicit AssociatedBinding(ImplPointerType impl) { stub_.set_sink(impl); } |
53 | 53 |
54 // Constructs a completed associated binding of |impl|. The output |ptr_info| | 54 // Constructs a completed associated binding of |impl|. The output |ptr_info| |
55 // should be passed through the message pipe endpoint referred to by | 55 // should be passed through the message pipe endpoint referred to by |
56 // |associated_group| to setup the corresponding asssociated interface | 56 // |associated_group| to setup the corresponding asssociated interface |
57 // pointer. |impl| must outlive this object. | 57 // pointer. |impl| must outlive this object. |
58 AssociatedBinding(ImplPointerType impl, | 58 AssociatedBinding(ImplPointerType impl, |
59 AssociatedInterfacePtrInfo<Interface>* ptr_info, | 59 AssociatedInterfacePtrInfo<Interface>* ptr_info, |
60 AssociatedGroup* associated_group, | 60 AssociatedGroup* associated_group, |
61 scoped_refptr<base::SingleThreadTaskRunner> runner = | 61 scoped_refptr<base::SequencedTaskRunner> runner = |
62 base::ThreadTaskRunnerHandle::Get()) | 62 base::SequencedTaskRunnerHandle::Get()) |
63 : AssociatedBinding(std::move(impl)) { | 63 : AssociatedBinding(std::move(impl)) { |
64 Bind(ptr_info, associated_group, std::move(runner)); | 64 Bind(ptr_info, associated_group, std::move(runner)); |
65 } | 65 } |
66 | 66 |
67 // Constructs a completed associated binding of |impl|. |impl| must outlive | 67 // Constructs a completed associated binding of |impl|. |impl| must outlive |
68 // the binding. | 68 // the binding. |
69 AssociatedBinding(ImplPointerType impl, | 69 AssociatedBinding(ImplPointerType impl, |
70 AssociatedInterfaceRequest<Interface> request, | 70 AssociatedInterfaceRequest<Interface> request, |
71 scoped_refptr<base::SingleThreadTaskRunner> runner = | 71 scoped_refptr<base::SequencedTaskRunner> runner = |
72 base::ThreadTaskRunnerHandle::Get()) | 72 base::SequencedTaskRunnerHandle::Get()) |
73 : AssociatedBinding(std::move(impl)) { | 73 : AssociatedBinding(std::move(impl)) { |
74 Bind(std::move(request), std::move(runner)); | 74 Bind(std::move(request), std::move(runner)); |
75 } | 75 } |
76 | 76 |
77 ~AssociatedBinding() {} | 77 ~AssociatedBinding() {} |
78 | 78 |
79 // Creates an associated inteface and sets up this object as the | 79 // Creates an associated inteface and sets up this object as the |
80 // implementation side. The output |ptr_info| should be passed through the | 80 // implementation side. The output |ptr_info| should be passed through the |
81 // message pipe endpoint referred to by |associated_group| to setup the | 81 // message pipe endpoint referred to by |associated_group| to setup the |
82 // corresponding asssociated interface pointer. | 82 // corresponding asssociated interface pointer. |
83 void Bind(AssociatedInterfacePtrInfo<Interface>* ptr_info, | 83 void Bind(AssociatedInterfacePtrInfo<Interface>* ptr_info, |
84 AssociatedGroup* associated_group, | 84 AssociatedGroup* associated_group, |
85 scoped_refptr<base::SingleThreadTaskRunner> runner = | 85 scoped_refptr<base::SequencedTaskRunner> runner = |
86 base::ThreadTaskRunnerHandle::Get()) { | 86 base::SequencedTaskRunnerHandle::Get()) { |
87 AssociatedInterfaceRequest<Interface> request; | 87 AssociatedInterfaceRequest<Interface> request; |
88 associated_group->CreateAssociatedInterface(AssociatedGroup::WILL_PASS_PTR, | 88 associated_group->CreateAssociatedInterface(AssociatedGroup::WILL_PASS_PTR, |
89 ptr_info, &request); | 89 ptr_info, &request); |
90 Bind(std::move(request), std::move(runner)); | 90 Bind(std::move(request), std::move(runner)); |
91 } | 91 } |
92 | 92 |
93 // Sets up this object as the implementation side of an associated interface. | 93 // Sets up this object as the implementation side of an associated interface. |
94 void Bind(AssociatedInterfaceRequest<Interface> request, | 94 void Bind(AssociatedInterfaceRequest<Interface> request, |
95 scoped_refptr<base::SingleThreadTaskRunner> runner = | 95 scoped_refptr<base::SequencedTaskRunner> runner = |
96 base::ThreadTaskRunnerHandle::Get()) { | 96 base::SequencedTaskRunnerHandle::Get()) { |
97 ScopedInterfaceEndpointHandle handle = request.PassHandle(); | 97 ScopedInterfaceEndpointHandle handle = request.PassHandle(); |
98 | 98 |
99 DCHECK(handle.is_local()) | 99 DCHECK(handle.is_local()) |
100 << "The AssociatedInterfaceRequest is supposed to be used at the " | 100 << "The AssociatedInterfaceRequest is supposed to be used at the " |
101 << "other side of the message pipe."; | 101 << "other side of the message pipe."; |
102 | 102 |
103 if (!handle.is_valid() || !handle.is_local()) { | 103 if (!handle.is_valid() || !handle.is_local()) { |
104 endpoint_client_.reset(); | 104 endpoint_client_.reset(); |
105 return; | 105 return; |
106 } | 106 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 private: | 190 private: |
191 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; | 191 std::unique_ptr<InterfaceEndpointClient> endpoint_client_; |
192 typename Interface::template Stub_<ImplRefTraits> stub_; | 192 typename Interface::template Stub_<ImplRefTraits> stub_; |
193 | 193 |
194 DISALLOW_COPY_AND_ASSIGN(AssociatedBinding); | 194 DISALLOW_COPY_AND_ASSIGN(AssociatedBinding); |
195 }; | 195 }; |
196 | 196 |
197 } // namespace mojo | 197 } // namespace mojo |
198 | 198 |
199 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ | 199 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_BINDING_H_ |
OLD | NEW |