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/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop/message_loop.h" | |
12 #include "mojo/public/cpp/bindings/binding.h" | 11 #include "mojo/public/cpp/bindings/binding.h" |
13 #include "mojo/public/cpp/bindings/callback.h" | 12 #include "mojo/public/cpp/bindings/callback.h" |
14 #include "mojo/public/cpp/bindings/interface_ptr.h" | 13 #include "mojo/public/cpp/bindings/interface_ptr.h" |
15 #include "mojo/public/cpp/bindings/interface_request.h" | 14 #include "mojo/public/cpp/bindings/interface_request.h" |
| 15 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 16 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
| 17 #include "mojo/public/cpp/bindings/lib/router.h" |
16 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/system/core.h" |
17 | 19 |
18 namespace mojo { | 20 namespace mojo { |
19 | 21 |
20 // This connects an interface implementation strongly to a pipe. When a | 22 // This connects an interface implementation strongly to a pipe. When a |
21 // connection error is detected or the current message loop is destructed the | 23 // connection error is detected the implementation is deleted. Deleting the |
22 // implementation is deleted. | 24 // connector also closes the pipe. |
23 // | 25 // |
24 // Example of an implementation that is always bound strongly to a pipe | 26 // Example of an implementation that is always bound strongly to a pipe |
25 // | 27 // |
26 // class StronglyBound : public Foo { | 28 // class StronglyBound : public Foo { |
27 // public: | 29 // public: |
28 // explicit StronglyBound(InterfaceRequest<Foo> request) | 30 // explicit StronglyBound(InterfaceRequest<Foo> request) |
29 // : binding_(this, std::move(request)) {} | 31 // : binding_(this, std::move(request)) {} |
30 // | 32 // |
31 // // Foo implementation here | 33 // // Foo implementation here |
32 // | 34 // |
33 // private: | 35 // private: |
34 // StrongBinding<Foo> binding_; | 36 // StrongBinding<Foo> binding_; |
35 // }; | 37 // }; |
36 // | 38 // |
37 // class MyFooFactory : public InterfaceFactory<Foo> { | 39 // class MyFooFactory : public InterfaceFactory<Foo> { |
38 // public: | 40 // public: |
39 // void Create(..., InterfaceRequest<Foo> request) override { | 41 // void Create(..., InterfaceRequest<Foo> request) override { |
40 // new StronglyBound(std::move(request)); // The binding now owns the | 42 // new StronglyBound(std::move(request)); // The binding now owns the |
41 // // instance of StronglyBound. | 43 // // instance of StronglyBound. |
42 // } | 44 // } |
43 // }; | 45 // }; |
44 // | 46 // |
45 // This class is thread hostile once it is bound to a message pipe. Until it is | 47 // This class is thread hostile once it is bound to a message pipe. Until it is |
46 // bound, it may be bound or destroyed on any thread. | 48 // bound, it may be bound or destroyed on any thread. |
47 template <typename Interface> | 49 template <typename Interface> |
48 class StrongBinding : public base::MessageLoop::DestructionObserver { | 50 class StrongBinding { |
49 MOVE_ONLY_TYPE_FOR_CPP_03(StrongBinding); | 51 MOVE_ONLY_TYPE_FOR_CPP_03(StrongBinding); |
50 | 52 |
51 public: | 53 public: |
52 explicit StrongBinding(Interface* impl) : binding_(impl), observing_(true) { | 54 explicit StrongBinding(Interface* impl) : binding_(impl) {} |
53 base::MessageLoop::current()->AddDestructionObserver(this); | |
54 } | |
55 | 55 |
56 StrongBinding(Interface* impl, ScopedMessagePipeHandle handle) | 56 StrongBinding(Interface* impl, ScopedMessagePipeHandle handle) |
57 : StrongBinding(impl) { | 57 : StrongBinding(impl) { |
58 Bind(std::move(handle)); | 58 Bind(std::move(handle)); |
59 } | 59 } |
60 | 60 |
61 StrongBinding(Interface* impl, InterfacePtr<Interface>* ptr) | 61 StrongBinding(Interface* impl, InterfacePtr<Interface>* ptr) |
62 : StrongBinding(impl) { | 62 : StrongBinding(impl) { |
63 Bind(ptr); | 63 Bind(ptr); |
64 } | 64 } |
65 | 65 |
66 StrongBinding(Interface* impl, InterfaceRequest<Interface> request) | 66 StrongBinding(Interface* impl, InterfaceRequest<Interface> request) |
67 : StrongBinding(impl) { | 67 : StrongBinding(impl) { |
68 Bind(std::move(request)); | 68 Bind(std::move(request)); |
69 } | 69 } |
70 | 70 |
71 ~StrongBinding() override { StopObservingIfNecessary(); } | 71 ~StrongBinding() {} |
72 | 72 |
73 void Bind(ScopedMessagePipeHandle handle) { | 73 void Bind(ScopedMessagePipeHandle handle) { |
74 DCHECK(!binding_.is_bound()); | 74 DCHECK(!binding_.is_bound()); |
75 binding_.Bind(std::move(handle)); | 75 binding_.Bind(std::move(handle)); |
76 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); | 76 binding_.set_connection_error_handler([this]() { OnConnectionError(); }); |
77 } | 77 } |
78 | 78 |
79 void Bind(InterfacePtr<Interface>* ptr) { | 79 void Bind(InterfacePtr<Interface>* ptr) { |
80 DCHECK(!binding_.is_bound()); | 80 DCHECK(!binding_.is_bound()); |
81 binding_.Bind(ptr); | 81 binding_.Bind(ptr); |
(...skipping 12 matching lines...) Expand all Loading... |
94 | 94 |
95 // Note: The error handler must not delete the interface implementation. | 95 // Note: The error handler must not delete the interface implementation. |
96 // | 96 // |
97 // This method may only be called after this StrongBinding has been bound to a | 97 // This method may only be called after this StrongBinding has been bound to a |
98 // message pipe. | 98 // message pipe. |
99 void set_connection_error_handler(const Closure& error_handler) { | 99 void set_connection_error_handler(const Closure& error_handler) { |
100 DCHECK(binding_.is_bound()); | 100 DCHECK(binding_.is_bound()); |
101 connection_error_handler_ = error_handler; | 101 connection_error_handler_ = error_handler; |
102 } | 102 } |
103 | 103 |
| 104 Interface* impl() { return binding_.impl(); } |
| 105 // Exposed for testing, should not generally be used. |
| 106 internal::Router* internal_router() { return binding_.internal_router(); } |
| 107 |
104 void OnConnectionError() { | 108 void OnConnectionError() { |
105 StopObservingIfNecessary(); | |
106 connection_error_handler_.Run(); | 109 connection_error_handler_.Run(); |
107 delete binding_.impl(); | 110 delete binding_.impl(); |
108 } | 111 } |
109 | 112 |
110 // base::MessageLoop::DestructionObserver: | |
111 void WillDestroyCurrentMessageLoop() override { | |
112 StopObservingIfNecessary(); | |
113 binding_.Close(); | |
114 delete binding_.impl(); | |
115 } | |
116 | |
117 private: | 113 private: |
118 void StopObservingIfNecessary() { | |
119 if (observing_) { | |
120 observing_ = false; | |
121 base::MessageLoop::current()->RemoveDestructionObserver(this); | |
122 } | |
123 } | |
124 | |
125 Closure connection_error_handler_; | 114 Closure connection_error_handler_; |
126 Binding<Interface> binding_; | 115 Binding<Interface> binding_; |
127 // Whether the object is observing message loop destruction. | |
128 bool observing_; | |
129 }; | 116 }; |
130 | 117 |
131 } // namespace mojo | 118 } // namespace mojo |
132 | 119 |
133 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ | 120 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_ |
OLD | NEW |