[email protected] | 8a58f9a | 2010-05-18 18:38:09 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [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/renderer_host/sync_resource_handler.h" |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 6 | |
[email protected] | 5203e60 | 2009-07-29 03:42:00 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 83475ff | 2010-09-20 09:16:46 | [diff] [blame] | 8 | #include "chrome/browser/net/load_timing_observer.h" |
| 9 | #include "chrome/browser/renderer_host/global_request_id.h" |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 10 | #include "chrome/common/render_messages.h" |
[email protected] | aa2f7f3 | 2010-08-16 21:35:33 | [diff] [blame] | 11 | #include "net/base/io_buffer.h" |
[email protected] | 24fe6f1e | 2010-08-13 21:25:28 | [diff] [blame] | 12 | #include "net/http/http_response_headers.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 13 | |
| 14 | SyncResourceHandler::SyncResourceHandler( |
| 15 | ResourceDispatcherHost::Receiver* receiver, |
[email protected] | 83475ff | 2010-09-20 09:16:46 | [diff] [blame] | 16 | int process_id, |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 17 | const GURL& url, |
[email protected] | 83475ff | 2010-09-20 09:16:46 | [diff] [blame] | 18 | IPC::Message* result_message, |
| 19 | ResourceDispatcherHost* resource_dispatcher_host) |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 20 | : read_buffer_(new net::IOBuffer(kReadBufSize)), |
| 21 | receiver_(receiver), |
[email protected] | 83475ff | 2010-09-20 09:16:46 | [diff] [blame] | 22 | process_id_(process_id), |
| 23 | result_message_(result_message), |
| 24 | rdh_(resource_dispatcher_host) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 25 | result_.final_url = url; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 26 | } |
| 27 | |
[email protected] | 63f3e1d | 2009-03-06 19:56:12 | [diff] [blame] | 28 | SyncResourceHandler::~SyncResourceHandler() { |
[email protected] | 63f3e1d | 2009-03-06 19:56:12 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | afd832c | 2010-03-02 04:53:31 | [diff] [blame] | 31 | bool SyncResourceHandler::OnUploadProgress(int request_id, |
| 32 | uint64 position, |
| 33 | uint64 size) { |
| 34 | return true; |
| 35 | } |
| 36 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 37 | bool SyncResourceHandler::OnRequestRedirected(int request_id, |
[email protected] | 6568a9e3 | 2009-07-30 18:01:39 | [diff] [blame] | 38 | const GURL& new_url, |
| 39 | ResourceResponse* response, |
| 40 | bool* defer) { |
[email protected] | 83475ff | 2010-09-20 09:16:46 | [diff] [blame] | 41 | URLRequest* request = rdh_->GetURLRequest( |
| 42 | GlobalRequestID(process_id_, request_id)); |
| 43 | LoadTimingObserver::PopulateTimingInfo(request, response); |
| 44 | |
[email protected] | 6568a9e3 | 2009-07-30 18:01:39 | [diff] [blame] | 45 | // TODO(darin): It would be much better if this could live in WebCore, but |
| 46 | // doing so requires API changes at all levels. Similar code exists in |
| 47 | // WebCore/platform/network/cf/ResourceHandleCFNet.cpp :-( |
| 48 | if (new_url.GetOrigin() != result_.final_url.GetOrigin()) { |
| 49 | LOG(ERROR) << "Cross origin redirect denied"; |
| 50 | return false; |
| 51 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 52 | result_.final_url = new_url; |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | bool SyncResourceHandler::OnResponseStarted(int request_id, |
| 57 | ResourceResponse* response) { |
[email protected] | 83475ff | 2010-09-20 09:16:46 | [diff] [blame] | 58 | URLRequest* request = rdh_->GetURLRequest( |
| 59 | GlobalRequestID(process_id_, request_id)); |
| 60 | LoadTimingObserver::PopulateTimingInfo(request, response); |
| 61 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 62 | // We don't care about copying the status here. |
| 63 | result_.headers = response->response_head.headers; |
| 64 | result_.mime_type = response->response_head.mime_type; |
| 65 | result_.charset = response->response_head.charset; |
[email protected] | 83475ff | 2010-09-20 09:16:46 | [diff] [blame] | 66 | result_.request_time = response->response_head.request_time; |
| 67 | result_.response_time = response->response_head.response_time; |
| 68 | result_.connection_id = response->response_head.connection_id; |
| 69 | result_.connection_reused = response->response_head.connection_reused; |
| 70 | result_.load_timing = response->response_head.load_timing; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | |
[email protected] | afd832c | 2010-03-02 04:53:31 | [diff] [blame] | 74 | bool SyncResourceHandler::OnWillStart(int request_id, |
| 75 | const GURL& url, |
| 76 | bool* defer) { |
| 77 | return true; |
| 78 | } |
| 79 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 80 | bool SyncResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
| 81 | int* buf_size, int min_size) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 82 | DCHECK(min_size == -1); |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 83 | *buf = read_buffer_.get(); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 84 | *buf_size = kReadBufSize; |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool SyncResourceHandler::OnReadCompleted(int request_id, int* bytes_read) { |
| 89 | if (!*bytes_read) |
| 90 | return true; |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 91 | result_.data.append(read_buffer_->data(), *bytes_read); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
[email protected] | c4891b3 | 2009-03-08 07:41:31 | [diff] [blame] | 95 | bool SyncResourceHandler::OnResponseCompleted( |
| 96 | int request_id, |
| 97 | const URLRequestStatus& status, |
| 98 | const std::string& security_info) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 99 | result_.status = status; |
| 100 | |
| 101 | ViewHostMsg_SyncLoad::WriteReplyParams(result_message_, result_); |
| 102 | receiver_->Send(result_message_); |
[email protected] | 63f3e1d | 2009-03-06 19:56:12 | [diff] [blame] | 103 | result_message_ = NULL; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 104 | return true; |
| 105 | } |
[email protected] | afd832c | 2010-03-02 04:53:31 | [diff] [blame] | 106 | |
| 107 | void SyncResourceHandler::OnRequestClosed() { |
[email protected] | 3dc9937 | 2010-05-22 02:25:09 | [diff] [blame] | 108 | if (!result_message_) |
| 109 | return; |
| 110 | |
| 111 | result_message_->set_reply_error(); |
| 112 | receiver_->Send(result_message_); |
| 113 | receiver_ = NULL; // URLRequest is gone, and perhaps also the receiver. |
[email protected] | afd832c | 2010-03-02 04:53:31 | [diff] [blame] | 114 | } |