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

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 <map>
Anand Mistry (off Chromium) 2015/11/12 07:11:32 nit: blank line after stl includes
Sam McNally 2015/11/13 00:49:59 Done.
9 #include "base/bind.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
16 // A set of mojo services where each service's lifetime is bound to a message
17 // pipe to its client. The template parameters Interface and Impl should be a
18 // mojo interface and a concrete implementation of that interface, respectively.
19 //
20 // To create an instance of Impl in response to a request, call
21 // CreateService(request.Pass(), <other args to Impl's constructor>);
22 //
23 // When the client of a service closes its connection, that service is
24 // automatically destroyed. set_connection_error_handler() can be used to
25 // register an ErrorHandler to be called when this occurs. When the handler is
26 // called, the service is no longer contained in the set but is only destroyed
27 // after the callback returns.
28 //
29 // A service may be manually destroyed by calling DestroyService(). Services
30 // destroyed this way will not trigger a call to the ErrorHandler.
31 template <typename Interface, typename Impl>
32 class ServiceSet {
33 public:
34 using ErrorHandler = Callback<void(Impl*)>;
35 class Iterator;
36
37 ServiceSet() = default;
38
39 void set_connection_error_handler(const ErrorHandler& error_handler) {
40 error_handler_ = error_handler;
41 }
42
43 template <typename... Args>
44 Impl* CreateService(InterfaceRequest<Interface> request, Args&&... args) {
45 auto holder = make_scoped_ptr(
46 new Holder(this, request.Pass(), std::forward<Args>(args)...));
Anand Mistry (off Chromium) 2015/11/12 07:11:32 #include <utility> for std::forward
Sam McNally 2015/11/13 00:49:59 Done.
47 Impl* impl = holder->impl();
48 holders_.insert(std::make_pair(impl, holder.Pass()));
49 return impl;
50 }
51
52 void DestroyService(Impl* impl) {
53 size_t num_erased = holders_.erase(impl);
54 DCHECK_EQ(1u, num_erased);
55 }
56
57 void clear() { holders_.clear(); }
58 bool empty() const { return holders_.empty(); }
59 size_t size() const { return holders_.size(); }
60 Iterator begin() const { return Iterator(holders_.begin()); }
61 Iterator end() const { return Iterator(holders_.end()); }
62
63 private:
64 class Holder;
65 using Container = std::map<Impl*, scoped_ptr<Holder>>;
66
67 void OnConnectionError(Holder* holder) {
68 Impl* impl = holder->impl();
69 auto it = holders_.find(holder->impl());
70 DCHECK(it != holders_.end());
71 scoped_ptr<Holder> holder_owner = it->second.Pass();
72 holders_.erase(it);
73 if (!error_handler_.is_null())
74 error_handler_.Run(impl);
75 }
76
77 ErrorHandler error_handler_;
78 Container holders_;
79
80 DISALLOW_COPY_AND_ASSIGN(ServiceSet);
81 };
82
83 template <typename Interface, typename Impl>
84 class ServiceSet<Interface, Impl>::Holder {
85 public:
86 template <typename... Args>
87 Holder(ServiceSet* service_set,
88 InterfaceRequest<Interface> request,
89 Args&&... args)
90 : impl_(std::forward<Args>(args)...),
91 binding_(&impl_, request.Pass()),
92 service_set_(service_set) {
93 binding_.set_connection_error_handler(
94 base::Bind(&Holder::OnConnectionError, base::Unretained(this)));
95 }
96
97 void OnConnectionError() { service_set_->OnConnectionError(this); }
98
99 Impl* impl() { return &impl_; }
100
101 private:
102 Impl impl_;
103 Binding<Interface> binding_;
104
105 // Owns this.
106 ServiceSet* const service_set_;
107
108 DISALLOW_COPY_AND_ASSIGN(Holder);
109 };
110
111 template <typename Interface, typename Impl>
112 class ServiceSet<Interface, Impl>::Iterator {
113 public:
114 using iterator_category = std::input_iterator_tag;
115 using difference_type = std::ptrdiff_t;
116 using value_type = Impl*;
117 using pointer = value_type*;
118 using reference = value_type&;
119
120 Iterator() = default;
121
122 Iterator& operator++() {
123 ++it_;
124 return *this;
125 }
126
127 Impl* operator*() const { return it_->first; }
128 Impl* operator->() const { return it_->first; }
129 bool operator==(const Iterator& other) const { return it_ == other.it_; }
130 bool operator!=(const Iterator& other) const { return it_ != other.it_; }
131
132 private:
133 using IteratorImpl = typename Container::const_iterator;
134 friend class ServiceSet;
135
136 explicit Iterator(IteratorImpl it) : it_(it) {}
137
138 IteratorImpl it_;
139 };
140
141 } // namespace mojo
142
143 #endif // MOJO_COMMON_SERVICE_SET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698