Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / media / midi_dispatcher_host.cc
blob9b6faa1c585d0808dcee40ab1e987fdaf17c4bbe
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"
7 #include "base/bind.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/render_frame_host.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "url/gurl.h"
19 namespace content {
21 MidiDispatcherHost::PendingPermission::PendingPermission(
22 int render_process_id,
23 int render_frame_id,
24 int bridge_id)
25 : render_process_id(render_process_id),
26 render_frame_id(render_frame_id),
27 bridge_id(bridge_id) {
30 MidiDispatcherHost::PendingPermission::~PendingPermission() {
33 MidiDispatcherHost::MidiDispatcherHost(WebContents* web_contents)
34 : WebContentsObserver(web_contents),
35 weak_factory_(this) {
38 MidiDispatcherHost::~MidiDispatcherHost() {
41 bool MidiDispatcherHost::OnMessageReceived(const IPC::Message& message,
42 RenderFrameHost* render_frame_host) {
43 bool handled = true;
44 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MidiDispatcherHost, message,
45 render_frame_host)
46 IPC_MESSAGE_HANDLER(MidiHostMsg_RequestSysExPermission,
47 OnRequestSysExPermission)
48 IPC_MESSAGE_HANDLER(MidiHostMsg_CancelSysExPermissionRequest,
49 OnCancelSysExPermissionRequest)
50 IPC_MESSAGE_UNHANDLED(handled = false)
51 IPC_END_MESSAGE_MAP()
52 return handled;
55 void MidiDispatcherHost::OnRequestSysExPermission(
56 RenderFrameHost* render_frame_host,
57 int bridge_id,
58 const GURL& origin,
59 bool user_gesture) {
60 int render_process_id = render_frame_host->GetProcess()->GetID();
61 int render_frame_id = render_frame_host->GetRoutingID();
63 PendingPermission pending_permission(
64 render_process_id, render_frame_id, bridge_id);
65 pending_permissions_.push_back(pending_permission);
67 GetContentClient()->browser()->RequestMidiSysExPermission(
68 web_contents(),
69 bridge_id,
70 origin,
71 user_gesture,
72 base::Bind(&MidiDispatcherHost::WasSysExPermissionGranted,
73 weak_factory_.GetWeakPtr(),
74 render_process_id, render_frame_id, bridge_id),
75 &pending_permissions_.back().cancel);
78 void MidiDispatcherHost::OnCancelSysExPermissionRequest(
79 RenderFrameHost* render_frame_host,
80 int bridge_id,
81 const GURL& requesting_frame) {
82 int render_process_id = render_frame_host->GetProcess()->GetID();
83 int render_frame_id = render_frame_host->GetRoutingID();
85 for (size_t i = 0; i < pending_permissions_.size(); ++i) {
86 if (pending_permissions_[i].render_process_id == render_process_id &&
87 pending_permissions_[i].render_frame_id == render_frame_id &&
88 pending_permissions_[i].bridge_id == bridge_id) {
89 if (!pending_permissions_[i].cancel.is_null())
90 pending_permissions_[i].cancel.Run();
91 pending_permissions_.erase(pending_permissions_.begin() + i);
92 return;
97 void MidiDispatcherHost::WasSysExPermissionGranted(int render_process_id,
98 int render_frame_id,
99 int bridge_id,
100 bool is_allowed) {
101 for (size_t i = 0; i < pending_permissions_.size(); ++i) {
102 if (pending_permissions_[i].render_process_id == render_process_id &&
103 pending_permissions_[i].render_frame_id == render_frame_id &&
104 pending_permissions_[i].bridge_id == bridge_id) {
105 RenderFrameHost* render_frame_host =
106 RenderFrameHost::FromID(render_process_id, render_frame_id);
107 if (render_frame_host) {
108 render_frame_host->Send(new MidiMsg_SysExPermissionApproved(
109 render_frame_id, bridge_id, is_allowed));
112 if (is_allowed) {
113 ChildProcessSecurityPolicyImpl::GetInstance()->
114 GrantSendMidiSysExMessage(render_process_id);
117 pending_permissions_.erase(pending_permissions_.begin() + i);
118 return;
123 } // namespace content