| OLD | NEW |
| (Empty) |
| 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/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/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/navigation_details.h" | |
| 15 #include "content/public/browser/render_frame_host.h" | |
| 16 #include "content/public/browser/render_process_host.h" | |
| 17 #include "content/public/browser/web_contents.h" | |
| 18 #include "url/gurl.h" | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 MidiDispatcherHost::PendingPermission::PendingPermission( | |
| 23 int render_process_id, | |
| 24 int render_frame_id, | |
| 25 int bridge_id) | |
| 26 : render_process_id(render_process_id), | |
| 27 render_frame_id(render_frame_id), | |
| 28 bridge_id(bridge_id) { | |
| 29 } | |
| 30 | |
| 31 MidiDispatcherHost::PendingPermission::~PendingPermission() { | |
| 32 } | |
| 33 | |
| 34 MidiDispatcherHost::MidiDispatcherHost(WebContents* web_contents) | |
| 35 : WebContentsObserver(web_contents), | |
| 36 weak_factory_(this) { | |
| 37 } | |
| 38 | |
| 39 MidiDispatcherHost::~MidiDispatcherHost() { | |
| 40 } | |
| 41 | |
| 42 void MidiDispatcherHost::RenderFrameDeleted( | |
| 43 RenderFrameHost* render_frame_host) { | |
| 44 CancelPermissionRequestsForFrame(render_frame_host); | |
| 45 } | |
| 46 | |
| 47 void MidiDispatcherHost::DidNavigateAnyFrame( | |
| 48 RenderFrameHost* render_frame_host, | |
| 49 const LoadCommittedDetails& details, | |
| 50 const FrameNavigateParams& params) { | |
| 51 if (details.is_in_page) | |
| 52 return; | |
| 53 | |
| 54 CancelPermissionRequestsForFrame(render_frame_host); | |
| 55 } | |
| 56 | |
| 57 bool MidiDispatcherHost::OnMessageReceived(const IPC::Message& message, | |
| 58 RenderFrameHost* render_frame_host) { | |
| 59 bool handled = true; | |
| 60 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MidiDispatcherHost, message, | |
| 61 render_frame_host) | |
| 62 IPC_MESSAGE_HANDLER(MidiHostMsg_RequestSysExPermission, | |
| 63 OnRequestSysExPermission) | |
| 64 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 65 IPC_END_MESSAGE_MAP() | |
| 66 return handled; | |
| 67 } | |
| 68 | |
| 69 void MidiDispatcherHost::OnRequestSysExPermission( | |
| 70 RenderFrameHost* render_frame_host, | |
| 71 int bridge_id, | |
| 72 const GURL& origin, | |
| 73 bool user_gesture) { | |
| 74 int render_process_id = render_frame_host->GetProcess()->GetID(); | |
| 75 int render_frame_id = render_frame_host->GetRoutingID(); | |
| 76 | |
| 77 PendingPermission pending_permission( | |
| 78 render_process_id, render_frame_id, bridge_id); | |
| 79 pending_permissions_.push_back(pending_permission); | |
| 80 | |
| 81 GetContentClient()->browser()->RequestPermission( | |
| 82 PERMISSION_MIDI_SYSEX, | |
| 83 web_contents(), | |
| 84 bridge_id, | |
| 85 origin, | |
| 86 user_gesture, | |
| 87 base::Bind(&MidiDispatcherHost::WasSysExPermissionGranted, | |
| 88 weak_factory_.GetWeakPtr(), | |
| 89 render_process_id, | |
| 90 render_frame_id, | |
| 91 bridge_id)); | |
| 92 } | |
| 93 | |
| 94 void MidiDispatcherHost::CancelPermissionRequestsForFrame( | |
| 95 RenderFrameHost* render_frame_host) { | |
| 96 int render_process_id = render_frame_host->GetProcess()->GetID(); | |
| 97 int render_frame_id = render_frame_host->GetRoutingID(); | |
| 98 | |
| 99 for (size_t i = 0; i < pending_permissions_.size(); ++i) { | |
| 100 if (pending_permissions_[i].render_process_id == render_process_id && | |
| 101 pending_permissions_[i].render_frame_id == render_frame_id) { | |
| 102 GetContentClient()->browser()->CancelPermissionRequest( | |
| 103 PERMISSION_MIDI_SYSEX, | |
| 104 web_contents(), | |
| 105 pending_permissions_[i].bridge_id, | |
| 106 render_frame_host->GetLastCommittedURL()); | |
| 107 | |
| 108 pending_permissions_.erase(pending_permissions_.begin() + i); | |
| 109 return; | |
| 110 } | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void MidiDispatcherHost::WasSysExPermissionGranted(int render_process_id, | |
| 115 int render_frame_id, | |
| 116 int bridge_id, | |
| 117 bool is_allowed) { | |
| 118 for (size_t i = 0; i < pending_permissions_.size(); ++i) { | |
| 119 if (pending_permissions_[i].render_process_id == render_process_id && | |
| 120 pending_permissions_[i].render_frame_id == render_frame_id && | |
| 121 pending_permissions_[i].bridge_id == bridge_id) { | |
| 122 RenderFrameHost* render_frame_host = | |
| 123 RenderFrameHost::FromID(render_process_id, render_frame_id); | |
| 124 if (render_frame_host) { | |
| 125 render_frame_host->Send(new MidiMsg_SysExPermissionApproved( | |
| 126 render_frame_id, bridge_id, is_allowed)); | |
| 127 } | |
| 128 | |
| 129 if (is_allowed) { | |
| 130 ChildProcessSecurityPolicyImpl::GetInstance()-> | |
| 131 GrantSendMidiSysExMessage(render_process_id); | |
| 132 } | |
| 133 | |
| 134 pending_permissions_.erase(pending_permissions_.begin() + i); | |
| 135 return; | |
| 136 } | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 } // namespace content | |
| OLD | NEW |