summaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/strong_associated_binding.h
blob: a1e299bb2d8def727bed3f545382ee9c31f3b5c8 (plain)
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
// Copyright 2016 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_STRONG_ASSOCIATED_BINDING_H_
#define MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_

#include <memory>
#include <string>
#include <utility>

#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/associated_binding.h"
#include "mojo/public/cpp/bindings/associated_interface_request.h"
#include "mojo/public/cpp/bindings/connection_error_callback.h"
#include "mojo/public/cpp/system/core.h"

namespace mojo {

template <typename Interface>
class StrongAssociatedBinding;

template <typename Interface>
using StrongAssociatedBindingPtr =
    base::WeakPtr<StrongAssociatedBinding<Interface>>;

// This connects an interface implementation strongly to an associated pipe.
// When a connection error is detected the implementation is deleted. If the
// task runner that a StrongAssociatedBinding is bound on is stopped, the
// connection error handler will not be invoked and the implementation will not
// be deleted.
//
// To use, call StrongAssociatedBinding<T>::Create() (see below) or the helper
// MakeStrongAssociatedBinding function:
//
//   mojo::MakeStrongAssociatedBinding(base::MakeUnique<FooImpl>(),
//                                     std::move(foo_request));
//
template <typename Interface>
class StrongAssociatedBinding {
 public:
  // Create a new StrongAssociatedBinding instance. The instance owns itself,
  // cleaning up only in the event of a pipe connection error. Returns a WeakPtr
  // to the new StrongAssociatedBinding instance.
  static StrongAssociatedBindingPtr<Interface> Create(
      std::unique_ptr<Interface> impl,
      AssociatedInterfaceRequest<Interface> request) {
    StrongAssociatedBinding* binding =
        new StrongAssociatedBinding(std::move(impl), std::move(request));
    return binding->weak_factory_.GetWeakPtr();
  }

  // Note: The error handler must not delete the interface implementation.
  //
  // This method may only be called after this StrongAssociatedBinding has been
  // bound to a message pipe.
  void set_connection_error_handler(const base::Closure& error_handler) {
    DCHECK(binding_.is_bound());
    connection_error_handler_ = error_handler;
    connection_error_with_reason_handler_.Reset();
  }

  void set_connection_error_with_reason_handler(
      const ConnectionErrorWithReasonCallback& error_handler) {
    DCHECK(binding_.is_bound());
    connection_error_with_reason_handler_ = error_handler;
    connection_error_handler_.Reset();
  }

  // Forces the binding to close. This destroys the StrongBinding 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:
  StrongAssociatedBinding(std::unique_ptr<Interface> impl,
                          AssociatedInterfaceRequest<Interface> request)
      : impl_(std::move(impl)),
        binding_(impl_.get(), std::move(request)),
        weak_factory_(this) {
    binding_.set_connection_error_with_reason_handler(base::Bind(
        &StrongAssociatedBinding::OnConnectionError, base::Unretained(this)));
  }

  ~StrongAssociatedBinding() {}

  void OnConnectionError(uint32_t custom_reason,
                         const std::string& description) {
    if (!connection_error_handler_.is_null())
      connection_error_handler_.Run();
    else if (!connection_error_with_reason_handler_.is_null())
      connection_error_with_reason_handler_.Run(custom_reason, description);
    Close();
  }

  std::unique_ptr<Interface> impl_;
  base::Closure connection_error_handler_;
  ConnectionErrorWithReasonCallback connection_error_with_reason_handler_;
  AssociatedBinding<Interface> binding_;
  base::WeakPtrFactory<StrongAssociatedBinding> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(StrongAssociatedBinding);
};

template <typename Interface, typename Impl>
StrongAssociatedBindingPtr<Interface> MakeStrongAssociatedBinding(
    std::unique_ptr<Impl> impl,
    AssociatedInterfaceRequest<Interface> request) {
  return StrongAssociatedBinding<Interface>::Create(std::move(impl),
                                                    std::move(request));
}

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_STRONG_ASSOCIATED_BINDING_H_