| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <queue> | 10 #include <queue> |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 using helpers::MergeOnBeforeRequestResponses; | 84 using helpers::MergeOnBeforeRequestResponses; |
| 85 using helpers::RequestCookieModification; | 85 using helpers::RequestCookieModification; |
| 86 using helpers::ResponseCookieModification; | 86 using helpers::ResponseCookieModification; |
| 87 using helpers::ResponseHeader; | 87 using helpers::ResponseHeader; |
| 88 using helpers::ResponseHeaders; | 88 using helpers::ResponseHeaders; |
| 89 using helpers::StringToCharList; | 89 using helpers::StringToCharList; |
| 90 | 90 |
| 91 namespace extensions { | 91 namespace extensions { |
| 92 | 92 |
| 93 namespace { | 93 namespace { |
| 94 |
| 94 static void EventHandledOnIOThread( | 95 static void EventHandledOnIOThread( |
| 95 void* profile, | 96 void* profile, |
| 96 const std::string& extension_id, | 97 const std::string& extension_id, |
| 97 const std::string& event_name, | 98 const std::string& event_name, |
| 98 const std::string& sub_event_name, | 99 const std::string& sub_event_name, |
| 99 uint64_t request_id, | 100 uint64_t request_id, |
| 100 ExtensionWebRequestEventRouter::EventResponse* response) { | 101 ExtensionWebRequestEventRouter::EventResponse* response) { |
| 101 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( | 102 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( |
| 102 profile, extension_id, event_name, sub_event_name, request_id, | 103 profile, extension_id, event_name, sub_event_name, request_id, |
| 103 response); | 104 response); |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 base::RunLoop().Run(); | 349 base::RunLoop().Run(); |
| 349 | 350 |
| 350 EXPECT_TRUE(!request2->is_pending()); | 351 EXPECT_TRUE(!request2->is_pending()); |
| 351 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request2->status().status()); | 352 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request2->status().status()); |
| 352 EXPECT_EQ(0, request2->status().error()); | 353 EXPECT_EQ(0, request2->status().error()); |
| 353 EXPECT_EQ(redirect_url, request2->url()); | 354 EXPECT_EQ(redirect_url, request2->url()); |
| 354 EXPECT_EQ(2U, request2->url_chain().size()); | 355 EXPECT_EQ(2U, request2->url_chain().size()); |
| 355 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 356 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 356 } | 357 } |
| 357 | 358 |
| 358 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 359 ExtensionWebRequestEventRouter::EventListener::Identifier id1( |
| 359 &profile_, extension1_id, kEventName + "/1", 0, 0); | 360 &profile_, extension1_id, kEventName + "/1", 0, 0); |
| 360 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 361 ExtensionWebRequestEventRouter::EventListener::Identifier id2( |
| 361 &profile_, extension2_id, kEventName + "/2", 0, 0); | 362 &profile_, extension2_id, kEventName + "/2", 0, 0); |
| 363 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 364 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2); |
| 362 } | 365 } |
| 363 | 366 |
| 364 // Test that a request is canceled if this is requested by any extension | 367 // Test that a request is canceled if this is requested by any extension |
| 365 // regardless whether it is the extension with the highest precedence. | 368 // regardless whether it is the extension with the highest precedence. |
| 366 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedenceCancel) { | 369 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedenceCancel) { |
| 367 std::string extension1_id("1"); | 370 std::string extension1_id("1"); |
| 368 std::string extension2_id("2"); | 371 std::string extension2_id("2"); |
| 369 ExtensionWebRequestEventRouter::RequestFilter filter; | 372 ExtensionWebRequestEventRouter::RequestFilter filter; |
| 370 const std::string kEventName(web_request::OnBeforeRequest::kEventName); | 373 const std::string kEventName(web_request::OnBeforeRequest::kEventName); |
| 371 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); | 374 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 | 415 |
| 413 base::RunLoop().Run(); | 416 base::RunLoop().Run(); |
| 414 | 417 |
| 415 EXPECT_TRUE(!request->is_pending()); | 418 EXPECT_TRUE(!request->is_pending()); |
| 416 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 419 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
| 417 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request->status().error()); | 420 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request->status().error()); |
| 418 EXPECT_EQ(request_url, request->url()); | 421 EXPECT_EQ(request_url, request->url()); |
| 419 EXPECT_EQ(1U, request->url_chain().size()); | 422 EXPECT_EQ(1U, request->url_chain().size()); |
| 420 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 423 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 421 | 424 |
| 422 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 425 ExtensionWebRequestEventRouter::EventListener::Identifier id1( |
| 423 &profile_, extension1_id, kEventName + "/1", 0, 0); | 426 &profile_, extension1_id, kEventName + "/1", 0, 0); |
| 424 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 427 ExtensionWebRequestEventRouter::EventListener::Identifier id2( |
| 425 &profile_, extension2_id, kEventName + "/2", 0, 0); | 428 &profile_, extension2_id, kEventName + "/2", 0, 0); |
| 429 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 430 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2); |
| 426 } | 431 } |
| 427 | 432 |
| 428 TEST_F(ExtensionWebRequestTest, SimulateChancelWhileBlocked) { | 433 TEST_F(ExtensionWebRequestTest, SimulateChancelWhileBlocked) { |
| 429 // We subscribe to OnBeforeRequest and OnErrorOccurred. | 434 // We subscribe to OnBeforeRequest and OnErrorOccurred. |
| 430 // While the OnBeforeRequest handler is blocked, we cancel the request. | 435 // While the OnBeforeRequest handler is blocked, we cancel the request. |
| 431 // We verify that the response of the blocked OnBeforeRequest handler | 436 // We verify that the response of the blocked OnBeforeRequest handler |
| 432 // is ignored. | 437 // is ignored. |
| 433 | 438 |
| 434 std::string extension_id("1"); | 439 std::string extension_id("1"); |
| 435 ExtensionWebRequestEventRouter::RequestFilter filter; | 440 ExtensionWebRequestEventRouter::RequestFilter filter; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 request->Cancel(); | 481 request->Cancel(); |
| 477 run_loop.Run(); | 482 run_loop.Run(); |
| 478 | 483 |
| 479 EXPECT_TRUE(!request->is_pending()); | 484 EXPECT_TRUE(!request->is_pending()); |
| 480 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status()); | 485 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status()); |
| 481 EXPECT_EQ(net::ERR_ABORTED, request->status().error()); | 486 EXPECT_EQ(net::ERR_ABORTED, request->status().error()); |
| 482 EXPECT_EQ(request_url, request->url()); | 487 EXPECT_EQ(request_url, request->url()); |
| 483 EXPECT_EQ(1U, request->url_chain().size()); | 488 EXPECT_EQ(1U, request->url_chain().size()); |
| 484 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 489 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 485 | 490 |
| 486 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 491 ExtensionWebRequestEventRouter::EventListener::Identifier id1( |
| 487 &profile_, extension_id, kEventName + "/1", 0, 0); | 492 &profile_, extension_id, kEventName + "/1", 0, 0); |
| 488 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 493 ExtensionWebRequestEventRouter::EventListener::Identifier id2( |
| 489 &profile_, extension_id, kEventName2 + "/1", 0, 0); | 494 &profile_, extension_id, kEventName2 + "/1", 0, 0); |
| 495 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 496 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2); |
| 490 } | 497 } |
| 491 | 498 |
| 492 namespace { | 499 namespace { |
| 493 | 500 |
| 494 // Create the numerical representation of |values|, strings passed as | 501 // Create the numerical representation of |values|, strings passed as |
| 495 // extraInfoSpec by the event handler. Returns true on success, otherwise false. | 502 // extraInfoSpec by the event handler. Returns true on success, otherwise false. |
| 496 bool GenerateInfoSpec(const std::string& values, int* result) { | 503 bool GenerateInfoSpec(const std::string& values, int* result) { |
| 497 // Create a base::ListValue of strings. | 504 // Create a base::ListValue of strings. |
| 498 base::ListValue list_value; | 505 base::ListValue list_value; |
| 499 for (const std::string& cur : | 506 for (const std::string& cur : |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 643 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 637 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, | 644 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, |
| 638 kEventName + "/1", filter, extra_info_spec_body, 0, 0, | 645 kEventName + "/1", filter, extra_info_spec_body, 0, 0, |
| 639 ipc_sender_factory.GetWeakPtr()); | 646 ipc_sender_factory.GetWeakPtr()); |
| 640 | 647 |
| 641 FireURLRequestWithData(kMethodPost, kMultipart, form_1, form_2); | 648 FireURLRequestWithData(kMethodPost, kMultipart, form_1, form_2); |
| 642 | 649 |
| 643 // We inspect the result in the message list of |ipc_sender_| later. | 650 // We inspect the result in the message list of |ipc_sender_| later. |
| 644 base::RunLoop().RunUntilIdle(); | 651 base::RunLoop().RunUntilIdle(); |
| 645 | 652 |
| 646 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 653 ExtensionWebRequestEventRouter::EventListener::Identifier id1( |
| 647 &profile_, extension_id, kEventName + "/1", 0, 0); | 654 &profile_, extension_id, kEventName + "/1", 0, 0); |
| 655 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 648 | 656 |
| 649 // Part 2. | 657 // Part 2. |
| 650 // Now subscribe to OnBeforeRequest *without* the requestBody requirement. | 658 // Now subscribe to OnBeforeRequest *without* the requestBody requirement. |
| 651 ASSERT_TRUE( | 659 ASSERT_TRUE( |
| 652 GenerateInfoSpec(string_spec_no_post, &extra_info_spec_empty)); | 660 GenerateInfoSpec(string_spec_no_post, &extra_info_spec_empty)); |
| 653 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 661 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 654 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, | 662 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, |
| 655 kEventName + "/1", filter, extra_info_spec_empty, 0, 0, | 663 kEventName + "/1", filter, extra_info_spec_empty, 0, 0, |
| 656 ipc_sender_factory.GetWeakPtr()); | 664 ipc_sender_factory.GetWeakPtr()); |
| 657 | 665 |
| 658 FireURLRequestWithData(kMethodPost, kMultipart, form_1, form_2); | 666 FireURLRequestWithData(kMethodPost, kMultipart, form_1, form_2); |
| 659 | 667 |
| 660 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 668 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 661 &profile_, extension_id, kEventName + "/1", 0, 0); | |
| 662 | 669 |
| 663 // Subscribe to OnBeforeRequest with requestBody requirement. | 670 // Subscribe to OnBeforeRequest with requestBody requirement. |
| 664 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 671 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 665 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, | 672 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, |
| 666 kEventName + "/1", filter, extra_info_spec_body, 0, 0, | 673 kEventName + "/1", filter, extra_info_spec_body, 0, 0, |
| 667 ipc_sender_factory.GetWeakPtr()); | 674 ipc_sender_factory.GetWeakPtr()); |
| 668 | 675 |
| 669 // Part 3. | 676 // Part 3. |
| 670 // Now send a POST request with body which is not parseable as a form. | 677 // Now send a POST request with body which is not parseable as a form. |
| 671 FireURLRequestWithData(kMethodPost, NULL /*no header*/, plain_1, plain_2); | 678 FireURLRequestWithData(kMethodPost, NULL /*no header*/, plain_1, plain_2); |
| 672 | 679 |
| 673 // Part 4. | 680 // Part 4. |
| 674 // Now send a PUT request with the same body as above. | 681 // Now send a PUT request with the same body as above. |
| 675 FireURLRequestWithData(kMethodPut, NULL /*no header*/, plain_1, plain_2); | 682 FireURLRequestWithData(kMethodPut, NULL /*no header*/, plain_1, plain_2); |
| 676 | 683 |
| 677 base::RunLoop().RunUntilIdle(); | 684 base::RunLoop().RunUntilIdle(); |
| 678 | 685 |
| 679 // Clean-up. | 686 // Clean-up. |
| 680 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 687 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 681 &profile_, extension_id, kEventName + "/1", 0, 0); | |
| 682 | 688 |
| 683 IPC::Message* message = NULL; | 689 IPC::Message* message = NULL; |
| 684 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); | 690 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); |
| 685 for (size_t test = 0; test < arraysize(kExpected); ++test) { | 691 for (size_t test = 0; test < arraysize(kExpected); ++test) { |
| 686 SCOPED_TRACE(testing::Message("iteration number ") << test); | 692 SCOPED_TRACE(testing::Message("iteration number ") << test); |
| 687 EXPECT_NE(i, ipc_sender_.sent_end()); | 693 EXPECT_NE(i, ipc_sender_.sent_end()); |
| 688 message = (i++)->get(); | 694 message = (i++)->get(); |
| 689 const base::DictionaryValue* details; | 695 const base::DictionaryValue* details; |
| 690 ExtensionMsg_MessageInvoke::Param param; | 696 ExtensionMsg_MessageInvoke::Param param; |
| 691 GetPartOfMessageArguments(message, &details, ¶m); | 697 GetPartOfMessageArguments(message, &details, ¶m); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 // Only one request is sent, but more than one event will be triggered. | 753 // Only one request is sent, but more than one event will be triggered. |
| 748 for (size_t i = 1; i < arraysize(kExpected); ++i) | 754 for (size_t i = 1; i < arraysize(kExpected); ++i) |
| 749 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); | 755 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); |
| 750 | 756 |
| 751 const std::vector<char> part_of_body(1); | 757 const std::vector<char> part_of_body(1); |
| 752 FireURLRequestWithData("POST", nullptr, part_of_body, part_of_body); | 758 FireURLRequestWithData("POST", nullptr, part_of_body, part_of_body); |
| 753 | 759 |
| 754 base::RunLoop().RunUntilIdle(); | 760 base::RunLoop().RunUntilIdle(); |
| 755 | 761 |
| 756 // Clean-up | 762 // Clean-up |
| 757 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 763 ExtensionWebRequestEventRouter::EventListener::Identifier id1( |
| 758 &profile_, extension_id1, kEventName + "/1", 0, 0); | 764 &profile_, extension_id1, kEventName + "/1", 0, 0); |
| 759 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 765 ExtensionWebRequestEventRouter::EventListener::Identifier id2( |
| 766 &profile_, extension_id1, kEventName + "/2", 0, 0); |
| 767 ExtensionWebRequestEventRouter::EventListener::Identifier id3( |
| 768 &profile_, extension_id2, kEventName + "/1", 0, 0); |
| 769 ExtensionWebRequestEventRouter::EventListener::Identifier id4( |
| 760 &profile_, extension_id2, kEventName + "/2", 0, 0); | 770 &profile_, extension_id2, kEventName + "/2", 0, 0); |
| 761 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 771 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 762 &profile_, extension_id1, kEventName + "/1", 0, 0); | 772 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2); |
| 763 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 773 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id3); |
| 764 &profile_, extension_id2, kEventName + "/2", 0, 0); | 774 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id4); |
| 765 | 775 |
| 766 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); | 776 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); |
| 767 | 777 |
| 768 for (size_t test = 0; test < arraysize(kExpected); ++test, ++i) { | 778 for (size_t test = 0; test < arraysize(kExpected); ++test, ++i) { |
| 769 SCOPED_TRACE(testing::Message("iteration number ") << test); | 779 SCOPED_TRACE(testing::Message("iteration number ") << test); |
| 770 EXPECT_NE(i, ipc_sender_.sent_end()); | 780 EXPECT_NE(i, ipc_sender_.sent_end()); |
| 771 IPC::Message* message = i->get(); | 781 IPC::Message* message = i->get(); |
| 772 const base::DictionaryValue* details = nullptr; | 782 const base::DictionaryValue* details = nullptr; |
| 773 ExtensionMsg_MessageInvoke::Param param; | 783 ExtensionMsg_MessageInvoke::Param param; |
| 774 GetPartOfMessageArguments(message, &details, ¶m); | 784 GetPartOfMessageArguments(message, &details, ¶m); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 std::unique_ptr<net::URLRequest> request(context_->CreateRequest( | 819 std::unique_ptr<net::URLRequest> request(context_->CreateRequest( |
| 810 request_url, net::DEFAULT_PRIORITY, &delegate_)); | 820 request_url, net::DEFAULT_PRIORITY, &delegate_)); |
| 811 request->set_method(kMethods[i]); | 821 request->set_method(kMethods[i]); |
| 812 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); | 822 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); |
| 813 request->Start(); | 823 request->Start(); |
| 814 } | 824 } |
| 815 | 825 |
| 816 // We inspect the result in the message list of |ipc_sender_| later. | 826 // We inspect the result in the message list of |ipc_sender_| later. |
| 817 base::RunLoop().RunUntilIdle(); | 827 base::RunLoop().RunUntilIdle(); |
| 818 | 828 |
| 819 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 829 ExtensionWebRequestEventRouter::EventListener::Identifier id1( |
| 820 &profile_, extension_id, kEventName + "/1", 0, 0); | 830 &profile_, extension_id, kEventName + "/1", 0, 0); |
| 831 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 821 | 832 |
| 822 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); | 833 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); |
| 823 for (size_t test = 0; test < arraysize(kMethods); ++test, ++i) { | 834 for (size_t test = 0; test < arraysize(kMethods); ++test, ++i) { |
| 824 SCOPED_TRACE(testing::Message("iteration number ") << test); | 835 SCOPED_TRACE(testing::Message("iteration number ") << test); |
| 825 EXPECT_NE(i, ipc_sender_.sent_end()); | 836 EXPECT_NE(i, ipc_sender_.sent_end()); |
| 826 IPC::Message* message = i->get(); | 837 IPC::Message* message = i->get(); |
| 827 const base::DictionaryValue* details = NULL; | 838 const base::DictionaryValue* details = NULL; |
| 828 ExtensionMsg_MessageInvoke::Param param; | 839 ExtensionMsg_MessageInvoke::Param param; |
| 829 GetPartOfMessageArguments(message, &details, ¶m); | 840 GetPartOfMessageArguments(message, &details, ¶m); |
| 830 ASSERT_TRUE(details != NULL); | 841 ASSERT_TRUE(details != NULL); |
| 831 EXPECT_FALSE(details->HasKey(keys::kRequestBodyKey)); | 842 EXPECT_FALSE(details->HasKey(keys::kRequestBodyKey)); |
| 832 } | 843 } |
| 833 | 844 |
| 834 EXPECT_EQ(i, ipc_sender_.sent_end()); | 845 EXPECT_EQ(i, ipc_sender_.sent_end()); |
| 835 } | 846 } |
| 836 | 847 |
| 848 // Tests that |embedder_process_id| is not relevant for adding and removing |
| 849 // listeners with |web_view_instance_id| = 0. |
| 850 TEST_F(ExtensionWebRequestTest, AddAndRemoveListeners) { |
| 851 std::string ext_id("abcdefghijklmnopabcdefghijklmnop"); |
| 852 ExtensionWebRequestEventRouter::RequestFilter filter; |
| 853 const std::string kEventName(web_request::OnBeforeRequest::kEventName); |
| 854 const std::string kSubEventName = kEventName + "/1"; |
| 855 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); |
| 856 EXPECT_EQ( |
| 857 0u, |
| 858 ExtensionWebRequestEventRouter::GetInstance()->GetListenerCountForTesting( |
| 859 &profile_, kEventName)); |
| 860 |
| 861 // Add two non-webview listeners. |
| 862 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 863 &profile_, ext_id, ext_id, events::FOR_TEST, kEventName, kSubEventName, |
| 864 filter, 0, 1 /* embedder_process_id */, 0, |
| 865 ipc_sender_factory.GetWeakPtr()); |
| 866 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 867 &profile_, ext_id, ext_id, events::FOR_TEST, kEventName, kSubEventName, |
| 868 filter, 0, 2 /* embedder_process_id */, 0, |
| 869 ipc_sender_factory.GetWeakPtr()); |
| 870 EXPECT_EQ( |
| 871 2u, |
| 872 ExtensionWebRequestEventRouter::GetInstance()->GetListenerCountForTesting( |
| 873 &profile_, kEventName)); |
| 874 |
| 875 // Now remove the events without passing an explicit process ID. |
| 876 ExtensionWebRequestEventRouter::EventListener::Identifier id1( |
| 877 &profile_, ext_id, kSubEventName, 0, 0); |
| 878 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 879 EXPECT_EQ( |
| 880 1u, |
| 881 ExtensionWebRequestEventRouter::GetInstance()->GetListenerCountForTesting( |
| 882 &profile_, kEventName)); |
| 883 |
| 884 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 885 EXPECT_EQ( |
| 886 0u, |
| 887 ExtensionWebRequestEventRouter::GetInstance()->GetListenerCountForTesting( |
| 888 &profile_, kEventName)); |
| 889 } |
| 890 |
| 891 // The set of blocked requests should not grow unbounded. |
| 892 TEST_F(ExtensionWebRequestTest, BlockedRequestsAreRemoved) { |
| 893 std::string extension_id("1"); |
| 894 ExtensionWebRequestEventRouter::RequestFilter filter; |
| 895 |
| 896 // Subscribe to OnBeforeRequest. |
| 897 const std::string kEventName(web_request::OnBeforeRequest::kEventName); |
| 898 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); |
| 899 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 900 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, |
| 901 kEventName + "/1", filter, ExtraInfoSpec::BLOCKING, 0, 0, |
| 902 ipc_sender_factory.GetWeakPtr()); |
| 903 ExtensionWebRequestEventRouter::EventListener::Identifier id( |
| 904 &profile_, extension_id, kEventName + "/1", 0, 0); |
| 905 ExtensionWebRequestEventRouter::EventListener* listener = |
| 906 ExtensionWebRequestEventRouter::GetInstance()->FindEventListener(id); |
| 907 ASSERT_NE(nullptr, listener); |
| 908 EXPECT_EQ(0u, listener->blocked_requests.size()); |
| 909 |
| 910 // Send a request. It should block. Wait for the run loop to become idle. |
| 911 GURL request_url("about:blank"); |
| 912 std::unique_ptr<net::URLRequest> request( |
| 913 context_->CreateRequest(request_url, net::DEFAULT_PRIORITY, &delegate_)); |
| 914 // Extension response for OnErrorOccurred: Terminate the message loop. |
| 915 { |
| 916 base::RunLoop run_loop; |
| 917 ipc_sender_.PushTask( |
| 918 base::Bind(base::IgnoreResult(&base::SingleThreadTaskRunner::PostTask), |
| 919 base::ThreadTaskRunnerHandle::Get(), FROM_HERE, |
| 920 run_loop.QuitWhenIdleClosure())); |
| 921 request->Start(); |
| 922 run_loop.Run(); |
| 923 } |
| 924 |
| 925 // Confirm that there is a blocked request. |
| 926 EXPECT_EQ(1u, listener->blocked_requests.size()); |
| 927 |
| 928 // Send a response through. |
| 929 ExtensionWebRequestEventRouter::EventResponse* response = |
| 930 new ExtensionWebRequestEventRouter::EventResponse( |
| 931 extension_id, base::Time::FromDoubleT(1)); |
| 932 response->cancel = true; |
| 933 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( |
| 934 &profile_, extension_id, kEventName, kEventName + "/1", |
| 935 request->identifier(), response); |
| 936 { |
| 937 base::RunLoop run_loop; |
| 938 run_loop.RunUntilIdle(); |
| 939 } |
| 940 |
| 941 // Now there should be no blocked requests. |
| 942 EXPECT_EQ(0u, listener->blocked_requests.size()); |
| 943 |
| 944 EXPECT_TRUE(!request->is_pending()); |
| 945 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
| 946 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request->status().error()); |
| 947 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 948 |
| 949 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id); |
| 950 } |
| 951 |
| 837 struct HeaderModificationTest_Header { | 952 struct HeaderModificationTest_Header { |
| 838 const char* name; | 953 const char* name; |
| 839 const char* value; | 954 const char* value; |
| 840 }; | 955 }; |
| 841 | 956 |
| 842 struct HeaderModificationTest_Modification { | 957 struct HeaderModificationTest_Modification { |
| 843 enum Type { | 958 enum Type { |
| 844 SET, | 959 SET, |
| 845 REMOVE | 960 REMOVE |
| 846 }; | 961 }; |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1039 std::string value; | 1154 std::string value; |
| 1040 ASSERT_TRUE(header->GetString(keys::kHeaderNameKey, &key)); | 1155 ASSERT_TRUE(header->GetString(keys::kHeaderNameKey, &key)); |
| 1041 ASSERT_TRUE(header->GetString(keys::kHeaderValueKey, &value)); | 1156 ASSERT_TRUE(header->GetString(keys::kHeaderValueKey, &value)); |
| 1042 observed_headers.SetHeader(key, value); | 1157 observed_headers.SetHeader(key, value); |
| 1043 } | 1158 } |
| 1044 | 1159 |
| 1045 EXPECT_EQ(expected_headers.ToString(), observed_headers.ToString()); | 1160 EXPECT_EQ(expected_headers.ToString(), observed_headers.ToString()); |
| 1046 ++num_headers_observed; | 1161 ++num_headers_observed; |
| 1047 } | 1162 } |
| 1048 EXPECT_EQ(1, num_headers_observed); | 1163 EXPECT_EQ(1, num_headers_observed); |
| 1049 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 1164 ExtensionWebRequestEventRouter::EventListener::Identifier id1( |
| 1050 &profile_, extension1_id, kEventName + "/1", 0, 0); | 1165 &profile_, extension1_id, kEventName + "/1", 0, 0); |
| 1051 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 1166 ExtensionWebRequestEventRouter::EventListener::Identifier id2( |
| 1052 &profile_, extension2_id, kEventName + "/2", 0, 0); | 1167 &profile_, extension2_id, kEventName + "/2", 0, 0); |
| 1053 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( | 1168 ExtensionWebRequestEventRouter::EventListener::Identifier id3( |
| 1054 &profile_, extension3_id, | 1169 &profile_, extension3_id, std::string(keys::kOnSendHeadersEvent) + "/3", |
| 1055 std::string(keys::kOnSendHeadersEvent) + "/3", 0, 0); | 1170 0, 0); |
| 1171 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1); |
| 1172 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2); |
| 1173 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id3); |
| 1056 }; | 1174 }; |
| 1057 | 1175 |
| 1058 namespace { | 1176 namespace { |
| 1059 | 1177 |
| 1060 void TestInitFromValue(const std::string& values, bool expected_return_code, | 1178 void TestInitFromValue(const std::string& values, bool expected_return_code, |
| 1061 int expected_extra_info_spec) { | 1179 int expected_extra_info_spec) { |
| 1062 int actual_info_spec; | 1180 int actual_info_spec; |
| 1063 bool actual_return_code = GenerateInfoSpec(values, &actual_info_spec); | 1181 bool actual_return_code = GenerateInfoSpec(values, &actual_info_spec); |
| 1064 EXPECT_EQ(expected_return_code, actual_return_code); | 1182 EXPECT_EQ(expected_return_code, actual_return_code); |
| 1065 if (expected_return_code) | 1183 if (expected_return_code) |
| (...skipping 1249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2315 EXPECT_TRUE(credentials_set); | 2433 EXPECT_TRUE(credentials_set); |
| 2316 EXPECT_FALSE(auth3.Empty()); | 2434 EXPECT_FALSE(auth3.Empty()); |
| 2317 EXPECT_EQ(username, auth1.username()); | 2435 EXPECT_EQ(username, auth1.username()); |
| 2318 EXPECT_EQ(password, auth1.password()); | 2436 EXPECT_EQ(password, auth1.password()); |
| 2319 EXPECT_EQ(1u, warning_set.size()); | 2437 EXPECT_EQ(1u, warning_set.size()); |
| 2320 EXPECT_TRUE(HasWarning(warning_set, "extid2")); | 2438 EXPECT_TRUE(HasWarning(warning_set, "extid2")); |
| 2321 EXPECT_EQ(3u, capturing_net_log.GetSize()); | 2439 EXPECT_EQ(3u, capturing_net_log.GetSize()); |
| 2322 } | 2440 } |
| 2323 | 2441 |
| 2324 } // namespace extensions | 2442 } // namespace extensions |
| OLD | NEW |