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/renderer/media/midi_dispatcher.h"
8 #include "content/public/common/service_registry.h"
9 #include "content/public/renderer/render_frame.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"
15 using blink::WebMIDIPermissionRequest
;
16 using blink::WebSecurityOrigin
;
20 MidiDispatcher::MidiDispatcher(RenderFrame
* render_frame
)
21 : RenderFrameObserver(render_frame
) {
24 MidiDispatcher::~MidiDispatcher() {}
26 void MidiDispatcher::requestSysexPermission(
27 const WebMIDIPermissionRequest
& request
) {
28 if (!permission_service_
.get()) {
29 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
30 mojo::GetProxy(&permission_service_
));
33 int permission_request_id
=
34 requests_
.Add(new WebMIDIPermissionRequest(request
));
36 permission_service_
->RequestPermission(
37 PERMISSION_NAME_MIDI_SYSEX
,
38 request
.securityOrigin().toString().utf8(),
39 blink::WebUserGestureIndicator::isProcessingUserGesture(),
40 base::Bind(&MidiDispatcher::OnSysExPermissionSet
,
41 base::Unretained(this),
42 permission_request_id
));
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
))
51 requests_
.Remove(it
.GetCurrentKey());
56 void MidiDispatcher::OnSysExPermissionSet(int request_id
,
57 PermissionStatus status
) {
58 // |request| can be NULL when the request is canceled.
59 WebMIDIPermissionRequest
* request
= requests_
.Lookup(request_id
);
62 request
->setIsAllowed(status
== PERMISSION_STATUS_GRANTED
);
63 requests_
.Remove(request_id
);
66 } // namespace content