| 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/renderer_host/render_view_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_process_host.h" |
| 15 #include "content/public/browser/render_view_host.h" |
| 16 #include "content/public/browser/web_contents.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 MidiDispatcherHost::PendingPermission::PendingPermission( |
| 22 int render_process_id, |
| 23 int render_view_id, |
| 24 int bridge_id) |
| 25 : render_process_id(render_process_id), |
| 26 render_view_id(render_view_id), |
| 27 bridge_id(bridge_id) { |
| 28 } |
| 29 |
| 30 MidiDispatcherHost::PendingPermission::~PendingPermission() { |
| 31 } |
| 32 |
| 33 MidiDispatcherHost::MidiDispatcherHost(WebContents* web_contents) |
| 34 : WebContentsObserver(web_contents), |
| 35 weak_factory_(this) { |
| 36 } |
| 37 |
| 38 MidiDispatcherHost::~MidiDispatcherHost() { |
| 39 } |
| 40 |
| 41 bool MidiDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
| 42 bool handled = true; |
| 43 IPC_BEGIN_MESSAGE_MAP(MidiDispatcherHost, message) |
| 44 IPC_MESSAGE_HANDLER(MidiHostMsg_RequestSysExPermission, |
| 45 OnRequestSysExPermission) |
| 46 IPC_MESSAGE_HANDLER(MidiHostMsg_CancelSysExPermissionRequest, |
| 47 OnCancelSysExPermissionRequest) |
| 48 IPC_MESSAGE_UNHANDLED(handled = false) |
| 49 IPC_END_MESSAGE_MAP() |
| 50 return handled; |
| 51 } |
| 52 |
| 53 void MidiDispatcherHost::OnRequestSysExPermission(int bridge_id, |
| 54 const GURL& origin, |
| 55 bool user_gesture) { |
| 56 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
| 57 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 58 |
| 59 PendingPermission pending_permission( |
| 60 render_process_id, render_view_id, bridge_id); |
| 61 pending_permissions_.push_back(pending_permission); |
| 62 |
| 63 GetContentClient()->browser()->RequestMidiSysExPermission( |
| 64 web_contents(), |
| 65 bridge_id, |
| 66 origin, |
| 67 user_gesture, |
| 68 base::Bind(&MidiDispatcherHost::WasSysExPermissionGranted, |
| 69 weak_factory_.GetWeakPtr(), |
| 70 render_process_id, render_view_id, bridge_id), |
| 71 &pending_permissions_.back().cancel); |
| 72 } |
| 73 |
| 74 void MidiDispatcherHost::OnCancelSysExPermissionRequest( |
| 75 int bridge_id, |
| 76 const GURL& requesting_frame) { |
| 77 int render_process_id = web_contents()->GetRenderProcessHost()->GetID(); |
| 78 int render_view_id = web_contents()->GetRenderViewHost()->GetRoutingID(); |
| 79 |
| 80 for (size_t i = 0; i < pending_permissions_.size(); ++i) { |
| 81 if (pending_permissions_[i].render_process_id == render_process_id && |
| 82 pending_permissions_[i].render_view_id == render_view_id && |
| 83 pending_permissions_[i].bridge_id == bridge_id) { |
| 84 if (!pending_permissions_[i].cancel.is_null()) |
| 85 pending_permissions_[i].cancel.Run(); |
| 86 pending_permissions_.erase(pending_permissions_.begin() + i); |
| 87 return; |
| 88 } |
| 89 } |
| 90 } |
| 91 |
| 92 void MidiDispatcherHost::WasSysExPermissionGranted(int render_process_id, |
| 93 int render_view_id, |
| 94 int bridge_id, |
| 95 bool is_allowed) { |
| 96 for (size_t i = 0; i < pending_permissions_.size(); ++i) { |
| 97 if (pending_permissions_[i].render_process_id == render_process_id && |
| 98 pending_permissions_[i].render_view_id == render_view_id && |
| 99 pending_permissions_[i].bridge_id == bridge_id) { |
| 100 RenderViewHost* render_view_host = |
| 101 RenderViewHost::FromID(render_process_id, render_view_id); |
| 102 if (render_view_host) { |
| 103 render_view_host->Send(new MidiMsg_SysExPermissionApproved( |
| 104 render_view_id, bridge_id, is_allowed)); |
| 105 } |
| 106 |
| 107 if (is_allowed) { |
| 108 ChildProcessSecurityPolicyImpl::GetInstance()-> |
| 109 GrantSendMidiSysExMessage(render_process_id); |
| 110 } |
| 111 |
| 112 pending_permissions_.erase(pending_permissions_.begin() + i); |
| 113 return; |
| 114 } |
| 115 } |
| 116 } |
| 117 |
| 118 } // namespace content |
| OLD | NEW |