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

Side by Side Diff: base/cancelable_callback.cc

Issue 8673008: base::Bind: Implement CancelableCallback to replace CancelableTaske. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Test build fix. Created 9 years, 1 month 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/cancelable_callback.h"
csilv 2011/11/23 19:37:30 add includes for base/bind.h and base/compiler_spe
James Hawkins 2011/11/23 22:12:52 Done.
6
7 #include "base/logging.h"
8
9 namespace base {
10
11 CancelableCallback::CancelableCallback()
12 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
13 }
14
15 CancelableCallback::CancelableCallback(const base::Closure& callback)
16 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
17 callback_(callback) {
18 DCHECK_EQ(false, callback.is_null());
19 BindForwarder();
20 }
21
22 CancelableCallback::~CancelableCallback() {}
23
24 void CancelableCallback::Cancel() {
25 weak_factory_.InvalidateWeakPtrs();
26 callback_.Reset();
awong 2011/11/23 19:58:23 We should have a unittest assert that the wrapped
James Hawkins 2011/11/23 22:12:52 Done.
27 }
28
29 void CancelableCallback::Reset(const base::Closure& callback) {
30 DCHECK_EQ(false, callback.is_null());
31
32 // Outstanding tasks (e.g., posted to a message loop) must not be called.
33 Cancel();
34
35 // |forwarder_| is no longer valid after Cancel(), so re-bind.
36 BindForwarder();
37
38 callback_ = callback;
39 }
40
41 const base::Closure& CancelableCallback::callback() const {
42 return forwarder_;
43 }
44
45 bool CancelableCallback::is_null() const {
46 return callback_.is_null();
47 }
48
49 void CancelableCallback::RunCallback() {
50 callback_.Run();
51 }
52
53 void CancelableCallback::BindForwarder() {
54 forwarder_ = base::Bind(&CancelableCallback::RunCallback,
55 weak_factory_.GetWeakPtr());
56 }
57
58 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698