OLD | NEW |
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_util.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 { |
36 | 37 |
37 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager) | 38 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager) |
38 : renderer_process_id_(renderer_process_id), | 39 : renderer_process_id_(renderer_process_id), |
| 40 has_sys_ex_permission_(false), |
39 midi_manager_(midi_manager), | 41 midi_manager_(midi_manager), |
40 sent_bytes_in_flight_(0), | 42 sent_bytes_in_flight_(0), |
41 bytes_sent_since_last_acknowledgement_(0) { | 43 bytes_sent_since_last_acknowledgement_(0) { |
42 } | 44 } |
43 | 45 |
44 MIDIHost::~MIDIHost() { | 46 MIDIHost::~MIDIHost() { |
45 if (midi_manager_) | 47 if (midi_manager_) |
46 midi_manager_->EndSession(this); | 48 midi_manager_->EndSession(this); |
47 } | 49 } |
48 | 50 |
(...skipping 19 matching lines...) Expand all Loading... |
68 MIDIPortInfoList input_ports; | 70 MIDIPortInfoList input_ports; |
69 MIDIPortInfoList output_ports; | 71 MIDIPortInfoList output_ports; |
70 | 72 |
71 // Initialize devices and register to receive MIDI data. | 73 // Initialize devices and register to receive MIDI data. |
72 bool success = false; | 74 bool success = false; |
73 if (midi_manager_) { | 75 if (midi_manager_) { |
74 success = midi_manager_->StartSession(this); | 76 success = midi_manager_->StartSession(this); |
75 if (success) { | 77 if (success) { |
76 input_ports = midi_manager_->input_ports(); | 78 input_ports = midi_manager_->input_ports(); |
77 output_ports = midi_manager_->output_ports(); | 79 output_ports = midi_manager_->output_ports(); |
| 80 received_messages_queues_.clear(); |
| 81 received_messages_queues_.resize(input_ports.size()); |
| 82 // ChildSecurityPolicy is set just before OnStartSession by |
| 83 // MIDIDispatcherHost. So we can safely cache the policy. |
| 84 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()-> |
| 85 CanSendMIDISysExMessage(renderer_process_id_); |
78 } | 86 } |
79 } | 87 } |
80 | 88 |
81 Send(new MIDIMsg_SessionStarted( | 89 Send(new MIDIMsg_SessionStarted( |
82 client_id, | 90 client_id, |
83 success, | 91 success, |
84 input_ports, | 92 input_ports, |
85 output_ports)); | 93 output_ports)); |
86 } | 94 } |
87 | 95 |
88 void MIDIHost::OnSendData(uint32 port, | 96 void MIDIHost::OnSendData(uint32 port, |
89 const std::vector<uint8>& data, | 97 const std::vector<uint8>& data, |
90 double timestamp) { | 98 double timestamp) { |
91 if (!midi_manager_) | 99 if (!midi_manager_) |
92 return; | 100 return; |
93 | 101 |
94 if (data.empty()) | 102 if (data.empty()) |
95 return; | 103 return; |
96 | 104 |
97 base::AutoLock auto_lock(in_flight_lock_); | 105 // Blink running in a renderer checks permission to raise a SecurityError |
| 106 // in JavaScript. The actual permission check for security purposes |
| 107 // happens here in the browser process. |
| 108 if (!has_sys_ex_permission_ && |
| 109 (std::find(data.begin(), data.end(), kSysExMessage) != data.end())) { |
| 110 RecordAction(UserMetricsAction("BadMessageTerminate_MIDI")); |
| 111 BadMessageReceived(); |
| 112 return; |
| 113 } |
98 | 114 |
99 // Sanity check that we won't send too much. | 115 if (!media::IsValidWebMIDIMessage(data)) |
100 if (sent_bytes_in_flight_ > kMaxInFlightBytes || | |
101 data.size() > kMaxInFlightBytes || | |
102 data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes) | |
103 return; | 116 return; |
104 | 117 |
105 if (data[0] >= kSysExMessage) { | 118 base::AutoLock auto_lock(in_flight_lock_); |
106 // Blink running in a renderer checks permission to raise a SecurityError in | 119 // Sanity check that we won't send too much data. |
107 // JavaScript. The actual permission check for security perposes happens | 120 // TODO(yukawa): Consider to send an error event back to the renderer |
108 // here in the browser process. | 121 // after some future discussion in W3C. |
109 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage( | 122 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes) |
110 renderer_process_id_)) { | 123 return; |
111 RecordAction(UserMetricsAction("BadMessageTerminate_MIDI")); | 124 midi_manager_->DispatchSendMIDIData(this, port, data, timestamp); |
112 BadMessageReceived(); | |
113 return; | |
114 } | |
115 } | |
116 | |
117 midi_manager_->DispatchSendMIDIData( | |
118 this, | |
119 port, | |
120 data, | |
121 timestamp); | |
122 | |
123 sent_bytes_in_flight_ += data.size(); | 125 sent_bytes_in_flight_ += data.size(); |
124 } | 126 } |
125 | 127 |
126 void MIDIHost::ReceiveMIDIData( | 128 void MIDIHost::ReceiveMIDIData( |
127 uint32 port, | 129 uint32 port, |
128 const uint8* data, | 130 const uint8* data, |
129 size_t length, | 131 size_t length, |
130 double timestamp) { | 132 double timestamp) { |
131 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData"); | 133 TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData"); |
132 | 134 |
133 // Check a process security policy to receive a system exclusive message. | 135 if (received_messages_queues_.size() <= port) |
134 if (length > 0 && data[0] >= kSysExMessage) { | 136 return; |
135 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanSendMIDISysExMessage( | 137 |
136 renderer_process_id_)) { | 138 // Lazy initialization |
137 // MIDI devices may send a system exclusive messages even if the renderer | 139 if (received_messages_queues_[port] == NULL) |
138 // doesn't have a permission to receive it. Don't kill the renderer as | 140 received_messages_queues_[port] = new media::MIDIMessageQueue(true); |
139 // OnSendData() does. | 141 |
140 return; | 142 received_messages_queues_[port]->Add(data, length); |
141 } | 143 std::vector<uint8> message; |
| 144 while (true) { |
| 145 received_messages_queues_[port]->Get(&message); |
| 146 if (message.empty()) |
| 147 break; |
| 148 |
| 149 // MIDI devices may send a system exclusive messages even if the renderer |
| 150 // doesn't have a permission to receive it. Don't kill the renderer as |
| 151 // OnSendData() does. |
| 152 if (message[0] == kSysExMessage && !has_sys_ex_permission_) |
| 153 continue; |
| 154 |
| 155 // Send to the renderer. |
| 156 Send(new MIDIMsg_DataReceived(port, message, timestamp)); |
142 } | 157 } |
143 | |
144 // Send to the renderer. | |
145 std::vector<uint8> v(data, data + length); | |
146 Send(new MIDIMsg_DataReceived(port, v, timestamp)); | |
147 } | 158 } |
148 | 159 |
149 void MIDIHost::AccumulateMIDIBytesSent(size_t n) { | 160 void MIDIHost::AccumulateMIDIBytesSent(size_t n) { |
150 { | 161 { |
151 base::AutoLock auto_lock(in_flight_lock_); | 162 base::AutoLock auto_lock(in_flight_lock_); |
152 if (n <= sent_bytes_in_flight_) | 163 if (n <= sent_bytes_in_flight_) |
153 sent_bytes_in_flight_ -= n; | 164 sent_bytes_in_flight_ -= n; |
154 } | 165 } |
155 | 166 |
156 if (bytes_sent_since_last_acknowledgement_ + n >= | 167 if (bytes_sent_since_last_acknowledgement_ + n >= |
157 bytes_sent_since_last_acknowledgement_) | 168 bytes_sent_since_last_acknowledgement_) |
158 bytes_sent_since_last_acknowledgement_ += n; | 169 bytes_sent_since_last_acknowledgement_ += n; |
159 | 170 |
160 if (bytes_sent_since_last_acknowledgement_ >= | 171 if (bytes_sent_since_last_acknowledgement_ >= |
161 kAcknowledgementThresholdBytes) { | 172 kAcknowledgementThresholdBytes) { |
162 Send(new MIDIMsg_AcknowledgeSentData( | 173 Send(new MIDIMsg_AcknowledgeSentData( |
163 bytes_sent_since_last_acknowledgement_)); | 174 bytes_sent_since_last_acknowledgement_)); |
164 bytes_sent_since_last_acknowledgement_ = 0; | 175 bytes_sent_since_last_acknowledgement_ = 0; |
165 } | 176 } |
166 } | 177 } |
167 | 178 |
168 } // namespace content | 179 } // namespace content |
OLD | NEW |