Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: content/browser/renderer_host/media/midi_host.cc

Issue 68353002: Use MIDIMessageQueue/IsValidWebMIDIData for MIDI byte stream validation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use MIDIMessageQueue from MIDIHost Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/renderer_host/media/midi_host.h ('k') | media/media.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/media/midi_host.h" 5 #include "content/browser/renderer_host/media/midi_host.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/process/process.h" 10 #include "base/process/process.h"
11 #include "content/browser/browser_main_loop.h" 11 #include "content/browser/browser_main_loop.h"
12 #include "content/browser/child_process_security_policy_impl.h" 12 #include "content/browser/child_process_security_policy_impl.h"
13 #include "content/browser/media/media_internals.h" 13 #include "content/browser/media/media_internals.h"
14 #include "content/common/media/midi_messages.h" 14 #include "content/common/media/midi_messages.h"
15 #include "content/public/browser/content_browser_client.h" 15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/browser/media_observer.h" 16 #include "content/public/browser/media_observer.h"
17 #include "content/public/browser/user_metrics.h" 17 #include "content/public/browser/user_metrics.h"
18 #include "media/midi/midi_manager.h" 18 #include "media/midi/midi_manager.h"
19 #include "media/midi/midi_message_queue.h"
19 20
20 using media::MIDIManager; 21 using media::MIDIManager;
21 using media::MIDIPortInfoList; 22 using media::MIDIPortInfoList;
22 23
23 // The total number of bytes which we're allowed to send to the OS 24 // The total number of bytes which we're allowed to send to the OS
24 // before knowing that they have been successfully sent. 25 // before knowing that they have been successfully sent.
25 static const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. 26 static const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB.
26 27
27 // We keep track of the number of bytes successfully sent to 28 // We keep track of the number of bytes successfully sent to
28 // the hardware. Every once in a while we report back to the renderer 29 // the hardware. Every once in a while we report back to the renderer
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 MIDIPortInfoList input_ports; 69 MIDIPortInfoList input_ports;
69 MIDIPortInfoList output_ports; 70 MIDIPortInfoList output_ports;
70 71
71 // Initialize devices and register to receive MIDI data. 72 // Initialize devices and register to receive MIDI data.
72 bool success = false; 73 bool success = false;
73 if (midi_manager_) { 74 if (midi_manager_) {
74 success = midi_manager_->StartSession(this); 75 success = midi_manager_->StartSession(this);
75 if (success) { 76 if (success) {
76 input_ports = midi_manager_->input_ports(); 77 input_ports = midi_manager_->input_ports();
77 output_ports = midi_manager_->output_ports(); 78 output_ports = midi_manager_->output_ports();
79 received_messages_queues_.clear();
80 received_messages_queues_.resize(input_ports.size());
78 } 81 }
79 } 82 }
80 83
81 Send(new MIDIMsg_SessionStarted( 84 Send(new MIDIMsg_SessionStarted(
82 client_id, 85 client_id,
83 success, 86 success,
84 input_ports, 87 input_ports,
85 output_ports)); 88 output_ports));
86 } 89 }
87 90
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 sent_bytes_in_flight_ += data.size(); 126 sent_bytes_in_flight_ += data.size();
124 } 127 }
125 128
126 void MIDIHost::ReceiveMIDIData( 129 void MIDIHost::ReceiveMIDIData(
127 uint32 port, 130 uint32 port,
128 const uint8* data, 131 const uint8* data,
129 size_t length, 132 size_t length,
130 double timestamp) { 133 double timestamp) {
131 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData"); 134 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData");
132 135
133 // Check a process security policy to receive a system exclusive message. 136 if (received_messages_queues_.size() <= port)
134 if (length > 0 && data[0] >= kSysExMessage) { 137 return;
135 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage( 138
136 renderer_process_id_)) { 139 // Lazy initialization
137 // MIDI devices may send a system exclusive messages even if the renderer 140 if (received_messages_queues_[port] == NULL)
138 // doesn't have a permission to receive it. Don't kill the renderer as 141 received_messages_queues_[port] = new media::MIDIMessageQueue(true);
139 // OnSendData() does. 142
140 return; 143 bool sys_ex_allowed = false;
144 bool sys_ex_allowed_initialized = false;
145
146 received_messages_queues_[port]->Add(data, length);
147 std::vector<uint8> message;
148 while (true) {
149 received_messages_queues_[port]->Get(&message);
150 if (message.empty())
151 break;
152
153 // MIDI devices may send a system exclusive messages even if the renderer
154 // doesn't have a permission to receive it. Don't kill the renderer as
155 // OnSendData() does.
156 if (message[0] == kSysExMessage) {
yukawa 2013/11/14 14:55:31 This condition is slightly different from the prev
Takashi Toyoshima 2013/11/19 01:08:37 I chatted this with Chris, and double checked that
157 // Lazily check if SysEx is allowed or not in case checking this is
158 // costly.
159 if (!sys_ex_allowed_initialized) {
160 sys_ex_allowed = ChildProcessSecurityPolicyImpl::GetInstance()->
161 CanSendMIDISysExMessage(renderer_process_id_);
162 sys_ex_allowed_initialized = true;
163 }
164 if (!sys_ex_allowed)
165 continue;
141 } 166 }
167
168 // Send to the renderer.
169 Send(new MIDIMsg_DataReceived(port, message, timestamp));
142 } 170 }
143
144 // Send to the renderer.
145 std::vector<uint8> v(data, data + length);
146 Send(new MIDIMsg_DataReceived(port, v, timestamp));
147 } 171 }
148 172
149 void MIDIHost::AccumulateMIDIBytesSent(size_t n) { 173 void MIDIHost::AccumulateMIDIBytesSent(size_t n) {
150 { 174 {
151 base::AutoLock auto_lock(in_flight_lock_); 175 base::AutoLock auto_lock(in_flight_lock_);
152 if (n <= sent_bytes_in_flight_) 176 if (n <= sent_bytes_in_flight_)
153 sent_bytes_in_flight_ -= n; 177 sent_bytes_in_flight_ -= n;
154 } 178 }
155 179
156 if (bytes_sent_since_last_acknowledgement_ + n >= 180 if (bytes_sent_since_last_acknowledgement_ + n >=
157 bytes_sent_since_last_acknowledgement_) 181 bytes_sent_since_last_acknowledgement_)
158 bytes_sent_since_last_acknowledgement_ += n; 182 bytes_sent_since_last_acknowledgement_ += n;
159 183
160 if (bytes_sent_since_last_acknowledgement_ >= 184 if (bytes_sent_since_last_acknowledgement_ >=
161 kAcknowledgementThresholdBytes) { 185 kAcknowledgementThresholdBytes) {
162 Send(new MIDIMsg_AcknowledgeSentData( 186 Send(new MIDIMsg_AcknowledgeSentData(
163 bytes_sent_since_last_acknowledgement_)); 187 bytes_sent_since_last_acknowledgement_));
164 bytes_sent_since_last_acknowledgement_ = 0; 188 bytes_sent_since_last_acknowledgement_ = 0;
165 } 189 }
166 } 190 }
167 191
168 } // namespace content 192 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/renderer_host/media/midi_host.h ('k') | media/media.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698