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

Side by Side Diff: base/memory/weak_ptr.cc

Issue 1602443003: Improve GetWeakPtr() to better protect against unsafe usage patterns. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 3 years, 9 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 | « base/memory/weak_ptr.h ('k') | base/memory/weak_ptr_unittest.cc » ('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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "base/memory/weak_ptr.h" 5 #include "base/memory/weak_ptr.h"
6 6
7 namespace base { 7 namespace base {
8 namespace internal { 8 namespace internal {
9 9
10 WeakReference::Flag::Flag() : is_valid_(true) { 10 WeakReference::Flag::Flag() : is_valid_(true) {
11 // Flags only become bound when checked for validity, or invalidated, 11 // Flags only become bound when checked for validity, or invalidated,
12 // so that we can check that later validity/invalidation operations on 12 // so that we can check that later validity/invalidation operations on
13 // the same Flag take place on the same sequenced thread. 13 // the same Flag take place on the same sequenced thread.
14 sequence_checker_.DetachFromSequence(); 14 sequence_checker_.DetachFromSequence();
15 } 15 }
16 16
17 void WeakReference::Flag::Invalidate() { 17 void WeakReference::Flag::Invalidate() {
18 // The flag being invalidated with a single ref implies that there are no 18 DCHECK(sequence_checker_.CalledOnValidSequence())
19 // weak pointers in existence. Allow deletion on other thread in this case.
20 DCHECK(sequence_checker_.CalledOnValidSequence() || HasOneRef())
21 << "WeakPtrs must be invalidated on the same sequenced thread."; 19 << "WeakPtrs must be invalidated on the same sequenced thread.";
22 is_valid_ = false; 20 is_valid_ = false;
23 } 21 }
24 22
25 bool WeakReference::Flag::IsValid() const { 23 bool WeakReference::Flag::IsValid() const {
26 DCHECK(sequence_checker_.CalledOnValidSequence()) 24 DCHECK(sequence_checker_.CalledOnValidSequence())
27 << "WeakPtrs must be checked on the same sequenced thread."; 25 << "WeakPtrs must be checked on the same sequenced thread.";
28 return is_valid_; 26 return is_valid_;
29 } 27 }
30 28
29 void WeakReference::Flag::DetachFromSequence() {
30 DCHECK(HasOneRef()) << "Cannot detach from Sequence while WeakPtrs exist.";
31 sequence_checker_.DetachFromSequence();
32 }
33
31 WeakReference::Flag::~Flag() { 34 WeakReference::Flag::~Flag() {
32 } 35 }
33 36
34 WeakReference::WeakReference() { 37 WeakReference::WeakReference() {
35 } 38 }
36 39
37 WeakReference::WeakReference(const Flag* flag) : flag_(flag) { 40 WeakReference::WeakReference(const scoped_refptr<Flag>& flag) : flag_(flag) {}
38 }
39 41
40 WeakReference::~WeakReference() { 42 WeakReference::~WeakReference() {
41 } 43 }
42 44
43 WeakReference::WeakReference(WeakReference&& other) = default; 45 WeakReference::WeakReference(WeakReference&& other) = default;
44 46
45 WeakReference::WeakReference(const WeakReference& other) = default; 47 WeakReference::WeakReference(const WeakReference& other) = default;
46 48
47 bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); } 49 bool WeakReference::is_valid() const {
50 return flag_ && flag_->IsValid();
51 }
48 52
49 WeakReferenceOwner::WeakReferenceOwner() { 53 WeakReferenceOwner::WeakReferenceOwner() {
50 } 54 }
51 55
52 WeakReferenceOwner::~WeakReferenceOwner() { 56 WeakReferenceOwner::~WeakReferenceOwner() {
53 Invalidate(); 57 Invalidate();
54 } 58 }
55 59
56 WeakReference WeakReferenceOwner::GetRef() const { 60 WeakReference WeakReferenceOwner::GetRef() const {
57 // If we hold the last reference to the Flag then create a new one. 61 if (!flag_)
58 if (!HasRefs())
59 flag_ = new WeakReference::Flag(); 62 flag_ = new WeakReference::Flag();
60 63
61 return WeakReference(flag_.get()); 64 return WeakReference(flag_);
62 } 65 }
63 66
64 void WeakReferenceOwner::Invalidate() { 67 void WeakReferenceOwner::Invalidate() {
65 if (flag_.get()) { 68 if (flag_) {
66 flag_->Invalidate(); 69 flag_->Invalidate();
67 flag_ = NULL; 70 flag_ = nullptr;
68 } 71 }
69 } 72 }
70 73
74 void WeakReferenceOwner::DetachFromSequence() {
75 flag_->DetachFromSequence();
76 }
77
71 WeakPtrBase::WeakPtrBase() { 78 WeakPtrBase::WeakPtrBase() {
72 } 79 }
73 80
74 WeakPtrBase::~WeakPtrBase() { 81 WeakPtrBase::~WeakPtrBase() {
75 } 82 }
76 83
77 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) { 84 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) {
78 } 85 }
79 86
80 } // namespace internal 87 } // namespace internal
81 } // namespace base 88 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/weak_ptr.h ('k') | base/memory/weak_ptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698