| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 int ReadResponseInfoFromEntry(); | 279 int ReadResponseInfoFromEntry(); |
| 280 | 280 |
| 281 // Called to write data to the cache entry. If the write fails, then the | 281 // Called to write data to the cache entry. If the write fails, then the |
| 282 // cache entry is destroyed. Future calls to this function will just do | 282 // cache entry is destroyed. Future calls to this function will just do |
| 283 // nothing without side-effect. | 283 // nothing without side-effect. |
| 284 void WriteToEntry(int index, int offset, IOBuffer* data, int data_len); | 284 void WriteToEntry(int index, int offset, IOBuffer* data, int data_len); |
| 285 | 285 |
| 286 // Called to write response_ to the cache entry. | 286 // Called to write response_ to the cache entry. |
| 287 void WriteResponseInfoToEntry(); | 287 void WriteResponseInfoToEntry(); |
| 288 | 288 |
| 289 // Called to truncate response content in the entry. |
| 290 void TruncateResponseData(); |
| 291 |
| 289 // Called to append response data to the cache entry. | 292 // Called to append response data to the cache entry. |
| 290 void AppendResponseDataToEntry(IOBuffer* data, int data_len); | 293 void AppendResponseDataToEntry(IOBuffer* data, int data_len); |
| 291 | 294 |
| 292 // Called when we are done writing to the cache entry. | 295 // Called when we are done writing to the cache entry. |
| 293 void DoneWritingToEntry(bool success); | 296 void DoneWritingToEntry(bool success); |
| 294 | 297 |
| 295 // Called to signal completion of the network transaction's Start method: | 298 // Called to signal completion of the network transaction's Start method: |
| 296 void OnNetworkInfoAvailable(int result); | 299 void OnNetworkInfoAvailable(int result); |
| 297 | 300 |
| 298 // Called to signal completion of the network transaction's Read method: | 301 // Called to signal completion of the network transaction's Read method: |
| (...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 | 786 |
| 784 return true; | 787 return true; |
| 785 } | 788 } |
| 786 | 789 |
| 787 int HttpCache::Transaction::ReadResponseInfoFromEntry() { | 790 int HttpCache::Transaction::ReadResponseInfoFromEntry() { |
| 788 DCHECK(entry_); | 791 DCHECK(entry_); |
| 789 | 792 |
| 790 if (!HttpCache::ReadResponseInfo(entry_->disk_entry, &response_)) | 793 if (!HttpCache::ReadResponseInfo(entry_->disk_entry, &response_)) |
| 791 return ERR_FAILED; | 794 return ERR_FAILED; |
| 792 | 795 |
| 796 // If the cache object is used for media file, we want the file handle of |
| 797 // response data. |
| 798 if (cache_->type() == HttpCache::MEDIA) |
| 799 response_.response_data_file = |
| 800 entry_->disk_entry->GetPlatformFile(kResponseContentIndex); |
| 801 |
| 793 return OK; | 802 return OK; |
| 794 } | 803 } |
| 795 | 804 |
| 796 void HttpCache::Transaction::WriteToEntry(int index, int offset, | 805 void HttpCache::Transaction::WriteToEntry(int index, int offset, |
| 797 IOBuffer* data, int data_len) { | 806 IOBuffer* data, int data_len) { |
| 798 if (!entry_) | 807 if (!entry_) |
| 799 return; | 808 return; |
| 800 | 809 |
| 801 int rv = entry_->disk_entry->WriteData(index, offset, data, data_len, NULL, | 810 int rv = entry_->disk_entry->WriteData(index, offset, data, data_len, NULL, |
| 802 true); | 811 true); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 839 | 848 |
| 840 void HttpCache::Transaction::AppendResponseDataToEntry(IOBuffer* data, | 849 void HttpCache::Transaction::AppendResponseDataToEntry(IOBuffer* data, |
| 841 int data_len) { | 850 int data_len) { |
| 842 if (!entry_) | 851 if (!entry_) |
| 843 return; | 852 return; |
| 844 | 853 |
| 845 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | 854 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
| 846 WriteToEntry(kResponseContentIndex, current_size, data, data_len); | 855 WriteToEntry(kResponseContentIndex, current_size, data, data_len); |
| 847 } | 856 } |
| 848 | 857 |
| 858 void HttpCache::Transaction::TruncateResponseData() { |
| 859 if (!entry_) |
| 860 return; |
| 861 |
| 862 // If the cache is for media files, we try to prepare the response data |
| 863 // file as an external file and truncate it afterwards. |
| 864 // Recipient of ResponseInfo should judge from |response_.response_data_file| |
| 865 // to tell whether an external file of response data is available for reading |
| 866 // or not. |
| 867 // TODO(hclam): we should prepare the target stream as extern file only |
| 868 // if we get a valid response from server, i.e. 200. We don't want empty |
| 869 // cache files for redirection or external files for erroneous requests. |
| 870 response_.response_data_file = base::kInvalidPlatformFileValue; |
| 871 if (cache_->type() == HttpCache::MEDIA) |
| 872 response_.response_data_file = |
| 873 entry_->disk_entry->UseExternalFile(kResponseContentIndex); |
| 874 |
| 875 // Truncate the stream. |
| 876 WriteToEntry(kResponseContentIndex, 0, NULL, 0); |
| 877 } |
| 878 |
| 849 void HttpCache::Transaction::DoneWritingToEntry(bool success) { | 879 void HttpCache::Transaction::DoneWritingToEntry(bool success) { |
| 850 if (!entry_) | 880 if (!entry_) |
| 851 return; | 881 return; |
| 852 | 882 |
| 853 if (cache_->mode() == RECORD) | 883 if (cache_->mode() == RECORD) |
| 854 DLOG(INFO) << "Recorded: " << request_->method << request_->url | 884 DLOG(INFO) << "Recorded: " << request_->method << request_->url |
| 855 << " status: " << response_.headers->response_code(); | 885 << " status: " << response_.headers->response_code(); |
| 856 | 886 |
| 857 cache_->DoneWritingToEntry(entry_, success); | 887 cache_->DoneWritingToEntry(entry_, success); |
| 858 entry_ = NULL; | 888 entry_ = NULL; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 } | 925 } |
| 896 } else { | 926 } else { |
| 897 mode_ = WRITE; | 927 mode_ = WRITE; |
| 898 } | 928 } |
| 899 } | 929 } |
| 900 | 930 |
| 901 if (!(mode_ & READ)) { | 931 if (!(mode_ & READ)) { |
| 902 response_ = *new_response; | 932 response_ = *new_response; |
| 903 WriteResponseInfoToEntry(); | 933 WriteResponseInfoToEntry(); |
| 904 | 934 |
| 905 // Truncate the response data | 935 // Truncate response data |
| 906 WriteToEntry(kResponseContentIndex, 0, NULL, 0); | 936 TruncateResponseData(); |
| 907 | 937 |
| 908 // If this response is a redirect, then we can stop writing now. (We | 938 // If this response is a redirect, then we can stop writing now. (We |
| 909 // don't need to cache the response body of a redirect.) | 939 // don't need to cache the response body of a redirect.) |
| 910 if (response_.headers->IsRedirect(NULL)) | 940 if (response_.headers->IsRedirect(NULL)) |
| 911 DoneWritingToEntry(true); | 941 DoneWritingToEntry(true); |
| 912 } | 942 } |
| 913 } | 943 } |
| 914 } else if (IsCertificateError(result)) { | 944 } else if (IsCertificateError(result)) { |
| 915 response_.ssl_info = network_trans_->GetResponseInfo()->ssl_info; | 945 response_.ssl_info = network_trans_->GetResponseInfo()->ssl_info; |
| 916 } | 946 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 951 HandleResult(result); | 981 HandleResult(result); |
| 952 } | 982 } |
| 953 | 983 |
| 954 //----------------------------------------------------------------------------- | 984 //----------------------------------------------------------------------------- |
| 955 | 985 |
| 956 HttpCache::HttpCache(ProxyService* proxy_service, | 986 HttpCache::HttpCache(ProxyService* proxy_service, |
| 957 const std::wstring& cache_dir, | 987 const std::wstring& cache_dir, |
| 958 int cache_size) | 988 int cache_size) |
| 959 : disk_cache_dir_(cache_dir), | 989 : disk_cache_dir_(cache_dir), |
| 960 mode_(NORMAL), | 990 mode_(NORMAL), |
| 991 type_(COMMON), |
| 961 network_layer_(HttpNetworkLayer::CreateFactory(proxy_service)), | 992 network_layer_(HttpNetworkLayer::CreateFactory(proxy_service)), |
| 962 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), | 993 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), |
| 963 in_memory_cache_(false), | 994 in_memory_cache_(false), |
| 964 cache_size_(cache_size) { | 995 cache_size_(cache_size) { |
| 965 } | 996 } |
| 966 | 997 |
| 998 HttpCache::HttpCache(HttpNetworkSession* session, |
| 999 const std::wstring& cache_dir, |
| 1000 int cache_size) |
| 1001 : disk_cache_dir_(cache_dir), |
| 1002 mode_(NORMAL), |
| 1003 type_(COMMON), |
| 1004 network_layer_(HttpNetworkLayer::CreateFactory(session)), |
| 1005 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), |
| 1006 in_memory_cache_(false), |
| 1007 cache_size_(cache_size) { |
| 1008 } |
| 1009 |
| 967 HttpCache::HttpCache(ProxyService* proxy_service, int cache_size) | 1010 HttpCache::HttpCache(ProxyService* proxy_service, int cache_size) |
| 968 : mode_(NORMAL), | 1011 : mode_(NORMAL), |
| 1012 type_(COMMON), |
| 969 network_layer_(HttpNetworkLayer::CreateFactory(proxy_service)), | 1013 network_layer_(HttpNetworkLayer::CreateFactory(proxy_service)), |
| 970 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), | 1014 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), |
| 971 in_memory_cache_(true), | 1015 in_memory_cache_(true), |
| 972 cache_size_(cache_size) { | 1016 cache_size_(cache_size) { |
| 973 } | 1017 } |
| 974 | 1018 |
| 975 HttpCache::HttpCache(HttpTransactionFactory* network_layer, | 1019 HttpCache::HttpCache(HttpTransactionFactory* network_layer, |
| 976 disk_cache::Backend* disk_cache) | 1020 disk_cache::Backend* disk_cache) |
| 977 : mode_(NORMAL), | 1021 : mode_(NORMAL), |
| 1022 type_(COMMON), |
| 978 network_layer_(network_layer), | 1023 network_layer_(network_layer), |
| 979 disk_cache_(disk_cache), | 1024 disk_cache_(disk_cache), |
| 980 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), | 1025 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), |
| 981 in_memory_cache_(false), | 1026 in_memory_cache_(false), |
| 982 cache_size_(0) { | 1027 cache_size_(0) { |
| 983 } | 1028 } |
| 984 | 1029 |
| 985 HttpCache::~HttpCache() { | 1030 HttpCache::~HttpCache() { |
| 986 // If we have any active entries remaining, then we need to deactivate them. | 1031 // If we have any active entries remaining, then we need to deactivate them. |
| 987 // We may have some pending calls to OnProcessPendingQueue, but since those | 1032 // We may have some pending calls to OnProcessPendingQueue, but since those |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1407 | 1452 |
| 1408 entry->pending_queue.erase(entry->pending_queue.begin()); | 1453 entry->pending_queue.erase(entry->pending_queue.begin()); |
| 1409 | 1454 |
| 1410 AddTransactionToEntry(entry, next); | 1455 AddTransactionToEntry(entry, next); |
| 1411 } | 1456 } |
| 1412 | 1457 |
| 1413 //----------------------------------------------------------------------------- | 1458 //----------------------------------------------------------------------------- |
| 1414 | 1459 |
| 1415 } // namespace net | 1460 } // namespace net |
| 1416 | 1461 |
| OLD | NEW |