15267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)/*
25267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
35267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *
45267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * modification, are permitted provided that the following conditions are
65267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * met:
75267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *
85267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * in the documentation and/or other materials provided with the
135267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * distribution.
145267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
165267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * this software without specific prior written permission.
175267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *
185267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) */
305267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
315267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#ifndef MIDIAccess_h
325267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#define MIDIAccess_h
335267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
34197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/ScriptPromise.h"
355267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "core/dom/ActiveDOMObject.h"
36d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "modules/EventTargetModules.h"
375d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)#include "modules/webmidi/MIDIAccessInitializer.h"
381e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "modules/webmidi/MIDIAccessor.h"
391e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "modules/webmidi/MIDIAccessorClient.h"
4010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch#include "platform/heap/Handle.h"
415d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)#include "wtf/Vector.h"
425267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
43c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
445267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
451e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)class ExecutionContext;
467242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciclass MIDIInput;
477242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciclass MIDIInputMap;
487242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciclass MIDIOutput;
497242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciclass MIDIOutputMap;
505267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
51197021e6b966cfb06891637935ef33fff06433d1Ben Murdochclass MIDIAccess FINAL : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<MIDIAccess>, public ActiveDOMObject, public EventTargetWithInlineData, public MIDIAccessorClient {
52197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<MIDIAccess>);
537242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    DEFINE_WRAPPERTYPEINFO();
54d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MIDIAccess);
555267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)public:
56197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    static MIDIAccess* create(PassOwnPtr<MIDIAccessor> accessor, bool sysexEnabled, const Vector<MIDIAccessInitializer::PortDescriptor>& ports, ExecutionContext* executionContext)
575d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    {
587242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        MIDIAccess* access = adoptRefCountedGarbageCollectedWillBeNoop(new MIDIAccess(accessor, sysexEnabled, ports, executionContext));
595d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        access->suspendIfNeeded();
605d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        return access;
615d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    }
625267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    virtual ~MIDIAccess();
635267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
647242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    MIDIInputMap* inputs() const;
657242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    MIDIOutputMap* outputs() const;
665267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
675267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    DEFINE_ATTRIBUTE_EVENT_LISTENER(connect);
685267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect);
695267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
7007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    bool sysexEnabled() const { return m_sysexEnabled; }
71e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch
725267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // EventTarget
731e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    virtual const AtomicString& interfaceName() const OVERRIDE { return EventTargetNames::MIDIAccess; }
741e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    virtual ExecutionContext* executionContext() const OVERRIDE { return ActiveDOMObject::executionContext(); }
755267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
765267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    // ActiveDOMObject
7709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void stop() OVERRIDE;
78591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch
79591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch    // MIDIAccessorClient
80591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch    virtual void didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version) OVERRIDE;
81591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch    virtual void didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) OVERRIDE;
825d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    virtual void didStartSession(bool success, const String& error, const String& message) OVERRIDE
835d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    {
845d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        // This method is for MIDIAccess initialization: MIDIAccessInitializer
855d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        // has the implementation.
865d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)        ASSERT_NOT_REACHED();
875d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    }
88591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch    virtual void didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp) OVERRIDE;
895267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
9002772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    // |timeStampInMilliseconds| is in the same time coordinate system as performance.now().
9102772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch    void sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds);
9202772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
93d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    virtual void trace(Visitor*) OVERRIDE;
94d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
955267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)private:
965d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    MIDIAccess(PassOwnPtr<MIDIAccessor>, bool sysexEnabled, const Vector<MIDIAccessInitializer::PortDescriptor>&, ExecutionContext*);
9707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
98591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch    OwnPtr<MIDIAccessor> m_accessor;
9907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    bool m_sysexEnabled;
1007242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    HeapVector<Member<MIDIInput> > m_inputs;
1017242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    HeapVector<Member<MIDIOutput> > m_outputs;
1025267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)};
1035267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
104c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
1055267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
1065267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#endif // MIDIAccess_h
107