OLD | NEW |
---|---|
(Empty) | |
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 #include "config.h" | |
32 #include "modules/webmidi/MIDIAccessPromise.h" | |
33 | |
34 #include "core/dom/DOMError.h" | |
35 #include "modules/webmidi/MIDIAccess.h" | |
36 #include "modules/webmidi/MIDIErrorCallback.h" | |
37 #include "modules/webmidi/MIDISuccessCallback.h" | |
38 | |
39 namespace WebCore { | |
40 | |
41 PassRefPtr<MIDIAccessPromise> MIDIAccessPromise::create(ScriptExecutionContext* context, const Dictionary& options) | |
42 { | |
43 RefPtr<MIDIAccessPromise> midiAccessPromise(adoptRef(new MIDIAccessPromise(c ontext, options))); | |
44 midiAccessPromise->suspendIfNeeded(); | |
45 return midiAccessPromise.release(); | |
46 } | |
47 | |
48 MIDIAccessPromise::~MIDIAccessPromise() | |
49 { | |
50 stop(); | |
51 } | |
52 | |
53 void MIDIAccessPromise::fulfill() | |
54 { | |
55 if (m_state == Pending) { | |
56 if (m_successCallback) { | |
57 m_state = Invoked; | |
58 m_successCallback->handleEvent(m_access.release().leakRef(), m_optio ns->sysexEnabled); | |
59 m_options.clear(); | |
60 } else { | |
61 m_state = Accepted; | |
62 } | |
63 } | |
64 m_successCallback.clear(); | |
65 m_errorCallback.clear(); | |
66 } | |
67 | |
68 void MIDIAccessPromise::reject(DOMError* error) | |
69 { | |
70 if (m_state == Pending) { | |
71 if (m_errorCallback) { | |
72 m_state = Invoked; | |
73 m_errorCallback->handleEvent(error); | |
74 } else { | |
75 m_state = Rejected; | |
76 m_error = adoptRef(error); | |
77 } | |
78 } | |
79 m_successCallback.clear(); | |
80 m_errorCallback.clear(); | |
81 } | |
82 | |
83 MIDIAccessPromise::MIDIAccessPromise(ScriptExecutionContext* context, const Dict ionary& options) | |
Chris Rogers
2013/06/07 02:50:08
nit: constructor should be near top of file just b
Takashi Toyoshima
2013/06/07 03:55:37
Done.
| |
84 : ActiveDOMObject(context) | |
85 , m_context(context) | |
86 , m_state(Pending) | |
87 , m_options(adoptPtr(new MIDIOptions(options))) | |
88 , m_access(MIDIAccess::create(m_context)) | |
Chris Rogers
2013/06/07 02:50:08
I think we will need to add an additional construc
Takashi Toyoshima
2013/06/07 03:55:37
Done.
| |
89 { | |
90 // FIXME: Make MIDIAccess have PromiseResolver functions to call fulfill() a nd reject(). | |
91 // MIDIAccess should start asynchronous operation to get an access permissio n to MIDI, | |
92 // and be ready for accessing MIDI ports, here. | |
93 ScriptWrappable::init(this); | |
94 } | |
95 | |
96 void MIDIAccessPromise::then(PassRefPtr<MIDISuccessCallback> successCallback, Pa ssRefPtr<MIDIErrorCallback> errorCallback) | |
97 { | |
98 switch (m_state) { | |
99 case Accepted: | |
100 successCallback->handleEvent(m_access.release().leakRef(), m_options->sy sexEnabled); | |
101 m_options.clear(); | |
102 m_state = Invoked; | |
103 break; | |
104 case Rejected: | |
105 errorCallback->handleEvent(m_error.release().leakRef()); | |
106 m_state = Invoked; | |
107 break; | |
108 case Pending: | |
109 m_successCallback = successCallback; | |
110 m_errorCallback = errorCallback; | |
111 break; | |
112 case Invoked: | |
113 break; | |
114 default: | |
115 ASSERT_NOT_REACHED(); | |
116 } | |
117 } | |
118 | |
119 } // namespace WebCore | |
OLD | NEW |