| Index: mojo/common/strong_binding_set.h
|
| diff --git a/mojo/common/strong_binding_set.h b/mojo/common/strong_binding_set.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..f9d9349b52601ae357c965d669242b7562acb6b0
|
| --- /dev/null
|
| +++ b/mojo/common/strong_binding_set.h
|
| @@ -0,0 +1,156 @@
|
| +// 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_STRONG_BINDING_SET_H_
|
| +#define MOJO_COMMON_STRONG_BINDING_SET_H_
|
| +
|
| +#include <iterator>
|
| +#include <map>
|
| +#include <type_traits>
|
| +#include <utility>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/logging.h"
|
| +#include "base/macros.h"
|
| +#include "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 parameter Impl should be a
|
| +// a concrete implementation of a mojo interface. The InterfaceOverride template
|
| +// parameter is only required if Impl implements more than one mojo interface.
|
| +//
|
| +// To create an instance of Impl in response to a request, call
|
| +// EmplaceService(std::move(request), <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 Impl, typename InterfaceOverride = Impl>
|
| +class StrongBindingSet {
|
| + public:
|
| + using ErrorHandler = Callback<void(Impl*)>;
|
| + class Iterator;
|
| + using Interface = typename std::decay<decltype(
|
| + *std::declval<typename InterfaceOverride::Stub_>().sink())>::type;
|
| +
|
| + StrongBindingSet() = default;
|
| +
|
| + void set_connection_error_handler(const ErrorHandler& error_handler) {
|
| + error_handler_ = error_handler;
|
| + }
|
| +
|
| + template <typename... Args>
|
| + Impl* EmplaceService(InterfaceRequest<Interface> request,
|
| + Args&&... args) {
|
| + auto holder = make_scoped_ptr(
|
| + new Holder(this, std::move(request), std::forward<Args>(args)...));
|
| + Impl* impl = holder->impl();
|
| + holders_.insert(std::make_pair(impl, std::move(holder)));
|
| + 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 = std::map<Impl*, scoped_ptr<Holder>>;
|
| +
|
| + void OnConnectionError(Holder* holder) {
|
| + Impl* impl = holder->impl();
|
| + auto it = holders_.find(holder->impl());
|
| + DCHECK(it != holders_.end());
|
| + scoped_ptr<Holder> holder_owner = std::move(it->second);
|
| + holders_.erase(it);
|
| + if (!error_handler_.is_null())
|
| + error_handler_.Run(impl);
|
| + }
|
| +
|
| + ErrorHandler error_handler_;
|
| + Container holders_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(StrongBindingSet);
|
| +};
|
| +
|
| +template <typename Impl, typename Interface>
|
| +class StrongBindingSet<Impl, Interface>::Holder {
|
| + public:
|
| + template <typename... Args>
|
| + Holder(StrongBindingSet* service_set,
|
| + InterfaceRequest<Interface> request,
|
| + Args&&... args)
|
| + : impl_(std::forward<Args>(args)...),
|
| + binding_(&impl_, std::move(request)),
|
| + 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.
|
| + StrongBindingSet* const service_set_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(Holder);
|
| +};
|
| +
|
| +template <typename Impl, typename Interface>
|
| +class StrongBindingSet<Impl, Interface>::Iterator
|
| + : public std::iterator<std::bidirectional_iterator_tag, Impl*> {
|
| + public:
|
| + Iterator() = default;
|
| +
|
| + Iterator& operator++() {
|
| + ++it_;
|
| + return *this;
|
| + }
|
| +
|
| + Iterator operator++(int) { return Iterator(it_++); }
|
| +
|
| + Iterator& operator--() {
|
| + --it_;
|
| + return *this;
|
| + }
|
| +
|
| + Iterator operator--(int) { return Iterator(it_--); }
|
| +
|
| + 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 StrongBindingSet;
|
| +
|
| + explicit Iterator(IteratorImpl it) : it_(it) {}
|
| +
|
| + IteratorImpl it_;
|
| +};
|
| +
|
| +} // namespace mojo
|
| +
|
| +#endif // MOJO_COMMON_STRONG_BINDING_SET_H_
|
|
|