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

Unified Diff: mojo/common/weak_interface_ptr_set.h

Issue 1227373002: Roll mojo SDK to 734c6e1652ff2f3b696e441722838f453f4f9b42 (Closed) Base URL: https://github.com/domokit/monet.git@master
Patch Set: Follow review Created 5 years, 5 months 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 | « DEPS ('k') | mojo/services/network/cookie_store_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/common/weak_interface_ptr_set.h
diff --git a/mojo/common/weak_interface_ptr_set.h b/mojo/common/weak_interface_ptr_set.h
index e2e88a5c4f3c178525b7ca64504fcaacb9e40409..f93c0259574a7b7cadcd51023c41b902f31fc54a 100644
--- a/mojo/common/weak_interface_ptr_set.h
+++ b/mojo/common/weak_interface_ptr_set.h
@@ -7,26 +7,68 @@
#include <vector>
+#include "base/logging.h"
#include "base/memory/weak_ptr.h"
-#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h"
+#include "mojo/public/cpp/bindings/interface_ptr.h"
namespace mojo {
+namespace internal {
+
+// TODO(vtl): This name of this class is a little odd -- it's not a "weak
+// pointer", but a wrapper around InterfacePtr that owns itself and can vend
+// weak pointers to itself. Probably, with connection error callbacks instead of
+// ErrorHandlers, this class is unneeded, and WeakInterfacePtrSet can simply
+// own/remove interface pointers as connection errors occur.
+// https://github.com/domokit/mojo/issues/311
template <typename Interface>
-class WeakInterfacePtr;
+class WeakInterfacePtr {
+ public:
+ explicit WeakInterfacePtr(InterfacePtr<Interface> ptr)
+ : ptr_(ptr.Pass()), 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 internal
+
+// A WeakInterfacePtrSet contains a collection of InterfacePtrs
+// that are automatically removed from the collection and destroyed
+// when their associated MessagePipe experiences a connection error.
+// When the set is destroyed all of the MessagePipes will be closed.
+// TODO(rudominer) Rename this class since the ownership of the elements
+// is not "weak" from the point of view of the client.
template <typename Interface>
class WeakInterfacePtrSet {
public:
WeakInterfacePtrSet() {}
~WeakInterfacePtrSet() { CloseAll(); }
+ // |ptr| must be bound to a message pipe.
void AddInterfacePtr(InterfacePtr<Interface> ptr) {
- auto weak_interface_ptr = new WeakInterfacePtr<Interface>(ptr.Pass());
+ DCHECK(ptr.is_bound());
+ auto weak_interface_ptr =
+ new internal::WeakInterfacePtr<Interface>(ptr.Pass());
ptrs_.push_back(weak_interface_ptr->GetWeakPtr());
ClearNullInterfacePtrs();
}
+ // Applies |function| to each of the InterfacePtrs in the set.
template <typename FunctionType>
void ForAllPtrs(FunctionType function) {
for (const auto& it : ptrs_) {
@@ -36,6 +78,8 @@ class WeakInterfacePtrSet {
ClearNullInterfacePtrs();
}
+ // Closes the MessagePipe associated with each of the InterfacePtrs in
+ // this set and clears the set.
void CloseAll() {
for (const auto& it : ptrs_) {
if (it)
@@ -44,8 +88,15 @@ class WeakInterfacePtrSet {
ptrs_.clear();
}
+ // TODO(rudominer) After reworking this class and eliminating the method
+ // ClearNullInterfacePtrs, this method should become const.
+ size_t size() {
+ ClearNullInterfacePtrs();
+ return ptrs_.size();
+ }
+
private:
- using WPWIPI = base::WeakPtr<WeakInterfacePtr<Interface>>;
+ using WPWIPI = base::WeakPtr<internal::WeakInterfacePtr<Interface>>;
void ClearNullInterfacePtrs() {
ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), [](const WPWIPI& p) {
@@ -56,33 +107,6 @@ class WeakInterfacePtrSet {
std::vector<WPWIPI> ptrs_;
};
-template <typename Interface>
-class WeakInterfacePtr : public ErrorHandler {
- public:
- explicit WeakInterfacePtr(InterfacePtr<Interface> ptr)
- : ptr_(ptr.Pass()), weak_ptr_factory_(this) {
- ptr_.set_error_handler(this);
- }
- ~WeakInterfacePtr() override {}
-
- void Close() { ptr_.reset(); }
-
- Interface* get() { return ptr_.get(); }
-
- base::WeakPtr<WeakInterfacePtr> GetWeakPtr() {
- return weak_ptr_factory_.GetWeakPtr();
- }
-
- private:
- // ErrorHandler implementation
- void OnConnectionError() override { delete this; }
-
- InterfacePtr<Interface> ptr_;
- base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_;
-
- DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr);
-};
-
} // namespace mojo
#endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_
« no previous file with comments | « DEPS ('k') | mojo/services/network/cookie_store_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698