Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: net/http/disk_cache_based_ssl_host_info.cc

Issue 5826001: net: fix callbacks in DiskCacheBasedSSLHostInfo (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/disk_cache_based_ssl_host_info.h" 5 #include "net/http/disk_cache_based_ssl_host_info.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "net/base/io_buffer.h" 9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h" 10 #include "net/base/net_errors.h"
11 #include "net/http/http_cache.h" 11 #include "net/http/http_cache.h"
12 12
13 namespace net { 13 namespace net {
14 14
15 DiskCacheBasedSSLHostInfo::DiskCacheBasedSSLHostInfo( 15 DiskCacheBasedSSLHostInfo::DiskCacheBasedSSLHostInfo(
16 const std::string& hostname, 16 const std::string& hostname,
17 const SSLConfig& ssl_config, 17 const SSLConfig& ssl_config,
18 HttpCache* http_cache) 18 HttpCache* http_cache)
19 : SSLHostInfo(hostname, ssl_config), 19 : SSLHostInfo(hostname, ssl_config),
20 callback_(new CancelableCompletionCallback<DiskCacheBasedSSLHostInfo>( 20 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
21 ALLOW_THIS_IN_INITIALIZER_LIST(this), 21 callback_(new CallbackImpl(weak_ptr_factory_.GetWeakPtr(),
22 &DiskCacheBasedSSLHostInfo::DoLoop)), 22 &DiskCacheBasedSSLHostInfo::DoLoop)),
23 state_(GET_BACKEND), 23 state_(GET_BACKEND),
24 ready_(false), 24 ready_(false),
25 hostname_(hostname), 25 hostname_(hostname),
26 http_cache_(http_cache), 26 http_cache_(http_cache),
27 backend_(NULL), 27 backend_(NULL),
28 entry_(NULL), 28 entry_(NULL),
29 user_callback_(NULL) { 29 user_callback_(NULL) {
30 } 30 }
31 31
32 void DiskCacheBasedSSLHostInfo::Start() { 32 void DiskCacheBasedSSLHostInfo::Start() {
33 DCHECK(CalledOnValidThread()); 33 DCHECK(CalledOnValidThread());
34 DCHECK_EQ(GET_BACKEND, state_); 34 DCHECK_EQ(GET_BACKEND, state_);
35 DoLoop(OK); 35 DoLoop(OK);
36 } 36 }
37 37
38 DiskCacheBasedSSLHostInfo::~DiskCacheBasedSSLHostInfo() { 38 DiskCacheBasedSSLHostInfo::~DiskCacheBasedSSLHostInfo() {
39 DCHECK(!user_callback_); 39 DCHECK(!user_callback_);
40 if (entry_) 40 if (entry_)
41 entry_->Close(); 41 entry_->Close();
42 callback_->Cancel(); 42 if (!IsCallbackPending())
43 delete callback_;
43 } 44 }
44 45
45 std::string DiskCacheBasedSSLHostInfo::key() const { 46 std::string DiskCacheBasedSSLHostInfo::key() const {
46 return "sslhostinfo:" + hostname_; 47 return "sslhostinfo:" + hostname_;
47 } 48 }
48 49
49 void DiskCacheBasedSSLHostInfo::DoLoop(int rv) { 50 void DiskCacheBasedSSLHostInfo::DoLoop(int rv) {
50 do { 51 do {
51 switch (state_) { 52 switch (state_) {
52 case GET_BACKEND: 53 case GET_BACKEND:
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 case SET_DONE: 86 case SET_DONE:
86 rv = SetDone(); 87 rv = SetDone();
87 break; 88 break;
88 default: 89 default:
89 rv = OK; 90 rv = OK;
90 NOTREACHED(); 91 NOTREACHED();
91 } 92 }
92 } while (rv != ERR_IO_PENDING && state_ != NONE); 93 } while (rv != ERR_IO_PENDING && state_ != NONE);
93 } 94 }
94 95
96 bool DiskCacheBasedSSLHostInfo::IsCallbackPending() const {
97 switch (state_) {
98 case GET_BACKEND_COMPLETE:
99 case OPEN_COMPLETE:
100 case READ_COMPLETE:
101 case CREATE_COMPLETE:
102 case WRITE_COMPLETE:
103 return true;
104 default:
105 return false;
106 }
107 }
108
95 int DiskCacheBasedSSLHostInfo::DoGetBackend() { 109 int DiskCacheBasedSSLHostInfo::DoGetBackend() {
96 state_ = GET_BACKEND_COMPLETE; 110 state_ = GET_BACKEND_COMPLETE;
97 return http_cache_->GetBackend(&backend_, callback_.get()); 111 return http_cache_->GetBackend(callback_->backend_pointer(), callback_);
98 } 112 }
99 113
100 int DiskCacheBasedSSLHostInfo::DoGetBackendComplete(int rv) { 114 int DiskCacheBasedSSLHostInfo::DoGetBackendComplete(int rv) {
101 if (rv == OK) { 115 if (rv == OK) {
116 backend_ = callback_->backend();
102 state_ = OPEN; 117 state_ = OPEN;
103 } else { 118 } else {
104 state_ = WAIT_FOR_DATA_READY_DONE; 119 state_ = WAIT_FOR_DATA_READY_DONE;
105 } 120 }
106 return OK; 121 return OK;
107 } 122 }
108 123
109 int DiskCacheBasedSSLHostInfo::DoOpen() { 124 int DiskCacheBasedSSLHostInfo::DoOpen() {
110 state_ = OPEN_COMPLETE; 125 state_ = OPEN_COMPLETE;
111 return backend_->OpenEntry(key(), &entry_, callback_.get()); 126 return backend_->OpenEntry(key(), callback_->entry_pointer(), callback_);
112 } 127 }
113 128
114 int DiskCacheBasedSSLHostInfo::DoOpenComplete(int rv) { 129 int DiskCacheBasedSSLHostInfo::DoOpenComplete(int rv) {
115 if (rv == OK) { 130 if (rv == OK) {
131 entry_ = callback_->entry();
116 state_ = READ; 132 state_ = READ;
117 } else { 133 } else {
118 state_ = WAIT_FOR_DATA_READY_DONE; 134 state_ = WAIT_FOR_DATA_READY_DONE;
119 } 135 }
120 136
121 return OK; 137 return OK;
122 } 138 }
123 139
124 int DiskCacheBasedSSLHostInfo::DoRead() { 140 int DiskCacheBasedSSLHostInfo::DoRead() {
125 const int32 size = entry_->GetDataSize(0 /* index */); 141 const int32 size = entry_->GetDataSize(0 /* index */);
126 if (!size) { 142 if (!size) {
127 state_ = WAIT_FOR_DATA_READY_DONE; 143 state_ = WAIT_FOR_DATA_READY_DONE;
128 return OK; 144 return OK;
129 } 145 }
130 146
131 read_buffer_ = new IOBuffer(size); 147 read_buffer_ = new IOBuffer(size);
132 state_ = READ_COMPLETE; 148 state_ = READ_COMPLETE;
133 return entry_->ReadData(0 /* index */, 0 /* offset */, read_buffer_, 149 return entry_->ReadData(0 /* index */, 0 /* offset */, read_buffer_,
134 size, callback_.get()); 150 size, callback_);
135 } 151 }
136 152
137 int DiskCacheBasedSSLHostInfo::DoReadComplete(int rv) { 153 int DiskCacheBasedSSLHostInfo::DoReadComplete(int rv) {
138 if (rv > 0) 154 if (rv > 0)
139 data_ = std::string(read_buffer_->data(), rv); 155 data_ = std::string(read_buffer_->data(), rv);
140 156
141 state_ = WAIT_FOR_DATA_READY_DONE; 157 state_ = WAIT_FOR_DATA_READY_DONE;
142 return OK; 158 return OK;
143 } 159 }
144 160
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 if (!backend_) 204 if (!backend_)
189 return; 205 return;
190 206
191 state_ = CREATE; 207 state_ = CREATE;
192 DoLoop(OK); 208 DoLoop(OK);
193 } 209 }
194 210
195 int DiskCacheBasedSSLHostInfo::DoCreate() { 211 int DiskCacheBasedSSLHostInfo::DoCreate() {
196 DCHECK(entry_ == NULL); 212 DCHECK(entry_ == NULL);
197 state_ = CREATE_COMPLETE; 213 state_ = CREATE_COMPLETE;
198 return backend_->CreateEntry(key(), &entry_, callback_.get()); 214 return backend_->CreateEntry(key(), callback_->entry_pointer(), callback_);
199 } 215 }
200 216
201 int DiskCacheBasedSSLHostInfo::DoCreateComplete(int rv) { 217 int DiskCacheBasedSSLHostInfo::DoCreateComplete(int rv) {
202 if (rv != OK) { 218 if (rv != OK) {
203 state_ = SET_DONE; 219 state_ = SET_DONE;
204 } else { 220 } else {
221 entry_ = callback_->entry();
205 state_ = WRITE; 222 state_ = WRITE;
206 } 223 }
207 return OK; 224 return OK;
208 } 225 }
209 226
210 int DiskCacheBasedSSLHostInfo::DoWrite() { 227 int DiskCacheBasedSSLHostInfo::DoWrite() {
211 write_buffer_ = new IOBuffer(new_data_.size()); 228 write_buffer_ = new IOBuffer(new_data_.size());
212 memcpy(write_buffer_->data(), new_data_.data(), new_data_.size()); 229 memcpy(write_buffer_->data(), new_data_.data(), new_data_.size());
213 state_ = WRITE_COMPLETE; 230 state_ = WRITE_COMPLETE;
231
214 return entry_->WriteData(0 /* index */, 0 /* offset */, write_buffer_, 232 return entry_->WriteData(0 /* index */, 0 /* offset */, write_buffer_,
215 new_data_.size(), callback_.get(), 233 new_data_.size(), callback_, true /* truncate */);
216 true /* truncate */);
217 } 234 }
218 235
219 int DiskCacheBasedSSLHostInfo::DoWriteComplete(int rv) { 236 int DiskCacheBasedSSLHostInfo::DoWriteComplete(int rv) {
220 state_ = SET_DONE; 237 state_ = SET_DONE;
221 return OK; 238 return OK;
222 } 239 }
223 240
224 int DiskCacheBasedSSLHostInfo::SetDone() { 241 int DiskCacheBasedSSLHostInfo::SetDone() {
225 if (entry_) 242 if (entry_)
226 entry_->Close(); 243 entry_->Close();
227 entry_ = NULL; 244 entry_ = NULL;
228 state_ = NONE; 245 state_ = NONE;
229 return OK; 246 return OK;
230 } 247 }
231 248
232 } // namespace net 249 } // namespace net
OLDNEW
« net/http/disk_cache_based_ssl_host_info.h ('K') | « net/http/disk_cache_based_ssl_host_info.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698