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

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

Issue 662853003: Manage MIDI related objects and sessions' lifecycles correctly (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: unittest Created 6 years, 2 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
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/media/midi_host.h" 5 #include "content/browser/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"
(...skipping 30 matching lines...) Expand all
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)
yhirano 2014/10/20 12:15:14 Please initialize |is_session_requested_|.
Takashi Toyoshima 2014/10/20 17:14:43 Oh, thanks! But, I'm surprised since there are no
52 : BrowserMessageFilter(MidiMsgStart), 52 : BrowserMessageFilter(MidiMsgStart),
53 renderer_process_id_(renderer_process_id), 53 renderer_process_id_(renderer_process_id),
54 has_sys_ex_permission_(false), 54 has_sys_ex_permission_(false),
55 midi_manager_(midi_manager), 55 midi_manager_(midi_manager),
56 sent_bytes_in_flight_(0), 56 sent_bytes_in_flight_(0),
57 bytes_sent_since_last_acknowledgement_(0) { 57 bytes_sent_since_last_acknowledgement_(0) {
58 CHECK(midi_manager_);
58 } 59 }
59 60
60 MidiHost::~MidiHost() { 61 MidiHost::~MidiHost() {
61 if (midi_manager_) 62 // Close an open session, or abort opening a session.
63 if (is_session_requested_)
62 midi_manager_->EndSession(this); 64 midi_manager_->EndSession(this);
63 } 65 }
64 66
65 void MidiHost::OnDestruct() const { 67 void MidiHost::OnDestruct() const {
66 BrowserThread::DeleteOnIOThread::Destruct(this); 68 BrowserThread::DeleteOnIOThread::Destruct(this);
67 } 69 }
68 70
69 // IPC Messages handler 71 // IPC Messages handler
70 bool MidiHost::OnMessageReceived(const IPC::Message& message) { 72 bool MidiHost::OnMessageReceived(const IPC::Message& message) {
71 bool handled = true; 73 bool handled = true;
72 IPC_BEGIN_MESSAGE_MAP(MidiHost, message) 74 IPC_BEGIN_MESSAGE_MAP(MidiHost, message)
73 IPC_MESSAGE_HANDLER(MidiHostMsg_StartSession, OnStartSession) 75 IPC_MESSAGE_HANDLER(MidiHostMsg_StartSession, OnStartSession)
74 IPC_MESSAGE_HANDLER(MidiHostMsg_SendData, OnSendData) 76 IPC_MESSAGE_HANDLER(MidiHostMsg_SendData, OnSendData)
77 IPC_MESSAGE_HANDLER(MidiHostMsg_EndSession, OnEndSession)
75 IPC_MESSAGE_UNHANDLED(handled = false) 78 IPC_MESSAGE_UNHANDLED(handled = false)
76 IPC_END_MESSAGE_MAP() 79 IPC_END_MESSAGE_MAP()
77 80
78 return handled; 81 return handled;
79 } 82 }
80 83
81 void MidiHost::OnStartSession(int client_id) { 84 void MidiHost::OnStartSession() {
82 if (midi_manager_) 85 is_session_requested_ = true;
83 midi_manager_->StartSession(this, client_id); 86 midi_manager_->StartSession(this);
84 } 87 }
85 88
86 void MidiHost::OnSendData(uint32 port, 89 void MidiHost::OnSendData(uint32 port,
87 const std::vector<uint8>& data, 90 const std::vector<uint8>& data,
88 double timestamp) { 91 double timestamp) {
89 if (!midi_manager_)
90 return;
91
92 if (data.empty()) 92 if (data.empty())
93 return; 93 return;
94 94
95 // Blink running in a renderer checks permission to raise a SecurityError 95 // Blink running in a renderer checks permission to raise a SecurityError
96 // in JavaScript. The actual permission check for security purposes 96 // in JavaScript. The actual permission check for security purposes
97 // happens here in the browser process. 97 // happens here in the browser process.
98 if (!has_sys_ex_permission_ && 98 if (!has_sys_ex_permission_ &&
99 std::find(data.begin(), data.end(), kSysExByte) != data.end()) { 99 std::find(data.begin(), data.end(), kSysExByte) != data.end()) {
100 RecordAction(base::UserMetricsAction("BadMessageTerminate_MIDI")); 100 RecordAction(base::UserMetricsAction("BadMessageTerminate_MIDI"));
101 BadMessageReceived(); 101 BadMessageReceived();
102 return; 102 return;
103 } 103 }
104 104
105 if (!IsValidWebMIDIData(data)) 105 if (!IsValidWebMIDIData(data))
106 return; 106 return;
107 107
108 { 108 {
109 base::AutoLock auto_lock(in_flight_lock_); 109 base::AutoLock auto_lock(in_flight_lock_);
110 // Sanity check that we won't send too much data. 110 // Sanity check that we won't send too much data.
111 // TODO(yukawa): Consider to send an error event back to the renderer 111 // TODO(yukawa): Consider to send an error event back to the renderer
112 // after some future discussion in W3C. 112 // after some future discussion in W3C.
113 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes) 113 if (data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
114 return; 114 return;
115 sent_bytes_in_flight_ += data.size(); 115 sent_bytes_in_flight_ += data.size();
116 } 116 }
117 midi_manager_->DispatchSendMidiData(this, port, data, timestamp); 117 midi_manager_->DispatchSendMidiData(this, port, data, timestamp);
118 } 118 }
119 119
120 void MidiHost::CompleteStartSession(int client_id, media::MidiResult result) { 120 void MidiHost::OnEndSession() {
121 is_session_requested_ = false;
122 midi_manager_->EndSession(this);
123 }
124
125 void MidiHost::CompleteStartSession(media::MidiResult result) {
126 DCHECK(is_session_requested_);
121 MidiPortInfoList input_ports; 127 MidiPortInfoList input_ports;
122 MidiPortInfoList output_ports; 128 MidiPortInfoList output_ports;
123 129
124 if (result == media::MIDI_OK) { 130 if (result == media::MIDI_OK) {
125 input_ports = midi_manager_->input_ports(); 131 input_ports = midi_manager_->input_ports();
126 output_ports = midi_manager_->output_ports(); 132 output_ports = midi_manager_->output_ports();
127 received_messages_queues_.clear(); 133 received_messages_queues_.clear();
128 received_messages_queues_.resize(input_ports.size()); 134 received_messages_queues_.resize(input_ports.size());
129 // ChildSecurityPolicy is set just before OnStartSession by 135 // ChildSecurityPolicy is set just before OnStartSession by
130 // MidiDispatcherHost. So we can safely cache the policy. 136 // MidiDispatcherHost. So we can safely cache the policy.
131 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()-> 137 has_sys_ex_permission_ = ChildProcessSecurityPolicyImpl::GetInstance()->
132 CanSendMidiSysExMessage(renderer_process_id_); 138 CanSendMidiSysExMessage(renderer_process_id_);
133 } 139 }
134 140
135 Send(new MidiMsg_SessionStarted(client_id, 141 Send(new MidiMsg_SessionStarted(result,
136 result,
137 input_ports, 142 input_ports,
138 output_ports)); 143 output_ports));
139 } 144 }
140 145
141 void MidiHost::ReceiveMidiData( 146 void MidiHost::ReceiveMidiData(
142 uint32 port, 147 uint32 port,
143 const uint8* data, 148 const uint8* data,
144 size_t length, 149 size_t length,
145 double timestamp) { 150 double timestamp) {
146 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData"); 151 TRACE_EVENT0("midi", "MidiHost::ReceiveMidiData");
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 } 221 }
217 waiting_data_length = media::GetMidiMessageLength(current); 222 waiting_data_length = media::GetMidiMessageLength(current);
218 if (waiting_data_length == 0) 223 if (waiting_data_length == 0)
219 return false; // Error: |current| should have been a valid status byte. 224 return false; // Error: |current| should have been a valid status byte.
220 --waiting_data_length; // Found status byte 225 --waiting_data_length; // Found status byte
221 } 226 }
222 return waiting_data_length == 0 && !in_sysex; 227 return waiting_data_length == 0 && !in_sysex;
223 } 228 }
224 229
225 } // namespace content 230 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698