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

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

Issue 93583002: Revert 237558 "Use MIDIMessageQueue/IsValidWebMIDIData for MIDI ..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years 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 | Annotate | Revision Log
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"
20 #include "media/midi/midi_message_util.h"
21 19
22 using media::MIDIManager; 20 using media::MIDIManager;
23 using media::MIDIPortInfoList; 21 using media::MIDIPortInfoList;
24 22
25 namespace content {
26 namespace {
27
28 // The total number of bytes which we're allowed to send to the OS 23 // The total number of bytes which we're allowed to send to the OS
29 // before knowing that they have been successfully sent. 24 // before knowing that they have been successfully sent.
30 const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB. 25 static const size_t kMaxInFlightBytes = 10 * 1024 * 1024; // 10 MB.
31 26
32 // We keep track of the number of bytes successfully sent to 27 // We keep track of the number of bytes successfully sent to
33 // the hardware. Every once in a while we report back to the renderer 28 // the hardware. Every once in a while we report back to the renderer
34 // the number of bytes sent since the last report. This threshold determines 29 // the number of bytes sent since the last report. This threshold determines
35 // how many bytes will be sent before reporting back to the renderer. 30 // how many bytes will be sent before reporting back to the renderer.
36 const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. 31 static const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB.
37 32
38 const uint8 kSysExMessage = 0xf0; 33 static const uint8 kSysExMessage = 0xf0;
39 const uint8 kEndOfSysExMessage = 0xf7;
40 34
41 bool IsDataByte(uint8 data) { 35 namespace content {
42 return (data & 0x80) == 0;
43 }
44
45 bool IsSystemRealTimeMessage(uint8 data) {
46 return 0xf8 <= data && data <= 0xff;
47 }
48
49 } // namespace
50 36
51 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager) 37 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager)
52 : renderer_process_id_(renderer_process_id), 38 : renderer_process_id_(renderer_process_id),
53 has_sys_ex_permission_(false),
54 midi_manager_(midi_manager), 39 midi_manager_(midi_manager),
55 sent_bytes_in_flight_(0), 40 sent_bytes_in_flight_(0),
56 bytes_sent_since_last_acknowledgement_(0) { 41 bytes_sent_since_last_acknowledgement_(0) {
57 } 42 }
58 43
59 MIDIHost::~MIDIHost() { 44 MIDIHost::~MIDIHost() {
60 if (midi_manager_) 45 if (midi_manager_)
61 midi_manager_->EndSession(this); 46 midi_manager_->EndSession(this);
62 } 47 }
63 48
(...skipping 19 matching lines...) Expand all
83 MIDIPortInfoList input_ports; 68 MIDIPortInfoList input_ports;
84 MIDIPortInfoList output_ports; 69 MIDIPortInfoList output_ports;
85 70
86 // Initialize devices and register to receive MIDI data. 71 // Initialize devices and register to receive MIDI data.
87 bool success = false; 72 bool success = false;
88 if (midi_manager_) { 73 if (midi_manager_) {
89 success = midi_manager_->StartSession(this); 74 success = midi_manager_->StartSession(this);
90 if (success) { 75 if (success) {
91 input_ports = midi_manager_->input_ports(); 76 input_ports = midi_manager_->input_ports();
92 output_ports = midi_manager_->output_ports(); 77 output_ports = midi_manager_->output_ports();
93 received_messages_queues_.clear();
94 received_messages_queues_.resize(input_ports.size());
95 // ChildSecurityPolicy is set just before OnStartSession by
96 // MIDIDispatcherHost. So we can safely cache the policy.
97 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()->
98 CanSendMIDISysExMessage(renderer_process_id_);
99 } 78 }
100 } 79 }
101 80
102 Send(new MIDIMsg_SessionStarted( 81 Send(new MIDIMsg_SessionStarted(
103 client_id, 82 client_id,
104 success, 83 success,
105 input_ports, 84 input_ports,
106 output_ports)); 85 output_ports));
107 } 86 }
108 87
109 void MIDIHost::OnSendData(uint32 port, 88 void MIDIHost::OnSendData(uint32 port,
110 const std::vector<uint8>& data, 89 const std::vector<uint8>& data,
111 double timestamp) { 90 double timestamp) {
112 if (!midi_manager_) 91 if (!midi_manager_)
113 return; 92 return;
114 93
115 if (data.empty()) 94 if (data.empty())
116 return; 95 return;
117 96
118 // Blink running in a renderer checks permission to raise a SecurityError 97 base::AutoLock auto_lock(in_flight_lock_);
119 // in JavaScript. The actual permission check for security purposes 98
120 // happens here in the browser process. 99 // Sanity check that we won't send too much.
121 if (!has_sys_ex_permission_ && 100 if (sent_bytes_in_flight_ > kMaxInFlightBytes ||
122 (std::find(data.begin(), data.end(), kSysExMessage) != data.end())) { 101 data.size() > kMaxInFlightBytes ||
123 RecordAction(UserMetricsAction("BadMessageTerminate_MIDI")); 102 data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
124 BadMessageReceived();
125 return; 103 return;
104
105 if (data[0] >= kSysExMessage) {
106 // Blink running in a renderer checks permission to raise a SecurityError in
107 // JavaScript. The actual permission check for security perposes happens
108 // here in the browser process.
109 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage(
110 renderer_process_id_)) {
111 RecordAction(UserMetricsAction("BadMessageTerminate_MIDI"));
112 BadMessageReceived();
113 return;
114 }
126 } 115 }
127 116
128 if (!IsValidWebMIDIData(data)) 117 midi_manager_->DispatchSendMIDIData(
129 return; 118 this,
119 port,
120 data,
121 timestamp);
130 122
131 base::AutoLock auto_lock(in_flight_lock_);
132 // Sanity check that we won't send too much data.
133 // TODO(yukawa): Consider to send an error event back to the renderer
134 // after some future discussion in W3C.
135 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
136 return;
137 midi_manager_->DispatchSendMIDIData(this, port, data, timestamp);
138 sent_bytes_in_flight_ += data.size(); 123 sent_bytes_in_flight_ += data.size();
139 } 124 }
140 125
141 void MIDIHost::ReceiveMIDIData( 126 void MIDIHost::ReceiveMIDIData(
142 uint32 port, 127 uint32 port,
143 const uint8* data, 128 const uint8* data,
144 size_t length, 129 size_t length,
145 double timestamp) { 130 double timestamp) {
146 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData"); 131 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData");
147 132
148 if (received_messages_queues_.size() <= port) 133 // Check a process security policy to receive a system exclusive message.
149 return; 134 if (length > 0 && data[0] >= kSysExMessage) {
135 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage(
136 renderer_process_id_)) {
137 // MIDI devices may send a system exclusive messages even if the renderer
138 // doesn't have a permission to receive it. Don't kill the renderer as
139 // OnSendData() does.
140 return;
141 }
142 }
150 143
151 // Lazy initialization 144 // Send to the renderer.
152 if (received_messages_queues_[port] == NULL) 145 std::vector<uint8> v(data, data + length);
153 received_messages_queues_[port] = new media::MIDIMessageQueue(true); 146 Send(new MIDIMsg_DataReceived(port, v, timestamp));
154
155 received_messages_queues_[port]->Add(data, length);
156 std::vector<uint8> message;
157 while (true) {
158 received_messages_queues_[port]->Get(&message);
159 if (message.empty())
160 break;
161
162 // MIDI devices may send a system exclusive messages even if the renderer
163 // doesn't have a permission to receive it. Don't kill the renderer as
164 // OnSendData() does.
165 if (message[0] == kSysExMessage && !has_sys_ex_permission_)
166 continue;
167
168 // Send to the renderer.
169 Send(new MIDIMsg_DataReceived(port, message, timestamp));
170 }
171 } 147 }
172 148
173 void MIDIHost::AccumulateMIDIBytesSent(size_t n) { 149 void MIDIHost::AccumulateMIDIBytesSent(size_t n) {
174 { 150 {
175 base::AutoLock auto_lock(in_flight_lock_); 151 base::AutoLock auto_lock(in_flight_lock_);
176 if (n <= sent_bytes_in_flight_) 152 if (n <= sent_bytes_in_flight_)
177 sent_bytes_in_flight_ -= n; 153 sent_bytes_in_flight_ -= n;
178 } 154 }
179 155
180 if (bytes_sent_since_last_acknowledgement_ + n >= 156 if (bytes_sent_since_last_acknowledgement_ + n >=
181 bytes_sent_since_last_acknowledgement_) 157 bytes_sent_since_last_acknowledgement_)
182 bytes_sent_since_last_acknowledgement_ += n; 158 bytes_sent_since_last_acknowledgement_ += n;
183 159
184 if (bytes_sent_since_last_acknowledgement_ >= 160 if (bytes_sent_since_last_acknowledgement_ >=
185 kAcknowledgementThresholdBytes) { 161 kAcknowledgementThresholdBytes) {
186 Send(new MIDIMsg_AcknowledgeSentData( 162 Send(new MIDIMsg_AcknowledgeSentData(
187 bytes_sent_since_last_acknowledgement_)); 163 bytes_sent_since_last_acknowledgement_));
188 bytes_sent_since_last_acknowledgement_ = 0; 164 bytes_sent_since_last_acknowledgement_ = 0;
189 } 165 }
190 } 166 }
191 167
192 // static
193 bool MIDIHost::IsValidWebMIDIData(const std::vector<uint8>& data) {
194 bool in_sysex = false;
195 size_t waiting_data_length = 0;
196 for (size_t i = 0; i < data.size(); ++i) {
197 const uint8 current = data[i];
198 if (IsSystemRealTimeMessage(current))
199 continue; // Real time message can be placed at any point.
200 if (waiting_data_length > 0) {
201 if (!IsDataByte(current))
202 return false; // Error: |current| should have been data byte.
203 --waiting_data_length;
204 continue; // Found data byte as expected.
205 }
206 if (in_sysex) {
207 if (data[i] == kEndOfSysExMessage)
208 in_sysex = false;
209 else if (!IsDataByte(current))
210 return false; // Error: |current| should have been data byte.
211 continue; // Found data byte as expected.
212 }
213 if (current == kSysExMessage) {
214 in_sysex = true;
215 continue; // Found SysEX
216 }
217 waiting_data_length = media::GetMIDIMessageLength(current);
218 if (waiting_data_length == 0)
219 return false; // Error: |current| should have been a valid status byte.
220 --waiting_data_length; // Found status byte
221 }
222 return waiting_data_length == 0 && !in_sysex;
223 }
224
225 } // namespace content 168 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698