blob: f9bb233d2f7f5e6131339afe51b9dee116b9ee86 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_BASE_COMPLETION_CALLBACK_H__
#define NET_BASE_COMPLETION_CALLBACK_H__
#include "base/task.h"
#include "net/base/io_buffer.h"
namespace net {
// A callback specialization that takes a single int parameter. Usually this
// is used to report a byte count or network error code.
typedef Callback1<int>::Type CompletionCallback;
// Used to implement a CompletionCallback.
template <class T>
class CompletionCallbackImpl :
public CallbackImpl< T, void (T::*)(int), Tuple1<int> > {
public:
CompletionCallbackImpl(T* obj, void (T::* meth)(int))
: CallbackImpl< T, void (T::*)(int), Tuple1<int> >::CallbackImpl(obj, meth) {
}
};
// CancelableCompletionCallback is used for completion callbacks
// which may outlive the target for the method dispatch. In such a case, the
// provider of the callback calls Cancel() to mark the callback as
// "canceled". When the canceled callback is eventually run it does nothing
// other than to decrement the refcount to 0 and free the memory.
template <class T>
class CancelableCompletionCallback :
public CompletionCallbackImpl<T>,
public base::RefCounted<CancelableCompletionCallback<T> > {
public:
CancelableCompletionCallback(T* obj, void (T::* meth)(int))
: CompletionCallbackImpl<T>(obj, meth), is_canceled_(false) {
}
void Cancel() {
is_canceled_ = true;
}
// Attaches the given buffer to this callback so it is valid until the
// operation completes. TODO(rvargas): This is a temporal fix for bug 5325
// while I send IOBuffer to the lower layers of code.
void UseBuffer(net::IOBuffer* buffer) {
DCHECK(!buffer_.get());
buffer_ = buffer;
}
// The callback is not expected anymore so release the buffer.
void ReleaseBuffer() {
DCHECK(buffer_.get());
buffer_ = NULL;
}
virtual void RunWithParams(const Tuple1<int>& params) {
if (is_canceled_) {
CancelableCompletionCallback<T>::ReleaseBuffer();
base::RefCounted<CancelableCompletionCallback<T> >::Release();
} else {
CompletionCallbackImpl<T>::RunWithParams(params);
}
}
private:
scoped_refptr<net::IOBuffer> buffer_;
bool is_canceled_;
};
} // namespace net
#endif // NET_BASE_COMPLETION_CALLBACK_H__
|