Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / media / webrtc_identity_service_host.cc
blobdc1e590cb706f9444e7ad5cc534d98352f675a34
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 "content/public/browser/content_browser_client.h"
13 #include "net/base/net_errors.h"
15 namespace content {
17 WebRTCIdentityServiceHost::WebRTCIdentityServiceHost(
18 int renderer_process_id,
19 scoped_refptr<WebRTCIdentityStore> identity_store,
20 ResourceContext* resource_context)
21 : BrowserMessageFilter(WebRTCIdentityMsgStart),
22 renderer_process_id_(renderer_process_id),
23 identity_store_(identity_store),
24 resource_context_(resource_context),
25 weak_factory_(this) {}
27 WebRTCIdentityServiceHost::~WebRTCIdentityServiceHost() {
28 if (!cancel_callback_.is_null())
29 cancel_callback_.Run();
32 bool WebRTCIdentityServiceHost::OnMessageReceived(const IPC::Message& message) {
33 bool handled = true;
34 IPC_BEGIN_MESSAGE_MAP(WebRTCIdentityServiceHost, message)
35 IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_RequestIdentity, OnRequestIdentity)
36 IPC_MESSAGE_HANDLER(WebRTCIdentityMsg_CancelRequest, OnCancelRequest)
37 IPC_MESSAGE_UNHANDLED(handled = false)
38 IPC_END_MESSAGE_MAP()
39 return handled;
42 void WebRTCIdentityServiceHost::OnRequestIdentity(
43 const WebRTCIdentityMsg_RequestIdentity_Params& params) {
44 if (!cancel_callback_.is_null()) {
45 DLOG(WARNING)
46 << "Request rejected because the previous request has not finished.";
47 SendErrorMessage(params.request_id, net::ERR_INSUFFICIENT_RESOURCES);
48 return;
51 // TODO(mkwst): Convert this to use 'url::Origin'.
52 GURL origin = params.url.GetOrigin();
54 ChildProcessSecurityPolicyImpl* policy =
55 ChildProcessSecurityPolicyImpl::GetInstance();
56 if (!policy->CanAccessDataForOrigin(renderer_process_id_, origin)) {
57 DLOG(WARNING) << "Request rejected because origin access is denied.";
58 SendErrorMessage(params.request_id, net::ERR_ACCESS_DENIED);
59 return;
62 bool cache_enabled =
63 GetContentClient()->browser()->AllowWebRTCIdentityCache(
64 params.url, params.first_party_for_cookies, resource_context_);
66 cancel_callback_ = identity_store_->RequestIdentity(
67 origin, params.identity_name, params.common_name,
68 base::Bind(&WebRTCIdentityServiceHost::OnComplete,
69 weak_factory_.GetWeakPtr(), params.request_id),
70 cache_enabled);
71 if (cancel_callback_.is_null()) {
72 SendErrorMessage(params.request_id, net::ERR_UNEXPECTED);
76 void WebRTCIdentityServiceHost::OnCancelRequest() {
77 // cancel_callback_ may be null if we have sent the reponse to the renderer
78 // but the renderer has not received it.
79 if (!cancel_callback_.is_null())
80 base::ResetAndReturn(&cancel_callback_).Run();
83 void WebRTCIdentityServiceHost::OnComplete(int request_id,
84 int status,
85 const std::string& certificate,
86 const std::string& private_key) {
87 cancel_callback_.Reset();
88 if (status == net::OK) {
89 Send(new WebRTCIdentityHostMsg_IdentityReady(
90 request_id, certificate, private_key));
91 } else {
92 SendErrorMessage(request_id, status);
96 void WebRTCIdentityServiceHost::SendErrorMessage(int request_id,
97 int error) {
98 Send(new WebRTCIdentityHostMsg_RequestFailed(request_id, error));
101 } // namespace content