| 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/mime_type_resource_handler.h" | 5 #include "content/browser/loader/mime_sniffing_resource_handler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "content/browser/loader/intercepting_resource_handler.h" |
| 17 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 18 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 18 #include "content/public/browser/resource_controller.h" | 19 #include "content/public/browser/resource_controller.h" |
| 19 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 20 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 20 #include "content/public/browser/resource_request_info.h" | 21 #include "content/public/browser/resource_request_info.h" |
| 21 #include "content/public/common/resource_response.h" | 22 #include "content/public/common/resource_response.h" |
| 22 #include "content/public/common/webplugininfo.h" | 23 #include "content/public/common/webplugininfo.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "content/public/test/test_utils.h" | 25 #include "content/public/test/test_utils.h" |
| 25 #include "content/test/fake_plugin_service.h" | 26 #include "content/test/fake_plugin_service.h" |
| 26 #include "net/url_request/url_request_context.h" | 27 #include "net/url_request/url_request_context.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 29 | 30 |
| 30 namespace content { | 31 namespace content { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 class TestResourceHandler : public ResourceHandler { | 35 class TestResourceHandler : public ResourceHandler { |
| 35 public: | 36 public: |
| 36 TestResourceHandler() : ResourceHandler(nullptr) {} | 37 TestResourceHandler(bool response_started_succeeds, |
| 38 bool defer_on_response_started, |
| 39 bool will_read_succeeds, |
| 40 bool read_completed_succeeds, |
| 41 bool defer_on_read_completed) |
| 42 : ResourceHandler(nullptr), |
| 43 buffer_(new net::IOBuffer(2048)), |
| 44 response_started_succeeds_(response_started_succeeds), |
| 45 defer_on_response_started_(defer_on_response_started), |
| 46 will_read_succeeds_(will_read_succeeds), |
| 47 read_completed_succeeds_(read_completed_succeeds), |
| 48 defer_on_read_completed_(defer_on_read_completed), |
| 49 on_will_start_called_(0), |
| 50 on_request_redirected_called_(0), |
| 51 on_response_started_called_(0), |
| 52 on_will_read_called_(0), |
| 53 on_read_completed_called_(0) {} |
| 37 | 54 |
| 38 void SetController(ResourceController* controller) override {} | 55 void SetController(ResourceController* controller) override {} |
| 39 | 56 |
| 40 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | 57 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, |
| 41 ResourceResponse* response, | 58 ResourceResponse* response, |
| 42 bool* defer) override { | 59 bool* defer) override { |
| 60 on_request_redirected_called_++; |
| 43 NOTREACHED(); | 61 NOTREACHED(); |
| 44 return false; | 62 return false; |
| 45 } | 63 } |
| 46 | 64 |
| 47 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { | 65 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { |
| 48 return false; | 66 on_response_started_called_++; |
| 67 if (defer_on_response_started_) |
| 68 *defer = true; |
| 69 return response_started_succeeds_; |
| 49 } | 70 } |
| 50 | 71 |
| 51 bool OnWillStart(const GURL& url, bool* defer) override { | 72 bool OnWillStart(const GURL& url, bool* defer) override { |
| 73 on_will_start_called_++; |
| 52 return false; | 74 return false; |
| 53 } | 75 } |
| 54 | 76 |
| 55 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 77 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 56 int* buf_size, | 78 int* buf_size, |
| 57 int min_size) override { | 79 int min_size) override { |
| 58 NOTREACHED(); | 80 on_will_read_called_++; |
| 59 return false; | 81 *buf = buffer_; |
| 82 *buf_size = 2048; |
| 83 return will_read_succeeds_; |
| 60 } | 84 } |
| 61 | 85 |
| 62 bool OnReadCompleted(int bytes_read, bool* defer) override { | 86 bool OnReadCompleted(int bytes_read, bool* defer) override { |
| 63 NOTREACHED(); | 87 DCHECK_LT(bytes_read, 2048); |
| 64 return false; | 88 on_read_completed_called_++; |
| 89 if (defer_on_read_completed_) |
| 90 *defer = true; |
| 91 return read_completed_succeeds_; |
| 65 } | 92 } |
| 66 | 93 |
| 67 void OnResponseCompleted(const net::URLRequestStatus& status, | 94 void OnResponseCompleted(const net::URLRequestStatus& status, |
| 68 const std::string& security_info, | 95 const std::string& security_info, |
| 69 bool* defer) override { | 96 bool* defer) override {} |
| 97 |
| 98 void OnDataDownloaded(int bytes_downloaded) override { NOTREACHED(); } |
| 99 |
| 100 scoped_refptr<net::IOBuffer> buffer() { return buffer_; } |
| 101 |
| 102 int on_will_start_called() const { return on_will_start_called_; } |
| 103 int on_request_redirected_called() const { |
| 104 return on_request_redirected_called_; |
| 70 } | 105 } |
| 106 int on_response_started_called() const { return on_response_started_called_; } |
| 107 int on_will_read_called() const { return on_will_read_called_; } |
| 108 int on_read_completed_called() const { return on_read_completed_called_; } |
| 71 | 109 |
| 72 void OnDataDownloaded(int bytes_downloaded) override { | 110 private: |
| 73 NOTREACHED(); | 111 scoped_refptr<net::IOBuffer> buffer_; |
| 112 bool response_started_succeeds_; |
| 113 bool defer_on_response_started_; |
| 114 bool will_read_succeeds_; |
| 115 bool read_completed_succeeds_; |
| 116 bool defer_on_read_completed_; |
| 117 |
| 118 int on_will_start_called_; |
| 119 int on_request_redirected_called_; |
| 120 int on_response_started_called_; |
| 121 int on_will_read_called_; |
| 122 int on_read_completed_called_; |
| 123 |
| 124 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); |
| 125 }; |
| 126 |
| 127 class TestResourceDispatcherHostDelegate |
| 128 : public ResourceDispatcherHostDelegate { |
| 129 public: |
| 130 explicit TestResourceDispatcherHostDelegate(bool must_download) |
| 131 : must_download_(must_download) {} |
| 132 |
| 133 bool ShouldForceDownloadResource(const GURL& url, |
| 134 const std::string& mime_type) override { |
| 135 return must_download_; |
| 74 } | 136 } |
| 75 | 137 |
| 76 private: | 138 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); | 139 const bool must_download_; |
| 78 }; | 140 }; |
| 79 | 141 |
| 80 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { | 142 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { |
| 81 public: | 143 public: |
| 82 explicit TestResourceDispatcherHost(bool stream_has_handler) | 144 explicit TestResourceDispatcherHost(bool stream_has_handler) |
| 83 : stream_has_handler_(stream_has_handler), | 145 : stream_has_handler_(stream_has_handler), |
| 84 intercepted_as_stream_(false), | 146 intercepted_as_stream_(false), |
| 85 intercepted_as_stream_count_(0) {} | 147 intercepted_as_stream_count_(0), |
| 148 new_resource_handler_(nullptr) {} |
| 86 | 149 |
| 87 bool intercepted_as_stream() const { return intercepted_as_stream_; } | 150 bool intercepted_as_stream() const { return intercepted_as_stream_; } |
| 88 | 151 |
| 89 std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload( | 152 std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload( |
| 90 net::URLRequest* request, | 153 net::URLRequest* request, |
| 91 bool is_content_initiated, | 154 bool is_content_initiated, |
| 92 bool must_download) override { | 155 bool must_download) override { |
| 93 return std::unique_ptr<ResourceHandler>(new TestResourceHandler); | 156 return CreateNewResourceHandler(); |
| 94 } | 157 } |
| 95 | 158 |
| 96 std::unique_ptr<ResourceHandler> MaybeInterceptAsStream( | 159 std::unique_ptr<ResourceHandler> MaybeInterceptAsStream( |
| 97 const base::FilePath& plugin_path, | 160 const base::FilePath& plugin_path, |
| 98 net::URLRequest* request, | 161 net::URLRequest* request, |
| 99 ResourceResponse* response, | 162 ResourceResponse* response, |
| 100 std::string* payload) override { | 163 std::string* payload) override { |
| 101 intercepted_as_stream_count_++; | 164 intercepted_as_stream_count_++; |
| 102 if (stream_has_handler_) { | 165 if (stream_has_handler_) |
| 103 intercepted_as_stream_ = true; | 166 intercepted_as_stream_ = true; |
| 104 return std::unique_ptr<ResourceHandler>(new TestResourceHandler); | 167 return CreateNewResourceHandler(); |
| 105 } else { | |
| 106 return std::unique_ptr<ResourceHandler>(); | |
| 107 } | |
| 108 } | 168 } |
| 109 | 169 |
| 110 int intercepted_as_stream_count() const { | 170 int intercepted_as_stream_count() const { |
| 111 return intercepted_as_stream_count_; | 171 return intercepted_as_stream_count_; |
| 112 } | 172 } |
| 113 | 173 |
| 174 TestResourceHandler* new_resource_handler() const { |
| 175 return new_resource_handler_; |
| 176 } |
| 177 |
| 114 private: | 178 private: |
| 179 std::unique_ptr<ResourceHandler> CreateNewResourceHandler() { |
| 180 std::unique_ptr<TestResourceHandler> new_resource_handler( |
| 181 new TestResourceHandler(false, false, true, true, false)); |
| 182 new_resource_handler_ = new_resource_handler.get(); |
| 183 return std::move(new_resource_handler); |
| 184 } |
| 185 |
| 115 // Whether the URL request should be intercepted as a stream. | 186 // Whether the URL request should be intercepted as a stream. |
| 116 bool stream_has_handler_; | 187 bool stream_has_handler_; |
| 117 | 188 |
| 118 // Whether the URL request has been intercepted as a stream. | 189 // Whether the URL request has been intercepted as a stream. |
| 119 bool intercepted_as_stream_; | 190 bool intercepted_as_stream_; |
| 120 | 191 |
| 121 // Count of number of times MaybeInterceptAsStream function get called in a | 192 // Count of number of times MaybeInterceptAsStream function get called in a |
| 122 // test. | 193 // test. |
| 123 int intercepted_as_stream_count_; | 194 int intercepted_as_stream_count_; |
| 124 }; | |
| 125 | 195 |
| 126 class TestResourceDispatcherHostDelegate | 196 // The last alternative TestResourceHandler created by this |
| 127 : public ResourceDispatcherHostDelegate { | 197 // TestResourceDispatcherHost. |
| 128 public: | 198 TestResourceHandler* new_resource_handler_; |
| 129 TestResourceDispatcherHostDelegate(bool must_download) | |
| 130 : must_download_(must_download) { | |
| 131 } | |
| 132 | |
| 133 bool ShouldForceDownloadResource(const GURL& url, | |
| 134 const std::string& mime_type) override { | |
| 135 return must_download_; | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 const bool must_download_; | |
| 140 }; | |
| 141 | |
| 142 class TestResourceController : public ResourceController { | |
| 143 public: | |
| 144 void Cancel() override {} | |
| 145 | |
| 146 void CancelAndIgnore() override { | |
| 147 NOTREACHED(); | |
| 148 } | |
| 149 | |
| 150 void CancelWithError(int error_code) override { | |
| 151 NOTREACHED(); | |
| 152 } | |
| 153 | |
| 154 void Resume() override { | |
| 155 NOTREACHED(); | |
| 156 } | |
| 157 }; | 199 }; |
| 158 | 200 |
| 159 class TestFakePluginService : public FakePluginService { | 201 class TestFakePluginService : public FakePluginService { |
| 160 public: | 202 public: |
| 161 // If |is_plugin_stale| is true, GetPluginInfo will indicate the plugins are | 203 // If |is_plugin_stale| is true, GetPluginInfo will indicate the plugins are |
| 162 // stale until GetPlugins is called. | 204 // stale until GetPlugins is called. |
| 163 TestFakePluginService(bool plugin_available, bool is_plugin_stale) | 205 TestFakePluginService(bool plugin_available, bool is_plugin_stale) |
| 164 : plugin_available_(plugin_available), | 206 : plugin_available_(plugin_available), |
| 165 is_plugin_stale_(is_plugin_stale) {} | 207 is_plugin_stale_(is_plugin_stale) {} |
| 166 | 208 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 190 FROM_HERE, base::Bind(callback, plugins)); | 232 FROM_HERE, base::Bind(callback, plugins)); |
| 191 } | 233 } |
| 192 | 234 |
| 193 private: | 235 private: |
| 194 const bool plugin_available_; | 236 const bool plugin_available_; |
| 195 bool is_plugin_stale_; | 237 bool is_plugin_stale_; |
| 196 | 238 |
| 197 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); | 239 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); |
| 198 }; | 240 }; |
| 199 | 241 |
| 200 class MimeTypeResourceHandlerTest : public testing::Test { | 242 class TestResourceController : public ResourceController { |
| 201 public: | 243 public: |
| 202 MimeTypeResourceHandlerTest() | 244 TestResourceController() : cancel_call_count_(0), resume_call_count_(0) {} |
| 245 |
| 246 void Cancel() override { cancel_call_count_++; } |
| 247 |
| 248 void CancelAndIgnore() override { NOTREACHED(); } |
| 249 |
| 250 void CancelWithError(int error_code) override { NOTREACHED(); } |
| 251 |
| 252 void Resume() override { resume_call_count_++; } |
| 253 |
| 254 int cancel_call_count() const { return cancel_call_count_; } |
| 255 int resume_call_count() const { return resume_call_count_; } |
| 256 |
| 257 private: |
| 258 int cancel_call_count_; |
| 259 int resume_call_count_; |
| 260 }; |
| 261 |
| 262 } // namespace |
| 263 |
| 264 class MimeSniffingResourceHandlerTest : public testing::Test { |
| 265 public: |
| 266 MimeSniffingResourceHandlerTest() |
| 203 : stream_has_handler_(false), | 267 : stream_has_handler_(false), |
| 204 plugin_available_(false), | 268 plugin_available_(false), |
| 205 plugin_stale_(false) {} | 269 plugin_stale_(false) {} |
| 206 | 270 |
| 271 // Tests that the MimeSniffingHandler properly sets the accept field in the |
| 272 // header. Returns the accept header value. |
| 273 std::string TestAcceptHeaderSetting(ResourceType request_resource_type); |
| 274 std::string TestAcceptHeaderSettingWithURLRequest( |
| 275 ResourceType request_resource_type, |
| 276 net::URLRequest* request); |
| 277 |
| 207 void set_stream_has_handler(bool stream_has_handler) { | 278 void set_stream_has_handler(bool stream_has_handler) { |
| 208 stream_has_handler_ = stream_has_handler; | 279 stream_has_handler_ = stream_has_handler; |
| 209 } | 280 } |
| 210 | 281 |
| 211 void set_plugin_available(bool plugin_available) { | 282 void set_plugin_available(bool plugin_available) { |
| 212 plugin_available_ = plugin_available; | 283 plugin_available_ = plugin_available; |
| 213 } | 284 } |
| 214 | 285 |
| 215 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } | 286 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } |
| 216 | 287 |
| 217 bool TestStreamIsIntercepted(bool allow_download, | 288 bool TestStreamIsIntercepted(bool allow_download, |
| 218 bool must_download, | 289 bool must_download, |
| 219 ResourceType request_resource_type); | 290 ResourceType request_resource_type); |
| 220 | 291 |
| 221 std::string TestAcceptHeaderSetting(ResourceType request_resource_type); | 292 // Tests the operation of the MimeSniffingHandler when it needs to buffer |
| 222 std::string TestAcceptHeaderSettingWithURLRequest( | 293 // data (example case: the response is text/plain). |
| 223 ResourceType request_resource_type, | 294 void TestHandlerSniffing(bool response_started, |
| 224 net::URLRequest* request); | 295 bool defer_response_started, |
| 296 bool will_read, |
| 297 bool read_completed, |
| 298 bool defer_read_completed); |
| 299 |
| 300 // Tests the operation of the MimeSniffingHandler when it doesn't buffer |
| 301 // data (example case: the response is text/html). |
| 302 void TestHandlerNoSniffing(bool response_started, |
| 303 bool defer_response_started, |
| 304 bool will_read, |
| 305 bool read_completed, |
| 306 bool defer_read_completed); |
| 225 | 307 |
| 226 private: | 308 private: |
| 227 // Whether the URL request should be intercepted as a stream. | 309 // Whether the URL request should be intercepted as a stream. |
| 228 bool stream_has_handler_; | 310 bool stream_has_handler_; |
| 229 bool plugin_available_; | 311 bool plugin_available_; |
| 230 bool plugin_stale_; | 312 bool plugin_stale_; |
| 231 | 313 |
| 232 TestBrowserThreadBundle thread_bundle_; | 314 TestBrowserThreadBundle thread_bundle_; |
| 233 }; | 315 }; |
| 234 | 316 |
| 235 bool MimeTypeResourceHandlerTest::TestStreamIsIntercepted( | 317 std::string MimeSniffingResourceHandlerTest::TestAcceptHeaderSetting( |
| 318 ResourceType request_resource_type) { |
| 319 net::URLRequestContext context; |
| 320 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 321 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 322 return TestAcceptHeaderSettingWithURLRequest(request_resource_type, |
| 323 request.get()); |
| 324 } |
| 325 |
| 326 std::string |
| 327 MimeSniffingResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest( |
| 328 ResourceType request_resource_type, |
| 329 net::URLRequest* request) { |
| 330 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; |
| 331 ResourceRequestInfo::AllocateForTesting(request, request_resource_type, |
| 332 nullptr, // context |
| 333 0, // render_process_id |
| 334 0, // render_view_id |
| 335 0, // render_frame_id |
| 336 is_main_frame, // is_main_frame |
| 337 false, // parent_is_main_frame |
| 338 false, // allow_download |
| 339 true, // is_async |
| 340 false); // is_using_lofi |
| 341 |
| 342 std::unique_ptr<ResourceHandler> mime_sniffing_handler( |
| 343 new MimeSniffingResourceHandler( |
| 344 std::unique_ptr<ResourceHandler>( |
| 345 new TestResourceHandler(false, false, false, false, false)), |
| 346 nullptr, nullptr, nullptr, request)); |
| 347 |
| 348 bool defer = false; |
| 349 mime_sniffing_handler->OnWillStart(request->url(), &defer); |
| 350 content::RunAllPendingInMessageLoop(); |
| 351 |
| 352 std::string accept_header; |
| 353 request->extra_request_headers().GetHeader("Accept", &accept_header); |
| 354 return accept_header; |
| 355 } |
| 356 |
| 357 bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted( |
| 236 bool allow_download, | 358 bool allow_download, |
| 237 bool must_download, | 359 bool must_download, |
| 238 ResourceType request_resource_type) { | 360 ResourceType request_resource_type) { |
| 239 net::URLRequestContext context; | 361 net::URLRequestContext context; |
| 240 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 362 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 241 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 363 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 242 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | 364 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; |
| 243 ResourceRequestInfo::AllocateForTesting( | 365 ResourceRequestInfo::AllocateForTesting(request.get(), request_resource_type, |
| 244 request.get(), | 366 nullptr, // context |
| 245 request_resource_type, | 367 0, // render_process_id |
| 246 nullptr, // context | 368 0, // render_view_id |
| 247 0, // render_process_id | 369 0, // render_frame_id |
| 248 0, // render_view_id | 370 is_main_frame, // is_main_frame |
| 249 0, // render_frame_id | 371 false, // parent_is_main_frame |
| 250 is_main_frame, // is_main_frame | 372 allow_download, // allow_download |
| 251 false, // parent_is_main_frame | 373 true, // is_async |
| 252 allow_download, // allow_download | 374 false); // is_using_lofi |
| 253 true, // is_async | |
| 254 false); // is_using_lofi | |
| 255 | 375 |
| 256 TestResourceDispatcherHost host(stream_has_handler_); | 376 TestResourceDispatcherHost host(stream_has_handler_); |
| 257 TestResourceDispatcherHostDelegate host_delegate(must_download); | 377 TestResourceDispatcherHostDelegate host_delegate(must_download); |
| 258 host.SetDelegate(&host_delegate); | 378 host.SetDelegate(&host_delegate); |
| 259 | 379 |
| 260 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 380 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
| 261 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 381 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 262 new MimeTypeResourceHandler( | 382 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
| 263 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, | 383 nullptr)); |
| 264 &plugin_service, request.get())); | 384 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( |
| 385 std::unique_ptr<ResourceHandler>( |
| 386 new TestResourceHandler(false, false, false, false, false)), |
| 387 &host, &plugin_service, intercepting_handler.get(), request.get())); |
| 388 |
| 265 TestResourceController resource_controller; | 389 TestResourceController resource_controller; |
| 266 mime_sniffing_handler->SetController(&resource_controller); | 390 mime_handler->SetController(&resource_controller); |
| 267 | 391 |
| 268 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 392 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 269 // The MIME type isn't important but it shouldn't be empty. | 393 // The MIME type isn't important but it shouldn't be empty. |
| 270 response->head.mime_type = "application/pdf"; | 394 response->head.mime_type = "application/pdf"; |
| 271 | 395 |
| 272 bool defer = false; | 396 bool defer = false; |
| 273 mime_sniffing_handler->OnResponseStarted(response.get(), &defer); | 397 mime_handler->OnResponseStarted(response.get(), &defer); |
| 274 | 398 |
| 275 content::RunAllPendingInMessageLoop(); | 399 content::RunAllPendingInMessageLoop(); |
| 276 EXPECT_LT(host.intercepted_as_stream_count(), 2); | 400 EXPECT_LT(host.intercepted_as_stream_count(), 2); |
| 401 if (allow_download) |
| 402 EXPECT_TRUE(intercepting_handler->new_handler_for_testing()); |
| 277 return host.intercepted_as_stream(); | 403 return host.intercepted_as_stream(); |
| 278 } | 404 } |
| 279 | 405 |
| 280 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSetting( | 406 void MimeSniffingResourceHandlerTest::TestHandlerSniffing( |
| 281 ResourceType request_resource_type) { | 407 bool response_started, |
| 408 bool defer_response_started, |
| 409 bool will_read, |
| 410 bool read_completed, |
| 411 bool defer_read_completed) { |
| 282 net::URLRequestContext context; | 412 net::URLRequestContext context; |
| 283 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 413 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 284 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 414 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 285 return TestAcceptHeaderSettingWithURLRequest( | 415 ResourceRequestInfo::AllocateForTesting(request.get(), |
| 286 request_resource_type, request.get()); | 416 RESOURCE_TYPE_MAIN_FRAME, |
| 287 } | 417 nullptr, // context |
| 288 | 418 0, // render_process_id |
| 289 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest( | 419 0, // render_view_id |
| 290 ResourceType request_resource_type, | 420 0, // render_frame_id |
| 291 net::URLRequest* request) { | 421 true, // is_main_frame |
| 292 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | 422 false, // parent_is_main_frame |
| 293 ResourceRequestInfo::AllocateForTesting( | 423 false, // allow_download |
| 294 request, | 424 true, // is_async |
| 295 request_resource_type, | 425 false); // is_using_lofi |
| 296 nullptr, // context | 426 |
| 297 0, // render_process_id | 427 TestResourceDispatcherHost host(false); |
| 298 0, // render_view_id | |
| 299 0, // render_frame_id | |
| 300 is_main_frame, // is_main_frame | |
| 301 false, // parent_is_main_frame | |
| 302 false, // allow_download | |
| 303 true, // is_async | |
| 304 false); // is_using_lofi | |
| 305 | |
| 306 TestResourceDispatcherHost host(stream_has_handler_); | |
| 307 TestResourceDispatcherHostDelegate host_delegate(false); | 428 TestResourceDispatcherHostDelegate host_delegate(false); |
| 308 host.SetDelegate(&host_delegate); | 429 host.SetDelegate(&host_delegate); |
| 309 | 430 |
| 310 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 431 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
| 311 new MimeTypeResourceHandler( | 432 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 312 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, | 433 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
| 313 nullptr, request)); | 434 nullptr)); |
| 435 std::unique_ptr<TestResourceHandler> scoped_test_handler = |
| 436 std::unique_ptr<TestResourceHandler>(new TestResourceHandler( |
| 437 response_started, defer_response_started, will_read, read_completed, |
| 438 defer_read_completed)); |
| 439 TestResourceHandler* test_handler = scoped_test_handler.get(); |
| 440 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( |
| 441 new MimeSniffingResourceHandler( |
| 442 std::move(scoped_test_handler), &host, &plugin_service, |
| 443 intercepting_handler.get(), request.get())); |
| 444 |
| 445 TestResourceController resource_controller; |
| 446 mime_sniffing_handler->SetController(&resource_controller); |
| 314 | 447 |
| 315 bool defer = false; | 448 bool defer = false; |
| 316 mime_sniffing_handler->OnWillStart(request->url(), &defer); | 449 mime_sniffing_handler->OnWillStart(GURL(), &defer); |
| 450 |
| 451 // The response should be sniffed. |
| 452 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 453 response->head.mime_type.assign("text/plain"); |
| 454 |
| 455 // Simulate the response starting. The MimeSniffingHandler should start |
| 456 // buffering, so the return value should always be true. |
| 457 EXPECT_TRUE(mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); |
| 458 EXPECT_EQ(0, resource_controller.cancel_call_count()); |
| 459 EXPECT_EQ(0, resource_controller.resume_call_count()); |
| 460 EXPECT_FALSE(defer); |
| 461 |
| 462 // Read some data to sniff the mime type. This will ask the next |
| 463 // ResourceHandler for a buffer. |
| 464 scoped_refptr<net::IOBuffer> read_buffer; |
| 465 int buf_size = 0; |
| 466 EXPECT_EQ(will_read, |
| 467 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); |
| 468 EXPECT_EQ(0, resource_controller.cancel_call_count()); |
| 469 |
| 470 if (!will_read) { |
| 471 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 472 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 473 EXPECT_EQ(0, test_handler->on_response_started_called()); |
| 474 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 475 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 476 |
| 477 // Process all messages to ensure proper test teardown. |
| 478 content::RunAllPendingInMessageLoop(); |
| 479 return; |
| 480 } |
| 481 |
| 482 // Simulate an HTML page. The mime sniffer will identify the MimeType and |
| 483 // proceed with replay. |
| 484 char data[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>"; |
| 485 memcpy(read_buffer->data(), data, sizeof(data)); |
| 486 |
| 487 defer = false; |
| 488 bool return_value = |
| 489 mime_sniffing_handler->OnReadCompleted(sizeof(data), &defer); |
| 490 |
| 491 // If the next handler cancels the response start, the caller of |
| 492 // MimeSniffingHandler::OnReadCompleted should be notified immediately. |
| 493 if (!response_started) { |
| 494 EXPECT_FALSE(defer); |
| 495 EXPECT_EQ(response_started, return_value); |
| 496 EXPECT_EQ(0, resource_controller.cancel_call_count()); |
| 497 |
| 498 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 499 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 500 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 501 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 502 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 503 |
| 504 // Process all messages to ensure proper test teardown. |
| 505 content::RunAllPendingInMessageLoop(); |
| 506 return; |
| 507 } |
| 508 |
| 509 // The replay can be deferred both at response started and read replay |
| 510 // stages. |
| 511 EXPECT_EQ(defer, defer_response_started || defer_read_completed); |
| 512 if (defer_response_started) { |
| 513 EXPECT_TRUE(defer); |
| 514 EXPECT_TRUE(return_value); |
| 515 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, |
| 516 mime_sniffing_handler->state_); |
| 517 mime_sniffing_handler->Resume(); |
| 518 } |
| 519 |
| 520 // The body that was sniffed should be transmitted to the next handler. This |
| 521 // may cancel the request. |
| 522 if (!read_completed) { |
| 523 if (defer_response_started) { |
| 524 EXPECT_EQ(1, resource_controller.cancel_call_count()); |
| 525 } else { |
| 526 EXPECT_EQ(0, resource_controller.cancel_call_count()); |
| 527 EXPECT_FALSE(return_value); |
| 528 } |
| 529 // Process all messages to ensure proper test teardown. |
| 530 content::RunAllPendingInMessageLoop(); |
| 531 return; |
| 532 } |
| 533 |
| 534 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, |
| 535 mime_sniffing_handler->state_); |
| 536 |
| 537 // The request may be deferred by the next handler once the read is done. |
| 538 if (defer_read_completed) { |
| 539 EXPECT_TRUE(defer); |
| 540 mime_sniffing_handler->Resume(); |
| 541 } |
| 542 |
| 543 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, |
| 544 mime_sniffing_handler->state_); |
| 545 EXPECT_EQ(0, resource_controller.cancel_call_count()); |
| 546 |
| 547 // Even if the next handler defers the request twice, the |
| 548 // MimeSniffingResourceHandler should only call Resume on its controller |
| 549 // once. |
| 550 if (defer_response_started || defer_read_completed) { |
| 551 EXPECT_EQ(1, resource_controller.resume_call_count()); |
| 552 } else { |
| 553 EXPECT_EQ(0, resource_controller.resume_call_count()); |
| 554 } |
| 555 |
| 556 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 557 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 558 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 559 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 560 EXPECT_EQ(1, test_handler->on_read_completed_called()); |
| 561 |
| 562 // Process all messages to ensure proper test teardown. |
| 317 content::RunAllPendingInMessageLoop(); | 563 content::RunAllPendingInMessageLoop(); |
| 318 | |
| 319 std::string accept_header; | |
| 320 request->extra_request_headers().GetHeader("Accept", &accept_header); | |
| 321 return accept_header; | |
| 322 } | 564 } |
| 323 | 565 |
| 566 void MimeSniffingResourceHandlerTest::TestHandlerNoSniffing( |
| 567 bool response_started, |
| 568 bool defer_response_started, |
| 569 bool will_read, |
| 570 bool read_completed, |
| 571 bool defer_read_completed) { |
| 572 net::URLRequestContext context; |
| 573 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 574 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 575 ResourceRequestInfo::AllocateForTesting(request.get(), |
| 576 RESOURCE_TYPE_MAIN_FRAME, |
| 577 nullptr, // context |
| 578 0, // render_process_id |
| 579 0, // render_view_id |
| 580 0, // render_frame_id |
| 581 true, // is_main_frame |
| 582 false, // parent_is_main_frame |
| 583 false, // allow_download |
| 584 true, // is_async |
| 585 false); // is_using_lofi |
| 586 |
| 587 TestResourceDispatcherHost host(false); |
| 588 TestResourceDispatcherHostDelegate host_delegate(false); |
| 589 host.SetDelegate(&host_delegate); |
| 590 |
| 591 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
| 592 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 593 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
| 594 nullptr)); |
| 595 |
| 596 std::unique_ptr<TestResourceHandler> scoped_test_handler = |
| 597 std::unique_ptr<TestResourceHandler>(new TestResourceHandler( |
| 598 response_started, defer_response_started, will_read, read_completed, |
| 599 defer_read_completed)); |
| 600 TestResourceHandler* test_handler = scoped_test_handler.get(); |
| 601 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( |
| 602 new MimeSniffingResourceHandler( |
| 603 std::move(scoped_test_handler), &host, &plugin_service, |
| 604 intercepting_handler.get(), request.get())); |
| 605 |
| 606 TestResourceController resource_controller; |
| 607 mime_sniffing_handler->SetController(&resource_controller); |
| 608 |
| 609 int expected_resume_calls = 0; |
| 610 |
| 611 bool defer = false; |
| 612 mime_sniffing_handler->OnWillStart(GURL(), &defer); |
| 613 |
| 614 // The response should not be sniffed. |
| 615 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 616 response->head.mime_type.assign("text/html"); |
| 617 |
| 618 // Simulate the response starting. There should be no need for buffering, so |
| 619 // the return value should be that of the next handler. |
| 620 EXPECT_EQ(response_started, |
| 621 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); |
| 622 EXPECT_EQ(0, resource_controller.cancel_call_count()); |
| 623 |
| 624 if (!response_started) { |
| 625 EXPECT_FALSE(defer); |
| 626 |
| 627 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 628 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 629 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 630 EXPECT_EQ(0, test_handler->on_will_read_called()); |
| 631 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 632 |
| 633 // Process all messages to ensure proper test teardown. |
| 634 content::RunAllPendingInMessageLoop(); |
| 635 return; |
| 636 } |
| 637 |
| 638 EXPECT_EQ(defer_response_started, defer); |
| 639 if (defer) { |
| 640 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, |
| 641 mime_sniffing_handler->state_); |
| 642 expected_resume_calls++; |
| 643 mime_sniffing_handler->Resume(); |
| 644 } |
| 645 |
| 646 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count()); |
| 647 |
| 648 // The MimeSniffingResourceHandler should be acting as a pass-through |
| 649 // ResourceHandler. |
| 650 scoped_refptr<net::IOBuffer> read_buffer; |
| 651 int buf_size = 0; |
| 652 EXPECT_EQ(will_read, |
| 653 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); |
| 654 EXPECT_EQ(0, resource_controller.cancel_call_count()); |
| 655 |
| 656 if (!will_read) { |
| 657 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 658 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 659 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 660 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 661 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 662 |
| 663 // Process all messages to ensure proper test teardown. |
| 664 content::RunAllPendingInMessageLoop(); |
| 665 return; |
| 666 } |
| 667 |
| 668 defer = false; |
| 669 EXPECT_EQ(read_completed, |
| 670 mime_sniffing_handler->OnReadCompleted(2000, &defer)); |
| 671 EXPECT_EQ(0, resource_controller.cancel_call_count()); |
| 672 |
| 673 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 674 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 675 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 676 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 677 EXPECT_EQ(1, test_handler->on_read_completed_called()); |
| 678 |
| 679 if (!read_completed) { |
| 680 EXPECT_FALSE(defer); |
| 681 |
| 682 // Process all messages to ensure proper test teardown. |
| 683 content::RunAllPendingInMessageLoop(); |
| 684 return; |
| 685 } |
| 686 |
| 687 EXPECT_EQ(defer_read_completed, defer); |
| 688 if (defer) { |
| 689 expected_resume_calls++; |
| 690 mime_sniffing_handler->Resume(); |
| 691 } |
| 692 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count()); |
| 693 |
| 694 // Process all messages to ensure proper test teardown. |
| 695 content::RunAllPendingInMessageLoop(); |
| 696 } |
| 697 |
| 324 // Test that the proper Accept: header is set based on the ResourceType | 698 // Test that the proper Accept: header is set based on the ResourceType |
| 325 TEST_F(MimeTypeResourceHandlerTest, AcceptHeaders) { | 699 TEST_F(MimeSniffingResourceHandlerTest, AcceptHeaders) { |
| 326 EXPECT_EQ( | 700 EXPECT_EQ( |
| 327 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | 701 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," |
| 328 "*/*;q=0.8", | 702 "*/*;q=0.8", |
| 329 TestAcceptHeaderSetting(RESOURCE_TYPE_MAIN_FRAME)); | 703 TestAcceptHeaderSetting(RESOURCE_TYPE_MAIN_FRAME)); |
| 330 EXPECT_EQ( | 704 EXPECT_EQ( |
| 331 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | 705 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," |
| 332 "*/*;q=0.8", | 706 "*/*;q=0.8", |
| 333 TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_FRAME)); | 707 TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_FRAME)); |
| 334 EXPECT_EQ("text/css,*/*;q=0.1", | 708 EXPECT_EQ("text/css,*/*;q=0.1", |
| 335 TestAcceptHeaderSetting(RESOURCE_TYPE_STYLESHEET)); | 709 TestAcceptHeaderSetting(RESOURCE_TYPE_STYLESHEET)); |
| 336 EXPECT_EQ("*/*", | 710 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SCRIPT)); |
| 337 TestAcceptHeaderSetting(RESOURCE_TYPE_SCRIPT)); | |
| 338 EXPECT_EQ("image/webp,image/*,*/*;q=0.8", | 711 EXPECT_EQ("image/webp,image/*,*/*;q=0.8", |
| 339 TestAcceptHeaderSetting(RESOURCE_TYPE_IMAGE)); | 712 TestAcceptHeaderSetting(RESOURCE_TYPE_IMAGE)); |
| 340 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FONT_RESOURCE)); | 713 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FONT_RESOURCE)); |
| 341 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_RESOURCE)); | 714 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_RESOURCE)); |
| 342 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_OBJECT)); | 715 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_OBJECT)); |
| 343 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_MEDIA)); | 716 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_MEDIA)); |
| 344 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_WORKER)); | 717 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_WORKER)); |
| 345 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SHARED_WORKER)); | 718 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SHARED_WORKER)); |
| 346 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PREFETCH)); | 719 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PREFETCH)); |
| 347 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FAVICON)); | 720 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FAVICON)); |
| 348 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_XHR)); | 721 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_XHR)); |
| 349 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PING)); | 722 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PING)); |
| 350 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SERVICE_WORKER)); | 723 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SERVICE_WORKER)); |
| 351 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_CSP_REPORT)); | 724 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_CSP_REPORT)); |
| 352 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PLUGIN_RESOURCE)); | 725 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PLUGIN_RESOURCE)); |
| 353 | 726 |
| 354 // Ensure that if an Accept header is already set, it is not overwritten. | 727 // Ensure that if an Accept header is already set, it is not overwritten. |
| 355 net::URLRequestContext context; | 728 net::URLRequestContext context; |
| 356 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 729 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 357 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 730 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 358 request->SetExtraRequestHeaderByName("Accept", "*", true); | 731 request->SetExtraRequestHeaderByName("Accept", "*", true); |
| 359 EXPECT_EQ("*", | 732 EXPECT_EQ("*", TestAcceptHeaderSettingWithURLRequest(RESOURCE_TYPE_XHR, |
| 360 TestAcceptHeaderSettingWithURLRequest(RESOURCE_TYPE_XHR, request.get())); | 733 request.get())); |
| 361 } | 734 } |
| 362 | 735 |
| 363 // Test that stream requests are correctly intercepted under the right | 736 // Test that stream requests are correctly intercepted under the right |
| 364 // circumstances. Test is not relevent when plugins are disabled. | 737 // circumstances. Test is not relevent when plugins are disabled. |
| 365 #if defined(ENABLE_PLUGINS) | 738 #if defined(ENABLE_PLUGINS) |
| 366 TEST_F(MimeTypeResourceHandlerTest, StreamHandling) { | 739 TEST_F(MimeSniffingResourceHandlerTest, StreamHandling) { |
| 367 bool allow_download; | 740 bool allow_download; |
| 368 bool must_download; | 741 bool must_download; |
| 369 ResourceType resource_type; | 742 ResourceType resource_type; |
| 370 | 743 |
| 371 // Ensure the stream is handled by MaybeInterceptAsStream in the | 744 // Ensure the stream is handled by MaybeInterceptAsStream in the |
| 372 // ResourceDispatcherHost. | 745 // ResourceDispatcherHost. |
| 373 set_stream_has_handler(true); | 746 set_stream_has_handler(true); |
| 374 set_plugin_available(true); | 747 set_plugin_available(true); |
| 375 | 748 |
| 376 // Main frame request with no download allowed. Stream shouldn't be | 749 // Main frame request with no download allowed. Stream shouldn't be |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 | 793 |
| 421 // Test the cases where the stream isn't handled by MaybeInterceptAsStream | 794 // Test the cases where the stream isn't handled by MaybeInterceptAsStream |
| 422 // in the ResourceDispatcherHost. | 795 // in the ResourceDispatcherHost. |
| 423 set_stream_has_handler(false); | 796 set_stream_has_handler(false); |
| 424 allow_download = false; | 797 allow_download = false; |
| 425 must_download = false; | 798 must_download = false; |
| 426 resource_type = RESOURCE_TYPE_OBJECT; | 799 resource_type = RESOURCE_TYPE_OBJECT; |
| 427 EXPECT_FALSE( | 800 EXPECT_FALSE( |
| 428 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 801 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 429 | 802 |
| 430 allow_download = true; | |
| 431 must_download = false; | |
| 432 resource_type = RESOURCE_TYPE_MAIN_FRAME; | |
| 433 EXPECT_FALSE( | |
| 434 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | |
| 435 | |
| 436 // Test the cases where the stream handled by MaybeInterceptAsStream | 803 // Test the cases where the stream handled by MaybeInterceptAsStream |
| 437 // with plugin not available. This is the case when intercepting streams for | 804 // with plugin not available. This is the case when intercepting streams for |
| 438 // the streamsPrivate extensions API. | 805 // the streamsPrivate extensions API. |
| 439 set_stream_has_handler(true); | 806 set_stream_has_handler(true); |
| 440 set_plugin_available(false); | 807 set_plugin_available(false); |
| 441 allow_download = false; | 808 allow_download = false; |
| 442 must_download = false; | 809 must_download = false; |
| 443 resource_type = RESOURCE_TYPE_OBJECT; | 810 resource_type = RESOURCE_TYPE_OBJECT; |
| 444 EXPECT_TRUE( | 811 EXPECT_TRUE( |
| 445 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 812 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 446 | 813 |
| 447 // Test the cases where the stream handled by MaybeInterceptAsStream | 814 // Test the cases where the stream handled by MaybeInterceptAsStream |
| 448 // with plugin not available. This is the case when intercepting streams for | 815 // with plugin not available. This is the case when intercepting streams for |
| 449 // the streamsPrivate extensions API with stale plugin. | 816 // the streamsPrivate extensions API with stale plugin. |
| 450 set_plugin_stale(true); | 817 set_plugin_stale(true); |
| 451 allow_download = false; | 818 allow_download = false; |
| 452 must_download = false; | 819 must_download = false; |
| 453 resource_type = RESOURCE_TYPE_OBJECT; | 820 resource_type = RESOURCE_TYPE_OBJECT; |
| 454 EXPECT_TRUE( | 821 EXPECT_TRUE( |
| 455 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 822 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
| 456 } | 823 } |
| 457 #endif | 824 #endif |
| 458 | 825 |
| 459 } // namespace | 826 // Test that the MimeSniffingHandler operates properly when it doesn't sniff |
| 827 // resources. |
| 828 TEST_F(MimeSniffingResourceHandlerTest, NoSniffing) { |
| 829 // Test simple case. |
| 830 TestHandlerNoSniffing( |
| 831 true /* response_started_succeeds */, |
| 832 false /* defer_response_started */, |
| 833 true /* will_read_succeeds */, |
| 834 true /* read_completed_succeeds */, |
| 835 false /* defer_read_completed */); |
| 836 |
| 837 // Test deferral in OnResponseStarted and/or in OnReadCompleted. |
| 838 TestHandlerNoSniffing( |
| 839 true /* response_started_succeeds */, |
| 840 true /* defer_response_started */, |
| 841 true /* will_read_succeeds */, |
| 842 true /* read_completed_succeeds */, |
| 843 false /* defer_read_completed */); |
| 844 TestHandlerNoSniffing( |
| 845 true /* response_started_succeeds */, |
| 846 false /* defer_response_started */, |
| 847 true /* will_read_succeeds */, |
| 848 true /* read_completed_succeeds */, |
| 849 true /* defer_read_completed */); |
| 850 TestHandlerNoSniffing( |
| 851 true /* response_started_succeeds */, |
| 852 true /* defer_response_started */, |
| 853 true /* will_read_succeeds */, |
| 854 true /* read_completed_succeeds */, |
| 855 true /* defer_read_completed */); |
| 856 |
| 857 // Test cancel in OnResponseStarted, OnWillRead, OnReadCompleted. |
| 858 TestHandlerNoSniffing( |
| 859 false /* response_started_succeeds */, |
| 860 false /* defer_response_started */, |
| 861 false /* will_read_succeeds */, |
| 862 false /* read_completed_succeeds */, |
| 863 false /* defer_read_completed */); |
| 864 TestHandlerNoSniffing( |
| 865 true /* response_started_succeeds */, |
| 866 false /* defer_response_started */, |
| 867 false /* will_read_succeeds */, |
| 868 false /* read_completed_succeeds */, |
| 869 false /* defer_read_completed */); |
| 870 TestHandlerNoSniffing( |
| 871 true /* response_started_succeeds */, |
| 872 false /* defer_response_started */, |
| 873 true /* will_read_succeeds */, |
| 874 false /* read_completed_succeeds */, |
| 875 false /* defer_read_completed */); |
| 876 |
| 877 // Test cancel after OnResponseStarted deferral. |
| 878 TestHandlerNoSniffing( |
| 879 true /* response_started_succeeds */, |
| 880 true /* defer_response_started */, |
| 881 false /* will_read_succeeds */, |
| 882 false /* read_completed_succeeds */, |
| 883 false /* defer_read_completed */); |
| 884 TestHandlerNoSniffing( |
| 885 true /* response_started_succeeds */, |
| 886 true /* defer_response_started */, |
| 887 true /* will_read_succeeds */, |
| 888 false /* read_completed_succeeds */, |
| 889 false /* defer_read_completed */); |
| 890 |
| 891 content::RunAllPendingInMessageLoop(); |
| 892 } |
| 893 |
| 894 // Test that the MimeSniffingHandler operates properly when it sniffs |
| 895 // resources. |
| 896 TEST_F(MimeSniffingResourceHandlerTest, Sniffing) { |
| 897 // Test simple case. |
| 898 TestHandlerSniffing( |
| 899 true /* response_started_succeeds */, |
| 900 false /* defer_response_started */, |
| 901 true /* will_read_succeeds */, |
| 902 true /* read_completed_succeeds */, |
| 903 false /* defer_read_completed */); |
| 904 |
| 905 // Test deferral in OnResponseStarted and/or in OnReadCompleted. |
| 906 TestHandlerSniffing( |
| 907 true /* response_started_succeeds */, |
| 908 true /* defer_response_started */, |
| 909 true /* will_read_succeeds */, |
| 910 true /* read_completed_succeeds */, |
| 911 false /* defer_read_completed */); |
| 912 TestHandlerSniffing( |
| 913 true /* response_started_succeeds */, |
| 914 false /* defer_response_started */, |
| 915 true /* will_read_succeeds */, |
| 916 true /* read_completed_succeeds */, |
| 917 true /* defer_read_completed */); |
| 918 TestHandlerSniffing( |
| 919 true /* response_started_succeeds */, |
| 920 true /* defer_response_started */, |
| 921 true /* will_read_succeeds */, |
| 922 true /* read_completed_succeeds */, |
| 923 true /* defer_read_completed */); |
| 924 |
| 925 // Test cancel in OnResponseStarted, OnWillRead, OnReadCompleted. |
| 926 TestHandlerSniffing( |
| 927 false /* response_started_succeeds */, |
| 928 false /* defer_response_started */, |
| 929 false /* will_read_succeeds */, |
| 930 false /* read_completed_succeeds */, |
| 931 false /* defer_read_completed */); |
| 932 TestHandlerSniffing( |
| 933 true /* response_started_succeeds */, |
| 934 false /* defer_response_started */, |
| 935 false /* will_read_succeeds */, |
| 936 false /* read_completed_succeeds */, |
| 937 false /* defer_read_completed */); |
| 938 TestHandlerSniffing( |
| 939 true /* response_started_succeeds */, |
| 940 false /* defer_response_started */, |
| 941 true /* will_read_succeeds */, |
| 942 false /* read_completed_succeeds */, |
| 943 false /* defer_read_completed */); |
| 944 |
| 945 // Test cancel after OnResponseStarted deferral. |
| 946 TestHandlerSniffing( |
| 947 true /* response_started_succeeds */, |
| 948 true /* defer_response_started */, |
| 949 false /* will_read_succeeds */, |
| 950 false /* read_completed_succeeds */, |
| 951 false /* defer_read_completed */); |
| 952 TestHandlerSniffing( |
| 953 true /* response_started_succeeds */, |
| 954 true /* defer_response_started */, |
| 955 true /* will_read_succeeds */, |
| 956 false /* read_completed_succeeds */, |
| 957 false /* defer_read_completed */); |
| 958 |
| 959 content::RunAllPendingInMessageLoop(); |
| 960 } |
| 961 |
| 962 // Tests that 304s do not trigger a change in handlers. |
| 963 TEST_F(MimeSniffingResourceHandlerTest, 304Handling) { |
| 964 net::URLRequestContext context; |
| 965 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 966 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 967 ResourceRequestInfo::AllocateForTesting(request.get(), |
| 968 RESOURCE_TYPE_MAIN_FRAME, |
| 969 nullptr, // context |
| 970 0, // render_process_id |
| 971 0, // render_view_id |
| 972 0, // render_frame_id |
| 973 true, // is_main_frame |
| 974 false, // parent_is_main_frame |
| 975 true, // allow_download |
| 976 true, // is_async |
| 977 false); // is_using_lofi |
| 978 |
| 979 TestResourceDispatcherHost host(false); |
| 980 TestResourceDispatcherHostDelegate host_delegate(false); |
| 981 host.SetDelegate(&host_delegate); |
| 982 |
| 983 TestFakePluginService plugin_service(false, false); |
| 984 std::unique_ptr<ResourceHandler> intercepting_handler( |
| 985 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
| 986 nullptr)); |
| 987 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( |
| 988 std::unique_ptr<ResourceHandler>( |
| 989 new TestResourceHandler(true, false, true, true, false)), |
| 990 &host, &plugin_service, |
| 991 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), |
| 992 request.get())); |
| 993 |
| 994 TestResourceController resource_controller; |
| 995 mime_handler->SetController(&resource_controller); |
| 996 |
| 997 // Simulate a 304 response. |
| 998 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 999 // The MIME type isn't important but it shouldn't be empty. |
| 1000 response->head.mime_type = "application/pdf"; |
| 1001 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); |
| 1002 |
| 1003 // The response is received. No new ResourceHandler should be created to |
| 1004 // handle the download. |
| 1005 bool defer = false; |
| 1006 mime_handler->OnResponseStarted(response.get(), &defer); |
| 1007 EXPECT_FALSE(defer); |
| 1008 EXPECT_FALSE(host.new_resource_handler()); |
| 1009 |
| 1010 content::RunAllPendingInMessageLoop(); |
| 1011 } |
| 460 | 1012 |
| 461 } // namespace content | 1013 } // namespace content |
| OLD | NEW |