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