| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 821 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 | 832 |
| 833 // We do this before calling EntryAvailable to force any further calls to | 833 // We do this before calling EntryAvailable to force any further calls to |
| 834 // AddTransactionToEntry to add their transaction to the pending queue, which | 834 // AddTransactionToEntry to add their transaction to the pending queue, which |
| 835 // ensures FIFO ordering. | 835 // ensures FIFO ordering. |
| 836 if (!entry->writer && !entry->pending_queue.empty()) | 836 if (!entry->writer && !entry->pending_queue.empty()) |
| 837 ProcessPendingQueue(entry); | 837 ProcessPendingQueue(entry); |
| 838 | 838 |
| 839 return OK; | 839 return OK; |
| 840 } | 840 } |
| 841 | 841 |
| 842 void HttpCache::DoneWithEntry(ActiveEntry* entry, Transaction* trans, | 842 void HttpCache::DoneWithEntry(ActiveEntry* entry, |
| 843 bool cancel) { | 843 Transaction* trans, |
| 844 EntryState entry_state) { |
| 844 // If we already posted a task to move on to the next transaction and this was | 845 // If we already posted a task to move on to the next transaction and this was |
| 845 // the writer, there is nothing to cancel. | 846 // the writer, there is nothing to cancel. |
| 846 if (entry->will_process_pending_queue && entry->readers.empty()) | 847 if (entry->will_process_pending_queue && entry->readers.empty()) |
| 847 return; | 848 return; |
| 848 | 849 |
| 849 if (entry->writer) { | 850 if (entry->writer) { |
| 850 DCHECK(trans == entry->writer); | 851 DCHECK_EQ(trans, entry->writer); |
| 852 DCHECK(entry->readers.empty()); |
| 851 | 853 |
| 852 // Assume there was a failure. | 854 entry->writer = nullptr; |
| 853 bool success = false; | 855 |
| 854 if (cancel) { | 856 if (entry_state == EntryState::DOOM || entry->doomed) { |
| 855 DCHECK(entry->disk_entry); | 857 DCHECK(!entry->will_process_pending_queue); |
| 856 // This is a successful operation in the sense that we want to keep the | 858 |
| 857 // entry. | 859 TransactionList pending_queue; |
| 858 success = trans->AddTruncatedFlag(); | 860 pending_queue.swap(entry->pending_queue); |
| 859 // The previous operation may have deleted the entry. | 861 if (!entry->doomed) |
| 860 if (!trans->entry()) | 862 entry->disk_entry->Doom(); |
| 861 return; | 863 DestroyEntry(entry); |
| 864 |
| 865 // We need to do something about these pending entries, which now need to |
| 866 // be added to a new entry. |
| 867 while (!pending_queue.empty()) { |
| 868 // ERR_CACHE_RACE causes the transaction to restart the whole process. |
| 869 pending_queue.front()->io_callback().Run(ERR_CACHE_RACE); |
| 870 pending_queue.pop_front(); |
| 871 } |
| 872 return; |
| 862 } | 873 } |
| 863 DoneWritingToEntry(entry, success); | |
| 864 } else { | 874 } else { |
| 865 DoneReadingFromEntry(entry, trans); | 875 auto it = std::find(entry->readers.begin(), entry->readers.end(), trans); |
| 876 DCHECK(it != entry->readers.end()); |
| 877 entry->readers.erase(it); |
| 866 } | 878 } |
| 867 } | |
| 868 | |
| 869 void HttpCache::DoneWritingToEntry(ActiveEntry* entry, bool success) { | |
| 870 DCHECK(entry->readers.empty()); | |
| 871 | |
| 872 entry->writer = NULL; | |
| 873 | |
| 874 if (success) { | |
| 875 ProcessPendingQueue(entry); | |
| 876 } else { | |
| 877 DCHECK(!entry->will_process_pending_queue); | |
| 878 | |
| 879 // We failed to create this entry. | |
| 880 TransactionList pending_queue; | |
| 881 pending_queue.swap(entry->pending_queue); | |
| 882 | |
| 883 entry->disk_entry->Doom(); | |
| 884 DestroyEntry(entry); | |
| 885 | |
| 886 // We need to do something about these pending entries, which now need to | |
| 887 // be added to a new entry. | |
| 888 while (!pending_queue.empty()) { | |
| 889 // ERR_CACHE_RACE causes the transaction to restart the whole process. | |
| 890 pending_queue.front()->io_callback().Run(ERR_CACHE_RACE); | |
| 891 pending_queue.pop_front(); | |
| 892 } | |
| 893 } | |
| 894 } | |
| 895 | |
| 896 void HttpCache::DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans) { | |
| 897 DCHECK(!entry->writer); | |
| 898 | |
| 899 TransactionList::iterator it = | |
| 900 std::find(entry->readers.begin(), entry->readers.end(), trans); | |
| 901 DCHECK(it != entry->readers.end()); | |
| 902 | |
| 903 entry->readers.erase(it); | |
| 904 | |
| 905 ProcessPendingQueue(entry); | 879 ProcessPendingQueue(entry); |
| 906 } | 880 } |
| 907 | 881 |
| 908 void HttpCache::ConvertWriterToReader(ActiveEntry* entry) { | 882 void HttpCache::ConvertWriterToReader(ActiveEntry* entry) { |
| 909 DCHECK(entry->writer); | 883 DCHECK(entry->writer); |
| 910 DCHECK(entry->writer->mode() == Transaction::READ_WRITE); | 884 DCHECK(entry->writer->mode() == Transaction::READ_WRITE); |
| 911 DCHECK(entry->readers.empty()); | 885 DCHECK(entry->readers.empty()); |
| 912 | 886 |
| 913 Transaction* trans = entry->writer; | 887 Transaction* trans = entry->writer; |
| 914 | 888 |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1181 building_backend_ = false; | 1155 building_backend_ = false; |
| 1182 DeletePendingOp(pending_op); | 1156 DeletePendingOp(pending_op); |
| 1183 } | 1157 } |
| 1184 | 1158 |
| 1185 // The cache may be gone when we return from the callback. | 1159 // The cache may be gone when we return from the callback. |
| 1186 if (!item->DoCallback(result, disk_cache_.get())) | 1160 if (!item->DoCallback(result, disk_cache_.get())) |
| 1187 item->NotifyTransaction(result, NULL); | 1161 item->NotifyTransaction(result, NULL); |
| 1188 } | 1162 } |
| 1189 | 1163 |
| 1190 } // namespace net | 1164 } // namespace net |
| OLD | NEW |