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/system/macros.h" | 10 #include "mojo/public/cpp/system/macros.h" |
11 | 11 |
12 namespace mojo { | 12 namespace mojo { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 template <typename Interface> | 15 template <typename Interface> |
16 class InterfaceImplState : public ErrorHandler { | 16 class InterfaceImplState : public ErrorHandler { |
17 public: | 17 public: |
| 18 typedef typename Interface::Client Client; |
| 19 |
18 explicit InterfaceImplState(WithErrorHandler<Interface>* instance) | 20 explicit InterfaceImplState(WithErrorHandler<Interface>* instance) |
19 : router_(NULL), | 21 : router_(NULL), |
| 22 client_(NULL), |
20 proxy_(NULL) { | 23 proxy_(NULL) { |
21 assert(instance); | 24 assert(instance); |
22 stub_.set_sink(instance); | 25 stub_.set_sink(instance); |
23 } | 26 } |
24 | 27 |
25 virtual ~InterfaceImplState() { | 28 virtual ~InterfaceImplState() { |
26 delete proxy_; | 29 delete proxy_; |
27 if (router_) { | 30 if (router_) { |
28 router_->set_error_handler(NULL); | 31 router_->set_error_handler(NULL); |
29 delete router_; | 32 delete router_; |
30 } | 33 } |
31 } | 34 } |
32 | 35 |
33 void BindProxy( | 36 void BindProxy( |
34 InterfacePtr<Interface>* ptr, | 37 InterfacePtr<Interface>* ptr, |
35 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { | 38 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { |
36 MessagePipe pipe; | 39 MessagePipe pipe; |
37 ptr->Bind(pipe.handle0.Pass(), waiter); | 40 ptr->Bind(pipe.handle0.Pass(), waiter); |
38 Bind(pipe.handle1.Pass(), waiter); | 41 Bind(pipe.handle1.Pass(), waiter); |
39 } | 42 } |
40 | 43 |
41 void Bind(ScopedMessagePipeHandle handle, | 44 void Bind(ScopedMessagePipeHandle handle, |
42 MojoAsyncWaiter* waiter) { | 45 MojoAsyncWaiter* waiter) { |
43 assert(!router_); | 46 assert(!router_); |
44 | 47 |
45 router_ = new Router(handle.Pass(), waiter); | 48 router_ = new Router(handle.Pass(), waiter); |
46 router_->set_incoming_receiver(&stub_); | 49 router_->set_incoming_receiver(&stub_); |
47 router_->set_error_handler(this); | 50 router_->set_error_handler(this); |
48 | 51 |
49 proxy_ = new typename Interface::Client_::Proxy_(router_); | 52 proxy_ = new typename Client::Proxy_(router_); |
50 | 53 |
51 stub_.sink()->SetClient(proxy_); | 54 stub_.sink()->SetClient(proxy_); |
52 } | 55 } |
53 | 56 |
54 Router* router() { return router_; } | 57 Router* router() { return router_; } |
55 | 58 |
| 59 void set_client(Client* client) { client_ = client; } |
| 60 Client* client() { return client_; } |
| 61 |
56 private: | 62 private: |
57 virtual void OnConnectionError() MOJO_OVERRIDE { | 63 virtual void OnConnectionError() MOJO_OVERRIDE { |
58 static_cast<WithErrorHandler<Interface>*>(stub_.sink())-> | 64 static_cast<WithErrorHandler<Interface>*>(stub_.sink())-> |
59 OnConnectionError(); | 65 OnConnectionError(); |
60 } | 66 } |
61 | 67 |
62 internal::Router* router_; | 68 internal::Router* router_; |
63 typename Interface::Client_::Proxy_* proxy_; | 69 Client* client_; |
| 70 typename Client::Proxy_* proxy_; |
64 typename Interface::Stub_ stub_; | 71 typename Interface::Stub_ stub_; |
65 | 72 |
66 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); | 73 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState); |
67 }; | 74 }; |
68 | 75 |
69 } // namespace internal | 76 } // namespace internal |
70 } // namespace mojo | 77 } // namespace mojo |
71 | 78 |
72 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ | 79 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_ |
OLD | NEW |