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/buffered_resource_handler.h" | 5 #include "content/browser/loader/buffered_resource_handler.h" |
6 | 6 |
7 #include "base/files/file_path.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/macros.h" | 9 #include "base/macros.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 11 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
11 #include "content/public/browser/resource_controller.h" | 12 #include "content/public/browser/resource_controller.h" |
12 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 13 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
13 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
14 #include "content/public/common/resource_response.h" | 15 #include "content/public/common/resource_response.h" |
16 #include "content/public/common/webplugininfo.h" | |
15 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
16 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" |
17 #include "content/test/fake_plugin_service.h" | 19 #include "content/test/fake_plugin_service.h" |
18 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "url/gurl.h" | 22 #include "url/gurl.h" |
21 | 23 |
22 namespace content { | 24 namespace content { |
23 | 25 |
24 namespace { | 26 namespace { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 net::URLRequest* request, | 94 net::URLRequest* request, |
93 bool is_content_initiated, | 95 bool is_content_initiated, |
94 bool must_download, | 96 bool must_download, |
95 uint32 id, | 97 uint32 id, |
96 scoped_ptr<DownloadSaveInfo> save_info, | 98 scoped_ptr<DownloadSaveInfo> save_info, |
97 const DownloadUrlParameters::OnStartedCallback& started_cb) override { | 99 const DownloadUrlParameters::OnStartedCallback& started_cb) override { |
98 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | 100 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
99 } | 101 } |
100 | 102 |
101 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( | 103 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( |
104 const base::FilePath& plugin_path, | |
102 net::URLRequest* request, | 105 net::URLRequest* request, |
103 ResourceResponse* response, | 106 ResourceResponse* response, |
104 std::string* payload) override { | 107 std::string* payload) override { |
mmenke
2015/03/30 01:17:38
Could you add an EXPECT that this method is only i
Deepak
2015/04/07 10:21:16
On 2015/03/30 01:17:38, mmenke wrote:
Done.
| |
105 if (stream_has_handler_) { | 108 if (stream_has_handler_) { |
106 intercepted_as_stream_ = true; | 109 intercepted_as_stream_ = true; |
107 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | 110 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
108 } else { | 111 } else { |
109 return scoped_ptr<ResourceHandler>(); | 112 return scoped_ptr<ResourceHandler>(); |
110 } | 113 } |
111 } | 114 } |
112 | 115 |
113 private: | 116 private: |
114 // Whether the URL request should be intercepted as a stream. | 117 // Whether the URL request should be intercepted as a stream. |
(...skipping 29 matching lines...) Expand all Loading... | |
144 | 147 |
145 void CancelWithError(int error_code) override { | 148 void CancelWithError(int error_code) override { |
146 NOTREACHED(); | 149 NOTREACHED(); |
147 } | 150 } |
148 | 151 |
149 void Resume() override { | 152 void Resume() override { |
150 NOTREACHED(); | 153 NOTREACHED(); |
151 } | 154 } |
152 }; | 155 }; |
153 | 156 |
157 class TestFakePluginService : public FakePluginService { | |
158 public: | |
159 TestFakePluginService(bool plugin_available, bool is_plugin_stale) | |
160 : plugin_available_(plugin_available), | |
161 is_plugin_stale_(is_plugin_stale) {} | |
162 | |
163 bool GetPluginInfo(int render_process_id, | |
164 int render_frame_id, | |
165 ResourceContext* context, | |
166 const GURL& url, | |
167 const GURL& page_url, | |
168 const std::string& mime_type, | |
169 bool allow_wildcard, | |
170 bool* is_stale, | |
171 WebPluginInfo* info, | |
172 std::string* actual_mime_type) override { | |
173 *is_stale = is_plugin_stale_; | |
raymes
2015/03/30 01:00:07
nit: if it's stale, we should return false
Deepak
2015/04/07 10:21:16
On 2015/03/30 01:00:07, raymes wrote:
Done.
| |
174 if (!plugin_available_) | |
175 return false; | |
176 info->type = WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN; | |
177 info->path = base::FilePath::FromUTF8Unsafe( | |
178 std::string("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/")); | |
179 return true; | |
180 } | |
181 | |
182 void GetPlugins(const GetPluginsCallback& callback) override { | |
183 is_plugin_stale_ = false; | |
184 scoped_refptr<base::MessageLoopProxy> target_loop( | |
185 base::MessageLoop::current()->message_loop_proxy()); | |
186 std::vector<WebPluginInfo> plugins; | |
raymes
2015/03/30 01:00:07
We should add a valid plugin here to make sure the
Deepak
2015/04/07 10:21:16
On 2015/03/30 01:00:07, raymes wrote:
Actually on
| |
187 target_loop->PostTask(FROM_HERE, base::Bind(callback, plugins)); | |
raymes
2015/03/30 01:00:07
it should be sufficient to use base::MessageLoop::
Deepak
2015/04/07 10:21:16
On 2015/03/30 01:00:07, raymes wrote:
Done.
| |
188 } | |
189 | |
190 private: | |
191 const bool plugin_available_; | |
192 bool is_plugin_stale_; | |
193 | |
194 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); | |
195 }; | |
196 | |
154 class BufferedResourceHandlerTest : public testing::Test { | 197 class BufferedResourceHandlerTest : public testing::Test { |
155 public: | 198 public: |
156 BufferedResourceHandlerTest() : stream_has_handler_(false) {} | 199 BufferedResourceHandlerTest() |
200 : stream_has_handler_(false), | |
201 plugin_available_(false), | |
202 plugin_stale_(false) {} | |
157 | 203 |
158 void set_stream_has_handler(bool stream_has_handler) { | 204 void set_stream_has_handler(bool stream_has_handler) { |
159 stream_has_handler_ = stream_has_handler; | 205 stream_has_handler_ = stream_has_handler; |
160 } | 206 } |
161 | 207 |
208 void set_plugin_available(bool plugin_available) { | |
209 plugin_available_ = plugin_available; | |
210 } | |
211 | |
212 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } | |
213 | |
162 bool TestStreamIsIntercepted(bool allow_download, | 214 bool TestStreamIsIntercepted(bool allow_download, |
163 bool must_download, | 215 bool must_download, |
164 ResourceType request_resource_type); | 216 ResourceType request_resource_type); |
165 | 217 |
166 private: | 218 private: |
167 // Whether the URL request should be intercepted as a stream. | 219 // Whether the URL request should be intercepted as a stream. |
168 bool stream_has_handler_; | 220 bool stream_has_handler_; |
221 bool plugin_available_; | |
222 bool plugin_stale_; | |
169 | 223 |
170 TestBrowserThreadBundle thread_bundle_; | 224 TestBrowserThreadBundle thread_bundle_; |
171 }; | 225 }; |
172 | 226 |
173 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( | 227 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( |
174 bool allow_download, | 228 bool allow_download, |
175 bool must_download, | 229 bool must_download, |
176 ResourceType request_resource_type) { | 230 ResourceType request_resource_type) { |
177 net::URLRequestContext context; | 231 net::URLRequestContext context; |
178 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 232 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
179 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 233 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
180 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | 234 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; |
181 ResourceRequestInfo::AllocateForTesting( | 235 ResourceRequestInfo::AllocateForTesting( |
182 request.get(), | 236 request.get(), |
183 request_resource_type, | 237 request_resource_type, |
184 nullptr, // context | 238 nullptr, // context |
185 0, // render_process_id | 239 0, // render_process_id |
186 0, // render_view_id | 240 0, // render_view_id |
187 0, // render_frame_id | 241 0, // render_frame_id |
188 is_main_frame, // is_main_frame | 242 is_main_frame, // is_main_frame |
189 false, // parent_is_main_frame | 243 false, // parent_is_main_frame |
190 allow_download, // allow_download | 244 allow_download, // allow_download |
191 true); // is_async | 245 true); // is_async |
192 | 246 |
193 TestResourceDispatcherHost host(stream_has_handler_); | 247 TestResourceDispatcherHost host(stream_has_handler_); |
194 TestResourceDispatcherHostDelegate host_delegate(must_download); | 248 TestResourceDispatcherHostDelegate host_delegate(must_download); |
195 host.SetDelegate(&host_delegate); | 249 host.SetDelegate(&host_delegate); |
196 | 250 |
197 FakePluginService plugin_service; | 251 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
198 scoped_ptr<ResourceHandler> buffered_handler( | 252 scoped_ptr<ResourceHandler> buffered_handler( |
199 new BufferedResourceHandler( | 253 new BufferedResourceHandler( |
200 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), | 254 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), |
201 &host, | 255 &host, |
202 &plugin_service, | 256 &plugin_service, |
203 request.get())); | 257 request.get())); |
204 TestResourceController resource_controller; | 258 TestResourceController resource_controller; |
205 buffered_handler->SetController(&resource_controller); | 259 buffered_handler->SetController(&resource_controller); |
206 | 260 |
207 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 261 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
208 // The MIME type isn't important but it shouldn't be empty. | 262 // The MIME type isn't important but it shouldn't be empty. |
209 response->head.mime_type = "application/pdf"; | 263 response->head.mime_type = "application/pdf"; |
210 | 264 |
211 bool defer = false; | 265 bool defer = false; |
212 buffered_handler->OnResponseStarted(response.get(), &defer); | 266 buffered_handler->OnResponseStarted(response.get(), &defer); |
213 | 267 |
214 content::RunAllPendingInMessageLoop(); | 268 content::RunAllPendingInMessageLoop(); |
215 | 269 |
216 return host.intercepted_as_stream(); | 270 return host.intercepted_as_stream(); |
217 } | 271 } |
218 | 272 |
219 // Test that stream requests are correctly intercepted under the right | 273 // Test that stream requests are correctly intercepted under the right |
220 // circumstances. | 274 // circumstances. Test is not relevent for Android OS as plugin is disabled and |
275 // always returns false. | |
276 #if !defined(OS_ANDROID) | |
221 TEST_F(BufferedResourceHandlerTest, StreamHandling) { | 277 TEST_F(BufferedResourceHandlerTest, StreamHandling) { |
222 bool allow_download; | 278 bool allow_download; |
223 bool must_download; | 279 bool must_download; |
224 ResourceType resource_type; | 280 ResourceType resource_type; |
225 | 281 |
226 // Ensure the stream is handled by MaybeInterceptAsStream in the | 282 // Ensure the stream is handled by MaybeInterceptAsStream in the |
227 // ResourceDispatcherHost. | 283 // ResourceDispatcherHost. |
228 set_stream_has_handler(true); | 284 set_stream_has_handler(true); |
285 set_plugin_available(true); | |
229 | 286 |
230 // Main frame request with no download allowed. Stream shouldn't be | 287 // Main frame request with no download allowed. Stream shouldn't be |
231 // intercepted. | 288 // intercepted. |
232 allow_download = false; | 289 allow_download = false; |
233 must_download = false; | 290 must_download = false; |
234 resource_type = RESOURCE_TYPE_MAIN_FRAME; | 291 resource_type = RESOURCE_TYPE_MAIN_FRAME; |
235 EXPECT_FALSE( | 292 EXPECT_FALSE( |
236 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 293 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
237 | 294 |
238 // Main frame request with download allowed. Stream should be intercepted. | 295 // Main frame request with download allowed. Stream should be intercepted. |
(...skipping 21 matching lines...) Expand all Loading... | |
260 // Object request with download not allowed. Stream should be intercepted. | 317 // Object request with download not allowed. Stream should be intercepted. |
261 allow_download = false; | 318 allow_download = false; |
262 must_download = false; | 319 must_download = false; |
263 resource_type = RESOURCE_TYPE_OBJECT; | 320 resource_type = RESOURCE_TYPE_OBJECT; |
264 EXPECT_TRUE( | 321 EXPECT_TRUE( |
265 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 322 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
266 | 323 |
267 // Test the cases where the stream isn't handled by MaybeInterceptAsStream | 324 // Test the cases where the stream isn't handled by MaybeInterceptAsStream |
268 // in the ResourceDispatcherHost. | 325 // in the ResourceDispatcherHost. |
269 set_stream_has_handler(false); | 326 set_stream_has_handler(false); |
270 | |
271 allow_download = false; | 327 allow_download = false; |
272 must_download = false; | 328 must_download = false; |
273 resource_type = RESOURCE_TYPE_OBJECT; | 329 resource_type = RESOURCE_TYPE_OBJECT; |
274 EXPECT_FALSE( | 330 EXPECT_FALSE( |
275 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 331 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
276 | 332 |
277 allow_download = true; | 333 allow_download = true; |
278 must_download = false; | 334 must_download = false; |
279 resource_type = RESOURCE_TYPE_MAIN_FRAME; | 335 resource_type = RESOURCE_TYPE_MAIN_FRAME; |
280 EXPECT_FALSE( | 336 EXPECT_FALSE( |
281 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 337 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
338 | |
339 // Test the cases where the stream handled by MaybeInterceptAsStream | |
340 // with plugin not available. This is the case when intercepting streams for | |
341 // the streamsPrivate extensions API. | |
342 set_stream_has_handler(true); | |
343 set_plugin_available(false); | |
344 allow_download = false; | |
345 must_download = false; | |
346 resource_type = RESOURCE_TYPE_OBJECT; | |
347 EXPECT_TRUE( | |
348 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
349 | |
350 // Test the cases where the stream handled by MaybeInterceptAsStream | |
351 // with plugin not available. This is the case when intercepting streams for | |
352 // the streamsPrivate extensions API with stale plugin. | |
353 set_plugin_stale(true); | |
354 allow_download = false; | |
355 must_download = false; | |
356 resource_type = RESOURCE_TYPE_OBJECT; | |
357 EXPECT_TRUE( | |
358 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
282 } | 359 } |
360 #endif | |
283 | 361 |
284 } // namespace | 362 } // namespace |
285 | 363 |
286 } // namespace content | 364 } // namespace content |
OLD | NEW |