Home | History | Annotate | Download | only in webmidi
      1 // Copyright 2014 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 MIDIAccessInitializer_h
      6 #define MIDIAccessInitializer_h
      7 
      8 #include "bindings/core/v8/ScriptPromise.h"
      9 #include "bindings/core/v8/ScriptPromiseResolver.h"
     10 #include "modules/webmidi/MIDIAccessor.h"
     11 #include "modules/webmidi/MIDIAccessorClient.h"
     12 #include "modules/webmidi/MIDIPort.h"
     13 #include "wtf/OwnPtr.h"
     14 #include "wtf/Vector.h"
     15 
     16 namespace blink {
     17 
     18 class MIDIAccess;
     19 class MIDIOptions;
     20 class Navigator;
     21 class ScriptState;
     22 
     23 class MIDIAccessInitializer : public ScriptPromiseResolver, public MIDIAccessorClient {
     24 public:
     25     struct PortDescriptor {
     26         String id;
     27         String manufacturer;
     28         String name;
     29         MIDIPort::MIDIPortTypeCode type;
     30         String version;
     31 
     32         PortDescriptor(const String& id, const String& manufacturer, const String& name, MIDIPort::MIDIPortTypeCode type, const String& version)
     33             : id(id)
     34             , manufacturer(manufacturer)
     35             , name(name)
     36             , type(type)
     37             , version(version) { }
     38     };
     39 
     40     static ScriptPromise start(ScriptState* scriptState, const MIDIOptions& options)
     41     {
     42         RefPtr<MIDIAccessInitializer> p = adoptRef(new MIDIAccessInitializer(scriptState, options));
     43         p->keepAliveWhilePending();
     44         p->suspendIfNeeded();
     45         return p->start();
     46     }
     47 
     48     virtual ~MIDIAccessInitializer();
     49 
     50     // MIDIAccessorClient
     51     virtual void didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version) OVERRIDE;
     52     virtual void didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) OVERRIDE;
     53     virtual void didStartSession(bool success, const String& error, const String& message) OVERRIDE;
     54     virtual void didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp) OVERRIDE { }
     55 
     56     void resolveSysexPermission(bool allowed);
     57     SecurityOrigin* securityOrigin() const;
     58 
     59 private:
     60     ScriptPromise start();
     61 
     62     MIDIAccessInitializer(ScriptState*, const MIDIOptions&);
     63 
     64     ExecutionContext* executionContext() const;
     65 
     66     OwnPtr<MIDIAccessor> m_accessor;
     67     bool m_requestSysex;
     68     Vector<PortDescriptor> m_portDescriptors;
     69 };
     70 
     71 } // namespace blink
     72 
     73 
     74 #endif // MIDIAccessInitializer_h
     75