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/browser/renderer_host/media/webrtc_identity_service_host.h"
8 #include "base/callback_helpers.h"
9 #include "content/browser/child_process_security_policy_impl.h"
10 #include "content/browser/media/webrtc_identity_store.h"
11 #include "content/common/media/webrtc_identity_messages.h"
12 #include "net/base/net_errors.h"
16 WebRTCIdentityServiceHost::WebRTCIdentityServiceHost(
17 int renderer_process_id,
18 WebRTCIdentityStore* identity_store)
19 : renderer_process_id_(renderer_process_id),
20 identity_store_(identity_store) {}
22 WebRTCIdentityServiceHost::~WebRTCIdentityServiceHost() {
23 if (!cancel_callback_.is_null())
24 cancel_callback_.Run();
27 bool WebRTCIdentityServiceHost::OnMessageReceived(const IPC::Message& message,
28 bool* message_was_ok) {
30 IPC_BEGIN_MESSAGE_MAP_EX(WebRTCIdentityServiceHost, message, *message_was_ok)
31 IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_RequestIdentity, OnRequestIdentity)
32 IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_CancelRequest, OnCancelRequest)
33 IPC_MESSAGE_UNHANDLED(handled = false)
34 IPC_END_MESSAGE_MAP_EX()
38 void WebRTCIdentityServiceHost::OnRequestIdentity(
40 const std::string& identity_name,
41 const std::string& common_name) {
42 if (!cancel_callback_.is_null()) {
44 << "Request rejected because the previous request has not finished.";
45 SendErrorMessage(net::ERR_INSUFFICIENT_RESOURCES);
49 ChildProcessSecurityPolicyImpl* policy =
50 ChildProcessSecurityPolicyImpl::GetInstance();
51 if (!policy->CanAccessCookiesForOrigin(renderer_process_id_, origin)) {
52 DLOG(WARNING) << "Request rejected because origin access is denied.";
53 SendErrorMessage(net::ERR_ACCESS_DENIED);
57 cancel_callback_ = identity_store_->RequestIdentity(
61 base::Bind(&WebRTCIdentityServiceHost::OnComplete,
62 base::Unretained(this)));
63 if (cancel_callback_.is_null()) {
64 SendErrorMessage(net::ERR_UNEXPECTED);
68 void WebRTCIdentityServiceHost::OnCancelRequest() {
69 base::ResetAndReturn(&cancel_callback_).Run();
72 void WebRTCIdentityServiceHost::OnComplete(int status,
73 const std::string& certificate,
74 const std::string& private_key) {
75 cancel_callback_.Reset();
76 if (status == net::OK) {
77 Send(new WebRTCIdentityHostMsg_IdentityReady(certificate, private_key));
79 SendErrorMessage(status);
83 void WebRTCIdentityServiceHost::SendErrorMessage(int error) {
84 Send(new WebRTCIdentityHostMsg_RequestFailed(error));
87 } // namespace content