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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/common/BUILD.gn ('k') | mojo/common/service_set_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/common/service_set.h
diff --git a/mojo/common/service_set.h b/mojo/common/service_set.h
new file mode 100644
index 0000000000000000000000000000000000000000..a47953b1cd7eb3bb83d037b334c50272791db4ad
--- /dev/null
+++ b/mojo/common/service_set.h
@@ -0,0 +1,145 @@
+// Copyright 2015 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_COMMON_SERVICE_SET_H_
+#define MOJO_COMMON_SERVICE_SET_H_
+
+#include <map>
+#include <utility>
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "base/macros.h"
+#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
+
+namespace mojo {
+
+// A set of mojo services where each service's lifetime is bound to a message
+// pipe to its client. The template parameters Interface and Impl should be a
+// mojo interface and a concrete implementation of that interface, respectively.
+//
+// To create an instance of Impl in response to a request, call
+// CreateService(request.Pass(), <other args to Impl's constructor>);
+//
+// When the client of a service closes its connection, that service is
+// automatically destroyed. set_connection_error_handler() can be used to
+// register an ErrorHandler to be called when this occurs. When the handler is
+// called, the service is no longer contained in the set but is only destroyed
+// after the callback returns.
+//
+// A service may be manually destroyed by calling DestroyService(). Services
+// destroyed this way will not trigger a call to the ErrorHandler.
+template <typename Interface, typename Impl>
+class ServiceSet {
+ public:
+ using ErrorHandler = Callback<void(Impl*)>;
+ class Iterator;
+
+ ServiceSet() = default;
+
+ void set_connection_error_handler(const ErrorHandler& error_handler) {
+ error_handler_ = error_handler;
+ }
+
+ template <typename... Args>
+ Impl* CreateService(InterfaceRequest<Interface> request, Args&&... args) {
danakj 2015/11/17 21:42:17 Consider putting Emplace in the name so people rea
Sam McNally 2015/11/18 03:17:18 Done.
+ auto holder = make_scoped_ptr(
+ new Holder(this, request.Pass(), std::forward<Args>(args)...));
+ Impl* impl = holder->impl();
+ holders_.insert(std::make_pair(impl, holder.Pass()));
+ return impl;
+ }
+
+ void DestroyService(Impl* impl) {
+ size_t num_erased = holders_.erase(impl);
+ DCHECK_EQ(1u, num_erased);
+ }
+
+ void clear() { holders_.clear(); }
danakj 2015/11/17 21:42:17 These aren't member var accessors, they should be
Sam McNally 2015/11/18 03:17:18 empty() and size() are both cheap; begin() and end
danakj 2015/11/18 19:59:11 Oh, I re-read the style guide, as I've been readin
+ bool empty() const { return holders_.empty(); }
+ size_t size() const { return holders_.size(); }
+ Iterator begin() const { return Iterator(holders_.begin()); }
+ Iterator end() const { return Iterator(holders_.end()); }
+
+ private:
+ class Holder;
+ using Container = std::map<Impl*, scoped_ptr<Holder>>;
danakj 2015/11/17 21:42:17 Consider not using a typedef that is only referred
Sam McNally 2015/11/18 03:17:18 It's also used by ServiceSet::Iterator.
+
+ void OnConnectionError(Holder* holder) {
+ Impl* impl = holder->impl();
+ auto it = holders_.find(holder->impl());
+ DCHECK(it != holders_.end());
+ scoped_ptr<Holder> holder_owner = it->second.Pass();
+ holders_.erase(it);
+ if (!error_handler_.is_null())
+ error_handler_.Run(impl);
+ }
+
+ ErrorHandler error_handler_;
+ Container holders_;
+
+ DISALLOW_COPY_AND_ASSIGN(ServiceSet);
+};
+
+template <typename Interface, typename Impl>
+class ServiceSet<Interface, Impl>::Holder {
+ public:
+ template <typename... Args>
+ Holder(ServiceSet* service_set,
+ InterfaceRequest<Interface> request,
+ Args&&... args)
+ : impl_(std::forward<Args>(args)...),
+ binding_(&impl_, request.Pass()),
+ service_set_(service_set) {
+ binding_.set_connection_error_handler(
+ base::Bind(&Holder::OnConnectionError, base::Unretained(this)));
+ }
+
+ void OnConnectionError() { service_set_->OnConnectionError(this); }
+
+ Impl* impl() { return &impl_; }
+
+ private:
+ Impl impl_;
+ Binding<Interface> binding_;
+
+ // Owns this.
+ ServiceSet* const service_set_;
+
+ DISALLOW_COPY_AND_ASSIGN(Holder);
+};
+
+template <typename Interface, typename Impl>
+class ServiceSet<Interface, Impl>::Iterator {
+ public:
+ using iterator_category = std::input_iterator_tag;
+ using difference_type = std::ptrdiff_t;
+ using value_type = Impl*;
+ using pointer = value_type*;
+ using reference = value_type&;
+
+ Iterator() = default;
+
+ Iterator& operator++() {
+ ++it_;
+ return *this;
+ }
+
+ Impl* operator*() const { return it_->first; }
+ Impl* operator->() const { return it_->first; }
+ bool operator==(const Iterator& other) const { return it_ == other.it_; }
+ bool operator!=(const Iterator& other) const { return it_ != other.it_; }
+
+ private:
+ using IteratorImpl = typename Container::const_iterator;
+ friend class ServiceSet;
+
+ explicit Iterator(IteratorImpl it) : it_(it) {}
+
+ IteratorImpl it_;
+};
+
+} // namespace mojo
+
+#endif // MOJO_COMMON_SERVICE_SET_H_
« no previous file with comments | « mojo/common/BUILD.gn ('k') | mojo/common/service_set_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698