Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/MIDIAccess.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_MIDIAccess_h
8
#define mozilla_dom_MIDIAccess_h
9
10
#include "mozilla/Attributes.h"
11
#include "mozilla/DOMEventTargetHelper.h"
12
#include "mozilla/ErrorResult.h"
13
#include "mozilla/Observer.h"
14
#include "nsCycleCollectionParticipant.h"
15
#include "nsIObserver.h"
16
#include "nsWrapperCache.h"
17
18
struct JSContext;
19
20
namespace mozilla {
21
// Predeclare void_t here, as including IPCMessageUtils brings in windows.h and
22
// causes binding compilation problems.
23
struct void_t;
24
25
namespace dom {
26
27
class MIDIAccessManager;
28
class MIDIInputMap;
29
struct MIDIOptions;
30
class MIDIOutputMap;
31
class MIDIPermissionRequest;
32
class MIDIPort;
33
class MIDIPortChangeEvent;
34
class MIDIPortInfo;
35
class MIDIPortList;
36
class Promise;
37
38
typedef Observer<void_t> MIDIAccessDestructionObserver;
39
40
/**
41
 * MIDIAccess is the DOM object that is handed to the user upon MIDI permissions
42
 * being successfully granted. It manages access to MIDI ports, and fires events
43
 * for device connection and disconnection.
44
 *
45
 * New MIDIAccess objects are created every time RequestMIDIAccess is called.
46
 * MIDIAccess objects are managed via MIDIAccessManager.
47
 */
48
class MIDIAccess final : public DOMEventTargetHelper,
49
                         public Observer<MIDIPortList>
50
{
51
  // Use the Permission Request class in MIDIAccessManager for creating
52
  // MIDIAccess objects.
53
  friend class MIDIPermissionRequest;
54
  friend class MIDIAccessManager;
55
public:
56
  NS_DECL_ISUPPORTS_INHERITED
57
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(MIDIAccess,
58
                                                         DOMEventTargetHelper)
59
public:
60
  virtual JSObject* WrapObject(JSContext* aCx,
61
                               JS::Handle<JSObject*> aGivenProto) override;
62
63
  // Return map of MIDI Input Ports
64
  MIDIInputMap* Inputs() const
65
0
  {
66
0
    return mInputMap;
67
0
  }
68
69
  // Return map of MIDI Output Ports
70
  MIDIOutputMap* Outputs() const
71
0
  {
72
0
    return mOutputMap;
73
0
  }
74
75
  // Returns true if sysex permissions were given
76
  bool SysexEnabled() const
77
0
  {
78
0
    return mSysexEnabled;
79
0
  }
80
81
  // Observer implementation for receiving port connection updates
82
  void Notify(const MIDIPortList& aEvent) override;
83
84
  // All MIDIPort objects observe destruction of the MIDIAccess object that
85
  // created them, as the port object receives disconnection events which then
86
  // must be passed up to the MIDIAccess object. If the Port object dies before
87
  // the MIDIAccess object, it needs to be removed from the observer list.
88
  void RemovePortListener(MIDIAccessDestructionObserver* aPort);
89
90
  // Fires DOM event on port connection/disconnection
91
  void FireConnectionEvent(MIDIPort* aPort);
92
93
  // Notify all MIDIPorts that were created by this MIDIAccess and are still
94
  // alive, and detach from the MIDIAccessManager.
95
  void Shutdown();
96
  IMPL_EVENT_HANDLER(statechange);
97
private:
98
  MIDIAccess(nsPIDOMWindowInner* aWindow, bool aSysexEnabled,
99
             Promise* aAccessPromise);
100
  ~MIDIAccess();
101
102
  // On receiving a connection event from MIDIAccessManager, create a
103
  // corresponding MIDIPort object if we don't already have one.
104
  void MaybeCreateMIDIPort(const MIDIPortInfo& aInfo, ErrorResult& aRv);
105
106
  // Stores all known MIDIInput Ports
107
  RefPtr<MIDIInputMap> mInputMap;
108
  // Stores all known MIDIOutput Ports
109
  RefPtr<MIDIOutputMap> mOutputMap;
110
  // List of MIDIPort observers that need to be updated on destruction.
111
  ObserverList<void_t> mDestructionObservers;
112
  // True if user gave permissions for sysex usage to this object.
113
  bool mSysexEnabled;
114
  // Promise created by RequestMIDIAccess call, to be resolved after port
115
  // populating is finished.
116
  RefPtr<Promise> mAccessPromise;
117
  // True if shutdown process has started, so we don't try to add more ports.
118
  bool mHasShutdown;
119
};
120
121
} // namespace dom
122
} // namespace mozilla
123
124
#endif // mozilla_dom_MIDIAccess_h