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/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
16 #include "chrome/test/testing_pref_service.h" | 17 #include "chrome/test/testing_pref_service.h" |
17 #include "chrome/test/testing_profile.h" | 18 #include "chrome/test/testing_profile.h" |
18 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
19 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
22 namespace keys = extension_webrequest_api_constants; | 23 namespace keys = extension_webrequest_api_constants; |
23 | 24 |
24 namespace { | 25 namespace { |
25 static void EventHandledOnIOThread( | 26 static void EventHandledOnIOThread( |
26 ProfileId profile_id, | 27 ProfileId profile_id, |
27 const std::string& extension_id, | 28 const std::string& extension_id, |
28 const std::string& event_name, | 29 const std::string& event_name, |
29 const std::string& sub_event_name, | 30 const std::string& sub_event_name, |
30 uint64 request_id, | 31 uint64 request_id, |
31 ExtensionWebRequestEventRouter::EventResponse* response) { | 32 ExtensionWebRequestEventRouter::EventResponse* response) { |
32 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( | 33 ExtensionWebRequestEventRouter::GetInstance()->OnEventHandled( |
33 profile_id, extension_id, event_name, sub_event_name, request_id, | 34 profile_id, extension_id, event_name, sub_event_name, request_id, |
34 response); | 35 response); |
35 } | 36 } |
36 } // namespace | 37 } // namespace |
37 | 38 |
38 // 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 |
39 // Tasks. | 40 // Tasks. |
40 class TestEventRouter : public ExtensionEventRouterForwarder { | 41 class TestIPCSender : public IPC::Message::Sender { |
41 public: | 42 public: |
42 // 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 |
43 // dispatched. | 44 // dispatched. |
44 void PushTask(Task* task) { | 45 void PushTask(Task* task) { |
45 task_queue_.push(task); | 46 task_queue_.push(task); |
46 } | 47 } |
47 | 48 |
48 size_t GetNumTasks() { return task_queue_.size(); } | 49 size_t GetNumTasks() { return task_queue_.size(); } |
49 | 50 |
50 private: | 51 private: |
51 // ExtensionEventRouterForwarder: | 52 // IPC::Message::Sender |
52 virtual void HandleEvent(const std::string& extension_id, | 53 virtual bool Send(IPC::Message* message) { |
53 const std::string& event_name, | 54 EXPECT_EQ(ExtensionMsg_MessageInvoke::ID, message->type()); |
54 const std::string& event_args, | 55 |
55 ProfileId profile_id, | 56 EXPECT_FALSE(task_queue_.empty()); |
56 bool use_profile_to_restrict_events, | |
57 const GURL& event_url) { | |
58 ASSERT_FALSE(task_queue_.empty()); | |
59 MessageLoop::current()->PostTask(FROM_HERE, task_queue_.front()); | 57 MessageLoop::current()->PostTask(FROM_HERE, task_queue_.front()); |
60 task_queue_.pop(); | 58 task_queue_.pop(); |
| 59 |
| 60 return false; |
61 } | 61 } |
62 | 62 |
63 std::queue<Task*> task_queue_; | 63 std::queue<Task*> task_queue_; |
64 }; | 64 }; |
65 | 65 |
66 class ExtensionWebRequestTest : public testing::Test { | 66 class ExtensionWebRequestTest : public testing::Test { |
67 protected: | 67 protected: |
68 virtual void SetUp() { | 68 virtual void SetUp() { |
69 event_router_ = new TestEventRouter(); | 69 event_router_ = new ExtensionEventRouterForwarder(); |
70 enable_referrers_.Init( | 70 enable_referrers_.Init( |
71 prefs::kEnableReferrers, profile_.GetTestingPrefService(), NULL); | 71 prefs::kEnableReferrers, profile_.GetTestingPrefService(), NULL); |
72 network_delegate_.reset(new ChromeNetworkDelegate( | 72 network_delegate_.reset(new ChromeNetworkDelegate( |
73 event_router_.get(), profile_.GetRuntimeId(), | 73 event_router_.get(), NULL, profile_.GetRuntimeId(), |
74 &enable_referrers_)); | 74 &enable_referrers_)); |
75 context_ = new TestURLRequestContext(); | 75 context_ = new TestURLRequestContext(); |
76 context_->set_network_delegate(network_delegate_.get()); | 76 context_->set_network_delegate(network_delegate_.get()); |
77 } | 77 } |
78 | 78 |
79 MessageLoopForIO io_loop_; | 79 MessageLoopForIO io_loop_; |
80 TestingProfile profile_; | 80 TestingProfile profile_; |
81 TestDelegate delegate_; | 81 TestDelegate delegate_; |
82 BooleanPrefMember enable_referrers_; | 82 BooleanPrefMember enable_referrers_; |
83 scoped_refptr<TestEventRouter> event_router_; | 83 TestIPCSender ipc_sender_; |
| 84 scoped_refptr<ExtensionEventRouterForwarder> event_router_; |
| 85 scoped_refptr<ExtensionInfoMap> extension_info_map_; |
84 scoped_ptr<ChromeNetworkDelegate> network_delegate_; | 86 scoped_ptr<ChromeNetworkDelegate> network_delegate_; |
85 scoped_refptr<TestURLRequestContext> context_; | 87 scoped_refptr<TestURLRequestContext> context_; |
86 }; | 88 }; |
87 | 89 |
88 // Tests that we handle disagreements among extensions about responses to | 90 // Tests that we handle disagreements among extensions about responses to |
89 // blocking events by choosing the response from the most-recently-installed | 91 // blocking events by choosing the response from the most-recently-installed |
90 // extension. | 92 // extension. |
91 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedence) { | 93 TEST_F(ExtensionWebRequestTest, BlockingEventPrecedence) { |
92 std::string extension1_id("1"); | 94 std::string extension1_id("1"); |
93 std::string extension2_id("2"); | 95 std::string extension2_id("2"); |
94 ExtensionWebRequestEventRouter::RequestFilter filter; | 96 ExtensionWebRequestEventRouter::RequestFilter filter; |
95 const std::string kEventName(keys::kOnBeforeRequest); | 97 const std::string kEventName(keys::kOnBeforeRequest); |
| 98 base::WeakPtrFactory<TestIPCSender> ipc_sender_factory(&ipc_sender_); |
96 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 99 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
97 profile_.GetRuntimeId(), extension1_id, kEventName, | 100 profile_.GetRuntimeId(), extension1_id, kEventName, |
98 kEventName + "/1", filter, | 101 kEventName + "/1", filter, |
99 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING); | 102 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, |
| 103 ipc_sender_factory.GetWeakPtr()); |
100 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( | 104 ExtensionWebRequestEventRouter::GetInstance()->AddEventListener( |
101 profile_.GetRuntimeId(), extension2_id, kEventName, | 105 profile_.GetRuntimeId(), extension2_id, kEventName, |
102 kEventName + "/2", filter, | 106 kEventName + "/2", filter, |
103 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING); | 107 ExtensionWebRequestEventRouter::ExtraInfoSpec::BLOCKING, |
| 108 ipc_sender_factory.GetWeakPtr()); |
104 | 109 |
105 net::URLRequest request(GURL("about:blank"), &delegate_); | 110 net::URLRequest request(GURL("about:blank"), &delegate_); |
106 request.set_context(context_); | 111 request.set_context(context_); |
107 | 112 |
108 { | 113 { |
109 // onBeforeRequest will be dispatched twice initially. The second response - | 114 // onBeforeRequest will be dispatched twice initially. The second response - |
110 // the redirect - should win, since it has a later |install_time|. The | 115 // the redirect - should win, since it has a later |install_time|. The |
111 // redirect will dispatch another pair of onBeforeRequest. There, the first | 116 // redirect will dispatch another pair of onBeforeRequest. There, the first |
112 // response should win (later |install_time|). | 117 // response should win (later |install_time|). |
113 GURL redirect_url("about:redirected"); | 118 GURL redirect_url("about:redirected"); |
114 ExtensionWebRequestEventRouter::EventResponse* response = NULL; | 119 ExtensionWebRequestEventRouter::EventResponse* response = NULL; |
115 | 120 |
116 // Extension1 response. Arrives first, but ignored due to install_time. | 121 // Extension1 response. Arrives first, but ignored due to install_time. |
117 response = new ExtensionWebRequestEventRouter::EventResponse( | 122 response = new ExtensionWebRequestEventRouter::EventResponse( |
118 extension1_id, base::Time::FromDoubleT(1)); | 123 extension1_id, base::Time::FromDoubleT(1)); |
119 response->cancel = true; | 124 response->cancel = true; |
120 event_router_->PushTask( | 125 ipc_sender_.PushTask( |
121 NewRunnableFunction(&EventHandledOnIOThread, | 126 NewRunnableFunction(&EventHandledOnIOThread, |
122 profile_.GetRuntimeId(), extension1_id, | 127 profile_.GetRuntimeId(), extension1_id, |
123 kEventName, kEventName + "/1", request.identifier(), response)); | 128 kEventName, kEventName + "/1", request.identifier(), response)); |
124 | 129 |
125 // Extension2 response. Arrives second, and chosen because of install_time. | 130 // Extension2 response. Arrives second, and chosen because of install_time. |
126 response = new ExtensionWebRequestEventRouter::EventResponse( | 131 response = new ExtensionWebRequestEventRouter::EventResponse( |
127 extension2_id, base::Time::FromDoubleT(2)); | 132 extension2_id, base::Time::FromDoubleT(2)); |
128 response->new_url = redirect_url; | 133 response->new_url = redirect_url; |
129 event_router_->PushTask( | 134 ipc_sender_.PushTask( |
130 NewRunnableFunction(&EventHandledOnIOThread, | 135 NewRunnableFunction(&EventHandledOnIOThread, |
131 profile_.GetRuntimeId(), extension2_id, | 136 profile_.GetRuntimeId(), extension2_id, |
132 kEventName, kEventName + "/2", request.identifier(), response)); | 137 kEventName, kEventName + "/2", request.identifier(), response)); |
133 | 138 |
134 // Extension2 response to the redirected URL. Arrives first, and chosen. | 139 // Extension2 response to the redirected URL. Arrives first, and chosen. |
135 response = new ExtensionWebRequestEventRouter::EventResponse( | 140 response = new ExtensionWebRequestEventRouter::EventResponse( |
136 extension2_id, base::Time::FromDoubleT(2)); | 141 extension2_id, base::Time::FromDoubleT(2)); |
137 event_router_->PushTask( | 142 ipc_sender_.PushTask( |
138 NewRunnableFunction(&EventHandledOnIOThread, | 143 NewRunnableFunction(&EventHandledOnIOThread, |
139 profile_.GetRuntimeId(), extension2_id, | 144 profile_.GetRuntimeId(), extension2_id, |
140 kEventName, kEventName + "/2", request.identifier(), response)); | 145 kEventName, kEventName + "/2", request.identifier(), response)); |
141 | 146 |
142 // Extension1 response to the redirected URL. Arrives second, and ignored. | 147 // Extension1 response to the redirected URL. Arrives second, and ignored. |
143 response = new ExtensionWebRequestEventRouter::EventResponse( | 148 response = new ExtensionWebRequestEventRouter::EventResponse( |
144 extension1_id, base::Time::FromDoubleT(1)); | 149 extension1_id, base::Time::FromDoubleT(1)); |
145 response->cancel = true; | 150 response->cancel = true; |
146 event_router_->PushTask( | 151 ipc_sender_.PushTask( |
147 NewRunnableFunction(&EventHandledOnIOThread, | 152 NewRunnableFunction(&EventHandledOnIOThread, |
148 profile_.GetRuntimeId(), extension1_id, | 153 profile_.GetRuntimeId(), extension1_id, |
149 kEventName, kEventName + "/1", request.identifier(), response)); | 154 kEventName, kEventName + "/1", request.identifier(), response)); |
150 | 155 |
151 request.Start(); | 156 request.Start(); |
152 MessageLoop::current()->Run(); | 157 MessageLoop::current()->Run(); |
153 | 158 |
154 EXPECT_TRUE(!request.is_pending()); | 159 EXPECT_TRUE(!request.is_pending()); |
155 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 160 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
156 EXPECT_EQ(0, request.status().os_error()); | 161 EXPECT_EQ(0, request.status().os_error()); |
157 EXPECT_EQ(redirect_url, request.url()); | 162 EXPECT_EQ(redirect_url, request.url()); |
158 EXPECT_EQ(2U, request.url_chain().size()); | 163 EXPECT_EQ(2U, request.url_chain().size()); |
159 EXPECT_EQ(0U, event_router_->GetNumTasks()); | 164 EXPECT_EQ(0U, ipc_sender_.GetNumTasks()); |
160 } | 165 } |
161 } | 166 } |
OLD | NEW |