| 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 #include "build/build_config.h" // For OS_POSIX |
| 6 |
| 7 #if defined(OS_POSIX) |
| 8 #include <unistd.h> |
| 9 #endif |
| 10 |
| 11 #include <memory> |
| 12 #include <utility> |
| 13 #include "base/bind.h" |
| 14 #include "base/callback_helpers.h" |
| 15 #include "base/compiler_specific.h" |
| 16 #include "base/format_macros.h" |
| 17 #include "base/location.h" |
| 18 #include "base/macros.h" |
| 19 #include "base/single_thread_task_runner.h" |
| 20 #include "net/disk_cache/disk_cache.h" |
| 21 #include "net/http/http_cache_shared_writers.h" |
| 22 #include "net/http/http_cache_transaction.h" |
| 23 |
| 24 namespace net { |
| 25 |
| 26 void HttpCache::SharedWriters::Create( |
| 27 Transaction* cache_transaction, |
| 28 std::unique_ptr<HttpTransaction> network_transaction, |
| 29 base::WeakPtr<HttpCache> cache, |
| 30 RequestPriority priority) { |
| 31 ActiveEntry* entry = cache_transaction->entry(); |
| 32 DCHECK(!entry->shared_writers); |
| 33 DCHECK_EQ(entry->writer, cache_transaction); |
| 34 DCHECK(entry->readers.empty()); |
| 35 entry->shared_writers.reset( |
| 36 new HttpCache::SharedWriters(cache, entry, cache_transaction, priority, |
| 37 std::move(network_transaction))); |
| 38 |
| 39 // entry->writer should no longer exist as it is moved to |
| 40 // shared_writers. |
| 41 entry->writer = nullptr; |
| 42 } |
| 43 |
| 44 HttpCache::SharedWriters::SharedWriters( |
| 45 base::WeakPtr<HttpCache> cache, |
| 46 ActiveEntry* entry, |
| 47 Transaction* cache_transaction, |
| 48 RequestPriority priority, |
| 49 std::unique_ptr<HttpTransaction> network_transaction) |
| 50 : cache_(cache), entry_(entry), priority_(priority), weak_factory_(this) { |
| 51 cache_transaction->SetShared(); |
| 52 all_writers_.insert(cache_transaction); |
| 53 network_transaction_ = std::move(network_transaction); |
| 54 io_callback_ = base::Bind(&HttpCache::SharedWriters::OnIOComplete, |
| 55 weak_factory_.GetWeakPtr()); |
| 56 // Add the eligible transactions to waiting_for_validation_ and process the |
| 57 // first among them. After this, pending_queue will only contain transactions |
| 58 // that are not eligible for shared writing. |
| 59 MoveFromPendingQueue(); |
| 60 ProcessFirstWaitingValidation(); |
| 61 } |
| 62 |
| 63 HttpCache::SharedWriters::~SharedWriters() {} |
| 64 |
| 65 int HttpCache::SharedWriters::AddTransaction(Transaction* transaction) { |
| 66 transaction->SetShared(); |
| 67 |
| 68 if (!validating_transaction_) { |
| 69 validating_transaction_ = transaction; |
| 70 return OK; |
| 71 } |
| 72 |
| 73 // Another transaction is in the process of validation, wait for it and any |
| 74 // other transactions already in the queue to complete. |
| 75 waiting_for_validation_.push_back(transaction); |
| 76 return ERR_IO_PENDING; |
| 77 } |
| 78 |
| 79 int HttpCache::SharedWriters::Read(scoped_refptr<IOBuffer> buf, |
| 80 int buf_len, |
| 81 const CompletionCallback& callback, |
| 82 Transaction* transaction, |
| 83 bool* read_in_progress) { |
| 84 DCHECK(buf); |
| 85 DCHECK_GT(buf_len, 0); |
| 86 DCHECK(!callback.is_null()); |
| 87 |
| 88 // If another transaction is already reading from the network, then this |
| 89 // transaction waits for the read to complete and gets its buffer filled |
| 90 // with the data returned from that read. |
| 91 if (active_transaction_) { |
| 92 WaitingForRead waiting_transaction(transaction, buf, buf_len, callback); |
| 93 waiting_for_read_.push_back(waiting_transaction); |
| 94 *read_in_progress = true; |
| 95 return ERR_IO_PENDING; |
| 96 } |
| 97 |
| 98 DCHECK_EQ(next_state_, State::NONE); |
| 99 DCHECK(callback_.is_null()); |
| 100 |
| 101 active_transaction_ = transaction; |
| 102 |
| 103 read_buf_ = std::move(buf); |
| 104 io_buf_len_ = buf_len; |
| 105 |
| 106 next_state_ = State::NETWORK_READ; |
| 107 int rv = DoLoop(OK); |
| 108 |
| 109 if (rv == ERR_IO_PENDING) { |
| 110 DCHECK(callback_.is_null()); |
| 111 callback_ = callback; |
| 112 } |
| 113 return rv; |
| 114 } |
| 115 |
| 116 int HttpCache::SharedWriters::CacheWrite(scoped_refptr<IOBuffer> buf, |
| 117 int write_len, |
| 118 const CompletionCallback& callback, |
| 119 Transaction* transaction) { |
| 120 DCHECK_EQ(next_state_, State::NONE); |
| 121 DCHECK(buf); |
| 122 DCHECK_GE(write_len, 0); |
| 123 DCHECK(callback_.is_null()); |
| 124 DCHECK(!callback.is_null()); |
| 125 DCHECK_EQ(active_transaction_, transaction); |
| 126 |
| 127 read_buf_ = std::move(buf); |
| 128 next_state_ = State::CACHE_WRITE_DATA; |
| 129 int rv = DoLoop(write_len); |
| 130 |
| 131 if (rv == ERR_IO_PENDING) { |
| 132 DCHECK(callback_.is_null()); |
| 133 callback_ = callback; |
| 134 } |
| 135 |
| 136 return rv; |
| 137 } |
| 138 |
| 139 void HttpCache::SharedWriters::OnValidationMatch(Transaction* transaction, |
| 140 RequestPriority priority) { |
| 141 DCHECK_EQ(validating_transaction_, transaction); |
| 142 ValidationDoneContinue(transaction, priority); |
| 143 } |
| 144 |
| 145 std::unique_ptr<HttpTransaction> HttpCache::SharedWriters::OnValidationNoMatch( |
| 146 const std::string& key, |
| 147 Transaction* transaction, |
| 148 std::unique_ptr<HttpTransaction> network_transaction, |
| 149 RequestPriority priority) { |
| 150 DCHECK_EQ(validating_transaction_, transaction); |
| 151 // If there is no transaction in all_writers_, its ok to rewrite the entry |
| 152 // response. |
| 153 if (all_writers_.empty()) { |
| 154 network_transaction_ = std::move(network_transaction); |
| 155 ValidationDoneContinue(transaction, priority); |
| 156 return std::unique_ptr<HttpTransaction>(); |
| 157 } |
| 158 |
| 159 transaction->ResetShared(); |
| 160 validating_transaction_ = nullptr; |
| 161 MoveToPendingQueue(); |
| 162 cache_->DoomEntryRestartPendingQueue(key, entry_); |
| 163 return network_transaction; |
| 164 } |
| 165 |
| 166 void HttpCache::SharedWriters::DoneReading(Transaction* transaction) { |
| 167 // Should only be invoked when the transaction is not currently reading. |
| 168 DCHECK_NE(transaction, active_transaction_); |
| 169 auto it = waiting_for_read_.begin(); |
| 170 for (; it != waiting_for_read_.end(); it++) |
| 171 DCHECK_NE(transaction, it->transaction); |
| 172 |
| 173 // The transaction should be part of all_writers. |
| 174 size_t result = all_writers_.erase(transaction); |
| 175 DCHECK_EQ(result, (size_t)1); |
| 176 transaction->ResetShared(); |
| 177 |
| 178 // If active_transaction_ is set, then wait for active_transaction_ to detect |
| 179 // the end |
| 180 // of stream. |
| 181 if (active_transaction_) { |
| 182 return; |
| 183 } |
| 184 DCHECK(waiting_for_read_.empty()); |
| 185 // If there is a transaction validating currently, return. |
| 186 if (validating_transaction_) { |
| 187 return; |
| 188 } |
| 189 |
| 190 // Else empty the SharedWriters object. |
| 191 MoveIdleWritersToReaders(); |
| 192 MoveToPendingQueue(); |
| 193 DCHECK(empty()); |
| 194 cache_->ResetSharedWritersProcessPendingQueue(entry_); |
| 195 } |
| 196 |
| 197 void HttpCache::SharedWriters::StopCaching(Transaction* transaction) { |
| 198 // If this is the only transaction in SharedWriters either in validation or |
| 199 // reading stage, then stopping will be successful. If not, then we will not |
| 200 // stop caching since there are other consumers waiting to read from the |
| 201 // cache. |
| 202 bool result = false; |
| 203 if (transaction == validating_transaction_) { |
| 204 if (all_writers_.empty()) { |
| 205 result = true; |
| 206 validating_transaction_ = nullptr; |
| 207 } |
| 208 } else if (all_writers_.size() == 1 && all_writers_.count(transaction) && |
| 209 !validating_transaction_) { |
| 210 if (active_transaction_ == transaction) { |
| 211 active_transaction_ = nullptr; |
| 212 } |
| 213 all_writers_.erase(transaction); |
| 214 result = true; |
| 215 } |
| 216 if (result) { |
| 217 transaction->ContinueWithoutSharedWriting(std::move(network_transaction_), |
| 218 true); |
| 219 entry_->writer = transaction; |
| 220 MoveToPendingQueue(); |
| 221 } |
| 222 // If stopped, let the cache_ destroy |this|. |
| 223 if (result) |
| 224 cache_->ResetSharedWriters(entry_); |
| 225 } |
| 226 |
| 227 void HttpCache::SharedWriters::RemoveIdleTransaction(Transaction* transaction) { |
| 228 // The transaction should be part of all_writers. |
| 229 auto it = all_writers_.find(transaction); |
| 230 DCHECK(it != all_writers_.end()); |
| 231 all_writers_.erase(transaction); |
| 232 transaction->ResetShared(); |
| 233 PriorityChanged(); |
| 234 // If the response is not complete and there are no more consumers for this, |
| 235 // attempt to mark it as truncated. If |this| is empty, it will also be |
| 236 // destroyed in this call. |
| 237 if (empty()) |
| 238 cache_->RemovedSharedWriterTransaction(transaction, entry_); |
| 239 } |
| 240 |
| 241 void HttpCache::SharedWriters::RemoveWaitingForReadTransaction( |
| 242 Transaction* transaction) { |
| 243 auto it = waiting_for_read_.begin(); |
| 244 for (; it != waiting_for_read_.end(); it++) { |
| 245 if (transaction == it->transaction) { |
| 246 waiting_for_read_.erase(it); |
| 247 all_writers_.erase(transaction); |
| 248 transaction->ResetShared(); |
| 249 PriorityChanged(); |
| 250 // If a waiting transaction existed, there should have been an |
| 251 // active_transaction_. |
| 252 DCHECK(active_transaction_); |
| 253 break; |
| 254 } |
| 255 } |
| 256 } |
| 257 |
| 258 void HttpCache::SharedWriters::RemoveValidatingTransaction( |
| 259 Transaction* transaction) { |
| 260 DCHECK_EQ(validating_transaction_, transaction); |
| 261 validating_transaction_ = nullptr; |
| 262 transaction->ResetShared(); |
| 263 ProcessFirstWaitingValidation(); |
| 264 if (empty()) |
| 265 cache_->ResetSharedWritersProcessPendingQueue(entry_); |
| 266 } |
| 267 |
| 268 bool HttpCache::SharedWriters::RemoveWaitingForValidationTransaction( |
| 269 Transaction* transaction) { |
| 270 auto it = std::find(waiting_for_validation_.begin(), |
| 271 waiting_for_validation_.end(), transaction); |
| 272 if (it != waiting_for_validation_.end()) { |
| 273 transaction->ResetShared(); |
| 274 waiting_for_validation_.erase(it); |
| 275 return true; |
| 276 } |
| 277 return false; |
| 278 } |
| 279 |
| 280 void HttpCache::SharedWriters::RemoveActiveTransaction( |
| 281 Transaction* transaction) { |
| 282 DCHECK_EQ(active_transaction_, transaction); |
| 283 ResetActiveTransaction(); |
| 284 callback_.Reset(); |
| 285 // If the response is not complete and there are no more consumers for this, |
| 286 // attempt to mark it as truncated. If |this| is empty, it will also be |
| 287 // destroyed in this call. |
| 288 if (empty()) |
| 289 cache_->RemovedSharedWriterTransaction(transaction, entry_); |
| 290 } |
| 291 bool HttpCache::SharedWriters::empty() { |
| 292 int count = all_writers_.size() + waiting_for_validation_.size() + |
| 293 (validating_transaction_ ? 1 : 0); |
| 294 return count ? false : true; |
| 295 } |
| 296 |
| 297 bool HttpCache::SharedWriters::CanAddNewTransaction() { |
| 298 if (next_state_ == State::CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE) |
| 299 return false; |
| 300 return true; |
| 301 } |
| 302 |
| 303 void HttpCache::SharedWriters::PriorityChanged() { |
| 304 RequestPriority current_highest = getCurrentHighestPriority(); |
| 305 if (priority_ != current_highest) { |
| 306 network_transaction_->SetPriority(current_highest); |
| 307 priority_ = current_highest; |
| 308 } |
| 309 } |
| 310 |
| 311 HttpCache::SharedWriters::WaitingForRead::WaitingForRead( |
| 312 Transaction* cache_transaction, |
| 313 scoped_refptr<IOBuffer> buf, |
| 314 int len, |
| 315 const CompletionCallback& consumer_callback) |
| 316 : transaction(cache_transaction), |
| 317 read_buf(std::move(buf)), |
| 318 read_buf_len(len), |
| 319 write_len(0), |
| 320 callback(consumer_callback) {} |
| 321 |
| 322 HttpCache::SharedWriters::WaitingForRead::~WaitingForRead() {} |
| 323 |
| 324 HttpCache::SharedWriters::WaitingForRead::WaitingForRead( |
| 325 const WaitingForRead&) = default; |
| 326 |
| 327 int HttpCache::SharedWriters::DoLoop(int result) { |
| 328 DCHECK(next_state_ != State::NONE); |
| 329 |
| 330 int rv = result; |
| 331 |
| 332 do { |
| 333 State state = next_state_; |
| 334 next_state_ = State::NONE; |
| 335 |
| 336 switch (state) { |
| 337 case State::NETWORK_READ: |
| 338 DCHECK_EQ(OK, rv); |
| 339 rv = DoNetworkRead(); |
| 340 break; |
| 341 case State::NETWORK_READ_COMPLETE: |
| 342 rv = DoNetworkReadComplete(rv); |
| 343 break; |
| 344 case State::CACHE_WRITE_DATA: |
| 345 rv = DoCacheWriteData(rv); |
| 346 break; |
| 347 case State::CACHE_WRITE_DATA_COMPLETE: |
| 348 rv = DoCacheWriteDataComplete(rv); |
| 349 break; |
| 350 case State::CACHE_WRITE_TRUNCATED_RESPONSE: |
| 351 rv = DoCacheWriteTruncatedResponse(); |
| 352 break; |
| 353 case State::CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE: |
| 354 rv = DoCacheWriteTruncatedResponseComplete(rv); |
| 355 break; |
| 356 default: |
| 357 NOTREACHED() << "bad state"; |
| 358 rv = ERR_FAILED; |
| 359 break; |
| 360 } |
| 361 } while (next_state_ != State::DONE && rv != ERR_IO_PENDING && |
| 362 next_state_ != State::NONE); |
| 363 |
| 364 // Save the callback as this object may be destroyed in the cache callback. |
| 365 bool destroyed = false; |
| 366 CompletionCallback callback = callback_; |
| 367 if (next_state_ == State::DONE && cache_callback_) { |
| 368 base::ResetAndReturn(&cache_callback_).Run(&destroyed); |
| 369 } |
| 370 |
| 371 if (rv != ERR_IO_PENDING && !callback.is_null()) { |
| 372 if (!destroyed) { |
| 373 read_buf_ = NULL; // Release the buffer before invoking the callback. |
| 374 base::ResetAndReturn(&callback_).Run(rv); |
| 375 } else { |
| 376 base::ResetAndReturn(&callback).Run(rv); |
| 377 } |
| 378 } |
| 379 // This object may have been destroyed in the callback or cache_callback_. |
| 380 |
| 381 return rv; |
| 382 } |
| 383 |
| 384 int HttpCache::SharedWriters::DoNetworkRead() { |
| 385 next_state_ = State::NETWORK_READ_COMPLETE; |
| 386 return network_transaction_->Read(read_buf_.get(), io_buf_len_, io_callback_); |
| 387 } |
| 388 |
| 389 int HttpCache::SharedWriters::DoNetworkReadComplete(int result) { |
| 390 // Remember at this point active_transaction_ may or may not be alive. |
| 391 if (result < 0) { |
| 392 // Empty SharedWriters of all transactions. |
| 393 OnNetworkReadFailure(result); |
| 394 return result; |
| 395 } |
| 396 |
| 397 if (result == 0) { |
| 398 // Check if the response is actually completed or if not, attempt to mark |
| 399 // the entry as truncated. |
| 400 if (cache_->IsResponseCompleted(entry_, |
| 401 network_transaction_->GetResponseInfo())) { |
| 402 ProcessWaitingForReadTransactions(result); |
| 403 ResponseDataComplete(); |
| 404 } else { |
| 405 OnNetworkReadFailure(result); |
| 406 } |
| 407 return result; |
| 408 } |
| 409 |
| 410 // Successful non zero response. |
| 411 |
| 412 // if no consumer exists, then invoke cache write itself. |
| 413 if (!active_transaction_) |
| 414 next_state_ = State::CACHE_WRITE_DATA; |
| 415 |
| 416 return result; |
| 417 } |
| 418 |
| 419 int HttpCache::SharedWriters::DoCacheWriteData(int num_bytes) { |
| 420 next_state_ = State::CACHE_WRITE_DATA_COMPLETE; |
| 421 write_len_ = num_bytes; |
| 422 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
| 423 return entry_->disk_entry->WriteData(kResponseContentIndex, current_size, |
| 424 read_buf_.get(), num_bytes, io_callback_, |
| 425 true); |
| 426 } |
| 427 |
| 428 int HttpCache::SharedWriters::DoCacheWriteDataComplete(int result) { |
| 429 if (result != write_len_) { |
| 430 // Need to take care of all the transactions in SharedWriters and |
| 431 // delete SharedWriters as without the cache, we cannot continue the shared |
| 432 // logic. |
| 433 OnCacheWriteFailure(); |
| 434 } else { |
| 435 OnCacheWriteSuccess(result); |
| 436 } |
| 437 |
| 438 return result; |
| 439 } |
| 440 |
| 441 int HttpCache::SharedWriters::DoCacheWriteTruncatedResponse() { |
| 442 next_state_ = State::CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE; |
| 443 return cache_->WriteResponseInfo(entry_, |
| 444 network_transaction_->GetResponseInfo(), |
| 445 io_callback_, true, &io_buf_len_); |
| 446 } |
| 447 |
| 448 int HttpCache::SharedWriters::DoCacheWriteTruncatedResponseComplete( |
| 449 int result) { |
| 450 bool success = true; |
| 451 if (result != io_buf_len_) { |
| 452 DLOG(ERROR) << "failed to write response info to cache"; |
| 453 success = false; |
| 454 } |
| 455 |
| 456 next_state_ = State::DONE; |
| 457 cache_callback_ = base::Bind(&HttpCache::ResponseDoneSharedWriters, |
| 458 cache_->GetWeakPtr(), entry_, success); |
| 459 |
| 460 // If consumer exists, return the saved value. |
| 461 int rv = 0; |
| 462 if (callback_) { |
| 463 rv = rv_post_truncation_; |
| 464 rv_post_truncation_ = 0; |
| 465 } else { |
| 466 rv = result; |
| 467 } |
| 468 return rv; |
| 469 } |
| 470 |
| 471 void HttpCache::SharedWriters::OnNetworkReadFailure(int result) { |
| 472 FailureCleanup(result, false); |
| 473 if (AttemptTruncation()) { |
| 474 rv_post_truncation_ = result; |
| 475 } else { |
| 476 next_state_ = State::DONE; |
| 477 cache_callback_ = base::Bind(&HttpCache::ResponseDoneSharedWriters, |
| 478 cache_->GetWeakPtr(), entry_, false); |
| 479 } |
| 480 } |
| 481 |
| 482 bool HttpCache::SharedWriters::AttemptTruncation() { |
| 483 if (cache_->CanResumeEntry(true, "GET", |
| 484 network_transaction_->GetResponseInfo(), entry_)) { |
| 485 next_state_ = State::CACHE_WRITE_TRUNCATED_RESPONSE; |
| 486 return true; |
| 487 } else { |
| 488 return false; |
| 489 } |
| 490 } |
| 491 |
| 492 void HttpCache::SharedWriters::OnCacheWriteSuccess(int result) { |
| 493 // Save the data in all the waiting transactions' read buffers. |
| 494 for (auto it = waiting_for_read_.begin(); it != waiting_for_read_.end(); |
| 495 it++) { |
| 496 it->write_len = std::min(it->read_buf_len, result); |
| 497 memcpy(it->read_buf->data(), read_buf_->data(), it->write_len); |
| 498 } |
| 499 // Notify waiting_for_read_. Tasks will be posted for all the |
| 500 // transactions. |
| 501 ProcessWaitingForReadTransactions(write_len_); |
| 502 |
| 503 if (result > 0) { // not the end of response |
| 504 active_transaction_ = nullptr; |
| 505 return; |
| 506 } |
| 507 |
| 508 DCHECK_EQ(result, 0); |
| 509 |
| 510 ResponseDataComplete(); |
| 511 } |
| 512 |
| 513 void HttpCache::SharedWriters::ResponseDataComplete() { |
| 514 ResetActiveTransaction(); |
| 515 |
| 516 // If there is a transaction validating currently, return. |
| 517 if (validating_transaction_) |
| 518 return; |
| 519 |
| 520 // Else empty the SharedWriters object. |
| 521 MoveIdleWritersToReaders(); |
| 522 DCHECK(all_writers_.empty()); |
| 523 |
| 524 MoveToPendingQueue(); |
| 525 |
| 526 // Inform cache_ so it can take care of entry_. |
| 527 next_state_ = State::DONE; |
| 528 cache_callback_ = base::Bind(&HttpCache::ResponseDoneSharedWriters, |
| 529 cache_->GetWeakPtr(), entry_, true); |
| 530 } |
| 531 |
| 532 void HttpCache::SharedWriters::OnCacheWriteFailure() { |
| 533 Transaction* current_writer = active_transaction_; |
| 534 |
| 535 // Needs network_transaction_ to be valid so needs to be invoked before |
| 536 // ContinueWithoutSharedWriting. |
| 537 FailureCleanup(net::ERR_CACHE_WRITE_FAILURE, true); |
| 538 |
| 539 if (current_writer) // If the transaction is still alive in this callback. |
| 540 current_writer->ContinueWithoutSharedWriting( |
| 541 std::move(network_transaction_), false); |
| 542 |
| 543 // Inform cache_ so it can take care of entry_. |
| 544 next_state_ = State::DONE; |
| 545 cache_callback_ = base::Bind(&HttpCache::ResponseDoneSharedWriters, |
| 546 cache_->GetWeakPtr(), entry_, false); |
| 547 } |
| 548 |
| 549 void HttpCache::SharedWriters::FailureCleanup(int error, |
| 550 bool continue_network_reading) { |
| 551 ResetActiveTransaction(continue_network_reading); |
| 552 |
| 553 // Notify waiting_for_read_ of the failure. Tasks will be posted for all the |
| 554 // transactions. |
| 555 ProcessWaitingForReadTransactions(error); |
| 556 |
| 557 // Idle readers should know to fail when Read is invoked by their consumers. |
| 558 SetIdleWritersFailState(error); |
| 559 DCHECK(all_writers_.empty()); |
| 560 |
| 561 // If there exists a validating_transaction_, it may be waiting |
| 562 // to read response headers from the cache or waiting for receiving |
| 563 // validation response from the network. In both scenarios, it should be safe |
| 564 // to fail. |
| 565 if (validating_transaction_) { |
| 566 validating_transaction_->SetSharedWritingFailState(error); |
| 567 validating_transaction_->ResetShared(true); |
| 568 validating_transaction_ = nullptr; |
| 569 } |
| 570 |
| 571 MoveToPendingQueue(); |
| 572 } |
| 573 |
| 574 void HttpCache::SharedWriters::MoveToPendingQueue() { |
| 575 // For maintaining the order of the transactions as they arrived, append |
| 576 // these to the front of the pending_queue. Note that the order is preserved |
| 577 // only among the transactions that are eligible for sharing. For others, they |
| 578 // may have arrived earlier but may be processed later which is fair since |
| 579 // they have to anyways wait till the entry is written to the cache. |
| 580 while (!waiting_for_validation_.empty()) { |
| 581 Transaction* transaction = waiting_for_validation_.back(); |
| 582 transaction->ResetShared(true); |
| 583 entry_->pending_queue.push_front(transaction); |
| 584 waiting_for_validation_.pop_back(); |
| 585 } |
| 586 } |
| 587 |
| 588 void HttpCache::SharedWriters::MoveFromPendingQueue() { |
| 589 auto it = entry_->pending_queue.begin(); |
| 590 while (it != entry_->pending_queue.end()) { |
| 591 Transaction* transaction = *it; |
| 592 if (transaction->IsEligibleForSharedWriting()) { |
| 593 transaction->SetShared(); |
| 594 waiting_for_validation_.push_back(transaction); |
| 595 it = entry_->pending_queue.erase(it); |
| 596 } else { |
| 597 ++it; |
| 598 } |
| 599 } |
| 600 } |
| 601 |
| 602 void HttpCache::SharedWriters::MoveIdleWritersToReaders() { |
| 603 // Should be invoked after waiting_for_read_ are all processed so that |
| 604 // all_writers_ only contains the idle writers. |
| 605 DCHECK(waiting_for_read_.empty()); |
| 606 DCHECK(!active_transaction_); |
| 607 for (auto idle_writer : all_writers_) { |
| 608 entry_->readers.insert(idle_writer); |
| 609 idle_writer->ResetShared(false, true); |
| 610 } |
| 611 all_writers_.clear(); |
| 612 } |
| 613 |
| 614 void HttpCache::SharedWriters::ProcessFirstWaitingValidation() { |
| 615 if (!waiting_for_validation_.empty() || validating_transaction_) |
| 616 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 617 FROM_HERE, |
| 618 base::Bind(&HttpCache::SharedWriters::OnProcessFirstWaitingValidation, |
| 619 weak_factory_.GetWeakPtr())); |
| 620 } |
| 621 |
| 622 void HttpCache::SharedWriters::OnProcessFirstWaitingValidation() { |
| 623 if (waiting_for_validation_.empty() && !validating_transaction_) |
| 624 return; |
| 625 |
| 626 Transaction* transaction = validating_transaction_; |
| 627 if (!transaction) { |
| 628 transaction = waiting_for_validation_.front(); |
| 629 waiting_for_validation_.erase(waiting_for_validation_.begin()); |
| 630 validating_transaction_ = transaction; |
| 631 } |
| 632 transaction->io_callback().Run(OK); |
| 633 } |
| 634 |
| 635 void HttpCache::SharedWriters::ProcessWaitingForReadTransactions(int result) { |
| 636 for (auto it = waiting_for_read_.begin(); it != waiting_for_read_.end(); |
| 637 it++) { |
| 638 Transaction* transaction = it->transaction; |
| 639 |
| 640 if (result > 0) { // success |
| 641 // Fill result with the length of buffer filled for this transaction which |
| 642 // may be different from the transaction that actually wrote to the cache |
| 643 // based on the buffer size. |
| 644 result = it->write_len; |
| 645 } else { |
| 646 // If its response completion or failure, this transaction needs to be |
| 647 // removed. |
| 648 transaction->ResetShared(); |
| 649 all_writers_.erase(transaction); |
| 650 } |
| 651 |
| 652 // Post task to notify transaction. |
| 653 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 654 FROM_HERE, base::Bind(it->callback, result)); |
| 655 } |
| 656 |
| 657 waiting_for_read_.clear(); |
| 658 } |
| 659 |
| 660 void HttpCache::SharedWriters::ResetActiveTransaction( |
| 661 bool continue_network_reading) { |
| 662 // If active_transaction_ is already destroyed, return. |
| 663 if (!active_transaction_) |
| 664 return; |
| 665 active_transaction_->ResetShared(continue_network_reading); |
| 666 all_writers_.erase(active_transaction_); |
| 667 active_transaction_ = nullptr; |
| 668 PriorityChanged(); |
| 669 } |
| 670 |
| 671 void HttpCache::SharedWriters::SetIdleWritersFailState(int result) { |
| 672 // Since this is only for idle transactions, all waiting_for_read_ and |
| 673 // active_transaction_ should be empty. |
| 674 DCHECK(waiting_for_read_.empty()); |
| 675 DCHECK(!active_transaction_); |
| 676 |
| 677 for (auto transaction : all_writers_) { |
| 678 transaction->SetSharedWritingFailState(result); |
| 679 transaction->ResetShared(); |
| 680 } |
| 681 |
| 682 all_writers_.clear(); |
| 683 } |
| 684 |
| 685 void HttpCache::SharedWriters::ValidationDoneContinue( |
| 686 Transaction* transaction, |
| 687 RequestPriority priority) { |
| 688 validating_transaction_ = nullptr; |
| 689 if (priority > priority_) { |
| 690 network_transaction_->SetPriority(priority); |
| 691 priority_ = priority; |
| 692 } |
| 693 all_writers_.insert(transaction); |
| 694 ProcessFirstWaitingValidation(); |
| 695 } |
| 696 |
| 697 RequestPriority HttpCache::SharedWriters::getCurrentHighestPriority() { |
| 698 RequestPriority priority = MINIMUM_PRIORITY; |
| 699 for (auto transaction : all_writers_) |
| 700 priority = std::max(transaction->priority(), priority); |
| 701 return priority; |
| 702 } |
| 703 |
| 704 void HttpCache::SharedWriters::OnIOComplete(int result) { |
| 705 DoLoop(result); |
| 706 } |
| 707 |
| 708 } // namespace net |
| OLD | NEW |