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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « DEPS ('k') | mojo/services/network/cookie_store_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ 5 #ifndef MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_
6 #define MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ 6 #define MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/logging.h"
10 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
11 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h" 12 #include "mojo/public/cpp/bindings/interface_ptr.h"
12 13
13 namespace mojo { 14 namespace mojo {
14 15
16 namespace internal {
17
18 // TODO(vtl): This name of this class is a little odd -- it's not a "weak
19 // pointer", but a wrapper around InterfacePtr that owns itself and can vend
20 // weak pointers to itself. Probably, with connection error callbacks instead of
21 // ErrorHandlers, this class is unneeded, and WeakInterfacePtrSet can simply
22 // own/remove interface pointers as connection errors occur.
23 // https://github.com/domokit/mojo/issues/311
15 template <typename Interface> 24 template <typename Interface>
16 class WeakInterfacePtr; 25 class WeakInterfacePtr {
26 public:
27 explicit WeakInterfacePtr(InterfacePtr<Interface> ptr)
28 : ptr_(ptr.Pass()), weak_ptr_factory_(this) {
29 ptr_.set_connection_error_handler([this]() { delete this; });
30 }
31 ~WeakInterfacePtr() {}
17 32
33 void Close() { ptr_.reset(); }
34
35 Interface* get() { return ptr_.get(); }
36
37 base::WeakPtr<WeakInterfacePtr> GetWeakPtr() {
38 return weak_ptr_factory_.GetWeakPtr();
39 }
40
41 private:
42 InterfacePtr<Interface> ptr_;
43 base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_;
44
45 DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr);
46 };
47
48 } // namespace internal
49
50 // A WeakInterfacePtrSet contains a collection of InterfacePtrs
51 // that are automatically removed from the collection and destroyed
52 // when their associated MessagePipe experiences a connection error.
53 // When the set is destroyed all of the MessagePipes will be closed.
54 // TODO(rudominer) Rename this class since the ownership of the elements
55 // is not "weak" from the point of view of the client.
18 template <typename Interface> 56 template <typename Interface>
19 class WeakInterfacePtrSet { 57 class WeakInterfacePtrSet {
20 public: 58 public:
21 WeakInterfacePtrSet() {} 59 WeakInterfacePtrSet() {}
22 ~WeakInterfacePtrSet() { CloseAll(); } 60 ~WeakInterfacePtrSet() { CloseAll(); }
23 61
62 // |ptr| must be bound to a message pipe.
24 void AddInterfacePtr(InterfacePtr<Interface> ptr) { 63 void AddInterfacePtr(InterfacePtr<Interface> ptr) {
25 auto weak_interface_ptr = new WeakInterfacePtr<Interface>(ptr.Pass()); 64 DCHECK(ptr.is_bound());
65 auto weak_interface_ptr =
66 new internal::WeakInterfacePtr<Interface>(ptr.Pass());
26 ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); 67 ptrs_.push_back(weak_interface_ptr->GetWeakPtr());
27 ClearNullInterfacePtrs(); 68 ClearNullInterfacePtrs();
28 } 69 }
29 70
71 // Applies |function| to each of the InterfacePtrs in the set.
30 template <typename FunctionType> 72 template <typename FunctionType>
31 void ForAllPtrs(FunctionType function) { 73 void ForAllPtrs(FunctionType function) {
32 for (const auto& it : ptrs_) { 74 for (const auto& it : ptrs_) {
33 if (it) 75 if (it)
34 function(it->get()); 76 function(it->get());
35 } 77 }
36 ClearNullInterfacePtrs(); 78 ClearNullInterfacePtrs();
37 } 79 }
38 80
81 // Closes the MessagePipe associated with each of the InterfacePtrs in
82 // this set and clears the set.
39 void CloseAll() { 83 void CloseAll() {
40 for (const auto& it : ptrs_) { 84 for (const auto& it : ptrs_) {
41 if (it) 85 if (it)
42 it->Close(); 86 it->Close();
43 } 87 }
44 ptrs_.clear(); 88 ptrs_.clear();
45 } 89 }
46 90
91 // TODO(rudominer) After reworking this class and eliminating the method
92 // ClearNullInterfacePtrs, this method should become const.
93 size_t size() {
94 ClearNullInterfacePtrs();
95 return ptrs_.size();
96 }
97
47 private: 98 private:
48 using WPWIPI = base::WeakPtr<WeakInterfacePtr<Interface>>; 99 using WPWIPI = base::WeakPtr<internal::WeakInterfacePtr<Interface>>;
49 100
50 void ClearNullInterfacePtrs() { 101 void ClearNullInterfacePtrs() {
51 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), [](const WPWIPI& p) { 102 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), [](const WPWIPI& p) {
52 return p.get() == nullptr; 103 return p.get() == nullptr;
53 }), ptrs_.end()); 104 }), ptrs_.end());
54 } 105 }
55 106
56 std::vector<WPWIPI> ptrs_; 107 std::vector<WPWIPI> ptrs_;
57 }; 108 };
58 109
59 template <typename Interface>
60 class WeakInterfacePtr : public ErrorHandler {
61 public:
62 explicit WeakInterfacePtr(InterfacePtr<Interface> ptr)
63 : ptr_(ptr.Pass()), weak_ptr_factory_(this) {
64 ptr_.set_error_handler(this);
65 }
66 ~WeakInterfacePtr() override {}
67
68 void Close() { ptr_.reset(); }
69
70 Interface* get() { return ptr_.get(); }
71
72 base::WeakPtr<WeakInterfacePtr> GetWeakPtr() {
73 return weak_ptr_factory_.GetWeakPtr();
74 }
75
76 private:
77 // ErrorHandler implementation
78 void OnConnectionError() override { delete this; }
79
80 InterfacePtr<Interface> ptr_;
81 base::WeakPtrFactory<WeakInterfacePtr> weak_ptr_factory_;
82
83 DISALLOW_COPY_AND_ASSIGN(WeakInterfacePtr);
84 };
85
86 } // namespace mojo 110 } // namespace mojo
87 111
88 #endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_ 112 #endif // MOJO_COMMON_WEAK_INTERFACE_PTR_SET_H_
OLDNEW
« 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