Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / media / webrtc_identity_service_host.cc
blob1c7291e23d62434a60fc7a2939d9f1513501b782
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"
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"
14 namespace content {
16 WebRTCIdentityServiceHost::WebRTCIdentityServiceHost(
17 int renderer_process_id,
18 scoped_refptr<WebRTCIdentityStore> identity_store)
19 : BrowserMessageFilter(WebRTCIdentityMsgStart),
20 renderer_process_id_(renderer_process_id),
21 identity_store_(identity_store),
22 weak_factory_(this) {}
24 WebRTCIdentityServiceHost::~WebRTCIdentityServiceHost() {
25 if (!cancel_callback_.is_null())
26 cancel_callback_.Run();
29 bool WebRTCIdentityServiceHost::OnMessageReceived(const IPC::Message& message,
30 bool* message_was_ok) {
31 bool handled = true;
32 IPC_BEGIN_MESSAGE_MAP_EX(WebRTCIdentityServiceHost, message, *message_was_ok)
33 IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_RequestIdentity, OnRequestIdentity)
34 IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_CancelRequest, OnCancelRequest)
35 IPC_MESSAGE_UNHANDLED(handled = false)
36 IPC_END_MESSAGE_MAP_EX()
37 return handled;
40 void WebRTCIdentityServiceHost::OnRequestIdentity(
41 int sequence_number,
42 const GURL& origin,
43 const std::string& identity_name,
44 const std::string& common_name) {
45 if (!cancel_callback_.is_null()) {
46 DLOG(WARNING)
47 << "Request rejected because the previous request has not finished.";
48 SendErrorMessage(sequence_number, net::ERR_INSUFFICIENT_RESOURCES);
49 return;
52 ChildProcessSecurityPolicyImpl* policy =
53 ChildProcessSecurityPolicyImpl::GetInstance();
54 if (!policy->CanAccessCookiesForOrigin(renderer_process_id_, origin)) {
55 DLOG(WARNING) << "Request rejected because origin access is denied.";
56 SendErrorMessage(sequence_number, net::ERR_ACCESS_DENIED);
57 return;
60 cancel_callback_ = identity_store_->RequestIdentity(
61 origin,
62 identity_name,
63 common_name,
64 base::Bind(&WebRTCIdentityServiceHost::OnComplete,
65 weak_factory_.GetWeakPtr(),
66 sequence_number));
67 if (cancel_callback_.is_null()) {
68 SendErrorMessage(sequence_number, net::ERR_UNEXPECTED);
72 void WebRTCIdentityServiceHost::OnCancelRequest() {
73 // cancel_callback_ may be null if we have sent the reponse to the renderer
74 // but the renderer has not received it.
75 if (!cancel_callback_.is_null())
76 base::ResetAndReturn(&cancel_callback_).Run();
79 void WebRTCIdentityServiceHost::OnComplete(int sequence_number,
80 int status,
81 const std::string& certificate,
82 const std::string& private_key) {
83 cancel_callback_.Reset();
84 if (status == net::OK) {
85 Send(new WebRTCIdentityHostMsg_IdentityReady(
86 sequence_number, certificate, private_key));
87 } else {
88 SendErrorMessage(sequence_number, status);
92 void WebRTCIdentityServiceHost::SendErrorMessage(int sequence_number,
93 int error) {
94 Send(new WebRTCIdentityHostMsg_RequestFailed(sequence_number, error));
97 } // namespace content