| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 | 10 |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 network_read_callback_(this, &Transaction::OnNetworkReadCompleted)), | 187 network_read_callback_(this, &Transaction::OnNetworkReadCompleted)), |
| 188 ALLOW_THIS_IN_INITIALIZER_LIST( | 188 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 189 cache_read_callback_(new CancelableCompletionCallback<Transaction>( | 189 cache_read_callback_(new CancelableCompletionCallback<Transaction>( |
| 190 this, &Transaction::OnCacheReadCompleted))) { | 190 this, &Transaction::OnCacheReadCompleted))) { |
| 191 } | 191 } |
| 192 | 192 |
| 193 // Clean up the transaction. | 193 // Clean up the transaction. |
| 194 virtual ~Transaction(); | 194 virtual ~Transaction(); |
| 195 | 195 |
| 196 // HttpTransaction methods: | 196 // HttpTransaction methods: |
| 197 virtual int Start(const HttpRequestInfo*, CompletionCallback*); | 197 virtual int Start(LoadLog*, const HttpRequestInfo*, CompletionCallback*); |
| 198 virtual int RestartIgnoringLastError(CompletionCallback*); | 198 virtual int RestartIgnoringLastError(CompletionCallback*); |
| 199 virtual int RestartWithCertificate(X509Certificate* client_cert, | 199 virtual int RestartWithCertificate(X509Certificate* client_cert, |
| 200 CompletionCallback* callback); | 200 CompletionCallback* callback); |
| 201 virtual int RestartWithAuth(const std::wstring& username, | 201 virtual int RestartWithAuth(const std::wstring& username, |
| 202 const std::wstring& password, | 202 const std::wstring& password, |
| 203 CompletionCallback* callback); | 203 CompletionCallback* callback); |
| 204 virtual bool IsReadyToRestartForAuth(); | 204 virtual bool IsReadyToRestartForAuth(); |
| 205 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback*); | 205 virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback*); |
| 206 virtual const HttpResponseInfo* GetResponseInfo() const; | 206 virtual const HttpResponseInfo* GetResponseInfo() const; |
| 207 virtual LoadState GetLoadState() const; | 207 virtual LoadState GetLoadState() const; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 | 249 |
| 250 private: | 250 private: |
| 251 // This is a helper function used to trigger a completion callback. It may | 251 // This is a helper function used to trigger a completion callback. It may |
| 252 // only be called if callback_ is non-null. | 252 // only be called if callback_ is non-null. |
| 253 void DoCallback(int rv); | 253 void DoCallback(int rv); |
| 254 | 254 |
| 255 // This will trigger the completion callback if appropriate. | 255 // This will trigger the completion callback if appropriate. |
| 256 int HandleResult(int rv); | 256 int HandleResult(int rv); |
| 257 | 257 |
| 258 // Sets request_ and fields derived from it. | 258 // Sets request_ and fields derived from it. |
| 259 void SetRequest(const HttpRequestInfo* request); | 259 void SetRequest(LoadLog* load_log, const HttpRequestInfo* request); |
| 260 | 260 |
| 261 // Returns true if the request should be handled exclusively by the network | 261 // Returns true if the request should be handled exclusively by the network |
| 262 // layer (skipping the cache entirely). | 262 // layer (skipping the cache entirely). |
| 263 bool ShouldPassThrough(); | 263 bool ShouldPassThrough(); |
| 264 | 264 |
| 265 // Returns true if we should force an end-to-end fetch. | 265 // Returns true if we should force an end-to-end fetch. |
| 266 bool ShouldBypassCache(); | 266 bool ShouldBypassCache(); |
| 267 | 267 |
| 268 // Called to begin reading from the cache. Returns network error code. | 268 // Called to begin reading from the cache. Returns network error code. |
| 269 int BeginCacheRead(); | 269 int BeginCacheRead(); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 | 358 |
| 359 // Called to signal completion of the network transaction's Start method: | 359 // Called to signal completion of the network transaction's Start method: |
| 360 void OnNetworkInfoAvailable(int result); | 360 void OnNetworkInfoAvailable(int result); |
| 361 | 361 |
| 362 // Called to signal completion of the network transaction's Read method: | 362 // Called to signal completion of the network transaction's Read method: |
| 363 void OnNetworkReadCompleted(int result); | 363 void OnNetworkReadCompleted(int result); |
| 364 | 364 |
| 365 // Called to signal completion of the cache's ReadData method: | 365 // Called to signal completion of the cache's ReadData method: |
| 366 void OnCacheReadCompleted(int result); | 366 void OnCacheReadCompleted(int result); |
| 367 | 367 |
| 368 scoped_refptr<LoadLog> load_log_; |
| 368 const HttpRequestInfo* request_; | 369 const HttpRequestInfo* request_; |
| 369 scoped_ptr<HttpRequestInfo> custom_request_; | 370 scoped_ptr<HttpRequestInfo> custom_request_; |
| 370 // If extra_headers specified a "if-modified-since" or "if-none-match", | 371 // If extra_headers specified a "if-modified-since" or "if-none-match", |
| 371 // |external_validation_| contains the value of that header. | 372 // |external_validation_| contains the value of that header. |
| 372 ValidationHeader external_validation_; | 373 ValidationHeader external_validation_; |
| 373 HttpCache* cache_; | 374 HttpCache* cache_; |
| 374 HttpCache::ActiveEntry* entry_; | 375 HttpCache::ActiveEntry* entry_; |
| 375 scoped_ptr<HttpTransaction> network_trans_; | 376 scoped_ptr<HttpTransaction> network_trans_; |
| 376 CompletionCallback* callback_; // Consumer's callback. | 377 CompletionCallback* callback_; // Consumer's callback. |
| 377 HttpResponseInfo response_; | 378 HttpResponseInfo response_; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 402 | 403 |
| 403 // If there is an outstanding callback, mark it as cancelled so running it | 404 // If there is an outstanding callback, mark it as cancelled so running it |
| 404 // does nothing. | 405 // does nothing. |
| 405 cache_read_callback_->Cancel(); | 406 cache_read_callback_->Cancel(); |
| 406 | 407 |
| 407 // We could still have a cache read in progress, so we just null the cache_ | 408 // We could still have a cache read in progress, so we just null the cache_ |
| 408 // pointer to signal that we are dead. See OnCacheReadCompleted. | 409 // pointer to signal that we are dead. See OnCacheReadCompleted. |
| 409 cache_ = NULL; | 410 cache_ = NULL; |
| 410 } | 411 } |
| 411 | 412 |
| 412 int HttpCache::Transaction::Start(const HttpRequestInfo* request, | 413 int HttpCache::Transaction::Start(LoadLog* load_log, |
| 414 const HttpRequestInfo* request, |
| 413 CompletionCallback* callback) { | 415 CompletionCallback* callback) { |
| 414 DCHECK(request); | 416 DCHECK(request); |
| 415 DCHECK(callback); | 417 DCHECK(callback); |
| 416 | 418 |
| 417 // ensure that we only have one asynchronous call at a time. | 419 // ensure that we only have one asynchronous call at a time. |
| 418 DCHECK(!callback_); | 420 DCHECK(!callback_); |
| 419 | 421 |
| 420 if (revoked()) | 422 if (revoked()) |
| 421 return ERR_UNEXPECTED; | 423 return ERR_UNEXPECTED; |
| 422 | 424 |
| 423 SetRequest(request); | 425 SetRequest(load_log, request); |
| 424 | 426 |
| 425 int rv; | 427 int rv; |
| 426 | 428 |
| 427 if (!ShouldPassThrough()) { | 429 if (!ShouldPassThrough()) { |
| 428 cache_key_ = cache_->GenerateCacheKey(request); | 430 cache_key_ = cache_->GenerateCacheKey(request); |
| 429 | 431 |
| 430 // requested cache access mode | 432 // requested cache access mode |
| 431 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { | 433 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { |
| 432 mode_ = READ; | 434 mode_ = READ; |
| 433 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) { | 435 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) { |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 c->Run(rv); | 704 c->Run(rv); |
| 703 } | 705 } |
| 704 | 706 |
| 705 int HttpCache::Transaction::HandleResult(int rv) { | 707 int HttpCache::Transaction::HandleResult(int rv) { |
| 706 DCHECK(rv != ERR_IO_PENDING); | 708 DCHECK(rv != ERR_IO_PENDING); |
| 707 if (callback_) | 709 if (callback_) |
| 708 DoCallback(rv); | 710 DoCallback(rv); |
| 709 return rv; | 711 return rv; |
| 710 } | 712 } |
| 711 | 713 |
| 712 void HttpCache::Transaction::SetRequest(const HttpRequestInfo* request) { | 714 void HttpCache::Transaction::SetRequest(LoadLog* load_log, |
| 715 const HttpRequestInfo* request) { |
| 716 load_log_ = load_log; |
| 713 request_ = request; | 717 request_ = request; |
| 714 effective_load_flags_ = request_->load_flags; | 718 effective_load_flags_ = request_->load_flags; |
| 715 | 719 |
| 716 switch (cache_->mode()) { | 720 switch (cache_->mode()) { |
| 717 case NORMAL: | 721 case NORMAL: |
| 718 break; | 722 break; |
| 719 case RECORD: | 723 case RECORD: |
| 720 // When in record mode, we want to NEVER load from the cache. | 724 // When in record mode, we want to NEVER load from the cache. |
| 721 // The reason for this is beacuse we save the Set-Cookie headers | 725 // The reason for this is beacuse we save the Set-Cookie headers |
| 722 // (intentionally). If we read from the cache, we replay them | 726 // (intentionally). If we read from the cache, we replay them |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 971 } | 975 } |
| 972 | 976 |
| 973 int HttpCache::Transaction::BeginNetworkRequest() { | 977 int HttpCache::Transaction::BeginNetworkRequest() { |
| 974 DCHECK(mode_ & WRITE || mode_ == NONE); | 978 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 975 DCHECK(!network_trans_.get()); | 979 DCHECK(!network_trans_.get()); |
| 976 | 980 |
| 977 network_trans_.reset(cache_->network_layer_->CreateTransaction()); | 981 network_trans_.reset(cache_->network_layer_->CreateTransaction()); |
| 978 if (!network_trans_.get()) | 982 if (!network_trans_.get()) |
| 979 return net::ERR_CACHE_CANNOT_CREATE_NETWORK_TRANSACTION; | 983 return net::ERR_CACHE_CANNOT_CREATE_NETWORK_TRANSACTION; |
| 980 | 984 |
| 981 int rv = network_trans_->Start(request_, &network_info_callback_); | 985 int rv = network_trans_->Start(load_log_, request_, &network_info_callback_); |
| 982 if (rv != ERR_IO_PENDING) | 986 if (rv != ERR_IO_PENDING) |
| 983 OnNetworkInfoAvailable(rv); | 987 OnNetworkInfoAvailable(rv); |
| 984 return rv; | 988 return rv; |
| 985 } | 989 } |
| 986 | 990 |
| 987 int HttpCache::Transaction::RestartNetworkRequest() { | 991 int HttpCache::Transaction::RestartNetworkRequest() { |
| 988 DCHECK(mode_ & WRITE || mode_ == NONE); | 992 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 989 DCHECK(network_trans_.get()); | 993 DCHECK(network_trans_.get()); |
| 990 | 994 |
| 991 int rv = network_trans_->RestartIgnoringLastError(&network_info_callback_); | 995 int rv = network_trans_->RestartIgnoringLastError(&network_info_callback_); |
| (...skipping 1033 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2025 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); | 2029 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); |
| 2026 HttpNetworkSession* session = network->GetSession(); | 2030 HttpNetworkSession* session = network->GetSession(); |
| 2027 if (session) { | 2031 if (session) { |
| 2028 session->connection_pool()->CloseIdleSockets(); | 2032 session->connection_pool()->CloseIdleSockets(); |
| 2029 } | 2033 } |
| 2030 } | 2034 } |
| 2031 | 2035 |
| 2032 //----------------------------------------------------------------------------- | 2036 //----------------------------------------------------------------------------- |
| 2033 | 2037 |
| 2034 } // namespace net | 2038 } // namespace net |
| OLD | NEW |