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/navigation_details.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/web_contents.h"
22 MidiDispatcherHost::PendingPermission::PendingPermission(
23 int render_process_id
,
26 : render_process_id(render_process_id
),
27 render_frame_id(render_frame_id
),
28 bridge_id(bridge_id
) {
31 MidiDispatcherHost::PendingPermission::~PendingPermission() {
34 MidiDispatcherHost::MidiDispatcherHost(WebContents
* web_contents
)
35 : WebContentsObserver(web_contents
),
39 MidiDispatcherHost::~MidiDispatcherHost() {
42 void MidiDispatcherHost::RenderFrameDeleted(
43 RenderFrameHost
* render_frame_host
) {
44 CancelPermissionRequestsForFrame(render_frame_host
);
47 void MidiDispatcherHost::DidNavigateAnyFrame(
48 RenderFrameHost
* render_frame_host
,
49 const LoadCommittedDetails
& details
,
50 const FrameNavigateParams
& params
) {
51 if (details
.is_in_page
)
54 CancelPermissionRequestsForFrame(render_frame_host
);
57 bool MidiDispatcherHost::OnMessageReceived(const IPC::Message
& message
,
58 RenderFrameHost
* render_frame_host
) {
60 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MidiDispatcherHost
, message
,
62 IPC_MESSAGE_HANDLER(MidiHostMsg_RequestSysExPermission
,
63 OnRequestSysExPermission
)
64 IPC_MESSAGE_UNHANDLED(handled
= false)
69 void MidiDispatcherHost::OnRequestSysExPermission(
70 RenderFrameHost
* render_frame_host
,
74 int render_process_id
= render_frame_host
->GetProcess()->GetID();
75 int render_frame_id
= render_frame_host
->GetRoutingID();
77 PendingPermission
pending_permission(
78 render_process_id
, render_frame_id
, bridge_id
);
79 pending_permissions_
.push_back(pending_permission
);
81 GetContentClient()->browser()->RequestPermission(
82 PERMISSION_MIDI_SYSEX
,
87 base::Bind(&MidiDispatcherHost::WasSysExPermissionGranted
,
88 weak_factory_
.GetWeakPtr(),
94 void MidiDispatcherHost::CancelPermissionRequestsForFrame(
95 RenderFrameHost
* render_frame_host
) {
96 int render_process_id
= render_frame_host
->GetProcess()->GetID();
97 int render_frame_id
= render_frame_host
->GetRoutingID();
99 for (size_t i
= 0; i
< pending_permissions_
.size(); ++i
) {
100 if (pending_permissions_
[i
].render_process_id
== render_process_id
&&
101 pending_permissions_
[i
].render_frame_id
== render_frame_id
) {
102 GetContentClient()->browser()->CancelPermissionRequest(
103 PERMISSION_MIDI_SYSEX
,
105 pending_permissions_
[i
].bridge_id
,
106 render_frame_host
->GetLastCommittedURL());
108 pending_permissions_
.erase(pending_permissions_
.begin() + i
);
114 void MidiDispatcherHost::WasSysExPermissionGranted(int render_process_id
,
118 for (size_t i
= 0; i
< pending_permissions_
.size(); ++i
) {
119 if (pending_permissions_
[i
].render_process_id
== render_process_id
&&
120 pending_permissions_
[i
].render_frame_id
== render_frame_id
&&
121 pending_permissions_
[i
].bridge_id
== bridge_id
) {
122 RenderFrameHost
* render_frame_host
=
123 RenderFrameHost::FromID(render_process_id
, render_frame_id
);
124 if (render_frame_host
) {
125 render_frame_host
->Send(new MidiMsg_SysExPermissionApproved(
126 render_frame_id
, bridge_id
, is_allowed
));
130 ChildProcessSecurityPolicyImpl::GetInstance()->
131 GrantSendMidiSysExMessage(render_process_id
);
134 pending_permissions_
.erase(pending_permissions_
.begin() + i
);
140 } // namespace content