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/webrtc_identity_service.h"
7 #include "content/common/media/webrtc_identity_messages.h"
8 #include "content/public/renderer/render_thread.h"
9 #include "net/base/net_errors.h"
13 WebRTCIdentityService::RequestInfo::RequestInfo(
16 const std::string& identity_name,
17 const std::string& common_name,
18 const SuccessCallback& success_callback,
19 const FailureCallback& failure_callback)
20 : request_id(request_id),
22 identity_name(identity_name),
23 common_name(common_name),
24 success_callback(success_callback),
25 failure_callback(failure_callback) {}
27 WebRTCIdentityService::RequestInfo::~RequestInfo() {}
29 WebRTCIdentityService::WebRTCIdentityService() : next_request_id_(1) {
30 // RenderThread::Get() could be NULL in unit tests.
31 if (RenderThread::Get())
32 RenderThread::Get()->AddObserver(this);
35 WebRTCIdentityService::~WebRTCIdentityService() {
36 // RenderThread::Get() could be NULL in unit tests.
37 if (RenderThread::Get()) {
38 RenderThread::Get()->RemoveObserver(this);
40 if (!pending_requests_.empty()) {
41 RenderThread::Get()->Send(new WebRTCIdentityMsg_CancelRequest());
46 int WebRTCIdentityService::RequestIdentity(
48 const std::string& identity_name,
49 const std::string& common_name,
50 const SuccessCallback& success_callback,
51 const FailureCallback& failure_callback) {
52 int request_id = next_request_id_++;
54 RequestInfo request_info(request_id,
61 pending_requests_.push_back(request_info);
62 if (pending_requests_.size() == 1)
63 SendRequest(request_info);
68 void WebRTCIdentityService::CancelRequest(int request_id) {
69 std::deque<RequestInfo>::iterator it;
70 for (it = pending_requests_.begin(); it != pending_requests_.end(); ++it) {
71 if (it->request_id != request_id)
73 if (it != pending_requests_.begin()) {
74 pending_requests_.erase(it);
76 Send(new WebRTCIdentityMsg_CancelRequest());
77 OnOutstandingRequestReturned();
83 bool WebRTCIdentityService::Send(IPC::Message* message) {
84 // Unit tests should override this method to avoid null-ptr-deref.
85 return RenderThread::Get()->Send(message);
88 bool WebRTCIdentityService::OnControlMessageReceived(
89 const IPC::Message& message) {
91 IPC_BEGIN_MESSAGE_MAP(WebRTCIdentityService, message)
92 IPC_MESSAGE_HANDLER(WebRTCIdentityHostMsg_IdentityReady, OnIdentityReady)
93 IPC_MESSAGE_HANDLER(WebRTCIdentityHostMsg_RequestFailed, OnRequestFailed)
94 IPC_MESSAGE_UNHANDLED(handled = false)
100 void WebRTCIdentityService::OnIdentityReady(const std::string& certificate,
101 const std::string& private_key) {
102 DCHECK(!pending_requests_.empty());
103 pending_requests_.front().success_callback.Run(certificate, private_key);
104 OnOutstandingRequestReturned();
107 void WebRTCIdentityService::OnRequestFailed(int error) {
108 DCHECK(!pending_requests_.empty());
109 pending_requests_.front().failure_callback.Run(error);
110 OnOutstandingRequestReturned();
113 void WebRTCIdentityService::SendRequest(const RequestInfo& request_info) {
114 if (!Send(new WebRTCIdentityMsg_RequestIdentity(request_info.origin,
115 request_info.identity_name,
116 request_info.common_name))) {
117 base::MessageLoop::current()->PostTask(
119 base::Bind(&WebRTCIdentityService::OnRequestFailed,
120 base::Unretained(this),
121 net::ERR_UNEXPECTED));
125 void WebRTCIdentityService::OnOutstandingRequestReturned() {
126 pending_requests_.pop_front();
128 if (!pending_requests_.empty())
129 SendRequest(pending_requests_.front());
132 } // namespace content