| 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" | |
| 8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 9 #include "base/macros.h" | 8 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 11 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 10 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 12 #include "content/public/browser/resource_controller.h" | 11 #include "content/public/browser/resource_controller.h" |
| 13 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 12 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 14 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
| 15 #include "content/public/common/resource_response.h" | 14 #include "content/public/common/resource_response.h" |
| 16 #include "content/public/common/webplugininfo.h" | |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
| 19 #include "content/test/fake_plugin_service.h" | 17 #include "content/test/fake_plugin_service.h" |
| 20 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 23 | 21 |
| 24 namespace content { | 22 namespace content { |
| 25 | 23 |
| 26 namespace { | 24 namespace { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 } | 77 } |
| 80 | 78 |
| 81 private: | 79 private: |
| 82 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); | 80 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); |
| 83 }; | 81 }; |
| 84 | 82 |
| 85 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { | 83 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { |
| 86 public: | 84 public: |
| 87 explicit TestResourceDispatcherHost(bool stream_has_handler) | 85 explicit TestResourceDispatcherHost(bool stream_has_handler) |
| 88 : stream_has_handler_(stream_has_handler), | 86 : stream_has_handler_(stream_has_handler), |
| 89 intercepted_as_stream_(false), | 87 intercepted_as_stream_(false) {} |
| 90 intercepted_as_stream_count_(0) {} | |
| 91 | 88 |
| 92 bool intercepted_as_stream() const { return intercepted_as_stream_; } | 89 bool intercepted_as_stream() const { return intercepted_as_stream_; } |
| 93 | 90 |
| 94 scoped_ptr<ResourceHandler> CreateResourceHandlerForDownload( | 91 scoped_ptr<ResourceHandler> CreateResourceHandlerForDownload( |
| 95 net::URLRequest* request, | 92 net::URLRequest* request, |
| 96 bool is_content_initiated, | 93 bool is_content_initiated, |
| 97 bool must_download, | 94 bool must_download, |
| 98 uint32 id, | 95 uint32 id, |
| 99 scoped_ptr<DownloadSaveInfo> save_info, | 96 scoped_ptr<DownloadSaveInfo> save_info, |
| 100 const DownloadUrlParameters::OnStartedCallback& started_cb) override { | 97 const DownloadUrlParameters::OnStartedCallback& started_cb) override { |
| 101 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | 98 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
| 102 } | 99 } |
| 103 | 100 |
| 104 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( | 101 scoped_ptr<ResourceHandler> MaybeInterceptAsStream( |
| 105 const base::FilePath& plugin_path, | |
| 106 net::URLRequest* request, | 102 net::URLRequest* request, |
| 107 ResourceResponse* response, | 103 ResourceResponse* response, |
| 108 std::string* payload) override { | 104 std::string* payload) override { |
| 109 intercepted_as_stream_count_++; | |
| 110 if (stream_has_handler_) { | 105 if (stream_has_handler_) { |
| 111 intercepted_as_stream_ = true; | 106 intercepted_as_stream_ = true; |
| 112 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); | 107 return scoped_ptr<ResourceHandler>(new TestResourceHandler).Pass(); |
| 113 } else { | 108 } else { |
| 114 return scoped_ptr<ResourceHandler>(); | 109 return scoped_ptr<ResourceHandler>(); |
| 115 } | 110 } |
| 116 } | 111 } |
| 117 | 112 |
| 118 int intercepted_as_stream_count() const { | |
| 119 return intercepted_as_stream_count_; | |
| 120 } | |
| 121 | |
| 122 private: | 113 private: |
| 123 // Whether the URL request should be intercepted as a stream. | 114 // Whether the URL request should be intercepted as a stream. |
| 124 bool stream_has_handler_; | 115 bool stream_has_handler_; |
| 125 | 116 |
| 126 // Whether the URL request has been intercepted as a stream. | 117 // Whether the URL request has been intercepted as a stream. |
| 127 bool intercepted_as_stream_; | 118 bool intercepted_as_stream_; |
| 128 | |
| 129 // Count of number of times MaybeInterceptAsStream function get called in a | |
| 130 // test. | |
| 131 int intercepted_as_stream_count_; | |
| 132 }; | 119 }; |
| 133 | 120 |
| 134 class TestResourceDispatcherHostDelegate | 121 class TestResourceDispatcherHostDelegate |
| 135 : public ResourceDispatcherHostDelegate { | 122 : public ResourceDispatcherHostDelegate { |
| 136 public: | 123 public: |
| 137 TestResourceDispatcherHostDelegate(bool must_download) | 124 TestResourceDispatcherHostDelegate(bool must_download) |
| 138 : must_download_(must_download) { | 125 : must_download_(must_download) { |
| 139 } | 126 } |
| 140 | 127 |
| 141 bool ShouldForceDownloadResource(const GURL& url, | 128 bool ShouldForceDownloadResource(const GURL& url, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 157 | 144 |
| 158 void CancelWithError(int error_code) override { | 145 void CancelWithError(int error_code) override { |
| 159 NOTREACHED(); | 146 NOTREACHED(); |
| 160 } | 147 } |
| 161 | 148 |
| 162 void Resume() override { | 149 void Resume() override { |
| 163 NOTREACHED(); | 150 NOTREACHED(); |
| 164 } | 151 } |
| 165 }; | 152 }; |
| 166 | 153 |
| 167 class TestFakePluginService : public FakePluginService { | |
| 168 public: | |
| 169 // If |is_plugin_stale| is true, GetPluginInfo will indicate the plugins are | |
| 170 // stale until GetPlugins is called. | |
| 171 TestFakePluginService(bool plugin_available, bool is_plugin_stale) | |
| 172 : plugin_available_(plugin_available), | |
| 173 is_plugin_stale_(is_plugin_stale) {} | |
| 174 | |
| 175 bool GetPluginInfo(int render_process_id, | |
| 176 int render_frame_id, | |
| 177 ResourceContext* context, | |
| 178 const GURL& url, | |
| 179 const GURL& page_url, | |
| 180 const std::string& mime_type, | |
| 181 bool allow_wildcard, | |
| 182 bool* is_stale, | |
| 183 WebPluginInfo* info, | |
| 184 std::string* actual_mime_type) override { | |
| 185 *is_stale = is_plugin_stale_; | |
| 186 if (!is_plugin_stale_ || !plugin_available_) | |
| 187 return false; | |
| 188 info->type = WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN; | |
| 189 info->path = base::FilePath::FromUTF8Unsafe( | |
| 190 std::string("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/")); | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 void GetPlugins(const GetPluginsCallback& callback) override { | |
| 195 is_plugin_stale_ = false; | |
| 196 std::vector<WebPluginInfo> plugins; | |
| 197 base::MessageLoop::current()->PostTask(FROM_HERE, | |
| 198 base::Bind(callback, plugins)); | |
| 199 } | |
| 200 | |
| 201 private: | |
| 202 const bool plugin_available_; | |
| 203 bool is_plugin_stale_; | |
| 204 | |
| 205 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); | |
| 206 }; | |
| 207 | |
| 208 class BufferedResourceHandlerTest : public testing::Test { | 154 class BufferedResourceHandlerTest : public testing::Test { |
| 209 public: | 155 public: |
| 210 BufferedResourceHandlerTest() | 156 BufferedResourceHandlerTest() : stream_has_handler_(false) {} |
| 211 : stream_has_handler_(false), | |
| 212 plugin_available_(false), | |
| 213 plugin_stale_(false) {} | |
| 214 | 157 |
| 215 void set_stream_has_handler(bool stream_has_handler) { | 158 void set_stream_has_handler(bool stream_has_handler) { |
| 216 stream_has_handler_ = stream_has_handler; | 159 stream_has_handler_ = stream_has_handler; |
| 217 } | 160 } |
| 218 | 161 |
| 219 void set_plugin_available(bool plugin_available) { | |
| 220 plugin_available_ = plugin_available; | |
| 221 } | |
| 222 | |
| 223 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } | |
| 224 | |
| 225 bool TestStreamIsIntercepted(bool allow_download, | 162 bool TestStreamIsIntercepted(bool allow_download, |
| 226 bool must_download, | 163 bool must_download, |
| 227 ResourceType request_resource_type); | 164 ResourceType request_resource_type); |
| 228 | 165 |
| 229 private: | 166 private: |
| 230 // Whether the URL request should be intercepted as a stream. | 167 // Whether the URL request should be intercepted as a stream. |
| 231 bool stream_has_handler_; | 168 bool stream_has_handler_; |
| 232 bool plugin_available_; | |
| 233 bool plugin_stale_; | |
| 234 | 169 |
| 235 TestBrowserThreadBundle thread_bundle_; | 170 TestBrowserThreadBundle thread_bundle_; |
| 236 }; | 171 }; |
| 237 | 172 |
| 238 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( | 173 bool BufferedResourceHandlerTest::TestStreamIsIntercepted( |
| 239 bool allow_download, | 174 bool allow_download, |
| 240 bool must_download, | 175 bool must_download, |
| 241 ResourceType request_resource_type) { | 176 ResourceType request_resource_type) { |
| 242 net::URLRequestContext context; | 177 net::URLRequestContext context; |
| 243 scoped_ptr<net::URLRequest> request(context.CreateRequest( | 178 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
| 244 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 179 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 245 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | 180 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; |
| 246 ResourceRequestInfo::AllocateForTesting( | 181 ResourceRequestInfo::AllocateForTesting( |
| 247 request.get(), | 182 request.get(), |
| 248 request_resource_type, | 183 request_resource_type, |
| 249 nullptr, // context | 184 nullptr, // context |
| 250 0, // render_process_id | 185 0, // render_process_id |
| 251 0, // render_view_id | 186 0, // render_view_id |
| 252 0, // render_frame_id | 187 0, // render_frame_id |
| 253 is_main_frame, // is_main_frame | 188 is_main_frame, // is_main_frame |
| 254 false, // parent_is_main_frame | 189 false, // parent_is_main_frame |
| 255 allow_download, // allow_download | 190 allow_download, // allow_download |
| 256 true); // is_async | 191 true); // is_async |
| 257 | 192 |
| 258 TestResourceDispatcherHost host(stream_has_handler_); | 193 TestResourceDispatcherHost host(stream_has_handler_); |
| 259 TestResourceDispatcherHostDelegate host_delegate(must_download); | 194 TestResourceDispatcherHostDelegate host_delegate(must_download); |
| 260 host.SetDelegate(&host_delegate); | 195 host.SetDelegate(&host_delegate); |
| 261 | 196 |
| 262 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 197 FakePluginService plugin_service; |
| 263 scoped_ptr<ResourceHandler> buffered_handler( | 198 scoped_ptr<ResourceHandler> buffered_handler( |
| 264 new BufferedResourceHandler( | 199 new BufferedResourceHandler( |
| 265 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), | 200 scoped_ptr<ResourceHandler>(new TestResourceHandler()).Pass(), |
| 266 &host, | 201 &host, |
| 267 &plugin_service, | 202 &plugin_service, |
| 268 request.get())); | 203 request.get())); |
| 269 TestResourceController resource_controller; | 204 TestResourceController resource_controller; |
| 270 buffered_handler->SetController(&resource_controller); | 205 buffered_handler->SetController(&resource_controller); |
| 271 | 206 |
| 272 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 207 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 273 // The MIME type isn't important but it shouldn't be empty. | 208 // The MIME type isn't important but it shouldn't be empty. |
| 274 response->head.mime_type = "application/pdf"; | 209 response->head.mime_type = "application/pdf"; |
| 275 | 210 |
| 276 bool defer = false; | 211 bool defer = false; |
| 277 buffered_handler->OnResponseStarted(response.get(), &defer); | 212 buffered_handler->OnResponseStarted(response.get(), &defer); |
| 278 | 213 |
| 279 content::RunAllPendingInMessageLoop(); | 214 content::RunAllPendingInMessageLoop(); |
| 280 EXPECT_LT(host.intercepted_as_stream_count(), 2); | 215 |
| 281 return host.intercepted_as_stream(); | 216 return host.intercepted_as_stream(); |
| 282 } | 217 } |
| 283 | 218 |
| 284 // Test that stream requests are correctly intercepted under the right | 219 // Test that stream requests are correctly intercepted under the right |
| 285 // circumstances. Test is not relevent when plugins are disabled. | 220 // circumstances. |
| 286 #if defined(ENABLE_PLUGINS) | |
| 287 TEST_F(BufferedResourceHandlerTest, StreamHandling) { | 221 TEST_F(BufferedResourceHandlerTest, StreamHandling) { |
| 288 bool allow_download; | 222 bool allow_download; |
| 289 bool must_download; | 223 bool must_download; |
| 290 ResourceType resource_type; | 224 ResourceType resource_type; |
| 291 | 225 |
| 292 // Ensure the stream is handled by MaybeInterceptAsStream in the | 226 // Ensure the stream is handled by MaybeInterceptAsStream in the |
| 293 // ResourceDispatcherHost. | 227 // ResourceDispatcherHost. |
| 294 set_stream_has_handler(true); | 228 set_stream_has_handler(true); |
| 295 set_plugin_available(true); | |
| 296 | 229 |
| 297 // Main frame request with no download allowed. Stream shouldn't be | 230 // Main frame request with no download allowed. Stream shouldn't be |
| 298 // intercepted. | 231 // intercepted. |
| 299 allow_download = false; | 232 allow_download = false; |
| 300 must_download = false; | 233 must_download = false; |
| 301 resource_type = RESOURCE_TYPE_MAIN_FRAME; | 234 resource_type = RESOURCE_TYPE_MAIN_FRAME; |
| 302 EXPECT_FALSE( | 235 EXPECT_FALSE( |
| 303 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 236 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 304 | 237 |
| 305 // Main frame request with download allowed. Stream should be intercepted. | 238 // Main frame request with download allowed. Stream should be intercepted. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 327 // Object request with download not allowed. Stream should be intercepted. | 260 // Object request with download not allowed. Stream should be intercepted. |
| 328 allow_download = false; | 261 allow_download = false; |
| 329 must_download = false; | 262 must_download = false; |
| 330 resource_type = RESOURCE_TYPE_OBJECT; | 263 resource_type = RESOURCE_TYPE_OBJECT; |
| 331 EXPECT_TRUE( | 264 EXPECT_TRUE( |
| 332 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 265 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 333 | 266 |
| 334 // Test the cases where the stream isn't handled by MaybeInterceptAsStream | 267 // Test the cases where the stream isn't handled by MaybeInterceptAsStream |
| 335 // in the ResourceDispatcherHost. | 268 // in the ResourceDispatcherHost. |
| 336 set_stream_has_handler(false); | 269 set_stream_has_handler(false); |
| 270 |
| 337 allow_download = false; | 271 allow_download = false; |
| 338 must_download = false; | 272 must_download = false; |
| 339 resource_type = RESOURCE_TYPE_OBJECT; | 273 resource_type = RESOURCE_TYPE_OBJECT; |
| 340 EXPECT_FALSE( | 274 EXPECT_FALSE( |
| 341 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 275 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 342 | 276 |
| 343 allow_download = true; | 277 allow_download = true; |
| 344 must_download = false; | 278 must_download = false; |
| 345 resource_type = RESOURCE_TYPE_MAIN_FRAME; | 279 resource_type = RESOURCE_TYPE_MAIN_FRAME; |
| 346 EXPECT_FALSE( | 280 EXPECT_FALSE( |
| 347 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 281 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 348 | |
| 349 // Test the cases where the stream handled by MaybeInterceptAsStream | |
| 350 // with plugin not available. This is the case when intercepting streams for | |
| 351 // the streamsPrivate extensions API. | |
| 352 set_stream_has_handler(true); | |
| 353 set_plugin_available(false); | |
| 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)); | |
| 359 | |
| 360 // Test the cases where the stream handled by MaybeInterceptAsStream | |
| 361 // with plugin not available. This is the case when intercepting streams for | |
| 362 // the streamsPrivate extensions API with stale plugin. | |
| 363 set_plugin_stale(true); | |
| 364 allow_download = false; | |
| 365 must_download = false; | |
| 366 resource_type = RESOURCE_TYPE_OBJECT; | |
| 367 EXPECT_TRUE( | |
| 368 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 369 } | 282 } |
| 370 #endif | |
| 371 | 283 |
| 372 } // namespace | 284 } // namespace |
| 373 | 285 |
| 374 } // namespace content | 286 } // namespace content |
| OLD | NEW |