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

Side by Side Diff: net/ocsp/nss_ocsp.cc

Issue 449009: AddRef() / Release() while URLRequest is alive in OCSPRequestSession (Closed)
Patch Set: fix wtc's comment Created 11 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/ocsp/nss_ocsp.h" 5 #include "net/ocsp/nss_ocsp.h"
6 6
7 #include <certt.h> 7 #include <certt.h>
8 #include <certdb.h> 8 #include <certdb.h>
9 #include <ocsp.h> 9 #include <ocsp.h>
10 #include <nspr.h> 10 #include <nspr.h>
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 } 121 }
122 } 122 }
123 123
124 bool Started() const { 124 bool Started() const {
125 return request_ != NULL; 125 return request_ != NULL;
126 } 126 }
127 127
128 void Cancel() { 128 void Cancel() {
129 // IO thread may set |io_loop_| to NULL, so protect by |lock_|. 129 // IO thread may set |io_loop_| to NULL, so protect by |lock_|.
130 AutoLock autolock(lock_); 130 AutoLock autolock(lock_);
131 if (io_loop_) { 131 CancelLocked();
132 io_loop_->PostTask(
133 FROM_HERE,
134 NewRunnableMethod(this, &OCSPRequestSession::CancelURLRequest));
135 }
136 } 132 }
137 133
138 bool Finished() const { 134 bool Finished() const {
139 AutoLock autolock(lock_); 135 AutoLock autolock(lock_);
140 return finished_; 136 return finished_;
141 } 137 }
142 138
143 bool Wait() { 139 bool Wait() {
144 base::TimeDelta timeout = timeout_; 140 base::TimeDelta timeout = timeout_;
145 AutoLock autolock(lock_); 141 AutoLock autolock(lock_);
146 while (!finished_) { 142 while (!finished_) {
147 base::TimeTicks last_time = base::TimeTicks::Now(); 143 base::TimeTicks last_time = base::TimeTicks::Now();
148 cv_.TimedWait(timeout); 144 cv_.TimedWait(timeout);
149 // Check elapsed time 145 // Check elapsed time
150 base::TimeDelta elapsed_time = base::TimeTicks::Now() - last_time; 146 base::TimeDelta elapsed_time = base::TimeTicks::Now() - last_time;
151 timeout -= elapsed_time; 147 timeout -= elapsed_time;
152 if (timeout < base::TimeDelta()) { 148 if (timeout < base::TimeDelta()) {
153 LOG(INFO) << "OCSP Timed out"; 149 LOG(INFO) << "OCSP Timed out";
154 if (!finished_) 150 if (!finished_)
155 Cancel(); 151 CancelLocked();
156 break; 152 break;
157 } 153 }
158 } 154 }
159 return finished_; 155 return finished_;
160 } 156 }
161 157
162 const GURL& url() const { 158 const GURL& url() const {
163 return url_; 159 return url_;
164 } 160 }
165 161
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 if (!request_->status().is_io_pending()) { 214 if (!request_->status().is_io_pending()) {
219 delete request_; 215 delete request_;
220 request_ = NULL; 216 request_ = NULL;
221 io_loop_->RemoveDestructionObserver(this); 217 io_loop_->RemoveDestructionObserver(this);
222 { 218 {
223 AutoLock autolock(lock_); 219 AutoLock autolock(lock_);
224 finished_ = true; 220 finished_ = true;
225 io_loop_ = NULL; 221 io_loop_ = NULL;
226 } 222 }
227 cv_.Signal(); 223 cv_.Signal();
224 Release(); // Balanced with StartURLRequest().
228 } 225 }
229 } 226 }
230 227
231 virtual void WillDestroyCurrentMessageLoop() { 228 virtual void WillDestroyCurrentMessageLoop() {
wtc 2009/12/08 01:44:01 It's a shame that the WillDestroyCurrentMessageLoo
232 DCHECK_EQ(MessageLoopForIO::current(), io_loop_); 229 DCHECK_EQ(MessageLoopForIO::current(), io_loop_);
233 { 230 {
234 AutoLock autolock(lock_); 231 AutoLock autolock(lock_);
235 io_loop_ = NULL; 232 io_loop_ = NULL;
236 } 233 }
237 if (request_) { 234 CancelURLRequest();
238 request_->Cancel();
239 delete request_;
240 request_ = NULL;
241 }
242 } 235 }
243 236
244 private: 237 private:
245 friend class base::RefCountedThreadSafe<OCSPRequestSession>; 238 friend class base::RefCountedThreadSafe<OCSPRequestSession>;
246 239
247 virtual ~OCSPRequestSession() { 240 virtual ~OCSPRequestSession() {
248 DCHECK(!request_); 241 DCHECK(!request_);
249 if (io_loop_) 242 if (io_loop_)
250 io_loop_->RemoveDestructionObserver(this); 243 io_loop_->RemoveDestructionObserver(this);
251 } 244 }
252 245
246 void CancelLocked() {
wtc 2009/12/08 01:44:01 Please add a comment to document that we must call
ukai 2009/12/08 06:02:05 Sure. Added lock_.AssertAcquired() too.
247 if (io_loop_) {
248 io_loop_->PostTask(
249 FROM_HERE,
250 NewRunnableMethod(this, &OCSPRequestSession::CancelURLRequest));
251 }
252 }
253
253 void StartURLRequest() { 254 void StartURLRequest() {
254 DCHECK_EQ(MessageLoopForIO::current(), io_loop_); 255 DCHECK_EQ(MessageLoopForIO::current(), io_loop_);
255 DCHECK(!request_); 256 DCHECK(!request_);
256 257
257 io_loop_->AddDestructionObserver(this); 258 io_loop_->AddDestructionObserver(this);
258 259
259 request_ = new URLRequest(url_, this); 260 request_ = new URLRequest(url_, this);
260 request_->set_context( 261 request_->set_context(
261 Singleton<OCSPInitSingleton>::get()->url_request_context()); 262 Singleton<OCSPInitSingleton>::get()->url_request_context());
262 // To meet the privacy requirements of off-the-record mode. 263 // To meet the privacy requirements of off-the-record mode.
263 request_->set_load_flags( 264 request_->set_load_flags(
264 net::LOAD_DISABLE_CACHE|net::LOAD_DO_NOT_SAVE_COOKIES); 265 net::LOAD_DISABLE_CACHE|net::LOAD_DO_NOT_SAVE_COOKIES);
265 266
266 if (http_request_method_ == "POST") { 267 if (http_request_method_ == "POST") {
267 DCHECK(!upload_content_.empty()); 268 DCHECK(!upload_content_.empty());
268 DCHECK(!upload_content_type_.empty()); 269 DCHECK(!upload_content_type_.empty());
269 270
270 request_->set_method("POST"); 271 request_->set_method("POST");
271 if (!extra_request_headers_.empty()) 272 if (!extra_request_headers_.empty())
272 extra_request_headers_ += "\r\n"; 273 extra_request_headers_ += "\r\n";
273 StringAppendF(&extra_request_headers_, 274 StringAppendF(&extra_request_headers_,
274 "Content-Type: %s", upload_content_type_.c_str()); 275 "Content-Type: %s", upload_content_type_.c_str());
275 request_->AppendBytesToUpload(upload_content_.data(), 276 request_->AppendBytesToUpload(upload_content_.data(),
276 static_cast<int>(upload_content_.size())); 277 static_cast<int>(upload_content_.size()));
277 } 278 }
278 if (!extra_request_headers_.empty()) 279 if (!extra_request_headers_.empty())
279 request_->SetExtraRequestHeaders(extra_request_headers_); 280 request_->SetExtraRequestHeaders(extra_request_headers_);
280 281
281 request_->Start(); 282 request_->Start();
283 AddRef(); // Release after |request_| deleted.
282 } 284 }
283 285
284 void CancelURLRequest() { 286 void CancelURLRequest() {
285 if (io_loop_) 287 if (io_loop_)
286 DCHECK_EQ(MessageLoopForIO::current(), io_loop_); 288 DCHECK_EQ(MessageLoopForIO::current(), io_loop_);
287 if (request_) { 289 if (request_) {
288 request_->Cancel(); 290 request_->Cancel();
289 delete request_; 291 delete request_;
290 request_ = NULL; 292 request_ = NULL;
293 {
294 AutoLock autolock(lock_);
295 finished_ = true;
296 io_loop_ = NULL;
297 }
298 cv_.Signal();
299 Release(); // Balanced with StartURLRequest().
291 } 300 }
292 } 301 }
293 302
294 GURL url_; // The URL we eventually wound up at 303 GURL url_; // The URL we eventually wound up at
295 std::string http_request_method_; 304 std::string http_request_method_;
296 base::TimeDelta timeout_; // The timeout for OCSP 305 base::TimeDelta timeout_; // The timeout for OCSP
297 URLRequest* request_; // The actual request this wraps 306 URLRequest* request_; // The actual request this wraps
298 scoped_refptr<net::IOBuffer> buffer_; // Read buffer 307 scoped_refptr<net::IOBuffer> buffer_; // Read buffer
299 std::string extra_request_headers_; // Extra headers for the request, if any 308 std::string extra_request_headers_; // Extra headers for the request, if any
300 std::string upload_content_; // HTTP POST payload 309 std::string upload_content_; // HTTP POST payload
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 // This function would be called before NSS initialization. 567 // This function would be called before NSS initialization.
559 void SetURLRequestContextForOCSP(URLRequestContext* request_context) { 568 void SetURLRequestContextForOCSP(URLRequestContext* request_context) {
560 OCSPInitSingleton::set_url_request_context(request_context); 569 OCSPInitSingleton::set_url_request_context(request_context);
561 } 570 }
562 571
563 URLRequestContext* GetURLRequestContextForOCSP() { 572 URLRequestContext* GetURLRequestContextForOCSP() {
564 return OCSPInitSingleton::url_request_context(); 573 return OCSPInitSingleton::url_request_context();
565 } 574 }
566 575
567 } // namespace net 576 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698