Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_HTTP_HTTP_CACHE_SHARED_WRITERS_H_ | |
| 6 #define NET_HTTP_HTTP_CACHE_SHARED_WRITERS_H_ | |
| 7 | |
| 8 #include <list> | |
| 9 #include <memory> | |
| 10 #include "base/memory/weak_ptr.h" | |
| 11 #include "net/base/completion_callback.h" | |
| 12 #include "net/http/http_cache.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 // SharedWriters represents the set of all HttpCache::Transactions that are | |
| 17 // reading from the network using the same network transaction and writing to | |
| 18 // the same cache entry. It is owned by the ActiveEntry. | |
| 19 class HttpCache::SharedWriters { | |
| 20 public: | |
| 21 // Creates a new SharedWriters object and transfers the ownership of network | |
| 22 // transaction to SharedWriters. Consumer must ensure that |*entry| and | |
| 23 // |*cache| outlives |this|. It is ok for cache_transaction to die before this | |
| 24 // object. | |
| 25 SharedWriters(HttpCache* cache, | |
| 26 ActiveEntry* entry, | |
| 27 Transaction* cache_transaction, | |
| 28 RequestPriority priority, | |
| 29 std::unique_ptr<HttpTransaction> network_transaction); | |
| 30 ~SharedWriters(); | |
| 31 | |
| 32 // Adds a transaction to SharedWriters. Return value is true if it is the | |
| 33 // current validating transaction and false if it is waiting. | |
| 34 bool AddTransaction(Transaction* transaction); | |
| 35 | |
| 36 // Invokes Read on network transaction if a read is not already in progress. | |
| 37 // In case a read is already in progress then this transaction is added to | |
| 38 // a waiting queue, read_in_progress is set to true and ERR_IO_PENDING is | |
| 39 // returned. | |
| 40 int Read(scoped_refptr<IOBuffer> buf, | |
| 41 int buf_len, | |
| 42 const CompletionCallback& callback, | |
| 43 Transaction* transaction, | |
| 44 bool* read_in_progress); | |
| 45 | |
| 46 // Invokes WriteData on disk entry. | |
| 47 int CacheWrite(scoped_refptr<IOBuffer> buf, | |
| 48 int write_len, | |
| 49 const CompletionCallback& callback, | |
| 50 Transaction* transaction); | |
| 51 | |
| 52 // Invoked when the validating transaction wants to continue with the entry | |
| 53 // as the validation successfully matched it. | |
| 54 void OnValidationMatch(Transaction* transaction, RequestPriority priority); | |
| 55 | |
| 56 // Invoked when the validating transaction cannot continue with the entry as | |
| 57 // the validation did not return a 304. Its fine to continue writing to cache | |
| 58 // if this is the only transaction, so network transaction ownership is | |
| 59 // transferred to SharedWriters. But if there are other transactions, entry | |
| 60 // will be doomed and this transaction will continue reading from the network | |
| 61 // so the return value will pass the ownership of the network transaction | |
| 62 // back to the transaction. | |
| 63 std::unique_ptr<HttpTransaction> OnValidationNoMatch( | |
| 64 const std::string& key, | |
| 65 Transaction* transaction, | |
| 66 std::unique_ptr<HttpTransaction> network_transaction, | |
| 67 RequestPriority priority); | |
| 68 | |
| 69 // Invoked when DoneReading is invoked on a shared transaction. The | |
| 70 // transaction will be removed from |this|. If another transaction is | |
| 71 // currently reading, then other transactions will not be impacted. | |
| 72 void DoneReading(Transaction* transaction); | |
| 73 | |
| 74 // Invoked when StopCaching is called for a shared writer transaction. | |
| 75 // It stops caching only if there are no other transactions. | |
| 76 void StopCaching(Transaction* transaction); | |
| 77 | |
| 78 // All transactions eligible for shared writing from entry's pending queue | |
| 79 // will be tracked from SharedWriters after this function. | |
| 80 void MoveFromPendingQueue(); | |
| 81 | |
| 82 // Posts a task for invoking the io callback of the first transaction waiting | |
| 83 // for validation. | |
| 84 void ProcessFirstWaitingValidation(); | |
| 85 | |
| 86 // Removes a pending transaction. | |
| 87 bool RemoveWaitingTransaction(Transaction* transaction); | |
| 88 | |
| 89 // Removes a transaction which is waiting for Read to be invoked by the | |
| 90 // consumer. | |
| 91 void RemoveIdleTransaction(Transaction* transaction); | |
| 92 | |
| 93 // Removes a transaction waiting on a Read call. | |
| 94 void RemoveWaitingForReadTransaction(Transaction* transaction); | |
| 95 | |
| 96 // Removes the currently validating transaction. | |
| 97 void RemoveValidatingTransaction(Transaction* transaction); | |
| 98 | |
| 99 // Removes the currently active transaction. | |
| 100 void RemoveActiveTransaction(Transaction* transaction); | |
| 101 | |
| 102 // Returns true if this object is empty. | |
| 103 bool empty(); | |
| 104 | |
| 105 HttpTransaction* network_transaction() { return network_transaction_.get(); } | |
| 106 | |
| 107 // Do not add new transactions if in the process of marking the entry as | |
| 108 // truncated. | |
| 109 bool CanAddNewTransaction(); | |
| 110 | |
| 111 // Invoked when there is a change in a member transaction's priority or a | |
| 112 // member transaction is removed. | |
| 113 void PriorityChanged(); | |
| 114 | |
| 115 private: | |
| 116 // Runs the state transition loop. Resets and calls |callback_| on exit, | |
| 117 // unless the return value is ERR_IO_PENDING. | |
| 118 int DoLoop(int result); | |
| 119 | |
| 120 // IO Completion callback function. | |
| 121 void OnIOComplete(int result); | |
| 122 | |
| 123 // State machine functions. | |
| 124 int DoNetworkRead(); | |
| 125 int DoNetworkReadComplete(int result); | |
| 126 int DoCacheWriteData(int num_bytes); | |
| 127 int DoCacheWriteDataComplete(int result); | |
| 128 int DoCacheWriteTruncatedResponse(); | |
| 129 int DoCacheWriteTruncatedResponseComplete(int result); | |
| 130 | |
| 131 // Helper functions for callback. | |
| 132 | |
| 133 void OnNetworkReadSuccess(int len); | |
| 134 | |
| 135 void OnCacheWriteSuccess(int result); | |
| 136 | |
| 137 void OnCacheWriteFailure(); | |
| 138 | |
| 139 void OnNetworkReadFailure(int result); | |
| 140 | |
| 141 void FailureCleanup(int error, bool continue_network_reading); | |
| 142 | |
| 143 // All pending transactions will not be tracked by entry. | |
| 144 void MoveToPendingQueue(); | |
| 145 | |
| 146 // Notifies the transactions waiting on Read of the result, by posting a task | |
| 147 // for each of them. | |
| 148 void ProcessWaitingForReadTransactions(int result); | |
| 149 | |
| 150 void OnProcessFirstWaitingValidation(); | |
| 151 | |
| 152 // Removes the active transaction from |this|. | |
| 153 void ResetActiveTransaction(bool continue_network_reading = false); | |
| 154 | |
| 155 // Sets the state of idle writers so that they can fail any subsequent | |
| 156 // Read. | |
| 157 void SetIdleWritersFailState(int result); | |
| 158 | |
| 159 // When response is completely written, any idle writers are moved to | |
| 160 // entry_->readers. | |
| 161 void MoveIdleWritersToReaders(); | |
| 162 | |
| 163 // Helper function to let the validating transaction continue being a part of | |
| 164 // SharedWriters. | |
| 165 void ValidationDoneContinue(Transaction* transaction, | |
| 166 RequestPriority priority); | |
| 167 | |
| 168 // Helper function invoked when response is successfully written to the cache. | |
| 169 void ResponseDataComplete(); | |
| 170 | |
| 171 bool AttemptTruncation(); | |
| 172 | |
| 173 RequestPriority getCurrentHighestPriority(); | |
| 174 | |
| 175 enum State { | |
|
jkarlin
2017/02/03 18:26:19
The enum declaration should come before the privat
shivanisha
2017/02/06 21:14:10
done
| |
| 176 STATE_NONE, | |
| 177 STATE_NETWORK_READ, | |
| 178 STATE_NETWORK_READ_COMPLETE, | |
| 179 STATE_CACHE_WRITE_DATA, | |
| 180 STATE_CACHE_WRITE_DATA_COMPLETE, | |
| 181 STATE_CACHE_WRITE_TRUNCATED_RESPONSE, | |
| 182 STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE, | |
| 183 STATE_DONE | |
| 184 }; | |
| 185 | |
| 186 State next_state_ = STATE_NONE; | |
| 187 | |
| 188 // Http Cache. | |
| 189 base::WeakPtr<HttpCache> cache_; | |
| 190 | |
| 191 // Owner of this object. | |
| 192 ActiveEntry* entry_ = nullptr; | |
| 193 | |
| 194 std::unique_ptr<HttpTransaction> network_transaction_; | |
| 195 | |
| 196 scoped_refptr<IOBuffer> read_buf_; | |
| 197 int io_buf_len_ = 0; | |
| 198 int write_len_ = 0; | |
| 199 | |
| 200 // The cache transaction that is the current consumer of network_transaction_ | |
| 201 // ::Read or writing to the entry and is waiting for the operation to be | |
| 202 // completed. This is used to ensure there is at most one consumer. | |
| 203 // After the network read and cache write is successful and data | |
| 204 // written to cache, other waiting transactions will be notified. | |
| 205 Transaction* active_transaction_ = nullptr; | |
| 206 | |
| 207 // This will point to the currently validating transaction. | |
| 208 Transaction* validating_transaction_ = nullptr; | |
| 209 | |
| 210 // These transactions are waiting for the validating transaction to complete | |
| 211 // and then the first of these will begin validation. | |
| 212 TransactionList waiting_for_validation_; | |
| 213 | |
| 214 // These transactions are waiting on Read. After the active transaction | |
| 215 // completes writing the data to the cache, their buffer would be filled with | |
| 216 // the data and their callback will be invoked. | |
| 217 struct WaitingForRead { | |
|
jkarlin
2017/02/03 18:26:20
struct should be at beginning of private:
shivanisha
2017/02/06 21:14:10
done
| |
| 218 Transaction* transaction; | |
| 219 scoped_refptr<IOBuffer> read_buf; | |
| 220 int read_buf_len; | |
| 221 int write_len; | |
| 222 const CompletionCallback callback; | |
| 223 WaitingForRead(Transaction* transaction, | |
| 224 scoped_refptr<IOBuffer> read_buf, | |
| 225 int len, | |
| 226 const CompletionCallback& consumer_callback); | |
| 227 ~WaitingForRead(); | |
| 228 WaitingForRead(const WaitingForRead&); | |
| 229 }; | |
| 230 typedef std::list<WaitingForRead> WaitingForReadList; | |
|
jkarlin
2017/02/03 18:26:19
using WaitingForReadList = std::list<WaitingForRea
shivanisha
2017/02/06 21:14:10
done
| |
| 231 WaitingForReadList waiting_for_read_; | |
| 232 | |
| 233 // Includes a transaction if it is past the validation stage. | |
| 234 TransactionSet all_writers_; | |
| 235 | |
| 236 // Current priority of the request. If a higher priority transaction is | |
| 237 // added, the priority of network transaction will be increased. | |
| 238 RequestPriority priority_ = DEFAULT_PRIORITY; | |
| 239 | |
| 240 CompletionCallback callback_; // Consumer's callback. | |
| 241 base::Callback<void(bool*)> cache_callback_; // Cache callback. | |
| 242 CompletionCallback io_callback_; | |
| 243 | |
| 244 // Saved so it could be returned to the consumer after marking the entry as | |
| 245 // truncated, if needed. | |
| 246 int rv_post_truncation_ = 0; | |
| 247 | |
| 248 base::WeakPtrFactory<SharedWriters> weak_factory_; | |
| 249 | |
| 250 DISALLOW_COPY_AND_ASSIGN(SharedWriters); | |
| 251 }; | |
| 252 | |
| 253 } // namespace net | |
| 254 | |
| 255 #endif // NET_HTTP_HTTP_CACHE_SHARED_WRITERS_H_ | |
| OLD | NEW |