Home | History | Annotate | Download | only in media
      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 
     20 MidiDispatcher::MidiDispatcher(RenderFrame* render_frame)
     21     : RenderFrameObserver(render_frame) {
     22 }
     23 
     24 MidiDispatcher::~MidiDispatcher() {}
     25 
     26 bool 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 
     36 void 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 
     45 void 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       // The request will be removed by OnSysExPermissionApproved once
     54       // the blink MIDIAccessInitializer object is deleted.
     55       break;
     56     }
     57   }
     58 }
     59 
     60 void MidiDispatcher::OnSysExPermissionApproved(int bridge_id,
     61                                                bool is_allowed) {
     62   // |request| can be NULL when the request is canceled.
     63   WebMIDIPermissionRequest* request = requests_.Lookup(bridge_id);
     64   if (!request)
     65     return;
     66   request->setIsAllowed(is_allowed);
     67   requests_.Remove(bridge_id);
     68 }
     69 
     70 }  // namespace content
     71