OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017 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_WRITERS_H_ | |
6 #define NET_HTTP_HTTP_CACHE_WRITERS_H_ | |
7 #include <list> | |
8 #include <memory> | |
9 #include "base/memory/weak_ptr.h" | |
10 #include "net/base/completion_callback.h" | |
11 #include "net/http/http_cache.h" | |
12 | |
13 // If multiple HttpCache::Transactions are accessing the same cache entry | |
14 // simultaneously, their access to the data read from network is synchronized | |
15 // by HttpCache::Writers. This enables each of those transactions to drive | |
16 // reading the response body from the network ensuring a slow consumer does not | |
17 // starve other consumers of the same resource. | |
18 | |
19 namespace net { | |
20 | |
21 // Writer represents the set of all HttpCache::Transactions that are | |
22 // reading from the network using the same network transaction and writing to | |
23 // the same cache entry. It is owned by the ActiveEntry. | |
24 class NET_EXPORT_PRIVATE HttpCache::Writers { | |
25 public: | |
26 // |*entry| and |*cache| should always be valid when accessed from Writers. | |
27 Writers(HttpCache* cache, ActiveEntry* entry); | |
28 ~Writers(); | |
29 | |
30 // Invokes Read on network transaction if a read is not already in progress. | |
31 // In case a read is already in progress then this transaction is added to | |
32 // a waiting queue and ERR_IO_PENDING is returned. If ERR_IO_PENDING is | |
33 // returned, the result of the read will be later communicated to the consumer | |
34 // via the |callback|. | |
35 int Read(scoped_refptr<IOBuffer> buf, | |
36 int buf_len, | |
37 const CompletionCallback& callback, | |
38 Transaction* transaction); | |
39 | |
40 // Invoked when StopCaching is called on a member transaction. | |
41 // It stops caching only if there are no other transactions. Returns true if | |
42 // caching can be stopped. | |
43 bool StopCaching(Transaction* transaction); | |
44 | |
45 // Membership functions. | |
46 | |
47 // Adds an HttpCache::Transaction to Writers and if its the first transaction | |
48 // added, transfers the ownership of the network transaction to Writers. | |
49 // Should only be invoked if CanAddTransaction() returns true. If | |
50 // |network_transaction| is non-null, it will be assigned to | |
51 // |network_transaction_|. The first transaction added should definitely pass | |
52 // a non-null |network_transaction|. |transaction| can be destroyed at any | |
53 // point and it should invoke RemoveTransaction() during its destruction. | |
54 void AddTransaction(Transaction* transaction, | |
55 std::unique_ptr<HttpTransaction> network_transaction); | |
56 | |
57 // Removes a transaction. | |
58 void RemoveTransaction(Transaction* transaction); | |
59 | |
60 // Invoked when there is a change in a member transaction's priority or a | |
61 // member transaction is removed. | |
62 void PriorityChanged(); | |
63 | |
64 // Returns true if this object is empty. | |
65 bool IsEmpty() const { return all_writers_.empty(); } | |
66 | |
67 // Returns true is |transaction| is part of writers. | |
68 bool IsPresent(Transaction* transaction) const { | |
69 return all_writers_.count(transaction) > 0; | |
70 } | |
71 | |
72 // Returns true if |this| only contains idle writers. | |
73 bool ContainsOnlyIdleWriters() const; | |
74 | |
75 // When response is completely written, any idle writers are moved to | |
Randy Smith (Not in Mondays)
2017/05/24 23:09:19
nit, suggestion: Rephrase to "Move any idle writer
shivanisha
2017/05/31 19:21:26
Rephrased to
// Move any idle writers to entry_->
| |
76 // entry_->readers. Should only be invoked if ContainsOnlyIdleWriters returns | |
77 // true. | |
78 void MoveIdleWritersToReaders(); | |
79 | |
80 // Makes writing an exclusine operation implying that Writers can contain at | |
81 // most one transaction for the complete reading/writing of the response body. | |
82 void SetExclusive() { is_exclusive_ = true; } | |
83 | |
84 // Returns true if more writers can be added for shared writing. | |
85 bool CanAddWriters(); | |
86 | |
87 HttpTransaction* network_transaction() { return network_transaction_.get(); } | |
88 | |
89 // Invoked from HttpCache when it is notified of a transaction failing to | |
90 // write. |error| indicates network read error code or cache write error. | |
91 void ProcessFailure(Transaction* transaction, int error); | |
92 | |
93 // Invoked to mark an entry as truncated. Should be invoked when reading is | |
94 // not currently in progress. | |
95 void TruncateEntry(); | |
96 | |
97 LoadState GetWriterLoadState(); | |
98 | |
99 // For testing. | |
100 | |
101 int CountTransactionsForTesting() const { return all_writers_.size(); } | |
102 bool IsTruncatedForTesting() const { return truncated_; } | |
103 | |
104 private: | |
105 enum class State { | |
106 NONE, | |
107 NETWORK_READ, | |
108 NETWORK_READ_COMPLETE, | |
109 CACHE_WRITE_DATA, | |
110 CACHE_WRITE_DATA_COMPLETE, | |
111 CACHE_WRITE_TRUNCATED_RESPONSE, | |
112 CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE, | |
113 }; | |
114 | |
115 // These transactions are waiting on Read. After the active transaction | |
116 // completes writing the data to the cache, their buffer would be filled with | |
117 // the data and their callback will be invoked. | |
118 struct WaitingForRead { | |
119 Transaction* transaction; | |
120 scoped_refptr<IOBuffer> read_buf; | |
121 int read_buf_len; | |
122 int write_len; | |
123 const CompletionCallback callback; | |
124 WaitingForRead(Transaction* transaction, | |
125 scoped_refptr<IOBuffer> read_buf, | |
126 int len, | |
127 const CompletionCallback& consumer_callback); | |
128 ~WaitingForRead(); | |
129 WaitingForRead(const WaitingForRead&); | |
130 }; | |
131 using WaitingForReadList = std::list<WaitingForRead>; | |
132 | |
133 // Runs the state transition loop. Resets and calls |callback_| on exit, | |
134 // unless the return value is ERR_IO_PENDING. | |
135 int DoLoop(int result); | |
136 | |
137 // State machine functions. | |
138 int DoNetworkRead(); | |
139 int DoNetworkReadComplete(int result); | |
140 int DoCacheWriteData(int num_bytes); | |
141 int DoCacheWriteDataComplete(int result); | |
142 int DoCacheWriteTruncatedResponse(); | |
143 int DoCacheWriteTruncatedResponseComplete(int result); | |
144 | |
145 // Helper functions for callback. | |
146 | |
147 void OnNetworkReadFailure(int result); | |
148 void OnCacheWriteFailure(); | |
149 void OnDataReceived(int result); | |
150 | |
151 // Helper function for writing to cache. | |
152 int WriteToEntry(int index, | |
153 int offset, | |
154 IOBuffer* data, | |
155 int data_len, | |
156 const CompletionCallback& callback); | |
157 | |
158 // Notifies the transactions waiting on Read of the result, by posting a task | |
159 // for each of them. | |
160 void ProcessWaitingForReadTransactions(int result); | |
161 | |
162 // Sets the state of idle writers so that they can fail any subsequent | |
163 // Read. | |
164 void SetIdleWritersFailState(int result); | |
165 | |
166 // IO Completion callback function. | |
167 void OnIOComplete(int result); | |
168 | |
169 State next_state_ = State::NONE; | |
170 | |
171 // True if only reading from network and not writing to cache. | |
172 bool network_read_only_ = false; | |
173 | |
174 // Http Cache. | |
175 HttpCache* cache_; | |
176 | |
177 // Owner of this object. | |
178 ActiveEntry* entry_ = nullptr; | |
179 | |
180 std::unique_ptr<HttpTransaction> network_transaction_; | |
181 | |
182 scoped_refptr<IOBuffer> read_buf_; | |
183 | |
184 int io_buf_len_ = 0; | |
185 int write_len_ = 0; | |
186 | |
187 // The cache transaction that is the current consumer of network_transaction_ | |
188 // ::Read or writing to the entry and is waiting for the operation to be | |
189 // completed. This is used to ensure there is at most one consumer of | |
190 // network_transaction_ at a time. | |
191 Transaction* active_transaction_ = nullptr; | |
192 | |
193 // Transactions whose consumers have invoked Read, but another transaction is | |
194 // currently the |active_transaction_|. After the network read and cache write | |
195 // is complete, the waiting transactions will be notified. | |
196 WaitingForReadList waiting_for_read_; | |
197 | |
198 // Includes all transactions. | |
199 TransactionSet all_writers_; | |
200 | |
201 // True if multiple transactions are not allowed e.g. for partial requests. | |
202 bool is_exclusive_ = false; | |
203 | |
204 // Current priority of the request. If a higher priority transaction is | |
205 // added, the priority of network transaction will be increased. | |
206 RequestPriority priority_ = MINIMUM_PRIORITY; | |
207 | |
208 bool truncated_ = false; // used for testing. | |
209 | |
210 CompletionCallback callback_; // Consumer's callback. | |
211 CompletionCallback io_callback_; | |
212 base::WeakPtrFactory<Writers> weak_factory_; | |
213 DISALLOW_COPY_AND_ASSIGN(Writers); | |
214 }; | |
215 | |
216 } // namespace net | |
217 #endif // NET_HTTP_HTTP_CACHE_WRITERS_H_ | |
OLD | NEW |