Index: content/renderer/media/midi_message_filter.h |
diff --git a/content/renderer/media/midi_message_filter.h b/content/renderer/media/midi_message_filter.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5f9ec5896fc2b23efde4bfdbc52828db3ceaa0f5 |
--- /dev/null |
+++ b/content/renderer/media/midi_message_filter.h |
@@ -0,0 +1,116 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ |
+#define CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ |
+ |
+#include <map> |
+#include <vector> |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "content/common/content_export.h" |
+#include "ipc/ipc_channel_proxy.h" |
+#include "media/midi/midi_port_info.h" |
+#include "third_party/WebKit/public/platform/WebMIDIAccessor.h" |
+#include "third_party/WebKit/public/platform/WebMIDIAccessorClient.h" |
+ |
+namespace base { |
+class MessageLoopProxy; |
+} |
+ |
+namespace content { |
+ |
+// MessageFilter that handles MIDI messages. |
+class CONTENT_EXPORT MIDIMessageFilter |
+ : public IPC::ChannelProxy::MessageFilter { |
+ public: |
+ explicit MIDIMessageFilter( |
+ const scoped_refptr<base::MessageLoopProxy>& io_message_loop); |
+ |
+ // Each client registers for MIDI access here. |
+ // If permission is granted, then the client's |
+ // addInputPort() and addOutputPort() methods will be called, |
+ // giving the client access to receive and send data. |
+ void RequestAccess(WebKit::WebMIDIAccessorClient* client, int access); |
+ void RemoveClient(WebKit::WebMIDIAccessorClient* client); |
+ |
+ // A client will only be able to call this method if it has a suitable |
+ // output port (from addOutputPort()). |
+ void SendMIDIData(int port_index, |
+ const uint8* data, |
+ size_t length, |
+ double timestamp); |
+ |
+ // IO message loop associated with this message filter. |
+ scoped_refptr<base::MessageLoopProxy> io_message_loop() const { |
+ return io_message_loop_; |
+ } |
+ |
+ protected: |
+ virtual ~MIDIMessageFilter(); |
+ |
+ private: |
+ // Sends an IPC message using |channel_|. |
+ void Send(IPC::Message* message); |
+ |
+ // IPC::ChannelProxy::MessageFilter override. Called on |io_message_loop|. |
+ virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
+ virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE; |
+ virtual void OnFilterRemoved() OVERRIDE; |
+ virtual void OnChannelClosing() OVERRIDE; |
+ |
+ // Called when the browser process has approved (or denied) access to |
+ // MIDI hardware. |
+ void OnAccessApproved(int client_id, |
+ int access, |
+ bool success, |
+ media::MIDIPortInfoList inputs, |
+ media::MIDIPortInfoList outputs); |
+ |
+ // Called when the browser process has sent MIDI data containing one or |
+ // more messages. |
+ void OnDataReceived(int port, |
+ const std::vector<uint8>& data, |
+ double timestamp); |
+ |
+ void HandleAccessApproved(int client_id, |
+ int access, |
+ bool success, |
+ media::MIDIPortInfoList inputs, |
+ media::MIDIPortInfoList outputs); |
+ |
+ void HandleDataReceived(int port, |
+ const std::vector<uint8>& data, |
+ double timestamp); |
+ |
+ WebKit::WebMIDIAccessorClient* GetClientFromId(int client_id); |
+ |
+ // IPC channel for Send(); must only be accessed on |io_message_loop_|. |
+ IPC::Channel* channel_; |
+ |
+ // Message loop on which IPC calls are driven. |
+ const scoped_refptr<base::MessageLoopProxy> io_message_loop_; |
+ |
+ // Keeps track of all MIDI clients. |
+ // We map client to "client id" used to track permission. |
+ // When access has been approved, we add the input and output ports to |
+ // the client, allowing it to actually receive and send MIDI data. |
+ typedef std::map<WebKit::WebMIDIAccessorClient*, int> ClientsMap; |
+ ClientsMap clients_; |
+ |
+ // Dishes out client ids. |
+ static int next_available_id_; |
piman
2013/06/20 20:16:20
nit: does this need to be static?
Chris Rogers
2013/06/21 23:04:11
Done.
|
+ |
+ // Protects access to our clients. |
+ base::Lock clients_lock_; |
piman
2013/06/20 20:16:20
I think you can do away with the lock:
channel_ is
Chris Rogers
2013/06/21 23:04:11
Yes you're right - removed it.
|
+ |
+ // Will normally be the JS main thread. |
+ scoped_refptr<base::MessageLoopProxy> client_message_loop_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MIDIMessageFilter); |
+}; |
+ |
+} // namespace content |
+ |
+#endif // CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ |