Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: chrome/browser/extensions/api/web_request/web_request_api_unittest.cc

Issue 2303113002: Fix semantics of ExtensionWebRequestEventRouter::EventListeners. (Closed)
Patch Set: Comments from rdevlin. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | extensions/browser/api/web_request/web_request_api.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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::ID id1(
359 &profile_, extension1_id, kEventName + "/1", 0, 0); 360 &profile_, extension1_id, kEventName + "/1", 0, 0);
360 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 361 ExtensionWebRequestEventRouter::EventListener::ID id2(
361 &profile_, extension2_id, kEventName + "/2", 0, 0); 362 &profile_, extension2_id, kEventName + "/2", 0, 0);
363 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
364 false);
365 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2,
366 false);
362 } 367 }
363 368
364 // Test that a request is canceled if this is requested by any extension 369 // Test that a request is canceled if this is requested by any extension
365 // regardless whether it is the extension with the highest precedence. 370 // regardless whether it is the extension with the highest precedence.
366 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedenceCancel) { 371 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedenceCancel) {
367 std::string extension1_id("1"); 372 std::string extension1_id("1");
368 std::string extension2_id("2"); 373 std::string extension2_id("2");
369 ExtensionWebRequestEventRouter::RequestFilter filter; 374 ExtensionWebRequestEventRouter::RequestFilter filter;
370 const std::string kEventName(web_request::OnBeforeRequest::kEventName); 375 const std::string kEventName(web_request::OnBeforeRequest::kEventName);
371 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); 376 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 417
413 base::RunLoop().Run(); 418 base::RunLoop().Run();
414 419
415 EXPECT_TRUE(!request->is_pending()); 420 EXPECT_TRUE(!request->is_pending());
416 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); 421 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
417 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request->status().error()); 422 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request->status().error());
418 EXPECT_EQ(request_url, request->url()); 423 EXPECT_EQ(request_url, request->url());
419 EXPECT_EQ(1U, request->url_chain().size()); 424 EXPECT_EQ(1U, request->url_chain().size());
420 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); 425 EXPECT_EQ(0U, ipc_sender_.GetNumTasks());
421 426
422 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 427 ExtensionWebRequestEventRouter::EventListener::ID id1(
423 &profile_, extension1_id, kEventName + "/1", 0, 0); 428 &profile_, extension1_id, kEventName + "/1", 0, 0);
424 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 429 ExtensionWebRequestEventRouter::EventListener::ID id2(
425 &profile_, extension2_id, kEventName + "/2", 0, 0); 430 &profile_, extension2_id, kEventName + "/2", 0, 0);
431 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
432 false);
433 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2,
434 false);
426 } 435 }
427 436
428 TEST_F(ExtensionWebRequestTest, SimulateChancelWhileBlocked) { 437 TEST_F(ExtensionWebRequestTest, SimulateChancelWhileBlocked) {
429 // We subscribe to OnBeforeRequest and OnErrorOccurred. 438 // We subscribe to OnBeforeRequest and OnErrorOccurred.
430 // While the OnBeforeRequest handler is blocked, we cancel the request. 439 // While the OnBeforeRequest handler is blocked, we cancel the request.
431 // We verify that the response of the blocked OnBeforeRequest handler 440 // We verify that the response of the blocked OnBeforeRequest handler
432 // is ignored. 441 // is ignored.
433 442
434 std::string extension_id("1"); 443 std::string extension_id("1");
435 ExtensionWebRequestEventRouter::RequestFilter filter; 444 ExtensionWebRequestEventRouter::RequestFilter filter;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 request->Cancel(); 485 request->Cancel();
477 run_loop.Run(); 486 run_loop.Run();
478 487
479 EXPECT_TRUE(!request->is_pending()); 488 EXPECT_TRUE(!request->is_pending());
480 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status()); 489 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status());
481 EXPECT_EQ(net::ERR_ABORTED, request->status().error()); 490 EXPECT_EQ(net::ERR_ABORTED, request->status().error());
482 EXPECT_EQ(request_url, request->url()); 491 EXPECT_EQ(request_url, request->url());
483 EXPECT_EQ(1U, request->url_chain().size()); 492 EXPECT_EQ(1U, request->url_chain().size());
484 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); 493 EXPECT_EQ(0U, ipc_sender_.GetNumTasks());
485 494
486 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 495 ExtensionWebRequestEventRouter::EventListener::ID id1(
487 &profile_, extension_id, kEventName + "/1", 0, 0); 496 &profile_, extension_id, kEventName + "/1", 0, 0);
488 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 497 ExtensionWebRequestEventRouter::EventListener::ID id2(
489 &profile_, extension_id, kEventName2 + "/1", 0, 0); 498 &profile_, extension_id, kEventName2 + "/1", 0, 0);
499 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
500 false);
501 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2,
502 false);
490 } 503 }
491 504
492 namespace { 505 namespace {
493 506
494 // Create the numerical representation of |values|, strings passed as 507 // Create the numerical representation of |values|, strings passed as
495 // extraInfoSpec by the event handler. Returns true on success, otherwise false. 508 // extraInfoSpec by the event handler. Returns true on success, otherwise false.
496 bool GenerateInfoSpec(const std::string& values, int* result) { 509 bool GenerateInfoSpec(const std::string& values, int* result) {
497 // Create a base::ListValue of strings. 510 // Create a base::ListValue of strings.
498 base::ListValue list_value; 511 base::ListValue list_value;
499 for (const std::string& cur : 512 for (const std::string& cur :
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 649 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
637 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, 650 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName,
638 kEventName + "/1", filter, extra_info_spec_body, 0, 0, 651 kEventName + "/1", filter, extra_info_spec_body, 0, 0,
639 ipc_sender_factory.GetWeakPtr()); 652 ipc_sender_factory.GetWeakPtr());
640 653
641 FireURLRequestWithData(kMethodPost, kMultipart, form_1, form_2); 654 FireURLRequestWithData(kMethodPost, kMultipart, form_1, form_2);
642 655
643 // We inspect the result in the message list of |ipc_sender_| later. 656 // We inspect the result in the message list of |ipc_sender_| later.
644 base::RunLoop().RunUntilIdle(); 657 base::RunLoop().RunUntilIdle();
645 658
646 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 659 ExtensionWebRequestEventRouter::EventListener::ID id1(
647 &profile_, extension_id, kEventName + "/1", 0, 0); 660 &profile_, extension_id, kEventName + "/1", 0, 0);
661 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
662 false);
648 663
649 // Part 2. 664 // Part 2.
650 // Now subscribe to OnBeforeRequest *without* the requestBody requirement. 665 // Now subscribe to OnBeforeRequest *without* the requestBody requirement.
651 ASSERT_TRUE( 666 ASSERT_TRUE(
652 GenerateInfoSpec(string_spec_no_post, &extra_info_spec_empty)); 667 GenerateInfoSpec(string_spec_no_post, &extra_info_spec_empty));
653 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 668 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
654 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, 669 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName,
655 kEventName + "/1", filter, extra_info_spec_empty, 0, 0, 670 kEventName + "/1", filter, extra_info_spec_empty, 0, 0,
656 ipc_sender_factory.GetWeakPtr()); 671 ipc_sender_factory.GetWeakPtr());
657 672
658 FireURLRequestWithData(kMethodPost, kMultipart, form_1, form_2); 673 FireURLRequestWithData(kMethodPost, kMultipart, form_1, form_2);
659 674
660 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 675 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
661 &profile_, extension_id, kEventName + "/1", 0, 0); 676 false);
662 677
663 // Subscribe to OnBeforeRequest with requestBody requirement. 678 // Subscribe to OnBeforeRequest with requestBody requirement.
664 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( 679 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
665 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName, 680 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName,
666 kEventName + "/1", filter, extra_info_spec_body, 0, 0, 681 kEventName + "/1", filter, extra_info_spec_body, 0, 0,
667 ipc_sender_factory.GetWeakPtr()); 682 ipc_sender_factory.GetWeakPtr());
668 683
669 // Part 3. 684 // Part 3.
670 // Now send a POST request with body which is not parseable as a form. 685 // Now send a POST request with body which is not parseable as a form.
671 FireURLRequestWithData(kMethodPost, NULL /*no header*/, plain_1, plain_2); 686 FireURLRequestWithData(kMethodPost, NULL /*no header*/, plain_1, plain_2);
672 687
673 // Part 4. 688 // Part 4.
674 // Now send a PUT request with the same body as above. 689 // Now send a PUT request with the same body as above.
675 FireURLRequestWithData(kMethodPut, NULL /*no header*/, plain_1, plain_2); 690 FireURLRequestWithData(kMethodPut, NULL /*no header*/, plain_1, plain_2);
676 691
677 base::RunLoop().RunUntilIdle(); 692 base::RunLoop().RunUntilIdle();
678 693
679 // Clean-up. 694 // Clean-up.
680 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 695 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
681 &profile_, extension_id, kEventName + "/1", 0, 0); 696 false);
682 697
683 IPC::Message* message = NULL; 698 IPC::Message* message = NULL;
684 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); 699 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin();
685 for (size_t test = 0; test < arraysize(kExpected); ++test) { 700 for (size_t test = 0; test < arraysize(kExpected); ++test) {
686 SCOPED_TRACE(testing::Message("iteration number ") << test); 701 SCOPED_TRACE(testing::Message("iteration number ") << test);
687 EXPECT_NE(i, ipc_sender_.sent_end()); 702 EXPECT_NE(i, ipc_sender_.sent_end());
688 message = (i++)->get(); 703 message = (i++)->get();
689 const base::DictionaryValue* details; 704 const base::DictionaryValue* details;
690 ExtensionMsg_MessageInvoke::Param param; 705 ExtensionMsg_MessageInvoke::Param param;
691 GetPartOfMessageArguments(message, &details, &param); 706 GetPartOfMessageArguments(message, &details, &param);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 // Only one request is sent, but more than one event will be triggered. 762 // Only one request is sent, but more than one event will be triggered.
748 for (size_t i = 1; i < arraysize(kExpected); ++i) 763 for (size_t i = 1; i < arraysize(kExpected); ++i)
749 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); 764 ipc_sender_.PushTask(base::Bind(&base::DoNothing));
750 765
751 const std::vector<char> part_of_body(1); 766 const std::vector<char> part_of_body(1);
752 FireURLRequestWithData("POST", nullptr, part_of_body, part_of_body); 767 FireURLRequestWithData("POST", nullptr, part_of_body, part_of_body);
753 768
754 base::RunLoop().RunUntilIdle(); 769 base::RunLoop().RunUntilIdle();
755 770
756 // Clean-up 771 // Clean-up
757 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 772 ExtensionWebRequestEventRouter::EventListener::ID id1(
758 &profile_, extension_id1, kEventName + "/1", 0, 0); 773 &profile_, extension_id1, kEventName + "/1", 0, 0);
759 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 774 ExtensionWebRequestEventRouter::EventListener::ID id2(
775 &profile_, extension_id1, kEventName + "/2", 0, 0);
776 ExtensionWebRequestEventRouter::EventListener::ID id3(
777 &profile_, extension_id2, kEventName + "/1", 0, 0);
778 ExtensionWebRequestEventRouter::EventListener::ID id4(
760 &profile_, extension_id2, kEventName + "/2", 0, 0); 779 &profile_, extension_id2, kEventName + "/2", 0, 0);
761 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 780 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
762 &profile_, extension_id1, kEventName + "/1", 0, 0); 781 false);
763 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 782 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2,
764 &profile_, extension_id2, kEventName + "/2", 0, 0); 783 false);
784 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id3,
785 false);
786 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id4,
787 false);
765 788
766 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); 789 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin();
767 790
768 for (size_t test = 0; test < arraysize(kExpected); ++test, ++i) { 791 for (size_t test = 0; test < arraysize(kExpected); ++test, ++i) {
769 SCOPED_TRACE(testing::Message("iteration number ") << test); 792 SCOPED_TRACE(testing::Message("iteration number ") << test);
770 EXPECT_NE(i, ipc_sender_.sent_end()); 793 EXPECT_NE(i, ipc_sender_.sent_end());
771 IPC::Message* message = i->get(); 794 IPC::Message* message = i->get();
772 const base::DictionaryValue* details = nullptr; 795 const base::DictionaryValue* details = nullptr;
773 ExtensionMsg_MessageInvoke::Param param; 796 ExtensionMsg_MessageInvoke::Param param;
774 GetPartOfMessageArguments(message, &details, &param); 797 GetPartOfMessageArguments(message, &details, &param);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 std::unique_ptr<net::URLRequest> request(context_->CreateRequest( 832 std::unique_ptr<net::URLRequest> request(context_->CreateRequest(
810 request_url, net::DEFAULT_PRIORITY, &delegate_)); 833 request_url, net::DEFAULT_PRIORITY, &delegate_));
811 request->set_method(kMethods[i]); 834 request->set_method(kMethods[i]);
812 ipc_sender_.PushTask(base::Bind(&base::DoNothing)); 835 ipc_sender_.PushTask(base::Bind(&base::DoNothing));
813 request->Start(); 836 request->Start();
814 } 837 }
815 838
816 // We inspect the result in the message list of |ipc_sender_| later. 839 // We inspect the result in the message list of |ipc_sender_| later.
817 base::RunLoop().RunUntilIdle(); 840 base::RunLoop().RunUntilIdle();
818 841
819 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 842 ExtensionWebRequestEventRouter::EventListener::ID id1(
820 &profile_, extension_id, kEventName + "/1", 0, 0); 843 &profile_, extension_id, kEventName + "/1", 0, 0);
844 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
845 false);
821 846
822 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin(); 847 TestIPCSender::SentMessages::const_iterator i = ipc_sender_.sent_begin();
823 for (size_t test = 0; test < arraysize(kMethods); ++test, ++i) { 848 for (size_t test = 0; test < arraysize(kMethods); ++test, ++i) {
824 SCOPED_TRACE(testing::Message("iteration number ") << test); 849 SCOPED_TRACE(testing::Message("iteration number ") << test);
825 EXPECT_NE(i, ipc_sender_.sent_end()); 850 EXPECT_NE(i, ipc_sender_.sent_end());
826 IPC::Message* message = i->get(); 851 IPC::Message* message = i->get();
827 const base::DictionaryValue* details = NULL; 852 const base::DictionaryValue* details = NULL;
828 ExtensionMsg_MessageInvoke::Param param; 853 ExtensionMsg_MessageInvoke::Param param;
829 GetPartOfMessageArguments(message, &details, &param); 854 GetPartOfMessageArguments(message, &details, &param);
830 ASSERT_TRUE(details != NULL); 855 ASSERT_TRUE(details != NULL);
831 EXPECT_FALSE(details->HasKey(keys::kRequestBodyKey)); 856 EXPECT_FALSE(details->HasKey(keys::kRequestBodyKey));
832 } 857 }
833 858
834 EXPECT_EQ(i, ipc_sender_.sent_end()); 859 EXPECT_EQ(i, ipc_sender_.sent_end());
835 } 860 }
836 861
862 // Tests that |embedder_process_id| is not relevant for adding and removing
863 // listeners with |web_view_instance_id| = 0.
864 TEST_F(ExtensionWebRequestTest, AddAndRemoveListeners) {
865 std::string ext_id("abcdefghijklmnopabcdefghijklmnop");
866 ExtensionWebRequestEventRouter::RequestFilter filter;
867 const std::string kEventName(web_request::OnBeforeRequest::kEventName);
868 const std::string kSubEventName = kEventName + "/1";
869 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_);
870 EXPECT_EQ(
871 0u,
872 ExtensionWebRequestEventRouter::GetInstance()->GetListenerCountForTesting(
873 &profile_, kEventName));
874
875 // Add two non-webview listeners.
876 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
877 &profile_, ext_id, ext_id, events::FOR_TEST, kEventName, kSubEventName,
878 filter, 0, 1 /* embedder_process_id */, 0,
879 ipc_sender_factory.GetWeakPtr());
880 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
881 &profile_, ext_id, ext_id, events::FOR_TEST, kEventName, kSubEventName,
882 filter, 0, 2 /* embedder_process_id */, 0,
883 ipc_sender_factory.GetWeakPtr());
884 EXPECT_EQ(
885 2u,
886 ExtensionWebRequestEventRouter::GetInstance()->GetListenerCountForTesting(
887 &profile_, kEventName));
888
889 // Now remove the events without passing an explicit process ID.
890 ExtensionWebRequestEventRouter::EventListener::ID id1(&profile_, ext_id,
891 kSubEventName, 0, 0);
892 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
893 false);
894 EXPECT_EQ(
895 1u,
896 ExtensionWebRequestEventRouter::GetInstance()->GetListenerCountForTesting(
897 &profile_, kEventName));
898
899 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
900 false);
901 EXPECT_EQ(
902 0u,
903 ExtensionWebRequestEventRouter::GetInstance()->GetListenerCountForTesting(
904 &profile_, kEventName));
905 }
906
907 // The set of blocked requests should not grow unbounded.
908 TEST_F(ExtensionWebRequestTest, BlockedRequestsAreRemoved) {
909 std::string extension_id("1");
910 ExtensionWebRequestEventRouter::RequestFilter filter;
911
912 // Subscribe to OnBeforeRequest.
913 const std::string kEventName(web_request::OnBeforeRequest::kEventName);
914 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_);
915 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener(
916 &profile_, extension_id, extension_id, events::FOR_TEST, kEventName,
917 kEventName + "/1", filter, ExtraInfoSpec::BLOCKING, 0, 0,
918 ipc_sender_factory.GetWeakPtr());
919 ExtensionWebRequestEventRouter::EventListener::ID id(&profile_, extension_id,
920 kEventName + "/1", 0, 0);
921 ExtensionWebRequestEventRouter::EventListener* listener =
922 ExtensionWebRequestEventRouter::GetInstance()->FindEventListener(id);
923 ASSERT_NE(nullptr, listener);
924 EXPECT_EQ(0u, listener->blocked_requests.size());
925
926 // Send a request. It should block. Wait for the run loop to become idle.
927 GURL request_url("about:blank");
928 std::unique_ptr<net::URLRequest> request(
929 context_->CreateRequest(request_url, net::DEFAULT_PRIORITY, &delegate_));
930 // Extension response for OnErrorOccurred: Terminate the message loop.
931 {
932 base::RunLoop run_loop;
933 ipc_sender_.PushTask(
934 base::Bind(base::IgnoreResult(&base::SingleThreadTaskRunner::PostTask),
935 base::ThreadTaskRunnerHandle::Get(), FROM_HERE,
936 run_loop.QuitWhenIdleClosure()));
937 request->Start();
938 run_loop.Run();
939 }
940
941 // Confirm that there is a blocked request.
942 EXPECT_EQ(1u, listener->blocked_requests.size());
943
944 // Send a response through.
945 ExtensionWebRequestEventRouter::EventResponse* response =
946 new ExtensionWebRequestEventRouter::EventResponse(
947 extension_id, base::Time::FromDoubleT(1));
948 response->cancel = true;
949 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled(
950 &profile_, extension_id, kEventName, kEventName + "/1",
951 request->identifier(), response);
952 {
953 base::RunLoop run_loop;
954 run_loop.RunUntilIdle();
955 }
956
957 // Now there should be no blocked requests.
958 EXPECT_EQ(0u, listener->blocked_requests.size());
959
960 EXPECT_TRUE(!request->is_pending());
961 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
962 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request->status().error());
963 EXPECT_EQ(0U, ipc_sender_.GetNumTasks());
964
965 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id, false);
966 }
967
837 struct HeaderModificationTest_Header { 968 struct HeaderModificationTest_Header {
838 const char* name; 969 const char* name;
839 const char* value; 970 const char* value;
840 }; 971 };
841 972
842 struct HeaderModificationTest_Modification { 973 struct HeaderModificationTest_Modification {
843 enum Type { 974 enum Type {
844 SET, 975 SET,
845 REMOVE 976 REMOVE
846 }; 977 };
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 std::string value; 1170 std::string value;
1040 ASSERT_TRUE(header->GetString(keys::kHeaderNameKey, &key)); 1171 ASSERT_TRUE(header->GetString(keys::kHeaderNameKey, &key));
1041 ASSERT_TRUE(header->GetString(keys::kHeaderValueKey, &value)); 1172 ASSERT_TRUE(header->GetString(keys::kHeaderValueKey, &value));
1042 observed_headers.SetHeader(key, value); 1173 observed_headers.SetHeader(key, value);
1043 } 1174 }
1044 1175
1045 EXPECT_EQ(expected_headers.ToString(), observed_headers.ToString()); 1176 EXPECT_EQ(expected_headers.ToString(), observed_headers.ToString());
1046 ++num_headers_observed; 1177 ++num_headers_observed;
1047 } 1178 }
1048 EXPECT_EQ(1, num_headers_observed); 1179 EXPECT_EQ(1, num_headers_observed);
1049 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 1180 ExtensionWebRequestEventRouter::EventListener::ID id1(
1050 &profile_, extension1_id, kEventName + "/1", 0, 0); 1181 &profile_, extension1_id, kEventName + "/1", 0, 0);
1051 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 1182 ExtensionWebRequestEventRouter::EventListener::ID id2(
1052 &profile_, extension2_id, kEventName + "/2", 0, 0); 1183 &profile_, extension2_id, kEventName + "/2", 0, 0);
1053 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener( 1184 ExtensionWebRequestEventRouter::EventListener::ID id3(
1054 &profile_, extension3_id, 1185 &profile_, extension3_id, std::string(keys::kOnSendHeadersEvent) + "/3",
1055 std::string(keys::kOnSendHeadersEvent) + "/3", 0, 0); 1186 0, 0);
1187 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id1,
1188 false);
1189 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id2,
1190 false);
1191 ExtensionWebRequestEventRouter::GetInstance()->RemoveEventListener(id3,
1192 false);
1056 }; 1193 };
1057 1194
1058 namespace { 1195 namespace {
1059 1196
1060 void TestInitFromValue(const std::string& values, bool expected_return_code, 1197 void TestInitFromValue(const std::string& values, bool expected_return_code,
1061 int expected_extra_info_spec) { 1198 int expected_extra_info_spec) {
1062 int actual_info_spec; 1199 int actual_info_spec;
1063 bool actual_return_code = GenerateInfoSpec(values, &actual_info_spec); 1200 bool actual_return_code = GenerateInfoSpec(values, &actual_info_spec);
1064 EXPECT_EQ(expected_return_code, actual_return_code); 1201 EXPECT_EQ(expected_return_code, actual_return_code);
1065 if (expected_return_code) 1202 if (expected_return_code)
(...skipping 1249 matching lines...) Expand 10 before | Expand all | Expand 10 after
2315 EXPECT_TRUE(credentials_set); 2452 EXPECT_TRUE(credentials_set);
2316 EXPECT_FALSE(auth3.Empty()); 2453 EXPECT_FALSE(auth3.Empty());
2317 EXPECT_EQ(username, auth1.username()); 2454 EXPECT_EQ(username, auth1.username());
2318 EXPECT_EQ(password, auth1.password()); 2455 EXPECT_EQ(password, auth1.password());
2319 EXPECT_EQ(1u, warning_set.size()); 2456 EXPECT_EQ(1u, warning_set.size());
2320 EXPECT_TRUE(HasWarning(warning_set, "extid2")); 2457 EXPECT_TRUE(HasWarning(warning_set, "extid2"));
2321 EXPECT_EQ(3u, capturing_net_log.GetSize()); 2458 EXPECT_EQ(3u, capturing_net_log.GetSize());
2322 } 2459 }
2323 2460
2324 } // namespace extensions 2461 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | extensions/browser/api/web_request/web_request_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698