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

Side by Side Diff: base/callback_unittest.cc

Issue 10388060: Make Callback.Reset safe against deleting itself. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review changes Created 8 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « base/callback_internal.cc ('k') | no next file » | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/bind.h" 5 #include "base/bind.h"
6 #include "base/callback.h" 6 #include "base/callback.h"
7 #include "base/callback_helpers.h" 7 #include "base/callback_helpers.h"
8 #include "base/callback_internal.h" 8 #include "base/callback_internal.h"
9 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 12
12 namespace base { 13 namespace base {
13 14
14 namespace { 15 namespace {
15 16
16 struct FakeInvoker { 17 struct FakeInvoker {
17 typedef void(RunType)(internal::BindStateBase*); 18 typedef void(RunType)(internal::BindStateBase*);
18 static void Run(internal::BindStateBase*) { 19 static void Run(internal::BindStateBase*) {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 139
139 TEST_F(CallbackTest, ResetAndReturn) { 140 TEST_F(CallbackTest, ResetAndReturn) {
140 TestForReentrancy tfr; 141 TestForReentrancy tfr;
141 ASSERT_FALSE(tfr.cb.is_null()); 142 ASSERT_FALSE(tfr.cb.is_null());
142 ASSERT_FALSE(tfr.cb_already_run); 143 ASSERT_FALSE(tfr.cb_already_run);
143 ResetAndReturn(&tfr.cb).Run(); 144 ResetAndReturn(&tfr.cb).Run();
144 ASSERT_TRUE(tfr.cb.is_null()); 145 ASSERT_TRUE(tfr.cb.is_null());
145 ASSERT_TRUE(tfr.cb_already_run); 146 ASSERT_TRUE(tfr.cb_already_run);
146 } 147 }
147 148
149 class CallbackOwner : public base::RefCounted<CallbackOwner> {
150 public:
151 CallbackOwner(bool* deleted) {
152 callback_ = Bind(&CallbackOwner::Unused, this);
153 deleted_ = deleted;
154 }
155 void Reset() {
156 callback_.Reset();
157 // We are deleted here if no-one else had a ref to us.
158 }
159
160 private:
161 friend class base::RefCounted<CallbackOwner>;
162 virtual ~CallbackOwner() {
163 *deleted_ = true;
164 }
165 void Unused() {
166 FAIL() << "Should never be called";
167 }
168
169 Closure callback_;
170 bool* deleted_;
171 };
172
173 TEST_F(CallbackTest, CallbackHasLastRefOnContainingObject) {
174 bool deleted = false;
175 CallbackOwner* owner = new CallbackOwner(&deleted);
176 owner->Reset();
177 ASSERT_TRUE(deleted);
178 }
179
148 } // namespace 180 } // namespace
149 } // namespace base 181 } // namespace base
OLDNEW
« no previous file with comments | « base/callback_internal.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698