jiayl@webrtc.org | 2548406 | 2015-02-18 23:58:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2015 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 27 | |
| 28 | #ifndef TALK_APP_WEBRTC_DTLSIDENTITYSTORE_H_ |
| 29 | #define TALK_APP_WEBRTC_DTLSIDENTITYSTORE_H_ |
| 30 | |
| 31 | #include <queue> |
| 32 | #include <string> |
| 33 | |
| 34 | #include "talk/app/webrtc/peerconnectioninterface.h" |
| 35 | #include "webrtc/base/messagehandler.h" |
jiayl@webrtc.org | d83f4ef | 2015-03-13 21:26:12 +0000 | [diff] [blame^] | 36 | #include "webrtc/base/messagequeue.h" |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 37 | #include "webrtc/base/scoped_ptr.h" |
| 38 | #include "webrtc/base/scoped_ref_ptr.h" |
| 39 | |
| 40 | namespace webrtc { |
| 41 | class DTLSIdentityRequestObserver; |
| 42 | class SSLIdentity; |
| 43 | class Thread; |
| 44 | |
| 45 | // This class implements an in-memory DTLS identity store, which generates the |
| 46 | // DTLS identity on the worker thread. |
| 47 | // APIs calls must be made on the signaling thread and the callbacks are also |
| 48 | // called on the signaling thread. |
| 49 | class DtlsIdentityStore : public rtc::MessageHandler { |
| 50 | public: |
| 51 | static const char kIdentityName[]; |
| 52 | |
| 53 | DtlsIdentityStore(rtc::Thread* signaling_thread, |
| 54 | rtc::Thread* worker_thread); |
jiayl@webrtc.org | d83f4ef | 2015-03-13 21:26:12 +0000 | [diff] [blame^] | 55 | virtual ~DtlsIdentityStore(); |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 56 | |
| 57 | // Initialize will start generating the free identity in the background. |
| 58 | void Initialize(); |
| 59 | |
| 60 | // The |observer| will be called when the requested identity is ready, or when |
| 61 | // identity generation fails. |
| 62 | void RequestIdentity(webrtc::DTLSIdentityRequestObserver* observer); |
| 63 | |
| 64 | // rtc::MessageHandler override; |
| 65 | void OnMessage(rtc::Message* msg) override; |
| 66 | |
| 67 | // Returns true if there is a free identity, used for unit tests. |
| 68 | bool HasFreeIdentityForTesting() const; |
| 69 | |
| 70 | private: |
jiayl@webrtc.org | d83f4ef | 2015-03-13 21:26:12 +0000 | [diff] [blame^] | 71 | sigslot::signal0<sigslot::multi_threaded_local> SignalDestroyed; |
| 72 | class WorkerTask; |
| 73 | typedef rtc::ScopedMessageData<DtlsIdentityStore::WorkerTask> |
| 74 | IdentityTaskMessageData; |
| 75 | |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 76 | void GenerateIdentity(); |
| 77 | void OnIdentityGenerated(rtc::scoped_ptr<rtc::SSLIdentity> identity); |
| 78 | void ReturnIdentity(rtc::scoped_ptr<rtc::SSLIdentity> identity); |
| 79 | |
jiayl@webrtc.org | d83f4ef | 2015-03-13 21:26:12 +0000 | [diff] [blame^] | 80 | void PostGenerateIdentityResult_w(rtc::scoped_ptr<rtc::SSLIdentity> identity); |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 81 | |
| 82 | rtc::Thread* signaling_thread_; |
| 83 | rtc::Thread* worker_thread_; |
| 84 | |
| 85 | // These members should be accessed on the signaling thread only. |
| 86 | int pending_jobs_; |
| 87 | rtc::scoped_ptr<rtc::SSLIdentity> free_identity_; |
| 88 | typedef std::queue<rtc::scoped_refptr<webrtc::DTLSIdentityRequestObserver>> |
| 89 | ObserverList; |
| 90 | ObserverList pending_observers_; |
| 91 | }; |
| 92 | |
| 93 | } // namespace webrtc |
| 94 | |
| 95 | #endif // TALK_APP_WEBRTC_DTLSIDENTITYSTORE_H_ |