Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/mime_type_resource_handler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 bool* defer) override { | 38 bool* defer) override { |
| 39 NOTREACHED(); | 39 NOTREACHED(); |
| 40 return false; | 40 return false; |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { | 43 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { |
| 44 return false; | 44 return false; |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool OnWillStart(const GURL& url, bool* defer) override { | 47 bool OnWillStart(const GURL& url, bool* defer) override { |
| 48 NOTREACHED(); | |
| 49 return false; | 48 return false; |
| 50 } | 49 } |
| 51 | 50 |
| 52 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { | 51 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { |
| 53 NOTREACHED(); | 52 NOTREACHED(); |
| 54 return false; | 53 return false; |
| 55 } | 54 } |
| 56 | 55 |
| 57 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 56 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 58 int* buf_size, | 57 int* buf_size, |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 void set_plugin_available(bool plugin_available) { | 212 void set_plugin_available(bool plugin_available) { |
| 214 plugin_available_ = plugin_available; | 213 plugin_available_ = plugin_available; |
| 215 } | 214 } |
| 216 | 215 |
| 217 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } | 216 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } |
| 218 | 217 |
| 219 bool TestStreamIsIntercepted(bool allow_download, | 218 bool TestStreamIsIntercepted(bool allow_download, |
| 220 bool must_download, | 219 bool must_download, |
| 221 ResourceType request_resource_type); | 220 ResourceType request_resource_type); |
| 222 | 221 |
| 222 std::string TestAcceptHeaderSetting(ResourceType request_resource_type); | |
| 223 std::string TestAcceptHeaderSettingWithURLRequest( | |
| 224 ResourceType request_resource_type, | |
| 225 net::URLRequest* request); | |
| 226 | |
| 223 private: | 227 private: |
| 224 // Whether the URL request should be intercepted as a stream. | 228 // Whether the URL request should be intercepted as a stream. |
| 225 bool stream_has_handler_; | 229 bool stream_has_handler_; |
| 226 bool plugin_available_; | 230 bool plugin_available_; |
| 227 bool plugin_stale_; | 231 bool plugin_stale_; |
| 228 | 232 |
| 229 TestBrowserThreadBundle thread_bundle_; | 233 TestBrowserThreadBundle thread_bundle_; |
| 230 }; | 234 }; |
| 231 | 235 |
| 232 bool MimeTypeResourceHandlerTest::TestStreamIsIntercepted( | 236 bool MimeTypeResourceHandlerTest::TestStreamIsIntercepted( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 response->head.mime_type = "application/pdf"; | 270 response->head.mime_type = "application/pdf"; |
| 267 | 271 |
| 268 bool defer = false; | 272 bool defer = false; |
| 269 mime_sniffing_handler->OnResponseStarted(response.get(), &defer); | 273 mime_sniffing_handler->OnResponseStarted(response.get(), &defer); |
| 270 | 274 |
| 271 content::RunAllPendingInMessageLoop(); | 275 content::RunAllPendingInMessageLoop(); |
| 272 EXPECT_LT(host.intercepted_as_stream_count(), 2); | 276 EXPECT_LT(host.intercepted_as_stream_count(), 2); |
| 273 return host.intercepted_as_stream(); | 277 return host.intercepted_as_stream(); |
| 274 } | 278 } |
| 275 | 279 |
| 280 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSetting( | |
| 281 ResourceType request_resource_type) { | |
| 282 net::URLRequestContext context; | |
| 283 GURL url("http://www.google.com"); | |
|
mmenke
2016/04/04 19:08:58
Not used.
Nate Chapin
2016/04/04 23:21:57
Done.
| |
| 284 scoped_ptr<net::URLRequest> request(context.CreateRequest( | |
| 285 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | |
| 286 return TestAcceptHeaderSettingWithURLRequest( | |
| 287 request_resource_type, request.get()); | |
| 288 } | |
| 289 | |
| 290 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest( | |
| 291 ResourceType request_resource_type, | |
| 292 net::URLRequest* request) { | |
| 293 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | |
| 294 ResourceRequestInfo::AllocateForTesting( | |
| 295 request, | |
| 296 request_resource_type, | |
| 297 nullptr, // context | |
| 298 0, // render_process_id | |
| 299 0, // render_view_id | |
| 300 0, // render_frame_id | |
| 301 is_main_frame, // is_main_frame | |
| 302 false, // parent_is_main_frame | |
| 303 false, // allow_download | |
| 304 true, // is_async | |
| 305 false); // is_using_lofi | |
| 306 | |
| 307 TestResourceDispatcherHost host(stream_has_handler_); | |
| 308 TestResourceDispatcherHostDelegate host_delegate(false); | |
| 309 host.SetDelegate(&host_delegate); | |
| 310 | |
| 311 scoped_ptr<ResourceHandler> mime_sniffing_handler(new MimeTypeResourceHandler( | |
| 312 scoped_ptr<ResourceHandler>(new TestResourceHandler()), &host, | |
| 313 nullptr, request)); | |
| 314 | |
| 315 bool defer = false; | |
| 316 mime_sniffing_handler->OnWillStart(request->url(), &defer); | |
| 317 content::RunAllPendingInMessageLoop(); | |
| 318 | |
| 319 std::string accept_header; | |
| 320 request->extra_request_headers().GetHeader("Accept", &accept_header); | |
| 321 return accept_header; | |
| 322 } | |
| 323 | |
| 324 // Test that the proper Accept: header is set based on the ResourceType | |
| 325 TEST_F(MimeTypeResourceHandlerTest, AcceptHeaders) { | |
| 326 EXPECT_EQ( | |
| 327 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | |
| 328 "*/*;q=0.8", | |
| 329 TestAcceptHeaderSetting(RESOURCE_TYPE_MAIN_FRAME)); | |
| 330 EXPECT_EQ( | |
| 331 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | |
| 332 "*/*;q=0.8", | |
| 333 TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_FRAME)); | |
| 334 EXPECT_EQ("text/css,*/*;q=0.1", | |
| 335 TestAcceptHeaderSetting(RESOURCE_TYPE_STYLESHEET)); | |
| 336 EXPECT_EQ("*/*", | |
| 337 TestAcceptHeaderSetting(RESOURCE_TYPE_SCRIPT)); | |
| 338 EXPECT_EQ("image/webp,image/*,*/*;q=0.8", | |
| 339 TestAcceptHeaderSetting(RESOURCE_TYPE_IMAGE)); | |
| 340 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FONT_RESOURCE)); | |
| 341 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_RESOURCE)); | |
| 342 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_OBJECT)); | |
| 343 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_MEDIA)); | |
| 344 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_WORKER)); | |
| 345 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SHARED_WORKER)); | |
| 346 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PREFETCH)); | |
| 347 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FAVICON)); | |
| 348 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_XHR)); | |
| 349 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PING)); | |
| 350 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SERVICE_WORKER)); | |
| 351 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_CSP_REPORT)); | |
| 352 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PLUGIN_RESOURCE)); | |
| 353 | |
| 354 // Ensure that if an Accept header is already set, it is not overwritten. | |
| 355 net::URLRequestContext context; | |
| 356 GURL url("http://www.google.com"); | |
|
mmenke
2016/04/04 19:08:58
Not used.
Nate Chapin
2016/04/04 23:21:57
Done.
| |
| 357 scoped_ptr<net::URLRequest> request(context.CreateRequest( | |
| 358 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | |
| 359 request->SetExtraRequestHeaderByName("Accept", "*", true); | |
| 360 EXPECT_EQ("*", | |
| 361 TestAcceptHeaderSettingWithURLRequest(RESOURCE_TYPE_XHR, request.get())); | |
| 362 } | |
| 363 | |
| 276 // Test that stream requests are correctly intercepted under the right | 364 // Test that stream requests are correctly intercepted under the right |
| 277 // circumstances. Test is not relevent when plugins are disabled. | 365 // circumstances. Test is not relevent when plugins are disabled. |
| 278 #if defined(ENABLE_PLUGINS) | 366 #if defined(ENABLE_PLUGINS) |
| 279 TEST_F(MimeTypeResourceHandlerTest, StreamHandling) { | 367 TEST_F(MimeTypeResourceHandlerTest, StreamHandling) { |
| 280 bool allow_download; | 368 bool allow_download; |
| 281 bool must_download; | 369 bool must_download; |
| 282 ResourceType resource_type; | 370 ResourceType resource_type; |
| 283 | 371 |
| 284 // Ensure the stream is handled by MaybeInterceptAsStream in the | 372 // Ensure the stream is handled by MaybeInterceptAsStream in the |
| 285 // ResourceDispatcherHost. | 373 // ResourceDispatcherHost. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 must_download = false; | 453 must_download = false; |
| 366 resource_type = RESOURCE_TYPE_OBJECT; | 454 resource_type = RESOURCE_TYPE_OBJECT; |
| 367 EXPECT_TRUE( | 455 EXPECT_TRUE( |
| 368 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 456 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 369 } | 457 } |
| 370 #endif | 458 #endif |
| 371 | 459 |
| 372 } // namespace | 460 } // namespace |
| 373 | 461 |
| 374 } // namespace content | 462 } // namespace content |
| OLD | NEW |