Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / media / midi_host.h
bloba8e84f260ae9ff9807a0a010143d5b961a156981
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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_MIDI_HOST_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_MIDI_HOST_H_
8 #include <vector>
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/browser_message_filter.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "media/midi/midi_manager.h"
19 namespace media {
20 class MidiManager;
21 class MidiMessageQueue;
24 namespace content {
26 class CONTENT_EXPORT MidiHost
27 : public BrowserMessageFilter,
28 public media::MidiManagerClient {
29 public:
30 // Called from UI thread from the owner of this object.
31 MidiHost(int renderer_process_id, media::MidiManager* midi_manager);
33 // BrowserMessageFilter implementation.
34 virtual void OnDestruct() const OVERRIDE;
35 virtual bool OnMessageReceived(const IPC::Message& message,
36 bool* message_was_ok) OVERRIDE;
38 // MidiManagerClient implementation.
39 virtual void CompleteStartSession(int client_id,
40 media::MidiResult result) OVERRIDE;
41 virtual void ReceiveMidiData(uint32 port,
42 const uint8* data,
43 size_t length,
44 double timestamp) OVERRIDE;
45 virtual void AccumulateMidiBytesSent(size_t n) OVERRIDE;
47 // Start session to access MIDI hardware.
48 void OnStartSession(int client_id);
50 // Data to be sent to a MIDI output port.
51 void OnSendData(uint32 port,
52 const std::vector<uint8>& data,
53 double timestamp);
55 private:
56 FRIEND_TEST_ALL_PREFIXES(MidiHostTest, IsValidWebMIDIData);
57 friend class base::DeleteHelper<MidiHost>;
58 friend class BrowserThread;
60 virtual ~MidiHost();
62 // Returns true if |data| fulfills the requirements of MidiOutput.send API
63 // defined in the WebMIDI spec.
64 // - |data| must be any number of complete MIDI messages (data abbreviation
65 // called "running status" is disallowed).
66 // - 1-byte MIDI realtime messages can be placed at any position of |data|.
67 static bool IsValidWebMIDIData(const std::vector<uint8>& data);
69 int renderer_process_id_;
71 // Represents if the renderer has a permission to send/receive MIDI SysEX
72 // messages.
73 bool has_sys_ex_permission_;
75 // |midi_manager_| talks to the platform-specific MIDI APIs.
76 // It can be NULL if the platform (or our current implementation)
77 // does not support MIDI. If not supported then a call to
78 // OnRequestAccess() will always refuse access and a call to
79 // OnSendData() will do nothing.
80 media::MidiManager* const midi_manager_;
82 // Buffers where data sent from each MIDI input port is stored.
83 ScopedVector<media::MidiMessageQueue> received_messages_queues_;
85 // The number of bytes sent to the platform-specific MIDI sending
86 // system, but not yet completed.
87 size_t sent_bytes_in_flight_;
89 // The number of bytes successfully sent since the last time
90 // we've acknowledged back to the renderer.
91 size_t bytes_sent_since_last_acknowledgement_;
93 // Protects access to |sent_bytes_in_flight_|.
94 base::Lock in_flight_lock_;
96 DISALLOW_COPY_AND_ASSIGN(MidiHost);
99 } // namespace content
101 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MIDI_HOST_H_