| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/resource_loader.h" | 5 #include "content/browser/loader/resource_loader.h" |
| 6 | 6 |
| 7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 // Stub client certificate store that returns a preset list of certificates for | 38 // Stub client certificate store that returns a preset list of certificates for |
| 39 // each request and records the arguments of the most recent request for later | 39 // each request and records the arguments of the most recent request for later |
| 40 // inspection. | 40 // inspection. |
| 41 class ClientCertStoreStub : public net::ClientCertStore { | 41 class ClientCertStoreStub : public net::ClientCertStore { |
| 42 public: | 42 public: |
| 43 ClientCertStoreStub(const net::CertificateList& certs) | 43 ClientCertStoreStub(const net::CertificateList& certs) |
| 44 : response_(certs), | 44 : response_(certs), |
| 45 request_count_(0) {} | 45 request_count_(0) {} |
| 46 | 46 |
| 47 virtual ~ClientCertStoreStub() {} | 47 ~ClientCertStoreStub() override {} |
| 48 | 48 |
| 49 // Returns |cert_authorities| field of the certificate request passed in the | 49 // Returns |cert_authorities| field of the certificate request passed in the |
| 50 // most recent call to GetClientCerts(). | 50 // most recent call to GetClientCerts(). |
| 51 // TODO(ppi): Make the stub independent from the internal representation of | 51 // TODO(ppi): Make the stub independent from the internal representation of |
| 52 // SSLCertRequestInfo. For now it seems that we cannot neither save the | 52 // SSLCertRequestInfo. For now it seems that we cannot neither save the |
| 53 // scoped_refptr<> (since it is never passed to us) nor copy the entire | 53 // scoped_refptr<> (since it is never passed to us) nor copy the entire |
| 54 // CertificateRequestInfo (since there is no copy constructor). | 54 // CertificateRequestInfo (since there is no copy constructor). |
| 55 std::vector<std::string> requested_authorities() { | 55 std::vector<std::string> requested_authorities() { |
| 56 return requested_authorities_; | 56 return requested_authorities_; |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Returns the number of calls to GetClientCerts(). | 59 // Returns the number of calls to GetClientCerts(). |
| 60 int request_count() { | 60 int request_count() { |
| 61 return request_count_; | 61 return request_count_; |
| 62 } | 62 } |
| 63 | 63 |
| 64 // net::ClientCertStore: | 64 // net::ClientCertStore: |
| 65 virtual void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, | 65 void GetClientCerts(const net::SSLCertRequestInfo& cert_request_info, |
| 66 net::CertificateList* selected_certs, | 66 net::CertificateList* selected_certs, |
| 67 const base::Closure& callback) override { | 67 const base::Closure& callback) override { |
| 68 ++request_count_; | 68 ++request_count_; |
| 69 requested_authorities_ = cert_request_info.cert_authorities; | 69 requested_authorities_ = cert_request_info.cert_authorities; |
| 70 *selected_certs = response_; | 70 *selected_certs = response_; |
| 71 callback.Run(); | 71 callback.Run(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 private: | 74 private: |
| 75 const net::CertificateList response_; | 75 const net::CertificateList response_; |
| 76 int request_count_; | 76 int request_count_; |
| 77 std::vector<std::string> requested_authorities_; | 77 std::vector<std::string> requested_authorities_; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 return received_response_completed_; | 120 return received_response_completed_; |
| 121 } | 121 } |
| 122 const net::URLRequestStatus& status() const { return status_; } | 122 const net::URLRequestStatus& status() const { return status_; } |
| 123 int total_bytes_downloaded() const { return total_bytes_downloaded_; } | 123 int total_bytes_downloaded() const { return total_bytes_downloaded_; } |
| 124 | 124 |
| 125 void Resume() { | 125 void Resume() { |
| 126 controller()->Resume(); | 126 controller()->Resume(); |
| 127 } | 127 } |
| 128 | 128 |
| 129 // ResourceHandler implementation: | 129 // ResourceHandler implementation: |
| 130 virtual bool OnUploadProgress(uint64 position, uint64 size) override { | 130 bool OnUploadProgress(uint64 position, uint64 size) override { |
| 131 NOTREACHED(); | 131 NOTREACHED(); |
| 132 return true; | 132 return true; |
| 133 } | 133 } |
| 134 | 134 |
| 135 virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | 135 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, |
| 136 ResourceResponse* response, | 136 ResourceResponse* response, |
| 137 bool* defer) override { | 137 bool* defer) override { |
| 138 NOTREACHED(); | 138 NOTREACHED(); |
| 139 return true; | 139 return true; |
| 140 } | 140 } |
| 141 | 141 |
| 142 virtual bool OnResponseStarted(ResourceResponse* response, | 142 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { |
| 143 bool* defer) override { | |
| 144 EXPECT_FALSE(response_.get()); | 143 EXPECT_FALSE(response_.get()); |
| 145 response_ = response; | 144 response_ = response; |
| 146 return true; | 145 return true; |
| 147 } | 146 } |
| 148 | 147 |
| 149 virtual bool OnWillStart(const GURL& url, bool* defer) override { | 148 bool OnWillStart(const GURL& url, bool* defer) override { |
| 150 EXPECT_TRUE(start_url_.is_empty()); | 149 EXPECT_TRUE(start_url_.is_empty()); |
| 151 start_url_ = url; | 150 start_url_ = url; |
| 152 *defer = defer_request_on_will_start_; | 151 *defer = defer_request_on_will_start_; |
| 153 return true; | 152 return true; |
| 154 } | 153 } |
| 155 | 154 |
| 156 virtual bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { | 155 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { |
| 157 return true; | 156 return true; |
| 158 } | 157 } |
| 159 | 158 |
| 160 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 159 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 161 int* buf_size, | 160 int* buf_size, |
| 162 int min_size) override { | 161 int min_size) override { |
| 163 EXPECT_TRUE(expect_reads_); | 162 EXPECT_TRUE(expect_reads_); |
| 164 EXPECT_FALSE(received_on_will_read_); | 163 EXPECT_FALSE(received_on_will_read_); |
| 165 EXPECT_FALSE(received_eof_); | 164 EXPECT_FALSE(received_eof_); |
| 166 EXPECT_FALSE(received_response_completed_); | 165 EXPECT_FALSE(received_response_completed_); |
| 167 | 166 |
| 168 *buf = read_buffer_; | 167 *buf = read_buffer_; |
| 169 *buf_size = kReadBufSize; | 168 *buf_size = kReadBufSize; |
| 170 received_on_will_read_ = true; | 169 received_on_will_read_ = true; |
| 171 return true; | 170 return true; |
| 172 } | 171 } |
| 173 | 172 |
| 174 virtual bool OnReadCompleted(int bytes_read, bool* defer) override { | 173 bool OnReadCompleted(int bytes_read, bool* defer) override { |
| 175 EXPECT_TRUE(received_on_will_read_); | 174 EXPECT_TRUE(received_on_will_read_); |
| 176 EXPECT_TRUE(expect_reads_); | 175 EXPECT_TRUE(expect_reads_); |
| 177 EXPECT_FALSE(received_response_completed_); | 176 EXPECT_FALSE(received_response_completed_); |
| 178 | 177 |
| 179 if (bytes_read == 0) { | 178 if (bytes_read == 0) { |
| 180 received_eof_ = true; | 179 received_eof_ = true; |
| 181 if (defer_eof_) { | 180 if (defer_eof_) { |
| 182 defer_eof_ = false; | 181 defer_eof_ = false; |
| 183 *defer = true; | 182 *defer = true; |
| 184 } | 183 } |
| 185 } | 184 } |
| 186 | 185 |
| 187 // Need another OnWillRead() call before seeing an OnReadCompleted(). | 186 // Need another OnWillRead() call before seeing an OnReadCompleted(). |
| 188 received_on_will_read_ = false; | 187 received_on_will_read_ = false; |
| 189 | 188 |
| 190 return !cancel_on_read_completed_; | 189 return !cancel_on_read_completed_; |
| 191 } | 190 } |
| 192 | 191 |
| 193 virtual void OnResponseCompleted(const net::URLRequestStatus& status, | 192 void OnResponseCompleted(const net::URLRequestStatus& status, |
| 194 const std::string& security_info, | 193 const std::string& security_info, |
| 195 bool* defer) override { | 194 bool* defer) override { |
| 196 EXPECT_FALSE(received_response_completed_); | 195 EXPECT_FALSE(received_response_completed_); |
| 197 if (status.is_success() && expect_reads_) | 196 if (status.is_success() && expect_reads_) |
| 198 EXPECT_TRUE(received_eof_); | 197 EXPECT_TRUE(received_eof_); |
| 199 | 198 |
| 200 received_response_completed_ = true; | 199 received_response_completed_ = true; |
| 201 status_ = status; | 200 status_ = status; |
| 202 } | 201 } |
| 203 | 202 |
| 204 virtual void OnDataDownloaded(int bytes_downloaded) override { | 203 void OnDataDownloaded(int bytes_downloaded) override { |
| 205 EXPECT_FALSE(expect_reads_); | 204 EXPECT_FALSE(expect_reads_); |
| 206 total_bytes_downloaded_ += bytes_downloaded; | 205 total_bytes_downloaded_ += bytes_downloaded; |
| 207 } | 206 } |
| 208 | 207 |
| 209 private: | 208 private: |
| 210 scoped_refptr<net::IOBuffer> read_buffer_; | 209 scoped_refptr<net::IOBuffer> read_buffer_; |
| 211 | 210 |
| 212 bool defer_request_on_will_start_; | 211 bool defer_request_on_will_start_; |
| 213 bool expect_reads_; | 212 bool expect_reads_; |
| 214 bool cancel_on_read_completed_; | 213 bool cancel_on_read_completed_; |
| 215 bool defer_eof_; | 214 bool defer_eof_; |
| 216 | 215 |
| 217 GURL start_url_; | 216 GURL start_url_; |
| 218 scoped_refptr<ResourceResponse> response_; | 217 scoped_refptr<ResourceResponse> response_; |
| 219 bool received_on_will_read_; | 218 bool received_on_will_read_; |
| 220 bool received_eof_; | 219 bool received_eof_; |
| 221 bool received_response_completed_; | 220 bool received_response_completed_; |
| 222 net::URLRequestStatus status_; | 221 net::URLRequestStatus status_; |
| 223 int total_bytes_downloaded_; | 222 int total_bytes_downloaded_; |
| 224 }; | 223 }; |
| 225 | 224 |
| 226 // Test browser client that captures calls to SelectClientCertificates and | 225 // Test browser client that captures calls to SelectClientCertificates and |
| 227 // records the arguments of the most recent call for later inspection. | 226 // records the arguments of the most recent call for later inspection. |
| 228 class SelectCertificateBrowserClient : public TestContentBrowserClient { | 227 class SelectCertificateBrowserClient : public TestContentBrowserClient { |
| 229 public: | 228 public: |
| 230 SelectCertificateBrowserClient() : call_count_(0) {} | 229 SelectCertificateBrowserClient() : call_count_(0) {} |
| 231 | 230 |
| 232 virtual void SelectClientCertificate( | 231 void SelectClientCertificate( |
| 233 int render_process_id, | 232 int render_process_id, |
| 234 int render_view_id, | 233 int render_view_id, |
| 235 net::SSLCertRequestInfo* cert_request_info, | 234 net::SSLCertRequestInfo* cert_request_info, |
| 236 const base::Callback<void(net::X509Certificate*)>& callback) override { | 235 const base::Callback<void(net::X509Certificate*)>& callback) override { |
| 237 ++call_count_; | 236 ++call_count_; |
| 238 passed_certs_ = cert_request_info->client_certs; | 237 passed_certs_ = cert_request_info->client_certs; |
| 239 } | 238 } |
| 240 | 239 |
| 241 int call_count() { | 240 int call_count() { |
| 242 return call_count_; | 241 return call_count_; |
| 243 } | 242 } |
| 244 | 243 |
| 245 net::CertificateList passed_certs() { | 244 net::CertificateList passed_certs() { |
| 246 return passed_certs_; | 245 return passed_certs_; |
| 247 } | 246 } |
| 248 | 247 |
| 249 private: | 248 private: |
| 250 net::CertificateList passed_certs_; | 249 net::CertificateList passed_certs_; |
| 251 int call_count_; | 250 int call_count_; |
| 252 }; | 251 }; |
| 253 | 252 |
| 254 class ResourceContextStub : public MockResourceContext { | 253 class ResourceContextStub : public MockResourceContext { |
| 255 public: | 254 public: |
| 256 explicit ResourceContextStub(net::URLRequestContext* test_request_context) | 255 explicit ResourceContextStub(net::URLRequestContext* test_request_context) |
| 257 : MockResourceContext(test_request_context) {} | 256 : MockResourceContext(test_request_context) {} |
| 258 | 257 |
| 259 virtual scoped_ptr<net::ClientCertStore> CreateClientCertStore() override { | 258 scoped_ptr<net::ClientCertStore> CreateClientCertStore() override { |
| 260 return dummy_cert_store_.Pass(); | 259 return dummy_cert_store_.Pass(); |
| 261 } | 260 } |
| 262 | 261 |
| 263 void SetClientCertStore(scoped_ptr<net::ClientCertStore> store) { | 262 void SetClientCertStore(scoped_ptr<net::ClientCertStore> store) { |
| 264 dummy_cert_store_ = store.Pass(); | 263 dummy_cert_store_ = store.Pass(); |
| 265 } | 264 } |
| 266 | 265 |
| 267 private: | 266 private: |
| 268 scoped_ptr<net::ClientCertStore> dummy_cert_store_; | 267 scoped_ptr<net::ClientCertStore> dummy_cert_store_; |
| 269 }; | 268 }; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 scoped_ptr<ResourceHandlerStub> resource_handler( | 327 scoped_ptr<ResourceHandlerStub> resource_handler( |
| 329 new ResourceHandlerStub(request.get())); | 328 new ResourceHandlerStub(request.get())); |
| 330 raw_ptr_resource_handler_ = resource_handler.get(); | 329 raw_ptr_resource_handler_ = resource_handler.get(); |
| 331 loader_.reset(new ResourceLoader( | 330 loader_.reset(new ResourceLoader( |
| 332 request.Pass(), | 331 request.Pass(), |
| 333 WrapResourceHandler(resource_handler.Pass(), raw_ptr_to_request_), | 332 WrapResourceHandler(resource_handler.Pass(), raw_ptr_to_request_), |
| 334 this)); | 333 this)); |
| 335 } | 334 } |
| 336 | 335 |
| 337 // ResourceLoaderDelegate: | 336 // ResourceLoaderDelegate: |
| 338 virtual ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( | 337 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( |
| 339 ResourceLoader* loader, | 338 ResourceLoader* loader, |
| 340 net::AuthChallengeInfo* auth_info) override { | 339 net::AuthChallengeInfo* auth_info) override { |
| 341 return NULL; | 340 return NULL; |
| 342 } | 341 } |
| 343 virtual bool HandleExternalProtocol(ResourceLoader* loader, | 342 bool HandleExternalProtocol(ResourceLoader* loader, |
| 344 const GURL& url) override { | 343 const GURL& url) override { |
| 345 return false; | 344 return false; |
| 346 } | 345 } |
| 347 virtual void DidStartRequest(ResourceLoader* loader) override {} | 346 void DidStartRequest(ResourceLoader* loader) override {} |
| 348 virtual void DidReceiveRedirect(ResourceLoader* loader, | 347 void DidReceiveRedirect(ResourceLoader* loader, |
| 349 const GURL& new_url) override {} | 348 const GURL& new_url) override {} |
| 350 virtual void DidReceiveResponse(ResourceLoader* loader) override {} | 349 void DidReceiveResponse(ResourceLoader* loader) override {} |
| 351 virtual void DidFinishLoading(ResourceLoader* loader) override {} | 350 void DidFinishLoading(ResourceLoader* loader) override {} |
| 352 | 351 |
| 353 content::TestBrowserThreadBundle thread_bundle_; | 352 content::TestBrowserThreadBundle thread_bundle_; |
| 354 | 353 |
| 355 net::URLRequestJobFactoryImpl job_factory_; | 354 net::URLRequestJobFactoryImpl job_factory_; |
| 356 net::TestURLRequestContext test_url_request_context_; | 355 net::TestURLRequestContext test_url_request_context_; |
| 357 ResourceContextStub resource_context_; | 356 ResourceContextStub resource_context_; |
| 358 | 357 |
| 359 // The ResourceLoader owns the URLRequest and the ResourceHandler. | 358 // The ResourceLoader owns the URLRequest and the ResourceHandler. |
| 360 ResourceHandlerStub* raw_ptr_resource_handler_; | 359 ResourceHandlerStub* raw_ptr_resource_handler_; |
| 361 net::URLRequest* raw_ptr_to_request_; | 360 net::URLRequest* raw_ptr_to_request_; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const { | 489 RedirectToFileResourceHandler* redirect_to_file_resource_handler() const { |
| 491 return redirect_to_file_resource_handler_; | 490 return redirect_to_file_resource_handler_; |
| 492 } | 491 } |
| 493 | 492 |
| 494 void ReleaseLoader() { | 493 void ReleaseLoader() { |
| 495 file_stream_ = NULL; | 494 file_stream_ = NULL; |
| 496 deletable_file_ = NULL; | 495 deletable_file_ = NULL; |
| 497 loader_.reset(); | 496 loader_.reset(); |
| 498 } | 497 } |
| 499 | 498 |
| 500 virtual scoped_ptr<ResourceHandler> WrapResourceHandler( | 499 scoped_ptr<ResourceHandler> WrapResourceHandler( |
| 501 scoped_ptr<ResourceHandlerStub> leaf_handler, | 500 scoped_ptr<ResourceHandlerStub> leaf_handler, |
| 502 net::URLRequest* request) override { | 501 net::URLRequest* request) override { |
| 503 leaf_handler->set_expect_reads(false); | 502 leaf_handler->set_expect_reads(false); |
| 504 | 503 |
| 505 // Make a temporary file. | 504 // Make a temporary file. |
| 506 CHECK(base::CreateTemporaryFile(&temp_path_)); | 505 CHECK(base::CreateTemporaryFile(&temp_path_)); |
| 507 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | | 506 int flags = base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | |
| 508 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; | 507 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_ASYNC; |
| 509 base::File file(temp_path_, flags); | 508 base::File file(temp_path_, flags); |
| 510 CHECK(file.IsValid()); | 509 CHECK(file.IsValid()); |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); | 718 ASSERT_TRUE(base::ReadFileToString(temp_path(), &contents)); |
| 720 EXPECT_EQ(test_data(), contents); | 719 EXPECT_EQ(test_data(), contents); |
| 721 | 720 |
| 722 // Release the loader. The file should be gone now. | 721 // Release the loader. The file should be gone now. |
| 723 ReleaseLoader(); | 722 ReleaseLoader(); |
| 724 base::RunLoop().RunUntilIdle(); | 723 base::RunLoop().RunUntilIdle(); |
| 725 EXPECT_FALSE(base::PathExists(temp_path())); | 724 EXPECT_FALSE(base::PathExists(temp_path())); |
| 726 } | 725 } |
| 727 | 726 |
| 728 } // namespace content | 727 } // namespace content |
| OLD | NEW |