| Index: mojo/public/cpp/bindings/interface_binding.h
|
| diff --git a/mojo/public/cpp/bindings/interface_binding.h b/mojo/public/cpp/bindings/interface_binding.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4fd24b2ac0e7a6a6f141cd7712ce04e80809adba
|
| --- /dev/null
|
| +++ b/mojo/public/cpp/bindings/interface_binding.h
|
| @@ -0,0 +1,109 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_BINDING_H_
|
| +#define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_BINDING_H_
|
| +
|
| +#include "mojo/public/cpp/bindings/interface_request.h"
|
| +#include "mojo/public/cpp/bindings/lib/interface_impl_internal.h"
|
| +#include "mojo/public/cpp/environment/environment.h"
|
| +#include "mojo/public/cpp/system/macros.h"
|
| +
|
| +namespace mojo {
|
| +
|
| +template <typename Interface>
|
| +class InterfaceImpl;
|
| +
|
| +// InterfaceBinding is used to bind an implementation of an interface to a pipe.
|
| +// Once bound, client() returns the client of the Interface.
|
| +// InterfaceBinding owns the underlying pipe. Deleting InterfaceBinding
|
| +// closes the pipe (assuming it is open).
|
| +// InterfaceBinding can be configured to delete the supplied interface when
|
| +// InterfaceBinding is deleted. See set_owns_interface().
|
| +// See also InterfaceImpl.
|
| +template <typename Interface>
|
| +class InterfaceBinding : public InterfaceBindingDelegate {
|
| + public:
|
| + typedef typename Interface::Client Client;
|
| +
|
| + InterfaceBinding(Interface* interface, InterfaceBindingDelegate* delegate)
|
| + : delegate_(delegate),
|
| + internal_state_(interface, this),
|
| + destroyed_(nullptr),
|
| + owns_interface_(false) {}
|
| +
|
| + virtual ~InterfaceBinding() {
|
| + if (destroyed_)
|
| + *destroyed_ = true;
|
| + if (owns_interface_)
|
| + delete internal_state_.instance();
|
| + }
|
| +
|
| + void BindToPipe(
|
| + ScopedMessagePipeHandle handle,
|
| + const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
|
| + internal_state_.Bind(handle.Pass(), false, waiter);
|
| + }
|
| +
|
| + void BindToRequest(
|
| + InterfaceRequest<Interface>* request,
|
| + const MojoAsyncWaiter* waiter = Environment::GetDefaultAsyncWaiter()) {
|
| + return BindToPipe(request->PassMessagePipe(), waiter);
|
| + }
|
| +
|
| + // Whether the supplied interface should be deleted when InterfaceBinding is
|
| + // destroyed.
|
| + void set_owns_interface(bool owns_interface) {
|
| + owns_interface_ = owns_interface;
|
| + }
|
| +
|
| + Client* client() { return internal_state_.client(); }
|
| +
|
| + private:
|
| + friend class InterfaceImpl<Interface>;
|
| +
|
| + void set_delete_interface_on_error(bool delete_interface_on_error) {
|
| + delete_interface_on_error_ = delete_interface_on_error;
|
| + }
|
| +
|
| + // InterfaceBindingDelegate:
|
| + void OnConnectionEstablished() override {
|
| + delegate_->OnConnectionEstablished();
|
| + }
|
| + void OnConnectionError() override {
|
| + bool destroyed = false;
|
| + destroyed_ = &destroyed;
|
| + // Allow OnConnectionError() to delete 'this'.
|
| + delegate_->OnConnectionError();
|
| + if (destroyed)
|
| + return;
|
| + destroyed_ = nullptr;
|
| + // |delete_interface_on_error_| means we're in an InterfaceImpl and need
|
| + // to delete the InterfaceImpl (which triggers deleting us).
|
| + if (internal_state_.instance_bound_to_pipe()) {
|
| + if (delete_interface_on_error_)
|
| + delete internal_state_.instance();
|
| + else
|
| + delete this;
|
| + }
|
| + }
|
| +
|
| + InterfaceBindingDelegate* delegate_;
|
| +
|
| + internal::InterfaceImplState<Interface> internal_state_;
|
| + // If non-null the destructor sets the value to true. Used to detect deletion
|
| + // while calling to delegate.
|
| + bool* destroyed_;
|
| +
|
| + bool owns_interface_;
|
| +
|
| + // See description in OnConnectionError().
|
| + bool delete_interface_on_error_;
|
| +
|
| + MOJO_DISALLOW_COPY_AND_ASSIGN(InterfaceBinding);
|
| +};
|
| +
|
| +} // namespace mojo
|
| +
|
| +#endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_BINDING_H_
|
|
|