Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / peer_connection_identity_service.cc
blobd81320b9e6cf667136a32a6d2e96c30ee01a2657
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"
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 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);
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(&webrtc::DTLSIdentityRequestObserver::OnSuccess, observer_,
45 certificate, private_key));
46 signaling_thread_->PostTask(FROM_HERE,
47 base::Bind(&RequestHandler::EnsureReleaseObserverOnSignalingThread,
48 this));
51 void OnRequestFailed(int error) {
52 signaling_thread_->PostTask(FROM_HERE,
53 base::Bind(&webrtc::DTLSIdentityRequestObserver::OnFailure, observer_,
54 error));
55 signaling_thread_->PostTask(FROM_HERE,
56 base::Bind(&RequestHandler::EnsureReleaseObserverOnSignalingThread,
57 this));
60 void EnsureReleaseObserverOnSignalingThread() {
61 DCHECK(signaling_thread_->BelongsToCurrentThread());
62 observer_ = nullptr;
65 const scoped_refptr<base::SingleThreadTaskRunner> signaling_thread_;
66 scoped_refptr<webrtc::DTLSIdentityRequestObserver> observer_;
67 const GURL origin_;
68 const std::string identity_name_;
69 const std::string common_name_;
71 } // namespace
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());
88 DCHECK(observer);
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));
95 return true;
98 } // namespace content