1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef MIDIAccess_h
32#define MIDIAccess_h
33
34#include "bindings/v8/ScriptWrappable.h"
35#include "core/dom/ActiveDOMObject.h"
36#include "core/events/EventTarget.h"
37#include "modules/webmidi/MIDIAccessor.h"
38#include "modules/webmidi/MIDIAccessorClient.h"
39#include "modules/webmidi/MIDIInput.h"
40#include "modules/webmidi/MIDIOutput.h"
41#include "wtf/RefCounted.h"
42#include "wtf/RefPtr.h"
43#include "wtf/Vector.h"
44
45namespace WebCore {
46
47class ExecutionContext;
48class MIDIAccessPromise;
49
50class MIDIAccess : public RefCounted<MIDIAccess>, public ScriptWrappable, public ActiveDOMObject, public EventTargetWithInlineData, public MIDIAccessorClient {
51    REFCOUNTED_EVENT_TARGET(MIDIAccess);
52public:
53    virtual ~MIDIAccess();
54    static PassRefPtr<MIDIAccess> create(ExecutionContext*, MIDIAccessPromise*);
55
56    MIDIInputVector inputs() const { return m_inputs; }
57    MIDIOutputVector outputs() const { return m_outputs; }
58
59    DEFINE_ATTRIBUTE_EVENT_LISTENER(connect);
60    DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect);
61
62    void setSysExEnabled(bool);
63    bool sysExEnabled() const { return m_sysExEnabled; }
64
65    // EventTarget
66    virtual const AtomicString& interfaceName() const OVERRIDE { return EventTargetNames::MIDIAccess; }
67    virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
68
69    // ActiveDOMObject
70    virtual void stop();
71
72    // MIDIAccessorClient
73    virtual void didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version) OVERRIDE;
74    virtual void didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) OVERRIDE;
75    virtual void didStartSession(bool success) OVERRIDE;
76    virtual void didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp) OVERRIDE;
77
78    // |timeStampInMilliseconds| is in the same time coordinate system as performance.now().
79    void sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds);
80
81private:
82    MIDIAccess(ExecutionContext*, MIDIAccessPromise*);
83
84    void startRequest();
85    virtual void permissionDenied();
86
87    MIDIInputVector m_inputs;
88    MIDIOutputVector m_outputs;
89    MIDIAccessPromise* m_promise;
90
91    OwnPtr<MIDIAccessor> m_accessor;
92    bool m_hasAccess;
93    bool m_sysExEnabled;
94    bool m_requesting;
95};
96
97} // namespace WebCore
98
99#endif // MIDIAccess_h
100