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