| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/automation/url_request_automation_job.h" | 5 #include "chrome/browser/automation/url_request_automation_job.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 id_(0), | 101 id_(0), |
| 102 tab_(tab), | 102 tab_(tab), |
| 103 message_filter_(filter), | 103 message_filter_(filter), |
| 104 pending_buf_size_(0), | 104 pending_buf_size_(0), |
| 105 redirect_status_(0), | 105 redirect_status_(0), |
| 106 request_id_(request_id), | 106 request_id_(request_id), |
| 107 is_pending_(is_pending), | 107 is_pending_(is_pending), |
| 108 upload_size_(0), | 108 upload_size_(0), |
| 109 weak_factory_(this) { | 109 weak_factory_(this) { |
| 110 DVLOG(1) << "URLRequestAutomationJob create. Count: " << ++instance_count_; | 110 DVLOG(1) << "URLRequestAutomationJob create. Count: " << ++instance_count_; |
| 111 DCHECK(message_filter_ != NULL); | 111 DCHECK(message_filter_.get() != NULL); |
| 112 | 112 |
| 113 if (message_filter_) { | 113 if (message_filter_.get()) { |
| 114 id_ = message_filter_->NewAutomationRequestId(); | 114 id_ = message_filter_->NewAutomationRequestId(); |
| 115 DCHECK_NE(id_, 0); | 115 DCHECK_NE(id_, 0); |
| 116 } | 116 } |
| 117 } | 117 } |
| 118 | 118 |
| 119 URLRequestAutomationJob::~URLRequestAutomationJob() { | 119 URLRequestAutomationJob::~URLRequestAutomationJob() { |
| 120 DVLOG(1) << "URLRequestAutomationJob delete. Count: " << --instance_count_; | 120 DVLOG(1) << "URLRequestAutomationJob delete. Count: " << --instance_count_; |
| 121 Cleanup(); | 121 Cleanup(); |
| 122 } | 122 } |
| 123 | 123 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 145 // Returning null here just means that the built-in handler will be used. | 145 // Returning null here just means that the built-in handler will be used. |
| 146 if (scheme_is_http || scheme_is_https) { | 146 if (scheme_is_http || scheme_is_https) { |
| 147 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); | 147 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 148 if (info) { | 148 if (info) { |
| 149 int child_id = info->GetChildID(); | 149 int child_id = info->GetChildID(); |
| 150 int route_id = info->GetRouteID(); | 150 int route_id = info->GetRouteID(); |
| 151 AutomationResourceMessageFilter::AutomationDetails details; | 151 AutomationResourceMessageFilter::AutomationDetails details; |
| 152 if (AutomationResourceMessageFilter::LookupRegisteredRenderView( | 152 if (AutomationResourceMessageFilter::LookupRegisteredRenderView( |
| 153 child_id, route_id, &details)) { | 153 child_id, route_id, &details)) { |
| 154 URLRequestAutomationJob* job = new URLRequestAutomationJob( | 154 URLRequestAutomationJob* job = new URLRequestAutomationJob( |
| 155 request, network_delegate, | 155 request, |
| 156 network_delegate, |
| 156 request->context()->http_user_agent_settings(), | 157 request->context()->http_user_agent_settings(), |
| 157 details.tab_handle, info->GetRequestID(), details.filter, | 158 details.tab_handle, |
| 159 info->GetRequestID(), |
| 160 details.filter.get(), |
| 158 details.is_pending_render_view); | 161 details.is_pending_render_view); |
| 159 return job; | 162 return job; |
| 160 } | 163 } |
| 161 } | 164 } |
| 162 | 165 |
| 163 if (scheme_is_http && old_http_factory_) | 166 if (scheme_is_http && old_http_factory_) |
| 164 return old_http_factory_(request, network_delegate, scheme); | 167 return old_http_factory_(request, network_delegate, scheme); |
| 165 else if (scheme_is_https && old_https_factory_) | 168 else if (scheme_is_https && old_https_factory_) |
| 166 return old_https_factory_(request, network_delegate, scheme); | 169 return old_https_factory_(request, network_delegate, scheme); |
| 167 } | 170 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 net::IOBuffer* buf, int buf_size, int* bytes_read) { | 204 net::IOBuffer* buf, int buf_size, int* bytes_read) { |
| 202 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() | 205 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() |
| 203 << " - read pending: " << buf_size; | 206 << " - read pending: " << buf_size; |
| 204 | 207 |
| 205 // We should not receive a read request for a pending job. | 208 // We should not receive a read request for a pending job. |
| 206 DCHECK(!is_pending()); | 209 DCHECK(!is_pending()); |
| 207 | 210 |
| 208 pending_buf_ = buf; | 211 pending_buf_ = buf; |
| 209 pending_buf_size_ = buf_size; | 212 pending_buf_size_ = buf_size; |
| 210 | 213 |
| 211 if (message_filter_) { | 214 if (message_filter_.get()) { |
| 212 message_filter_->Send(new AutomationMsg_RequestRead(tab_, id_, buf_size)); | 215 message_filter_->Send(new AutomationMsg_RequestRead(tab_, id_, buf_size)); |
| 213 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | 216 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 214 } else { | 217 } else { |
| 215 base::MessageLoop::current()->PostTask( | 218 base::MessageLoop::current()->PostTask( |
| 216 FROM_HERE, | 219 FROM_HERE, |
| 217 base::Bind(&URLRequestAutomationJob::NotifyJobCompletionTask, | 220 base::Bind(&URLRequestAutomationJob::NotifyJobCompletionTask, |
| 218 weak_factory_.GetWeakPtr())); | 221 weak_factory_.GetWeakPtr())); |
| 219 } | 222 } |
| 220 return false; | 223 return false; |
| 221 } | 224 } |
| 222 | 225 |
| 223 bool URLRequestAutomationJob::GetMimeType(std::string* mime_type) const { | 226 bool URLRequestAutomationJob::GetMimeType(std::string* mime_type) const { |
| 224 if (!mime_type_.empty()) { | 227 if (!mime_type_.empty()) { |
| 225 *mime_type = mime_type_; | 228 *mime_type = mime_type_; |
| 226 } else if (headers_) { | 229 } else if (headers_.get()) { |
| 227 headers_->GetMimeType(mime_type); | 230 headers_->GetMimeType(mime_type); |
| 228 } | 231 } |
| 229 | 232 |
| 230 return (!mime_type->empty()); | 233 return (!mime_type->empty()); |
| 231 } | 234 } |
| 232 | 235 |
| 233 bool URLRequestAutomationJob::GetCharset(std::string* charset) { | 236 bool URLRequestAutomationJob::GetCharset(std::string* charset) { |
| 234 if (headers_) | 237 if (headers_.get()) |
| 235 return headers_->GetCharset(charset); | 238 return headers_->GetCharset(charset); |
| 236 return false; | 239 return false; |
| 237 } | 240 } |
| 238 | 241 |
| 239 void URLRequestAutomationJob::GetResponseInfo(net::HttpResponseInfo* info) { | 242 void URLRequestAutomationJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 240 if (headers_) | 243 if (headers_.get()) |
| 241 info->headers = headers_; | 244 info->headers = headers_; |
| 242 if (request_->url().SchemeIsSecure()) { | 245 if (request_->url().SchemeIsSecure()) { |
| 243 // Make up a fake certificate for this response since we don't have | 246 // Make up a fake certificate for this response since we don't have |
| 244 // access to the real SSL info. | 247 // access to the real SSL info. |
| 245 const char* kCertIssuer = "Chrome Internal"; | 248 const char* kCertIssuer = "Chrome Internal"; |
| 246 const int kLifetimeDays = 100; | 249 const int kLifetimeDays = 100; |
| 247 | 250 |
| 248 info->ssl_info.cert = | 251 info->ssl_info.cert = |
| 249 new net::X509Certificate(request_->url().GetWithEmptyPath().spec(), | 252 new net::X509Certificate(request_->url().GetWithEmptyPath().spec(), |
| 250 kCertIssuer, | 253 kCertIssuer, |
| 251 Time::Now(), | 254 Time::Now(), |
| 252 Time::Now() + | 255 Time::Now() + |
| 253 TimeDelta::FromDays(kLifetimeDays)); | 256 TimeDelta::FromDays(kLifetimeDays)); |
| 254 info->ssl_info.cert_status = 0; | 257 info->ssl_info.cert_status = 0; |
| 255 info->ssl_info.security_bits = -1; | 258 info->ssl_info.security_bits = -1; |
| 256 } | 259 } |
| 257 } | 260 } |
| 258 | 261 |
| 259 int URLRequestAutomationJob::GetResponseCode() const { | 262 int URLRequestAutomationJob::GetResponseCode() const { |
| 260 if (headers_) | 263 if (headers_.get()) |
| 261 return headers_->response_code(); | 264 return headers_->response_code(); |
| 262 | 265 |
| 263 static const int kDefaultResponseCode = 200; | 266 static const int kDefaultResponseCode = 200; |
| 264 return kDefaultResponseCode; | 267 return kDefaultResponseCode; |
| 265 } | 268 } |
| 266 | 269 |
| 267 bool URLRequestAutomationJob::IsRedirectResponse( | 270 bool URLRequestAutomationJob::IsRedirectResponse( |
| 268 GURL* location, int* http_status_code) { | 271 GURL* location, int* http_status_code) { |
| 269 if (!net::HttpResponseHeaders::IsRedirectResponseCode(redirect_status_)) | 272 if (!net::HttpResponseHeaders::IsRedirectResponseCode(redirect_status_)) |
| 270 return false; | 273 return false; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 void URLRequestAutomationJob::OnDataAvailable( | 355 void URLRequestAutomationJob::OnDataAvailable( |
| 353 int id, const std::string& bytes) { | 356 int id, const std::string& bytes) { |
| 354 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() | 357 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() |
| 355 << " - data available, Size: " << bytes.size(); | 358 << " - data available, Size: " << bytes.size(); |
| 356 DCHECK(!bytes.empty()); | 359 DCHECK(!bytes.empty()); |
| 357 | 360 |
| 358 // The request completed, and we have all the data. | 361 // The request completed, and we have all the data. |
| 359 // Clear any IO pending status. | 362 // Clear any IO pending status. |
| 360 SetStatus(net::URLRequestStatus()); | 363 SetStatus(net::URLRequestStatus()); |
| 361 | 364 |
| 362 if (pending_buf_ && pending_buf_->data()) { | 365 if (pending_buf_.get() && pending_buf_->data()) { |
| 363 DCHECK_GE(pending_buf_size_, bytes.size()); | 366 DCHECK_GE(pending_buf_size_, bytes.size()); |
| 364 const int bytes_to_copy = std::min(bytes.size(), pending_buf_size_); | 367 const int bytes_to_copy = std::min(bytes.size(), pending_buf_size_); |
| 365 memcpy(pending_buf_->data(), &bytes[0], bytes_to_copy); | 368 memcpy(pending_buf_->data(), &bytes[0], bytes_to_copy); |
| 366 | 369 |
| 367 pending_buf_ = NULL; | 370 pending_buf_ = NULL; |
| 368 pending_buf_size_ = 0; | 371 pending_buf_size_ = 0; |
| 369 | 372 |
| 370 NotifyReadComplete(bytes_to_copy); | 373 NotifyReadComplete(bytes_to_copy); |
| 371 } else { | 374 } else { |
| 372 NOTREACHED() << "Received unexpected data of length:" << bytes.size(); | 375 NOTREACHED() << "Received unexpected data of length:" << bytes.size(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 396 // NotifyDone may have been called on the job if the original request was | 399 // NotifyDone may have been called on the job if the original request was |
| 397 // redirected. | 400 // redirected. |
| 398 if (!is_done()) { | 401 if (!is_done()) { |
| 399 // We can complete the job if we have a valid response or a pending read. | 402 // We can complete the job if we have a valid response or a pending read. |
| 400 // An end request can be received in the following cases | 403 // An end request can be received in the following cases |
| 401 // 1. We failed to connect to the server, in which case we did not receive | 404 // 1. We failed to connect to the server, in which case we did not receive |
| 402 // a valid response. | 405 // a valid response. |
| 403 // 2. In response to a read request. | 406 // 2. In response to a read request. |
| 404 if (!has_response_started()) { | 407 if (!has_response_started()) { |
| 405 NotifyStartError(status); | 408 NotifyStartError(status); |
| 406 } else if (pending_buf_) { | 409 } else if (pending_buf_.get()) { |
| 407 pending_buf_ = NULL; | 410 pending_buf_ = NULL; |
| 408 pending_buf_size_ = 0; | 411 pending_buf_size_ = 0; |
| 409 NotifyDone(status); | 412 NotifyDone(status); |
| 410 NotifyReadComplete(0); | 413 NotifyReadComplete(0); |
| 411 } else { | 414 } else { |
| 412 // Wait for the http stack to issue a Read request where we will notify | 415 // Wait for the http stack to issue a Read request where we will notify |
| 413 // that the job has completed. | 416 // that the job has completed. |
| 414 request_status_ = status; | 417 request_status_ = status; |
| 415 } | 418 } |
| 416 } | 419 } |
| 417 // Note | 420 // Note |
| 418 // The job could have been destroyed above. Please don't attempt to access | 421 // The job could have been destroyed above. Please don't attempt to access |
| 419 // member variables here. | 422 // member variables here. |
| 420 } | 423 } |
| 421 | 424 |
| 422 void URLRequestAutomationJob::Cleanup() { | 425 void URLRequestAutomationJob::Cleanup() { |
| 423 headers_ = NULL; | 426 headers_ = NULL; |
| 424 mime_type_.erase(); | 427 mime_type_.erase(); |
| 425 | 428 |
| 426 id_ = 0; | 429 id_ = 0; |
| 427 tab_ = 0; | 430 tab_ = 0; |
| 428 | 431 |
| 429 DCHECK(!message_filter_); | 432 DCHECK(!message_filter_.get()); |
| 430 DisconnectFromMessageFilter(); | 433 DisconnectFromMessageFilter(); |
| 431 | 434 |
| 432 pending_buf_ = NULL; | 435 pending_buf_ = NULL; |
| 433 pending_buf_size_ = 0; | 436 pending_buf_size_ = 0; |
| 434 } | 437 } |
| 435 | 438 |
| 436 void URLRequestAutomationJob::StartAsync() { | 439 void URLRequestAutomationJob::StartAsync() { |
| 437 DVLOG(1) << "URLRequestAutomationJob: start request: " | 440 DVLOG(1) << "URLRequestAutomationJob: start request: " |
| 438 << (request_ ? request_->url().spec() : "NULL request"); | 441 << (request_ ? request_->url().spec() : "NULL request"); |
| 439 | 442 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 // Ask automation to start this request. | 502 // Ask automation to start this request. |
| 500 AutomationURLRequest automation_request; | 503 AutomationURLRequest automation_request; |
| 501 automation_request.url = request_->url().spec(); | 504 automation_request.url = request_->url().spec(); |
| 502 automation_request.method = request_->method(); | 505 automation_request.method = request_->method(); |
| 503 automation_request.referrer = referrer.spec(); | 506 automation_request.referrer = referrer.spec(); |
| 504 automation_request.extra_request_headers = new_request_headers.ToString(); | 507 automation_request.extra_request_headers = new_request_headers.ToString(); |
| 505 automation_request.upload_data = upload_data; | 508 automation_request.upload_data = upload_data; |
| 506 automation_request.resource_type = resource_type; | 509 automation_request.resource_type = resource_type; |
| 507 automation_request.load_flags = request_->load_flags(); | 510 automation_request.load_flags = request_->load_flags(); |
| 508 | 511 |
| 509 DCHECK(message_filter_); | 512 DCHECK(message_filter_.get()); |
| 510 message_filter_->Send(new AutomationMsg_RequestStart( | 513 message_filter_->Send( |
| 511 tab_, id_, automation_request)); | 514 new AutomationMsg_RequestStart(tab_, id_, automation_request)); |
| 512 } | 515 } |
| 513 | 516 |
| 514 void URLRequestAutomationJob::DisconnectFromMessageFilter() { | 517 void URLRequestAutomationJob::DisconnectFromMessageFilter() { |
| 515 if (message_filter_) { | 518 if (message_filter_.get()) { |
| 516 message_filter_->UnRegisterRequest(this); | 519 message_filter_->UnRegisterRequest(this); |
| 517 message_filter_ = NULL; | 520 message_filter_ = NULL; |
| 518 } | 521 } |
| 519 } | 522 } |
| 520 | 523 |
| 521 void URLRequestAutomationJob::StartPendingJob( | 524 void URLRequestAutomationJob::StartPendingJob( |
| 522 int new_tab_handle, | 525 int new_tab_handle, |
| 523 AutomationResourceMessageFilter* new_filter) { | 526 AutomationResourceMessageFilter* new_filter) { |
| 524 DCHECK(new_filter != NULL); | 527 DCHECK(new_filter != NULL); |
| 525 tab_ = new_tab_handle; | 528 tab_ = new_tab_handle; |
| 526 message_filter_ = new_filter; | 529 message_filter_ = new_filter; |
| 527 is_pending_ = false; | 530 is_pending_ = false; |
| 528 Start(); | 531 Start(); |
| 529 } | 532 } |
| 530 | 533 |
| 531 void URLRequestAutomationJob::NotifyJobCompletionTask() { | 534 void URLRequestAutomationJob::NotifyJobCompletionTask() { |
| 532 if (!is_done()) { | 535 if (!is_done()) { |
| 533 NotifyDone(request_status_); | 536 NotifyDone(request_status_); |
| 534 } | 537 } |
| 535 // Reset any pending reads. | 538 // Reset any pending reads. |
| 536 if (pending_buf_) { | 539 if (pending_buf_.get()) { |
| 537 pending_buf_ = NULL; | 540 pending_buf_ = NULL; |
| 538 pending_buf_size_ = 0; | 541 pending_buf_size_ = 0; |
| 539 NotifyReadComplete(0); | 542 NotifyReadComplete(0); |
| 540 } | 543 } |
| 541 } | 544 } |
| OLD | NEW |