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_PTR_INTERNAL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ |
7 | 7 |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 | 9 |
| 10 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
| 11 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
10 #include "mojo/public/cpp/bindings/lib/router.h" | 12 #include "mojo/public/cpp/bindings/lib/router.h" |
11 | 13 |
12 namespace mojo { | 14 namespace mojo { |
13 namespace internal { | 15 namespace internal { |
14 | 16 |
15 template <typename Interface> | 17 template <typename Interface> |
16 class InterfacePtrState { | 18 class InterfacePtrState { |
17 public: | 19 public: |
18 InterfacePtrState() : instance_(NULL), client_(NULL), router_(NULL) {} | 20 InterfacePtrState() : instance_(NULL), router_(NULL) {} |
19 | 21 |
20 ~InterfacePtrState() { | 22 ~InterfacePtrState() { |
21 // Destruction order matters here. We delete |instance_| first, even though | 23 // Destruction order matters here. We delete |instance_| first, even though |
22 // |router_| may have a reference to it, so that |~Interface| may have a | 24 // |router_| may have a reference to it, so that |~Interface| may have a |
23 // shot at generating new outbound messages (ie, invoking client methods). | 25 // shot at generating new outbound messages (ie, invoking client methods). |
24 delete instance_; | 26 delete instance_; |
25 delete router_; | 27 delete router_; |
26 delete client_; | |
27 } | 28 } |
28 | 29 |
29 Interface* instance() const { return instance_; } | 30 Interface* instance() const { return instance_; } |
30 void set_instance(Interface* instance) { instance_ = instance; } | |
31 | 31 |
32 Router* router() const { return router_; } | 32 Router* router() const { return router_; } |
33 | 33 |
34 bool is_configured_as_proxy() const { | |
35 // This question only makes sense if we have a bound pipe. | |
36 return router_ && !client_; | |
37 } | |
38 | |
39 void Swap(InterfacePtrState* other) { | 34 void Swap(InterfacePtrState* other) { |
40 std::swap(other->instance_, instance_); | 35 std::swap(other->instance_, instance_); |
41 std::swap(other->client_, client_); | |
42 std::swap(other->router_, router_); | 36 std::swap(other->router_, router_); |
43 } | 37 } |
44 | 38 |
45 void ConfigureProxy(ScopedMessagePipeHandle handle, | 39 void ConfigureProxy(ScopedMessagePipeHandle handle, |
46 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { | 40 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { |
47 assert(!instance_); | 41 assert(!instance_); |
48 assert(!router_); | 42 assert(!router_); |
49 | 43 |
50 router_ = new Router(handle.Pass(), waiter); | 44 FilterChain filters; |
| 45 filters.Append(new MessageHeaderValidator) |
| 46 .Append(new typename Interface::Client::RequestValidator_) |
| 47 .Append(new typename Interface::ResponseValidator_); |
| 48 |
| 49 router_ = new Router(handle.Pass(), filters.Pass(), waiter); |
51 ProxyWithStub* proxy = new ProxyWithStub(router_); | 50 ProxyWithStub* proxy = new ProxyWithStub(router_); |
52 router_->set_incoming_receiver(&proxy->stub); | 51 router_->set_incoming_receiver(&proxy->stub); |
53 | 52 |
54 instance_ = proxy; | 53 instance_ = proxy; |
55 } | 54 } |
56 | 55 |
57 void ConfigureStub(ScopedMessagePipeHandle handle, | |
58 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { | |
59 assert(instance_); // Should have already been set! | |
60 assert(!router_); | |
61 | |
62 // Stub for binding to state_.instance | |
63 // Proxy for communicating to the client on the other end of the pipe. | |
64 | |
65 router_ = new Router(handle.Pass(), waiter); | |
66 ClientProxyWithStub* proxy = new ClientProxyWithStub(router_); | |
67 proxy->stub.set_sink(instance_); | |
68 router_->set_incoming_receiver(&proxy->stub); | |
69 | |
70 instance_->SetClient(proxy); | |
71 client_ = proxy; | |
72 } | |
73 | |
74 private: | 56 private: |
75 class ProxyWithStub : public Interface::Proxy_ { | 57 class ProxyWithStub : public Interface::Proxy_ { |
76 public: | 58 public: |
77 explicit ProxyWithStub(MessageReceiver* receiver) | 59 explicit ProxyWithStub(MessageReceiver* receiver) |
78 : Interface::Proxy_(receiver) { | 60 : Interface::Proxy_(receiver) { |
79 } | 61 } |
80 virtual void SetClient(typename Interface::Client* client) MOJO_OVERRIDE { | 62 virtual void SetClient(typename Interface::Client* client) MOJO_OVERRIDE { |
81 stub.set_sink(client); | 63 stub.set_sink(client); |
82 } | 64 } |
83 typename Interface::Client::Stub_ stub; | 65 typename Interface::Client::Stub_ stub; |
84 private: | 66 private: |
85 MOJO_DISALLOW_COPY_AND_ASSIGN(ProxyWithStub); | 67 MOJO_DISALLOW_COPY_AND_ASSIGN(ProxyWithStub); |
86 }; | 68 }; |
87 | 69 |
88 class ClientProxyWithStub : public Interface::Client::Proxy_ { | |
89 public: | |
90 explicit ClientProxyWithStub(MessageReceiver* receiver) | |
91 : Interface::Client::Proxy_(receiver) { | |
92 } | |
93 typename Interface::Stub_ stub; | |
94 private: | |
95 MOJO_DISALLOW_COPY_AND_ASSIGN(ClientProxyWithStub); | |
96 }; | |
97 | |
98 Interface* instance_; | 70 Interface* instance_; |
99 typename Interface::Client* client_; | |
100 Router* router_; | 71 Router* router_; |
101 | 72 |
102 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); | 73 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); |
103 }; | 74 }; |
104 | 75 |
105 } // namespace internal | 76 } // namespace internal |
106 } // namespace mojo | 77 } // namespace mojo |
107 | 78 |
108 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ | 79 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ |
OLD | NEW |