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/midi_dispatcher_host.h"
6
7 #include "base/bind.h"
8 #include "content/browser/child_process_security_policy_impl.h"
9 #include "content/browser/renderer_host/render_view_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 "url/gurl.h"
14
15 namespace content {
16
MIDIDispatcherHost(int render_process_id,BrowserContext * browser_context)17 MIDIDispatcherHost::MIDIDispatcherHost(int render_process_id,
18 BrowserContext* browser_context)
19 : render_process_id_(render_process_id),
20 browser_context_(browser_context) {
21 }
22
~MIDIDispatcherHost()23 MIDIDispatcherHost::~MIDIDispatcherHost() {
24 }
25
OnMessageReceived(const IPC::Message & message,bool * message_was_ok)26 bool MIDIDispatcherHost::OnMessageReceived(const IPC::Message& message,
27 bool* message_was_ok) {
28 bool handled = true;
29 IPC_BEGIN_MESSAGE_MAP_EX(MIDIDispatcherHost, message, *message_was_ok)
30 IPC_MESSAGE_HANDLER(MIDIHostMsg_RequestSysExPermission,
31 OnRequestSysExPermission)
32 IPC_MESSAGE_HANDLER(MIDIHostMsg_CancelSysExPermissionRequest,
33 OnCancelSysExPermissionRequest)
34 IPC_MESSAGE_UNHANDLED(handled = false)
35 IPC_END_MESSAGE_MAP_EX()
36 return handled;
37 }
38
OverrideThreadForMessage(const IPC::Message & message,BrowserThread::ID * thread)39 void MIDIDispatcherHost::OverrideThreadForMessage(
40 const IPC::Message& message, BrowserThread::ID* thread) {
41 if (IPC_MESSAGE_CLASS(message) == MIDIMsgStart)
42 *thread = BrowserThread::UI;
43 }
44
OnRequestSysExPermission(int render_view_id,int bridge_id,const GURL & origin)45 void MIDIDispatcherHost::OnRequestSysExPermission(int render_view_id,
46 int bridge_id,
47 const GURL& origin) {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49
50 browser_context_->RequestMIDISysExPermission(
51 render_process_id_,
52 render_view_id,
53 bridge_id,
54 origin,
55 base::Bind(&MIDIDispatcherHost::WasSysExPermissionGranted,
56 base::Unretained(this),
57 render_view_id,
58 bridge_id));
59 }
60
OnCancelSysExPermissionRequest(int render_view_id,int bridge_id,const GURL & requesting_frame)61 void MIDIDispatcherHost::OnCancelSysExPermissionRequest(
62 int render_view_id,
63 int bridge_id,
64 const GURL& requesting_frame) {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
66 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" << render_view_id
67 << ":" << bridge_id;
68 browser_context_->CancelMIDISysExPermissionRequest(
69 render_process_id_, render_view_id, bridge_id, requesting_frame);
70 }
WasSysExPermissionGranted(int render_view_id,int bridge_id,bool success)71 void MIDIDispatcherHost::WasSysExPermissionGranted(int render_view_id,
72 int bridge_id,
73 bool success) {
74 ChildProcessSecurityPolicyImpl::GetInstance()->GrantSendMIDISysExMessage(
75 render_process_id_);
76 Send(new MIDIMsg_SysExPermissionApproved(render_view_id, bridge_id, success));
77 }
78
79 } // namespace content
80