| 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..fdab3efbd0ac16f22f21db6aea22d55b6b2992f8
|
| --- /dev/null
|
| +++ b/mojo/common/service_set.h
|
| @@ -0,0 +1,171 @@
|
| +// 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 "base/bind.h"
|
| +#include "base/containers/scoped_ptr_map.h"
|
| +#include "base/logging.h"
|
| +#include "base/macros.h"
|
| +#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
|
| +
|
| +namespace mojo {
|
| +namespace internal {
|
| +
|
| +template <typename T>
|
| +struct RemoveReference {
|
| + using type = T;
|
| +};
|
| +
|
| +template <typename T>
|
| +struct RemoveReference<T&> {
|
| + using type = T;
|
| +};
|
| +
|
| +template <typename T>
|
| +struct RemoveReference<T&&> {
|
| + using type = T;
|
| +};
|
| +
|
| +// These are a copy of std::forward. TODO(sammc): Remove when C++11 library
|
| +// features are allowed.
|
| +template <typename T>
|
| +T&& Forward(typename RemoveReference<T>::type& t) {
|
| + return static_cast<T&&>(t);
|
| +}
|
| +
|
| +template <typename T>
|
| +T&& Forward(typename RemoveReference<T>::type&& t) {
|
| + return static_cast<T&&>(t);
|
| +}
|
| +
|
| +} // namespace internal
|
| +
|
| +// 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) {
|
| + auto holder = make_scoped_ptr(
|
| + new Holder(this, request.Pass(), internal::Forward<Args>(args)...));
|
| + Impl* impl = holder->impl();
|
| + holders_.insert(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(); }
|
| + 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 = base::ScopedPtrMap<Impl*, scoped_ptr<Holder>>;
|
| +
|
| + void OnConnectionError(Holder* holder) {
|
| + Impl* impl = holder->impl();
|
| + scoped_ptr<Holder> holder_owner = holders_.take_and_erase(holder->impl());
|
| + DCHECK(holder);
|
| + 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_(internal::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_
|
|
|