OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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. | |
4 | |
5 #include "content/browser/loader/detached_resource_handler.h" | |
6 | |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
10 #include "base/debug/alias.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "content/browser/devtools/devtools_netlog_observer.h" | |
13 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
14 #include "content/browser/loader/resource_message_filter.h" | |
15 #include "content/browser/loader/resource_request_info_impl.h" | |
16 #include "content/common/resource_messages.h" | |
17 #include "content/public/browser/resource_dispatcher_host_delegate.h" | |
18 #include "net/base/io_buffer.h" | |
19 #include "net/base/net_errors.h" | |
20 #include "net/url_request/url_request.h" | |
21 | |
22 using base::TimeTicks; | |
23 | |
24 namespace content { | |
25 | |
26 namespace { | |
27 | |
28 static const int kReadBufSize = 3840; | |
asanka
2013/10/11 21:01:20
Nit: Explain where this comes from? Why is it this
jkarlin2
2013/10/14 14:30:00
Done. Ah, this was supposed to be fixed earlier.
| |
29 | |
30 } // namespace | |
31 | |
32 | |
33 DetachedResourceHandler::DetachedResourceHandler( | |
34 net::URLRequest* request, ResourceDispatcherHostImpl* rdh) | |
35 : request_(request), rdh_(rdh) {} | |
36 | |
37 DetachedResourceHandler::~DetachedResourceHandler() {} | |
38 | |
39 bool DetachedResourceHandler::OnResponseCompleted( | |
40 int request_id, const net::URLRequestStatus& status, | |
41 const std::string& security_info) { | |
42 const ResourceRequestInfoImpl* info = | |
43 ResourceRequestInfoImpl::ForRequest(request_); | |
44 | |
45 if (!info->filter()) | |
46 return true; | |
47 | |
48 TimeTicks completion_time = TimeTicks::Now(); | |
49 | |
50 int error_code = status.error(); | |
51 | |
52 bool was_ignored_by_handler = info->WasIgnoredByHandler(); | |
53 | |
54 DCHECK(status.status() != net::URLRequestStatus::IO_PENDING); | |
asanka
2013/10/11 21:01:20
I'm curious why you are checking this in response
jkarlin2
2013/10/14 14:30:00
Done. Copied from AsyncResourceHandler::OnResponse
| |
55 // If this check fails, then we're in an inconsistent state because all | |
56 // requests ignored by the handler should be canceled (which should result in | |
57 // the ERR_ABORTED error code). | |
58 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED); | |
59 | |
60 // TODO(mkosiba): Fix up cases where we create a URLRequestStatus | |
61 // with a status() != SUCCESS and an error_code() == net::OK. | |
62 if (status.status() == net::URLRequestStatus::CANCELED && | |
63 error_code == net::OK) { | |
64 error_code = net::ERR_ABORTED; | |
65 } else if (status.status() == net::URLRequestStatus::FAILED && | |
66 error_code == net::OK) { | |
67 error_code = net::ERR_FAILED; | |
68 } | |
69 | |
70 info->filter()->Send(new ResourceMsg_RequestComplete( | |
71 request_id, error_code, was_ignored_by_handler, security_info, | |
72 completion_time)); | |
73 return true; | |
74 } | |
75 | |
76 bool DetachedResourceHandler::OnUploadProgress(int request_id, uint64 position, | |
77 uint64 size) { | |
78 const ResourceRequestInfoImpl* info = | |
79 ResourceRequestInfoImpl::ForRequest(request_); | |
80 if (!info->filter()) | |
81 return true; | |
82 info->filter()->Send( | |
83 new ResourceMsg_UploadProgress(request_id, position, size)); | |
84 return true; | |
85 } | |
86 | |
87 bool DetachedResourceHandler::OnResponseStarted(int request_id, | |
88 ResourceResponse* response, | |
89 bool* defer) { | |
90 const ResourceRequestInfoImpl* info = | |
91 ResourceRequestInfoImpl::ForRequest(request_); | |
92 | |
93 if (!info->filter()) | |
94 return true; | |
95 | |
96 if (rdh_->delegate()) { | |
97 rdh_->delegate()->OnResponseStarted(request_, info->GetContext(), response, | |
98 info->filter()); | |
99 } | |
100 | |
101 DevToolsNetLogObserver::PopulateResponseInfo(request_, response); | |
102 | |
103 response->head.request_start = request_->creation_time(); | |
104 response->head.response_start = TimeTicks::Now(); | |
105 info->filter()->Send( | |
106 new ResourceMsg_ReceivedResponse(request_id, response->head)); | |
107 | |
108 if (request_->response_info().metadata.get()) { | |
109 std::vector<char> copy(request_->response_info().metadata->data(), | |
110 request_->response_info().metadata->data() + | |
111 request_->response_info().metadata->size()); | |
112 info->filter()->Send( | |
113 new ResourceMsg_ReceivedCachedMetadata(request_id, copy)); | |
114 } | |
115 | |
116 return true; | |
117 } | |
118 | |
119 bool DetachedResourceHandler::OnWillStart(int request_id, const GURL& url, | |
120 bool* defer) { | |
121 return true; | |
122 } | |
123 | |
124 bool DetachedResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | |
125 int* buf_size, int min_size) { | |
126 // Give the loader something to write its data to, but never reference it. | |
asanka
2013/10/11 21:01:20
hm? But you are keeping a reference to the buffer
jkarlin2
2013/10/14 14:30:00
Done.
| |
127 if (!read_buffer_) { | |
asanka
2013/10/11 21:01:20
Nit: No braces for single line conditional.
jkarlin2
2013/10/14 14:30:00
Done.
| |
128 read_buffer_ = new net::IOBuffer(kReadBufSize); | |
129 } | |
130 | |
131 *buf = read_buffer_.get(); | |
132 *buf_size = kReadBufSize; | |
133 return true; | |
134 } | |
135 | |
136 bool DetachedResourceHandler::OnReadCompleted(int request_id, int bytes_read, | |
137 bool* defer) { | |
138 return true; | |
139 } | |
140 | |
141 bool DetachedResourceHandler::OnRequestRedirected(int request_id, | |
142 const GURL& new_url, | |
143 ResourceResponse* response, | |
144 bool* defer) { | |
145 return true; | |
146 } | |
147 | |
148 void DetachedResourceHandler::OnDataDownloaded(int request_id, | |
149 int bytes_downloaded) {} | |
150 | |
151 } // namespace content | |
OLD | NEW |