Chromium Code Reviews| 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_STRONG_BINDING_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| 7 | 7 |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
| 13 #include "mojo/public/cpp/bindings/callback.h" | 14 #include "mojo/public/cpp/bindings/callback.h" |
| 14 #include "mojo/public/cpp/bindings/interface_ptr.h" | 15 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 15 #include "mojo/public/cpp/bindings/interface_request.h" | 16 #include "mojo/public/cpp/bindings/interface_request.h" |
| 16 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 17 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 17 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 18 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
| 18 #include "mojo/public/cpp/bindings/lib/router.h" | 19 #include "mojo/public/cpp/bindings/lib/router.h" |
| 19 #include "mojo/public/cpp/system/core.h" | 20 #include "mojo/public/cpp/system/core.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 StrongBinding(Interface* impl, InterfaceRequest<Interface> request) | 66 StrongBinding(Interface* impl, InterfaceRequest<Interface> request) |
| 66 : StrongBinding(impl) { | 67 : StrongBinding(impl) { |
| 67 Bind(std::move(request)); | 68 Bind(std::move(request)); |
| 68 } | 69 } |
| 69 | 70 |
| 70 ~StrongBinding() {} | 71 ~StrongBinding() {} |
| 71 | 72 |
| 72 void Bind(ScopedMessagePipeHandle handle) { | 73 void Bind(ScopedMessagePipeHandle handle) { |
| 73 DCHECK(!binding_.is_bound()); | 74 DCHECK(!binding_.is_bound()); |
| 74 binding_.Bind(std::move(handle)); | 75 binding_.Bind(std::move(handle)); |
| 75 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 76 binding_.set_connection_error_handler( |
| 77 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this))); | |
| 76 } | 78 } |
| 77 | 79 |
| 78 void Bind(InterfacePtr<Interface>* ptr) { | 80 void Bind(InterfacePtr<Interface>* ptr) { |
| 79 DCHECK(!binding_.is_bound()); | 81 DCHECK(!binding_.is_bound()); |
| 80 binding_.Bind(ptr); | 82 binding_.Bind(ptr); |
| 81 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 83 binding_.set_connection_error_handler( |
| 84 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this))); | |
| 82 } | 85 } |
| 83 | 86 |
| 84 void Bind(InterfaceRequest<Interface> request) { | 87 void Bind(InterfaceRequest<Interface> request) { |
| 85 DCHECK(!binding_.is_bound()); | 88 DCHECK(!binding_.is_bound()); |
| 86 binding_.Bind(std::move(request)); | 89 binding_.Bind(std::move(request)); |
| 87 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 90 binding_.set_connection_error_handler( |
| 91 base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this))); | |
| 88 } | 92 } |
| 89 | 93 |
| 90 bool WaitForIncomingMethodCall() { | 94 bool WaitForIncomingMethodCall() { |
| 91 return binding_.WaitForIncomingMethodCall(); | 95 return binding_.WaitForIncomingMethodCall(); |
| 92 } | 96 } |
| 93 | 97 |
| 94 // Note: The error handler must not delete the interface implementation. | 98 // Note: The error handler must not delete the interface implementation. |
| 95 // | 99 // |
| 96 // This method may only be called after this StrongBinding has been bound to a | 100 // This method may only be called after this StrongBinding has been bound to a |
| 97 // message pipe. | 101 // message pipe. |
| 98 void set_connection_error_handler(const Closure& error_handler) { | 102 void set_connection_error_handler(const Closure& error_handler) { |
| 99 DCHECK(binding_.is_bound()); | 103 DCHECK(binding_.is_bound()); |
| 100 connection_error_handler_ = error_handler; | 104 connection_error_handler_ = error_handler; |
| 101 } | 105 } |
| 102 | 106 |
| 103 Interface* impl() { return binding_.impl(); } | 107 Interface* impl() { return binding_.impl(); } |
| 104 // Exposed for testing, should not generally be used. | 108 // Exposed for testing, should not generally be used. |
| 105 internal::Router* internal_router() { return binding_.internal_router(); } | 109 internal::Router* internal_router() { return binding_.internal_router(); } |
| 106 | 110 |
| 107 void OnConnectionError() { | 111 void OnConnectionError() { |
| 108 connection_error_handler_.Run(); | 112 RunConnectionErrorHandler(); |
|
yzshen1
2016/06/18 00:01:18
nit: maybe no need to have such a helper because t
Ken Rockot(use gerrit already)
2016/06/18 03:24:26
Oops, done.
| |
| 109 delete binding_.impl(); | 113 delete binding_.impl(); |
| 110 } | 114 } |
| 111 | 115 |
| 112 private: | 116 private: |
| 117 void RunConnectionErrorHandler() { | |
| 118 if (!connection_error_handler_.is_null()) | |
| 119 connection_error_handler_.Run(); | |
| 120 } | |
| 121 | |
| 113 Closure connection_error_handler_; | 122 Closure connection_error_handler_; |
| 114 Binding<Interface> binding_; | 123 Binding<Interface> binding_; |
| 115 | 124 |
| 116 DISALLOW_COPY_AND_ASSIGN(StrongBinding); | 125 DISALLOW_COPY_AND_ASSIGN(StrongBinding); |
| 117 }; | 126 }; |
| 118 | 127 |
| 119 } // namespace mojo | 128 } // namespace mojo |
| 120 | 129 |
| 121 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 130 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
| OLD | NEW |