Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / media / peer_connection_identity_service.cc
blobf58dc3b6f39de617d48855060684dbe3f6fb7a42
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 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) {
26 int request_id =
27 RenderThreadImpl::current()
28 ->get_webrtc_identity_service()
29 ->RequestIdentity(
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);
36 private:
37 friend class base::RefCountedThreadSafe<RequestHandler>;
38 ~RequestHandler() {
39 DCHECK(!observer_.get());
42 void OnIdentityReady(
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,
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 PeerConnectionIdentityService::PeerConnectionIdentityService(
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 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());
91 DCHECK(observer);
93 scoped_refptr<RequestHandler> handler(new RequestHandler(observer));
94 main_thread_->PostTask(
95 FROM_HERE,
96 base::Bind(&RequestHandler::RequestIdentityOnUIThread, handler, url_,
97 first_party_for_cookies_, identity_name, common_name));
99 return true;
102 } // namespace content