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

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

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

Powered by Google App Engine
This is Rietveld 408576698