| 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_controller.h" | 19 #include "content/browser/loader/mock_resource_loader.h" |
| 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 21 #include "content/browser/loader/test_resource_handler.h" | 21 #include "content/browser/loader/test_resource_handler.h" |
| 22 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 22 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 23 #include "content/public/browser/resource_request_info.h" | 23 #include "content/public/browser/resource_request_info.h" |
| 24 #include "content/public/common/resource_response.h" | 24 #include "content/public/common/resource_response.h" |
| 25 #include "content/public/common/webplugininfo.h" | 25 #include "content/public/common/webplugininfo.h" |
| 26 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
| 27 #include "content/public/test/test_utils.h" | 27 #include "content/public/test/test_utils.h" |
| 28 #include "content/test/fake_plugin_service.h" | 28 #include "content/test/fake_plugin_service.h" |
| 29 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 FROM_HERE, base::Bind(callback, plugins)); | 146 FROM_HERE, base::Bind(callback, plugins)); |
| 147 } | 147 } |
| 148 | 148 |
| 149 private: | 149 private: |
| 150 const bool plugin_available_; | 150 const bool plugin_available_; |
| 151 bool is_plugin_stale_; | 151 bool is_plugin_stale_; |
| 152 | 152 |
| 153 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); | 153 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); |
| 154 }; | 154 }; |
| 155 | 155 |
| 156 class TestResourceController : public ResourceController { | |
| 157 public: | |
| 158 TestResourceController() : cancel_call_count_(0), resume_call_count_(0) {} | |
| 159 | |
| 160 void Cancel() override { cancel_call_count_++; } | |
| 161 | |
| 162 void CancelAndIgnore() override { NOTREACHED(); } | |
| 163 | |
| 164 void CancelWithError(int error_code) override { NOTREACHED(); } | |
| 165 | |
| 166 void Resume() override { resume_call_count_++; } | |
| 167 | |
| 168 int cancel_call_count() const { return cancel_call_count_; } | |
| 169 int resume_call_count() const { return resume_call_count_; } | |
| 170 | |
| 171 private: | |
| 172 int cancel_call_count_; | |
| 173 int resume_call_count_; | |
| 174 }; | |
| 175 | |
| 176 } // namespace | 156 } // namespace |
| 177 | 157 |
| 178 class MimeSniffingResourceHandlerTest : public testing::Test { | 158 class MimeSniffingResourceHandlerTest : public testing::Test { |
| 179 public: | 159 public: |
| 180 MimeSniffingResourceHandlerTest() | 160 MimeSniffingResourceHandlerTest() |
| 181 : stream_has_handler_(false), | 161 : stream_has_handler_(false), |
| 182 plugin_available_(false), | 162 plugin_available_(false), |
| 183 plugin_stale_(false) {} | 163 plugin_stale_(false) {} |
| 184 | 164 |
| 185 // Tests that the MimeSniffingHandler properly sets the accept field in the | 165 // Tests that the MimeSniffingHandler properly sets the accept field in the |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 is_main_frame, // is_main_frame | 230 is_main_frame, // is_main_frame |
| 251 false, // parent_is_main_frame | 231 false, // parent_is_main_frame |
| 252 false, // allow_download | 232 false, // allow_download |
| 253 true, // is_async | 233 true, // is_async |
| 254 false); // is_using_lofi | 234 false); // is_using_lofi |
| 255 | 235 |
| 256 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 236 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 257 new TestResourceHandler()); | 237 new TestResourceHandler()); |
| 258 scoped_test_handler->set_on_response_started_result(false); | 238 scoped_test_handler->set_on_response_started_result(false); |
| 259 | 239 |
| 260 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 240 MimeSniffingResourceHandler mime_sniffing_handler( |
| 261 new MimeSniffingResourceHandler(std::move(scoped_test_handler), nullptr, | 241 std::move(scoped_test_handler), nullptr, nullptr, nullptr, request, |
| 262 nullptr, nullptr, request, | 242 REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 263 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 243 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 264 | 244 |
| 265 bool defer = false; | 245 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 266 mime_sniffing_handler->OnWillStart(request->url(), &defer); | 246 mock_loader.OnWillStart(request->url())); |
| 267 content::RunAllPendingInMessageLoop(); | |
| 268 | 247 |
| 269 std::string accept_header; | 248 std::string accept_header; |
| 270 request->extra_request_headers().GetHeader("Accept", &accept_header); | 249 request->extra_request_headers().GetHeader("Accept", &accept_header); |
| 271 return accept_header; | 250 return accept_header; |
| 272 } | 251 } |
| 273 | 252 |
| 274 bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted( | 253 bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted( |
| 275 bool allow_download, | 254 bool allow_download, |
| 276 bool must_download, | 255 bool must_download, |
| 277 ResourceType request_resource_type) { | 256 ResourceType request_resource_type) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 295 host.SetDelegate(&host_delegate); | 274 host.SetDelegate(&host_delegate); |
| 296 | 275 |
| 297 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 276 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
| 298 | 277 |
| 299 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 278 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 300 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), | 279 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 301 nullptr)); | 280 nullptr)); |
| 302 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 281 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 303 new TestResourceHandler()); | 282 new TestResourceHandler()); |
| 304 scoped_test_handler->set_on_response_started_result(false); | 283 scoped_test_handler->set_on_response_started_result(false); |
| 305 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( | 284 MimeSniffingResourceHandler mime_sniffing_handler( |
| 306 std::unique_ptr<ResourceHandler>(std::move(scoped_test_handler)), &host, | 285 std::unique_ptr<ResourceHandler>(std::move(scoped_test_handler)), &host, |
| 307 &plugin_service, intercepting_handler.get(), request.get(), | 286 &plugin_service, intercepting_handler.get(), request.get(), |
| 308 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 287 REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 309 | 288 |
| 310 TestResourceController resource_controller; | 289 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 311 mime_handler->SetController(&resource_controller); | |
| 312 | 290 |
| 313 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 291 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 314 // The MIME type isn't important but it shouldn't be empty. | 292 // The MIME type isn't important but it shouldn't be empty. |
| 315 response->head.mime_type = "application/pdf"; | 293 response->head.mime_type = "application/pdf"; |
| 316 | 294 |
| 317 bool defer = false; | 295 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 318 mime_handler->OnWillStart(request->url(), &defer); | 296 mock_loader.OnWillStart(request->url())); |
| 319 EXPECT_FALSE(defer); | |
| 320 | 297 |
| 321 mime_handler->OnResponseStarted(response.get(), &defer); | 298 mock_loader.OnResponseStarted(std::move(response)); |
| 299 mock_loader.WaitUntilIdleOrCanceled(); |
| 300 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 301 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 322 | 302 |
| 323 content::RunAllPendingInMessageLoop(); | 303 content::RunAllPendingInMessageLoop(); |
| 324 EXPECT_LT(host.intercepted_as_stream_count(), 2); | 304 EXPECT_LT(host.intercepted_as_stream_count(), 2); |
| 325 if (allow_download) | 305 if (allow_download) |
| 326 EXPECT_TRUE(intercepting_handler->new_handler_for_testing()); | 306 EXPECT_TRUE(intercepting_handler->new_handler_for_testing()); |
| 327 return host.intercepted_as_stream(); | 307 return host.intercepted_as_stream(); |
| 328 } | 308 } |
| 329 | 309 |
| 330 void MimeSniffingResourceHandlerTest::TestHandlerSniffing( | 310 void MimeSniffingResourceHandlerTest::TestHandlerSniffing( |
| 331 bool response_started, | 311 bool response_started, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 358 nullptr)); | 338 nullptr)); |
| 359 | 339 |
| 360 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 340 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 361 new TestResourceHandler()); | 341 new TestResourceHandler()); |
| 362 scoped_test_handler->set_on_response_started_result(response_started); | 342 scoped_test_handler->set_on_response_started_result(response_started); |
| 363 scoped_test_handler->set_defer_on_response_started(defer_response_started); | 343 scoped_test_handler->set_defer_on_response_started(defer_response_started); |
| 364 scoped_test_handler->set_on_will_read_result(will_read); | 344 scoped_test_handler->set_on_will_read_result(will_read); |
| 365 scoped_test_handler->set_on_read_completed_result(read_completed); | 345 scoped_test_handler->set_on_read_completed_result(read_completed); |
| 366 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); | 346 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); |
| 367 TestResourceHandler* test_handler = scoped_test_handler.get(); | 347 TestResourceHandler* test_handler = scoped_test_handler.get(); |
| 368 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( | 348 MimeSniffingResourceHandler mime_sniffing_handler( |
| 369 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, | 349 std::move(scoped_test_handler), &host, &plugin_service, |
| 370 &plugin_service, | 350 intercepting_handler.get(), request.get(), |
| 371 intercepting_handler.get(), request.get(), | 351 REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 372 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | |
| 373 | 352 |
| 374 TestResourceController resource_controller; | 353 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 375 mime_sniffing_handler->SetController(&resource_controller); | |
| 376 | 354 |
| 377 bool defer = false; | 355 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
| 378 mime_sniffing_handler->OnWillStart(GURL(), &defer); | 356 mock_loader.OnWillStart(request->url())); |
| 379 | 357 |
| 380 // The response should be sniffed. | 358 // The response should be sniffed. |
| 381 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 359 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 382 response->head.mime_type.assign("text/plain"); | 360 response->head.mime_type.assign("text/plain"); |
| 383 | 361 |
| 384 // Simulate the response starting. The MimeSniffingHandler should start | 362 // Simulate the response starting. The MimeSniffingHandler should start |
| 385 // buffering, so the return value should always be true. | 363 // buffering, so the return value should always be true. |
| 386 EXPECT_TRUE(mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); | 364 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
| 387 EXPECT_EQ(0, resource_controller.cancel_call_count()); | 365 mock_loader.OnResponseStarted(std::move(response))); |
| 388 EXPECT_EQ(0, resource_controller.resume_call_count()); | |
| 389 EXPECT_FALSE(defer); | |
| 390 | 366 |
| 391 // Read some data to sniff the mime type. This will ask the next | 367 // Read some data to sniff the mime type. This will ask the next |
| 392 // ResourceHandler for a buffer. | 368 // ResourceHandler for a buffer. |
| 393 scoped_refptr<net::IOBuffer> read_buffer; | 369 mock_loader.OnWillRead(-1); |
| 394 int buf_size = 0; | |
| 395 EXPECT_EQ(will_read, | |
| 396 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); | |
| 397 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 398 | 370 |
| 399 if (!will_read) { | 371 if (!will_read) { |
| 372 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 373 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 374 |
| 400 EXPECT_EQ(1, test_handler->on_will_start_called()); | 375 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 401 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 376 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 402 EXPECT_EQ(0, test_handler->on_response_started_called()); | 377 EXPECT_EQ(0, test_handler->on_response_started_called()); |
| 403 EXPECT_EQ(1, test_handler->on_will_read_called()); | 378 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 404 EXPECT_EQ(0, test_handler->on_read_completed_called()); | 379 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 405 | 380 |
| 406 // Process all messages to ensure proper test teardown. | 381 // Process all messages to ensure proper test teardown. |
| 407 content::RunAllPendingInMessageLoop(); | 382 content::RunAllPendingInMessageLoop(); |
| 408 return; | 383 return; |
| 409 } | 384 } |
| 410 | 385 |
| 386 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 387 |
| 411 // Simulate an HTML page. The mime sniffer will identify the MimeType and | 388 // Simulate an HTML page. The mime sniffer will identify the MimeType and |
| 412 // proceed with replay. | 389 // proceed with replay. |
| 413 char data[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>"; | 390 const char kData[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>"; |
| 414 memcpy(read_buffer->data(), data, sizeof(data)); | 391 // Construct StringPiece manually, as the terminal null needs to be included, |
| 415 | 392 // so it's sniffed as binary (Not important that it's sniffed as binary, but |
| 416 defer = false; | 393 // this gaurantees it's sniffed as something, without waiting for more data). |
| 417 bool return_value = | 394 mock_loader.OnReadCompleted(base::StringPiece(kData, sizeof(kData))); |
| 418 mime_sniffing_handler->OnReadCompleted(sizeof(data), &defer); | |
| 419 | 395 |
| 420 // If the next handler cancels the response start, the caller of | 396 // If the next handler cancels the response start, the caller of |
| 421 // MimeSniffingHandler::OnReadCompleted should be notified immediately. | 397 // MimeSniffingHandler::OnReadCompleted should be notified immediately. |
| 422 if (!response_started) { | 398 if (!response_started) { |
| 423 EXPECT_FALSE(defer); | 399 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 424 EXPECT_EQ(response_started, return_value); | 400 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 425 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 426 | 401 |
| 427 EXPECT_EQ(1, test_handler->on_will_start_called()); | 402 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 428 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 403 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 429 EXPECT_EQ(1, test_handler->on_response_started_called()); | 404 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 430 EXPECT_EQ(1, test_handler->on_will_read_called()); | 405 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 431 EXPECT_EQ(0, test_handler->on_read_completed_called()); | 406 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 432 | 407 |
| 433 // Process all messages to ensure proper test teardown. | 408 // Process all messages to ensure proper test teardown. |
| 434 content::RunAllPendingInMessageLoop(); | 409 content::RunAllPendingInMessageLoop(); |
| 435 return; | 410 return; |
| 436 } | 411 } |
| 437 | 412 |
| 438 // The replay can be deferred both at response started and read replay | |
| 439 // stages. | |
| 440 EXPECT_EQ(defer, defer_response_started || defer_read_completed); | |
| 441 if (defer_response_started) { | 413 if (defer_response_started) { |
| 442 EXPECT_TRUE(defer); | 414 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, |
| 443 EXPECT_TRUE(return_value); | 415 mock_loader.status()); |
| 444 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, | 416 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, |
| 445 mime_sniffing_handler->state_); | 417 mime_sniffing_handler.state_); |
| 446 mime_sniffing_handler->Resume(); | 418 test_handler->Resume(); |
| 419 // MimeSniffingResourceHandler may not synchronously resume the request. |
| 420 base::RunLoop().RunUntilIdle(); |
| 447 } | 421 } |
| 448 | 422 |
| 449 // The body that was sniffed should be transmitted to the next handler. This | 423 // The body that was sniffed should be transmitted to the next handler. This |
| 450 // may cancel the request. | 424 // may cancel the request. |
| 451 if (!read_completed) { | 425 if (!read_completed) { |
| 452 if (defer_response_started) { | 426 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 453 EXPECT_EQ(1, resource_controller.cancel_call_count()); | 427 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 454 } else { | 428 |
| 455 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 456 EXPECT_FALSE(return_value); | |
| 457 } | |
| 458 // Process all messages to ensure proper test teardown. | 429 // Process all messages to ensure proper test teardown. |
| 459 content::RunAllPendingInMessageLoop(); | 430 content::RunAllPendingInMessageLoop(); |
| 460 return; | 431 return; |
| 461 } | 432 } |
| 462 | 433 |
| 463 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, | 434 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, |
| 464 mime_sniffing_handler->state_); | 435 mime_sniffing_handler.state_); |
| 465 | 436 |
| 466 // The request may be deferred by the next handler once the read is done. | 437 // The request may be deferred by the next handler once the read is done. |
| 467 if (defer_read_completed) { | 438 if (defer_read_completed) { |
| 468 EXPECT_TRUE(defer); | 439 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, |
| 469 mime_sniffing_handler->Resume(); | 440 mock_loader.status()); |
| 441 test_handler->Resume(); |
| 442 // MimeSniffingResourceHandler may not synchronously resume the request. |
| 443 base::RunLoop().RunUntilIdle(); |
| 470 } | 444 } |
| 471 | 445 |
| 446 EXPECT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 472 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, | 447 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, |
| 473 mime_sniffing_handler->state_); | 448 mime_sniffing_handler.state_); |
| 474 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 475 | |
| 476 // Even if the next handler defers the request twice, the | |
| 477 // MimeSniffingResourceHandler should only call Resume on its controller | |
| 478 // once. | |
| 479 if (defer_response_started || defer_read_completed) { | |
| 480 EXPECT_EQ(1, resource_controller.resume_call_count()); | |
| 481 } else { | |
| 482 EXPECT_EQ(0, resource_controller.resume_call_count()); | |
| 483 } | |
| 484 | 449 |
| 485 EXPECT_EQ(1, test_handler->on_will_start_called()); | 450 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 486 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 451 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 487 EXPECT_EQ(1, test_handler->on_response_started_called()); | 452 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 488 EXPECT_EQ(1, test_handler->on_will_read_called()); | 453 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 489 EXPECT_EQ(1, test_handler->on_read_completed_called()); | 454 EXPECT_EQ(1, test_handler->on_read_completed_called()); |
| 490 | 455 |
| 491 // Process all messages to ensure proper test teardown. | 456 // Process all messages to ensure proper test teardown. |
| 492 content::RunAllPendingInMessageLoop(); | 457 content::RunAllPendingInMessageLoop(); |
| 493 } | 458 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 523 nullptr)); | 488 nullptr)); |
| 524 | 489 |
| 525 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 490 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 526 new TestResourceHandler()); | 491 new TestResourceHandler()); |
| 527 scoped_test_handler->set_on_response_started_result(response_started); | 492 scoped_test_handler->set_on_response_started_result(response_started); |
| 528 scoped_test_handler->set_defer_on_response_started(defer_response_started); | 493 scoped_test_handler->set_defer_on_response_started(defer_response_started); |
| 529 scoped_test_handler->set_on_will_read_result(will_read); | 494 scoped_test_handler->set_on_will_read_result(will_read); |
| 530 scoped_test_handler->set_on_read_completed_result(read_completed); | 495 scoped_test_handler->set_on_read_completed_result(read_completed); |
| 531 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); | 496 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); |
| 532 TestResourceHandler* test_handler = scoped_test_handler.get(); | 497 TestResourceHandler* test_handler = scoped_test_handler.get(); |
| 533 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( | 498 MimeSniffingResourceHandler mime_sniffing_handler( |
| 534 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, | 499 std::move(scoped_test_handler), &host, &plugin_service, |
| 535 &plugin_service, | 500 intercepting_handler.get(), request.get(), |
| 536 intercepting_handler.get(), request.get(), | 501 REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 537 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | |
| 538 | 502 |
| 539 TestResourceController resource_controller; | 503 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 540 mime_sniffing_handler->SetController(&resource_controller); | |
| 541 | 504 |
| 542 int expected_resume_calls = 0; | 505 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
| 543 | 506 mock_loader.OnWillStart(request->url())); |
| 544 bool defer = false; | |
| 545 mime_sniffing_handler->OnWillStart(GURL(), &defer); | |
| 546 | 507 |
| 547 // The response should not be sniffed. | 508 // The response should not be sniffed. |
| 548 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 509 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 549 response->head.mime_type.assign("text/html"); | 510 response->head.mime_type.assign("text/html"); |
| 550 | 511 |
| 551 // Simulate the response starting. There should be no need for buffering, so | 512 // Simulate the response starting. There should be no need for buffering, so |
| 552 // the return value should be that of the next handler. | 513 // the return value should be that of the next handler. |
| 553 EXPECT_EQ(response_started, | 514 mock_loader.OnResponseStarted(std::move(response)); |
| 554 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); | |
| 555 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 556 | 515 |
| 557 if (!response_started) { | 516 if (!response_started) { |
| 558 EXPECT_FALSE(defer); | 517 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 518 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 559 | 519 |
| 560 EXPECT_EQ(1, test_handler->on_will_start_called()); | 520 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 561 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 521 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 562 EXPECT_EQ(1, test_handler->on_response_started_called()); | 522 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 563 EXPECT_EQ(0, test_handler->on_will_read_called()); | 523 EXPECT_EQ(0, test_handler->on_will_read_called()); |
| 564 EXPECT_EQ(0, test_handler->on_read_completed_called()); | 524 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 565 | 525 |
| 566 // Process all messages to ensure proper test teardown. | 526 // Process all messages to ensure proper test teardown. |
| 567 content::RunAllPendingInMessageLoop(); | 527 content::RunAllPendingInMessageLoop(); |
| 568 return; | 528 return; |
| 569 } | 529 } |
| 570 | 530 |
| 571 EXPECT_EQ(defer_response_started, defer); | 531 if (defer_response_started) { |
| 572 if (defer) { | 532 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, |
| 533 mock_loader.status()); |
| 573 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, | 534 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, |
| 574 mime_sniffing_handler->state_); | 535 mime_sniffing_handler.state_); |
| 575 expected_resume_calls++; | 536 test_handler->Resume(); |
| 576 mime_sniffing_handler->Resume(); | 537 // MimeSniffingResourceHandler may not synchronously resume the request. |
| 538 base::RunLoop().RunUntilIdle(); |
| 577 } | 539 } |
| 578 | 540 |
| 579 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count()); | 541 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 580 | 542 |
| 581 // The MimeSniffingResourceHandler should be acting as a pass-through | 543 // The MimeSniffingResourceHandler should be acting as a pass-through |
| 582 // ResourceHandler. | 544 // ResourceHandler. |
| 583 scoped_refptr<net::IOBuffer> read_buffer; | 545 mock_loader.OnWillRead(-1); |
| 584 int buf_size = 0; | |
| 585 EXPECT_EQ(will_read, | |
| 586 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); | |
| 587 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 588 | 546 |
| 589 if (!will_read) { | 547 if (!will_read) { |
| 548 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 549 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 550 |
| 590 EXPECT_EQ(1, test_handler->on_will_start_called()); | 551 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 591 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 552 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 592 EXPECT_EQ(1, test_handler->on_response_started_called()); | 553 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 593 EXPECT_EQ(1, test_handler->on_will_read_called()); | 554 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 594 EXPECT_EQ(0, test_handler->on_read_completed_called()); | 555 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 595 | 556 |
| 596 // Process all messages to ensure proper test teardown. | 557 // Process all messages to ensure proper test teardown. |
| 597 content::RunAllPendingInMessageLoop(); | 558 content::RunAllPendingInMessageLoop(); |
| 598 return; | 559 return; |
| 599 } | 560 } |
| 600 | 561 |
| 601 defer = false; | 562 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 602 EXPECT_EQ(read_completed, | 563 |
| 603 mime_sniffing_handler->OnReadCompleted(2000, &defer)); | 564 mock_loader.OnReadCompleted(std::string(2000, 'a')); |
| 604 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 605 | 565 |
| 606 EXPECT_EQ(1, test_handler->on_will_start_called()); | 566 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 607 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 567 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 608 EXPECT_EQ(1, test_handler->on_response_started_called()); | 568 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 609 EXPECT_EQ(1, test_handler->on_will_read_called()); | 569 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 610 EXPECT_EQ(1, test_handler->on_read_completed_called()); | 570 EXPECT_EQ(1, test_handler->on_read_completed_called()); |
| 611 | 571 |
| 612 if (!read_completed) { | 572 if (!read_completed) { |
| 613 EXPECT_FALSE(defer); | 573 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 574 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 614 | 575 |
| 615 // Process all messages to ensure proper test teardown. | 576 // Process all messages to ensure proper test teardown. |
| 616 content::RunAllPendingInMessageLoop(); | 577 content::RunAllPendingInMessageLoop(); |
| 617 return; | 578 return; |
| 618 } | 579 } |
| 619 | 580 |
| 620 EXPECT_EQ(defer_read_completed, defer); | 581 if (mock_loader.status() == MockResourceLoader::Status::CALLBACK_PENDING) { |
| 621 if (defer) { | 582 test_handler->Resume(); |
| 622 expected_resume_calls++; | 583 // MimeSniffingResourceHandler may not synchronously resume the request. |
| 623 mime_sniffing_handler->Resume(); | 584 base::RunLoop().RunUntilIdle(); |
| 624 } | 585 } |
| 625 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count()); | 586 |
| 587 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 626 | 588 |
| 627 // Process all messages to ensure proper test teardown. | 589 // Process all messages to ensure proper test teardown. |
| 628 content::RunAllPendingInMessageLoop(); | 590 content::RunAllPendingInMessageLoop(); |
| 629 } | 591 } |
| 630 | 592 |
| 631 // Test that the proper Accept: header is set based on the ResourceType | 593 // Test that the proper Accept: header is set based on the ResourceType |
| 632 TEST_F(MimeSniffingResourceHandlerTest, AcceptHeaders) { | 594 TEST_F(MimeSniffingResourceHandlerTest, AcceptHeaders) { |
| 633 EXPECT_EQ( | 595 EXPECT_EQ( |
| 634 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | 596 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," |
| 635 "*/*;q=0.8", | 597 "*/*;q=0.8", |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 911 false); // is_using_lofi | 873 false); // is_using_lofi |
| 912 | 874 |
| 913 TestResourceDispatcherHost host(false); | 875 TestResourceDispatcherHost host(false); |
| 914 TestResourceDispatcherHostDelegate host_delegate(false); | 876 TestResourceDispatcherHostDelegate host_delegate(false); |
| 915 host.SetDelegate(&host_delegate); | 877 host.SetDelegate(&host_delegate); |
| 916 | 878 |
| 917 TestFakePluginService plugin_service(false, false); | 879 TestFakePluginService plugin_service(false, false); |
| 918 std::unique_ptr<ResourceHandler> intercepting_handler( | 880 std::unique_ptr<ResourceHandler> intercepting_handler( |
| 919 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), | 881 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 920 nullptr)); | 882 nullptr)); |
| 921 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( | 883 MimeSniffingResourceHandler mime_sniffing_handler( |
| 922 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, | 884 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, |
| 923 &plugin_service, | 885 &plugin_service, |
| 924 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), | 886 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), |
| 925 request.get(), REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 887 request.get(), REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 926 | 888 |
| 927 TestResourceController resource_controller; | 889 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 928 mime_handler->SetController(&resource_controller); | |
| 929 | 890 |
| 930 // Request starts. | 891 // Request starts. |
| 931 bool defer = false; | 892 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 932 mime_handler->OnWillStart(request->url(), &defer); | 893 mock_loader.OnWillStart(request->url())); |
| 933 EXPECT_FALSE(defer); | |
| 934 | 894 |
| 935 // Simulate a 304 response. | 895 // Simulate a 304 response. |
| 936 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 896 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 937 // The MIME type isn't important but it shouldn't be empty. | 897 // The MIME type isn't important but it shouldn't be empty. |
| 938 response->head.mime_type = "application/pdf"; | 898 response->head.mime_type = "application/pdf"; |
| 939 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); | 899 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); |
| 940 | 900 |
| 941 // The response is received. No new ResourceHandler should be created to | 901 // The response is received. No new ResourceHandler should be created to |
| 942 // handle the download. | 902 // handle the download. |
| 943 mime_handler->OnResponseStarted(response.get(), &defer); | 903 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 944 EXPECT_FALSE(defer); | 904 mock_loader.OnResponseStarted(std::move(response))); |
| 945 EXPECT_FALSE(host.new_resource_handler()); | 905 EXPECT_FALSE(host.new_resource_handler()); |
| 946 | 906 |
| 947 content::RunAllPendingInMessageLoop(); | 907 content::RunAllPendingInMessageLoop(); |
| 948 } | 908 } |
| 949 | 909 |
| 950 TEST_F(MimeSniffingResourceHandlerTest, FetchShouldDisableMimeSniffing) { | 910 TEST_F(MimeSniffingResourceHandlerTest, FetchShouldDisableMimeSniffing) { |
| 951 net::URLRequestContext context; | 911 net::URLRequestContext context; |
| 952 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 912 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 953 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 913 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 954 ResourceRequestInfo::AllocateForTesting(request.get(), | 914 ResourceRequestInfo::AllocateForTesting(request.get(), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 966 TestResourceDispatcherHost host(false); | 926 TestResourceDispatcherHost host(false); |
| 967 | 927 |
| 968 TestFakePluginService plugin_service(false, false); | 928 TestFakePluginService plugin_service(false, false); |
| 969 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 929 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 970 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), | 930 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 971 nullptr)); | 931 nullptr)); |
| 972 | 932 |
| 973 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 933 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 974 new TestResourceHandler()); | 934 new TestResourceHandler()); |
| 975 scoped_test_handler->set_on_response_started_result(false); | 935 scoped_test_handler->set_on_response_started_result(false); |
| 976 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 936 MimeSniffingResourceHandler mime_sniffing_handler( |
| 977 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, | 937 std::move(scoped_test_handler), &host, &plugin_service, |
| 978 &plugin_service, | 938 intercepting_handler.get(), request.get(), REQUEST_CONTEXT_TYPE_FETCH); |
| 979 intercepting_handler.get(), request.get(), | |
| 980 REQUEST_CONTEXT_TYPE_FETCH)); | |
| 981 | 939 |
| 982 TestResourceController resource_controller; | 940 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 983 mime_sniffing_handler->SetController(&resource_controller); | |
| 984 | 941 |
| 985 bool defer = false; | 942 // Request starts. |
| 986 mime_sniffing_handler->OnWillStart(GURL(), &defer); | 943 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 987 ASSERT_FALSE(defer); | 944 mock_loader.OnWillStart(request->url())); |
| 988 | 945 |
| 989 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 946 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 990 response->head.mime_type = "text/plain"; | 947 response->head.mime_type = "text/plain"; |
| 991 | 948 |
| 992 // |mime_sniffing_handler->OnResponseStarted| should return false because | 949 // |mime_sniffing_handler->OnResponseStarted| should return false because |
| 993 // mime sniffing is disabled and the wrapped resource handler returns false | 950 // mime sniffing is disabled and the wrapped resource handler returns false |
| 994 // on OnResponseStarted. | 951 // on OnResponseStarted. |
| 995 EXPECT_FALSE( | 952 EXPECT_EQ(MockResourceLoader::Status::CANCELED, |
| 996 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); | 953 mock_loader.OnResponseStarted(std::move(response))); |
| 954 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 997 | 955 |
| 998 // Process all messages to ensure proper test teardown. | 956 // Process all messages to ensure proper test teardown. |
| 999 content::RunAllPendingInMessageLoop(); | 957 content::RunAllPendingInMessageLoop(); |
| 1000 } | 958 } |
| 1001 | 959 |
| 1002 } // namespace content | 960 } // namespace content |
| OLD | NEW |