blob: ffe8067aa3d05ee8b904e15225f6c81835066bbf [file] [log] [blame]
jiayl@webrtc.org25484062015-02-18 23:58:16 +00001/*
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.org61e00b02015-03-04 22:17:38 +000027
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.orgd83f4ef2015-03-13 21:26:12 +000036#include "webrtc/base/messagequeue.h"
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000037#include "webrtc/base/scoped_ptr.h"
38#include "webrtc/base/scoped_ref_ptr.h"
39
40namespace webrtc {
41class DTLSIdentityRequestObserver;
42class SSLIdentity;
43class 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.
49class 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.orgd83f4ef2015-03-13 21:26:12 +000055 virtual ~DtlsIdentityStore();
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000056
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.orgd83f4ef2015-03-13 21:26:12 +000071 sigslot::signal0<sigslot::multi_threaded_local> SignalDestroyed;
72 class WorkerTask;
73 typedef rtc::ScopedMessageData<DtlsIdentityStore::WorkerTask>
74 IdentityTaskMessageData;
75
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000076 void GenerateIdentity();
77 void OnIdentityGenerated(rtc::scoped_ptr<rtc::SSLIdentity> identity);
78 void ReturnIdentity(rtc::scoped_ptr<rtc::SSLIdentity> identity);
79
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +000080 void PostGenerateIdentityResult_w(rtc::scoped_ptr<rtc::SSLIdentity> identity);
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000081
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_