Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: content/browser/loader/sync_resource_handler.cc

Issue 2476163003: Refactor ResourceHandler API. (Closed)
Patch Set: Minor cleanups, one real fix Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "content/browser/loader/sync_resource_handler.h" 5 #include "content/browser/loader/sync_resource_handler.h"
6 6
7 #include "base/callback_helpers.h" 7 #include "base/callback_helpers.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "content/browser/loader/netlog_observer.h" 9 #include "content/browser/loader/netlog_observer.h"
10 #include "content/browser/loader/resource_dispatcher_host_impl.h" 10 #include "content/browser/loader/resource_dispatcher_host_impl.h"
11 #include "content/browser/loader/resource_message_filter.h" 11 #include "content/browser/loader/resource_message_filter.h"
12 #include "content/browser/loader/resource_request_info_impl.h" 12 #include "content/browser/loader/resource_request_info_impl.h"
13 #include "content/common/resource_messages.h" 13 #include "content/common/resource_messages.h"
14 #include "content/public/browser/resource_controller.h"
14 #include "content/public/browser/resource_dispatcher_host_delegate.h" 15 #include "content/public/browser/resource_dispatcher_host_delegate.h"
15 #include "content/public/browser/resource_request_info.h" 16 #include "content/public/browser/resource_request_info.h"
16 #include "net/base/io_buffer.h" 17 #include "net/base/io_buffer.h"
17 #include "net/http/http_response_headers.h" 18 #include "net/http/http_response_headers.h"
18 #include "net/url_request/redirect_info.h" 19 #include "net/url_request/redirect_info.h"
19 20
20 namespace content { 21 namespace content {
21 22
22 SyncResourceHandler::SyncResourceHandler( 23 SyncResourceHandler::SyncResourceHandler(
23 net::URLRequest* request, 24 net::URLRequest* request,
24 const SyncLoadResultCallback& result_handler, 25 const SyncLoadResultCallback& result_handler,
25 ResourceDispatcherHostImpl* resource_dispatcher_host) 26 ResourceDispatcherHostImpl* resource_dispatcher_host)
26 : ResourceHandler(request), 27 : ResourceHandler(request),
27 read_buffer_(new net::IOBuffer(kReadBufSize)), 28 read_buffer_(new net::IOBuffer(kReadBufSize)),
28 result_handler_(result_handler), 29 result_handler_(result_handler),
29 rdh_(resource_dispatcher_host), 30 rdh_(resource_dispatcher_host),
30 total_transfer_size_(0) { 31 total_transfer_size_(0) {
31 result_.final_url = request->url(); 32 result_.final_url = request->url();
32 } 33 }
33 34
34 SyncResourceHandler::~SyncResourceHandler() { 35 SyncResourceHandler::~SyncResourceHandler() {
35 if (result_handler_) 36 if (result_handler_)
36 result_handler_.Run(nullptr); 37 result_handler_.Run(nullptr);
37 } 38 }
38 39
39 bool SyncResourceHandler::OnRequestRedirected( 40 void SyncResourceHandler::OnRequestRedirected(
40 const net::RedirectInfo& redirect_info, 41 const net::RedirectInfo& redirect_info,
41 ResourceResponse* response, 42 ResourceResponse* response,
42 bool* defer) { 43 bool* defer_or_cancel) {
43 if (rdh_->delegate()) { 44 if (rdh_->delegate()) {
44 rdh_->delegate()->OnRequestRedirected( 45 rdh_->delegate()->OnRequestRedirected(
45 redirect_info.new_url, request(), GetRequestInfo()->GetContext(), 46 redirect_info.new_url, request(), GetRequestInfo()->GetContext(),
46 response); 47 response);
47 } 48 }
48 49
49 NetLogObserver::PopulateResponseInfo(request(), response); 50 NetLogObserver::PopulateResponseInfo(request(), response);
50 // TODO(darin): It would be much better if this could live in WebCore, but 51 // TODO(darin): It would be much better if this could live in WebCore, but
51 // doing so requires API changes at all levels. Similar code exists in 52 // doing so requires API changes at all levels. Similar code exists in
52 // WebCore/platform/network/cf/ResourceHandleCFNet.cpp :-( 53 // WebCore/platform/network/cf/ResourceHandleCFNet.cpp :-(
53 if (redirect_info.new_url.GetOrigin() != result_.final_url.GetOrigin()) { 54 if (redirect_info.new_url.GetOrigin() != result_.final_url.GetOrigin()) {
54 LOG(ERROR) << "Cross origin redirect denied"; 55 LOG(ERROR) << "Cross origin redirect denied";
55 return false; 56 *defer_or_cancel = true;
57 controller()->Cancel();
58 return;
56 } 59 }
57 result_.final_url = redirect_info.new_url; 60 result_.final_url = redirect_info.new_url;
58 61
59 total_transfer_size_ += request()->GetTotalReceivedBytes(); 62 total_transfer_size_ += request()->GetTotalReceivedBytes();
60 return true;
61 } 63 }
62 64
63 bool SyncResourceHandler::OnResponseStarted( 65 void SyncResourceHandler::OnResponseStarted(ResourceResponse* response,
64 ResourceResponse* response, 66 bool* defer_or_cancel) {
65 bool* defer) {
66 const ResourceRequestInfoImpl* info = GetRequestInfo(); 67 const ResourceRequestInfoImpl* info = GetRequestInfo();
67 if (!info->filter()) 68 if (!info->filter()) {
68 return false; 69 *defer_or_cancel = true;
70 controller()->Cancel();
71 return;
72 }
69 73
70 if (rdh_->delegate()) { 74 if (rdh_->delegate()) {
71 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), 75 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(),
72 response); 76 response);
73 } 77 }
74 78
75 NetLogObserver::PopulateResponseInfo(request(), response); 79 NetLogObserver::PopulateResponseInfo(request(), response);
76 80
77 // We don't care about copying the status here. 81 // We don't care about copying the status here.
78 result_.headers = response->head.headers; 82 result_.headers = response->head.headers;
79 result_.mime_type = response->head.mime_type; 83 result_.mime_type = response->head.mime_type;
80 result_.charset = response->head.charset; 84 result_.charset = response->head.charset;
81 result_.download_file_path = response->head.download_file_path; 85 result_.download_file_path = response->head.download_file_path;
82 result_.request_time = response->head.request_time; 86 result_.request_time = response->head.request_time;
83 result_.response_time = response->head.response_time; 87 result_.response_time = response->head.response_time;
84 result_.load_timing = response->head.load_timing; 88 result_.load_timing = response->head.load_timing;
85 result_.devtools_info = response->head.devtools_info; 89 result_.devtools_info = response->head.devtools_info;
86 return true;
87 } 90 }
88 91
89 bool SyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { 92 void SyncResourceHandler::OnWillStart(const GURL& url, bool* defer_or_cancel) {}
90 return true;
91 }
92 93
93 bool SyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 94 bool SyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
94 int* buf_size, 95 int* buf_size,
95 int min_size) { 96 int min_size) {
96 DCHECK(min_size == -1); 97 DCHECK(min_size == -1);
97 *buf = read_buffer_.get(); 98 *buf = read_buffer_.get();
98 *buf_size = kReadBufSize; 99 *buf_size = kReadBufSize;
99 return true; 100 return true;
100 } 101 }
101 102
102 bool SyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { 103 void SyncResourceHandler::OnReadCompleted(int bytes_read,
104 bool* defer_or_cancel) {
103 if (!bytes_read) 105 if (!bytes_read)
104 return true; 106 return;
105 result_.data.append(read_buffer_->data(), bytes_read); 107 result_.data.append(read_buffer_->data(), bytes_read);
106 return true;
107 } 108 }
108 109
109 void SyncResourceHandler::OnResponseCompleted( 110 void SyncResourceHandler::OnResponseCompleted(
110 const net::URLRequestStatus& status, 111 const net::URLRequestStatus& status,
111 bool* defer) { 112 bool* defer) {
112 result_.error_code = status.error(); 113 result_.error_code = status.error();
113 114
114 int total_transfer_size = request()->GetTotalReceivedBytes(); 115 int total_transfer_size = request()->GetTotalReceivedBytes();
115 result_.encoded_data_length = total_transfer_size_ + total_transfer_size; 116 result_.encoded_data_length = total_transfer_size_ + total_transfer_size;
116 result_.encoded_body_length = request()->GetRawBodyBytes(); 117 result_.encoded_body_length = request()->GetRawBodyBytes();
117 118
118 base::ResetAndReturn(&result_handler_).Run(&result_); 119 base::ResetAndReturn(&result_handler_).Run(&result_);
119 } 120 }
120 121
121 void SyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { 122 void SyncResourceHandler::OnDataDownloaded(int bytes_downloaded) {
122 // Sync requests don't involve ResourceMsg_DataDownloaded messages 123 // Sync requests don't involve ResourceMsg_DataDownloaded messages
123 // being sent back to renderers as progress is made. 124 // being sent back to renderers as progress is made.
124 } 125 }
125 126
126 } // namespace content 127 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/sync_resource_handler.h ('k') | content/browser/loader/test_resource_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698