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_STRONG_BINDING_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_
8 #include "mojo/public/c/environment/async_waiter.h"
9 #include "mojo/public/cpp/bindings/binding.h"
10 #include "mojo/public/cpp/bindings/error_handler.h"
11 #include "mojo/public/cpp/bindings/interface_ptr.h"
12 #include "mojo/public/cpp/bindings/interface_request.h"
13 #include "mojo/public/cpp/bindings/lib/filter_chain.h"
14 #include "mojo/public/cpp/bindings/lib/message_header_validator.h"
15 #include "mojo/public/cpp/bindings/lib/router.h"
16 #include "mojo/public/cpp/system/core.h"
20 // This connects an interface implementation strongly to a pipe. When a
21 // connection error is detected the implementation is deleted. Deleting the
22 // connector also closes the pipe.
24 // Example of an implementation that is always bound strongly to a pipe
26 // class StronglyBound : public Foo {
28 // explicit StronglyBound(ScopedMessagePipeHandle handle)
29 // : binding_(this, handle.Pass()) {}
31 // // Foo implementation here
34 // StrongBinding<Foo> binding_;
37 template <typename Interface
>
38 class StrongBinding
: public ErrorHandler
{
40 explicit StrongBinding(Interface
* impl
) : binding_(impl
) {
41 binding_
.set_error_handler(this);
46 ScopedMessagePipeHandle handle
,
47 const MojoAsyncWaiter
* waiter
= Environment::GetDefaultAsyncWaiter())
48 : StrongBinding(impl
) {
49 binding_
.Bind(handle
.Pass(), waiter
);
54 InterfacePtr
<Interface
>* ptr
,
55 const MojoAsyncWaiter
* waiter
= Environment::GetDefaultAsyncWaiter())
56 : StrongBinding(impl
) {
57 binding_
.Bind(ptr
, waiter
);
62 InterfaceRequest
<Interface
> request
,
63 const MojoAsyncWaiter
* waiter
= Environment::GetDefaultAsyncWaiter())
64 : StrongBinding(impl
) {
65 binding_
.Bind(request
.Pass(), waiter
);
68 ~StrongBinding() override
{}
70 bool WaitForIncomingMethodCall() {
71 return binding_
.WaitForIncomingMethodCall();
74 void set_error_handler(ErrorHandler
* error_handler
) {
75 error_handler_
= error_handler
;
78 typename
Interface::Client
* client() { return binding_
.client(); }
79 // Exposed for testing, should not generally be used.
80 internal::Router
* internal_router() { return binding_
.internal_router(); }
82 // ErrorHandler implementation
83 void OnConnectionError() override
{
85 error_handler_
->OnConnectionError();
86 delete binding_
.impl();
90 ErrorHandler
* error_handler_
= nullptr;
91 Binding
<Interface
> binding_
;
96 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRONG_BINDING_H_