[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/automation/url_request_automation_job.h" |
| 6 | |
[email protected] | 8c6517e5 | 2011-10-17 01:20:36 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 4630e5a | 2010-12-03 06:16:55 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 9 | #include "base/message_loop.h" |
[email protected] | 7c46ef7 | 2009-08-07 20:52:58 | [diff] [blame] | 10 | #include "base/time.h" |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 11 | #include "chrome/browser/automation/automation_resource_message_filter.h" |
[email protected] | a8ba636 | 2010-11-10 20:02:25 | [diff] [blame] | 12 | #include "chrome/common/automation_messages.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 13 | #include "content/public/browser/browser_thread.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 14 | #include "content/public/browser/render_view_host.h" |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 15 | #include "content/public/browser/resource_request_info.h" |
[email protected] | 0d282c2e | 2011-02-24 20:42:51 | [diff] [blame] | 16 | #include "net/base/host_port_pair.h" |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 17 | #include "net/base/io_buffer.h" |
| 18 | #include "net/base/net_errors.h" |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 19 | #include "net/base/upload_bytes_element_reader.h" |
| 20 | #include "net/base/upload_data_stream.h" |
| 21 | #include "net/base/upload_file_element_reader.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 22 | #include "net/cookies/cookie_monster.h" |
[email protected] | ee1a29b0 | 2010-05-06 20:42:12 | [diff] [blame] | 23 | #include "net/http/http_request_headers.h" |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 24 | #include "net/http/http_response_headers.h" |
[email protected] | fa0cf1a | 2009-07-31 00:20:08 | [diff] [blame] | 25 | #include "net/http/http_util.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 26 | #include "net/url_request/http_user_agent_settings.h" |
[email protected] | 180aad7 | 2012-06-29 19:07:33 | [diff] [blame] | 27 | #include "net/url_request/url_request.h" |
[email protected] | 5f450e5 | 2009-07-28 13:28:11 | [diff] [blame] | 28 | #include "net/url_request/url_request_context.h" |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 29 | |
[email protected] | 7c46ef7 | 2009-08-07 20:52:58 | [diff] [blame] | 30 | using base::Time; |
| 31 | using base::TimeDelta; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 32 | using content::BrowserThread; |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 33 | using content::ResourceRequestInfo; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 34 | |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 35 | namespace { |
| 36 | |
[email protected] | fa0cf1a | 2009-07-31 00:20:08 | [diff] [blame] | 37 | // The list of filtered headers that are removed from requests sent via |
| 38 | // StartAsync(). These must be lower case. |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 39 | const char* const kFilteredHeaderStrings[] = { |
[email protected] | fa0cf1a | 2009-07-31 00:20:08 | [diff] [blame] | 40 | "connection", |
| 41 | "cookie", |
| 42 | "expect", |
[email protected] | fa0cf1a | 2009-07-31 00:20:08 | [diff] [blame] | 43 | "max-forwards", |
| 44 | "proxy-authorization", |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 45 | "referer", |
[email protected] | fa0cf1a | 2009-07-31 00:20:08 | [diff] [blame] | 46 | "te", |
| 47 | "upgrade", |
| 48 | "via" |
| 49 | }; |
| 50 | |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 51 | // Creates UploadData from UploadDataStream. |
| 52 | net::UploadData* CreateUploadData( |
| 53 | const net::UploadDataStream* upload_data_stream) { |
| 54 | net::UploadData* upload_data = new net::UploadData(); |
| 55 | const ScopedVector<net::UploadElementReader>& element_readers = |
| 56 | upload_data_stream->element_readers(); |
| 57 | for (size_t i = 0; i < element_readers.size(); ++i) { |
| 58 | const net::UploadElementReader* reader = element_readers[i]; |
| 59 | if (reader->AsBytesReader()) { |
| 60 | const net::UploadBytesElementReader* bytes_reader = |
| 61 | reader->AsBytesReader(); |
| 62 | upload_data->AppendBytes(bytes_reader->bytes(), bytes_reader->length()); |
| 63 | } else if (reader->AsFileReader()) { |
| 64 | const net::UploadFileElementReader* file_reader = |
| 65 | reader->AsFileReader(); |
| 66 | upload_data->AppendFileRange(file_reader->path(), |
| 67 | file_reader->range_offset(), |
| 68 | file_reader->range_length(), |
| 69 | file_reader->expected_modification_time()); |
| 70 | } else { |
| 71 | NOTIMPLEMENTED(); |
| 72 | } |
| 73 | } |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 74 | upload_data->set_identifier(upload_data_stream->identifier()); |
| 75 | upload_data->set_is_chunked(upload_data_stream->is_chunked()); |
| 76 | upload_data->set_last_chunk_appended( |
| 77 | upload_data_stream->last_chunk_appended()); |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 78 | return upload_data; |
| 79 | } |
| 80 | |
| 81 | } // namespace |
| 82 | |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 83 | int URLRequestAutomationJob::instance_count_ = 0; |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 84 | bool URLRequestAutomationJob::is_protocol_factory_registered_ = false; |
| 85 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 86 | net::URLRequest::ProtocolFactory* URLRequestAutomationJob::old_http_factory_ |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 87 | = NULL; |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 88 | net::URLRequest::ProtocolFactory* URLRequestAutomationJob::old_https_factory_ |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 89 | = NULL; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 90 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 91 | URLRequestAutomationJob::URLRequestAutomationJob( |
| 92 | net::URLRequest* request, |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 93 | net::NetworkDelegate* network_delegate, |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 94 | const net::HttpUserAgentSettings* http_user_agent_settings, |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 95 | int tab, |
| 96 | int request_id, |
| 97 | AutomationResourceMessageFilter* filter, |
| 98 | bool is_pending) |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 99 | : net::URLRequestJob(request, network_delegate), |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 100 | http_user_agent_settings_(http_user_agent_settings), |
[email protected] | c33ea14 | 2010-07-24 13:13:04 | [diff] [blame] | 101 | id_(0), |
[email protected] | fc6fb7fb | 2009-11-07 02:35:04 | [diff] [blame] | 102 | tab_(tab), |
| 103 | message_filter_(filter), |
| 104 | pending_buf_size_(0), |
[email protected] | dda7d9c | 2009-11-11 23:01:47 | [diff] [blame] | 105 | redirect_status_(0), |
[email protected] | 18290eb | 2010-01-30 00:47:06 | [diff] [blame] | 106 | request_id_(request_id), |
[email protected] | 4630e5a | 2010-12-03 06:16:55 | [diff] [blame] | 107 | is_pending_(is_pending), |
[email protected] | 7335ab0 | 2012-08-30 22:30:42 | [diff] [blame] | 108 | upload_size_(0), |
[email protected] | 9c00909 | 2013-05-01 03:14:09 | [diff] [blame] | 109 | weak_factory_(this) { |
[email protected] | 5fb40dc | 2010-10-22 20:35:27 | [diff] [blame] | 110 | DVLOG(1) << "URLRequestAutomationJob create. Count: " << ++instance_count_; |
[email protected] | dda7d9c | 2009-11-11 23:01:47 | [diff] [blame] | 111 | DCHECK(message_filter_ != NULL); |
| 112 | |
| 113 | if (message_filter_) { |
| 114 | id_ = message_filter_->NewAutomationRequestId(); |
| 115 | DCHECK_NE(id_, 0); |
| 116 | } |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | URLRequestAutomationJob::~URLRequestAutomationJob() { |
[email protected] | 5fb40dc | 2010-10-22 20:35:27 | [diff] [blame] | 120 | DVLOG(1) << "URLRequestAutomationJob delete. Count: " << --instance_count_; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 121 | Cleanup(); |
| 122 | } |
| 123 | |
[email protected] | 8c6517e5 | 2011-10-17 01:20:36 | [diff] [blame] | 124 | void URLRequestAutomationJob::EnsureProtocolFactoryRegistered() { |
[email protected] | e0d586c | 2010-10-06 03:57:24 | [diff] [blame] | 125 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 126 | |
| 127 | if (!is_protocol_factory_registered_) { |
| 128 | old_http_factory_ = |
[email protected] | e353940 | 2011-07-19 09:31:08 | [diff] [blame] | 129 | net::URLRequest::Deprecated::RegisterProtocolFactory( |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 130 | "http", &URLRequestAutomationJob::Factory); |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 131 | old_https_factory_ = |
[email protected] | e353940 | 2011-07-19 09:31:08 | [diff] [blame] | 132 | net::URLRequest::Deprecated::RegisterProtocolFactory( |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 133 | "https", &URLRequestAutomationJob::Factory); |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 134 | is_protocol_factory_registered_ = true; |
| 135 | } |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 9acd869e | 2010-12-11 10:18:59 | [diff] [blame] | 138 | net::URLRequestJob* URLRequestAutomationJob::Factory( |
| 139 | net::URLRequest* request, |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 140 | net::NetworkDelegate* network_delegate, |
[email protected] | 9acd869e | 2010-12-11 10:18:59 | [diff] [blame] | 141 | const std::string& scheme) { |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 142 | bool scheme_is_http = request->url().SchemeIs("http"); |
| 143 | bool scheme_is_https = request->url().SchemeIs("https"); |
| 144 | |
| 145 | // Returning null here just means that the built-in handler will be used. |
| 146 | if (scheme_is_http || scheme_is_https) { |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 147 | const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request); |
| 148 | if (info) { |
| 149 | int child_id = info->GetChildID(); |
| 150 | int route_id = info->GetRouteID(); |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 151 | AutomationResourceMessageFilter::AutomationDetails details; |
| 152 | if (AutomationResourceMessageFilter::LookupRegisteredRenderView( |
[email protected] | 2799c02a | 2009-11-07 15:39:55 | [diff] [blame] | 153 | child_id, route_id, &details)) { |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 154 | URLRequestAutomationJob* job = new URLRequestAutomationJob( |
| 155 | request, network_delegate, |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 156 | request->context()->http_user_agent_settings(), |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 157 | details.tab_handle, info->GetRequestID(), details.filter, |
[email protected] | 18290eb | 2010-01-30 00:47:06 | [diff] [blame] | 158 | details.is_pending_render_view); |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 159 | return job; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (scheme_is_http && old_http_factory_) |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 164 | return old_http_factory_(request, network_delegate, scheme); |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 165 | else if (scheme_is_https && old_https_factory_) |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 166 | return old_https_factory_(request, network_delegate, scheme); |
[email protected] | b377fe85 | 2009-11-06 05:57:17 | [diff] [blame] | 167 | } |
| 168 | return NULL; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | 9acd869e | 2010-12-11 10:18:59 | [diff] [blame] | 171 | // net::URLRequestJob Implementation. |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 172 | void URLRequestAutomationJob::Start() { |
[email protected] | 18290eb | 2010-01-30 00:47:06 | [diff] [blame] | 173 | if (!is_pending()) { |
| 174 | // Start reading asynchronously so that all error reporting and data |
| 175 | // callbacks happen as they would for network requests. |
[email protected] | 4630e5a | 2010-12-03 06:16:55 | [diff] [blame] | 176 | MessageLoop::current()->PostTask( |
| 177 | FROM_HERE, |
[email protected] | 8c6517e5 | 2011-10-17 01:20:36 | [diff] [blame] | 178 | base::Bind(&URLRequestAutomationJob::StartAsync, |
| 179 | weak_factory_.GetWeakPtr())); |
[email protected] | 18290eb | 2010-01-30 00:47:06 | [diff] [blame] | 180 | } else { |
| 181 | // If this is a pending job, then register it immediately with the message |
| 182 | // filter so it can be serviced later when we receive a request from the |
| 183 | // external host to connect to the corresponding external tab. |
| 184 | message_filter_->RegisterRequest(this); |
| 185 | } |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void URLRequestAutomationJob::Kill() { |
[email protected] | 8fdf9393 | 2010-02-15 00:47:25 | [diff] [blame] | 189 | if (message_filter_.get()) { |
| 190 | if (!is_pending()) { |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 191 | message_filter_->Send(new AutomationMsg_RequestEnd(tab_, id_, |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 192 | net::URLRequestStatus(net::URLRequestStatus::CANCELED, |
| 193 | net::ERR_ABORTED))); |
[email protected] | 18290eb | 2010-01-30 00:47:06 | [diff] [blame] | 194 | } |
[email protected] | b0a7fc6 | 2009-07-28 22:04:22 | [diff] [blame] | 195 | } |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 196 | DisconnectFromMessageFilter(); |
[email protected] | 9acd869e | 2010-12-11 10:18:59 | [diff] [blame] | 197 | net::URLRequestJob::Kill(); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | bool URLRequestAutomationJob::ReadRawData( |
| 201 | net::IOBuffer* buf, int buf_size, int* bytes_read) { |
[email protected] | 5fb40dc | 2010-10-22 20:35:27 | [diff] [blame] | 202 | DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() |
| 203 | << " - read pending: " << buf_size; |
[email protected] | 18290eb | 2010-01-30 00:47:06 | [diff] [blame] | 204 | |
| 205 | // We should not receive a read request for a pending job. |
| 206 | DCHECK(!is_pending()); |
| 207 | |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 208 | pending_buf_ = buf; |
| 209 | pending_buf_size_ = buf_size; |
| 210 | |
[email protected] | 3927f81 | 2010-02-10 06:41:02 | [diff] [blame] | 211 | if (message_filter_) { |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 212 | message_filter_->Send(new AutomationMsg_RequestRead(tab_, id_, buf_size)); |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 213 | SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
[email protected] | 3927f81 | 2010-02-10 06:41:02 | [diff] [blame] | 214 | } else { |
[email protected] | 4630e5a | 2010-12-03 06:16:55 | [diff] [blame] | 215 | MessageLoop::current()->PostTask( |
| 216 | FROM_HERE, |
[email protected] | 8c6517e5 | 2011-10-17 01:20:36 | [diff] [blame] | 217 | base::Bind(&URLRequestAutomationJob::NotifyJobCompletionTask, |
| 218 | weak_factory_.GetWeakPtr())); |
[email protected] | 3927f81 | 2010-02-10 06:41:02 | [diff] [blame] | 219 | } |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | |
| 223 | bool URLRequestAutomationJob::GetMimeType(std::string* mime_type) const { |
| 224 | if (!mime_type_.empty()) { |
| 225 | *mime_type = mime_type_; |
| 226 | } else if (headers_) { |
| 227 | headers_->GetMimeType(mime_type); |
| 228 | } |
| 229 | |
| 230 | return (!mime_type->empty()); |
| 231 | } |
| 232 | |
| 233 | bool URLRequestAutomationJob::GetCharset(std::string* charset) { |
| 234 | if (headers_) |
| 235 | return headers_->GetCharset(charset); |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | void URLRequestAutomationJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 240 | if (headers_) |
| 241 | info->headers = headers_; |
| 242 | if (request_->url().SchemeIsSecure()) { |
[email protected] | 7c46ef7 | 2009-08-07 20:52:58 | [diff] [blame] | 243 | // Make up a fake certificate for this response since we don't have |
| 244 | // access to the real SSL info. |
| 245 | const char* kCertIssuer = "Chrome Internal"; |
| 246 | const int kLifetimeDays = 100; |
| 247 | |
| 248 | info->ssl_info.cert = |
| 249 | new net::X509Certificate(request_->url().GetWithEmptyPath().spec(), |
| 250 | kCertIssuer, |
| 251 | Time::Now(), |
| 252 | Time::Now() + |
| 253 | TimeDelta::FromDays(kLifetimeDays)); |
| 254 | info->ssl_info.cert_status = 0; |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 255 | info->ssl_info.security_bits = -1; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | int URLRequestAutomationJob::GetResponseCode() const { |
| 260 | if (headers_) |
| 261 | return headers_->response_code(); |
| 262 | |
| 263 | static const int kDefaultResponseCode = 200; |
| 264 | return kDefaultResponseCode; |
| 265 | } |
| 266 | |
| 267 | bool URLRequestAutomationJob::IsRedirectResponse( |
| 268 | GURL* location, int* http_status_code) { |
[email protected] | 86de13d | 2009-11-24 01:21:35 | [diff] [blame] | 269 | if (!net::HttpResponseHeaders::IsRedirectResponseCode(redirect_status_)) |
| 270 | return false; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 271 | |
[email protected] | 86de13d | 2009-11-24 01:21:35 | [diff] [blame] | 272 | *http_status_code = redirect_status_; |
| 273 | *location = GURL(redirect_url_); |
| 274 | return true; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 275 | } |
| 276 | |
[email protected] | 7335ab0 | 2012-08-30 22:30:42 | [diff] [blame] | 277 | net::UploadProgress URLRequestAutomationJob::GetUploadProgress() const { |
| 278 | uint64 progress = 0; |
[email protected] | 02922607 | 2011-02-08 00:00:16 | [diff] [blame] | 279 | if (request_ && request_->status().is_success()) { |
| 280 | // We don't support incremental progress notifications in ChromeFrame. When |
| 281 | // we receive a response for the POST request from Chromeframe, it means |
| 282 | // that the upload is fully complete. |
[email protected] | 7335ab0 | 2012-08-30 22:30:42 | [diff] [blame] | 283 | progress = upload_size_; |
[email protected] | 02922607 | 2011-02-08 00:00:16 | [diff] [blame] | 284 | } |
[email protected] | 7335ab0 | 2012-08-30 22:30:42 | [diff] [blame] | 285 | return net::UploadProgress(progress, upload_size_); |
[email protected] | 02922607 | 2011-02-08 00:00:16 | [diff] [blame] | 286 | } |
| 287 | |
[email protected] | 0d282c2e | 2011-02-24 20:42:51 | [diff] [blame] | 288 | net::HostPortPair URLRequestAutomationJob::GetSocketAddress() const { |
| 289 | return socket_address_; |
| 290 | } |
| 291 | |
[email protected] | fc6fb7fb | 2009-11-07 02:35:04 | [diff] [blame] | 292 | bool URLRequestAutomationJob::MayFilterMessage(const IPC::Message& message, |
| 293 | int* request_id) { |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 294 | switch (message.type()) { |
| 295 | case AutomationMsg_RequestStarted::ID: |
| 296 | case AutomationMsg_RequestData::ID: |
| 297 | case AutomationMsg_RequestEnd::ID: { |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 298 | PickleIterator iter(message); |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 299 | if (message.ReadInt(&iter, request_id)) |
[email protected] | fc6fb7fb | 2009-11-07 02:35:04 | [diff] [blame] | 300 | return true; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
[email protected] | fc6fb7fb | 2009-11-07 02:35:04 | [diff] [blame] | 305 | return false; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void URLRequestAutomationJob::OnMessage(const IPC::Message& message) { |
[email protected] | 2cf5fd6f | 2009-12-11 19:21:48 | [diff] [blame] | 309 | if (!request_) { |
| 310 | NOTREACHED() << __FUNCTION__ |
| 311 | << ": Unexpected request received for job:" |
| 312 | << id(); |
| 313 | return; |
| 314 | } |
| 315 | |
[email protected] | c7fd7329 | 2011-07-29 16:45:43 | [diff] [blame] | 316 | bool deserialize_success = false; |
| 317 | IPC_BEGIN_MESSAGE_MAP_EX(URLRequestAutomationJob, |
| 318 | message, |
| 319 | deserialize_success) |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 320 | IPC_MESSAGE_HANDLER(AutomationMsg_RequestStarted, OnRequestStarted) |
| 321 | IPC_MESSAGE_HANDLER(AutomationMsg_RequestData, OnDataAvailable) |
| 322 | IPC_MESSAGE_HANDLER(AutomationMsg_RequestEnd, OnRequestEnd) |
[email protected] | c7fd7329 | 2011-07-29 16:45:43 | [diff] [blame] | 323 | IPC_END_MESSAGE_MAP_EX() |
| 324 | |
| 325 | if (!deserialize_success) { |
| 326 | LOG(ERROR) << "Failed to deserialize IPC message."; |
| 327 | } |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 330 | void URLRequestAutomationJob::OnRequestStarted( |
| 331 | int id, const AutomationURLResponse& response) { |
[email protected] | 5fb40dc | 2010-10-22 20:35:27 | [diff] [blame] | 332 | DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() |
| 333 | << " - response started."; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 334 | set_expected_content_size(response.content_length); |
| 335 | mime_type_ = response.mime_type; |
| 336 | |
[email protected] | a765931 | 2009-08-12 22:21:10 | [diff] [blame] | 337 | redirect_url_ = response.redirect_url; |
[email protected] | b4cdb69c | 2009-08-31 23:29:13 | [diff] [blame] | 338 | redirect_status_ = response.redirect_status; |
| 339 | DCHECK(redirect_status_ == 0 || redirect_status_ == 200 || |
| 340 | (redirect_status_ >= 300 && redirect_status_ < 400)); |
[email protected] | a765931 | 2009-08-12 22:21:10 | [diff] [blame] | 341 | |
[email protected] | 5f450e5 | 2009-07-28 13:28:11 | [diff] [blame] | 342 | if (!response.headers.empty()) { |
[email protected] | 382d276d | 2009-08-20 23:23:03 | [diff] [blame] | 343 | headers_ = new net::HttpResponseHeaders( |
| 344 | net::HttpUtil::AssembleRawHeaders(response.headers.data(), |
| 345 | response.headers.size())); |
[email protected] | 5f450e5 | 2009-07-28 13:28:11 | [diff] [blame] | 346 | } |
[email protected] | 0d282c2e | 2011-02-24 20:42:51 | [diff] [blame] | 347 | socket_address_ = response.socket_address; |
[email protected] | 7335ab0 | 2012-08-30 22:30:42 | [diff] [blame] | 348 | upload_size_ = response.upload_size; |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 349 | NotifyHeadersComplete(); |
| 350 | } |
| 351 | |
| 352 | void URLRequestAutomationJob::OnDataAvailable( |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 353 | int id, const std::string& bytes) { |
[email protected] | 5fb40dc | 2010-10-22 20:35:27 | [diff] [blame] | 354 | DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() |
| 355 | << " - data available, Size: " << bytes.size(); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 356 | DCHECK(!bytes.empty()); |
| 357 | |
| 358 | // The request completed, and we have all the data. |
| 359 | // Clear any IO pending status. |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 360 | SetStatus(net::URLRequestStatus()); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 361 | |
| 362 | if (pending_buf_ && pending_buf_->data()) { |
| 363 | DCHECK_GE(pending_buf_size_, bytes.size()); |
| 364 | const int bytes_to_copy = std::min(bytes.size(), pending_buf_size_); |
| 365 | memcpy(pending_buf_->data(), &bytes[0], bytes_to_copy); |
| 366 | |
| 367 | pending_buf_ = NULL; |
| 368 | pending_buf_size_ = 0; |
| 369 | |
| 370 | NotifyReadComplete(bytes_to_copy); |
[email protected] | 3927f81 | 2010-02-10 06:41:02 | [diff] [blame] | 371 | } else { |
| 372 | NOTREACHED() << "Received unexpected data of length:" << bytes.size(); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | void URLRequestAutomationJob::OnRequestEnd( |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 377 | int id, const net::URLRequestStatus& status) { |
[email protected] | 0c81476 | 2009-09-17 22:56:00 | [diff] [blame] | 378 | #ifndef NDEBUG |
| 379 | std::string url; |
| 380 | if (request_) |
| 381 | url = request_->url().spec(); |
[email protected] | 5fb40dc | 2010-10-22 20:35:27 | [diff] [blame] | 382 | DVLOG(1) << "URLRequestAutomationJob: " << url << " - request end. Status: " |
| 383 | << status.status(); |
[email protected] | 0c81476 | 2009-09-17 22:56:00 | [diff] [blame] | 384 | #endif |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 385 | |
[email protected] | fab40420 | 2009-09-10 17:19:06 | [diff] [blame] | 386 | // TODO(tommi): When we hit certificate errors, notify the delegate via |
| 387 | // OnSSLCertificateError(). Right now we don't have the certificate |
| 388 | // so we don't. We could possibly call OnSSLCertificateError with a NULL |
| 389 | // certificate, but I'm not sure if all implementations expect it. |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 390 | // if (status.status() == net::URLRequestStatus::FAILED && |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 391 | // net::IsCertificateError(status.error()) && request_->delegate()) { |
| 392 | // request_->delegate()->OnSSLCertificateError(request_, status.error()); |
[email protected] | fab40420 | 2009-09-10 17:19:06 | [diff] [blame] | 393 | // } |
| 394 | |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 395 | DisconnectFromMessageFilter(); |
[email protected] | eb834fb | 2009-10-23 16:09:41 | [diff] [blame] | 396 | // NotifyDone may have been called on the job if the original request was |
| 397 | // redirected. |
[email protected] | 3927f81 | 2010-02-10 06:41:02 | [diff] [blame] | 398 | if (!is_done()) { |
| 399 | // 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 |
| 401 | // 1. We failed to connect to the server, in which case we did not receive |
| 402 | // a valid response. |
| 403 | // 2. In response to a read request. |
[email protected] | fafc750 | 2011-04-27 04:37:56 | [diff] [blame] | 404 | if (!has_response_started()) { |
| 405 | NotifyStartError(status); |
| 406 | } else if (pending_buf_) { |
[email protected] | 732adca1 | 2011-05-11 02:26:40 | [diff] [blame] | 407 | pending_buf_ = NULL; |
| 408 | pending_buf_size_ = 0; |
[email protected] | 3927f81 | 2010-02-10 06:41:02 | [diff] [blame] | 409 | NotifyDone(status); |
[email protected] | 732adca1 | 2011-05-11 02:26:40 | [diff] [blame] | 410 | NotifyReadComplete(0); |
[email protected] | 3927f81 | 2010-02-10 06:41:02 | [diff] [blame] | 411 | } else { |
| 412 | // Wait for the http stack to issue a Read request where we will notify |
| 413 | // that the job has completed. |
| 414 | request_status_ = status; |
[email protected] | 3927f81 | 2010-02-10 06:41:02 | [diff] [blame] | 415 | } |
| 416 | } |
[email protected] | 732adca1 | 2011-05-11 02:26:40 | [diff] [blame] | 417 | // Note |
| 418 | // The job could have been destroyed above. Please don't attempt to access |
| 419 | // member variables here. |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void URLRequestAutomationJob::Cleanup() { |
| 423 | headers_ = NULL; |
| 424 | mime_type_.erase(); |
| 425 | |
| 426 | id_ = 0; |
| 427 | tab_ = 0; |
| 428 | |
[email protected] | cbfd6e11 | 2011-02-17 22:09:18 | [diff] [blame] | 429 | DCHECK(!message_filter_); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 430 | DisconnectFromMessageFilter(); |
| 431 | |
| 432 | pending_buf_ = NULL; |
| 433 | pending_buf_size_ = 0; |
| 434 | } |
| 435 | |
| 436 | void URLRequestAutomationJob::StartAsync() { |
[email protected] | 5fb40dc | 2010-10-22 20:35:27 | [diff] [blame] | 437 | DVLOG(1) << "URLRequestAutomationJob: start request: " |
| 438 | << (request_ ? request_->url().spec() : "NULL request"); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 439 | |
| 440 | // If the job is cancelled before we got a chance to start it |
| 441 | // we have nothing much to do here. |
| 442 | if (is_done()) |
| 443 | return; |
| 444 | |
[email protected] | 18290eb | 2010-01-30 00:47:06 | [diff] [blame] | 445 | // We should not receive a Start request for a pending job. |
| 446 | DCHECK(!is_pending()); |
| 447 | |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 448 | if (!request_) { |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 449 | NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 450 | net::ERR_FAILED)); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 451 | return; |
| 452 | } |
| 453 | |
| 454 | // Register this request with automation message filter. |
| 455 | message_filter_->RegisterRequest(this); |
| 456 | |
[email protected] | fa0cf1a | 2009-07-31 00:20:08 | [diff] [blame] | 457 | // Strip unwanted headers. |
[email protected] | ee1a29b0 | 2010-05-06 20:42:12 | [diff] [blame] | 458 | net::HttpRequestHeaders new_request_headers; |
| 459 | new_request_headers.MergeFrom(request_->extra_request_headers()); |
| 460 | for (size_t i = 0; i < arraysize(kFilteredHeaderStrings); ++i) |
| 461 | new_request_headers.RemoveHeader(kFilteredHeaderStrings[i]); |
[email protected] | fa0cf1a | 2009-07-31 00:20:08 | [diff] [blame] | 462 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 463 | // Only add default Accept-Language if the request didn't have it specified. |
[email protected] | 81293f48 | 2012-08-13 19:35:45 | [diff] [blame] | 464 | if (!new_request_headers.HasHeader( |
| 465 | net::HttpRequestHeaders::kAcceptLanguage) && |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 466 | http_user_agent_settings_) { |
| 467 | std::string accept_language = |
| 468 | http_user_agent_settings_->GetAcceptLanguage(); |
| 469 | if (!accept_language.empty()) { |
| 470 | new_request_headers.SetHeader(net::HttpRequestHeaders::kAcceptLanguage, |
| 471 | accept_language); |
| 472 | } |
[email protected] | 81293f48 | 2012-08-13 19:35:45 | [diff] [blame] | 473 | } |
[email protected] | eb834fb | 2009-10-23 16:09:41 | [diff] [blame] | 474 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 475 | // URLRequest::SetReferrer() ensures that we do not send username and |
| 476 | // password fields in the referrer. |
| 477 | GURL referrer(request_->referrer()); |
[email protected] | fab40420 | 2009-09-10 17:19:06 | [diff] [blame] | 478 | |
| 479 | // The referrer header must be suppressed if the preceding URL was |
[email protected] | e600c82 | 2009-08-31 16:57:08 | [diff] [blame] | 480 | // a secure one and the new one is not. |
| 481 | if (referrer.SchemeIsSecure() && !request_->url().SchemeIsSecure()) { |
[email protected] | 5fb40dc | 2010-10-22 20:35:27 | [diff] [blame] | 482 | DVLOG(1) << "Suppressing referrer header since going from secure to " |
| 483 | "non-secure"; |
[email protected] | fab40420 | 2009-09-10 17:19:06 | [diff] [blame] | 484 | referrer = GURL(); |
[email protected] | e600c82 | 2009-08-31 16:57:08 | [diff] [blame] | 485 | } |
[email protected] | e600c82 | 2009-08-31 16:57:08 | [diff] [blame] | 486 | |
[email protected] | 6063340 | 2010-10-01 16:33:37 | [diff] [blame] | 487 | // Get the resource type (main_frame/script/image/stylesheet etc. |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 488 | const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); |
[email protected] | 6063340 | 2010-10-01 16:33:37 | [diff] [blame] | 489 | ResourceType::Type resource_type = ResourceType::MAIN_FRAME; |
[email protected] | 60cf2db | 2012-03-07 21:24:18 | [diff] [blame] | 490 | if (info) { |
| 491 | resource_type = info->GetResourceType(); |
[email protected] | 6063340 | 2010-10-01 16:33:37 | [diff] [blame] | 492 | } |
| 493 | |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 494 | // Construct UploadData from UploadDataStream. |
| 495 | scoped_refptr<net::UploadData> upload_data; |
| 496 | if (request_->get_upload()) |
| 497 | upload_data = CreateUploadData(request_->get_upload()); |
| 498 | |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 499 | // Ask automation to start this request. |
[email protected] | 3b3ec721 | 2011-10-21 17:35:08 | [diff] [blame] | 500 | AutomationURLRequest automation_request; |
| 501 | automation_request.url = request_->url().spec(); |
| 502 | automation_request.method = request_->method(); |
| 503 | automation_request.referrer = referrer.spec(); |
| 504 | automation_request.extra_request_headers = new_request_headers.ToString(); |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 505 | automation_request.upload_data = upload_data; |
[email protected] | 3b3ec721 | 2011-10-21 17:35:08 | [diff] [blame] | 506 | automation_request.resource_type = resource_type; |
| 507 | automation_request.load_flags = request_->load_flags(); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 508 | |
| 509 | DCHECK(message_filter_); |
[email protected] | f5494d4 | 2010-12-23 22:15:34 | [diff] [blame] | 510 | message_filter_->Send(new AutomationMsg_RequestStart( |
| 511 | tab_, id_, automation_request)); |
[email protected] | 2e4633c | 2009-07-09 16:58:06 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | void URLRequestAutomationJob::DisconnectFromMessageFilter() { |
| 515 | if (message_filter_) { |
| 516 | message_filter_->UnRegisterRequest(this); |
| 517 | message_filter_ = NULL; |
| 518 | } |
| 519 | } |
[email protected] | aeb9efc | 2010-01-08 05:55:50 | [diff] [blame] | 520 | |
[email protected] | 18290eb | 2010-01-30 00:47:06 | [diff] [blame] | 521 | void URLRequestAutomationJob::StartPendingJob( |
| 522 | int new_tab_handle, |
| 523 | AutomationResourceMessageFilter* new_filter) { |
| 524 | DCHECK(new_filter != NULL); |
| 525 | tab_ = new_tab_handle; |
| 526 | message_filter_ = new_filter; |
| 527 | is_pending_ = false; |
| 528 | Start(); |
| 529 | } |
| 530 | |
[email protected] | 3927f81 | 2010-02-10 06:41:02 | [diff] [blame] | 531 | void URLRequestAutomationJob::NotifyJobCompletionTask() { |
| 532 | if (!is_done()) { |
| 533 | NotifyDone(request_status_); |
| 534 | } |
| 535 | // Reset any pending reads. |
| 536 | if (pending_buf_) { |
| 537 | pending_buf_ = NULL; |
| 538 | pending_buf_size_ = 0; |
| 539 | NotifyReadComplete(0); |
| 540 | } |
| 541 | } |