| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "content/browser/cancelable_request.h" | 5 #include "content/browser/cancelable_request.h" |
| 6 | 6 |
| 7 CancelableRequestProvider::CancelableRequestProvider() | 7 CancelableRequestProvider::CancelableRequestProvider() |
| 8 : next_handle_(1) { | 8 : next_handle_(1) { |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 100 } |
| 101 | 101 |
| 102 void CancelableRequestBase::Init(CancelableRequestProvider* provider, | 102 void CancelableRequestBase::Init(CancelableRequestProvider* provider, |
| 103 CancelableRequestProvider::Handle handle, | 103 CancelableRequestProvider::Handle handle, |
| 104 CancelableRequestConsumerBase* consumer) { | 104 CancelableRequestConsumerBase* consumer) { |
| 105 DCHECK(handle_ == 0 && provider_ == NULL && consumer_ == NULL); | 105 DCHECK(handle_ == 0 && provider_ == NULL && consumer_ == NULL); |
| 106 provider_ = provider; | 106 provider_ = provider; |
| 107 consumer_ = consumer; | 107 consumer_ = consumer; |
| 108 handle_ = handle; | 108 handle_ = handle; |
| 109 } | 109 } |
| 110 |
| 111 void CancelableRequestBase::DoForward(const base::Closure& forwarded_call, |
| 112 bool force_async) { |
| 113 if (force_async || callback_thread_ != MessageLoop::current()) { |
| 114 callback_thread_->PostTask( |
| 115 FROM_HERE, |
| 116 base::Bind(&CancelableRequestBase::ExecuteCallback, this, |
| 117 forwarded_call)); |
| 118 } else { |
| 119 // We can do synchronous callbacks when we're on the same thread. |
| 120 ExecuteCallback(forwarded_call); |
| 121 } |
| 122 } |
| 123 |
| 124 void CancelableRequestBase::ExecuteCallback( |
| 125 const base::Closure& forwarded_call) { |
| 126 DCHECK_EQ(callback_thread_, MessageLoop::current()); |
| 127 |
| 128 if (!canceled_.IsSet()) { |
| 129 WillExecute(); |
| 130 |
| 131 // Execute the callback. |
| 132 forwarded_call.Run(); |
| 133 } |
| 134 |
| 135 // Notify the provider that the request is complete. The provider will |
| 136 // notify the consumer for us. Note that it is possible for the callback to |
| 137 // cancel this request; we must check canceled again. |
| 138 if (!canceled_.IsSet()) |
| 139 NotifyCompleted(); |
| 140 } |
| OLD | NEW |