| 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_sniffing_resource_handler.h" | 5 #include "content/browser/loader/mime_sniffing_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/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "content/browser/loader/intercepting_resource_handler.h" | 18 #include "content/browser/loader/intercepting_resource_handler.h" |
| 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 19 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 20 #include "content/browser/loader/test_resource_handler.h" |
| 20 #include "content/public/browser/resource_controller.h" | 21 #include "content/public/browser/resource_controller.h" |
| 21 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 22 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 22 #include "content/public/browser/resource_request_info.h" | 23 #include "content/public/browser/resource_request_info.h" |
| 23 #include "content/public/common/resource_response.h" | 24 #include "content/public/common/resource_response.h" |
| 24 #include "content/public/common/webplugininfo.h" | 25 #include "content/public/common/webplugininfo.h" |
| 25 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
| 26 #include "content/public/test/test_utils.h" | 27 #include "content/public/test/test_utils.h" |
| 27 #include "content/test/fake_plugin_service.h" | 28 #include "content/test/fake_plugin_service.h" |
| 28 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 31 #include "url/origin.h" | 32 #include "url/origin.h" |
| 32 | 33 |
| 33 namespace content { | 34 namespace content { |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 class TestResourceHandler : public ResourceHandler { | |
| 38 public: | |
| 39 TestResourceHandler(bool response_started_succeeds, | |
| 40 bool defer_on_response_started, | |
| 41 bool will_read_succeeds, | |
| 42 bool read_completed_succeeds, | |
| 43 bool defer_on_read_completed) | |
| 44 : ResourceHandler(nullptr), | |
| 45 buffer_(new net::IOBuffer(2048)), | |
| 46 response_started_succeeds_(response_started_succeeds), | |
| 47 defer_on_response_started_(defer_on_response_started), | |
| 48 will_read_succeeds_(will_read_succeeds), | |
| 49 read_completed_succeeds_(read_completed_succeeds), | |
| 50 defer_on_read_completed_(defer_on_read_completed), | |
| 51 on_will_start_called_(0), | |
| 52 on_request_redirected_called_(0), | |
| 53 on_response_started_called_(0), | |
| 54 on_will_read_called_(0), | |
| 55 on_read_completed_called_(0) {} | |
| 56 | |
| 57 void SetController(ResourceController* controller) override {} | |
| 58 | |
| 59 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
| 60 ResourceResponse* response, | |
| 61 bool* defer) override { | |
| 62 on_request_redirected_called_++; | |
| 63 NOTREACHED(); | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { | |
| 68 on_response_started_called_++; | |
| 69 if (defer_on_response_started_) | |
| 70 *defer = true; | |
| 71 return response_started_succeeds_; | |
| 72 } | |
| 73 | |
| 74 bool OnWillStart(const GURL& url, bool* defer) override { | |
| 75 on_will_start_called_++; | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
| 80 int* buf_size, | |
| 81 int min_size) override { | |
| 82 on_will_read_called_++; | |
| 83 *buf = buffer_; | |
| 84 *buf_size = 2048; | |
| 85 return will_read_succeeds_; | |
| 86 } | |
| 87 | |
| 88 bool OnReadCompleted(int bytes_read, bool* defer) override { | |
| 89 DCHECK_LT(bytes_read, 2048); | |
| 90 on_read_completed_called_++; | |
| 91 if (defer_on_read_completed_) | |
| 92 *defer = true; | |
| 93 return read_completed_succeeds_; | |
| 94 } | |
| 95 | |
| 96 void OnResponseCompleted(const net::URLRequestStatus& status, | |
| 97 bool* defer) override {} | |
| 98 | |
| 99 void OnDataDownloaded(int bytes_downloaded) override { NOTREACHED(); } | |
| 100 | |
| 101 scoped_refptr<net::IOBuffer> buffer() { return buffer_; } | |
| 102 | |
| 103 int on_will_start_called() const { return on_will_start_called_; } | |
| 104 int on_request_redirected_called() const { | |
| 105 return on_request_redirected_called_; | |
| 106 } | |
| 107 int on_response_started_called() const { return on_response_started_called_; } | |
| 108 int on_will_read_called() const { return on_will_read_called_; } | |
| 109 int on_read_completed_called() const { return on_read_completed_called_; } | |
| 110 | |
| 111 private: | |
| 112 scoped_refptr<net::IOBuffer> buffer_; | |
| 113 bool response_started_succeeds_; | |
| 114 bool defer_on_response_started_; | |
| 115 bool will_read_succeeds_; | |
| 116 bool read_completed_succeeds_; | |
| 117 bool defer_on_read_completed_; | |
| 118 | |
| 119 int on_will_start_called_; | |
| 120 int on_request_redirected_called_; | |
| 121 int on_response_started_called_; | |
| 122 int on_will_read_called_; | |
| 123 int on_read_completed_called_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); | |
| 126 }; | |
| 127 | |
| 128 class TestResourceDispatcherHostDelegate | 38 class TestResourceDispatcherHostDelegate |
| 129 : public ResourceDispatcherHostDelegate { | 39 : public ResourceDispatcherHostDelegate { |
| 130 public: | 40 public: |
| 131 explicit TestResourceDispatcherHostDelegate(bool must_download) | 41 explicit TestResourceDispatcherHostDelegate(bool must_download) |
| 132 : must_download_(must_download) {} | 42 : must_download_(must_download) {} |
| 133 | 43 |
| 134 bool ShouldForceDownloadResource(const GURL& url, | 44 bool ShouldForceDownloadResource(const GURL& url, |
| 135 const std::string& mime_type) override { | 45 const std::string& mime_type) override { |
| 136 return must_download_; | 46 return must_download_; |
| 137 } | 47 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 return intercepted_as_stream_count_; | 83 return intercepted_as_stream_count_; |
| 174 } | 84 } |
| 175 | 85 |
| 176 TestResourceHandler* new_resource_handler() const { | 86 TestResourceHandler* new_resource_handler() const { |
| 177 return new_resource_handler_; | 87 return new_resource_handler_; |
| 178 } | 88 } |
| 179 | 89 |
| 180 private: | 90 private: |
| 181 std::unique_ptr<ResourceHandler> CreateNewResourceHandler() { | 91 std::unique_ptr<ResourceHandler> CreateNewResourceHandler() { |
| 182 std::unique_ptr<TestResourceHandler> new_resource_handler( | 92 std::unique_ptr<TestResourceHandler> new_resource_handler( |
| 183 new TestResourceHandler(false, false, true, true, false)); | 93 new TestResourceHandler()); |
| 94 new_resource_handler->set_on_response_started_result(false); |
| 184 new_resource_handler_ = new_resource_handler.get(); | 95 new_resource_handler_ = new_resource_handler.get(); |
| 185 return std::move(new_resource_handler); | 96 return std::move(new_resource_handler); |
| 186 } | 97 } |
| 187 | 98 |
| 188 // Whether the URL request should be intercepted as a stream. | 99 // Whether the URL request should be intercepted as a stream. |
| 189 bool stream_has_handler_; | 100 bool stream_has_handler_; |
| 190 | 101 |
| 191 // Whether the URL request has been intercepted as a stream. | 102 // Whether the URL request has been intercepted as a stream. |
| 192 bool intercepted_as_stream_; | 103 bool intercepted_as_stream_; |
| 193 | 104 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 nullptr, // context | 245 nullptr, // context |
| 335 0, // render_process_id | 246 0, // render_process_id |
| 336 0, // render_view_id | 247 0, // render_view_id |
| 337 0, // render_frame_id | 248 0, // render_frame_id |
| 338 is_main_frame, // is_main_frame | 249 is_main_frame, // is_main_frame |
| 339 false, // parent_is_main_frame | 250 false, // parent_is_main_frame |
| 340 false, // allow_download | 251 false, // allow_download |
| 341 true, // is_async | 252 true, // is_async |
| 342 false); // is_using_lofi | 253 false); // is_using_lofi |
| 343 | 254 |
| 255 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 256 new TestResourceHandler()); |
| 257 scoped_test_handler->set_on_response_started_result(false); |
| 258 |
| 344 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 259 std::unique_ptr<ResourceHandler> mime_sniffing_handler( |
| 345 new MimeSniffingResourceHandler( | 260 new MimeSniffingResourceHandler(std::move(scoped_test_handler), nullptr, |
| 346 std::unique_ptr<ResourceHandler>( | 261 nullptr, nullptr, request, |
| 347 new TestResourceHandler(false, false, false, false, false)), | 262 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
| 348 nullptr, nullptr, nullptr, request, | |
| 349 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | |
| 350 | 263 |
| 351 bool defer = false; | 264 bool defer = false; |
| 352 mime_sniffing_handler->OnWillStart(request->url(), &defer); | 265 mime_sniffing_handler->OnWillStart(request->url(), &defer); |
| 353 content::RunAllPendingInMessageLoop(); | 266 content::RunAllPendingInMessageLoop(); |
| 354 | 267 |
| 355 std::string accept_header; | 268 std::string accept_header; |
| 356 request->extra_request_headers().GetHeader("Accept", &accept_header); | 269 request->extra_request_headers().GetHeader("Accept", &accept_header); |
| 357 return accept_header; | 270 return accept_header; |
| 358 } | 271 } |
| 359 | 272 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 374 false, // parent_is_main_frame | 287 false, // parent_is_main_frame |
| 375 allow_download, // allow_download | 288 allow_download, // allow_download |
| 376 true, // is_async | 289 true, // is_async |
| 377 false); // is_using_lofi | 290 false); // is_using_lofi |
| 378 | 291 |
| 379 TestResourceDispatcherHost host(stream_has_handler_); | 292 TestResourceDispatcherHost host(stream_has_handler_); |
| 380 TestResourceDispatcherHostDelegate host_delegate(must_download); | 293 TestResourceDispatcherHostDelegate host_delegate(must_download); |
| 381 host.SetDelegate(&host_delegate); | 294 host.SetDelegate(&host_delegate); |
| 382 | 295 |
| 383 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 296 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
| 297 |
| 384 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 298 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 385 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>( | 299 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 386 true, // response_started_succeeds | |
| 387 false, // defer_response_started | |
| 388 true, // will_read_succeeds, | |
| 389 true, // read_completed_succeeds, | |
| 390 false), // defer_read_completed | |
| 391 nullptr)); | 300 nullptr)); |
| 301 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 302 new TestResourceHandler()); |
| 303 scoped_test_handler->set_on_response_started_result(false); |
| 392 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( | 304 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( |
| 393 std::unique_ptr<ResourceHandler>( | 305 std::unique_ptr<ResourceHandler>(std::move(scoped_test_handler)), &host, |
| 394 new TestResourceHandler(false, false, false, false, false)), | 306 &plugin_service, intercepting_handler.get(), request.get(), |
| 395 &host, &plugin_service, intercepting_handler.get(), request.get(), | |
| 396 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 307 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
| 397 | 308 |
| 398 TestResourceController resource_controller; | 309 TestResourceController resource_controller; |
| 399 mime_handler->SetController(&resource_controller); | 310 mime_handler->SetController(&resource_controller); |
| 400 | 311 |
| 401 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 312 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 402 // The MIME type isn't important but it shouldn't be empty. | 313 // The MIME type isn't important but it shouldn't be empty. |
| 403 response->head.mime_type = "application/pdf"; | 314 response->head.mime_type = "application/pdf"; |
| 404 | 315 |
| 405 bool defer = false; | 316 bool defer = false; |
| 317 mime_handler->OnWillStart(request->url(), &defer); |
| 318 EXPECT_FALSE(defer); |
| 319 |
| 406 mime_handler->OnResponseStarted(response.get(), &defer); | 320 mime_handler->OnResponseStarted(response.get(), &defer); |
| 407 | 321 |
| 408 content::RunAllPendingInMessageLoop(); | 322 content::RunAllPendingInMessageLoop(); |
| 409 EXPECT_LT(host.intercepted_as_stream_count(), 2); | 323 EXPECT_LT(host.intercepted_as_stream_count(), 2); |
| 410 if (allow_download) | 324 if (allow_download) |
| 411 EXPECT_TRUE(intercepting_handler->new_handler_for_testing()); | 325 EXPECT_TRUE(intercepting_handler->new_handler_for_testing()); |
| 412 return host.intercepted_as_stream(); | 326 return host.intercepted_as_stream(); |
| 413 } | 327 } |
| 414 | 328 |
| 415 void MimeSniffingResourceHandlerTest::TestHandlerSniffing( | 329 void MimeSniffingResourceHandlerTest::TestHandlerSniffing( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 432 false, // allow_download | 346 false, // allow_download |
| 433 true, // is_async | 347 true, // is_async |
| 434 false); // is_using_lofi | 348 false); // is_using_lofi |
| 435 | 349 |
| 436 TestResourceDispatcherHost host(false); | 350 TestResourceDispatcherHost host(false); |
| 437 TestResourceDispatcherHostDelegate host_delegate(false); | 351 TestResourceDispatcherHostDelegate host_delegate(false); |
| 438 host.SetDelegate(&host_delegate); | 352 host.SetDelegate(&host_delegate); |
| 439 | 353 |
| 440 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 354 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
| 441 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 355 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 442 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>( | 356 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 443 true, // response_started_succeeds | |
| 444 false, // defer_on_response_started | |
| 445 true, // will_read_succeeds | |
| 446 true, // read_completed_succeeds | |
| 447 false), // defer_on_read_completed | |
| 448 nullptr)); | 357 nullptr)); |
| 449 std::unique_ptr<TestResourceHandler> scoped_test_handler = | 358 |
| 450 std::unique_ptr<TestResourceHandler>(new TestResourceHandler( | 359 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 451 response_started, defer_response_started, will_read, read_completed, | 360 new TestResourceHandler()); |
| 452 defer_read_completed)); | 361 scoped_test_handler->set_on_response_started_result(response_started); |
| 362 scoped_test_handler->set_defer_on_response_started(defer_response_started); |
| 363 scoped_test_handler->set_on_will_read_result(will_read); |
| 364 scoped_test_handler->set_on_read_completed_result(read_completed); |
| 365 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); |
| 453 TestResourceHandler* test_handler = scoped_test_handler.get(); | 366 TestResourceHandler* test_handler = scoped_test_handler.get(); |
| 454 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( | 367 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( |
| 455 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, | 368 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, |
| 456 &plugin_service, | 369 &plugin_service, |
| 457 intercepting_handler.get(), request.get(), | 370 intercepting_handler.get(), request.get(), |
| 458 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 371 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
| 459 | 372 |
| 460 TestResourceController resource_controller; | 373 TestResourceController resource_controller; |
| 461 mime_sniffing_handler->SetController(&resource_controller); | 374 mime_sniffing_handler->SetController(&resource_controller); |
| 462 | 375 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 false, // allow_download | 511 false, // allow_download |
| 599 true, // is_async | 512 true, // is_async |
| 600 false); // is_using_lofi | 513 false); // is_using_lofi |
| 601 | 514 |
| 602 TestResourceDispatcherHost host(false); | 515 TestResourceDispatcherHost host(false); |
| 603 TestResourceDispatcherHostDelegate host_delegate(false); | 516 TestResourceDispatcherHostDelegate host_delegate(false); |
| 604 host.SetDelegate(&host_delegate); | 517 host.SetDelegate(&host_delegate); |
| 605 | 518 |
| 606 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 519 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
| 607 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 520 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 608 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>( | 521 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 609 true, // response_started_succeeds | |
| 610 false, // defer_response_started | |
| 611 true, // will_read_succeeds, | |
| 612 true, // read_completed_succeeds, | |
| 613 false), // defer_read_completed | |
| 614 nullptr)); | 522 nullptr)); |
| 615 | 523 |
| 616 std::unique_ptr<TestResourceHandler> scoped_test_handler = | 524 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 617 std::unique_ptr<TestResourceHandler>(new TestResourceHandler( | 525 new TestResourceHandler()); |
| 618 response_started, defer_response_started, will_read, read_completed, | 526 scoped_test_handler->set_on_response_started_result(response_started); |
| 619 defer_read_completed)); | 527 scoped_test_handler->set_defer_on_response_started(defer_response_started); |
| 528 scoped_test_handler->set_on_will_read_result(will_read); |
| 529 scoped_test_handler->set_on_read_completed_result(read_completed); |
| 530 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); |
| 620 TestResourceHandler* test_handler = scoped_test_handler.get(); | 531 TestResourceHandler* test_handler = scoped_test_handler.get(); |
| 621 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( | 532 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( |
| 622 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, | 533 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, |
| 623 &plugin_service, | 534 &plugin_service, |
| 624 intercepting_handler.get(), request.get(), | 535 intercepting_handler.get(), request.get(), |
| 625 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 536 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
| 626 | 537 |
| 627 TestResourceController resource_controller; | 538 TestResourceController resource_controller; |
| 628 mime_sniffing_handler->SetController(&resource_controller); | 539 mime_sniffing_handler->SetController(&resource_controller); |
| 629 | 540 |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 true, // allow_download | 908 true, // allow_download |
| 998 true, // is_async | 909 true, // is_async |
| 999 false); // is_using_lofi | 910 false); // is_using_lofi |
| 1000 | 911 |
| 1001 TestResourceDispatcherHost host(false); | 912 TestResourceDispatcherHost host(false); |
| 1002 TestResourceDispatcherHostDelegate host_delegate(false); | 913 TestResourceDispatcherHostDelegate host_delegate(false); |
| 1003 host.SetDelegate(&host_delegate); | 914 host.SetDelegate(&host_delegate); |
| 1004 | 915 |
| 1005 TestFakePluginService plugin_service(false, false); | 916 TestFakePluginService plugin_service(false, false); |
| 1006 std::unique_ptr<ResourceHandler> intercepting_handler( | 917 std::unique_ptr<ResourceHandler> intercepting_handler( |
| 1007 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>( | 918 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 1008 true, // response_started_succeeds | |
| 1009 false, // defer_response_started | |
| 1010 true, // will_read_succeeds, | |
| 1011 true, // read_completed_succeeds, | |
| 1012 false), // defer_read_completed | |
| 1013 nullptr)); | 919 nullptr)); |
| 1014 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( | 920 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( |
| 1015 std::unique_ptr<ResourceHandler>( | 921 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, |
| 1016 new TestResourceHandler(true, false, true, true, false)), | 922 &plugin_service, |
| 1017 &host, &plugin_service, | |
| 1018 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), | 923 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), |
| 1019 request.get(), REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 924 request.get(), REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
| 1020 | 925 |
| 1021 TestResourceController resource_controller; | 926 TestResourceController resource_controller; |
| 1022 mime_handler->SetController(&resource_controller); | 927 mime_handler->SetController(&resource_controller); |
| 1023 | 928 |
| 929 // Request starts. |
| 930 bool defer = false; |
| 931 mime_handler->OnWillStart(request->url(), &defer); |
| 932 EXPECT_FALSE(defer); |
| 933 |
| 1024 // Simulate a 304 response. | 934 // Simulate a 304 response. |
| 1025 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 935 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 1026 // The MIME type isn't important but it shouldn't be empty. | 936 // The MIME type isn't important but it shouldn't be empty. |
| 1027 response->head.mime_type = "application/pdf"; | 937 response->head.mime_type = "application/pdf"; |
| 1028 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); | 938 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); |
| 1029 | 939 |
| 1030 // The response is received. No new ResourceHandler should be created to | 940 // The response is received. No new ResourceHandler should be created to |
| 1031 // handle the download. | 941 // handle the download. |
| 1032 bool defer = false; | |
| 1033 mime_handler->OnResponseStarted(response.get(), &defer); | 942 mime_handler->OnResponseStarted(response.get(), &defer); |
| 1034 EXPECT_FALSE(defer); | 943 EXPECT_FALSE(defer); |
| 1035 EXPECT_FALSE(host.new_resource_handler()); | 944 EXPECT_FALSE(host.new_resource_handler()); |
| 1036 | 945 |
| 1037 content::RunAllPendingInMessageLoop(); | 946 content::RunAllPendingInMessageLoop(); |
| 1038 } | 947 } |
| 1039 | 948 |
| 1040 TEST_F(MimeSniffingResourceHandlerTest, FetchShouldDisableMimeSniffing) { | 949 TEST_F(MimeSniffingResourceHandlerTest, FetchShouldDisableMimeSniffing) { |
| 1041 net::URLRequestContext context; | 950 net::URLRequestContext context; |
| 1042 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 951 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 1043 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 952 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 1044 ResourceRequestInfo::AllocateForTesting(request.get(), | 953 ResourceRequestInfo::AllocateForTesting(request.get(), |
| 1045 RESOURCE_TYPE_MAIN_FRAME, | 954 RESOURCE_TYPE_MAIN_FRAME, |
| 1046 nullptr, // context | 955 nullptr, // context |
| 1047 0, // render_process_id | 956 0, // render_process_id |
| 1048 0, // render_view_id | 957 0, // render_view_id |
| 1049 0, // render_frame_id | 958 0, // render_frame_id |
| 1050 true, // is_main_frame | 959 true, // is_main_frame |
| 1051 false, // parent_is_main_frame | 960 false, // parent_is_main_frame |
| 1052 false, // allow_download | 961 false, // allow_download |
| 1053 true, // is_async | 962 true, // is_async |
| 1054 false); // is_using_lofi | 963 false); // is_using_lofi |
| 1055 | 964 |
| 1056 TestResourceDispatcherHost host(false); | 965 TestResourceDispatcherHost host(false); |
| 1057 | 966 |
| 1058 TestFakePluginService plugin_service(false, false); | 967 TestFakePluginService plugin_service(false, false); |
| 1059 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 968 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 1060 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>( | 969 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 1061 true, // response_started_succeeds | |
| 1062 false, // defer_response_started | |
| 1063 true, // will_read_succeeds, | |
| 1064 true, // read_completed_succeeds, | |
| 1065 false), // defer_read_completed | |
| 1066 nullptr)); | 970 nullptr)); |
| 1067 | 971 |
| 1068 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 972 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 1069 new TestResourceHandler(false, // response_started | 973 new TestResourceHandler()); |
| 1070 false, // defer_response_started | 974 scoped_test_handler->set_on_response_started_result(false); |
| 1071 true, // will_read, | |
| 1072 true, // read_completed, | |
| 1073 false)); // defer_read_completed | |
| 1074 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 975 std::unique_ptr<ResourceHandler> mime_sniffing_handler( |
| 1075 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, | 976 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, |
| 1076 &plugin_service, | 977 &plugin_service, |
| 1077 intercepting_handler.get(), request.get(), | 978 intercepting_handler.get(), request.get(), |
| 1078 REQUEST_CONTEXT_TYPE_FETCH)); | 979 REQUEST_CONTEXT_TYPE_FETCH)); |
| 1079 | 980 |
| 1080 TestResourceController resource_controller; | 981 TestResourceController resource_controller; |
| 1081 mime_sniffing_handler->SetController(&resource_controller); | 982 mime_sniffing_handler->SetController(&resource_controller); |
| 1082 | 983 |
| 1083 bool defer = false; | 984 bool defer = false; |
| 1084 mime_sniffing_handler->OnWillStart(GURL(), &defer); | 985 mime_sniffing_handler->OnWillStart(GURL(), &defer); |
| 1085 ASSERT_FALSE(defer); | 986 ASSERT_FALSE(defer); |
| 1086 | 987 |
| 1087 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 988 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 1088 response->head.mime_type = "text/plain"; | 989 response->head.mime_type = "text/plain"; |
| 1089 | 990 |
| 1090 // |mime_sniffing_handler->OnResponseStarted| should return false because | 991 // |mime_sniffing_handler->OnResponseStarted| should return false because |
| 1091 // mime sniffing is disabled and the wrapped resource handler returns false | 992 // mime sniffing is disabled and the wrapped resource handler returns false |
| 1092 // on OnResponseStarted. | 993 // on OnResponseStarted. |
| 1093 EXPECT_FALSE( | 994 EXPECT_FALSE( |
| 1094 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); | 995 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); |
| 1095 | 996 |
| 1096 // Process all messages to ensure proper test teardown. | 997 // Process all messages to ensure proper test teardown. |
| 1097 content::RunAllPendingInMessageLoop(); | 998 content::RunAllPendingInMessageLoop(); |
| 1098 } | 999 } |
| 1099 | 1000 |
| 1100 } // namespace content | 1001 } // namespace content |
| OLD | NEW |