OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ |
| 7 |
| 8 #include <stdio.h> |
| 9 |
| 10 #include "mojo/public/cpp/bindings/lib/router.h" |
| 11 |
| 12 namespace mojo { |
| 13 namespace internal { |
| 14 |
| 15 template <typename Interface> |
| 16 class InterfacePtrState { |
| 17 public: |
| 18 InterfacePtrState() : instance_(NULL), client_(NULL), router_(NULL) {} |
| 19 |
| 20 ~InterfacePtrState() { |
| 21 // Destruction order matters here. We delete |instance_| first, even though |
| 22 // |router_| may have a reference to it, so that |~Interface| may have a |
| 23 // shot at generating new outbound messages (ie, invoking client methods). |
| 24 delete instance_; |
| 25 delete router_; |
| 26 delete client_; |
| 27 } |
| 28 |
| 29 Interface* instance() const { return instance_; } |
| 30 void set_instance(Interface* instance) { instance_ = instance; } |
| 31 |
| 32 Router* router() const { return router_; } |
| 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) { |
| 40 std::swap(other->instance_, instance_); |
| 41 std::swap(other->client_, client_); |
| 42 std::swap(other->router_, router_); |
| 43 } |
| 44 |
| 45 void ConfigureProxy(ScopedMessagePipeHandle handle, |
| 46 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) { |
| 47 assert(!instance_); |
| 48 assert(!router_); |
| 49 |
| 50 router_ = new Router(handle.Pass(), waiter); |
| 51 ProxyWithStub* proxy = new ProxyWithStub(router_); |
| 52 router_->set_incoming_receiver(&proxy->stub); |
| 53 |
| 54 instance_ = proxy; |
| 55 } |
| 56 |
| 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: |
| 75 class ProxyWithStub : public Interface::Proxy_ { |
| 76 public: |
| 77 explicit ProxyWithStub(MessageReceiver* receiver) |
| 78 : Interface::Proxy_(receiver) { |
| 79 } |
| 80 virtual void SetClient(typename Interface::Client_* client) MOJO_OVERRIDE { |
| 81 stub.set_sink(client); |
| 82 } |
| 83 typename Interface::Client_::Stub_ stub; |
| 84 private: |
| 85 MOJO_DISALLOW_COPY_AND_ASSIGN(ProxyWithStub); |
| 86 }; |
| 87 |
| 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_; |
| 99 typename Interface::Client_* client_; |
| 100 Router* router_; |
| 101 |
| 102 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfacePtrState); |
| 103 }; |
| 104 |
| 105 } // namespace internal |
| 106 } // namespace mojo |
| 107 |
| 108 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_PTR_INTERNAL_H_ |
OLD | NEW |