OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "chrome/browser/automation/automation_resource_message_filter.h" | 10 #include "chrome/browser/automation/automation_resource_message_filter.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // If this is a pending job, then register it immediately with the message | 141 // If this is a pending job, then register it immediately with the message |
142 // filter so it can be serviced later when we receive a request from the | 142 // filter so it can be serviced later when we receive a request from the |
143 // external host to connect to the corresponding external tab. | 143 // external host to connect to the corresponding external tab. |
144 message_filter_->RegisterRequest(this); | 144 message_filter_->RegisterRequest(this); |
145 } | 145 } |
146 } | 146 } |
147 | 147 |
148 void URLRequestAutomationJob::Kill() { | 148 void URLRequestAutomationJob::Kill() { |
149 if (message_filter_.get()) { | 149 if (message_filter_.get()) { |
150 if (!is_pending()) { | 150 if (!is_pending()) { |
151 message_filter_->Send(new AutomationMsg_RequestEnd(0, tab_, id_, | 151 message_filter_->Send(new AutomationMsg_RequestEnd(tab_, id_, |
152 URLRequestStatus(URLRequestStatus::CANCELED, net::ERR_ABORTED))); | 152 URLRequestStatus(URLRequestStatus::CANCELED, net::ERR_ABORTED))); |
153 } | 153 } |
154 } | 154 } |
155 DisconnectFromMessageFilter(); | 155 DisconnectFromMessageFilter(); |
156 net::URLRequestJob::Kill(); | 156 net::URLRequestJob::Kill(); |
157 } | 157 } |
158 | 158 |
159 bool URLRequestAutomationJob::ReadRawData( | 159 bool URLRequestAutomationJob::ReadRawData( |
160 net::IOBuffer* buf, int buf_size, int* bytes_read) { | 160 net::IOBuffer* buf, int buf_size, int* bytes_read) { |
161 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() | 161 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() |
162 << " - read pending: " << buf_size; | 162 << " - read pending: " << buf_size; |
163 | 163 |
164 // We should not receive a read request for a pending job. | 164 // We should not receive a read request for a pending job. |
165 DCHECK(!is_pending()); | 165 DCHECK(!is_pending()); |
166 | 166 |
167 pending_buf_ = buf; | 167 pending_buf_ = buf; |
168 pending_buf_size_ = buf_size; | 168 pending_buf_size_ = buf_size; |
169 | 169 |
170 if (message_filter_) { | 170 if (message_filter_) { |
171 message_filter_->Send(new AutomationMsg_RequestRead(0, tab_, id_, | 171 message_filter_->Send(new AutomationMsg_RequestRead(tab_, id_, buf_size)); |
172 buf_size)); | |
173 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 172 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
174 } else { | 173 } else { |
175 MessageLoop::current()->PostTask( | 174 MessageLoop::current()->PostTask( |
176 FROM_HERE, | 175 FROM_HERE, |
177 method_factory_.NewRunnableMethod( | 176 method_factory_.NewRunnableMethod( |
178 &URLRequestAutomationJob::NotifyJobCompletionTask)); | 177 &URLRequestAutomationJob::NotifyJobCompletionTask)); |
179 } | 178 } |
180 return false; | 179 return false; |
181 } | 180 } |
182 | 181 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 return true; | 233 return true; |
235 } | 234 } |
236 | 235 |
237 bool URLRequestAutomationJob::MayFilterMessage(const IPC::Message& message, | 236 bool URLRequestAutomationJob::MayFilterMessage(const IPC::Message& message, |
238 int* request_id) { | 237 int* request_id) { |
239 switch (message.type()) { | 238 switch (message.type()) { |
240 case AutomationMsg_RequestStarted::ID: | 239 case AutomationMsg_RequestStarted::ID: |
241 case AutomationMsg_RequestData::ID: | 240 case AutomationMsg_RequestData::ID: |
242 case AutomationMsg_RequestEnd::ID: { | 241 case AutomationMsg_RequestEnd::ID: { |
243 void* iter = NULL; | 242 void* iter = NULL; |
244 int tab = 0; | 243 if (message.ReadInt(&iter, request_id)) |
245 if (message.ReadInt(&iter, &tab) && | |
246 message.ReadInt(&iter, request_id)) { | |
247 return true; | 244 return true; |
248 } | |
249 break; | 245 break; |
250 } | 246 } |
251 } | 247 } |
252 | 248 |
253 return false; | 249 return false; |
254 } | 250 } |
255 | 251 |
256 void URLRequestAutomationJob::OnMessage(const IPC::Message& message) { | 252 void URLRequestAutomationJob::OnMessage(const IPC::Message& message) { |
257 if (!request_) { | 253 if (!request_) { |
258 NOTREACHED() << __FUNCTION__ | 254 NOTREACHED() << __FUNCTION__ |
259 << ": Unexpected request received for job:" | 255 << ": Unexpected request received for job:" |
260 << id(); | 256 << id(); |
261 return; | 257 return; |
262 } | 258 } |
263 | 259 |
264 IPC_BEGIN_MESSAGE_MAP(URLRequestAutomationJob, message) | 260 IPC_BEGIN_MESSAGE_MAP(URLRequestAutomationJob, message) |
265 IPC_MESSAGE_HANDLER(AutomationMsg_RequestStarted, OnRequestStarted) | 261 IPC_MESSAGE_HANDLER(AutomationMsg_RequestStarted, OnRequestStarted) |
266 IPC_MESSAGE_HANDLER(AutomationMsg_RequestData, OnDataAvailable) | 262 IPC_MESSAGE_HANDLER(AutomationMsg_RequestData, OnDataAvailable) |
267 IPC_MESSAGE_HANDLER(AutomationMsg_RequestEnd, OnRequestEnd) | 263 IPC_MESSAGE_HANDLER(AutomationMsg_RequestEnd, OnRequestEnd) |
268 IPC_END_MESSAGE_MAP() | 264 IPC_END_MESSAGE_MAP() |
269 } | 265 } |
270 | 266 |
271 void URLRequestAutomationJob::OnRequestStarted(int tab, int id, | 267 void URLRequestAutomationJob::OnRequestStarted( |
272 const IPC::AutomationURLResponse& response) { | 268 int id, const AutomationURLResponse& response) { |
273 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() | 269 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() |
274 << " - response started."; | 270 << " - response started."; |
275 set_expected_content_size(response.content_length); | 271 set_expected_content_size(response.content_length); |
276 mime_type_ = response.mime_type; | 272 mime_type_ = response.mime_type; |
277 | 273 |
278 redirect_url_ = response.redirect_url; | 274 redirect_url_ = response.redirect_url; |
279 redirect_status_ = response.redirect_status; | 275 redirect_status_ = response.redirect_status; |
280 DCHECK(redirect_status_ == 0 || redirect_status_ == 200 || | 276 DCHECK(redirect_status_ == 0 || redirect_status_ == 200 || |
281 (redirect_status_ >= 300 && redirect_status_ < 400)); | 277 (redirect_status_ >= 300 && redirect_status_ < 400)); |
282 | 278 |
283 if (!response.headers.empty()) { | 279 if (!response.headers.empty()) { |
284 headers_ = new net::HttpResponseHeaders( | 280 headers_ = new net::HttpResponseHeaders( |
285 net::HttpUtil::AssembleRawHeaders(response.headers.data(), | 281 net::HttpUtil::AssembleRawHeaders(response.headers.data(), |
286 response.headers.size())); | 282 response.headers.size())); |
287 } | 283 } |
288 NotifyHeadersComplete(); | 284 NotifyHeadersComplete(); |
289 } | 285 } |
290 | 286 |
291 void URLRequestAutomationJob::OnDataAvailable( | 287 void URLRequestAutomationJob::OnDataAvailable( |
292 int tab, int id, const std::string& bytes) { | 288 int id, const std::string& bytes) { |
293 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() | 289 DVLOG(1) << "URLRequestAutomationJob: " << request_->url().spec() |
294 << " - data available, Size: " << bytes.size(); | 290 << " - data available, Size: " << bytes.size(); |
295 DCHECK(!bytes.empty()); | 291 DCHECK(!bytes.empty()); |
296 | 292 |
297 // The request completed, and we have all the data. | 293 // The request completed, and we have all the data. |
298 // Clear any IO pending status. | 294 // Clear any IO pending status. |
299 SetStatus(URLRequestStatus()); | 295 SetStatus(URLRequestStatus()); |
300 | 296 |
301 if (pending_buf_ && pending_buf_->data()) { | 297 if (pending_buf_ && pending_buf_->data()) { |
302 DCHECK_GE(pending_buf_size_, bytes.size()); | 298 DCHECK_GE(pending_buf_size_, bytes.size()); |
303 const int bytes_to_copy = std::min(bytes.size(), pending_buf_size_); | 299 const int bytes_to_copy = std::min(bytes.size(), pending_buf_size_); |
304 memcpy(pending_buf_->data(), &bytes[0], bytes_to_copy); | 300 memcpy(pending_buf_->data(), &bytes[0], bytes_to_copy); |
305 | 301 |
306 pending_buf_ = NULL; | 302 pending_buf_ = NULL; |
307 pending_buf_size_ = 0; | 303 pending_buf_size_ = 0; |
308 | 304 |
309 NotifyReadComplete(bytes_to_copy); | 305 NotifyReadComplete(bytes_to_copy); |
310 } else { | 306 } else { |
311 NOTREACHED() << "Received unexpected data of length:" << bytes.size(); | 307 NOTREACHED() << "Received unexpected data of length:" << bytes.size(); |
312 } | 308 } |
313 } | 309 } |
314 | 310 |
315 void URLRequestAutomationJob::OnRequestEnd( | 311 void URLRequestAutomationJob::OnRequestEnd( |
316 int tab, int id, const URLRequestStatus& status) { | 312 int id, const URLRequestStatus& status) { |
317 #ifndef NDEBUG | 313 #ifndef NDEBUG |
318 std::string url; | 314 std::string url; |
319 if (request_) | 315 if (request_) |
320 url = request_->url().spec(); | 316 url = request_->url().spec(); |
321 DVLOG(1) << "URLRequestAutomationJob: " << url << " - request end. Status: " | 317 DVLOG(1) << "URLRequestAutomationJob: " << url << " - request end. Status: " |
322 << status.status(); | 318 << status.status(); |
323 #endif | 319 #endif |
324 | 320 |
325 // TODO(tommi): When we hit certificate errors, notify the delegate via | 321 // TODO(tommi): When we hit certificate errors, notify the delegate via |
326 // OnSSLCertificateError(). Right now we don't have the certificate | 322 // OnSSLCertificateError(). Right now we don't have the certificate |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 | 425 |
430 // Get the resource type (main_frame/script/image/stylesheet etc. | 426 // Get the resource type (main_frame/script/image/stylesheet etc. |
431 ResourceDispatcherHostRequestInfo* request_info = | 427 ResourceDispatcherHostRequestInfo* request_info = |
432 ResourceDispatcherHost::InfoForRequest(request_); | 428 ResourceDispatcherHost::InfoForRequest(request_); |
433 ResourceType::Type resource_type = ResourceType::MAIN_FRAME; | 429 ResourceType::Type resource_type = ResourceType::MAIN_FRAME; |
434 if (request_info) { | 430 if (request_info) { |
435 resource_type = request_info->resource_type(); | 431 resource_type = request_info->resource_type(); |
436 } | 432 } |
437 | 433 |
438 // Ask automation to start this request. | 434 // Ask automation to start this request. |
439 IPC::AutomationURLRequest automation_request( | 435 AutomationURLRequest automation_request( |
440 request_->url().spec(), | 436 request_->url().spec(), |
441 request_->method(), | 437 request_->method(), |
442 referrer.spec(), | 438 referrer.spec(), |
443 new_request_headers.ToString(), | 439 new_request_headers.ToString(), |
444 request_->get_upload(), | 440 request_->get_upload(), |
445 resource_type, | 441 resource_type, |
446 request_->load_flags()); | 442 request_->load_flags()); |
447 | 443 |
448 DCHECK(message_filter_); | 444 DCHECK(message_filter_); |
449 message_filter_->Send(new AutomationMsg_RequestStart(0, tab_, id_, | 445 message_filter_->Send(new AutomationMsg_RequestStart( |
450 automation_request)); | 446 tab_, id_, automation_request)); |
451 } | 447 } |
452 | 448 |
453 void URLRequestAutomationJob::DisconnectFromMessageFilter() { | 449 void URLRequestAutomationJob::DisconnectFromMessageFilter() { |
454 if (message_filter_) { | 450 if (message_filter_) { |
455 message_filter_->UnRegisterRequest(this); | 451 message_filter_->UnRegisterRequest(this); |
456 message_filter_ = NULL; | 452 message_filter_ = NULL; |
457 } | 453 } |
458 } | 454 } |
459 | 455 |
460 void URLRequestAutomationJob::StartPendingJob( | 456 void URLRequestAutomationJob::StartPendingJob( |
(...skipping 10 matching lines...) Expand all Loading... |
471 if (!is_done()) { | 467 if (!is_done()) { |
472 NotifyDone(request_status_); | 468 NotifyDone(request_status_); |
473 } | 469 } |
474 // Reset any pending reads. | 470 // Reset any pending reads. |
475 if (pending_buf_) { | 471 if (pending_buf_) { |
476 pending_buf_ = NULL; | 472 pending_buf_ = NULL; |
477 pending_buf_size_ = 0; | 473 pending_buf_size_ = 0; |
478 NotifyReadComplete(0); | 474 NotifyReadComplete(0); |
479 } | 475 } |
480 } | 476 } |
OLD | NEW |