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

Side by Side Diff: mojo/common/service_set.h

Issue 1411063007: Add mojo::StrongBindingSet and use it in GeolocationServiceContext. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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 2015 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_COMMON_SERVICE_SET_H_
6 #define MOJO_COMMON_SERVICE_SET_H_
7
8 #include "base/bind.h"
9 #include "base/containers/scoped_ptr_map.h"
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
13
14 namespace mojo {
15 namespace internal {
16
17 template <typename T>
18 struct RemoveReference {
19 using type = T;
20 };
21
22 template <typename T>
23 struct RemoveReference<T&> {
24 using type = T;
25 };
26
27 template <typename T>
28 struct RemoveReference<T&&> {
29 using type = T;
30 };
31
32 // These are a copy of std::forward. TODO(sammc): Remove when C++11 library
33 // features are allowed.
34 template <typename T>
35 T&& Forward(typename RemoveReference<T>::type& t) {
36 return static_cast<T&&>(t);
37 }
38
39 template <typename T>
40 T&& Forward(typename RemoveReference<T>::type&& t) {
41 return static_cast<T&&>(t);
42 }
43
44 } // namespace internal
45
46 // A set of mojo services where each service's lifetime is bound to a message
47 // pipe to its client. The template parameters Interface and Impl should be a
48 // mojo interface and a concrete implementation of that interface, respectively.
49 //
50 // To create an instance of Impl in response to a request, call
51 // CreateService(request.Pass(), <other args to Impl's constructor>);
52 //
53 // When the client of a service closes its connection, that service is
54 // automatically destroyed. set_connection_error_handler() can be used to
55 // register an ErrorHandler to be called when this occurs. When the handler is
56 // called, the service is no longer contained in the set but is only destroyed
57 // after the callback returns.
58 //
59 // A service may be manually destroyed by calling DestroyService(). Services
60 // destroyed this way will not trigger a call to the ErrorHandler.
61 template <typename Interface, typename Impl>
62 class ServiceSet {
63 public:
64 using ErrorHandler = Callback<void(Impl*)>;
65 class Iterator;
66
67 ServiceSet() = default;
68
69 void set_connection_error_handler(const ErrorHandler& error_handler) {
70 error_handler_ = error_handler;
71 }
72
73 template <typename... Args>
74 Impl* CreateService(InterfaceRequest<Interface> request, Args&&... args) {
75 auto holder = make_scoped_ptr(
76 new Holder(this, request.Pass(), internal::Forward<Args>(args)...));
77 Impl* impl = holder->impl();
78 holders_.insert(impl, holder.Pass());
79 return impl;
80 }
81
82 void DestroyService(Impl* impl) {
83 size_t num_erased = holders_.erase(impl);
84 DCHECK_EQ(1u, num_erased);
85 }
86
87 void clear() { holders_.clear(); }
88 bool empty() const { return holders_.empty(); }
89 size_t size() const { return holders_.size(); }
90 Iterator begin() const { return Iterator(holders_.begin()); }
91 Iterator end() const { return Iterator(holders_.end()); }
92
93 private:
94 class Holder;
95 using Container = base::ScopedPtrMap<Impl*, scoped_ptr<Holder>>;
96
97 void OnConnectionError(Holder* holder) {
98 Impl* impl = holder->impl();
99 scoped_ptr<Holder> holder_owner = holders_.take_and_erase(holder->impl());
100 DCHECK(holder);
101 if (!error_handler_.is_null())
102 error_handler_.Run(impl);
103 }
104
105 ErrorHandler error_handler_;
106 Container holders_;
107
108 DISALLOW_COPY_AND_ASSIGN(ServiceSet);
109 };
110
111 template <typename Interface, typename Impl>
112 class ServiceSet<Interface, Impl>::Holder {
113 public:
114 template <typename... Args>
115 Holder(ServiceSet* service_set,
116 InterfaceRequest<Interface> request,
117 Args&&... args)
118 : impl_(internal::Forward<Args>(args)...),
119 binding_(&impl_, request.Pass()),
120 service_set_(service_set) {
121 binding_.set_connection_error_handler(
122 base::Bind(&Holder::OnConnectionError, base::Unretained(this)));
123 }
124
125 void OnConnectionError() { service_set_->OnConnectionError(this); }
126
127 Impl* impl() { return &impl_; }
128
129 private:
130 Impl impl_;
131 Binding<Interface> binding_;
132
133 // Owns this.
134 ServiceSet* const service_set_;
135
136 DISALLOW_COPY_AND_ASSIGN(Holder);
137 };
138
139 template <typename Interface, typename Impl>
140 class ServiceSet<Interface, Impl>::Iterator {
141 public:
142 using iterator_category = std::input_iterator_tag;
143 using difference_type = std::ptrdiff_t;
144 using value_type = Impl*;
145 using pointer = value_type*;
146 using reference = value_type&;
147
148 Iterator() = default;
149
150 Iterator& operator++() {
151 ++it_;
152 return *this;
153 }
154
155 Impl* operator*() const { return it_->first; }
156 Impl* operator->() const { return it_->first; }
157 bool operator==(const Iterator& other) const { return it_ == other.it_; }
158 bool operator!=(const Iterator& other) const { return it_ != other.it_; }
159
160 private:
161 using IteratorImpl = typename Container::const_iterator;
162 friend class ServiceSet;
163
164 explicit Iterator(IteratorImpl it) : it_(it) {}
165
166 IteratorImpl it_;
167 };
168
169 } // namespace mojo
170
171 #endif // MOJO_COMMON_SERVICE_SET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698