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.
4
5 #include "content/renderer/media/peer_connection_identity_service.h"
6
7 #include "content/renderer/media/webrtc_identity_service.h"
8 #include "content/renderer/render_thread_impl.h"
9
10 namespace content {
11
PeerConnectionIdentityService(const GURL & origin)12 PeerConnectionIdentityService::PeerConnectionIdentityService(const GURL& origin)
13 : origin_(origin), pending_observer_(NULL), pending_request_id_(0) {}
14
~PeerConnectionIdentityService()15 PeerConnectionIdentityService::~PeerConnectionIdentityService() {
16 if (pending_observer_)
17 RenderThreadImpl::current()->get_webrtc_identity_service()
18 ->CancelRequest(pending_request_id_);
19 }
20
RequestIdentity(const std::string & identity_name,const std::string & common_name,webrtc::DTLSIdentityRequestObserver * observer)21 bool PeerConnectionIdentityService::RequestIdentity(
22 const std::string& identity_name,
23 const std::string& common_name,
24 webrtc::DTLSIdentityRequestObserver* observer) {
25 DCHECK(observer);
26 if (pending_observer_)
27 return false;
28
29 pending_observer_ = observer;
30 pending_request_id_ = RenderThreadImpl::current()
31 ->get_webrtc_identity_service()->RequestIdentity(
32 origin_,
33 identity_name,
34 common_name,
35 base::Bind(&PeerConnectionIdentityService::OnIdentityReady,
36 base::Unretained(this)),
37 base::Bind(&PeerConnectionIdentityService::OnRequestFailed,
38 base::Unretained(this)));
39 return true;
40 }
41
OnIdentityReady(const std::string & certificate,const std::string & private_key)42 void PeerConnectionIdentityService::OnIdentityReady(
43 const std::string& certificate,
44 const std::string& private_key) {
45 pending_observer_->OnSuccess(certificate, private_key);
46 ResetPendingRequest();
47 }
48
OnRequestFailed(int error)49 void PeerConnectionIdentityService::OnRequestFailed(int error) {
50 pending_observer_->OnFailure(error);
51 ResetPendingRequest();
52 }
53
ResetPendingRequest()54 void PeerConnectionIdentityService::ResetPendingRequest() {
55 pending_observer_ = NULL;
56 pending_request_id_ = 0;
57 }
58
59 } // namespace content
60