| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef NET_BASE_COMPLETION_CALLBACK_H__ | 5 #ifndef NET_BASE_COMPLETION_CALLBACK_H__ |
| 6 #define NET_BASE_COMPLETION_CALLBACK_H__ | 6 #define NET_BASE_COMPLETION_CALLBACK_H__ |
| 7 | 7 |
| 8 #include "base/task.h" | 8 #include "base/task.h" |
| 9 #include "net/base/io_buffer.h" |
| 9 | 10 |
| 10 namespace net { | 11 namespace net { |
| 11 | 12 |
| 12 // A callback specialization that takes a single int parameter. Usually this | 13 // A callback specialization that takes a single int parameter. Usually this |
| 13 // is used to report a byte count or network error code. | 14 // is used to report a byte count or network error code. |
| 14 typedef Callback1<int>::Type CompletionCallback; | 15 typedef Callback1<int>::Type CompletionCallback; |
| 15 | 16 |
| 16 // Used to implement a CompletionCallback. | 17 // Used to implement a CompletionCallback. |
| 17 template <class T> | 18 template <class T> |
| 18 class CompletionCallbackImpl : | 19 class CompletionCallbackImpl : |
| (...skipping 15 matching lines...) Expand all Loading... |
| 34 public base::RefCounted<CancelableCompletionCallback<T> > { | 35 public base::RefCounted<CancelableCompletionCallback<T> > { |
| 35 public: | 36 public: |
| 36 CancelableCompletionCallback(T* obj, void (T::* meth)(int)) | 37 CancelableCompletionCallback(T* obj, void (T::* meth)(int)) |
| 37 : CompletionCallbackImpl<T>(obj, meth), is_canceled_(false) { | 38 : CompletionCallbackImpl<T>(obj, meth), is_canceled_(false) { |
| 38 } | 39 } |
| 39 | 40 |
| 40 void Cancel() { | 41 void Cancel() { |
| 41 is_canceled_ = true; | 42 is_canceled_ = true; |
| 42 } | 43 } |
| 43 | 44 |
| 45 // Attaches the given buffer to this callback so it is valid until the |
| 46 // operation completes. TODO(rvargas): This is a temporal fix for bug 5325 |
| 47 // while I send IOBuffer to the lower layers of code. |
| 48 void UseBuffer(net::IOBuffer* buffer) { |
| 49 DCHECK(!buffer_.get()); |
| 50 buffer_ = buffer; |
| 51 } |
| 52 |
| 53 // The callback is not expected anymore so release the buffer. |
| 54 void ReleaseBuffer() { |
| 55 DCHECK(buffer_.get()); |
| 56 buffer_ = NULL; |
| 57 } |
| 58 |
| 44 virtual void RunWithParams(const Tuple1<int>& params) { | 59 virtual void RunWithParams(const Tuple1<int>& params) { |
| 45 if (is_canceled_) { | 60 if (is_canceled_) { |
| 61 CancelableCompletionCallback<T>::ReleaseBuffer(); |
| 46 base::RefCounted<CancelableCompletionCallback<T> >::Release(); | 62 base::RefCounted<CancelableCompletionCallback<T> >::Release(); |
| 47 } else { | 63 } else { |
| 48 CompletionCallbackImpl<T>::RunWithParams(params); | 64 CompletionCallbackImpl<T>::RunWithParams(params); |
| 49 } | 65 } |
| 50 } | 66 } |
| 51 | 67 |
| 52 private: | 68 private: |
| 69 scoped_refptr<net::IOBuffer> buffer_; |
| 53 bool is_canceled_; | 70 bool is_canceled_; |
| 54 }; | 71 }; |
| 55 | 72 |
| 56 } // namespace net | 73 } // namespace net |
| 57 | 74 |
| 58 #endif // NET_BASE_COMPLETION_CALLBACK_H__ | 75 #endif // NET_BASE_COMPLETION_CALLBACK_H__ |
| 59 | 76 |
| OLD | NEW |