| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <queue> | 5 #include <queue> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 | 9 |
| 10 #include "chrome/browser/extensions/extension_event_router_forwarder.h" | 10 #include "chrome/browser/extensions/extension_event_router_forwarder.h" |
| 11 #include "chrome/browser/extensions/extension_webrequest_api.h" | 11 #include "chrome/browser/extensions/extension_webrequest_api.h" |
| 12 #include "chrome/browser/extensions/extension_webrequest_api_constants.h" | 12 #include "chrome/browser/extensions/extension_webrequest_api_constants.h" |
| 13 #include "chrome/browser/net/chrome_network_delegate.h" | 13 #include "chrome/browser/net/chrome_network_delegate.h" |
| 14 #include "chrome/browser/prefs/pref_member.h" | 14 #include "chrome/browser/prefs/pref_member.h" |
| 15 #include "chrome/common/extensions/extension_messages.h" | 15 #include "chrome/common/extensions/extension_messages.h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/testing_pref_service.h" | 17 #include "chrome/test/testing_pref_service.h" |
| 18 #include "chrome/test/testing_profile.h" | 18 #include "chrome/test/testing_profile.h" |
| 19 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 20 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 namespace keys = extension_webrequest_api_constants; | 23 namespace keys = extension_webrequest_api_constants; |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 static void EventHandledOnIOThread( | 26 static void EventHandledOnIOThread( |
| 27 ProfileId profile_id, | 27 void* profile, |
| 28 const std::string& extension_id, | 28 const std::string& extension_id, |
| 29 const std::string& event_name, | 29 const std::string& event_name, |
| 30 const std::string& sub_event_name, | 30 const std::string& sub_event_name, |
| 31 uint64 request_id, | 31 uint64 request_id, |
| 32 ExtensionWebRequestEventRouter::EventResponse* response) { | 32 ExtensionWebRequestEventRouter::EventResponse* response) { |
| 33 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( | 33 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( |
| 34 profile_id, extension_id, event_name, sub_event_name, request_id, | 34 profile, extension_id, event_name, sub_event_name, request_id, |
| 35 response); | 35 response); |
| 36 } | 36 } |
| 37 } // namespace | 37 } // namespace |
| 38 | 38 |
| 39 // A mock event router that responds to events with a pre-arranged queue of | 39 // A mock event router that responds to events with a pre-arranged queue of |
| 40 // Tasks. | 40 // Tasks. |
| 41 class TestIPCSender : public IPC::Message::Sender { | 41 class TestIPCSender : public IPC::Message::Sender { |
| 42 public: | 42 public: |
| 43 // Adds a Task to the queue. We will fire these in order as events are | 43 // Adds a Task to the queue. We will fire these in order as events are |
| 44 // dispatched. | 44 // dispatched. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 64 std::queue<Task*> task_queue_; | 64 std::queue<Task*> task_queue_; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 class ExtensionWebRequestTest : public testing::Test { | 67 class ExtensionWebRequestTest : public testing::Test { |
| 68 protected: | 68 protected: |
| 69 virtual void SetUp() { | 69 virtual void SetUp() { |
| 70 event_router_ = new ExtensionEventRouterForwarder(); | 70 event_router_ = new ExtensionEventRouterForwarder(); |
| 71 enable_referrers_.Init( | 71 enable_referrers_.Init( |
| 72 prefs::kEnableReferrers, profile_.GetTestingPrefService(), NULL); | 72 prefs::kEnableReferrers, profile_.GetTestingPrefService(), NULL); |
| 73 network_delegate_.reset(new ChromeNetworkDelegate( | 73 network_delegate_.reset(new ChromeNetworkDelegate( |
| 74 event_router_.get(), NULL, profile_.GetRuntimeId(), | 74 event_router_.get(), NULL, &profile_, &enable_referrers_)); |
| 75 &enable_referrers_)); | |
| 76 context_ = new TestURLRequestContext(); | 75 context_ = new TestURLRequestContext(); |
| 77 context_->set_network_delegate(network_delegate_.get()); | 76 context_->set_network_delegate(network_delegate_.get()); |
| 78 } | 77 } |
| 79 | 78 |
| 80 MessageLoopForIO io_loop_; | 79 MessageLoopForIO io_loop_; |
| 81 TestingProfile profile_; | 80 TestingProfile profile_; |
| 82 TestDelegate delegate_; | 81 TestDelegate delegate_; |
| 83 BooleanPrefMember enable_referrers_; | 82 BooleanPrefMember enable_referrers_; |
| 84 TestIPCSender ipc_sender_; | 83 TestIPCSender ipc_sender_; |
| 85 scoped_refptr<ExtensionEventRouterForwarder> event_router_; | 84 scoped_refptr<ExtensionEventRouterForwarder> event_router_; |
| 86 scoped_refptr<ExtensionInfoMap> extension_info_map_; | 85 scoped_refptr<ExtensionInfoMap> extension_info_map_; |
| 87 scoped_ptr<ChromeNetworkDelegate> network_delegate_; | 86 scoped_ptr<ChromeNetworkDelegate> network_delegate_; |
| 88 scoped_refptr<TestURLRequestContext> context_; | 87 scoped_refptr<TestURLRequestContext> context_; |
| 89 }; | 88 }; |
| 90 | 89 |
| 91 // Tests that we handle disagreements among extensions about responses to | 90 // Tests that we handle disagreements among extensions about responses to |
| 92 // blocking events by choosing the response from the most-recently-installed | 91 // blocking events by choosing the response from the most-recently-installed |
| 93 // extension. | 92 // extension. |
| 94 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedence) { | 93 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedence) { |
| 95 std::string extension1_id("1"); | 94 std::string extension1_id("1"); |
| 96 std::string extension2_id("2"); | 95 std::string extension2_id("2"); |
| 97 ExtensionWebRequestEventRouter::RequestFilter filter; | 96 ExtensionWebRequestEventRouter::RequestFilter filter; |
| 98 const std::string kEventName(keys::kOnBeforeRequest); | 97 const std::string kEventName(keys::kOnBeforeRequest); |
| 99 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); | 98 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); |
| 100 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 99 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 101 profile_.GetRuntimeId(), extension1_id, kEventName, | 100 &profile_, extension1_id, kEventName, kEventName + "/1", filter, |
| 102 kEventName + "/1", filter, | |
| 103 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, | 101 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, |
| 104 ipc_sender_factory.GetWeakPtr()); | 102 ipc_sender_factory.GetWeakPtr()); |
| 105 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 103 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
| 106 profile_.GetRuntimeId(), extension2_id, kEventName, | 104 &profile_, extension2_id, kEventName, kEventName + "/2", filter, |
| 107 kEventName + "/2", filter, | |
| 108 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, | 105 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, |
| 109 ipc_sender_factory.GetWeakPtr()); | 106 ipc_sender_factory.GetWeakPtr()); |
| 110 | 107 |
| 111 net::URLRequest request(GURL("about:blank"), &delegate_); | 108 net::URLRequest request(GURL("about:blank"), &delegate_); |
| 112 request.set_context(context_); | 109 request.set_context(context_); |
| 113 | 110 |
| 114 { | 111 { |
| 115 // onBeforeRequest will be dispatched twice initially. The second response - | 112 // onBeforeRequest will be dispatched twice initially. The second response - |
| 116 // the redirect - should win, since it has a later |install_time|. The | 113 // the redirect - should win, since it has a later |install_time|. The |
| 117 // redirect will dispatch another pair of onBeforeRequest. There, the first | 114 // redirect will dispatch another pair of onBeforeRequest. There, the first |
| 118 // response should win (later |install_time|). | 115 // response should win (later |install_time|). |
| 119 GURL redirect_url("about:redirected"); | 116 GURL redirect_url("about:redirected"); |
| 120 ExtensionWebRequestEventRouter::EventResponse* response = NULL; | 117 ExtensionWebRequestEventRouter::EventResponse* response = NULL; |
| 121 | 118 |
| 122 // Extension1 response. Arrives first, but ignored due to install_time. | 119 // Extension1 response. Arrives first, but ignored due to install_time. |
| 123 response = new ExtensionWebRequestEventRouter::EventResponse( | 120 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 124 extension1_id, base::Time::FromDoubleT(1)); | 121 extension1_id, base::Time::FromDoubleT(1)); |
| 125 response->cancel = true; | 122 response->cancel = true; |
| 126 ipc_sender_.PushTask( | 123 ipc_sender_.PushTask( |
| 127 NewRunnableFunction(&EventHandledOnIOThread, | 124 NewRunnableFunction(&EventHandledOnIOThread, |
| 128 profile_.GetRuntimeId(), extension1_id, | 125 &profile_, extension1_id, kEventName, kEventName + "/1", |
| 129 kEventName, kEventName + "/1", request.identifier(), response)); | 126 request.identifier(), response)); |
| 130 | 127 |
| 131 // Extension2 response. Arrives second, and chosen because of install_time. | 128 // Extension2 response. Arrives second, and chosen because of install_time. |
| 132 response = new ExtensionWebRequestEventRouter::EventResponse( | 129 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 133 extension2_id, base::Time::FromDoubleT(2)); | 130 extension2_id, base::Time::FromDoubleT(2)); |
| 134 response->new_url = redirect_url; | 131 response->new_url = redirect_url; |
| 135 ipc_sender_.PushTask( | 132 ipc_sender_.PushTask( |
| 136 NewRunnableFunction(&EventHandledOnIOThread, | 133 NewRunnableFunction(&EventHandledOnIOThread, |
| 137 profile_.GetRuntimeId(), extension2_id, | 134 &profile_, extension2_id, kEventName, kEventName + "/2", |
| 138 kEventName, kEventName + "/2", request.identifier(), response)); | 135 request.identifier(), response)); |
| 139 | 136 |
| 140 // Extension2 response to the redirected URL. Arrives first, and chosen. | 137 // Extension2 response to the redirected URL. Arrives first, and chosen. |
| 141 response = new ExtensionWebRequestEventRouter::EventResponse( | 138 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 142 extension2_id, base::Time::FromDoubleT(2)); | 139 extension2_id, base::Time::FromDoubleT(2)); |
| 143 ipc_sender_.PushTask( | 140 ipc_sender_.PushTask( |
| 144 NewRunnableFunction(&EventHandledOnIOThread, | 141 NewRunnableFunction(&EventHandledOnIOThread, |
| 145 profile_.GetRuntimeId(), extension2_id, | 142 &profile_, extension2_id, kEventName, kEventName + "/2", |
| 146 kEventName, kEventName + "/2", request.identifier(), response)); | 143 request.identifier(), response)); |
| 147 | 144 |
| 148 // Extension1 response to the redirected URL. Arrives second, and ignored. | 145 // Extension1 response to the redirected URL. Arrives second, and ignored. |
| 149 response = new ExtensionWebRequestEventRouter::EventResponse( | 146 response = new ExtensionWebRequestEventRouter::EventResponse( |
| 150 extension1_id, base::Time::FromDoubleT(1)); | 147 extension1_id, base::Time::FromDoubleT(1)); |
| 151 response->cancel = true; | 148 response->cancel = true; |
| 152 ipc_sender_.PushTask( | 149 ipc_sender_.PushTask( |
| 153 NewRunnableFunction(&EventHandledOnIOThread, | 150 NewRunnableFunction(&EventHandledOnIOThread, |
| 154 profile_.GetRuntimeId(), extension1_id, | 151 &profile_, extension1_id, kEventName, kEventName + "/1", |
| 155 kEventName, kEventName + "/1", request.identifier(), response)); | 152 request.identifier(), response)); |
| 156 | 153 |
| 157 request.Start(); | 154 request.Start(); |
| 158 MessageLoop::current()->Run(); | 155 MessageLoop::current()->Run(); |
| 159 | 156 |
| 160 EXPECT_TRUE(!request.is_pending()); | 157 EXPECT_TRUE(!request.is_pending()); |
| 161 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 158 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
| 162 EXPECT_EQ(0, request.status().os_error()); | 159 EXPECT_EQ(0, request.status().os_error()); |
| 163 EXPECT_EQ(redirect_url, request.url()); | 160 EXPECT_EQ(redirect_url, request.url()); |
| 164 EXPECT_EQ(2U, request.url_chain().size()); | 161 EXPECT_EQ(2U, request.url_chain().size()); |
| 165 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); | 162 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
| 166 } | 163 } |
| 167 } | 164 } |
| OLD | NEW |