summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/bindings/binding_set.h
blob: f2c948029a5bd3510f2a1d105f91ef179f019720 (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
// 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_BINDING_SET_H_
#define MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_

#include <algorithm>
#include <utility>
#include <vector>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/binding.h"

namespace mojo {

// Use this class to manage a set of bindings, which are automatically destroyed
// and removed from the set when the pipe they are bound to is disconnected.
template <typename Interface>
class BindingSet {
 public:
  BindingSet() {}
  ~BindingSet() { CloseAllBindings(); }

  void set_connection_error_handler(const Closure& error_handler) {
    error_handler_ = error_handler;
  }

  void AddBinding(Interface* impl, InterfaceRequest<Interface> request) {
    auto binding = new Element(impl, std::move(request));
    binding->set_connection_error_handler([this]() { OnConnectionError(); });
    bindings_.push_back(binding->GetWeakPtr());
  }

  // Returns an InterfacePtr bound to one end of a pipe whose other end is
  // bound to |this|.
  InterfacePtr<Interface> CreateInterfacePtrAndBind(Interface* impl) {
    InterfacePtr<Interface> interface_ptr;
    AddBinding(impl, GetProxy(&interface_ptr));
    return interface_ptr;
  }

  void CloseAllBindings() {
    for (const auto& it : bindings_) {
      if (it) {
        it->Close();
        delete it.get();
      }
    }
    bindings_.clear();
  }

  bool empty() const { return bindings_.empty(); }

 private:
  class Element {
   public:
    Element(Interface* impl, InterfaceRequest<Interface> request)
        : binding_(impl, std::move(request)), weak_ptr_factory_(this) {
      binding_.set_connection_error_handler([this]() { OnConnectionError(); });
    }

    ~Element() {}

    void set_connection_error_handler(const Closure& error_handler) {
      error_handler_ = error_handler;
    }

    base::WeakPtr<Element> GetWeakPtr() {
      return weak_ptr_factory_.GetWeakPtr();
    }

    void Close() { binding_.Close(); }

    void OnConnectionError() {
      Closure error_handler = error_handler_;
      delete this;
      error_handler.Run();
    }

   private:
    Binding<Interface> binding_;
    Closure error_handler_;
    base::WeakPtrFactory<Element> weak_ptr_factory_;

    DISALLOW_COPY_AND_ASSIGN(Element);
  };

  void OnConnectionError() {
    // Clear any deleted bindings.
    bindings_.erase(std::remove_if(bindings_.begin(), bindings_.end(),
                                   [](const base::WeakPtr<Element>& p) {
                                     return p.get() == nullptr;
                                   }),
                    bindings_.end());

    error_handler_.Run();
  }

  Closure error_handler_;
  std::vector<base::WeakPtr<Element>> bindings_;

  DISALLOW_COPY_AND_ASSIGN(BindingSet);
};

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_