1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/media/peer_connection_identity_store.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "content/renderer/media/webrtc_identity_service.h"
10 #include "content/renderer/render_thread_impl.h"
14 // Bridges identity requests between the main render thread and libjingle's
16 class RequestHandler
: public base::RefCountedThreadSafe
<RequestHandler
> {
18 explicit RequestHandler(webrtc::DtlsIdentityRequestObserver
* observer
)
19 : signaling_thread_(base::ThreadTaskRunnerHandle::Get()),
20 observer_(observer
) {}
22 void RequestIdentityOnUIThread(const GURL
& url
,
23 const GURL
& first_party_for_cookies
) {
25 RenderThreadImpl::current()
26 ->get_webrtc_identity_service()
28 url
, first_party_for_cookies
, "WebRTC", "WebRTC",
29 base::Bind(&RequestHandler::OnIdentityReady
, this),
30 base::Bind(&RequestHandler::OnRequestFailed
, this));
31 DCHECK_NE(request_id
, 0);
35 friend class base::RefCountedThreadSafe
<RequestHandler
>;
37 DCHECK(!observer_
.get());
41 const std::string
& certificate
,
42 const std::string
& private_key
) {
43 signaling_thread_
->PostTask(FROM_HERE
,
44 base::Bind(static_cast<void (webrtc::DtlsIdentityRequestObserver::*)(
45 const std::string
&, const std::string
&)>(
46 &webrtc::DtlsIdentityRequestObserver::OnSuccess
),
47 observer_
, certificate
, private_key
));
48 signaling_thread_
->PostTask(FROM_HERE
,
49 base::Bind(&RequestHandler::EnsureReleaseObserverOnSignalingThread
,
53 void OnRequestFailed(int error
) {
54 signaling_thread_
->PostTask(FROM_HERE
,
55 base::Bind(&webrtc::DtlsIdentityRequestObserver::OnFailure
, observer_
,
57 signaling_thread_
->PostTask(FROM_HERE
,
58 base::Bind(&RequestHandler::EnsureReleaseObserverOnSignalingThread
,
62 void EnsureReleaseObserverOnSignalingThread() {
63 DCHECK(signaling_thread_
->BelongsToCurrentThread());
67 const scoped_refptr
<base::SingleThreadTaskRunner
> signaling_thread_
;
68 scoped_refptr
<webrtc::DtlsIdentityRequestObserver
> observer_
;
72 PeerConnectionIdentityStore::PeerConnectionIdentityStore(
74 const GURL
& first_party_for_cookies
)
75 : main_thread_(base::ThreadTaskRunnerHandle::Get()),
77 first_party_for_cookies_(first_party_for_cookies
) {
78 signaling_thread_
.DetachFromThread();
79 DCHECK(main_thread_
.get());
82 PeerConnectionIdentityStore::~PeerConnectionIdentityStore() {
83 // Typically destructed on libjingle's signaling thread.
86 void PeerConnectionIdentityStore::RequestIdentity(
87 rtc::KeyType key_type
,
88 const rtc::scoped_refptr
<webrtc::DtlsIdentityRequestObserver
>& observer
) {
89 DCHECK(signaling_thread_
.CalledOnValidThread());
91 // This store only supports RSA.
92 DCHECK_EQ(key_type
, rtc::KT_RSA
);
94 scoped_refptr
<RequestHandler
> handler(new RequestHandler(observer
));
95 main_thread_
->PostTask(
97 base::Bind(&RequestHandler::RequestIdentityOnUIThread
, handler
, url_
,
98 first_party_for_cookies_
));
101 } // namespace content