rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 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 MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ |
| 6 | #define MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ |
| 7 | |
yzshen | 24438d2 | 2016-09-13 17:03:53 | [diff] [blame] | 8 | #include <string> |
dcheng | aa6492d | 2015-12-26 04:46:36 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
rockot | a21316a | 2016-06-19 17:08:36 | [diff] [blame] | 11 | #include "base/callback_forward.h" |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 12 | #include "base/macros.h" |
yzshen | f5d4893 | 2016-04-22 23:18:33 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "base/single_thread_task_runner.h" |
gab | 6e4c754 | 2016-05-11 18:11:16 | [diff] [blame] | 15 | #include "base/threading/thread_task_runner_handle.h" |
yzshen | 24438d2 | 2016-09-13 17:03:53 | [diff] [blame] | 16 | #include "mojo/public/cpp/bindings/connection_error_callback.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 17 | #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 18 | #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
| 19 | #include "mojo/public/cpp/bindings/interface_request.h" |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 20 | #include "mojo/public/cpp/bindings/lib/binding_state.h" |
rockot | 8e2a5d2 | 2016-10-10 06:38:10 | [diff] [blame] | 21 | #include "mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h" |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 22 | #include "mojo/public/cpp/system/core.h" |
| 23 | |
| 24 | namespace mojo { |
| 25 | |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 26 | class MessageReceiver; |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 27 | |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 28 | // Represents the binding of an interface implementation to a message pipe. |
| 29 | // When the |Binding| object is destroyed, the binding between the message pipe |
| 30 | // and the interface is torn down and the message pipe is closed, leaving the |
| 31 | // interface implementation in an unbound state. |
| 32 | // |
| 33 | // Example: |
| 34 | // |
| 35 | // #include "foo.mojom.h" |
| 36 | // |
| 37 | // class FooImpl : public Foo { |
| 38 | // public: |
| 39 | // explicit FooImpl(InterfaceRequest<Foo> request) |
yzshen | beda23123 | 2016-03-04 20:15:39 | [diff] [blame] | 40 | // : binding_(this, std::move(request)) {} |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 41 | // |
| 42 | // // Foo implementation here. |
| 43 | // |
| 44 | // private: |
| 45 | // Binding<Foo> binding_; |
| 46 | // }; |
| 47 | // |
| 48 | // class MyFooFactory : public InterfaceFactory<Foo> { |
| 49 | // public: |
| 50 | // void Create(..., InterfaceRequest<Foo> request) override { |
yzshen | beda23123 | 2016-03-04 20:15:39 | [diff] [blame] | 51 | // auto f = new FooImpl(std::move(request)); |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 52 | // // Do something to manage the lifetime of |f|. Use StrongBinding<> to |
| 53 | // // delete FooImpl on connection errors. |
| 54 | // } |
| 55 | // }; |
| 56 | // |
amistry | 913e9f1 | 2016-01-06 22:12:03 | [diff] [blame] | 57 | // This class is thread hostile while bound to a message pipe. All calls to this |
| 58 | // class must be from the thread that bound it. The interface implementation's |
| 59 | // methods will be called from the thread that bound this. If a Binding is not |
| 60 | // bound to a message pipe, it may be bound or destroyed on any thread. |
yzshen | f5d4893 | 2016-04-22 23:18:33 | [diff] [blame] | 61 | // |
| 62 | // When you bind this class to a message pipe, optionally you can specify a |
| 63 | // base::SingleThreadTaskRunner. This task runner must belong to the same |
| 64 | // thread. It will be used to dispatch incoming method calls and connection |
| 65 | // error notification. It is useful when you attach multiple task runners to a |
| 66 | // single thread for the purposes of task scheduling. Please note that incoming |
| 67 | // synchrounous method calls may not be run from this task runner, when they |
| 68 | // reenter outgoing synchrounous calls on the same thread. |
rockot | 8e2a5d2 | 2016-10-10 06:38:10 | [diff] [blame] | 69 | template <typename Interface, |
| 70 | typename ImplRefTraits = RawPtrImplRefTraits<Interface>> |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 71 | class Binding { |
| 72 | public: |
rockot | 8e2a5d2 | 2016-10-10 06:38:10 | [diff] [blame] | 73 | using ImplPointerType = typename ImplRefTraits::PointerType; |
| 74 | |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 75 | // Constructs an incomplete binding that will use the implementation |impl|. |
| 76 | // The binding may be completed with a subsequent call to the |Bind| method. |
| 77 | // Does not take ownership of |impl|, which must outlive the binding. |
rockot | 8e2a5d2 | 2016-10-10 06:38:10 | [diff] [blame] | 78 | explicit Binding(ImplPointerType impl) : internal_state_(std::move(impl)) {} |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 79 | |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 80 | // Constructs a completed binding of |impl| to the message pipe endpoint in |
| 81 | // |request|, taking ownership of the endpoint. Does not take ownership of |
yzshen | 076d0876 | 2016-03-02 06:57:14 | [diff] [blame] | 82 | // |impl|, which must outlive the binding. |
rockot | 8e2a5d2 | 2016-10-10 06:38:10 | [diff] [blame] | 83 | Binding(ImplPointerType impl, |
yzshen | f5d4893 | 2016-04-22 23:18:33 | [diff] [blame] | 84 | InterfaceRequest<Interface> request, |
| 85 | scoped_refptr<base::SingleThreadTaskRunner> runner = |
| 86 | base::ThreadTaskRunnerHandle::Get()) |
rockot | 8e2a5d2 | 2016-10-10 06:38:10 | [diff] [blame] | 87 | : Binding(std::move(impl)) { |
Ken Rockot | 4247245 | 2017-05-12 05:37:03 | [diff] [blame] | 88 | Bind(std::move(request), std::move(runner)); |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Tears down the binding, closing the message pipe and leaving the interface |
| 92 | // implementation unbound. |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 93 | ~Binding() {} |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 94 | |
| 95 | // Completes a binding that was constructed with only an interface |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 96 | // implementation by removing the message pipe endpoint from |request| and |
yzshen | 076d0876 | 2016-03-02 06:57:14 | [diff] [blame] | 97 | // binding it to the previously specified implementation. |
yzshen | f5d4893 | 2016-04-22 23:18:33 | [diff] [blame] | 98 | void Bind(InterfaceRequest<Interface> request, |
| 99 | scoped_refptr<base::SingleThreadTaskRunner> runner = |
| 100 | base::ThreadTaskRunnerHandle::Get()) { |
Ken Rockot | 4247245 | 2017-05-12 05:37:03 | [diff] [blame] | 101 | internal_state_.Bind(request.PassMessagePipe(), std::move(runner)); |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 102 | } |
| 103 | |
rockot | 222e7dd | 2016-08-24 23:37:11 | [diff] [blame] | 104 | // Adds a message filter to be notified of each incoming message before |
| 105 | // dispatch. If a filter returns |false| from Accept(), the message is not |
| 106 | // dispatched and the pipe is closed. Filters cannot be removed. |
| 107 | void AddFilter(std::unique_ptr<MessageReceiver> filter) { |
| 108 | DCHECK(is_bound()); |
| 109 | internal_state_.AddFilter(std::move(filter)); |
| 110 | } |
| 111 | |
yzshen | 1e8f666 | 2015-12-11 01:37:50 | [diff] [blame] | 112 | // Whether there are any associated interfaces running on the pipe currently. |
| 113 | bool HasAssociatedInterfaces() const { |
| 114 | return internal_state_.HasAssociatedInterfaces(); |
| 115 | } |
| 116 | |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 117 | // Stops processing incoming messages until |
| 118 | // ResumeIncomingMethodCallProcessing(), or WaitForIncomingMethodCall(). |
| 119 | // Outgoing messages are still sent. |
| 120 | // |
| 121 | // No errors are detected on the message pipe while paused. |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 122 | // |
yzshen | 1e8f666 | 2015-12-11 01:37:50 | [diff] [blame] | 123 | // This method may only be called if the object has been bound to a message |
| 124 | // pipe and there are no associated interfaces running. |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 125 | void PauseIncomingMethodCallProcessing() { |
yzshen | 1e8f666 | 2015-12-11 01:37:50 | [diff] [blame] | 126 | CHECK(!HasAssociatedInterfaces()); |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 127 | internal_state_.PauseIncomingMethodCallProcessing(); |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 128 | } |
| 129 | void ResumeIncomingMethodCallProcessing() { |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 130 | internal_state_.ResumeIncomingMethodCallProcessing(); |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Blocks the calling thread until either a call arrives on the previously |
| 134 | // bound message pipe, the deadline is exceeded, or an error occurs. Returns |
| 135 | // true if a method was successfully read and dispatched. |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 136 | // |
yzshen | 1e8f666 | 2015-12-11 01:37:50 | [diff] [blame] | 137 | // This method may only be called if the object has been bound to a message |
sky | 66b7a5e | 2017-03-24 19:11:22 | [diff] [blame] | 138 | // pipe. This returns once a message is received either on the master |
| 139 | // interface or any associated interfaces. |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 140 | bool WaitForIncomingMethodCall( |
| 141 | MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE) { |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 142 | return internal_state_.WaitForIncomingMethodCall(deadline); |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Closes the message pipe that was previously bound. Put this object into a |
| 146 | // state where it can be rebound to a new pipe. |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 147 | void Close() { internal_state_.Close(); } |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 148 | |
yzshen | 24438d2 | 2016-09-13 17:03:53 | [diff] [blame] | 149 | // Similar to the method above, but also specifies a disconnect reason. |
| 150 | void CloseWithReason(uint32_t custom_reason, const std::string& description) { |
| 151 | internal_state_.CloseWithReason(custom_reason, description); |
| 152 | } |
| 153 | |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 154 | // Unbinds the underlying pipe from this binding and returns it so it can be |
| 155 | // used in another context, such as on another thread or with a different |
| 156 | // implementation. Put this object into a state where it can be rebound to a |
| 157 | // new pipe. |
yzshen | 1e8f666 | 2015-12-11 01:37:50 | [diff] [blame] | 158 | // |
| 159 | // This method may only be called if the object has been bound to a message |
| 160 | // pipe and there are no associated interfaces running. |
| 161 | // |
| 162 | // TODO(yzshen): For now, users need to make sure there is no one holding |
| 163 | // on to associated interface endpoint handles at both sides of the |
| 164 | // message pipe in order to call this method. We need a way to forcefully |
| 165 | // invalidate associated interface endpoint handles. |
yzshen | b92c3dd | 2016-03-08 23:48:49 | [diff] [blame] | 166 | InterfaceRequest<Interface> Unbind() { |
yzshen | 1e8f666 | 2015-12-11 01:37:50 | [diff] [blame] | 167 | CHECK(!HasAssociatedInterfaces()); |
| 168 | return internal_state_.Unbind(); |
| 169 | } |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 170 | |
| 171 | // Sets an error handler that will be called if a connection error occurs on |
| 172 | // the bound message pipe. |
amistry | f7038757 | 2015-12-17 05:23:43 | [diff] [blame] | 173 | // |
| 174 | // This method may only be called after this Binding has been bound to a |
| 175 | // message pipe. The error handler will be reset when this Binding is unbound |
| 176 | // or closed. |
rockot | a21316a | 2016-06-19 17:08:36 | [diff] [blame] | 177 | void set_connection_error_handler(const base::Closure& error_handler) { |
amistry | f7038757 | 2015-12-17 05:23:43 | [diff] [blame] | 178 | DCHECK(is_bound()); |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 179 | internal_state_.set_connection_error_handler(error_handler); |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 180 | } |
| 181 | |
yzshen | 24438d2 | 2016-09-13 17:03:53 | [diff] [blame] | 182 | void set_connection_error_with_reason_handler( |
| 183 | const ConnectionErrorWithReasonCallback& error_handler) { |
| 184 | DCHECK(is_bound()); |
| 185 | internal_state_.set_connection_error_with_reason_handler(error_handler); |
| 186 | } |
| 187 | |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 188 | // Returns the interface implementation that was previously specified. Caller |
| 189 | // does not take ownership. |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 190 | Interface* impl() { return internal_state_.impl(); } |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 191 | |
| 192 | // Indicates whether the binding has been completed (i.e., whether a message |
| 193 | // pipe has been bound to the implementation). |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 194 | bool is_bound() const { return internal_state_.is_bound(); } |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 195 | |
| 196 | // Returns the value of the handle currently bound to this Binding which can |
| 197 | // be used to make explicit Wait/WaitMany calls. Requires that the Binding be |
| 198 | // bound. Ownership of the handle is retained by the Binding, it is not |
| 199 | // transferred to the caller. |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 200 | MessagePipeHandle handle() const { return internal_state_.handle(); } |
| 201 | |
Marijn Kruisselbrink | 3325c22c | 2017-06-07 20:54:26 | [diff] [blame] | 202 | // Reports the currently dispatching Message as bad and closes this binding. |
| 203 | // Note that this is only legal to call from directly within the stack frame |
| 204 | // of a message dispatch. If you need to do asynchronous work before you can |
| 205 | // determine the legitimacy of a message, use GetBadMessageCallback() and |
| 206 | // retain its result until you're ready to invoke or discard it. |
| 207 | void ReportBadMessage(const std::string& error) { |
| 208 | GetBadMessageCallback().Run(error); |
| 209 | } |
| 210 | |
| 211 | // Acquires a callback which may be run to report the currently dispatching |
| 212 | // Message as bad and close this binding. Note that this is only legal to call |
| 213 | // from directly within the stack frame of a message dispatch, but the |
| 214 | // returned callback may be called exactly once any time thereafter to report |
| 215 | // the message as bad. This may only be called once per message. The returned |
| 216 | // callback must be called on the Binding's own thread. |
| 217 | ReportBadMessageCallback GetBadMessageCallback() { |
| 218 | return internal_state_.GetBadMessageCallback(); |
| 219 | } |
| 220 | |
sammc | 462458f | 2016-09-01 09:52:58 | [diff] [blame] | 221 | // Sends a no-op message on the underlying message pipe and runs the current |
| 222 | // message loop until its response is received. This can be used in tests to |
| 223 | // verify that no message was sent on a message pipe in response to some |
| 224 | // stimulus. |
| 225 | void FlushForTesting() { internal_state_.FlushForTesting(); } |
| 226 | |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 227 | // Exposed for testing, should not generally be used. |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 228 | void EnableTestingMode() { internal_state_.EnableTestingMode(); } |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 229 | |
| 230 | private: |
yzshen | a87f4ac | 2016-11-30 19:03:20 | [diff] [blame] | 231 | internal::BindingState<Interface, ImplRefTraits> internal_state_; |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 232 | |
yzshen | 0b5628a | 2015-11-25 05:58:10 | [diff] [blame] | 233 | DISALLOW_COPY_AND_ASSIGN(Binding); |
rockot | 85dce086 | 2015-11-13 01:33:59 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | } // namespace mojo |
| 237 | |
| 238 | #endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_H_ |