| 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 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" | 12 #include "base/callback.h" |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 13 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 14 #include "base/format_macros.h" | 15 #include "base/format_macros.h" |
| 15 #include "base/location.h" | 16 #include "base/location.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 | 295 |
| 295 private: | 296 private: |
| 296 HttpCache* const http_cache_; | 297 HttpCache* const http_cache_; |
| 297 }; | 298 }; |
| 298 | 299 |
| 299 //----------------------------------------------------------------------------- | 300 //----------------------------------------------------------------------------- |
| 300 HttpCache::HttpCache(HttpNetworkSession* session, | 301 HttpCache::HttpCache(HttpNetworkSession* session, |
| 301 scoped_ptr<BackendFactory> backend_factory, | 302 scoped_ptr<BackendFactory> backend_factory, |
| 302 bool set_up_quic_server_info) | 303 bool set_up_quic_server_info) |
| 303 : HttpCache(make_scoped_ptr(new HttpNetworkLayer(session)), | 304 : HttpCache(make_scoped_ptr(new HttpNetworkLayer(session)), |
| 304 backend_factory.Pass(), | 305 std::move(backend_factory), |
| 305 set_up_quic_server_info) {} | 306 set_up_quic_server_info) {} |
| 306 | 307 |
| 307 HttpCache::HttpCache(scoped_ptr<HttpTransactionFactory> network_layer, | 308 HttpCache::HttpCache(scoped_ptr<HttpTransactionFactory> network_layer, |
| 308 scoped_ptr<BackendFactory> backend_factory, | 309 scoped_ptr<BackendFactory> backend_factory, |
| 309 bool set_up_quic_server_info) | 310 bool set_up_quic_server_info) |
| 310 : net_log_(nullptr), | 311 : net_log_(nullptr), |
| 311 backend_factory_(backend_factory.Pass()), | 312 backend_factory_(std::move(backend_factory)), |
| 312 building_backend_(false), | 313 building_backend_(false), |
| 313 bypass_lock_for_test_(false), | 314 bypass_lock_for_test_(false), |
| 314 fail_conditionalization_for_test_(false), | 315 fail_conditionalization_for_test_(false), |
| 315 mode_(NORMAL), | 316 mode_(NORMAL), |
| 316 network_layer_(network_layer.Pass()), | 317 network_layer_(std::move(network_layer)), |
| 317 clock_(new base::DefaultClock()), | 318 clock_(new base::DefaultClock()), |
| 318 weak_factory_(this) { | 319 weak_factory_(this) { |
| 319 HttpNetworkSession* session = network_layer_->GetSession(); | 320 HttpNetworkSession* session = network_layer_->GetSession(); |
| 320 // Session may be NULL in unittests. | 321 // Session may be NULL in unittests. |
| 321 // TODO(mmenke): Seems like tests could be changed to provide a session, | 322 // TODO(mmenke): Seems like tests could be changed to provide a session, |
| 322 // rather than having logic only used in unit tests here. | 323 // rather than having logic only used in unit tests here. |
| 323 if (session) { | 324 if (session) { |
| 324 net_log_ = session->net_log(); | 325 net_log_ = session->net_log(); |
| 325 if (set_up_quic_server_info && | 326 if (set_up_quic_server_info && |
| 326 !session->quic_stream_factory()->has_quic_server_info_factory()) { | 327 !session->quic_stream_factory()->has_quic_server_info_factory()) { |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 return this; | 474 return this; |
| 474 } | 475 } |
| 475 | 476 |
| 476 HttpNetworkSession* HttpCache::GetSession() { | 477 HttpNetworkSession* HttpCache::GetSession() { |
| 477 return network_layer_->GetSession(); | 478 return network_layer_->GetSession(); |
| 478 } | 479 } |
| 479 | 480 |
| 480 scoped_ptr<HttpTransactionFactory> | 481 scoped_ptr<HttpTransactionFactory> |
| 481 HttpCache::SetHttpNetworkTransactionFactoryForTesting( | 482 HttpCache::SetHttpNetworkTransactionFactoryForTesting( |
| 482 scoped_ptr<HttpTransactionFactory> new_network_layer) { | 483 scoped_ptr<HttpTransactionFactory> new_network_layer) { |
| 483 scoped_ptr<HttpTransactionFactory> old_network_layer(network_layer_.Pass()); | 484 scoped_ptr<HttpTransactionFactory> old_network_layer( |
| 484 network_layer_ = new_network_layer.Pass(); | 485 std::move(network_layer_)); |
| 485 return old_network_layer.Pass(); | 486 network_layer_ = std::move(new_network_layer); |
| 487 return old_network_layer; |
| 486 } | 488 } |
| 487 | 489 |
| 488 //----------------------------------------------------------------------------- | 490 //----------------------------------------------------------------------------- |
| 489 | 491 |
| 490 int HttpCache::CreateBackend(disk_cache::Backend** backend, | 492 int HttpCache::CreateBackend(disk_cache::Backend** backend, |
| 491 const CompletionCallback& callback) { | 493 const CompletionCallback& callback) { |
| 492 if (!backend_factory_.get()) | 494 if (!backend_factory_.get()) |
| 493 return ERR_FAILED; | 495 return ERR_FAILED; |
| 494 | 496 |
| 495 building_backend_ = true; | 497 building_backend_ = true; |
| (...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1134 | 1136 |
| 1135 // We don't need the callback anymore. | 1137 // We don't need the callback anymore. |
| 1136 pending_op->callback.Reset(); | 1138 pending_op->callback.Reset(); |
| 1137 | 1139 |
| 1138 if (backend_factory_.get()) { | 1140 if (backend_factory_.get()) { |
| 1139 // We may end up calling OnBackendCreated multiple times if we have pending | 1141 // We may end up calling OnBackendCreated multiple times if we have pending |
| 1140 // work items. The first call saves the backend and releases the factory, | 1142 // work items. The first call saves the backend and releases the factory, |
| 1141 // and the last call clears building_backend_. | 1143 // and the last call clears building_backend_. |
| 1142 backend_factory_.reset(); // Reclaim memory. | 1144 backend_factory_.reset(); // Reclaim memory. |
| 1143 if (result == OK) { | 1145 if (result == OK) { |
| 1144 disk_cache_ = pending_op->backend.Pass(); | 1146 disk_cache_ = std::move(pending_op->backend); |
| 1145 if (UseCertCache()) | 1147 if (UseCertCache()) |
| 1146 cert_cache_.reset(new DiskBasedCertCache(disk_cache_.get())); | 1148 cert_cache_.reset(new DiskBasedCertCache(disk_cache_.get())); |
| 1147 } | 1149 } |
| 1148 } | 1150 } |
| 1149 | 1151 |
| 1150 if (!pending_op->pending_queue.empty()) { | 1152 if (!pending_op->pending_queue.empty()) { |
| 1151 WorkItem* pending_item = pending_op->pending_queue.front(); | 1153 WorkItem* pending_item = pending_op->pending_queue.front(); |
| 1152 pending_op->pending_queue.pop_front(); | 1154 pending_op->pending_queue.pop_front(); |
| 1153 DCHECK_EQ(WI_CREATE_BACKEND, pending_item->operation()); | 1155 DCHECK_EQ(WI_CREATE_BACKEND, pending_item->operation()); |
| 1154 | 1156 |
| 1155 // We want to process a single callback at a time, because the cache may | 1157 // We want to process a single callback at a time, because the cache may |
| 1156 // go away from the callback. | 1158 // go away from the callback. |
| 1157 pending_op->writer = pending_item; | 1159 pending_op->writer = pending_item; |
| 1158 | 1160 |
| 1159 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1161 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1160 FROM_HERE, base::Bind(&HttpCache::OnBackendCreated, GetWeakPtr(), | 1162 FROM_HERE, base::Bind(&HttpCache::OnBackendCreated, GetWeakPtr(), |
| 1161 result, pending_op)); | 1163 result, pending_op)); |
| 1162 } else { | 1164 } else { |
| 1163 building_backend_ = false; | 1165 building_backend_ = false; |
| 1164 DeletePendingOp(pending_op); | 1166 DeletePendingOp(pending_op); |
| 1165 } | 1167 } |
| 1166 | 1168 |
| 1167 // The cache may be gone when we return from the callback. | 1169 // The cache may be gone when we return from the callback. |
| 1168 if (!item->DoCallback(result, disk_cache_.get())) | 1170 if (!item->DoCallback(result, disk_cache_.get())) |
| 1169 item->NotifyTransaction(result, NULL); | 1171 item->NotifyTransaction(result, NULL); |
| 1170 } | 1172 } |
| 1171 | 1173 |
| 1172 } // namespace net | 1174 } // namespace net |
| OLD | NEW |