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_LIB_INTERFACE_IMPL_INTERNAL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
7 | 7 |
8 #include "mojo/public/cpp/bindings/error_handler.h" | 8 #include "mojo/public/cpp/bindings/error_handler.h" |
9 #include "mojo/public/cpp/bindings/interface_ptr.h" | 9 #include "mojo/public/cpp/bindings/interface_ptr.h" |
10 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 10 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
11 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 11 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
12 #include "mojo/public/cpp/environment/environment.h" | 12 #include "mojo/public/cpp/environment/environment.h" |
| 13 #include "mojo/public/cpp/environment/logging.h" |
13 #include "mojo/public/cpp/system/macros.h" | 14 #include "mojo/public/cpp/system/macros.h" |
14 | 15 |
15 namespace mojo { | 16 namespace mojo { |
16 namespace internal { | 17 namespace internal { |
17 | 18 |
18 template <typename Interface> | 19 template <typename Interface> |
19 class InterfaceImplBase : public Interface { | 20 class InterfaceImplBase : public Interface { |
20 public: | 21 public: |
21 virtual ~InterfaceImplBase() {} | 22 virtual ~InterfaceImplBase() {} |
22 virtual void OnConnectionEstablished() = 0; | 23 virtual void OnConnectionEstablished() = 0; |
23 virtual void OnConnectionError() = 0; | 24 virtual void OnConnectionError() = 0; |
24 }; | 25 }; |
25 | 26 |
26 template <typename Interface> | 27 template <typename Interface> |
27 class InterfaceImplState : public ErrorHandler { | 28 class InterfaceImplState : public ErrorHandler { |
28 public: | 29 public: |
29 typedef typename Interface::Client Client; | 30 typedef typename Interface::Client Client; |
30 | 31 |
31 explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) | 32 explicit InterfaceImplState(InterfaceImplBase<Interface>* instance) |
32 : router_(NULL), | 33 : router_(NULL), |
33 proxy_(NULL) { | 34 proxy_(NULL) { |
34 assert(instance); | 35 MOJO_DCHECK(instance); |
35 stub_.set_sink(instance); | 36 stub_.set_sink(instance); |
36 } | 37 } |
37 | 38 |
38 virtual ~InterfaceImplState() { | 39 virtual ~InterfaceImplState() { |
39 delete proxy_; | 40 delete proxy_; |
40 if (router_) { | 41 if (router_) { |
41 router_->set_error_handler(NULL); | 42 router_->set_error_handler(NULL); |
42 delete router_; | 43 delete router_; |
43 } | 44 } |
44 } | 45 } |
45 | 46 |
46 void BindProxy( | 47 void BindProxy( |
47 InterfacePtr<Interface>* ptr, | 48 InterfacePtr<Interface>* ptr, |
48 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { | 49 const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) { |
49 MessagePipe pipe; | 50 MessagePipe pipe; |
50 ptr->Bind(pipe.handle0.Pass(), waiter); | 51 ptr->Bind(pipe.handle0.Pass(), waiter); |
51 Bind(pipe.handle1.Pass(), waiter); | 52 Bind(pipe.handle1.Pass(), waiter); |
52 } | 53 } |
53 | 54 |
54 void Bind(ScopedMessagePipeHandle handle, | 55 void Bind(ScopedMessagePipeHandle handle, |
55 const MojoAsyncWaiter* waiter) { | 56 const MojoAsyncWaiter* waiter) { |
56 assert(!router_); | 57 MOJO_DCHECK(!router_); |
57 | 58 |
58 FilterChain filters; | 59 FilterChain filters; |
59 filters.Append<MessageHeaderValidator>(); | 60 filters.Append<MessageHeaderValidator>(); |
60 filters.Append<typename Interface::RequestValidator_>(); | 61 filters.Append<typename Interface::RequestValidator_>(); |
61 filters.Append<typename Interface::Client::ResponseValidator_>(); | 62 filters.Append<typename Interface::Client::ResponseValidator_>(); |
62 | 63 |
63 router_ = new Router(handle.Pass(), filters.Pass(), waiter); | 64 router_ = new Router(handle.Pass(), filters.Pass(), waiter); |
64 router_->set_incoming_receiver(&stub_); | 65 router_->set_incoming_receiver(&stub_); |
65 router_->set_error_handler(this); | 66 router_->set_error_handler(this); |
66 | 67 |
67 proxy_ = new typename Client::Proxy_(router_); | 68 proxy_ = new typename Client::Proxy_(router_); |
68 | 69 |
69 instance()->OnConnectionEstablished(); | 70 instance()->OnConnectionEstablished(); |
70 } | 71 } |
71 | 72 |
72 bool WaitForIncomingMethodCall() { | 73 bool WaitForIncomingMethodCall() { |
73 assert(router_); | 74 MOJO_DCHECK(router_); |
74 return router_->WaitForIncomingMessage(); | 75 return router_->WaitForIncomingMessage(); |
75 } | 76 } |
76 | 77 |
77 Router* router() { return router_; } | 78 Router* router() { return router_; } |
78 Client* client() { return proxy_; } | 79 Client* client() { return proxy_; } |
79 | 80 |
80 private: | 81 private: |
81 InterfaceImplBase<Interface>* instance() { | 82 InterfaceImplBase<Interface>* instance() { |
82 return static_cast<InterfaceImplBase<Interface>*>(stub_.sink()); | 83 return static_cast<InterfaceImplBase<Interface>*>(stub_.sink()); |
83 } | 84 } |
84 | 85 |
85 virtual void OnConnectionError() MOJO_OVERRIDE { | 86 virtual void OnConnectionError() MOJO_OVERRIDE { |
86 instance()->OnConnectionError(); | 87 instance()->OnConnectionError(); |
87 } | 88 } |
88 | 89 |
89 Router* router_; | 90 Router* router_; |
90 typename Client::Proxy_* proxy_; | 91 typename Client::Proxy_* proxy_; |
91 typename Interface::Stub_ stub_; | 92 typename Interface::Stub_ stub_; |
92 | 93 |
93 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); | 94 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); |
94 }; | 95 }; |
95 | 96 |
96 } // namespace internal | 97 } // namespace internal |
97 } // namespace mojo | 98 } // namespace mojo |
98 | 99 |
99 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ | 100 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
OLD | NEW |