OLD | NEW |
---|---|
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 { | 68 { |
69 rtc::CritScope cs(&cs_); | 69 rtc::CritScope cs(&cs_); |
70 if (store_) { | 70 if (store_) { |
71 store_->PostGenerateIdentityResult_w(identity.Pass()); | 71 store_->PostGenerateIdentityResult_w(identity.Pass()); |
72 } | 72 } |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 void OnMessage(rtc::Message* msg) override { | 76 void OnMessage(rtc::Message* msg) override { |
77 switch (msg->message_id) { | 77 switch (msg->message_id) { |
78 case MSG_GENERATE_IDENTITY: | 78 case MSG_GENERATE_IDENTITY: |
hbos
2015/06/09 12:59:12
You could add a DCHECK here that we are on the wor
tommi
2015/06/09 13:17:17
I thought about that but decided to not add anothe
| |
79 GenerateIdentity(); | 79 GenerateIdentity(); |
80 | 80 |
81 // Must delete |this|, owned by msg->pdata, on the signaling thread to | 81 // Must delete |this|, owned by msg->pdata, on the signaling thread to |
82 // avoid races on disconnecting the signal. | 82 // avoid races on disconnecting the signal. |
83 signaling_thread_->Post(this, MSG_DESTROY, msg->pdata); | 83 signaling_thread_->Post(this, MSG_DESTROY, msg->pdata); |
84 break; | 84 break; |
85 case MSG_DESTROY: | 85 case MSG_DESTROY: |
hbos
2015/06/09 12:59:11
And here that we are on the signaling thread.
tommi
2015/06/09 13:17:18
Done. There also is one in the dtor.
| |
86 delete msg->pdata; | 86 delete msg->pdata; |
hbos
2015/06/09 12:59:11
Add a comment that this line will delete |this| to
tommi
2015/06/09 13:17:18
Done
| |
87 break; | 87 break; |
88 default: | 88 default: |
89 CHECK(false) << "Unexpected message type"; | 89 CHECK(false) << "Unexpected message type"; |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 private: | 93 private: |
94 void OnStoreDestroyed() { | 94 void OnStoreDestroyed() { |
95 rtc::CritScope cs(&cs_); | 95 rtc::CritScope cs(&cs_); |
96 store_ = NULL; | 96 store_ = NULL; |
(...skipping 12 matching lines...) Expand all Loading... | |
109 rtc::Thread* worker_thread) | 109 rtc::Thread* worker_thread) |
110 : signaling_thread_(signaling_thread), | 110 : signaling_thread_(signaling_thread), |
111 worker_thread_(worker_thread), | 111 worker_thread_(worker_thread), |
112 pending_jobs_(0) {} | 112 pending_jobs_(0) {} |
113 | 113 |
114 DtlsIdentityStore::~DtlsIdentityStore() { | 114 DtlsIdentityStore::~DtlsIdentityStore() { |
115 SignalDestroyed(); | 115 SignalDestroyed(); |
116 } | 116 } |
117 | 117 |
118 void DtlsIdentityStore::Initialize() { | 118 void DtlsIdentityStore::Initialize() { |
119 DCHECK(rtc::Thread::Current() == signaling_thread_); | |
119 // Do not aggressively generate the free identity if the worker thread and the | 120 // Do not aggressively generate the free identity if the worker thread and the |
120 // signaling thread are the same. | 121 // signaling thread are the same. |
121 if (worker_thread_ != signaling_thread_) { | 122 if (worker_thread_ != signaling_thread_) { |
122 GenerateIdentity(); | 123 GenerateIdentity(); |
123 } | 124 } |
124 } | 125 } |
125 | 126 |
126 void DtlsIdentityStore::RequestIdentity(DTLSIdentityRequestObserver* observer) { | 127 void DtlsIdentityStore::RequestIdentity(DTLSIdentityRequestObserver* observer) { |
127 DCHECK(rtc::Thread::Current() == signaling_thread_); | 128 DCHECK(rtc::Thread::Current() == signaling_thread_); |
128 DCHECK(observer); | 129 DCHECK(observer); |
129 | 130 |
130 // Must return the free identity async. | 131 // Must return the free identity async. |
131 if (free_identity_.get()) { | 132 if (free_identity_.get()) { |
132 IdentityResultMessageData* msg = | 133 IdentityResultMessageData* msg = |
133 new IdentityResultMessageData(free_identity_.release()); | 134 new IdentityResultMessageData(free_identity_.release()); |
134 signaling_thread_->Post(this, MSG_RETURN_FREE_IDENTITY, msg); | 135 signaling_thread_->Post(this, MSG_RETURN_FREE_IDENTITY, msg); |
135 } | 136 } |
136 | 137 |
137 pending_observers_.push(observer); | 138 pending_observers_.push(observer); |
138 GenerateIdentity(); | 139 GenerateIdentity(); |
139 } | 140 } |
140 | 141 |
141 void DtlsIdentityStore::OnMessage(rtc::Message* msg) { | 142 void DtlsIdentityStore::OnMessage(rtc::Message* msg) { |
hbos
2015/06/09 12:59:11
And here that we are on the signaling thread for b
tommi
2015/06/09 13:17:18
Done.
| |
142 switch (msg->message_id) { | 143 switch (msg->message_id) { |
143 case MSG_GENERATE_IDENTITY_RESULT: { | 144 case MSG_GENERATE_IDENTITY_RESULT: { |
144 rtc::scoped_ptr<IdentityResultMessageData> pdata( | 145 rtc::scoped_ptr<IdentityResultMessageData> pdata( |
145 static_cast<IdentityResultMessageData*>(msg->pdata)); | 146 static_cast<IdentityResultMessageData*>(msg->pdata)); |
146 OnIdentityGenerated(pdata->data().Pass()); | 147 OnIdentityGenerated(pdata->data().Pass()); |
147 break; | 148 break; |
148 } | 149 } |
149 case MSG_RETURN_FREE_IDENTITY: { | 150 case MSG_RETURN_FREE_IDENTITY: { |
150 rtc::scoped_ptr<IdentityResultMessageData> pdata( | 151 rtc::scoped_ptr<IdentityResultMessageData> pdata( |
151 static_cast<IdentityResultMessageData*>(msg->pdata)); | 152 static_cast<IdentityResultMessageData*>(msg->pdata)); |
152 ReturnIdentity(pdata->data().Pass()); | 153 ReturnIdentity(pdata->data().Pass()); |
153 break; | 154 break; |
154 } | 155 } |
155 } | 156 } |
156 } | 157 } |
157 | 158 |
158 bool DtlsIdentityStore::HasFreeIdentityForTesting() const { | 159 bool DtlsIdentityStore::HasFreeIdentityForTesting() const { |
159 return free_identity_.get(); | 160 DCHECK(rtc::Thread::Current() == signaling_thread_); |
161 return free_identity_.get() != nullptr; | |
160 } | 162 } |
161 | 163 |
162 void DtlsIdentityStore::GenerateIdentity() { | 164 void DtlsIdentityStore::GenerateIdentity() { |
hbos
2015/06/09 12:59:11
It's confusing that we have two GenerateIdentity i
tommi
2015/06/09 13:17:17
I added the _w() post fix to the one that runs on
| |
165 DCHECK(rtc::Thread::Current() == signaling_thread_); | |
163 pending_jobs_++; | 166 pending_jobs_++; |
164 LOG(LS_VERBOSE) << "New DTLS identity generation is posted, " | 167 LOG(LS_VERBOSE) << "New DTLS identity generation is posted, " |
165 << "pending_identities=" << pending_jobs_; | 168 << "pending_identities=" << pending_jobs_; |
166 | 169 |
167 WorkerTask* task = new WorkerTask(this); | 170 WorkerTask* task = new WorkerTask(this); |
168 // The WorkerTask is owned by the message data to make sure it will not be | 171 // The WorkerTask is owned by the message data to make sure it will not be |
169 // leaked even if the task does not get run. | 172 // leaked even if the task does not get run. |
170 IdentityTaskMessageData* msg = new IdentityTaskMessageData(task); | 173 IdentityTaskMessageData* msg = new IdentityTaskMessageData(task); |
171 worker_thread_->Post(task, MSG_GENERATE_IDENTITY, msg); | 174 worker_thread_->Post(task, MSG_GENERATE_IDENTITY, msg); |
172 } | 175 } |
(...skipping 11 matching lines...) Expand all Loading... | |
184 free_identity_.reset(identity.release()); | 187 free_identity_.reset(identity.release()); |
185 LOG(LS_VERBOSE) << "A free DTLS identity is saved"; | 188 LOG(LS_VERBOSE) << "A free DTLS identity is saved"; |
186 } | 189 } |
187 return; | 190 return; |
188 } | 191 } |
189 ReturnIdentity(identity.Pass()); | 192 ReturnIdentity(identity.Pass()); |
190 } | 193 } |
191 | 194 |
192 void DtlsIdentityStore::ReturnIdentity( | 195 void DtlsIdentityStore::ReturnIdentity( |
193 rtc::scoped_ptr<rtc::SSLIdentity> identity) { | 196 rtc::scoped_ptr<rtc::SSLIdentity> identity) { |
197 DCHECK(rtc::Thread::Current() == signaling_thread_); | |
194 DCHECK(!free_identity_.get()); | 198 DCHECK(!free_identity_.get()); |
195 DCHECK(!pending_observers_.empty()); | 199 DCHECK(!pending_observers_.empty()); |
196 | 200 |
197 rtc::scoped_refptr<DTLSIdentityRequestObserver> observer = | 201 rtc::scoped_refptr<DTLSIdentityRequestObserver> observer = |
198 pending_observers_.front(); | 202 pending_observers_.front(); |
199 pending_observers_.pop(); | 203 pending_observers_.pop(); |
200 | 204 |
201 if (identity.get()) { | 205 if (identity.get()) { |
202 observer->OnSuccessWithIdentityObj(identity.Pass()); | 206 observer->OnSuccessWithIdentityObj(identity.Pass()); |
203 } else { | 207 } else { |
204 // Pass an arbitrary error code. | 208 // Pass an arbitrary error code. |
205 observer->OnFailure(0); | 209 observer->OnFailure(0); |
206 LOG(LS_WARNING) << "Failed to generate SSL identity"; | 210 LOG(LS_WARNING) << "Failed to generate SSL identity"; |
207 } | 211 } |
208 | 212 |
209 // Do not aggressively generate the free identity if the worker thread and the | 213 // Do not aggressively generate the free identity if the worker thread and the |
210 // signaling thread are the same. | 214 // signaling thread are the same. |
211 if (worker_thread_ != signaling_thread_ && | 215 if (worker_thread_ != signaling_thread_ && |
212 pending_observers_.empty() && | 216 pending_observers_.empty() && |
213 pending_jobs_ == 0) { | 217 pending_jobs_ == 0) { |
214 // Generate a free identity in the background. | 218 // Generate a free identity in the background. |
215 GenerateIdentity(); | 219 GenerateIdentity(); |
hbos
2015/06/09 12:59:12
(This generates in case we want to request another
tommi
2015/06/09 13:17:17
yes. I guess that's what the "free identity" refe
| |
216 } | 220 } |
217 } | 221 } |
218 | 222 |
219 void DtlsIdentityStore::PostGenerateIdentityResult_w( | 223 void DtlsIdentityStore::PostGenerateIdentityResult_w( |
220 rtc::scoped_ptr<rtc::SSLIdentity> identity) { | 224 rtc::scoped_ptr<rtc::SSLIdentity> identity) { |
221 DCHECK(rtc::Thread::Current() == worker_thread_); | 225 DCHECK(rtc::Thread::Current() == worker_thread_); |
222 | 226 |
223 IdentityResultMessageData* msg = | 227 IdentityResultMessageData* msg = |
224 new IdentityResultMessageData(identity.release()); | 228 new IdentityResultMessageData(identity.release()); |
225 signaling_thread_->Post(this, MSG_GENERATE_IDENTITY_RESULT, msg); | 229 signaling_thread_->Post(this, MSG_GENERATE_IDENTITY_RESULT, msg); |
226 } | 230 } |
227 } // namespace webrtc | 231 } // namespace webrtc |
OLD | NEW |