1 // Copyright 2013 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_service.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 RequestHandler(const GURL
& origin
,
19 webrtc::DTLSIdentityRequestObserver
* observer
,
20 const std::string
& identity_name
,
21 const std::string
& common_name
)
22 : signaling_thread_(base::ThreadTaskRunnerHandle::Get()),
23 observer_(observer
), origin_(origin
), identity_name_(identity_name
),
24 common_name_(common_name
) {}
26 void RequestIdentityOnUIThread() {
27 int request_id
= RenderThreadImpl::current()->get_webrtc_identity_service()
28 ->RequestIdentity(origin_
, identity_name_
, common_name_
,
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(&webrtc::DTLSIdentityRequestObserver::OnSuccess
, observer_
,
45 certificate
, private_key
));
46 signaling_thread_
->PostTask(FROM_HERE
,
47 base::Bind(&RequestHandler::EnsureReleaseObserverOnSignalingThread
,
51 void OnRequestFailed(int error
) {
52 signaling_thread_
->PostTask(FROM_HERE
,
53 base::Bind(&webrtc::DTLSIdentityRequestObserver::OnFailure
, observer_
,
55 signaling_thread_
->PostTask(FROM_HERE
,
56 base::Bind(&RequestHandler::EnsureReleaseObserverOnSignalingThread
,
60 void EnsureReleaseObserverOnSignalingThread() {
61 DCHECK(signaling_thread_
->BelongsToCurrentThread());
65 const scoped_refptr
<base::SingleThreadTaskRunner
> signaling_thread_
;
66 scoped_refptr
<webrtc::DTLSIdentityRequestObserver
> observer_
;
68 const std::string identity_name_
;
69 const std::string common_name_
;
73 PeerConnectionIdentityService::PeerConnectionIdentityService(const GURL
& origin
)
74 : main_thread_(base::ThreadTaskRunnerHandle::Get()), origin_(origin
) {
75 signaling_thread_
.DetachFromThread();
76 DCHECK(main_thread_
.get());
79 PeerConnectionIdentityService::~PeerConnectionIdentityService() {
80 // Typically destructed on libjingle's signaling thread.
83 bool PeerConnectionIdentityService::RequestIdentity(
84 const std::string
& identity_name
,
85 const std::string
& common_name
,
86 webrtc::DTLSIdentityRequestObserver
* observer
) {
87 DCHECK(signaling_thread_
.CalledOnValidThread());
90 scoped_refptr
<RequestHandler
> handler(
91 new RequestHandler(origin_
, observer
, identity_name
, common_name
));
92 main_thread_
->PostTask(FROM_HERE
,
93 base::Bind(&RequestHandler::RequestIdentityOnUIThread
, handler
));
98 } // namespace content