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/browser/renderer_host/media/webrtc_identity_service_host.h" 6 7 #include "base/bind.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" 13 14 namespace content { 15 16 WebRTCIdentityServiceHost::WebRTCIdentityServiceHost( 17 int renderer_process_id, 18 WebRTCIdentityStore* identity_store) 19 : renderer_process_id_(renderer_process_id), 20 identity_store_(identity_store) {} 21 22 WebRTCIdentityServiceHost::~WebRTCIdentityServiceHost() { 23 if (!cancel_callback_.is_null()) 24 cancel_callback_.Run(); 25 } 26 27 bool WebRTCIdentityServiceHost::OnMessageReceived(const IPC::Message& message, 28 bool* message_was_ok) { 29 bool handled = true; 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() 35 return handled; 36 } 37 38 void WebRTCIdentityServiceHost::OnRequestIdentity( 39 const GURL& origin, 40 const std::string& identity_name, 41 const std::string& common_name) { 42 if (!cancel_callback_.is_null()) { 43 DLOG(WARNING) 44 << "Request rejected because the previous request has not finished."; 45 SendErrorMessage(net::ERR_INSUFFICIENT_RESOURCES); 46 return; 47 } 48 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); 54 return; 55 } 56 57 cancel_callback_ = identity_store_->RequestIdentity( 58 origin, 59 identity_name, 60 common_name, 61 base::Bind(&WebRTCIdentityServiceHost::OnComplete, 62 base::Unretained(this))); 63 if (cancel_callback_.is_null()) { 64 SendErrorMessage(net::ERR_UNEXPECTED); 65 } 66 } 67 68 void WebRTCIdentityServiceHost::OnCancelRequest() { 69 base::ResetAndReturn(&cancel_callback_).Run(); 70 } 71 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)); 78 } else { 79 SendErrorMessage(status); 80 } 81 } 82 83 void WebRTCIdentityServiceHost::SendErrorMessage(int error) { 84 Send(new WebRTCIdentityHostMsg_RequestFailed(error)); 85 } 86 87 } // namespace content 88