OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_transaction.h" | 5 #include "net/http/http_cache_transaction.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
10 #include <unistd.h> | 10 #include <unistd.h> |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 // If there is an outstanding callback, mark it as cancelled so running it | 156 // If there is an outstanding callback, mark it as cancelled so running it |
157 // does nothing. | 157 // does nothing. |
158 cache_callback_->Cancel(); | 158 cache_callback_->Cancel(); |
159 write_headers_callback_->Cancel(); | 159 write_headers_callback_->Cancel(); |
160 | 160 |
161 // We could still have a cache read or write in progress, so we just null the | 161 // We could still have a cache read or write in progress, so we just null the |
162 // cache_ pointer to signal that we are dead. See DoCacheReadCompleted. | 162 // cache_ pointer to signal that we are dead. See DoCacheReadCompleted. |
163 cache_.reset(); | 163 cache_.reset(); |
164 } | 164 } |
165 | 165 |
| 166 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, |
| 167 CompletionCallback* callback) { |
| 168 DCHECK(buf); |
| 169 DCHECK_GT(buf_len, 0); |
| 170 DCHECK(callback); |
| 171 if (!cache_ || !entry_) |
| 172 return ERR_UNEXPECTED; |
| 173 |
| 174 // We don't need to track this operation for anything. |
| 175 // It could be possible to check if there is something already written and |
| 176 // avoid writing again (it should be the same, right?), but let's allow the |
| 177 // caller to "update" the contents with something new. |
| 178 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len, |
| 179 callback, true); |
| 180 } |
| 181 |
| 182 // Histogram data from the end of 2010 show the following distribution of |
| 183 // response headers: |
| 184 // |
| 185 // Content-Length............... 87% |
| 186 // Date......................... 98% |
| 187 // Last-Modified................ 49% |
| 188 // Etag......................... 19% |
| 189 // Accept-Ranges: bytes......... 25% |
| 190 // Accept-Ranges: none.......... 0.4% |
| 191 // Strong Validator............. 50% |
| 192 // Strong Validator + ranges.... 24% |
| 193 // Strong Validator + CL........ 49% |
| 194 // |
| 195 bool HttpCache::Transaction::AddTruncatedFlag() { |
| 196 DCHECK(mode_ & WRITE); |
| 197 |
| 198 // Don't set the flag for sparse entries. |
| 199 if (partial_.get() && !truncated_) |
| 200 return true; |
| 201 |
| 202 // Double check that there is something worth keeping. |
| 203 if (!entry_->disk_entry->GetDataSize(kResponseContentIndex)) |
| 204 return false; |
| 205 |
| 206 if (response_.headers->GetContentLength() <= 0 || |
| 207 response_.headers->HasHeaderValue("Accept-Ranges", "none") || |
| 208 !response_.headers->HasStrongValidators()) |
| 209 return false; |
| 210 |
| 211 truncated_ = true; |
| 212 target_state_ = STATE_NONE; |
| 213 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE; |
| 214 DoLoop(OK); |
| 215 return true; |
| 216 } |
| 217 |
| 218 LoadState HttpCache::Transaction::GetWriterLoadState() const { |
| 219 if (network_trans_.get()) |
| 220 return network_trans_->GetLoadState(); |
| 221 if (entry_ || !request_) |
| 222 return LOAD_STATE_IDLE; |
| 223 return LOAD_STATE_WAITING_FOR_CACHE; |
| 224 } |
| 225 |
| 226 const BoundNetLog& HttpCache::Transaction::net_log() const { |
| 227 return net_log_; |
| 228 } |
| 229 |
166 int HttpCache::Transaction::Start(const HttpRequestInfo* request, | 230 int HttpCache::Transaction::Start(const HttpRequestInfo* request, |
167 CompletionCallback* callback, | 231 CompletionCallback* callback, |
168 const BoundNetLog& net_log) { | 232 const BoundNetLog& net_log) { |
169 DCHECK(request); | 233 DCHECK(request); |
170 DCHECK(callback); | 234 DCHECK(callback); |
171 | 235 |
172 // Ensure that we only have one asynchronous call at a time. | 236 // Ensure that we only have one asynchronous call at a time. |
173 DCHECK(!callback_); | 237 DCHECK(!callback_); |
174 DCHECK(!reading_); | 238 DCHECK(!reading_); |
175 DCHECK(!network_trans_.get()); | 239 DCHECK(!network_trans_.get()); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 | 395 |
332 return LOAD_STATE_IDLE; | 396 return LOAD_STATE_IDLE; |
333 } | 397 } |
334 | 398 |
335 uint64 HttpCache::Transaction::GetUploadProgress() const { | 399 uint64 HttpCache::Transaction::GetUploadProgress() const { |
336 if (network_trans_.get()) | 400 if (network_trans_.get()) |
337 return network_trans_->GetUploadProgress(); | 401 return network_trans_->GetUploadProgress(); |
338 return final_upload_progress_; | 402 return final_upload_progress_; |
339 } | 403 } |
340 | 404 |
341 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, | |
342 CompletionCallback* callback) { | |
343 DCHECK(buf); | |
344 DCHECK_GT(buf_len, 0); | |
345 DCHECK(callback); | |
346 if (!cache_ || !entry_) | |
347 return ERR_UNEXPECTED; | |
348 | |
349 // We don't need to track this operation for anything. | |
350 // It could be possible to check if there is something already written and | |
351 // avoid writing again (it should be the same, right?), but let's allow the | |
352 // caller to "update" the contents with something new. | |
353 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len, | |
354 callback, true); | |
355 } | |
356 | |
357 // Histogram data from the end of 2010 show the following distribution of | |
358 // response headers: | |
359 // | |
360 // Content-Length............... 87% | |
361 // Date......................... 98% | |
362 // Last-Modified................ 49% | |
363 // Etag......................... 19% | |
364 // Accept-Ranges: bytes......... 25% | |
365 // Accept-Ranges: none.......... 0.4% | |
366 // Strong Validator............. 50% | |
367 // Strong Validator + ranges.... 24% | |
368 // Strong Validator + CL........ 49% | |
369 // | |
370 bool HttpCache::Transaction::AddTruncatedFlag() { | |
371 DCHECK(mode_ & WRITE); | |
372 | |
373 // Don't set the flag for sparse entries. | |
374 if (partial_.get() && !truncated_) | |
375 return true; | |
376 | |
377 // Double check that there is something worth keeping. | |
378 if (!entry_->disk_entry->GetDataSize(kResponseContentIndex)) | |
379 return false; | |
380 | |
381 if (response_.headers->GetContentLength() <= 0 || | |
382 response_.headers->HasHeaderValue("Accept-Ranges", "none") || | |
383 !response_.headers->HasStrongValidators()) | |
384 return false; | |
385 | |
386 truncated_ = true; | |
387 target_state_ = STATE_NONE; | |
388 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE; | |
389 DoLoop(OK); | |
390 return true; | |
391 } | |
392 | |
393 LoadState HttpCache::Transaction::GetWriterLoadState() const { | |
394 if (network_trans_.get()) | |
395 return network_trans_->GetLoadState(); | |
396 if (entry_ || !request_) | |
397 return LOAD_STATE_IDLE; | |
398 return LOAD_STATE_WAITING_FOR_CACHE; | |
399 } | |
400 | |
401 const BoundNetLog& HttpCache::Transaction::net_log() const { | |
402 return net_log_; | |
403 } | |
404 | |
405 //----------------------------------------------------------------------------- | 405 //----------------------------------------------------------------------------- |
406 | 406 |
407 void HttpCache::Transaction::DoCallback(int rv) { | 407 void HttpCache::Transaction::DoCallback(int rv) { |
408 DCHECK(rv != ERR_IO_PENDING); | 408 DCHECK(rv != ERR_IO_PENDING); |
409 DCHECK(callback_); | 409 DCHECK(callback_); |
410 | 410 |
411 // Since Run may result in Read being called, clear callback_ up front. | 411 // Since Run may result in Read being called, clear callback_ up front. |
412 CompletionCallback* c = callback_; | 412 CompletionCallback* c = callback_; |
413 callback_ = NULL; | 413 callback_ = NULL; |
414 c->Run(rv); | 414 c->Run(rv); |
(...skipping 1493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1908 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; | 1908 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; |
1909 } | 1909 } |
1910 return result; | 1910 return result; |
1911 } | 1911 } |
1912 | 1912 |
1913 void HttpCache::Transaction::OnIOComplete(int result) { | 1913 void HttpCache::Transaction::OnIOComplete(int result) { |
1914 DoLoop(result); | 1914 DoLoop(result); |
1915 } | 1915 } |
1916 | 1916 |
1917 } // namespace net | 1917 } // namespace net |
OLD | NEW |