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 "net/url_request/url_request_job.h" | 5 #include "net/url_request/url_request_job.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "net/base/auth.h" | 11 #include "net/base/auth.h" |
12 #include "net/base/host_port_pair.h" | 12 #include "net/base/host_port_pair.h" |
13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
14 #include "net/base/load_states.h" | 14 #include "net/base/load_states.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/base/network_delegate.h" | 16 #include "net/base/network_delegate.h" |
17 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
18 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
19 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
20 #include "net/url_request/url_request_job_tracker.h" | |
21 | 20 |
22 namespace net { | 21 namespace net { |
23 | 22 |
24 URLRequestJob::URLRequestJob(URLRequest* request) | 23 URLRequestJob::URLRequestJob(URLRequest* request) |
25 : request_(request), | 24 : request_(request), |
26 done_(false), | 25 done_(false), |
27 prefilter_bytes_read_(0), | 26 prefilter_bytes_read_(0), |
28 postfilter_bytes_read_(0), | 27 postfilter_bytes_read_(0), |
29 filter_input_byte_count_(0), | 28 filter_input_byte_count_(0), |
30 filter_needs_more_output_space_(false), | 29 filter_needs_more_output_space_(false), |
31 filtered_read_buffer_len_(0), | 30 filtered_read_buffer_len_(0), |
32 has_handled_response_(false), | 31 has_handled_response_(false), |
33 expected_content_size_(-1), | 32 expected_content_size_(-1), |
34 deferred_redirect_status_code_(-1), | 33 deferred_redirect_status_code_(-1), |
35 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 34 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
36 g_url_request_job_tracker.AddNewJob(this); | 35 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); |
| 36 if (system_monitor) |
| 37 base::SystemMonitor::Get()->AddObserver(this); |
37 } | 38 } |
38 | 39 |
39 void URLRequestJob::SetUpload(UploadData* upload) { | 40 void URLRequestJob::SetUpload(UploadData* upload) { |
40 } | 41 } |
41 | 42 |
42 void URLRequestJob::SetExtraRequestHeaders( | 43 void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders& headers) { |
43 const HttpRequestHeaders& headers) { | |
44 } | 44 } |
45 | 45 |
46 void URLRequestJob::Kill() { | 46 void URLRequestJob::Kill() { |
47 // Make sure the request is notified that we are done. We assume that the | 47 // Make sure the request is notified that we are done. We assume that the |
48 // request took care of setting its error status before calling Kill. | 48 // request took care of setting its error status before calling Kill. |
49 if (request_) | 49 if (request_) |
50 NotifyCanceled(); | 50 NotifyCanceled(); |
51 } | 51 } |
52 | 52 |
53 void URLRequestJob::DetachRequest() { | 53 void URLRequestJob::DetachRequest() { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 } | 196 } |
197 | 197 |
198 int URLRequestJob::GetResponseCode() const { | 198 int URLRequestJob::GetResponseCode() const { |
199 return -1; | 199 return -1; |
200 } | 200 } |
201 | 201 |
202 HostPortPair URLRequestJob::GetSocketAddress() const { | 202 HostPortPair URLRequestJob::GetSocketAddress() const { |
203 return HostPortPair(); | 203 return HostPortPair(); |
204 } | 204 } |
205 | 205 |
| 206 void URLRequestJob::OnSuspend() { |
| 207 Kill(); |
| 208 } |
| 209 |
206 URLRequestJob::~URLRequestJob() { | 210 URLRequestJob::~URLRequestJob() { |
207 g_url_request_job_tracker.RemoveJob(this); | 211 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); |
| 212 if (system_monitor) |
| 213 base::SystemMonitor::Get()->RemoveObserver(this); |
208 } | 214 } |
209 | 215 |
210 void URLRequestJob::NotifyHeadersComplete() { | 216 void URLRequestJob::NotifyHeadersComplete() { |
211 if (!request_ || !request_->delegate()) | 217 if (!request_ || !request_->delegate()) |
212 return; // The request was destroyed, so there is no more work to do. | 218 return; // The request was destroyed, so there is no more work to do. |
213 | 219 |
214 if (has_handled_response_) | 220 if (has_handled_response_) |
215 return; | 221 return; |
216 | 222 |
217 DCHECK(!request_->status().is_io_pending()); | 223 DCHECK(!request_->status().is_io_pending()); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 // With async IO, it's quite possible to have a few outstanding | 361 // With async IO, it's quite possible to have a few outstanding |
356 // requests. We could receive a request to Cancel, followed shortly | 362 // requests. We could receive a request to Cancel, followed shortly |
357 // by a successful IO. For tracking the status(), once there is | 363 // by a successful IO. For tracking the status(), once there is |
358 // an error, we do not change the status back to success. To | 364 // an error, we do not change the status back to success. To |
359 // enforce this, only set the status if the job is so far | 365 // enforce this, only set the status if the job is so far |
360 // successful. | 366 // successful. |
361 if (request_->status().is_success()) | 367 if (request_->status().is_success()) |
362 request_->set_status(status); | 368 request_->set_status(status); |
363 } | 369 } |
364 | 370 |
365 g_url_request_job_tracker.OnJobDone(this, status); | |
366 | |
367 if (request_ && request_->context() && | 371 if (request_ && request_->context() && |
368 request_->context()->network_delegate()) { | 372 request_->context()->network_delegate()) { |
369 request_->context()->network_delegate()->NotifyCompleted(request_); | 373 request_->context()->network_delegate()->NotifyCompleted(request_); |
370 } | 374 } |
371 | 375 |
372 // Complete this notification later. This prevents us from re-entering the | 376 // Complete this notification later. This prevents us from re-entering the |
373 // delegate if we're done because of a synchronous call. | 377 // delegate if we're done because of a synchronous call. |
374 MessageLoop::current()->PostTask( | 378 MessageLoop::current()->PostTask( |
375 FROM_HERE, | 379 FROM_HERE, |
376 method_factory_.NewRunnableMethod(&URLRequestJob::CompleteNotifyDone)); | 380 method_factory_.NewRunnableMethod(&URLRequestJob::CompleteNotifyDone)); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 if (!request_->status().is_io_pending()) { | 575 if (!request_->status().is_io_pending()) { |
572 // If the read completes synchronously, either success or failure, | 576 // If the read completes synchronously, either success or failure, |
573 // invoke the OnRawReadComplete callback so we can account for the | 577 // invoke the OnRawReadComplete callback so we can account for the |
574 // completed read. | 578 // completed read. |
575 OnRawReadComplete(*bytes_read); | 579 OnRawReadComplete(*bytes_read); |
576 } | 580 } |
577 return rv; | 581 return rv; |
578 } | 582 } |
579 | 583 |
580 void URLRequestJob::FollowRedirect(const GURL& location, int http_status_code) { | 584 void URLRequestJob::FollowRedirect(const GURL& location, int http_status_code) { |
581 g_url_request_job_tracker.OnJobRedirect(this, location, http_status_code); | |
582 | |
583 int rv = request_->Redirect(location, http_status_code); | 585 int rv = request_->Redirect(location, http_status_code); |
584 if (rv != OK) | 586 if (rv != OK) |
585 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | 587 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
586 } | 588 } |
587 | 589 |
588 void URLRequestJob::OnRawReadComplete(int bytes_read) { | 590 void URLRequestJob::OnRawReadComplete(int bytes_read) { |
589 DCHECK(raw_read_buffer_); | 591 DCHECK(raw_read_buffer_); |
590 if (bytes_read > 0) { | 592 if (bytes_read > 0) { |
591 RecordBytesRead(bytes_read); | 593 RecordBytesRead(bytes_read); |
592 } | 594 } |
593 raw_read_buffer_ = NULL; | 595 raw_read_buffer_ = NULL; |
594 } | 596 } |
595 | 597 |
596 void URLRequestJob::RecordBytesRead(int bytes_read) { | 598 void URLRequestJob::RecordBytesRead(int bytes_read) { |
597 filter_input_byte_count_ += bytes_read; | 599 filter_input_byte_count_ += bytes_read; |
598 UpdatePacketReadTimes(); // Facilitate stats recording if it is active. | 600 UpdatePacketReadTimes(); // Facilitate stats recording if it is active. |
599 g_url_request_job_tracker.OnBytesRead(this, raw_read_buffer_->data(), | 601 const URLRequestContext* context = request_->context(); |
600 bytes_read); | 602 if (context && context->network_delegate()) |
| 603 context->network_delegate()->NotifyRawBytesRead(*request_, bytes_read); |
601 } | 604 } |
602 | 605 |
603 bool URLRequestJob::FilterHasData() { | 606 bool URLRequestJob::FilterHasData() { |
604 return filter_.get() && filter_->stream_data_len(); | 607 return filter_.get() && filter_->stream_data_len(); |
605 } | 608 } |
606 | 609 |
607 void URLRequestJob::UpdatePacketReadTimes() { | 610 void URLRequestJob::UpdatePacketReadTimes() { |
608 } | 611 } |
609 | 612 |
610 } // namespace net | 613 } // namespace net |
OLD | NEW |