OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/mime_type_resource_handler.h" | 5 #include "content/browser/loader/intercepting_resource_handler.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
(...skipping 25 matching lines...) Expand all Loading... |
41 ResourceResponse* response, | 41 ResourceResponse* response, |
42 bool* defer) override { | 42 bool* defer) override { |
43 NOTREACHED(); | 43 NOTREACHED(); |
44 return false; | 44 return false; |
45 } | 45 } |
46 | 46 |
47 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { | 47 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { |
48 return false; | 48 return false; |
49 } | 49 } |
50 | 50 |
51 bool OnWillStart(const GURL& url, bool* defer) override { | 51 bool OnWillStart(const GURL& url, bool* defer) override { return false; } |
52 return false; | |
53 } | |
54 | 52 |
55 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { | 53 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { |
56 NOTREACHED(); | 54 NOTREACHED(); |
57 return false; | 55 return false; |
58 } | 56 } |
59 | 57 |
60 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 58 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
61 int* buf_size, | 59 int* buf_size, |
62 int min_size) override { | 60 int min_size) override { |
63 NOTREACHED(); | 61 NOTREACHED(); |
64 return false; | 62 return false; |
65 } | 63 } |
66 | 64 |
67 bool OnReadCompleted(int bytes_read, bool* defer) override { | 65 bool OnReadCompleted(int bytes_read, bool* defer) override { |
68 NOTREACHED(); | 66 NOTREACHED(); |
69 return false; | 67 return false; |
70 } | 68 } |
71 | 69 |
72 void OnResponseCompleted(const net::URLRequestStatus& status, | 70 void OnResponseCompleted(const net::URLRequestStatus& status, |
73 const std::string& security_info, | 71 const std::string& security_info, |
74 bool* defer) override { | 72 bool* defer) override {} |
75 } | |
76 | 73 |
77 void OnDataDownloaded(int bytes_downloaded) override { | 74 void OnDataDownloaded(int bytes_downloaded) override { NOTREACHED(); } |
78 NOTREACHED(); | |
79 } | |
80 | 75 |
81 private: | 76 private: |
82 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); | 77 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); |
83 }; | 78 }; |
84 | 79 |
| 80 class TestResourceHandlerWithBuffer : public ResourceHandler { |
| 81 public: |
| 82 TestResourceHandlerWithBuffer() |
| 83 : ResourceHandler(nullptr), buffer_(new net::IOBuffer(2048)) {} |
| 84 ~TestResourceHandlerWithBuffer() override {} |
| 85 |
| 86 void SetController(ResourceController* controller) override {} |
| 87 |
| 88 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, |
| 89 ResourceResponse* response, |
| 90 bool* defer) override { |
| 91 NOTREACHED(); |
| 92 return false; |
| 93 } |
| 94 |
| 95 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { |
| 96 return true; |
| 97 } |
| 98 |
| 99 bool OnWillStart(const GURL& url, bool* defer) override { return false; } |
| 100 |
| 101 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { |
| 102 NOTREACHED(); |
| 103 return false; |
| 104 } |
| 105 |
| 106 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 107 int* buf_size, |
| 108 int min_size) override { |
| 109 *buf = buffer_; |
| 110 *buf_size = 2048; |
| 111 return true; |
| 112 } |
| 113 |
| 114 bool OnReadCompleted(int bytes_read, bool* defer) override { |
| 115 DCHECK_LT(bytes_read, 2048); |
| 116 return true; |
| 117 } |
| 118 |
| 119 void OnResponseCompleted(const net::URLRequestStatus& status, |
| 120 const std::string& security_info, |
| 121 bool* defer) override {} |
| 122 |
| 123 void OnDataDownloaded(int bytes_downloaded) override { NOTREACHED(); } |
| 124 |
| 125 scoped_refptr<net::IOBuffer> buffer() { return buffer_; } |
| 126 |
| 127 private: |
| 128 scoped_refptr<net::IOBuffer> buffer_; |
| 129 DISALLOW_COPY_AND_ASSIGN(TestResourceHandlerWithBuffer); |
| 130 }; |
| 131 |
85 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { | 132 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { |
86 public: | 133 public: |
87 explicit TestResourceDispatcherHost(bool stream_has_handler) | 134 TestResourceDispatcherHost(bool stream_has_handler, bool use_buffer) |
88 : stream_has_handler_(stream_has_handler), | 135 : stream_has_handler_(stream_has_handler), |
89 intercepted_as_stream_(false), | 136 intercepted_as_stream_(false), |
90 intercepted_as_stream_count_(0) {} | 137 intercepted_as_stream_count_(0), |
| 138 last_resource_handler_(nullptr), |
| 139 use_buffer_(use_buffer) {} |
91 | 140 |
92 bool intercepted_as_stream() const { return intercepted_as_stream_; } | 141 bool intercepted_as_stream() const { return intercepted_as_stream_; } |
93 | 142 |
94 std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload( | 143 std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload( |
95 net::URLRequest* request, | 144 net::URLRequest* request, |
96 bool is_content_initiated, | 145 bool is_content_initiated, |
97 bool must_download) override { | 146 bool must_download) override { |
98 return std::unique_ptr<ResourceHandler>(new TestResourceHandler); | 147 return CreateNewResourceHandler(); |
99 } | 148 } |
100 | 149 |
101 std::unique_ptr<ResourceHandler> MaybeInterceptAsStream( | 150 std::unique_ptr<ResourceHandler> MaybeInterceptAsStream( |
102 const base::FilePath& plugin_path, | 151 const base::FilePath& plugin_path, |
103 net::URLRequest* request, | 152 net::URLRequest* request, |
104 ResourceResponse* response, | 153 ResourceResponse* response, |
105 std::string* payload) override { | 154 std::string* payload) override { |
106 intercepted_as_stream_count_++; | 155 intercepted_as_stream_count_++; |
107 if (stream_has_handler_) { | 156 if (stream_has_handler_) { |
108 intercepted_as_stream_ = true; | 157 intercepted_as_stream_ = true; |
109 return std::unique_ptr<ResourceHandler>(new TestResourceHandler); | 158 return CreateNewResourceHandler(); |
110 } else { | 159 } else { |
111 return std::unique_ptr<ResourceHandler>(); | 160 return CreateNewResourceHandler(); |
112 } | 161 } |
113 } | 162 } |
114 | 163 |
115 int intercepted_as_stream_count() const { | 164 int intercepted_as_stream_count() const { |
116 return intercepted_as_stream_count_; | 165 return intercepted_as_stream_count_; |
117 } | 166 } |
118 | 167 |
| 168 TestResourceHandlerWithBuffer* last_resource_handler() const { |
| 169 return last_resource_handler_; |
| 170 } |
| 171 |
119 private: | 172 private: |
| 173 std::unique_ptr<ResourceHandler> CreateNewResourceHandler() { |
| 174 std::unique_ptr<ResourceHandler> new_resource_handler; |
| 175 if (use_buffer_) { |
| 176 new_resource_handler.reset(new TestResourceHandlerWithBuffer()); |
| 177 last_resource_handler_ = static_cast<TestResourceHandlerWithBuffer*>( |
| 178 new_resource_handler.get()); |
| 179 } else { |
| 180 new_resource_handler.reset(new TestResourceHandler()); |
| 181 } |
| 182 return new_resource_handler; |
| 183 } |
| 184 |
120 // Whether the URL request should be intercepted as a stream. | 185 // Whether the URL request should be intercepted as a stream. |
121 bool stream_has_handler_; | 186 bool stream_has_handler_; |
122 | 187 |
123 // Whether the URL request has been intercepted as a stream. | 188 // Whether the URL request has been intercepted as a stream. |
124 bool intercepted_as_stream_; | 189 bool intercepted_as_stream_; |
125 | 190 |
126 // Count of number of times MaybeInterceptAsStream function get called in a | 191 // Count of number of times MaybeInterceptAsStream function get called in a |
127 // test. | 192 // test. |
128 int intercepted_as_stream_count_; | 193 int intercepted_as_stream_count_; |
| 194 |
| 195 // The last TestResourceHandlerWithBuffer created by this |
| 196 // TestResourceDispatcherHost. |
| 197 TestResourceHandlerWithBuffer* last_resource_handler_; |
| 198 |
| 199 // Whether to create TestResourceHandlerWithBuffer instead of |
| 200 // TestResourceHandlers. |
| 201 bool use_buffer_; |
129 }; | 202 }; |
130 | 203 |
131 class TestResourceDispatcherHostDelegate | 204 class TestResourceDispatcherHostDelegate |
132 : public ResourceDispatcherHostDelegate { | 205 : public ResourceDispatcherHostDelegate { |
133 public: | 206 public: |
134 TestResourceDispatcherHostDelegate(bool must_download) | 207 TestResourceDispatcherHostDelegate(bool must_download) |
135 : must_download_(must_download) { | 208 : must_download_(must_download) {} |
136 } | |
137 | 209 |
138 bool ShouldForceDownloadResource(const GURL& url, | 210 bool ShouldForceDownloadResource(const GURL& url, |
139 const std::string& mime_type) override { | 211 const std::string& mime_type) override { |
140 return must_download_; | 212 return must_download_; |
141 } | 213 } |
142 | 214 |
143 private: | 215 private: |
144 const bool must_download_; | 216 const bool must_download_; |
145 }; | 217 }; |
146 | 218 |
147 class TestResourceController : public ResourceController { | 219 class TestResourceController : public ResourceController { |
148 public: | 220 public: |
149 void Cancel() override {} | 221 void Cancel() override {} |
150 | 222 |
151 void CancelAndIgnore() override { | 223 void CancelAndIgnore() override { NOTREACHED(); } |
152 NOTREACHED(); | |
153 } | |
154 | 224 |
155 void CancelWithError(int error_code) override { | 225 void CancelWithError(int error_code) override { NOTREACHED(); } |
156 NOTREACHED(); | |
157 } | |
158 | 226 |
159 void Resume() override { | 227 void Resume() override { NOTREACHED(); } |
160 NOTREACHED(); | |
161 } | |
162 }; | 228 }; |
163 | 229 |
164 class TestFakePluginService : public FakePluginService { | 230 class TestFakePluginService : public FakePluginService { |
165 public: | 231 public: |
166 // If |is_plugin_stale| is true, GetPluginInfo will indicate the plugins are | 232 // If |is_plugin_stale| is true, GetPluginInfo will indicate the plugins are |
167 // stale until GetPlugins is called. | 233 // stale until GetPlugins is called. |
168 TestFakePluginService(bool plugin_available, bool is_plugin_stale) | 234 TestFakePluginService(bool plugin_available, bool is_plugin_stale) |
169 : plugin_available_(plugin_available), | 235 : plugin_available_(plugin_available), |
170 is_plugin_stale_(is_plugin_stale) {} | 236 is_plugin_stale_(is_plugin_stale) {} |
171 | 237 |
(...skipping 23 matching lines...) Expand all Loading... |
195 FROM_HERE, base::Bind(callback, plugins)); | 261 FROM_HERE, base::Bind(callback, plugins)); |
196 } | 262 } |
197 | 263 |
198 private: | 264 private: |
199 const bool plugin_available_; | 265 const bool plugin_available_; |
200 bool is_plugin_stale_; | 266 bool is_plugin_stale_; |
201 | 267 |
202 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); | 268 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); |
203 }; | 269 }; |
204 | 270 |
205 class MimeTypeResourceHandlerTest : public testing::Test { | 271 class InterceptingResourceHandlerTest : public testing::Test { |
206 public: | 272 public: |
207 MimeTypeResourceHandlerTest() | 273 InterceptingResourceHandlerTest() |
208 : stream_has_handler_(false), | 274 : stream_has_handler_(false), |
209 plugin_available_(false), | 275 plugin_available_(false), |
210 plugin_stale_(false) {} | 276 plugin_stale_(false) {} |
211 | 277 |
212 void set_stream_has_handler(bool stream_has_handler) { | 278 void set_stream_has_handler(bool stream_has_handler) { |
213 stream_has_handler_ = stream_has_handler; | 279 stream_has_handler_ = stream_has_handler; |
214 } | 280 } |
215 | 281 |
216 void set_plugin_available(bool plugin_available) { | 282 void set_plugin_available(bool plugin_available) { |
217 plugin_available_ = plugin_available; | 283 plugin_available_ = plugin_available; |
218 } | 284 } |
219 | 285 |
220 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } | 286 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } |
221 | 287 |
222 bool TestStreamIsIntercepted(bool allow_download, | 288 bool TestStreamIsIntercepted(bool allow_download, |
223 bool must_download, | 289 bool must_download, |
224 ResourceType request_resource_type); | 290 ResourceType request_resource_type); |
225 | 291 |
226 std::string TestAcceptHeaderSetting(ResourceType request_resource_type); | |
227 std::string TestAcceptHeaderSettingWithURLRequest( | |
228 ResourceType request_resource_type, | |
229 net::URLRequest* request); | |
230 | |
231 private: | 292 private: |
232 // Whether the URL request should be intercepted as a stream. | 293 // Whether the URL request should be intercepted as a stream. |
233 bool stream_has_handler_; | 294 bool stream_has_handler_; |
234 bool plugin_available_; | 295 bool plugin_available_; |
235 bool plugin_stale_; | 296 bool plugin_stale_; |
236 | 297 |
237 TestBrowserThreadBundle thread_bundle_; | 298 TestBrowserThreadBundle thread_bundle_; |
238 }; | 299 }; |
239 | 300 |
240 bool MimeTypeResourceHandlerTest::TestStreamIsIntercepted( | 301 bool InterceptingResourceHandlerTest::TestStreamIsIntercepted( |
241 bool allow_download, | 302 bool allow_download, |
242 bool must_download, | 303 bool must_download, |
243 ResourceType request_resource_type) { | 304 ResourceType request_resource_type) { |
244 net::URLRequestContext context; | 305 net::URLRequestContext context; |
245 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 306 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
246 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 307 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
247 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | 308 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; |
248 ResourceRequestInfo::AllocateForTesting( | 309 ResourceRequestInfo::AllocateForTesting(request.get(), request_resource_type, |
249 request.get(), | 310 nullptr, // context |
250 request_resource_type, | 311 0, // render_process_id |
251 nullptr, // context | 312 0, // render_view_id |
252 0, // render_process_id | 313 0, // render_frame_id |
253 0, // render_view_id | 314 is_main_frame, // is_main_frame |
254 0, // render_frame_id | 315 false, // parent_is_main_frame |
255 is_main_frame, // is_main_frame | 316 allow_download, // allow_download |
256 false, // parent_is_main_frame | 317 true, // is_async |
257 allow_download, // allow_download | 318 false); // is_using_lofi |
258 true, // is_async | |
259 false); // is_using_lofi | |
260 | 319 |
261 TestResourceDispatcherHost host(stream_has_handler_); | 320 TestResourceDispatcherHost host(stream_has_handler_, false); |
262 TestResourceDispatcherHostDelegate host_delegate(must_download); | 321 TestResourceDispatcherHostDelegate host_delegate(must_download); |
263 host.SetDelegate(&host_delegate); | 322 host.SetDelegate(&host_delegate); |
264 | 323 |
265 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 324 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
266 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 325 std::unique_ptr<ResourceHandler> intercepting_handler( |
267 new MimeTypeResourceHandler( | 326 new InterceptingResourceHandler( |
268 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, | 327 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, |
269 &plugin_service, request.get())); | 328 &plugin_service, request.get())); |
| 329 |
270 TestResourceController resource_controller; | 330 TestResourceController resource_controller; |
271 mime_sniffing_handler->SetController(&resource_controller); | 331 intercepting_handler->SetController(&resource_controller); |
272 | 332 |
273 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 333 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
274 // The MIME type isn't important but it shouldn't be empty. | 334 // The MIME type isn't important but it shouldn't be empty. |
275 response->head.mime_type = "application/pdf"; | 335 response->head.mime_type = "application/pdf"; |
276 | 336 |
277 bool defer = false; | 337 bool defer = false; |
278 mime_sniffing_handler->OnResponseStarted(response.get(), &defer); | 338 intercepting_handler->OnResponseStarted(response.get(), &defer); |
279 | 339 |
280 content::RunAllPendingInMessageLoop(); | 340 content::RunAllPendingInMessageLoop(); |
281 EXPECT_LT(host.intercepted_as_stream_count(), 2); | 341 EXPECT_LT(host.intercepted_as_stream_count(), 2); |
282 return host.intercepted_as_stream(); | 342 return host.intercepted_as_stream(); |
283 } | 343 } |
284 | 344 |
285 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSetting( | |
286 ResourceType request_resource_type) { | |
287 net::URLRequestContext context; | |
288 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | |
289 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | |
290 return TestAcceptHeaderSettingWithURLRequest( | |
291 request_resource_type, request.get()); | |
292 } | |
293 | |
294 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest( | |
295 ResourceType request_resource_type, | |
296 net::URLRequest* request) { | |
297 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | |
298 ResourceRequestInfo::AllocateForTesting( | |
299 request, | |
300 request_resource_type, | |
301 nullptr, // context | |
302 0, // render_process_id | |
303 0, // render_view_id | |
304 0, // render_frame_id | |
305 is_main_frame, // is_main_frame | |
306 false, // parent_is_main_frame | |
307 false, // allow_download | |
308 true, // is_async | |
309 false); // is_using_lofi | |
310 | |
311 TestResourceDispatcherHost host(stream_has_handler_); | |
312 TestResourceDispatcherHostDelegate host_delegate(false); | |
313 host.SetDelegate(&host_delegate); | |
314 | |
315 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | |
316 new MimeTypeResourceHandler( | |
317 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, | |
318 nullptr, request)); | |
319 | |
320 bool defer = false; | |
321 mime_sniffing_handler->OnWillStart(request->url(), &defer); | |
322 content::RunAllPendingInMessageLoop(); | |
323 | |
324 std::string accept_header; | |
325 request->extra_request_headers().GetHeader("Accept", &accept_header); | |
326 return accept_header; | |
327 } | |
328 | |
329 // Test that the proper Accept: header is set based on the ResourceType | |
330 TEST_F(MimeTypeResourceHandlerTest, AcceptHeaders) { | |
331 EXPECT_EQ( | |
332 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | |
333 "*/*;q=0.8", | |
334 TestAcceptHeaderSetting(RESOURCE_TYPE_MAIN_FRAME)); | |
335 EXPECT_EQ( | |
336 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | |
337 "*/*;q=0.8", | |
338 TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_FRAME)); | |
339 EXPECT_EQ("text/css,*/*;q=0.1", | |
340 TestAcceptHeaderSetting(RESOURCE_TYPE_STYLESHEET)); | |
341 EXPECT_EQ("*/*", | |
342 TestAcceptHeaderSetting(RESOURCE_TYPE_SCRIPT)); | |
343 EXPECT_EQ("image/webp,image/*,*/*;q=0.8", | |
344 TestAcceptHeaderSetting(RESOURCE_TYPE_IMAGE)); | |
345 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FONT_RESOURCE)); | |
346 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_RESOURCE)); | |
347 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_OBJECT)); | |
348 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_MEDIA)); | |
349 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_WORKER)); | |
350 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SHARED_WORKER)); | |
351 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PREFETCH)); | |
352 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FAVICON)); | |
353 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_XHR)); | |
354 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PING)); | |
355 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SERVICE_WORKER)); | |
356 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_CSP_REPORT)); | |
357 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PLUGIN_RESOURCE)); | |
358 | |
359 // Ensure that if an Accept header is already set, it is not overwritten. | |
360 net::URLRequestContext context; | |
361 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | |
362 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | |
363 request->SetExtraRequestHeaderByName("Accept", "*", true); | |
364 EXPECT_EQ("*", | |
365 TestAcceptHeaderSettingWithURLRequest(RESOURCE_TYPE_XHR, request.get())); | |
366 } | |
367 | |
368 // Test that stream requests are correctly intercepted under the right | 345 // Test that stream requests are correctly intercepted under the right |
369 // circumstances. Test is not relevent when plugins are disabled. | 346 // circumstances. Test is not relevent when plugins are disabled. |
370 #if defined(ENABLE_PLUGINS) | 347 #if defined(ENABLE_PLUGINS) |
371 TEST_F(MimeTypeResourceHandlerTest, StreamHandling) { | 348 TEST_F(InterceptingResourceHandlerTest, StreamHandling) { |
372 bool allow_download; | 349 bool allow_download; |
373 bool must_download; | 350 bool must_download; |
374 ResourceType resource_type; | 351 ResourceType resource_type; |
375 | 352 |
376 // Ensure the stream is handled by MaybeInterceptAsStream in the | 353 // Ensure the stream is handled by MaybeInterceptAsStream in the |
377 // ResourceDispatcherHost. | 354 // ResourceDispatcherHost. |
378 set_stream_has_handler(true); | 355 set_stream_has_handler(true); |
379 set_plugin_available(true); | 356 set_plugin_available(true); |
380 | 357 |
381 // Main frame request with no download allowed. Stream shouldn't be | 358 // Main frame request with no download allowed. Stream shouldn't be |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 | 402 |
426 // Test the cases where the stream isn't handled by MaybeInterceptAsStream | 403 // Test the cases where the stream isn't handled by MaybeInterceptAsStream |
427 // in the ResourceDispatcherHost. | 404 // in the ResourceDispatcherHost. |
428 set_stream_has_handler(false); | 405 set_stream_has_handler(false); |
429 allow_download = false; | 406 allow_download = false; |
430 must_download = false; | 407 must_download = false; |
431 resource_type = RESOURCE_TYPE_OBJECT; | 408 resource_type = RESOURCE_TYPE_OBJECT; |
432 EXPECT_FALSE( | 409 EXPECT_FALSE( |
433 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 410 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
434 | 411 |
435 allow_download = true; | |
436 must_download = false; | |
437 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
438 EXPECT_FALSE( | |
439 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
440 | |
441 // Test the cases where the stream handled by MaybeInterceptAsStream | 412 // Test the cases where the stream handled by MaybeInterceptAsStream |
442 // with plugin not available. This is the case when intercepting streams for | 413 // with plugin not available. This is the case when intercepting streams for |
443 // the streamsPrivate extensions API. | 414 // the streamsPrivate extensions API. |
444 set_stream_has_handler(true); | 415 set_stream_has_handler(true); |
445 set_plugin_available(false); | 416 set_plugin_available(false); |
446 allow_download = false; | 417 allow_download = false; |
447 must_download = false; | 418 must_download = false; |
448 resource_type = RESOURCE_TYPE_OBJECT; | 419 resource_type = RESOURCE_TYPE_OBJECT; |
449 EXPECT_TRUE( | 420 EXPECT_TRUE( |
450 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 421 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
451 | 422 |
452 // Test the cases where the stream handled by MaybeInterceptAsStream | 423 // Test the cases where the stream handled by MaybeInterceptAsStream |
453 // with plugin not available. This is the case when intercepting streams for | 424 // with plugin not available. This is the case when intercepting streams for |
454 // the streamsPrivate extensions API with stale plugin. | 425 // the streamsPrivate extensions API with stale plugin. |
455 set_plugin_stale(true); | 426 set_plugin_stale(true); |
456 allow_download = false; | 427 allow_download = false; |
457 must_download = false; | 428 must_download = false; |
458 resource_type = RESOURCE_TYPE_OBJECT; | 429 resource_type = RESOURCE_TYPE_OBJECT; |
459 EXPECT_TRUE( | 430 EXPECT_TRUE( |
460 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 431 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
461 } | 432 } |
462 #endif | 433 #endif |
463 | 434 |
| 435 // Tests that the data received is transmitted to the newly created |
| 436 // ResourceHandler. |
| 437 TEST_F(InterceptingResourceHandlerTest, ResponseBodyHandling) { |
| 438 net::URLRequestContext context; |
| 439 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 440 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 441 ResourceRequestInfo::AllocateForTesting(request.get(), |
| 442 RESOURCE_TYPE_MAIN_FRAME, |
| 443 nullptr, // context |
| 444 0, // render_process_id |
| 445 0, // render_view_id |
| 446 0, // render_frame_id |
| 447 true, // is_main_frame |
| 448 false, // parent_is_main_frame |
| 449 true, // allow_download |
| 450 true, // is_async |
| 451 false); // is_using_lofi |
| 452 |
| 453 TestResourceDispatcherHost host(false, true); |
| 454 TestResourceDispatcherHostDelegate host_delegate(false); |
| 455 host.SetDelegate(&host_delegate); |
| 456 |
| 457 std::unique_ptr<ResourceHandler> old_handler( |
| 458 new TestResourceHandlerWithBuffer()); |
| 459 scoped_refptr<net::IOBuffer> old_buffer = |
| 460 static_cast<TestResourceHandlerWithBuffer*>(old_handler.get())->buffer(); |
| 461 TestFakePluginService plugin_service(false, false); |
| 462 std::unique_ptr<ResourceHandler> intercepting_handler( |
| 463 new InterceptingResourceHandler(std::move(old_handler), &host, |
| 464 &plugin_service, request.get())); |
| 465 |
| 466 TestResourceController resource_controller; |
| 467 intercepting_handler->SetController(&resource_controller); |
| 468 |
| 469 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 470 // The MIME type isn't important but it shouldn't be empty. |
| 471 response->head.mime_type = "application/pdf"; |
| 472 |
| 473 // Simulate the MimeSniffingResourceHandler buffering the data. |
| 474 scoped_refptr<net::IOBuffer> read_buffer; |
| 475 int buf_size = 0; |
| 476 intercepting_handler->OnWillRead(&read_buffer, &buf_size, -1); |
| 477 |
| 478 char data[] = "The data"; |
| 479 CHECK_EQ(read_buffer.get(), old_buffer.get()); |
| 480 memcpy(read_buffer->data(), data, sizeof(data)); |
| 481 |
| 482 // The response is received. A new ResourceHandler should be created to |
| 483 // handle the download. |
| 484 bool defer = false; |
| 485 intercepting_handler->OnResponseStarted(response.get(), &defer); |
| 486 EXPECT_FALSE(defer); |
| 487 TestResourceHandlerWithBuffer* new_handler = host.last_resource_handler(); |
| 488 CHECK(new_handler); |
| 489 |
| 490 // It should not have received the download data yet. |
| 491 EXPECT_FALSE(!memcmp(data, new_handler->buffer()->data(), sizeof(data))); |
| 492 |
| 493 // The read is replayed by the MimeSniffingResourceHandler. The data should |
| 494 // have been received by the new handler. |
| 495 intercepting_handler->OnReadCompleted(sizeof(data), &defer); |
| 496 EXPECT_FALSE(defer); |
| 497 EXPECT_TRUE(!memcmp(data, new_handler->buffer()->data(), sizeof(data))); |
| 498 } |
| 499 |
| 500 // Tests that 304s do not trigger a change in handlers. |
| 501 TEST_F(InterceptingResourceHandlerTest, 304Handling) { |
| 502 net::URLRequestContext context; |
| 503 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 504 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 505 ResourceRequestInfo::AllocateForTesting(request.get(), |
| 506 RESOURCE_TYPE_MAIN_FRAME, |
| 507 nullptr, // context |
| 508 0, // render_process_id |
| 509 0, // render_view_id |
| 510 0, // render_frame_id |
| 511 true, // is_main_frame |
| 512 false, // parent_is_main_frame |
| 513 true, // allow_download |
| 514 true, // is_async |
| 515 false); // is_using_lofi |
| 516 |
| 517 TestResourceDispatcherHost host(false, true); |
| 518 TestResourceDispatcherHostDelegate host_delegate(false); |
| 519 host.SetDelegate(&host_delegate); |
| 520 |
| 521 TestFakePluginService plugin_service(false, false); |
| 522 std::unique_ptr<ResourceHandler> intercepting_handler( |
| 523 new InterceptingResourceHandler( |
| 524 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, |
| 525 &plugin_service, request.get())); |
| 526 |
| 527 TestResourceController resource_controller; |
| 528 intercepting_handler->SetController(&resource_controller); |
| 529 |
| 530 // Simulate a 304 response. |
| 531 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 532 // The MIME type isn't important but it shouldn't be empty. |
| 533 response->head.mime_type = "application/pdf"; |
| 534 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); |
| 535 |
| 536 // The response is received. No new ResourceHandler should be created to |
| 537 // handle the download. |
| 538 bool defer = false; |
| 539 intercepting_handler->OnResponseStarted(response.get(), &defer); |
| 540 EXPECT_FALSE(defer); |
| 541 TestResourceHandlerWithBuffer* new_handler = host.last_resource_handler(); |
| 542 EXPECT_TRUE(!new_handler); |
| 543 } |
| 544 |
464 } // namespace | 545 } // namespace |
465 | 546 |
466 } // namespace content | 547 } // namespace content |
OLD | NEW |