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

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 in MIDIHost::OnSendData as well as MIDIHost::ReceiveMIDIData 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
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
29 // the number of bytes sent since the last report. This threshold determines 30 // the number of bytes sent since the last report. This threshold determines
30 // how many bytes will be sent before reporting back to the renderer. 31 // how many bytes will be sent before reporting back to the renderer.
31 static const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB. 32 static const size_t kAcknowledgementThresholdBytes = 1024 * 1024; // 1 MB.
32 33
33 static const uint8 kSysExMessage = 0xf0; 34 static const uint8 kSysExMessage = 0xf0;
34 35
35 namespace content { 36 namespace content {
37 namespace {
38
39 // A class to check if current child process security policy allows the the
40 // renderer to use SysEx MIDI messages or not. This class fetches the policy
41 // lazily and caches the result to avoid redundant access check.
42 class CanSendMIDISysExMessageQuery {
43 public:
44 explicit CanSendMIDISysExMessageQuery(int renderer_process_id)
45 : renderer_process_id_(renderer_process_id),
46 value_(false),
47 initialized_(false) {
48 }
49
50 bool get() {
51 if (!initialized_) {
52 value_ = ChildProcessSecurityPolicyImpl::GetInstance()->
53 CanSendMIDISysExMessage(renderer_process_id_);
54 initialized_ = true;
55 }
56 return value_;
57 }
58
59 private:
60 const int renderer_process_id_;
61 bool value_;
62 bool initialized_;
63
64 DISALLOW_COPY_AND_ASSIGN(CanSendMIDISysExMessageQuery);
65 };
66
67 } // namespace
36 68
37 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager) 69 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager)
38 : renderer_process_id_(renderer_process_id), 70 : renderer_process_id_(renderer_process_id),
39 midi_manager_(midi_manager), 71 midi_manager_(midi_manager),
40 sent_bytes_in_flight_(0), 72 sent_bytes_in_flight_(0),
41 bytes_sent_since_last_acknowledgement_(0) { 73 bytes_sent_since_last_acknowledgement_(0) {
42 } 74 }
43 75
44 MIDIHost::~MIDIHost() { 76 MIDIHost::~MIDIHost() {
45 if (midi_manager_) 77 if (midi_manager_)
(...skipping 22 matching lines...) Expand all
68 MIDIPortInfoList input_ports; 100 MIDIPortInfoList input_ports;
69 MIDIPortInfoList output_ports; 101 MIDIPortInfoList output_ports;
70 102
71 // Initialize devices and register to receive MIDI data. 103 // Initialize devices and register to receive MIDI data.
72 bool success = false; 104 bool success = false;
73 if (midi_manager_) { 105 if (midi_manager_) {
74 success = midi_manager_->StartSession(this); 106 success = midi_manager_->StartSession(this);
75 if (success) { 107 if (success) {
76 input_ports = midi_manager_->input_ports(); 108 input_ports = midi_manager_->input_ports();
77 output_ports = midi_manager_->output_ports(); 109 output_ports = midi_manager_->output_ports();
110 received_messages_queues_.clear();
111 received_messages_queues_.resize(input_ports.size());
78 } 112 }
79 } 113 }
80 114
81 Send(new MIDIMsg_SessionStarted( 115 Send(new MIDIMsg_SessionStarted(
82 client_id, 116 client_id,
83 success, 117 success,
84 input_ports, 118 input_ports,
85 output_ports)); 119 output_ports));
86 } 120 }
87 121
88 void MIDIHost::OnSendData(uint32 port, 122 void MIDIHost::OnSendData(uint32 port,
89 const std::vector<uint8>& data, 123 const std::vector<uint8>& data,
90 double timestamp) { 124 double timestamp) {
91 if (!midi_manager_) 125 if (!midi_manager_)
92 return; 126 return;
93 127
94 if (data.empty()) 128 if (data.empty())
95 return; 129 return;
96 130
97 base::AutoLock auto_lock(in_flight_lock_); 131 CanSendMIDISysExMessageQuery can_send_sys_ex(renderer_process_id_);
Takashi Toyoshima 2013/11/19 01:08:37 Can we make it a member variable? IIRC, this code
yukawa 2013/11/19 09:41:41 Hmm, I think the construction cost of |can_send_sy
Takashi Toyoshima 2013/11/19 12:49:59 I see. It's ok to keep this code as is. FYI, Chil
98 132
99 // Sanity check that we won't send too much. 133 // Note: In the spec, |data| can be any number of complete MIDI messages.
100 if (sent_bytes_in_flight_ > kMaxInFlightBytes || 134 // Running status is disallowed explicitly.
101 data.size() > kMaxInFlightBytes || 135 media::MIDIMessageQueue message_queue(false);
102 data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes) 136 message_queue.Add(data);
103 return; 137 std::vector<uint8> message;
138 while (true) {
139 message_queue.Get(&message);
140 if (message.empty())
141 break;
104 142
105 if (data[0] >= kSysExMessage) { 143 // Sanity check that we won't send too large message.
Takashi Toyoshima 2013/11/19 01:08:37 Here, we concern the buffer size which may be allo
yukawa 2013/11/19 09:41:41 Done.
106 // Blink running in a renderer checks permission to raise a SecurityError in 144 if (message.size() > kMaxInFlightBytes)
Takashi Toyoshima 2013/11/19 01:08:37 This check looks redundant.
yukawa 2013/11/19 09:41:41 I put this condition here as an early exit route b
107 // JavaScript. The actual permission check for security perposes happens 145 continue;
108 // here in the browser process. 146
109 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage( 147 if (message[0] == kSysExMessage && !can_send_sys_ex.get()) {
110 renderer_process_id_)) { 148 // Blink running in a renderer checks permission to raise a SecurityError
149 // in JavaScript. The actual permission check for security purposes
150 // happens here in the browser process.
111 RecordAction(UserMetricsAction("BadMessageTerminate_MIDI")); 151 RecordAction(UserMetricsAction("BadMessageTerminate_MIDI"));
112 BadMessageReceived(); 152 BadMessageReceived();
113 return; 153 return;
114 } 154 }
155
156 base::AutoLock auto_lock(in_flight_lock_);
157 // Sanity check that we won't send too much data.
158 if (sent_bytes_in_flight_ > kMaxInFlightBytes ||
159 message.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
Takashi Toyoshima 2013/11/19 01:08:37 Same TODO here, and any reason to check twice? I f
yukawa 2013/11/19 09:41:41 Done. BTW, the redundancy came from the original c
Takashi Toyoshima 2013/11/19 12:49:59 lol
160 return;
161 midi_manager_->DispatchSendMIDIData(this, port, message, timestamp);
162 sent_bytes_in_flight_ += message.size();
115 } 163 }
116
117 midi_manager_->DispatchSendMIDIData(
118 this,
119 port,
120 data,
121 timestamp);
122
123 sent_bytes_in_flight_ += data.size();
124 } 164 }
125 165
126 void MIDIHost::ReceiveMIDIData( 166 void MIDIHost::ReceiveMIDIData(
127 uint32 port, 167 uint32 port,
128 const uint8* data, 168 const uint8* data,
129 size_t length, 169 size_t length,
130 double timestamp) { 170 double timestamp) {
131 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData"); 171 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData");
132 172
133 // Check a process security policy to receive a system exclusive message. 173 if (received_messages_queues_.size() <= port)
134 if (length > 0 && data[0] >= kSysExMessage) { 174 return;
135 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage( 175
136 renderer_process_id_)) { 176 // Lazy initialization
137 // MIDI devices may send a system exclusive messages even if the renderer 177 if (received_messages_queues_[port] == NULL)
138 // doesn't have a permission to receive it. Don't kill the renderer as 178 received_messages_queues_[port] = new media::MIDIMessageQueue(true);
139 // OnSendData() does. 179
140 return; 180 CanSendMIDISysExMessageQuery can_send_sys_ex(renderer_process_id_);
Takashi Toyoshima 2013/11/19 01:08:37 ditto. I think we can share the same instance betw
yukawa 2013/11/19 09:41:41 See my previous comment.
Takashi Toyoshima 2013/11/19 12:49:59 ok
141 } 181
182 received_messages_queues_[port]->Add(data, length);
183 std::vector<uint8> message;
184 while (true) {
185 received_messages_queues_[port]->Get(&message);
186 if (message.empty())
187 break;
188
189 // MIDI devices may send a system exclusive messages even if the renderer
190 // doesn't have a permission to receive it. Don't kill the renderer as
191 // OnSendData() does.
192 if (message[0] == kSysExMessage && !can_send_sys_ex.get())
193 continue;
194
195 // Send to the renderer.
196 Send(new MIDIMsg_DataReceived(port, message, timestamp));
142 } 197 }
143
144 // Send to the renderer.
145 std::vector<uint8> v(data, data + length);
146 Send(new MIDIMsg_DataReceived(port, v, timestamp));
147 } 198 }
148 199
149 void MIDIHost::AccumulateMIDIBytesSent(size_t n) { 200 void MIDIHost::AccumulateMIDIBytesSent(size_t n) {
150 { 201 {
151 base::AutoLock auto_lock(in_flight_lock_); 202 base::AutoLock auto_lock(in_flight_lock_);
152 if (n <= sent_bytes_in_flight_) 203 if (n <= sent_bytes_in_flight_)
153 sent_bytes_in_flight_ -= n; 204 sent_bytes_in_flight_ -= n;
154 } 205 }
155 206
156 if (bytes_sent_since_last_acknowledgement_ + n >= 207 if (bytes_sent_since_last_acknowledgement_ + n >=
157 bytes_sent_since_last_acknowledgement_) 208 bytes_sent_since_last_acknowledgement_)
158 bytes_sent_since_last_acknowledgement_ += n; 209 bytes_sent_since_last_acknowledgement_ += n;
159 210
160 if (bytes_sent_since_last_acknowledgement_ >= 211 if (bytes_sent_since_last_acknowledgement_ >=
161 kAcknowledgementThresholdBytes) { 212 kAcknowledgementThresholdBytes) {
162 Send(new MIDIMsg_AcknowledgeSentData( 213 Send(new MIDIMsg_AcknowledgeSentData(
163 bytes_sent_since_last_acknowledgement_)); 214 bytes_sent_since_last_acknowledgement_));
164 bytes_sent_since_last_acknowledgement_ = 0; 215 bytes_sent_since_last_acknowledgement_ = 0;
165 } 216 }
166 } 217 }
167 218
168 } // namespace content 219 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698