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

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: More reviewer fixes. 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"
6
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "base/logging.h"
10
11 namespace base {
12
13 CancelableCallback::CancelableCallback()
14 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
15 }
16
17 CancelableCallback::CancelableCallback(const base::Closure& callback)
18 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
19 callback_(callback) {
20 DCHECK_EQ(false, callback.is_null());
21 BindForwarder();
22 }
23
24 CancelableCallback::~CancelableCallback() {}
25
26 void CancelableCallback::Cancel() {
27 weak_factory_.InvalidateWeakPtrs();
28 callback_.Reset();
29 }
30
31 void CancelableCallback::Reset(const base::Closure& callback) {
32 DCHECK_EQ(false, callback.is_null());
33
34 // Outstanding tasks (e.g., posted to a message loop) must not be called.
35 Cancel();
36
37 // |forwarder_| is no longer valid after Cancel(), so re-bind.
38 BindForwarder();
39
40 callback_ = callback;
41 }
42
43 const base::Closure& CancelableCallback::callback() const {
44 return forwarder_;
45 }
46
47 bool CancelableCallback::is_null() const {
48 return callback_.is_null();
49 }
50
51 void CancelableCallback::RunCallback() {
52 callback_.Run();
53 }
54
55 void CancelableCallback::BindForwarder() {
56 forwarder_ = base::Bind(&CancelableCallback::RunCallback,
57 weak_factory_.GetWeakPtr());
58 }
59
60 }
awong 2011/11/23 23:41:07 // namespace bind
James Hawkins 2011/11/24 00:11:15 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698