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 MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 |
| 10 #include <string> |
9 #include <utility> | 11 #include <utility> |
10 | 12 |
11 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/macros.h" | 15 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
15 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "mojo/public/cpp/bindings/connection_error_callback.h" |
17 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | 20 #include "mojo/public/cpp/bindings/interface_ptr_info.h" |
18 #include "mojo/public/cpp/bindings/lib/interface_ptr_state.h" | 21 #include "mojo/public/cpp/bindings/lib/interface_ptr_state.h" |
19 | 22 |
20 namespace mojo { | 23 namespace mojo { |
21 | 24 |
22 class AssociatedGroup; | 25 class AssociatedGroup; |
23 | 26 |
24 // A pointer to a local proxy of a remote Interface implementation. Uses a | 27 // A pointer to a local proxy of a remote Interface implementation. Uses a |
25 // message pipe to communicate with the remote implementation, and automatically | 28 // message pipe to communicate with the remote implementation, and automatically |
26 // closes the pipe and deletes the proxy on destruction. The pointer must be | 29 // closes the pipe and deletes the proxy on destruction. The pointer must be |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 // stimulus. | 123 // stimulus. |
121 void FlushForTesting() { internal_state_.FlushForTesting(); } | 124 void FlushForTesting() { internal_state_.FlushForTesting(); } |
122 | 125 |
123 // Closes the bound message pipe (if any) and returns the pointer to the | 126 // Closes the bound message pipe (if any) and returns the pointer to the |
124 // unbound state. | 127 // unbound state. |
125 void reset() { | 128 void reset() { |
126 State doomed; | 129 State doomed; |
127 internal_state_.Swap(&doomed); | 130 internal_state_.Swap(&doomed); |
128 } | 131 } |
129 | 132 |
| 133 // Similar to the method above, but also specifies a disconnect reason. |
| 134 void ResetWithReason(uint32_t custom_reason, const std::string& description) { |
| 135 if (internal_state_.is_bound()) |
| 136 internal_state_.SendDisconnectReason(custom_reason, description); |
| 137 reset(); |
| 138 } |
| 139 |
130 // Whether there are any associated interfaces running on the pipe currently. | 140 // Whether there are any associated interfaces running on the pipe currently. |
131 bool HasAssociatedInterfaces() const { | 141 bool HasAssociatedInterfaces() const { |
132 return internal_state_.HasAssociatedInterfaces(); | 142 return internal_state_.HasAssociatedInterfaces(); |
133 } | 143 } |
134 | 144 |
135 // Indicates whether the message pipe has encountered an error. If true, | 145 // Indicates whether the message pipe has encountered an error. If true, |
136 // method calls made on this interface will be dropped (and may already have | 146 // method calls made on this interface will be dropped (and may already have |
137 // been dropped). | 147 // been dropped). |
138 bool encountered_error() const { return internal_state_.encountered_error(); } | 148 bool encountered_error() const { return internal_state_.encountered_error(); } |
139 | 149 |
140 // Registers a handler to receive error notifications. The handler will be | 150 // Registers a handler to receive error notifications. The handler will be |
141 // called from the thread that owns this InterfacePtr. | 151 // called from the thread that owns this InterfacePtr. |
142 // | 152 // |
143 // This method may only be called after the InterfacePtr has been bound to a | 153 // This method may only be called after the InterfacePtr has been bound to a |
144 // message pipe. | 154 // message pipe. |
145 void set_connection_error_handler(const base::Closure& error_handler) { | 155 void set_connection_error_handler(const base::Closure& error_handler) { |
146 internal_state_.set_connection_error_handler(error_handler); | 156 internal_state_.set_connection_error_handler(error_handler); |
147 } | 157 } |
148 | 158 |
| 159 void set_connection_error_with_reason_handler( |
| 160 const ConnectionErrorWithReasonCallback& error_handler) { |
| 161 internal_state_.set_connection_error_with_reason_handler(error_handler); |
| 162 } |
| 163 |
149 // Unbinds the InterfacePtr and returns the information which could be used | 164 // Unbinds the InterfacePtr and returns the information which could be used |
150 // to setup an InterfacePtr again. This method may be used to move the proxy | 165 // to setup an InterfacePtr again. This method may be used to move the proxy |
151 // to a different thread (see class comments for details). | 166 // to a different thread (see class comments for details). |
152 // | 167 // |
153 // It is an error to call PassInterface() while: | 168 // It is an error to call PassInterface() while: |
154 // - there are pending responses; or | 169 // - there are pending responses; or |
155 // TODO: fix this restriction, it's not always obvious when there is a | 170 // TODO: fix this restriction, it's not always obvious when there is a |
156 // pending response. | 171 // pending response. |
157 // - there are associated interfaces running. | 172 // - there are associated interfaces running. |
158 // TODO(yzshen): For now, users need to make sure there is no one holding | 173 // TODO(yzshen): For now, users need to make sure there is no one holding |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 base::ThreadTaskRunnerHandle::Get()) { | 244 base::ThreadTaskRunnerHandle::Get()) { |
230 InterfacePtr<Interface> ptr; | 245 InterfacePtr<Interface> ptr; |
231 if (info.is_valid()) | 246 if (info.is_valid()) |
232 ptr.Bind(std::move(info), std::move(runner)); | 247 ptr.Bind(std::move(info), std::move(runner)); |
233 return std::move(ptr); | 248 return std::move(ptr); |
234 } | 249 } |
235 | 250 |
236 } // namespace mojo | 251 } // namespace mojo |
237 | 252 |
238 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ | 253 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_H_ |
OLD | NEW |