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/media/midi_dispatcher_host.h"
8 #include "content/browser/child_process_security_policy_impl.h"
9 #include "content/browser/frame_host/render_frame_host_impl.h"
10 #include "content/common/media/midi_messages.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/content_browser_client.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/web_contents.h"
21 MidiDispatcherHost::PendingPermission::PendingPermission(
22 int render_process_id
,
25 : render_process_id(render_process_id
),
26 render_frame_id(render_frame_id
),
27 bridge_id(bridge_id
) {
30 MidiDispatcherHost::PendingPermission::~PendingPermission() {
33 MidiDispatcherHost::MidiDispatcherHost(WebContents
* web_contents
)
34 : WebContentsObserver(web_contents
),
38 MidiDispatcherHost::~MidiDispatcherHost() {
41 bool MidiDispatcherHost::OnMessageReceived(const IPC::Message
& message
,
42 RenderFrameHost
* render_frame_host
) {
44 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MidiDispatcherHost
, message
,
46 IPC_MESSAGE_HANDLER(MidiHostMsg_RequestSysExPermission
,
47 OnRequestSysExPermission
)
48 IPC_MESSAGE_HANDLER(MidiHostMsg_CancelSysExPermissionRequest
,
49 OnCancelSysExPermissionRequest
)
50 IPC_MESSAGE_UNHANDLED(handled
= false)
55 void MidiDispatcherHost::OnRequestSysExPermission(
56 RenderFrameHost
* render_frame_host
,
60 int render_process_id
= render_frame_host
->GetProcess()->GetID();
61 int render_frame_id
= render_frame_host
->GetRoutingID();
63 PendingPermission
pending_permission(
64 render_process_id
, render_frame_id
, bridge_id
);
65 pending_permissions_
.push_back(pending_permission
);
67 GetContentClient()->browser()->RequestMidiSysExPermission(
72 base::Bind(&MidiDispatcherHost::WasSysExPermissionGranted
,
73 weak_factory_
.GetWeakPtr(),
74 render_process_id
, render_frame_id
, bridge_id
),
75 &pending_permissions_
.back().cancel
);
78 void MidiDispatcherHost::OnCancelSysExPermissionRequest(
79 RenderFrameHost
* render_frame_host
,
81 const GURL
& requesting_frame
) {
82 int render_process_id
= render_frame_host
->GetProcess()->GetID();
83 int render_frame_id
= render_frame_host
->GetRoutingID();
85 for (size_t i
= 0; i
< pending_permissions_
.size(); ++i
) {
86 if (pending_permissions_
[i
].render_process_id
== render_process_id
&&
87 pending_permissions_
[i
].render_frame_id
== render_frame_id
&&
88 pending_permissions_
[i
].bridge_id
== bridge_id
) {
89 if (!pending_permissions_
[i
].cancel
.is_null())
90 pending_permissions_
[i
].cancel
.Run();
91 pending_permissions_
.erase(pending_permissions_
.begin() + i
);
97 void MidiDispatcherHost::WasSysExPermissionGranted(int render_process_id
,
101 for (size_t i
= 0; i
< pending_permissions_
.size(); ++i
) {
102 if (pending_permissions_
[i
].render_process_id
== render_process_id
&&
103 pending_permissions_
[i
].render_frame_id
== render_frame_id
&&
104 pending_permissions_
[i
].bridge_id
== bridge_id
) {
105 RenderFrameHost
* render_frame_host
=
106 RenderFrameHost::FromID(render_process_id
, render_frame_id
);
107 if (render_frame_host
) {
108 render_frame_host
->Send(new MidiMsg_SysExPermissionApproved(
109 render_frame_id
, bridge_id
, is_allowed
));
113 ChildProcessSecurityPolicyImpl::GetInstance()->
114 GrantSendMidiSysExMessage(render_process_id
);
117 pending_permissions_
.erase(pending_permissions_
.begin() + i
);
123 } // namespace content