- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / media / midi_host.cc
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
3 // found in the LICENSE file.
4
5 #include "content/browser/renderer_host/media/midi_host.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/debug/trace_event.h"
10 #include "base/process/process.h"
11 #include "content/browser/browser_main_loop.h"
12 #include "content/browser/child_process_security_policy_impl.h"
13 #include "content/browser/media/media_internals.h"
14 #include "content/common/media/midi_messages.h"
15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/browser/media_observer.h"
17 #include "content/public/browser/user_metrics.h"
18 #include "media/midi/midi_manager.h"
19
20 using media::MIDIManager;
21 using media::MIDIPortInfoList;
22
23 // The total number of bytes which we're allowed to send to the OS
24 // before knowing that they have been successfully sent.
25 static const size_t kMaxInFlightBytes = 10 * 1024 * 1024;  // 10 MB.
26
27 // 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 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 static const size_t kAcknowledgementThresholdBytes = 1024 * 1024;  // 1 MB.
32
33 static const uint8 kSysExMessage = 0xf0;
34
35 namespace content {
36
37 MIDIHost::MIDIHost(int renderer_process_id, media::MIDIManager* midi_manager)
38     : renderer_process_id_(renderer_process_id),
39       midi_manager_(midi_manager),
40       sent_bytes_in_flight_(0),
41       bytes_sent_since_last_acknowledgement_(0) {
42 }
43
44 MIDIHost::~MIDIHost() {
45   if (midi_manager_)
46     midi_manager_->EndSession(this);
47 }
48
49 void MIDIHost::OnDestruct() const {
50   BrowserThread::DeleteOnIOThread::Destruct(this);
51 }
52
53 ///////////////////////////////////////////////////////////////////////////////
54 // IPC Messages handler
55 bool MIDIHost::OnMessageReceived(const IPC::Message& message,
56                                  bool* message_was_ok) {
57   bool handled = true;
58   IPC_BEGIN_MESSAGE_MAP_EX(MIDIHost, message, *message_was_ok)
59     IPC_MESSAGE_HANDLER(MIDIHostMsg_StartSession, OnStartSession)
60     IPC_MESSAGE_HANDLER(MIDIHostMsg_SendData, OnSendData)
61     IPC_MESSAGE_UNHANDLED(handled = false)
62   IPC_END_MESSAGE_MAP_EX()
63
64   return handled;
65 }
66
67 void MIDIHost::OnStartSession(int client_id) {
68   MIDIPortInfoList input_ports;
69   MIDIPortInfoList output_ports;
70
71   // Initialize devices and register to receive MIDI data.
72   bool success = false;
73   if (midi_manager_) {
74     success = midi_manager_->StartSession(this);
75     if (success) {
76       input_ports = midi_manager_->input_ports();
77       output_ports = midi_manager_->output_ports();
78     }
79   }
80
81   Send(new MIDIMsg_SessionStarted(
82        client_id,
83        success,
84        input_ports,
85        output_ports));
86 }
87
88 void MIDIHost::OnSendData(uint32 port,
89                           const std::vector<uint8>& data,
90                           double timestamp) {
91   if (!midi_manager_)
92     return;
93
94   if (data.empty())
95     return;
96
97   base::AutoLock auto_lock(in_flight_lock_);
98
99   // Sanity check that we won't send too much.
100   if (sent_bytes_in_flight_ > kMaxInFlightBytes ||
101       data.size() > kMaxInFlightBytes ||
102       data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
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     }
115   }
116
117   midi_manager_->DispatchSendMIDIData(
118       this,
119       port,
120       data,
121       timestamp);
122
123   sent_bytes_in_flight_ += data.size();
124 }
125
126 void MIDIHost::ReceiveMIDIData(
127     uint32 port,
128     const uint8* data,
129     size_t length,
130     double timestamp) {
131   TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData");
132
133   // Check a process security policy to receive a system exclusive message.
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   }
143
144   // Send to the renderer.
145   std::vector<uint8> v(data, data + length);
146   Send(new MIDIMsg_DataReceived(port, v, timestamp));
147 }
148
149 void MIDIHost::AccumulateMIDIBytesSent(size_t n) {
150   {
151     base::AutoLock auto_lock(in_flight_lock_);
152     if (n <= sent_bytes_in_flight_)
153       sent_bytes_in_flight_ -= n;
154   }
155
156   if (bytes_sent_since_last_acknowledgement_ + n >=
157       bytes_sent_since_last_acknowledgement_)
158     bytes_sent_since_last_acknowledgement_ += n;
159
160   if (bytes_sent_since_last_acknowledgement_ >=
161       kAcknowledgementThresholdBytes) {
162     Send(new MIDIMsg_AcknowledgeSentData(
163         bytes_sent_since_last_acknowledgement_));
164     bytes_sent_since_last_acknowledgement_ = 0;
165   }
166 }
167
168 }  // namespace content