Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/base/cert_verifier.h" | 5 #include "net/base/cert_verifier.h" |
| 6 | 6 |
| 7 #if defined(USE_NSS) | 7 #if defined(USE_NSS) |
| 8 #include <private/pprthred.h> // PR_DetatchThread | 8 #include <private/pprthred.h> // PR_DetatchThread |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/worker_pool.h" | 12 #include "base/worker_pool.h" |
| 13 #include "net/base/cert_verify_result.h" | 13 #include "net/base/cert_verify_result.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/base/x509_certificate.h" | 15 #include "net/base/x509_certificate.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 class CertVerifier::Request : | 19 class CertVerifier::Request : |
| 20 public base::RefCountedThreadSafe<CertVerifier::Request> { | 20 public base::RefCountedThreadSafe<CertVerifier::Request, |
| 21 CertVerifier::RequestTraits>, | |
| 22 public MessageLoop::DestructionObserver { | |
| 21 public: | 23 public: |
| 22 Request(CertVerifier* verifier, | 24 Request(CertVerifier* verifier, |
| 23 X509Certificate* cert, | 25 X509Certificate* cert, |
| 24 const std::string& hostname, | 26 const std::string& hostname, |
| 25 int flags, | 27 int flags, |
| 26 CertVerifyResult* verify_result, | 28 CertVerifyResult* verify_result, |
| 27 CompletionCallback* callback) | 29 CompletionCallback* callback) |
| 28 : cert_(cert), | 30 : cert_(cert), |
| 29 hostname_(hostname), | 31 hostname_(hostname), |
| 30 flags_(flags), | 32 flags_(flags), |
| 31 verifier_(verifier), | 33 verifier_(verifier), |
| 32 verify_result_(verify_result), | 34 verify_result_(verify_result), |
| 33 callback_(callback), | 35 callback_(callback), |
| 34 origin_loop_(MessageLoop::current()), | 36 origin_loop_(MessageLoop::current()), |
| 35 error_(OK) { | 37 error_(OK) { |
| 38 if (origin_loop_) | |
| 39 origin_loop_->AddDestructionObserver(this); | |
| 36 } | 40 } |
| 37 | 41 |
| 38 void DoVerify() { | 42 void DoVerify() { |
| 39 // Running on the worker thread | 43 // Running on the worker thread |
| 40 error_ = cert_->Verify(hostname_, flags_, &result_); | 44 error_ = cert_->Verify(hostname_, flags_, &result_); |
| 41 #if defined(USE_NSS) | 45 #if defined(USE_NSS) |
| 42 // Detach the thread from NSPR. | 46 // Detach the thread from NSPR. |
| 43 // Calling NSS functions attaches the thread to NSPR, which stores | 47 // Calling NSS functions attaches the thread to NSPR, which stores |
| 44 // the NSPR thread ID in thread-specific data. | 48 // the NSPR thread ID in thread-specific data. |
| 45 // The threads in our thread pool terminate after we have called | 49 // The threads in our thread pool terminate after we have called |
| 46 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 50 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
| 47 // segfaults on shutdown when the threads' thread-specific data | 51 // segfaults on shutdown when the threads' thread-specific data |
| 48 // destructors run. | 52 // destructors run. |
| 49 PR_DetachThread(); | 53 PR_DetachThread(); |
| 50 #endif | 54 #endif |
| 51 | 55 |
| 52 Task* reply = NewRunnableMethod(this, &Request::DoCallback); | 56 Task* reply = NewRunnableMethod(this, &Request::DoCallback); |
| 53 | 57 |
| 54 // The origin loop could go away while we are trying to post to it, so we | 58 // The origin loop could go away while we are trying to post to it, so we |
| 55 // need to call its PostTask method inside a lock. See ~CertVerifier. | 59 // need to call its PostTask method inside a lock. See ~CertVerifier. |
| 56 { | 60 { |
| 57 AutoLock locked(origin_loop_lock_); | 61 AutoLock locked(origin_loop_lock_); |
| 58 if (origin_loop_) { | 62 if (origin_loop_ && verifier_) { |
|
wtc
2010/11/10 22:18:20
BUG: Is this change necessary?
DoCallback will re
Zachary Kuznia
2010/11/11 10:35:31
I was trying to avoid a DCHECK if Cancel was calle
| |
| 59 origin_loop_->PostTask(FROM_HERE, reply); | 63 origin_loop_->PostTask(FROM_HERE, reply); |
| 60 reply = NULL; | 64 reply = NULL; |
| 61 } | 65 } |
| 62 } | 66 } |
| 63 | 67 |
| 64 // Does nothing if it got posted. | 68 // Does nothing if it got posted. |
| 65 delete reply; | 69 delete reply; |
| 66 } | 70 } |
| 67 | 71 |
| 68 void DoCallback() { | 72 void DoCallback() { |
| 69 // Running on the origin thread. | 73 // Running on the origin thread. |
| 70 | 74 |
| 71 // We may have been cancelled! | 75 // We may have been cancelled! |
| 72 if (!verifier_) | 76 if (!verifier_) |
| 73 return; | 77 return; |
| 74 | 78 |
| 75 *verify_result_ = result_; | 79 *verify_result_ = result_; |
| 76 | 80 |
| 77 // Drop the verifier's reference to us. Do this before running the | 81 // Drop the verifier's reference to us. Do this before running the |
| 78 // callback since the callback might result in the verifier being | 82 // callback since the callback might result in the verifier being |
| 79 // destroyed. | 83 // destroyed. |
| 80 verifier_->request_ = NULL; | 84 verifier_->request_ = NULL; |
| 81 | 85 |
| 82 callback_->Run(error_); | 86 callback_->Run(error_); |
| 83 } | 87 } |
| 84 | 88 |
| 85 void Cancel() { | 89 void Cancel() { |
| 86 verifier_ = NULL; | 90 verifier_ = NULL; |
| 91 } | |
| 87 | 92 |
| 93 // MessageLoop::DestructionObserver override. | |
| 94 virtual void WillDestroyCurrentMessageLoop() { | |
| 95 LOG(ERROR) << "CertVerifier wasn't deleted before the thread was deleted."; | |
| 88 AutoLock locked(origin_loop_lock_); | 96 AutoLock locked(origin_loop_lock_); |
| 89 origin_loop_ = NULL; | 97 origin_loop_ = NULL; |
| 90 } | 98 } |
| 91 | 99 |
| 92 private: | 100 private: |
| 93 friend class base::RefCountedThreadSafe<CertVerifier::Request>; | 101 friend class base::RefCountedThreadSafe<CertVerifier::Request>; |
| 102 friend class DeleteTask<CertVerifier::Request>; | |
| 103 friend struct CertVerifier::RequestTraits; | |
| 94 | 104 |
| 95 ~Request() {} | 105 ~Request() { |
|
wtc
2010/11/10 22:18:20
Please document that if origin_loop_ is not NULL,
Zachary Kuznia
2010/11/11 10:35:31
That's correct. Added a comment to OnDestruct.
| |
| 106 AutoLock locked(origin_loop_lock_); | |
| 107 if (origin_loop_) { | |
|
wtc
2010/11/10 22:18:20
Nit: omit the curly braces.
Zachary Kuznia
2010/11/11 10:35:31
Moved back to Cancel().
| |
| 108 origin_loop_->RemoveDestructionObserver(this); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void OnDestruct() const { | |
|
willchan no longer on Chromium
2010/11/10 22:33:17
I am concerned about this. DeleteSoon() will post
Zachary Kuznia
2010/11/11 10:35:31
Added the lock, and changed the if() so that it's
| |
| 113 if (origin_loop_) { | |
| 114 origin_loop_->DeleteSoon(FROM_HERE, this); | |
| 115 } else { | |
| 116 delete this; | |
| 117 } | |
| 118 } | |
| 96 | 119 |
| 97 // Set on the origin thread, read on the worker thread. | 120 // Set on the origin thread, read on the worker thread. |
| 98 scoped_refptr<X509Certificate> cert_; | 121 scoped_refptr<X509Certificate> cert_; |
| 99 std::string hostname_; | 122 std::string hostname_; |
| 100 // bitwise OR'd of X509Certificate::VerifyFlags. | 123 // bitwise OR'd of X509Certificate::VerifyFlags. |
| 101 int flags_; | 124 int flags_; |
| 102 | 125 |
| 103 // Only used on the origin thread (where Verify was called). | 126 // Only used on the origin thread (where Verify was called). |
| 104 CertVerifier* verifier_; | 127 CertVerifier* verifier_; |
| 105 CertVerifyResult* verify_result_; | 128 CertVerifyResult* verify_result_; |
| 106 CompletionCallback* callback_; | 129 CompletionCallback* callback_; |
| 107 | 130 |
| 108 // Used to post ourselves onto the origin thread. | 131 // Used to post ourselves onto the origin thread. |
| 109 Lock origin_loop_lock_; | 132 Lock origin_loop_lock_; |
| 110 MessageLoop* origin_loop_; | 133 MessageLoop* origin_loop_; |
| 111 | 134 |
| 112 // Assigned on the worker thread, read on the origin thread. | 135 // Assigned on the worker thread, read on the origin thread. |
| 113 int error_; | 136 int error_; |
| 114 CertVerifyResult result_; | 137 CertVerifyResult result_; |
| 115 }; | 138 }; |
| 116 | 139 |
| 140 struct CertVerifier::RequestTraits { | |
| 141 static void Destruct(const CertVerifier::Request* request) { | |
| 142 request->OnDestruct(); | |
| 143 } | |
| 144 }; | |
| 145 | |
| 117 //----------------------------------------------------------------------------- | 146 //----------------------------------------------------------------------------- |
| 118 | 147 |
| 119 CertVerifier::CertVerifier() { | 148 CertVerifier::CertVerifier() { |
| 120 } | 149 } |
| 121 | 150 |
| 122 CertVerifier::~CertVerifier() { | 151 CertVerifier::~CertVerifier() { |
| 123 if (request_) | 152 if (request_) |
| 124 request_->Cancel(); | 153 request_->Cancel(); |
| 125 } | 154 } |
| 126 | 155 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 146 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) { | 175 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) { |
| 147 NOTREACHED(); | 176 NOTREACHED(); |
| 148 request_ = NULL; | 177 request_ = NULL; |
| 149 return ERR_FAILED; | 178 return ERR_FAILED; |
| 150 } | 179 } |
| 151 | 180 |
| 152 return ERR_IO_PENDING; | 181 return ERR_IO_PENDING; |
| 153 } | 182 } |
| 154 | 183 |
| 155 } // namespace net | 184 } // namespace net |
| OLD | NEW |