OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/navigation_resource_handler.h" | |
6 | |
7 #include "base/guid.h" | |
8 #include "base/logging.h" | |
9 #include "content/browser/loader/navigation_url_loader_core.h" | |
10 #include "content/browser/loader/resource_request_info_impl.h" | |
11 #include "content/browser/resource_context_impl.h" | |
12 #include "content/browser/streams/stream.h" | |
13 #include "content/browser/streams/stream_context.h" | |
14 #include "content/browser/streams/stream_registry.h" | |
15 #include "content/public/browser/resource_controller.h" | |
16 #include "content/public/browser/stream_handle.h" | |
17 #include "content/public/common/resource_response.h" | |
18 #include "net/base/io_buffer.h" | |
19 #include "net/base/net_errors.h" | |
20 #include "net/url_request/url_request.h" | |
21 #include "url/url_constants.h" | |
22 | |
23 namespace content { | |
24 | |
25 NavigationResourceHandler::NavigationResourceHandler( | |
26 net::URLRequest* request, | |
27 NavigationURLLoaderCore* core) | |
28 : ResourceHandler(request), | |
29 core_(core) { | |
30 core_->set_resource_handler(this); | |
31 } | |
32 | |
33 NavigationResourceHandler::~NavigationResourceHandler() { | |
34 if (stream_.get()) { | |
35 stream_->Finalize(); | |
36 stream_->RemoveWriteObserver(this); | |
37 } | |
38 if (core_.get()) { | |
39 core_->RequestFailedOnIOThread(net::ERR_ABORTED); | |
40 core_ = NULL; | |
41 } | |
42 } | |
43 | |
44 void NavigationResourceHandler::Cancel() { | |
45 controller()->Cancel(); | |
clamy
2014/09/12 20:51:25
So if we cancel the request on the UI thread, this
davidben
2014/09/19 18:30:49
Done. It'll actually call onResponseCompleted firs
| |
46 } | |
47 | |
48 void NavigationResourceHandler::Resume() { | |
49 controller()->Resume(); | |
50 } | |
51 | |
52 bool NavigationResourceHandler::OnUploadProgress(uint64 position, | |
53 uint64 size) { | |
54 return true; | |
55 } | |
56 | |
57 bool NavigationResourceHandler::OnRequestRedirected( | |
58 const net::RedirectInfo& redirect_info, | |
59 ResourceResponse* response, | |
60 bool* defer) { | |
61 // TODO(davidben): Perform a CSP check here, and anything else that would have | |
62 // been done renderer-side. | |
63 core_->RequestRedirectedOnIOThread(redirect_info, response); | |
64 *defer = true; | |
65 return true; | |
66 } | |
67 | |
68 bool NavigationResourceHandler::OnResponseStarted(ResourceResponse* response, | |
69 bool* defer) { | |
70 DCHECK(!stream_.get()); | |
71 DCHECK(core_.get()); | |
72 | |
73 ResourceRequestInfoImpl* info = GetRequestInfo(); | |
74 | |
75 // If the BufferedResourceHandler intercepted this request and converted it | |
76 // into a download, it will still call OnResponseStarted and immediately | |
77 // cancel. Ignore the call; OnReadCompleted will happen shortly. | |
78 // | |
79 // TODO(davidben): Move the dispatch out of BufferedResourceHandler. Perhaps | |
80 // all the way to the UI thread. Downloads, user certificates, etc., should be | |
81 // dispatched at the navigation layer. | |
82 if (info->IsDownload() || info->is_stream()) | |
83 return true; | |
84 | |
85 StreamContext* stream_context = | |
86 GetStreamContextForResourceContext(info->GetContext()); | |
87 | |
88 GURL stream_url(std::string(url::kBlobScheme) + ":" + | |
89 request()->url().GetOrigin().spec() + | |
90 base::GenerateGUID()); | |
91 stream_ = new Stream(stream_context->registry(), this, stream_url); | |
92 | |
93 core_->ResponseStartedOnIOThread(response, stream_->CreateHandle()); | |
94 core_ = NULL; | |
95 return true; | |
96 } | |
97 | |
98 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
99 return true; | |
100 } | |
101 | |
102 bool NavigationResourceHandler::OnBeforeNetworkStart(const GURL& url, | |
103 bool* defer) { | |
104 return true; | |
105 } | |
106 | |
107 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
108 int* buf_size, | |
109 int min_size) { | |
110 static const int kReadBufSize = 32768; | |
clamy
2014/09/12 20:51:25
This seem to be a copy paste of StreamResourceHand
davidben
2014/09/19 18:30:49
Using StreamResourceHandler directly is a little a
| |
111 | |
112 DCHECK(buf && buf_size); | |
113 if (!read_buffer_.get()) | |
114 read_buffer_ = new net::IOBuffer(kReadBufSize); | |
115 *buf = read_buffer_.get(); | |
116 *buf_size = kReadBufSize; | |
117 | |
118 return true; | |
119 } | |
120 | |
121 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
122 DCHECK(stream_.get()); | |
123 | |
124 if (!bytes_read) | |
125 return true; | |
126 | |
127 // We have more data to read. | |
128 DCHECK(read_buffer_.get()); | |
129 | |
130 // Release the ownership of the buffer, and store a reference | |
131 // to it. A new one will be allocated in OnWillRead(). | |
132 scoped_refptr<net::IOBuffer> buffer; | |
133 read_buffer_.swap(buffer); | |
134 stream_->AddData(buffer, bytes_read); | |
135 | |
136 if (!stream_->can_add_data()) | |
137 *defer = true; | |
138 | |
139 return true; | |
140 } | |
141 | |
142 void NavigationResourceHandler::OnResponseCompleted( | |
143 const net::URLRequestStatus& status, | |
144 const std::string& security_info, | |
145 bool* defer) { | |
146 // If the request has already committed, close the stream and leave it as-is. | |
147 // | |
148 // TODO(davidben): Is the final status of a navigation request meaningful? | |
149 // Perhaps we should eventually extend the streams interface to push the | |
150 // status down. (If there was an error partway through loading the top-level.) | |
151 if (stream_.get()) { | |
152 stream_->Finalize(); | |
153 return; | |
154 } | |
155 | |
156 DCHECK_NE(net::OK, status.error()); | |
157 core_->RequestFailedOnIOThread(status.error()); | |
158 core_ = NULL; | |
159 } | |
160 | |
161 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { | |
162 NOTREACHED(); | |
163 } | |
164 | |
165 void NavigationResourceHandler::OnSpaceAvailable(Stream* stream) { | |
166 controller()->Resume(); | |
167 } | |
168 | |
169 void NavigationResourceHandler::OnClose(Stream* stream) { | |
170 // TODO(davidben): This doesn't cancel the request synchronously. Does this | |
171 // matter? | |
172 controller()->Cancel(); | |
173 } | |
174 | |
175 } // namespace content | |
OLD | NEW |