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
10namespace content {
11
12PeerConnectionIdentityService::PeerConnectionIdentityService(const GURL& origin)
13    : origin_(origin), pending_observer_(NULL), pending_request_id_(0) {}
14
15PeerConnectionIdentityService::~PeerConnectionIdentityService() {
16  if (pending_observer_)
17    RenderThreadImpl::current()->get_webrtc_identity_service()
18        ->CancelRequest(pending_request_id_);
19}
20
21bool 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
42void PeerConnectionIdentityService::OnIdentityReady(
43    const std::string& certificate,
44    const std::string& private_key) {
45  pending_observer_->OnSuccess(certificate, private_key);
46  ResetPendingRequest();
47}
48
49void PeerConnectionIdentityService::OnRequestFailed(int error) {
50  pending_observer_->OnFailure(error);
51  ResetPendingRequest();
52}
53
54void PeerConnectionIdentityService::ResetPendingRequest() {
55  pending_observer_ = NULL;
56  pending_request_id_ = 0;
57}
58
59}  // namespace content
60