blob: 663808400ee71def60bbc7b4b11f0369a31dc060 [file] [log] [blame]
[email protected]8a58f9a2010-05-18 18:38:091// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]e3c404b2008-12-23 01:07:322// 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]7a4de7a62010-08-17 18:38:246
[email protected]5203e602009-07-29 03:42:007#include "base/logging.h"
[email protected]83475ff2010-09-20 09:16:468#include "chrome/browser/net/load_timing_observer.h"
9#include "chrome/browser/renderer_host/global_request_id.h"
[email protected]e09ba552009-02-05 03:26:2910#include "chrome/common/render_messages.h"
[email protected]aa2f7f32010-08-16 21:35:3311#include "net/base/io_buffer.h"
[email protected]24fe6f1e2010-08-13 21:25:2812#include "net/http/http_response_headers.h"
[email protected]e3c404b2008-12-23 01:07:3213
14SyncResourceHandler::SyncResourceHandler(
15 ResourceDispatcherHost::Receiver* receiver,
[email protected]83475ff2010-09-20 09:16:4616 int process_id,
[email protected]e3c404b2008-12-23 01:07:3217 const GURL& url,
[email protected]83475ff2010-09-20 09:16:4618 IPC::Message* result_message,
19 ResourceDispatcherHost* resource_dispatcher_host)
[email protected]9dea9e1f2009-01-29 00:30:4720 : read_buffer_(new net::IOBuffer(kReadBufSize)),
21 receiver_(receiver),
[email protected]83475ff2010-09-20 09:16:4622 process_id_(process_id),
23 result_message_(result_message),
24 rdh_(resource_dispatcher_host) {
[email protected]e3c404b2008-12-23 01:07:3225 result_.final_url = url;
[email protected]e3c404b2008-12-23 01:07:3226}
27
[email protected]63f3e1d2009-03-06 19:56:1228SyncResourceHandler::~SyncResourceHandler() {
[email protected]63f3e1d2009-03-06 19:56:1229}
30
[email protected]afd832c2010-03-02 04:53:3131bool SyncResourceHandler::OnUploadProgress(int request_id,
32 uint64 position,
33 uint64 size) {
34 return true;
35}
36
[email protected]e3c404b2008-12-23 01:07:3237bool SyncResourceHandler::OnRequestRedirected(int request_id,
[email protected]6568a9e32009-07-30 18:01:3938 const GURL& new_url,
39 ResourceResponse* response,
40 bool* defer) {
[email protected]83475ff2010-09-20 09:16:4641 URLRequest* request = rdh_->GetURLRequest(
42 GlobalRequestID(process_id_, request_id));
43 LoadTimingObserver::PopulateTimingInfo(request, response);
44
[email protected]6568a9e32009-07-30 18:01:3945 // 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]e3c404b2008-12-23 01:07:3252 result_.final_url = new_url;
53 return true;
54}
55
56bool SyncResourceHandler::OnResponseStarted(int request_id,
57 ResourceResponse* response) {
[email protected]83475ff2010-09-20 09:16:4658 URLRequest* request = rdh_->GetURLRequest(
59 GlobalRequestID(process_id_, request_id));
60 LoadTimingObserver::PopulateTimingInfo(request, response);
61
[email protected]e3c404b2008-12-23 01:07:3262 // 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]83475ff2010-09-20 09:16:4666 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]e3c404b2008-12-23 01:07:3271 return true;
72}
73
[email protected]afd832c2010-03-02 04:53:3174bool SyncResourceHandler::OnWillStart(int request_id,
75 const GURL& url,
76 bool* defer) {
77 return true;
78}
79
[email protected]9dea9e1f2009-01-29 00:30:4780bool SyncResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf,
81 int* buf_size, int min_size) {
[email protected]e3c404b2008-12-23 01:07:3282 DCHECK(min_size == -1);
[email protected]9dea9e1f2009-01-29 00:30:4783 *buf = read_buffer_.get();
[email protected]e3c404b2008-12-23 01:07:3284 *buf_size = kReadBufSize;
85 return true;
86}
87
88bool SyncResourceHandler::OnReadCompleted(int request_id, int* bytes_read) {
89 if (!*bytes_read)
90 return true;
[email protected]9dea9e1f2009-01-29 00:30:4791 result_.data.append(read_buffer_->data(), *bytes_read);
[email protected]e3c404b2008-12-23 01:07:3292 return true;
93}
94
[email protected]c4891b32009-03-08 07:41:3195bool SyncResourceHandler::OnResponseCompleted(
96 int request_id,
97 const URLRequestStatus& status,
98 const std::string& security_info) {
[email protected]e3c404b2008-12-23 01:07:3299 result_.status = status;
100
101 ViewHostMsg_SyncLoad::WriteReplyParams(result_message_, result_);
102 receiver_->Send(result_message_);
[email protected]63f3e1d2009-03-06 19:56:12103 result_message_ = NULL;
[email protected]e3c404b2008-12-23 01:07:32104 return true;
105}
[email protected]afd832c2010-03-02 04:53:31106
107void SyncResourceHandler::OnRequestClosed() {
[email protected]3dc99372010-05-22 02:25:09108 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]afd832c2010-03-02 04:53:31114}