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 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 | 10 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 66 |
67 HttpCache::DefaultBackend::~DefaultBackend() {} | 67 HttpCache::DefaultBackend::~DefaultBackend() {} |
68 | 68 |
69 // static | 69 // static |
70 HttpCache::BackendFactory* HttpCache::DefaultBackend::InMemory(int max_bytes) { | 70 HttpCache::BackendFactory* HttpCache::DefaultBackend::InMemory(int max_bytes) { |
71 return new DefaultBackend(MEMORY_CACHE, net::CACHE_BACKEND_DEFAULT, | 71 return new DefaultBackend(MEMORY_CACHE, net::CACHE_BACKEND_DEFAULT, |
72 base::FilePath(), max_bytes, NULL); | 72 base::FilePath(), max_bytes, NULL); |
73 } | 73 } |
74 | 74 |
75 int HttpCache::DefaultBackend::CreateBackend( | 75 int HttpCache::DefaultBackend::CreateBackend( |
76 NetLog* net_log, disk_cache::Backend** backend, | 76 NetLog* net_log, scoped_ptr<disk_cache::Backend>* backend, |
77 const CompletionCallback& callback) { | 77 const CompletionCallback& callback) { |
78 DCHECK_GE(max_bytes_, 0); | 78 DCHECK_GE(max_bytes_, 0); |
79 return disk_cache::CreateCacheBackend(type_, | 79 return disk_cache::CreateCacheBackend(type_, |
80 backend_type_, | 80 backend_type_, |
81 path_, | 81 path_, |
82 max_bytes_, | 82 max_bytes_, |
83 true, | 83 true, |
84 thread_.get(), | 84 thread_.get(), |
85 net_log, | 85 net_log, |
86 backend, | 86 backend, |
(...skipping 14 matching lines...) Expand all Loading... |
101 disk_entry->Close(); | 101 disk_entry->Close(); |
102 disk_entry = NULL; | 102 disk_entry = NULL; |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 //----------------------------------------------------------------------------- | 106 //----------------------------------------------------------------------------- |
107 | 107 |
108 // This structure keeps track of work items that are attempting to create or | 108 // This structure keeps track of work items that are attempting to create or |
109 // open cache entries or the backend itself. | 109 // open cache entries or the backend itself. |
110 struct HttpCache::PendingOp { | 110 struct HttpCache::PendingOp { |
111 PendingOp() : disk_entry(NULL), backend(NULL), writer(NULL) {} | 111 PendingOp() : disk_entry(NULL), writer(NULL) {} |
112 ~PendingOp() {} | 112 ~PendingOp() {} |
113 | 113 |
114 disk_cache::Entry* disk_entry; | 114 disk_cache::Entry* disk_entry; |
115 disk_cache::Backend* backend; | 115 scoped_ptr<disk_cache::Backend> backend; |
116 WorkItem* writer; | 116 WorkItem* writer; |
117 CompletionCallback callback; // BackendCallback. | 117 CompletionCallback callback; // BackendCallback. |
118 WorkItemList pending_queue; | 118 WorkItemList pending_queue; |
119 }; | 119 }; |
120 | 120 |
121 //----------------------------------------------------------------------------- | 121 //----------------------------------------------------------------------------- |
122 | 122 |
123 // The type of operation represented by a work item. | 123 // The type of operation represented by a work item. |
124 enum WorkItemOperation { | 124 enum WorkItemOperation { |
125 WI_CREATE_BACKEND, | 125 WI_CREATE_BACKEND, |
(...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1104 } | 1104 } |
1105 } | 1105 } |
1106 | 1106 |
1107 void HttpCache::OnBackendCreated(int result, PendingOp* pending_op) { | 1107 void HttpCache::OnBackendCreated(int result, PendingOp* pending_op) { |
1108 scoped_ptr<WorkItem> item(pending_op->writer); | 1108 scoped_ptr<WorkItem> item(pending_op->writer); |
1109 WorkItemOperation op = item->operation(); | 1109 WorkItemOperation op = item->operation(); |
1110 DCHECK_EQ(WI_CREATE_BACKEND, op); | 1110 DCHECK_EQ(WI_CREATE_BACKEND, op); |
1111 | 1111 |
1112 // We don't need the callback anymore. | 1112 // We don't need the callback anymore. |
1113 pending_op->callback.Reset(); | 1113 pending_op->callback.Reset(); |
1114 disk_cache::Backend* backend = pending_op->backend; | |
1115 | 1114 |
1116 if (backend_factory_.get()) { | 1115 if (backend_factory_.get()) { |
1117 // We may end up calling OnBackendCreated multiple times if we have pending | 1116 // We may end up calling OnBackendCreated multiple times if we have pending |
1118 // work items. The first call saves the backend and releases the factory, | 1117 // work items. The first call saves the backend and releases the factory, |
1119 // and the last call clears building_backend_. | 1118 // and the last call clears building_backend_. |
1120 backend_factory_.reset(); // Reclaim memory. | 1119 backend_factory_.reset(); // Reclaim memory. |
1121 if (result == OK) | 1120 if (result == OK) |
1122 disk_cache_.reset(backend); | 1121 disk_cache_ = pending_op->backend.Pass(); |
1123 } | 1122 } |
1124 | 1123 |
1125 if (!pending_op->pending_queue.empty()) { | 1124 if (!pending_op->pending_queue.empty()) { |
1126 WorkItem* pending_item = pending_op->pending_queue.front(); | 1125 WorkItem* pending_item = pending_op->pending_queue.front(); |
1127 pending_op->pending_queue.pop_front(); | 1126 pending_op->pending_queue.pop_front(); |
1128 DCHECK_EQ(WI_CREATE_BACKEND, pending_item->operation()); | 1127 DCHECK_EQ(WI_CREATE_BACKEND, pending_item->operation()); |
1129 | 1128 |
1130 // We want to process a single callback at a time, because the cache may | 1129 // We want to process a single callback at a time, because the cache may |
1131 // go away from the callback. | 1130 // go away from the callback. |
1132 pending_op->writer = pending_item; | 1131 pending_op->writer = pending_item; |
1133 | 1132 |
1134 base::MessageLoop::current()->PostTask( | 1133 base::MessageLoop::current()->PostTask( |
1135 FROM_HERE, | 1134 FROM_HERE, |
1136 base::Bind( | 1135 base::Bind( |
1137 &HttpCache::OnBackendCreated, AsWeakPtr(), result, pending_op)); | 1136 &HttpCache::OnBackendCreated, AsWeakPtr(), result, pending_op)); |
1138 } else { | 1137 } else { |
1139 building_backend_ = false; | 1138 building_backend_ = false; |
1140 DeletePendingOp(pending_op); | 1139 DeletePendingOp(pending_op); |
1141 } | 1140 } |
1142 | 1141 |
1143 // The cache may be gone when we return from the callback. | 1142 // The cache may be gone when we return from the callback. |
1144 if (!item->DoCallback(result, backend)) | 1143 if (!item->DoCallback(result, disk_cache_.get())) |
1145 item->NotifyTransaction(result, NULL); | 1144 item->NotifyTransaction(result, NULL); |
1146 } | 1145 } |
1147 | 1146 |
1148 } // namespace net | 1147 } // namespace net |
OLD | NEW |