Chromium Code Reviews| 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/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 10 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 net::URLRequest* request, | 92 net::URLRequest* request, |
| 93 bool is_content_initiated, | 93 bool is_content_initiated, |
| 94 bool must_download, | 94 bool must_download, |
| 95 uint32 id, | 95 uint32 id, |
| 96 scoped_ptr<DownloadSaveInfo> save_info, | 96 scoped_ptr<DownloadSaveInfo> save_info, |
| 97 const DownloadUrlParameters::OnStartedCallback& started_cb) override { | 97 const DownloadUrlParameters::OnStartedCallback& started_cb) override { |
| 98 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | 98 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
| 99 } | 99 } |
| 100 | 100 |
| 101 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( | 101 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( |
| 102 const base::FilePath& plugin_path, | |
| 102 net::URLRequest* request, | 103 net::URLRequest* request, |
| 103 ResourceResponse* response, | 104 ResourceResponse* response, |
| 104 std::string* payload) override { | 105 std::string* payload) override { |
| 105 if (stream_has_handler_) { | 106 if (stream_has_handler_) { |
| 106 intercepted_as_stream_ = true; | 107 intercepted_as_stream_ = true; |
| 107 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | 108 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
| 108 } else { | 109 } else { |
| 109 return scoped_ptr<ResourceHandler>(); | 110 return scoped_ptr<ResourceHandler>(); |
| 110 } | 111 } |
| 111 } | 112 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 146 NOTREACHED(); | 147 NOTREACHED(); |
| 147 } | 148 } |
| 148 | 149 |
| 149 void Resume() override { | 150 void Resume() override { |
| 150 NOTREACHED(); | 151 NOTREACHED(); |
| 151 } | 152 } |
| 152 }; | 153 }; |
| 153 | 154 |
| 154 class BufferedResourceHandlerTest : public testing::Test { | 155 class BufferedResourceHandlerTest : public testing::Test { |
| 155 public: | 156 public: |
| 156 BufferedResourceHandlerTest() : stream_has_handler_(false) {} | 157 BufferedResourceHandlerTest() |
| 158 : stream_has_handler_(false), plugin_available_(false) {} | |
| 157 | 159 |
| 158 void set_stream_has_handler(bool stream_has_handler) { | 160 void set_stream_has_handler(bool stream_has_handler) { |
| 159 stream_has_handler_ = stream_has_handler; | 161 stream_has_handler_ = stream_has_handler; |
| 160 } | 162 } |
| 161 | 163 |
| 164 void set_plugin_available(bool plugin_available) { | |
| 165 plugin_available_ = plugin_available; | |
| 166 } | |
| 167 | |
| 162 bool TestStreamIsIntercepted(bool allow_download, | 168 bool TestStreamIsIntercepted(bool allow_download, |
| 163 bool must_download, | 169 bool must_download, |
| 164 ResourceType request_resource_type); | 170 ResourceType request_resource_type); |
| 165 | 171 |
| 166 private: | 172 private: |
| 167 // Whether the URL request should be intercepted as a stream. | 173 // Whether the URL request should be intercepted as a stream. |
| 168 bool stream_has_handler_; | 174 bool stream_has_handler_; |
| 175 bool plugin_available_; | |
| 169 | 176 |
| 170 TestBrowserThreadBundle thread_bundle_; | 177 TestBrowserThreadBundle thread_bundle_; |
| 171 }; | 178 }; |
| 172 | 179 |
| 173 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( | 180 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( |
| 174 bool allow_download, | 181 bool allow_download, |
| 175 bool must_download, | 182 bool must_download, |
| 176 ResourceType request_resource_type) { | 183 ResourceType request_resource_type) { |
| 177 net::URLRequestContext context; | 184 net::URLRequestContext context; |
| 178 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 185 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
| 179 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr, nullptr)); | 186 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr, nullptr)); |
| 180 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | 187 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; |
| 181 ResourceRequestInfo::AllocateForTesting( | 188 ResourceRequestInfo::AllocateForTesting( |
| 182 request.get(), | 189 request.get(), |
| 183 request_resource_type, | 190 request_resource_type, |
| 184 nullptr, // context | 191 nullptr, // context |
| 185 0, // render_process_id | 192 0, // render_process_id |
| 186 0, // render_view_id | 193 0, // render_view_id |
| 187 0, // render_frame_id | 194 0, // render_frame_id |
| 188 is_main_frame, // is_main_frame | 195 is_main_frame, // is_main_frame |
| 189 false, // parent_is_main_frame | 196 false, // parent_is_main_frame |
| 190 allow_download, // allow_download | 197 allow_download, // allow_download |
| 191 true); // is_async | 198 true); // is_async |
| 192 | 199 |
| 193 TestResourceDispatcherHost host(stream_has_handler_); | 200 TestResourceDispatcherHost host(stream_has_handler_); |
| 194 TestResourceDispatcherHostDelegate host_delegate(must_download); | 201 TestResourceDispatcherHostDelegate host_delegate(must_download); |
| 195 host.SetDelegate(&host_delegate); | 202 host.SetDelegate(&host_delegate); |
| 196 | 203 |
| 197 FakePluginService plugin_service; | 204 FakePluginService plugin_service(plugin_available_); |
| 198 scoped_ptr<ResourceHandler> buffered_handler( | 205 scoped_ptr<ResourceHandler> buffered_handler( |
| 199 new BufferedResourceHandler( | 206 new BufferedResourceHandler( |
| 200 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), | 207 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), |
| 201 &host, | 208 &host, |
| 202 &plugin_service, | 209 &plugin_service, |
| 203 request.get())); | 210 request.get())); |
| 204 TestResourceController resource_controller; | 211 TestResourceController resource_controller; |
| 205 buffered_handler->SetController(&resource_controller); | 212 buffered_handler->SetController(&resource_controller); |
| 206 | 213 |
| 207 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 214 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 208 // The MIME type isn't important but it shouldn't be empty. | 215 // The MIME type isn't important but it shouldn't be empty. |
| 209 response->head.mime_type = "application/pdf"; | 216 response->head.mime_type = "application/pdf"; |
| 210 | 217 |
| 211 bool defer = false; | 218 bool defer = false; |
| 212 buffered_handler->OnResponseStarted(response.get(), &defer); | 219 buffered_handler->OnResponseStarted(response.get(), &defer); |
| 213 | 220 |
| 214 content::RunAllPendingInMessageLoop(); | 221 content::RunAllPendingInMessageLoop(); |
| 215 | 222 |
| 216 return host.intercepted_as_stream(); | 223 return host.intercepted_as_stream(); |
| 217 } | 224 } |
| 218 | 225 |
| 219 // Test that stream requests are correctly intercepted under the right | 226 // Test that stream requests are correctly intercepted under the right |
| 220 // circumstances. | 227 // circumstances. |
| 221 TEST_F(BufferedResourceHandlerTest, StreamHandling) { | 228 TEST_F(BufferedResourceHandlerTest, StreamHandling) { |
| 222 bool allow_download; | 229 bool allow_download; |
| 223 bool must_download; | 230 bool must_download; |
| 224 ResourceType resource_type; | 231 ResourceType resource_type; |
| 225 | 232 |
| 233 set_plugin_available(false); | |
|
raymes
2015/03/19 00:45:51
Let's set the plugin as available for most of the
Deepak
2015/03/19 10:22:23
Done.
| |
| 226 // Ensure the stream is handled by MaybeInterceptAsStream in the | 234 // Ensure the stream is handled by MaybeInterceptAsStream in the |
| 227 // ResourceDispatcherHost. | 235 // ResourceDispatcherHost. |
| 228 set_stream_has_handler(true); | 236 set_stream_has_handler(true); |
| 229 | 237 |
| 230 // Main frame request with no download allowed. Stream shouldn't be | 238 // Main frame request with no download allowed. Stream shouldn't be |
| 231 // intercepted. | 239 // intercepted. |
| 232 allow_download = false; | 240 allow_download = false; |
| 233 must_download = false; | 241 must_download = false; |
| 234 resource_type = RESOURCE_TYPE_MAIN_FRAME; | 242 resource_type = RESOURCE_TYPE_MAIN_FRAME; |
| 235 EXPECT_FALSE( | 243 EXPECT_FALSE( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 EXPECT_FALSE( | 282 EXPECT_FALSE( |
| 275 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 283 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 276 | 284 |
| 277 allow_download = true; | 285 allow_download = true; |
| 278 must_download = false; | 286 must_download = false; |
| 279 resource_type = RESOURCE_TYPE_MAIN_FRAME; | 287 resource_type = RESOURCE_TYPE_MAIN_FRAME; |
| 280 EXPECT_FALSE( | 288 EXPECT_FALSE( |
| 281 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 289 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 282 } | 290 } |
| 283 | 291 |
| 292 // Test that stream requests are correctly intercepted under the right | |
| 293 // circumstances with plugin path. | |
| 294 TEST_F(BufferedResourceHandlerTest, StreamHandlingWithPluginPath) { | |
|
raymes
2015/03/19 00:39:22
I think it would be sufficient to add a single add
Deepak
2015/03/19 10:22:23
Done.
| |
| 295 bool allow_download; | |
| 296 bool must_download; | |
| 297 ResourceType resource_type; | |
| 298 | |
| 299 // Ensure the stream is handled by MaybeInterceptAsStream in the | |
| 300 // ResourceDispatcherHost. | |
| 301 set_stream_has_handler(true); | |
| 302 | |
| 303 allow_download = false; | |
| 304 must_download = false; | |
| 305 resource_type = RESOURCE_TYPE_OBJECT; | |
| 306 set_plugin_available(true); | |
| 307 EXPECT_TRUE( | |
| 308 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 309 | |
| 310 allow_download = false; | |
| 311 must_download = false; | |
| 312 resource_type = RESOURCE_TYPE_OBJECT; | |
| 313 set_plugin_available(false); | |
| 314 EXPECT_TRUE( | |
| 315 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 316 | |
| 317 // Test the cases where the stream isn't handled by MaybeInterceptAsStream | |
| 318 // in the ResourceDispatcherHost. | |
| 319 set_stream_has_handler(false); | |
| 320 | |
| 321 allow_download = false; | |
| 322 must_download = false; | |
| 323 resource_type = RESOURCE_TYPE_OBJECT; | |
| 324 set_plugin_available(true); | |
| 325 EXPECT_FALSE( | |
| 326 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 327 | |
| 328 allow_download = false; | |
| 329 must_download = false; | |
| 330 resource_type = RESOURCE_TYPE_OBJECT; | |
| 331 set_plugin_available(false); | |
| 332 EXPECT_FALSE( | |
| 333 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 334 } | |
| 335 | |
| 284 } // namespace | 336 } // namespace |
| 285 | 337 |
| 286 } // namespace content | 338 } // namespace content |
| OLD | NEW |