Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 #ifndef CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ | |
| 6 #define CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "content/common/content_export.h" | |
| 13 #include "ipc/ipc_channel_proxy.h" | |
| 14 #include "media/midi/midi_port_info.h" | |
| 15 #include "third_party/WebKit/public/platform/WebMIDIAccessor.h" | |
| 16 | |
| 17 namespace base { | |
| 18 class MessageLoopProxy; | |
| 19 } | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 // MessageFilter that handles MIDI messages. | |
| 24 class CONTENT_EXPORT MIDIMessageFilter | |
| 25 : public IPC::ChannelProxy::MessageFilter { | |
| 26 public: | |
| 27 explicit MIDIMessageFilter( | |
| 28 const scoped_refptr<base::MessageLoopProxy>& io_message_loop); | |
| 29 | |
| 30 // Each client registers for MIDI access here. | |
| 31 // If permission is granted, then the client's | |
| 32 // addInputPort() and addOutputPort() methods will be called, | |
| 33 // giving the client access to receive and send data. | |
| 34 void RequestAccess(WebKit::WebMIDIAccessor::Client* client, int access); | |
| 35 void RemoveClient(WebKit::WebMIDIAccessor::Client* client); | |
| 36 | |
| 37 // A client will only be able to call this method if it has a suitable | |
| 38 // output port (from addOutputPort()). | |
| 39 void SendMIDIData(int port_index, | |
| 40 const uint8* data, | |
| 41 size_t length, | |
| 42 double timestamp); | |
| 43 | |
| 44 // IO message loop associated with this message filter. | |
| 45 scoped_refptr<base::MessageLoopProxy> io_message_loop() const { | |
| 46 return io_message_loop_; | |
| 47 } | |
| 48 | |
| 49 protected: | |
| 50 virtual ~MIDIMessageFilter(); | |
| 51 | |
| 52 private: | |
| 53 // Sends an IPC message using |channel_|. | |
| 54 void Send(IPC::Message* message); | |
| 55 | |
| 56 // IPC::ChannelProxy::MessageFilter override. Called on |io_message_loop|. | |
| 57 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
| 58 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE; | |
| 59 virtual void OnFilterRemoved() OVERRIDE; | |
| 60 virtual void OnChannelClosing() OVERRIDE; | |
| 61 | |
| 62 // Called when the browser process has approved (or denied) access to | |
| 63 // MIDI hardware. | |
| 64 void OnAccessApproved(int client_id, | |
| 65 int access, | |
| 66 bool success, | |
| 67 media::MIDIPortInfoList inputs, | |
| 68 media::MIDIPortInfoList outputs); | |
| 69 | |
| 70 // Called when the browser process has sent MIDI data containing one or | |
| 71 // more messages. | |
| 72 void OnDataReceived(int port, | |
| 73 const std::vector<uint8>& data, | |
| 74 double timestamp); | |
| 75 | |
| 76 void HandleAccessApproved(int client_id, | |
| 77 int access, | |
| 78 bool success, | |
| 79 media::MIDIPortInfoList inputs, | |
| 80 media::MIDIPortInfoList outputs); | |
| 81 | |
| 82 void HandleDataReceived(int port, | |
| 83 const std::vector<uint8>& data, | |
| 84 double timestamp); | |
| 85 | |
| 86 WebKit::WebMIDIAccessor::Client* GetClientFromId(int client_id); | |
| 87 | |
| 88 // IPC channel for Send(); must only be accessed on |io_message_loop_|. | |
| 89 IPC::Channel* channel_; | |
| 90 | |
| 91 // Message loop on which IPC calls are driven. | |
| 92 const scoped_refptr<base::MessageLoopProxy> io_message_loop_; | |
| 93 | |
| 94 // Keeps track of all MIDI clients. | |
| 95 // We map client to "client id" used to track permission. | |
| 96 // When access has been approved, we add the input and output ports to | |
| 97 // the client, allowing it to actually receive and send MIDI data. | |
| 98 typedef std::map<WebKit::WebMIDIAccessor::Client*, int> ClientsMap; | |
| 99 ClientsMap clients_; | |
| 100 | |
| 101 // Dishes out client ids. | |
| 102 static int next_available_id_; | |
| 103 | |
| 104 // Protects access to our clients. | |
| 105 base::Lock clients_lock_; | |
| 106 | |
| 107 base::MessageLoop* client_message_loop_; | |
|
scherkus (not reviewing)
2013/06/12 01:28:47
is it possible to use scoped_refptr<base::MessageL
Chris Rogers
2013/06/12 20:34:35
Done.
| |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(MIDIMessageFilter); | |
| 110 }; | |
| 111 | |
| 112 } // namespace content | |
| 113 | |
| 114 #endif // CONTENT_RENDERER_MEDIA_MIDI_MESSAGE_FILTER_H_ | |
| OLD | NEW |