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

Side by Side Diff: base/cancelable_callback.cc

Issue 8662047: base::Bind: Implement a 1-arity CancelableCallback and use this to implement (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment fixes. Created 9 years 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/cancelable_callback.h ('k') | base/cancelable_callback_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
(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(!callback.is_null());
21 InitializeForwarder();
22 }
23
24 CancelableCallback::~CancelableCallback() {}
25
26 void CancelableCallback::Cancel() {
27 weak_factory_.InvalidateWeakPtrs();
28 callback_.Reset();
29 }
30
31 bool CancelableCallback::IsCancelled() const {
32 return callback_.is_null();
33 }
34
35 void CancelableCallback::Reset(const base::Closure& callback) {
36 DCHECK(!callback.is_null());
37
38 // Outstanding tasks (e.g., posted to a message loop) must not be called.
39 Cancel();
40
41 // |forwarder_| is no longer valid after Cancel(), so re-bind.
42 InitializeForwarder();
43
44 callback_ = callback;
45 }
46
47 const base::Closure& CancelableCallback::callback() const {
48 return forwarder_;
49 }
50
51 void CancelableCallback::RunCallback() {
52 callback_.Run();
53 }
54
55 void CancelableCallback::InitializeForwarder() {
56 forwarder_ = base::Bind(&CancelableCallback::RunCallback,
57 weak_factory_.GetWeakPtr());
58 }
59
60 } // namespace bind
OLDNEW
« no previous file with comments | « base/cancelable_callback.h ('k') | base/cancelable_callback_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698