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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_
8 #include "mojo/public/cpp/bindings/error_handler.h"
9 #include "mojo/public/cpp/bindings/interface_ptr.h"
10 #include "mojo/public/cpp/system/macros.h"
15 template <typename Interface>
16 class InterfaceImplState : public ErrorHandler {
18 explicit InterfaceImplState(WithErrorHandler<Interface>* instance)
22 stub_.set_sink(instance);
25 virtual ~InterfaceImplState() {
28 router_->set_error_handler(NULL);
34 InterfacePtr<Interface>* ptr,
35 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter()) {
37 ptr->Bind(pipe.handle0.Pass(), waiter);
38 Bind(pipe.handle1.Pass(), waiter);
41 void Bind(ScopedMessagePipeHandle handle,
42 MojoAsyncWaiter* waiter) {
45 router_ = new Router(handle.Pass(), waiter);
46 router_->set_incoming_receiver(&stub_);
47 router_->set_error_handler(this);
49 proxy_ = new typename Interface::Client_::Proxy_(router_);
51 stub_.sink()->SetClient(proxy_);
54 Router* router() { return router_; }
57 virtual void OnConnectionError() MOJO_OVERRIDE {
58 static_cast<WithErrorHandler<Interface>*>(stub_.sink())->
62 internal::Router* router_;
63 typename Interface::Client_::Proxy_* proxy_;
64 typename Interface::Stub_ stub_;
66 MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceImplState);
69 } // namespace internal
72 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_INTERFACE_IMPL_INTERNAL_H_