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/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 Loading... | |
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_) { | |
willchan no longer on Chromium
2010/12/15 00:03:14
indentation is off for these cases.
http://google-
agl
2010/12/15 16:28:08
Done.
| |
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((disk_cache::Backend**) &callback_->user_data, |
112 callback_); | |
98 } | 113 } |
99 | 114 |
100 int DiskCacheBasedSSLHostInfo::DoGetBackendComplete(int rv) { | 115 int DiskCacheBasedSSLHostInfo::DoGetBackendComplete(int rv) { |
101 if (rv == OK) { | 116 if (rv == OK) { |
117 backend_ = (disk_cache::Backend*) callback_->user_data; | |
102 state_ = OPEN; | 118 state_ = OPEN; |
103 } else { | 119 } else { |
104 state_ = WAIT_FOR_DATA_READY_DONE; | 120 state_ = WAIT_FOR_DATA_READY_DONE; |
105 } | 121 } |
106 return OK; | 122 return OK; |
107 } | 123 } |
108 | 124 |
109 int DiskCacheBasedSSLHostInfo::DoOpen() { | 125 int DiskCacheBasedSSLHostInfo::DoOpen() { |
110 state_ = OPEN_COMPLETE; | 126 state_ = OPEN_COMPLETE; |
111 return backend_->OpenEntry(key(), &entry_, callback_.get()); | 127 return backend_->OpenEntry(key(), (disk_cache::Entry**) &callback_->user_data, |
128 callback_); | |
112 } | 129 } |
113 | 130 |
114 int DiskCacheBasedSSLHostInfo::DoOpenComplete(int rv) { | 131 int DiskCacheBasedSSLHostInfo::DoOpenComplete(int rv) { |
115 if (rv == OK) { | 132 if (rv == OK) { |
133 entry_ = (disk_cache::Entry*) callback_->user_data; | |
116 state_ = READ; | 134 state_ = READ; |
117 } else { | 135 } else { |
118 state_ = WAIT_FOR_DATA_READY_DONE; | 136 state_ = WAIT_FOR_DATA_READY_DONE; |
119 } | 137 } |
120 | 138 |
121 return OK; | 139 return OK; |
122 } | 140 } |
123 | 141 |
124 int DiskCacheBasedSSLHostInfo::DoRead() { | 142 int DiskCacheBasedSSLHostInfo::DoRead() { |
125 const int32 size = entry_->GetDataSize(0 /* index */); | 143 const int32 size = entry_->GetDataSize(0 /* index */); |
126 if (!size) { | 144 if (!size) { |
127 state_ = WAIT_FOR_DATA_READY_DONE; | 145 state_ = WAIT_FOR_DATA_READY_DONE; |
128 return OK; | 146 return OK; |
129 } | 147 } |
130 | 148 |
131 read_buffer_ = new IOBuffer(size); | 149 read_buffer_ = new IOBuffer(size); |
132 state_ = READ_COMPLETE; | 150 state_ = READ_COMPLETE; |
133 return entry_->ReadData(0 /* index */, 0 /* offset */, read_buffer_, | 151 return entry_->ReadData(0 /* index */, 0 /* offset */, read_buffer_, |
134 size, callback_.get()); | 152 size, callback_); |
135 } | 153 } |
136 | 154 |
137 int DiskCacheBasedSSLHostInfo::DoReadComplete(int rv) { | 155 int DiskCacheBasedSSLHostInfo::DoReadComplete(int rv) { |
138 if (rv > 0) | 156 if (rv > 0) |
139 data_ = std::string(read_buffer_->data(), rv); | 157 data_ = std::string(read_buffer_->data(), rv); |
140 | 158 |
141 state_ = WAIT_FOR_DATA_READY_DONE; | 159 state_ = WAIT_FOR_DATA_READY_DONE; |
142 return OK; | 160 return OK; |
143 } | 161 } |
144 | 162 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 if (!backend_) | 206 if (!backend_) |
189 return; | 207 return; |
190 | 208 |
191 state_ = CREATE; | 209 state_ = CREATE; |
192 DoLoop(OK); | 210 DoLoop(OK); |
193 } | 211 } |
194 | 212 |
195 int DiskCacheBasedSSLHostInfo::DoCreate() { | 213 int DiskCacheBasedSSLHostInfo::DoCreate() { |
196 DCHECK(entry_ == NULL); | 214 DCHECK(entry_ == NULL); |
197 state_ = CREATE_COMPLETE; | 215 state_ = CREATE_COMPLETE; |
198 return backend_->CreateEntry(key(), &entry_, callback_.get()); | 216 return backend_->CreateEntry( |
217 key(), (disk_cache::Entry**) &callback_->user_data, callback_); | |
199 } | 218 } |
200 | 219 |
201 int DiskCacheBasedSSLHostInfo::DoCreateComplete(int rv) { | 220 int DiskCacheBasedSSLHostInfo::DoCreateComplete(int rv) { |
202 if (rv != OK) { | 221 if (rv != OK) { |
203 state_ = SET_DONE; | 222 state_ = SET_DONE; |
204 } else { | 223 } else { |
224 entry_ = (disk_cache::Entry*) callback_->user_data; | |
205 state_ = WRITE; | 225 state_ = WRITE; |
206 } | 226 } |
207 return OK; | 227 return OK; |
208 } | 228 } |
209 | 229 |
210 int DiskCacheBasedSSLHostInfo::DoWrite() { | 230 int DiskCacheBasedSSLHostInfo::DoWrite() { |
211 write_buffer_ = new IOBuffer(new_data_.size()); | 231 write_buffer_ = new IOBuffer(new_data_.size()); |
212 memcpy(write_buffer_->data(), new_data_.data(), new_data_.size()); | 232 memcpy(write_buffer_->data(), new_data_.data(), new_data_.size()); |
213 state_ = WRITE_COMPLETE; | 233 state_ = WRITE_COMPLETE; |
234 | |
214 return entry_->WriteData(0 /* index */, 0 /* offset */, write_buffer_, | 235 return entry_->WriteData(0 /* index */, 0 /* offset */, write_buffer_, |
215 new_data_.size(), callback_.get(), | 236 new_data_.size(), callback_, true /* truncate */); |
216 true /* truncate */); | |
217 } | 237 } |
218 | 238 |
219 int DiskCacheBasedSSLHostInfo::DoWriteComplete(int rv) { | 239 int DiskCacheBasedSSLHostInfo::DoWriteComplete(int rv) { |
220 state_ = SET_DONE; | 240 state_ = SET_DONE; |
221 return OK; | 241 return OK; |
222 } | 242 } |
223 | 243 |
224 int DiskCacheBasedSSLHostInfo::SetDone() { | 244 int DiskCacheBasedSSLHostInfo::SetDone() { |
225 if (entry_) | 245 if (entry_) |
226 entry_->Close(); | 246 entry_->Close(); |
227 entry_ = NULL; | 247 entry_ = NULL; |
228 state_ = NONE; | 248 state_ = NONE; |
229 return OK; | 249 return OK; |
230 } | 250 } |
231 | 251 |
232 } // namespace net | 252 } // namespace net |
OLD | NEW |