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/renderer/media/midi_dispatcher.h" 6 7 #include "base/bind.h" 8 #include "base/message_loop/message_loop.h" 9 #include "content/common/media/midi_messages.h" 10 #include "third_party/WebKit/public/platform/WebString.h" 11 #include "third_party/WebKit/public/web/WebMIDIPermissionRequest.h" 12 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" 13 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" 14 15 using blink::WebMIDIPermissionRequest; 16 using blink::WebSecurityOrigin; 17 18 namespace content { 19 MidiDispatcher(RenderFrame * render_frame)20MidiDispatcher::MidiDispatcher(RenderFrame* render_frame) 21 : RenderFrameObserver(render_frame) { 22 } 23 ~MidiDispatcher()24MidiDispatcher::~MidiDispatcher() {} 25 OnMessageReceived(const IPC::Message & message)26bool MidiDispatcher::OnMessageReceived(const IPC::Message& message) { 27 bool handled = true; 28 IPC_BEGIN_MESSAGE_MAP(MidiDispatcher, message) 29 IPC_MESSAGE_HANDLER(MidiMsg_SysExPermissionApproved, 30 OnSysExPermissionApproved) 31 IPC_MESSAGE_UNHANDLED(handled = false) 32 IPC_END_MESSAGE_MAP() 33 return handled; 34 } 35 requestSysexPermission(const WebMIDIPermissionRequest & request)36void MidiDispatcher::requestSysexPermission( 37 const WebMIDIPermissionRequest& request) { 38 int bridge_id = requests_.Add(new WebMIDIPermissionRequest(request)); 39 WebSecurityOrigin security_origin = request.securityOrigin(); 40 GURL url(security_origin.toString()); 41 Send(new MidiHostMsg_RequestSysExPermission(routing_id(), bridge_id, url, 42 blink::WebUserGestureIndicator::isProcessingUserGesture())); 43 } 44 cancelSysexPermissionRequest(const WebMIDIPermissionRequest & request)45void MidiDispatcher::cancelSysexPermissionRequest( 46 const WebMIDIPermissionRequest& request) { 47 for (Requests::iterator it(&requests_); !it.IsAtEnd(); it.Advance()) { 48 WebMIDIPermissionRequest* value = it.GetCurrentValue(); 49 if (value->equals(request)) { 50 base::string16 origin = request.securityOrigin().toString(); 51 Send(new MidiHostMsg_CancelSysExPermissionRequest( 52 routing_id(), it.GetCurrentKey(), GURL(origin))); 53 requests_.Remove(it.GetCurrentKey()); 54 break; 55 } 56 } 57 } 58 OnSysExPermissionApproved(int bridge_id,bool is_allowed)59void MidiDispatcher::OnSysExPermissionApproved(int bridge_id, 60 bool is_allowed) { 61 // |request| can be NULL when the request is canceled. 62 WebMIDIPermissionRequest* request = requests_.Lookup(bridge_id); 63 if (!request) 64 return; 65 request->setIsAllowed(is_allowed); 66 requests_.Remove(bridge_id); 67 } 68 69 } // namespace content 70