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 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
,
24 const std::string
& identity_name
,
25 const std::string
& common_name
) {
27 RenderThreadImpl::current()
28 ->get_webrtc_identity_service()
30 url
, first_party_for_cookies
, identity_name
, common_name
,
31 base::Bind(&RequestHandler::OnIdentityReady
, this),
32 base::Bind(&RequestHandler::OnRequestFailed
, this));
33 DCHECK_NE(request_id
, 0);
37 friend class base::RefCountedThreadSafe
<RequestHandler
>;
39 DCHECK(!observer_
.get());
43 const std::string
& certificate
,
44 const std::string
& private_key
) {
45 signaling_thread_
->PostTask(FROM_HERE
,
46 base::Bind(&webrtc::DTLSIdentityRequestObserver::OnSuccess
, observer_
,
47 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 PeerConnectionIdentityService::PeerConnectionIdentityService(
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 PeerConnectionIdentityService::~PeerConnectionIdentityService() {
83 // Typically destructed on libjingle's signaling thread.
86 bool PeerConnectionIdentityService::RequestIdentity(
87 const std::string
& identity_name
,
88 const std::string
& common_name
,
89 webrtc::DTLSIdentityRequestObserver
* observer
) {
90 DCHECK(signaling_thread_
.CalledOnValidThread());
93 scoped_refptr
<RequestHandler
> handler(new RequestHandler(observer
));
94 main_thread_
->PostTask(
96 base::Bind(&RequestHandler::RequestIdentityOnUIThread
, handler
, url_
,
97 first_party_for_cookies_
, identity_name
, common_name
));
102 } // namespace content