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 <assert.h> |
8 #include <stdio.h> | 9 #include <stdio.h> |
9 | 10 |
10 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | 11 #include "mojo/public/cpp/bindings/lib/filter_chain.h" |
11 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 12 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
12 #include "mojo/public/cpp/bindings/lib/router.h" | 13 #include "mojo/public/cpp/bindings/lib/router.h" |
13 | 14 |
14 namespace mojo { | 15 namespace mojo { |
15 namespace internal { | 16 namespace internal { |
16 | 17 |
17 template <typename Interface> | 18 template <typename Interface> |
18 class InterfacePtrState { | 19 class InterfacePtrState { |
19 public: | 20 public: |
20 InterfacePtrState() : instance_(NULL), router_(NULL) {} | 21 InterfacePtrState() : proxy_(NULL), router_(NULL) {} |
21 | 22 |
22 ~InterfacePtrState() { | 23 ~InterfacePtrState() { |
23 // Destruction order matters here. We delete |instance_| first, even though | 24 // Destruction order matters here. We delete |proxy_| first, even though |
24 // |router_| may have a reference to it, so that |~Interface| may have a | 25 // |router_| may have a reference to it, so that |~Interface| may have a |
25 // shot at generating new outbound messages (ie, invoking client methods). | 26 // shot at generating new outbound messages (ie, invoking client methods). |
26 delete instance_; | 27 delete proxy_; |
27 delete router_; | 28 delete router_; |
28 } | 29 } |
29 | 30 |
30 Interface* instance() const { return instance_; } | 31 Interface* instance() const { return proxy_; } |
31 | 32 |
32 Router* router() const { return router_; } | 33 Router* router() const { return router_; } |
33 | 34 |
34 void Swap(InterfacePtrState* other) { | 35 void Swap(InterfacePtrState* other) { |
35 std::swap(other->instance_, instance_); | 36 std::swap(other->proxy_, proxy_); |
36 std::swap(other->router_, router_); | 37 std::swap(other->router_, router_); |
37 } | 38 } |
38 | 39 |
39 void ConfigureProxy(ScopedMessagePipeHandle handle, | 40 void ConfigureProxy(ScopedMessagePipeHandle handle, |
40 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { | 41 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { |
41 assert(!instance_); | 42 assert(!proxy_); |
42 assert(!router_); | 43 assert(!router_); |
43 | 44 |
44 FilterChain filters; | 45 FilterChain filters; |
45 filters.Append<MessageHeaderValidator>(); | 46 filters.Append<MessageHeaderValidator>(); |
46 filters.Append<typename Interface::Client::RequestValidator_>(); | 47 filters.Append<typename Interface::Client::RequestValidator_>(); |
47 filters.Append<typename Interface::ResponseValidator_>(); | 48 filters.Append<typename Interface::ResponseValidator_>(); |
48 | 49 |
49 router_ = new Router(handle.Pass(), filters.Pass(), waiter); | 50 router_ = new Router(handle.Pass(), filters.Pass(), waiter); |
50 ProxyWithStub* proxy = new ProxyWithStub(router_); | 51 ProxyWithStub* proxy = new ProxyWithStub(router_); |
51 router_->set_incoming_receiver(&proxy->stub); | 52 router_->set_incoming_receiver(&proxy->stub); |
52 | 53 |
53 instance_ = proxy; | 54 proxy_ = proxy; |
| 55 } |
| 56 |
| 57 void set_client(typename Interface::Client* client) { |
| 58 assert(proxy_); |
| 59 proxy_->stub.set_sink(client); |
54 } | 60 } |
55 | 61 |
56 private: | 62 private: |
57 class ProxyWithStub : public Interface::Proxy_ { | 63 class ProxyWithStub : public Interface::Proxy_ { |
58 public: | 64 public: |
59 explicit ProxyWithStub(MessageReceiver* receiver) | 65 explicit ProxyWithStub(MessageReceiver* receiver) |
60 : Interface::Proxy_(receiver) { | 66 : Interface::Proxy_(receiver) { |
61 } | 67 } |
62 virtual void SetClient(typename Interface::Client* client) MOJO_OVERRIDE { | |
63 stub.set_sink(client); | |
64 } | |
65 typename Interface::Client::Stub_ stub; | 68 typename Interface::Client::Stub_ stub; |
66 private: | 69 private: |
67 MOJO_DISALLOW_COPY_AND_ASSIGN(ProxyWithStub); | 70 MOJO_DISALLOW_COPY_AND_ASSIGN(ProxyWithStub); |
68 }; | 71 }; |
69 | 72 |
70 Interface* instance_; | 73 ProxyWithStub* proxy_; |
71 Router* router_; | 74 Router* router_; |
72 | 75 |
73 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); | 76 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); |
74 }; | 77 }; |
75 | 78 |
76 } // namespace internal | 79 } // namespace internal |
77 } // namespace mojo | 80 } // namespace mojo |
78 | 81 |
79 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ | 82 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ |
OLD | NEW |