OLD | NEW |
| (Empty) |
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/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" | |
14 | |
15 namespace content { | |
16 | |
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) {} | |
26 | |
27 WebRTCIdentityServiceHost::~WebRTCIdentityServiceHost() { | |
28 if (!cancel_callback_.is_null()) | |
29 cancel_callback_.Run(); | |
30 } | |
31 | |
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; | |
40 } | |
41 | |
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; | |
49 } | |
50 | |
51 // TODO(mkwst): Convert this to use 'url::Origin'. | |
52 GURL origin = params.url.GetOrigin(); | |
53 | |
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; | |
60 } | |
61 | |
62 bool cache_enabled = | |
63 GetContentClient()->browser()->AllowWebRTCIdentityCache( | |
64 params.url, params.first_party_for_cookies, resource_context_); | |
65 | |
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); | |
73 } | |
74 } | |
75 | |
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(); | |
81 } | |
82 | |
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); | |
93 } | |
94 } | |
95 | |
96 void WebRTCIdentityServiceHost::SendErrorMessage(int request_id, | |
97 int error) { | |
98 Send(new WebRTCIdentityHostMsg_RequestFailed(request_id, error)); | |
99 } | |
100 | |
101 } // namespace content | |
OLD | NEW |