Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(579)

Side by Side Diff: mojo/public/cpp/bindings/weak_binding_set.h

Issue 1735583002: Rename WeakBindingSet/WeakInterfacePtrSet to BindingSet/InterfacePtrSet. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_WEAK_BINDING_SET_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_WEAK_BINDING_SET_H_
7
8 #include <algorithm>
9 #include <utility>
10 #include <vector>
11
12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
14 #include "mojo/public/cpp/bindings/binding.h"
15
16 namespace mojo {
17
18 template <typename Interface>
19 class WeakBinding;
20
21 // Use this class to manage a set of weak pointers to bindings each of which is
22 // owned by the pipe they are bound to.
23 template <typename Interface>
24 class WeakBindingSet {
25 public:
26 using GenericInterface = typename Interface::GenericInterface;
27
28 WeakBindingSet() {}
29 ~WeakBindingSet() { CloseAllBindings(); }
30
31 void set_connection_error_handler(const Closure& error_handler) {
32 error_handler_ = error_handler;
33 }
34
35 void AddBinding(Interface* impl, InterfaceRequest<GenericInterface> request) {
36 auto binding = new WeakBinding<Interface>(impl, std::move(request));
37 binding->set_connection_error_handler([this]() { OnConnectionError(); });
38 bindings_.push_back(binding->GetWeakPtr());
39 }
40
41 // Returns an InterfacePtr bound to one end of a pipe whose other end is
42 // bound to |this|.
43 InterfacePtr<Interface> CreateInterfacePtrAndBind(Interface* impl) {
44 InterfacePtr<Interface> interface_ptr;
45 AddBinding(impl, GetProxy(&interface_ptr));
46 return interface_ptr;
47 }
48
49 void CloseAllBindings() {
50 for (const auto& it : bindings_) {
51 if (it) {
52 it->Close();
53 delete it.get();
54 }
55 }
56 bindings_.clear();
57 }
58
59 bool empty() const { return bindings_.empty(); }
60
61 private:
62 void OnConnectionError() {
63 // Clear any deleted bindings.
64 bindings_.erase(
65 std::remove_if(bindings_.begin(), bindings_.end(),
66 [](const base::WeakPtr<WeakBinding<Interface>>& p) {
67 return p.get() == nullptr;
68 }),
69 bindings_.end());
70
71 error_handler_.Run();
72 }
73
74 Closure error_handler_;
75 std::vector<base::WeakPtr<WeakBinding<Interface>>> bindings_;
76
77 DISALLOW_COPY_AND_ASSIGN(WeakBindingSet);
78 };
79
80 template <typename Interface>
81 class WeakBinding {
82 public:
83 using GenericInterface = typename Interface::GenericInterface;
84
85 WeakBinding(Interface* impl, InterfaceRequest<GenericInterface> request)
86 : binding_(impl, std::move(request)), weak_ptr_factory_(this) {
87 binding_.set_connection_error_handler([this]() { OnConnectionError(); });
88 }
89
90 ~WeakBinding() {}
91
92 void set_connection_error_handler(const Closure& error_handler) {
93 error_handler_ = error_handler;
94 }
95
96 base::WeakPtr<WeakBinding> GetWeakPtr() {
97 return weak_ptr_factory_.GetWeakPtr();
98 }
99
100 void Close() { binding_.Close(); }
101
102 void OnConnectionError() {
103 Closure error_handler = error_handler_;
104 delete this;
105 error_handler.Run();
106 }
107
108 private:
109 Binding<Interface> binding_;
110 Closure error_handler_;
111 base::WeakPtrFactory<WeakBinding> weak_ptr_factory_;
112
113 DISALLOW_COPY_AND_ASSIGN(WeakBinding);
114 };
115
116 } // namespace mojo
117
118 #endif // MOJO_PUBLIC_CPP_BINDINGS_WEAK_BINDING_SET_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/struct_traits_unittest.cc ('k') | mojo/public/cpp/bindings/weak_interface_ptr_set.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698