Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / loader / sync_resource_handler.cc
blobebb2a437533dbb57152dd01ee0fc1c7a7ffbf59b
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
3 // found in the LICENSE file.
5 #include "content/browser/loader/sync_resource_handler.h"
7 #include "base/logging.h"
8 #include "content/browser/devtools/devtools_netlog_observer.h"
9 #include "content/browser/loader/resource_dispatcher_host_impl.h"
10 #include "content/browser/loader/resource_message_filter.h"
11 #include "content/browser/loader/resource_request_info_impl.h"
12 #include "content/common/resource_messages.h"
13 #include "content/public/browser/resource_dispatcher_host_delegate.h"
14 #include "content/public/browser/resource_request_info.h"
15 #include "net/base/io_buffer.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/url_request/redirect_info.h"
19 namespace content {
21 SyncResourceHandler::SyncResourceHandler(
22 net::URLRequest* request,
23 IPC::Message* result_message,
24 ResourceDispatcherHostImpl* resource_dispatcher_host)
25 : ResourceHandler(request),
26 read_buffer_(new net::IOBuffer(kReadBufSize)),
27 result_message_(result_message),
28 rdh_(resource_dispatcher_host),
29 total_transfer_size_(0) {
30 result_.final_url = request->url();
33 SyncResourceHandler::~SyncResourceHandler() {
34 if (result_message_) {
35 result_message_->set_reply_error();
36 ResourceMessageFilter* filter = GetFilter();
37 // If the filter doesn't exist at this point, the process has died and isn't
38 // waiting for the result message anymore.
39 if (filter)
40 filter->Send(result_message_);
44 bool SyncResourceHandler::OnRequestRedirected(
45 const net::RedirectInfo& redirect_info,
46 ResourceResponse* response,
47 bool* defer) {
48 if (rdh_->delegate()) {
49 rdh_->delegate()->OnRequestRedirected(
50 redirect_info.new_url, request(), GetRequestInfo()->GetContext(),
51 response);
54 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
55 // TODO(darin): It would be much better if this could live in WebCore, but
56 // doing so requires API changes at all levels. Similar code exists in
57 // WebCore/platform/network/cf/ResourceHandleCFNet.cpp :-(
58 if (redirect_info.new_url.GetOrigin() != result_.final_url.GetOrigin()) {
59 LOG(ERROR) << "Cross origin redirect denied";
60 return false;
62 result_.final_url = redirect_info.new_url;
64 total_transfer_size_ += request()->GetTotalReceivedBytes();
65 return true;
68 bool SyncResourceHandler::OnResponseStarted(
69 ResourceResponse* response,
70 bool* defer) {
71 const ResourceRequestInfoImpl* info = GetRequestInfo();
72 if (!info->filter())
73 return false;
75 if (rdh_->delegate()) {
76 rdh_->delegate()->OnResponseStarted(
77 request(), info->GetContext(), response, info->filter());
80 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
82 // We don't care about copying the status here.
83 result_.headers = response->head.headers;
84 result_.mime_type = response->head.mime_type;
85 result_.charset = response->head.charset;
86 result_.download_file_path = response->head.download_file_path;
87 result_.request_time = response->head.request_time;
88 result_.response_time = response->head.response_time;
89 result_.load_timing = response->head.load_timing;
90 result_.devtools_info = response->head.devtools_info;
91 return true;
94 bool SyncResourceHandler::OnWillStart(const GURL& url, bool* defer) {
95 return true;
98 bool SyncResourceHandler::OnBeforeNetworkStart(const GURL& url, bool* defer) {
99 return true;
102 bool SyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
103 int* buf_size,
104 int min_size) {
105 DCHECK(min_size == -1);
106 *buf = read_buffer_.get();
107 *buf_size = kReadBufSize;
108 return true;
111 bool SyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
112 if (!bytes_read)
113 return true;
114 result_.data.append(read_buffer_->data(), bytes_read);
115 return true;
118 void SyncResourceHandler::OnResponseCompleted(
119 const net::URLRequestStatus& status,
120 const std::string& security_info,
121 bool* defer) {
122 ResourceMessageFilter* filter = GetFilter();
123 if (!filter)
124 return;
126 result_.error_code = status.error();
128 int total_transfer_size = request()->GetTotalReceivedBytes();
129 result_.encoded_data_length = total_transfer_size_ + total_transfer_size;
131 ResourceHostMsg_SyncLoad::WriteReplyParams(result_message_, result_);
132 filter->Send(result_message_);
133 result_message_ = NULL;
134 return;
137 void SyncResourceHandler::OnDataDownloaded(int bytes_downloaded) {
138 // Sync requests don't involve ResourceMsg_DataDownloaded messages
139 // being sent back to renderers as progress is made.
142 } // namespace content