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 origin_loop_->AddDestructionObserver(this); |
36 } | 39 } |
37 | 40 |
38 void DoVerify() { | 41 void DoVerify() { |
39 // Running on the worker thread | 42 // Running on the worker thread |
40 error_ = cert_->Verify(hostname_, flags_, &result_); | 43 error_ = cert_->Verify(hostname_, flags_, &result_); |
41 #if defined(USE_NSS) | 44 #if defined(USE_NSS) |
42 // Detach the thread from NSPR. | 45 // Detach the thread from NSPR. |
43 // Calling NSS functions attaches the thread to NSPR, which stores | 46 // Calling NSS functions attaches the thread to NSPR, which stores |
44 // the NSPR thread ID in thread-specific data. | 47 // the NSPR thread ID in thread-specific data. |
45 // The threads in our thread pool terminate after we have called | 48 // The threads in our thread pool terminate after we have called |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 // Drop the verifier's reference to us. Do this before running the | 80 // Drop the verifier's reference to us. Do this before running the |
78 // callback since the callback might result in the verifier being | 81 // callback since the callback might result in the verifier being |
79 // destroyed. | 82 // destroyed. |
80 verifier_->request_ = NULL; | 83 verifier_->request_ = NULL; |
81 | 84 |
82 callback_->Run(error_); | 85 callback_->Run(error_); |
83 } | 86 } |
84 | 87 |
85 void Cancel() { | 88 void Cancel() { |
86 verifier_ = NULL; | 89 verifier_ = NULL; |
| 90 AutoLock locked(origin_loop_lock_); |
| 91 if (origin_loop_) { |
| 92 origin_loop_->RemoveDestructionObserver(this); |
| 93 origin_loop_ = NULL; |
| 94 } |
| 95 } |
87 | 96 |
| 97 // MessageLoop::DestructionObserver override. |
| 98 virtual void WillDestroyCurrentMessageLoop() { |
| 99 LOG(ERROR) << "CertVerifier wasn't deleted before the thread was deleted."; |
88 AutoLock locked(origin_loop_lock_); | 100 AutoLock locked(origin_loop_lock_); |
89 origin_loop_ = NULL; | 101 origin_loop_ = NULL; |
90 } | 102 } |
91 | 103 |
92 private: | 104 private: |
93 friend class base::RefCountedThreadSafe<CertVerifier::Request>; | 105 friend class base::RefCountedThreadSafe<CertVerifier::Request>; |
| 106 friend class DeleteTask<CertVerifier::Request>; |
| 107 friend struct CertVerifier::RequestTraits; |
94 | 108 |
95 ~Request() {} | 109 ~Request() { |
| 110 Cancel(); |
| 111 } |
| 112 |
| 113 // If we have an origin_loop_, the observer needs to be removed on that |
| 114 // thread. OnDestruct is called by RequestTraits to ensure this |
| 115 // happens. |
| 116 void OnDestruct() const { |
| 117 { |
| 118 AutoLock locked(origin_loop_lock_); |
| 119 if (origin_loop_ && origin_loop_ != MessageLoop::current()) { |
| 120 origin_loop_->DeleteSoon(FROM_HERE, this); |
| 121 return; |
| 122 } |
| 123 } |
| 124 delete this; |
| 125 } |
96 | 126 |
97 // Set on the origin thread, read on the worker thread. | 127 // Set on the origin thread, read on the worker thread. |
98 scoped_refptr<X509Certificate> cert_; | 128 scoped_refptr<X509Certificate> cert_; |
99 std::string hostname_; | 129 std::string hostname_; |
100 // bitwise OR'd of X509Certificate::VerifyFlags. | 130 // bitwise OR'd of X509Certificate::VerifyFlags. |
101 int flags_; | 131 int flags_; |
102 | 132 |
103 // Only used on the origin thread (where Verify was called). | 133 // Only used on the origin thread (where Verify was called). |
104 CertVerifier* verifier_; | 134 CertVerifier* verifier_; |
105 CertVerifyResult* verify_result_; | 135 CertVerifyResult* verify_result_; |
106 CompletionCallback* callback_; | 136 CompletionCallback* callback_; |
107 | 137 |
108 // Used to post ourselves onto the origin thread. | 138 // Used to post ourselves onto the origin thread. |
109 Lock origin_loop_lock_; | 139 mutable Lock origin_loop_lock_; |
110 MessageLoop* origin_loop_; | 140 MessageLoop* origin_loop_; |
111 | 141 |
112 // Assigned on the worker thread, read on the origin thread. | 142 // Assigned on the worker thread, read on the origin thread. |
113 int error_; | 143 int error_; |
114 CertVerifyResult result_; | 144 CertVerifyResult result_; |
115 }; | 145 }; |
116 | 146 |
| 147 struct CertVerifier::RequestTraits { |
| 148 static void Destruct(const CertVerifier::Request* request) { |
| 149 request->OnDestruct(); |
| 150 } |
| 151 }; |
| 152 |
117 //----------------------------------------------------------------------------- | 153 //----------------------------------------------------------------------------- |
118 | 154 |
119 CertVerifier::CertVerifier() { | 155 CertVerifier::CertVerifier() { |
120 } | 156 } |
121 | 157 |
122 CertVerifier::~CertVerifier() { | 158 CertVerifier::~CertVerifier() { |
123 if (request_) | 159 if (request_) |
124 request_->Cancel(); | 160 request_->Cancel(); |
125 } | 161 } |
126 | 162 |
(...skipping 19 matching lines...) Expand all Loading... |
146 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) { | 182 NewRunnableMethod(request_.get(), &Request::DoVerify), true)) { |
147 NOTREACHED(); | 183 NOTREACHED(); |
148 request_ = NULL; | 184 request_ = NULL; |
149 return ERR_FAILED; | 185 return ERR_FAILED; |
150 } | 186 } |
151 | 187 |
152 return ERR_IO_PENDING; | 188 return ERR_IO_PENDING; |
153 } | 189 } |
154 | 190 |
155 } // namespace net | 191 } // namespace net |
OLD | NEW |