diff options
Diffstat (limited to 'mojo/public/cpp/bindings')
| -rw-r--r-- | mojo/public/cpp/bindings/BUILD.gn | 4 | ||||
| -rw-r--r-- | mojo/public/cpp/bindings/binding_set.h | 113 | ||||
| -rw-r--r-- | mojo/public/cpp/bindings/interface_ptr_set.h | 83 | ||||
| -rw-r--r-- | mojo/public/cpp/bindings/tests/pickle_unittest.cc | 6 | ||||
| -rw-r--r-- | mojo/public/cpp/bindings/tests/struct_traits_unittest.cc | 8 | ||||
| -rw-r--r-- | mojo/public/cpp/bindings/weak_binding_set.h | 118 | ||||
| -rw-r--r-- | mojo/public/cpp/bindings/weak_interface_ptr_set.h | 88 |
7 files changed, 205 insertions, 215 deletions
diff --git a/mojo/public/cpp/bindings/BUILD.gn b/mojo/public/cpp/bindings/BUILD.gn index 72079fd..6b7a015 100644 --- a/mojo/public/cpp/bindings/BUILD.gn +++ b/mojo/public/cpp/bindings/BUILD.gn @@ -11,8 +11,10 @@ source_set("bindings") { "associated_interface_ptr_info.h", "associated_interface_request.h", "binding.h", + "binding_set.h", "interface_ptr.h", "interface_ptr_info.h", + "interface_ptr_set.h", "interface_request.h", "lib/array_internal.cc", "lib/array_internal.h", @@ -85,8 +87,6 @@ source_set("bindings") { "struct_ptr.h", "struct_traits.h", "type_converter.h", - "weak_binding_set.h", - "weak_interface_ptr_set.h", ] public_deps = [ diff --git a/mojo/public/cpp/bindings/binding_set.h b/mojo/public/cpp/bindings/binding_set.h new file mode 100644 index 0000000..82e7cfb --- /dev/null +++ b/mojo/public/cpp/bindings/binding_set.h @@ -0,0 +1,113 @@ +// Copyright 2014 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_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ +#define MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ + +#include <algorithm> +#include <utility> +#include <vector> + +#include "base/macros.h" +#include "base/memory/weak_ptr.h" +#include "mojo/public/cpp/bindings/binding.h" + +namespace mojo { + +// Use this class to manage a set of bindings, which are automatically destroyed +// and removed from the set when the pipe they bound to is disconnected. +template <typename Interface> +class BindingSet { + public: + using GenericInterface = typename Interface::GenericInterface; + + BindingSet() {} + ~BindingSet() { CloseAllBindings(); } + + void set_connection_error_handler(const Closure& error_handler) { + error_handler_ = error_handler; + } + + void AddBinding(Interface* impl, InterfaceRequest<GenericInterface> request) { + auto binding = new Element(impl, std::move(request)); + binding->set_connection_error_handler([this]() { OnConnectionError(); }); + bindings_.push_back(binding->GetWeakPtr()); + } + + // Returns an InterfacePtr bound to one end of a pipe whose other end is + // bound to |this|. + InterfacePtr<Interface> CreateInterfacePtrAndBind(Interface* impl) { + InterfacePtr<Interface> interface_ptr; + AddBinding(impl, GetProxy(&interface_ptr)); + return interface_ptr; + } + + void CloseAllBindings() { + for (const auto& it : bindings_) { + if (it) { + it->Close(); + delete it.get(); + } + } + bindings_.clear(); + } + + bool empty() const { return bindings_.empty(); } + + private: + class Element { + public: + using GenericInterface = typename Interface::GenericInterface; + + Element(Interface* impl, InterfaceRequest<GenericInterface> request) + : binding_(impl, std::move(request)), weak_ptr_factory_(this) { + binding_.set_connection_error_handler([this]() { OnConnectionError(); }); + } + + ~Element() {} + + void set_connection_error_handler(const Closure& error_handler) { + error_handler_ = error_handler; + } + + base::WeakPtr<Element> GetWeakPtr() { + return weak_ptr_factory_.GetWeakPtr(); + } + + void Close() { binding_.Close(); } + + void OnConnectionError() { + Closure error_handler = error_handler_; + delete this; + error_handler.Run(); + } + + private: + Binding<Interface> binding_; + Closure error_handler_; + base::WeakPtrFactory<Element> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(Element); + }; + + void OnConnectionError() { + // Clear any deleted bindings. + bindings_.erase(std::remove_if(bindings_.begin(), bindings_.end(), + [](const base::WeakPtr<Element>& p) { + return p.get() == nullptr; + }), + bindings_.end()); + + error_handler_.Run(); + } + + Closure error_handler_; + std::vector<base::WeakPtr<Element>> bindings_; + + DISALLOW_COPY_AND_ASSIGN(BindingSet); +}; + +} // namespace mojo + +#endif // MOJO_PUBLIC_CPP_BINDINGS_BINDING_SET_H_ diff --git a/mojo/public/cpp/bindings/interface_ptr_set.h b/mojo/public/cpp/bindings/interface_ptr_set.h new file mode 100644 index 0000000..c5d402d --- /dev/null +++ b/mojo/public/cpp/bindings/interface_ptr_set.h @@ -0,0 +1,83 @@ +// Copyright 2014 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_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ +#define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ + +#include <utility> +#include <vector> + +#include "base/macros.h" +#include "base/memory/weak_ptr.h" +#include "mojo/public/cpp/bindings/interface_ptr.h" + +namespace mojo { + +template <typename Interface> +class InterfacePtrSet { + public: + InterfacePtrSet() {} + ~InterfacePtrSet() { CloseAll(); } + + void AddInterfacePtr(InterfacePtr<Interface> ptr) { + auto weak_interface_ptr = new Element(std::move(ptr)); + ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); + ClearNullInterfacePtrs(); + } + + template <typename FunctionType> + void ForAllPtrs(FunctionType function) { + for (const auto& it : ptrs_) { + if (it) + function(it->get()); + } + ClearNullInterfacePtrs(); + } + + void CloseAll() { + for (const auto& it : ptrs_) { + if (it) + it->Close(); + } + ptrs_.clear(); + } + + private: + class Element { + public: + explicit Element(InterfacePtr<Interface> ptr) + : ptr_(std::move(ptr)), weak_ptr_factory_(this) { + ptr_.set_connection_error_handler([this]() { delete this; }); + } + ~Element() {} + + void Close() { ptr_.reset(); } + + Interface* get() { return ptr_.get(); } + + base::WeakPtr<Element> GetWeakPtr() { + return weak_ptr_factory_.GetWeakPtr(); + } + + private: + InterfacePtr<Interface> ptr_; + base::WeakPtrFactory<Element> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(Element); + }; + + void ClearNullInterfacePtrs() { + ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), + [](const base::WeakPtr<Element>& p) { + return p.get() == nullptr; + }), + ptrs_.end()); + } + + std::vector<base::WeakPtr<Element>> ptrs_; +}; + +} // namespace mojo + +#endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ diff --git a/mojo/public/cpp/bindings/tests/pickle_unittest.cc b/mojo/public/cpp/bindings/tests/pickle_unittest.cc index a0d7a2e..33961b4 100644 --- a/mojo/public/cpp/bindings/tests/pickle_unittest.cc +++ b/mojo/public/cpp/bindings/tests/pickle_unittest.cc @@ -9,10 +9,10 @@ #include "base/logging.h" #include "base/message_loop/message_loop.h" #include "base/run_loop.h" +#include "mojo/public/cpp/bindings/binding_set.h" #include "mojo/public/cpp/bindings/interface_request.h" #include "mojo/public/cpp/bindings/tests/pickled_struct_blink.h" #include "mojo/public/cpp/bindings/tests/pickled_struct_chromium.h" -#include "mojo/public/cpp/bindings/weak_binding_set.h" #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom-blink.h" #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom-chromium.h" #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom.h" @@ -134,9 +134,9 @@ class PickleTest : public testing::Test { private: base::MessageLoop loop_; ChromiumPicklePasserImpl chromium_service_; - mojo::WeakBindingSet<chromium::PicklePasser> chromium_bindings_; + mojo::BindingSet<chromium::PicklePasser> chromium_bindings_; BlinkPicklePasserImpl blink_service_; - mojo::WeakBindingSet<blink::PicklePasser> blink_bindings_; + mojo::BindingSet<blink::PicklePasser> blink_bindings_; }; } // namespace diff --git a/mojo/public/cpp/bindings/tests/struct_traits_unittest.cc b/mojo/public/cpp/bindings/tests/struct_traits_unittest.cc index 02fb53f..b4c3e49 100644 --- a/mojo/public/cpp/bindings/tests/struct_traits_unittest.cc +++ b/mojo/public/cpp/bindings/tests/struct_traits_unittest.cc @@ -7,11 +7,11 @@ #include "base/logging.h" #include "base/message_loop/message_loop.h" #include "base/run_loop.h" +#include "mojo/public/cpp/bindings/binding_set.h" #include "mojo/public/cpp/bindings/interface_request.h" #include "mojo/public/cpp/bindings/tests/rect_blink.h" #include "mojo/public/cpp/bindings/tests/rect_chromium.h" #include "mojo/public/cpp/bindings/tests/struct_with_traits_impl.h" -#include "mojo/public/cpp/bindings/weak_binding_set.h" #include "mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.h" #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom-blink.h" #include "mojo/public/interfaces/bindings/tests/test_native_types.mojom-chromium.h" @@ -126,12 +126,12 @@ class StructTraitsTest : public testing::Test, base::MessageLoop loop_; ChromiumRectServiceImpl chromium_service_; - mojo::WeakBindingSet<chromium::RectService> chromium_bindings_; + mojo::BindingSet<chromium::RectService> chromium_bindings_; BlinkRectServiceImpl blink_service_; - mojo::WeakBindingSet<blink::RectService> blink_bindings_; + mojo::BindingSet<blink::RectService> blink_bindings_; - mojo::WeakBindingSet<TraitsTestService> traits_test_bindings_; + mojo::BindingSet<TraitsTestService> traits_test_bindings_; }; } // namespace diff --git a/mojo/public/cpp/bindings/weak_binding_set.h b/mojo/public/cpp/bindings/weak_binding_set.h deleted file mode 100644 index bf06a28..0000000 --- a/mojo/public/cpp/bindings/weak_binding_set.h +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright 2014 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_PUBLIC_CPP_BINDINGS_WEAK_BINDING_SET_H_ -#define MOJO_PUBLIC_CPP_BINDINGS_WEAK_BINDING_SET_H_ - -#include <algorithm> -#include <utility> -#include <vector> - -#include "base/macros.h" -#include "base/memory/weak_ptr.h" -#include "mojo/public/cpp/bindings/binding.h" - -namespace mojo { - -template <typename Interface> -class WeakBinding; - -// Use this class to manage a set of weak pointers to bindings each of which is -// owned by the pipe they are bound to. -template <typename Interface> -class WeakBindingSet { - public: - using GenericInterface = typename Interface::GenericInterface; - - WeakBindingSet() {} - ~WeakBindingSet() { CloseAllBindings(); } - - void set_connection_error_handler(const Closure& error_handler) { - error_handler_ = error_handler; - } - - void AddBinding(Interface* impl, InterfaceRequest<GenericInterface> request) { - auto binding = new WeakBinding<Interface>(impl, std::move(request)); - binding->set_connection_error_handler([this]() { OnConnectionError(); }); - bindings_.push_back(binding->GetWeakPtr()); - } - - // Returns an InterfacePtr bound to one end of a pipe whose other end is - // bound to |this|. - InterfacePtr<Interface> CreateInterfacePtrAndBind(Interface* impl) { - InterfacePtr<Interface> interface_ptr; - AddBinding(impl, GetProxy(&interface_ptr)); - return interface_ptr; - } - - void CloseAllBindings() { - for (const auto& it : bindings_) { - if (it) { - it->Close(); - delete it.get(); - } - } - bindings_.clear(); - } - - bool empty() const { return bindings_.empty(); } - - private: - void OnConnectionError() { - // Clear any deleted bindings. - bindings_.erase( - std::remove_if(bindings_.begin(), bindings_.end(), - [](const base::WeakPtr<WeakBinding<Interface>>& p) { - return p.get() == nullptr; - }), - bindings_.end()); - - error_handler_.Run(); - } - - Closure error_handler_; - std::vector<base::WeakPtr<WeakBinding<Interface>>> bindings_; - - DISALLOW_COPY_AND_ASSIGN(WeakBindingSet); -}; - -template <typename Interface> -class WeakBinding { - public: - using GenericInterface = typename Interface::GenericInterface; - - WeakBinding(Interface* impl, InterfaceRequest<GenericInterface> request) - : binding_(impl, std::move(request)), weak_ptr_factory_(this) { - binding_.set_connection_error_handler([this]() { OnConnectionError(); }); - } - - ~WeakBinding() {} - - void set_connection_error_handler(const Closure& error_handler) { - error_handler_ = error_handler; - } - - base::WeakPtr<WeakBinding> GetWeakPtr() { - return weak_ptr_factory_.GetWeakPtr(); - } - - void Close() { binding_.Close(); } - - void OnConnectionError() { - Closure error_handler = error_handler_; - delete this; - error_handler.Run(); - } - - private: - Binding<Interface> binding_; - Closure error_handler_; - base::WeakPtrFactory<WeakBinding> weak_ptr_factory_; - - DISALLOW_COPY_AND_ASSIGN(WeakBinding); -}; - -} // namespace mojo - -#endif // MOJO_PUBLIC_CPP_BINDINGS_WEAK_BINDING_SET_H_ diff --git a/mojo/public/cpp/bindings/weak_interface_ptr_set.h b/mojo/public/cpp/bindings/weak_interface_ptr_set.h deleted file mode 100644 index f92a5ed..0000000 --- a/mojo/public/cpp/bindings/weak_interface_ptr_set.h +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright 2014 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_PUBLIC_CPP_BINDINGS_WEAK_INTERFACE_PTR_SET_H_ -#define MOJO_PUBLIC_CPP_BINDINGS_WEAK_INTERFACE_PTR_SET_H_ - -#include <utility> -#include <vector> - -#include "base/macros.h" -#include "base/memory/weak_ptr.h" -#include "mojo/public/cpp/bindings/interface_ptr.h" - -namespace mojo { - -template <typename Interface> -class WeakInterfacePtr; - -template <typename Interface> -class WeakInterfacePtrSet { - public: - WeakInterfacePtrSet() {} - ~WeakInterfacePtrSet() { CloseAll(); } - - void AddInterfacePtr(InterfacePtr<Interface> ptr) { - auto weak_interface_ptr = new WeakInterfacePtr<Interface>(std::move(ptr)); - ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); - ClearNullInterfacePtrs(); - } - - template <typename FunctionType> - void ForAllPtrs(FunctionType function) { - for (const auto& it : ptrs_) { - if (it) - function(it->get()); - } - ClearNullInterfacePtrs(); - } - - void CloseAll() { - for (const auto& it : ptrs_) { - if (it) - it->Close(); - } - ptrs_.clear(); - } - - private: - using WPWIPI = base::WeakPtr<WeakInterfacePtr<Interface>>; - - void ClearNullInterfacePtrs() { - ptrs_.erase( - std::remove_if(ptrs_.begin(), ptrs_.end(), - [](const WPWIPI& p) { return p.get() == nullptr; }), - ptrs_.end()); - } - - std::vector<WPWIPI> ptrs_; -}; - -template <typename Interface> -class WeakInterfacePtr { - public: - explicit WeakInterfacePtr(InterfacePtr<Interface> ptr) - : ptr_(std::move(ptr)), weak_ptr_factory_(this) { - ptr_.set_connection_error_handler([this]() { delete this; }); - } - ~WeakInterfacePtr() {} - - void Close() { ptr_.reset(); } - - Interface* get() { return ptr_.get(); } - - base::WeakPtr<WeakInterfacePtr> GetWeakPtr() { - return weak_ptr_factory_.GetWeakPtr(); - } - - private: - InterfacePtr<Interface> ptr_; - base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_; - - DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr); -}; - -} // namespace mojo - -#endif // MOJO_PUBLIC_CPP_BINDINGS_WEAK_INTERFACE_PTR_SET_H_ |
