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

Side by Side Diff: mojo/public/cpp/bindings/associated_interface_ptr_set.h

Issue 1839223003: Add basic Chrome interaction with the mash shelf. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use associated interfaces for observers and item delegates. Created 4 years, 8 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
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_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_SET_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_SET_H_
7 7
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "mojo/public/cpp/bindings/interface_ptr.h" 13 #include "mojo/public/cpp/bindings/associated_interface_ptr.h"
sky 2016/04/07 22:02:46 Can you separate this change out and add Yuzhu? I
msw 2016/04/08 21:20:48 Done: https://codereview.chromium.org/1872903003
14 14
15 namespace mojo { 15 namespace mojo {
16 16
17 template <typename Interface> 17 template <typename Interface>
18 class InterfacePtrSet { 18 class AssociatedInterfacePtrSet {
19 public: 19 public:
20 InterfacePtrSet() {} 20 AssociatedInterfacePtrSet() {}
21 ~InterfacePtrSet() { CloseAll(); } 21 ~AssociatedInterfacePtrSet() { CloseAll(); }
22 22
23 void AddInterfacePtr(InterfacePtr<Interface> ptr) { 23 void AddInterfacePtr(AssociatedInterfacePtr<Interface> ptr) {
24 auto weak_interface_ptr = new Element(std::move(ptr)); 24 auto weak_interface_ptr = new Element(std::move(ptr));
25 ptrs_.push_back(weak_interface_ptr->GetWeakPtr()); 25 ptrs_.push_back(weak_interface_ptr->GetWeakPtr());
26 ClearNullInterfacePtrs(); 26 ClearNullInterfacePtrs();
27 } 27 }
28 28
29 template <typename FunctionType> 29 template <typename FunctionType>
30 void ForAllPtrs(FunctionType function) { 30 void ForAllPtrs(FunctionType function) {
31 for (const auto& it : ptrs_) { 31 for (const auto& it : ptrs_) {
32 if (it) 32 if (it)
33 function(it->get()); 33 function(it->get());
34 } 34 }
35 ClearNullInterfacePtrs(); 35 ClearNullInterfacePtrs();
36 } 36 }
37 37
38 void CloseAll() { 38 void CloseAll() {
39 for (const auto& it : ptrs_) { 39 for (const auto& it : ptrs_) {
40 if (it) 40 if (it)
41 it->Close(); 41 it->Close();
42 } 42 }
43 ptrs_.clear(); 43 ptrs_.clear();
44 } 44 }
45 45
46 private: 46 private:
47 class Element { 47 class Element {
48 public: 48 public:
49 explicit Element(InterfacePtr<Interface> ptr) 49 explicit Element(AssociatedInterfacePtr<Interface> ptr)
50 : ptr_(std::move(ptr)), weak_ptr_factory_(this) { 50 : ptr_(std::move(ptr)), weak_ptr_factory_(this) {
51 ptr_.set_connection_error_handler([this]() { delete this; }); 51 ptr_.set_connection_error_handler([this]() { delete this; });
52 } 52 }
53 ~Element() {} 53 ~Element() {}
54 54
55 void Close() { ptr_.reset(); } 55 void Close() { ptr_.reset(); }
56 56
57 Interface* get() { return ptr_.get(); } 57 Interface* get() { return ptr_.get(); }
58 58
59 base::WeakPtr<Element> GetWeakPtr() { 59 base::WeakPtr<Element> GetWeakPtr() {
60 return weak_ptr_factory_.GetWeakPtr(); 60 return weak_ptr_factory_.GetWeakPtr();
61 } 61 }
62 62
63 private: 63 private:
64 InterfacePtr<Interface> ptr_; 64 AssociatedInterfacePtr<Interface> ptr_;
65 base::WeakPtrFactory<Element> weak_ptr_factory_; 65 base::WeakPtrFactory<Element> weak_ptr_factory_;
66 66
67 DISALLOW_COPY_AND_ASSIGN(Element); 67 DISALLOW_COPY_AND_ASSIGN(Element);
68 }; 68 };
69 69
70 void ClearNullInterfacePtrs() { 70 void ClearNullInterfacePtrs() {
71 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(), 71 ptrs_.erase(std::remove_if(ptrs_.begin(), ptrs_.end(),
72 [](const base::WeakPtr<Element>& p) { 72 [](const base::WeakPtr<Element>& p) {
73 return p.get() == nullptr; 73 return p.get() == nullptr;
74 }), 74 }),
75 ptrs_.end()); 75 ptrs_.end());
76 } 76 }
77 77
78 std::vector<base::WeakPtr<Element>> ptrs_; 78 std::vector<base::WeakPtr<Element>> ptrs_;
79 }; 79 };
80 80
81 } // namespace mojo 81 } // namespace mojo
82 82
83 #endif // MOJO_PUBLIC_CPP_BINDINGS_INTERFACE_PTR_SET_H_ 83 #endif // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_SET_H_
OLDNEW
« ash/mus/shelf_delegate_mus.cc ('K') | « mojo/public/cpp/bindings/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698