1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright 2018 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_MOJO_REVOCABLE_STRONG_BINDING_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_MOJO_REVOCABLE_STRONG_BINDING_H_
#include <memory>
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/connection_error_callback.h"
#include "mojo/public/cpp/bindings/filter_chain.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "mojo/public/cpp/bindings/message_header_validator.h"
#include "mojo/public/cpp/system/core.h"
#include "third_party/blink/renderer/platform/mojo/revocable_binding.h"
namespace blink {
class InterfaceInvalidator;
template <typename Interface>
class RevocableStrongBinding;
template <typename Interface>
using RevocableStrongBindingPtr =
base::WeakPtr<RevocableStrongBinding<Interface>>;
// This is a wrapper around a StrongBinding that binds it to an interface
// invalidator. When the invalidator is destroyed or a connection error is
// detected, the interface implementation is deleted. If the task runner that a
// RevocableStrongBinding is bound on is stopped, the connection error handler
// will not be invoked and the implementation will not be deleted.
//
// To use, call RevocableStrongBinding<T>::Create() (see below) or the helper
// MakeRevocableStrongBinding function:
//
// MakeRevocableStrongBinding(std::make_unique<FooImpl>(),
// std::move(foo_request));
//
template <typename Interface>
class RevocableStrongBinding {
public:
// Create a new RevocableStrongBinding instance. The instance owns itself,
// cleaning up only in the event of a pipe connection error or invalidation.
// Returns a WeakPtr to the new RevocableStrongBinding instance.
static RevocableStrongBindingPtr<Interface> Create(
std::unique_ptr<Interface> impl,
mojo::InterfaceRequest<Interface> request,
InterfaceInvalidator* invalidator) {
RevocableStrongBinding* binding = new RevocableStrongBinding(
std::move(impl), std::move(request), invalidator);
return binding->weak_factory_.GetWeakPtr();
}
// Note: The error handler must not delete the interface implementation.
//
// This method may only be called after this RevocableStrongBinding has been
// bound to a message pipe.
void set_connection_error_handler(base::OnceClosure error_handler) {
DCHECK(binding_.is_bound());
connection_error_handler_ = std::move(error_handler);
}
// Stops processing incoming messages until
// ResumeIncomingMethodCallProcessing().
// Outgoing messages are still sent.
//
// No errors are detected on the message pipe while paused.
//
// This method may only be called if the object has been bound to a message
// pipe and there are no associated interfaces running.
void PauseIncomingMethodCallProcessing() {
binding_.PauseIncomingMethodCallProcessing();
}
void ResumeIncomingMethodCallProcessing() {
binding_.ResumeIncomingMethodCallProcessing();
}
// Forces the binding to close. This destroys the RevocableStrongBinding
// instance.
void Close() { delete this; }
Interface* impl() { return impl_.get(); }
// Sends a message on the underlying message pipe and runs the current
// message loop until its response is received. This can be used in tests to
// verify that no message was sent on a message pipe in response to some
// stimulus.
void FlushForTesting() { binding_.FlushForTesting(); }
private:
RevocableStrongBinding(std::unique_ptr<Interface> impl,
mojo::InterfaceRequest<Interface> request,
InterfaceInvalidator* invalidator)
: impl_(std::move(impl)),
binding_(impl_.get(), std::move(request), invalidator),
weak_factory_(this) {
binding_.set_connection_error_handler(base::BindOnce(
&RevocableStrongBinding::OnConnectionError, base::Unretained(this)));
}
~RevocableStrongBinding() = default;
void OnConnectionError() {
if (connection_error_handler_) {
std::move(connection_error_handler_).Run();
}
Close();
}
std::unique_ptr<Interface> impl_;
base::OnceClosure connection_error_handler_;
RevocableBinding<Interface> binding_;
base::WeakPtrFactory<RevocableStrongBinding> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(RevocableStrongBinding);
};
template <typename Interface, typename Impl>
RevocableStrongBindingPtr<Interface> MakeRevocableStrongBinding(
std::unique_ptr<Impl> impl,
mojo::InterfaceRequest<Interface> request,
InterfaceInvalidator* invalidator) {
return RevocableStrongBinding<Interface>::Create(
std::move(impl), std::move(request), invalidator);
}
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_MOJO_REVOCABLE_STRONG_BINDING_H_
|