Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / media / peer_connection_identity_store.cc
blob444006ac656acb72affc09b549d9b315174c572b
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"
7 #include "base/bind.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"
12 namespace content {
13 namespace {
14 // Bridges identity requests between the main render thread and libjingle's
15 // signaling thread.
16 class RequestHandler : public base::RefCountedThreadSafe<RequestHandler> {
17 public:
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 int request_id =
25 RenderThreadImpl::current()
26 ->get_webrtc_identity_service()
27 ->RequestIdentity(
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);
34 private:
35 friend class base::RefCountedThreadSafe<RequestHandler>;
36 ~RequestHandler() {
37 DCHECK(!observer_.get());
40 void OnIdentityReady(
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,
50 this));
53 void OnRequestFailed(int error) {
54 signaling_thread_->PostTask(FROM_HERE,
55 base::Bind(&webrtc::DtlsIdentityRequestObserver::OnFailure, observer_,
56 error));
57 signaling_thread_->PostTask(FROM_HERE,
58 base::Bind(&RequestHandler::EnsureReleaseObserverOnSignalingThread,
59 this));
62 void EnsureReleaseObserverOnSignalingThread() {
63 DCHECK(signaling_thread_->BelongsToCurrentThread());
64 observer_ = nullptr;
67 const scoped_refptr<base::SingleThreadTaskRunner> signaling_thread_;
68 scoped_refptr<webrtc::DtlsIdentityRequestObserver> observer_;
70 } // namespace
72 PeerConnectionIdentityStore::PeerConnectionIdentityStore(
73 const GURL& url,
74 const GURL& first_party_for_cookies)
75 : main_thread_(base::ThreadTaskRunnerHandle::Get()),
76 url_(url),
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());
90 DCHECK(observer);
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(
96 FROM_HERE,
97 base::Bind(&RequestHandler::RequestIdentityOnUIThread, handler, url_,
98 first_party_for_cookies_));
101 } // namespace content