| 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 DEFINE_GC_INFO(MIDIAccessPromise); | |
| 42 | |
| 43 PassRefPtrWillBeRawPtr<MIDIAccessPromise> MIDIAccessPromise::create(ExecutionCon
text* context, const Dictionary& options) | |
| 44 { | |
| 45 RefPtrWillBeRawPtr<MIDIAccessPromise> midiAccessPromise(adoptRefCountedWillB
eRefCountedGarbageCollected(new MIDIAccessPromise(context, options))); | |
| 46 midiAccessPromise->suspendIfNeeded(); | |
| 47 return midiAccessPromise.release(); | |
| 48 } | |
| 49 | |
| 50 MIDIAccessPromise::MIDIAccessPromise(ExecutionContext* context, const Dictionary
& options) | |
| 51 : ActiveDOMObject(context) | |
| 52 , m_state(Pending) | |
| 53 , m_options(adoptPtr(new MIDIOptions(options))) | |
| 54 { | |
| 55 ScriptWrappable::init(this); | |
| 56 } | |
| 57 | |
| 58 MIDIAccessPromise::~MIDIAccessPromise() | |
| 59 { | |
| 60 stop(); | |
| 61 } | |
| 62 | |
| 63 bool MIDIAccessPromise::hasPendingActivity() const | |
| 64 { | |
| 65 return !!m_access && m_state != Accepted && m_state != Rejected; | |
| 66 } | |
| 67 | |
| 68 void MIDIAccessPromise::stop() | |
| 69 { | |
| 70 m_state = Invoked; | |
| 71 if (!!m_access) | |
| 72 m_access->stop(); | |
| 73 clear(); | |
| 74 } | |
| 75 | |
| 76 void MIDIAccessPromise::fulfill() | |
| 77 { | |
| 78 if (m_state == Pending) { | |
| 79 if (m_successCallback) { | |
| 80 m_state = Invoked; | |
| 81 ASSERT(m_access); | |
| 82 m_successCallback->handleEvent(m_access.get(), m_options->sysex); | |
| 83 clear(); | |
| 84 } else { | |
| 85 m_state = Accepted; | |
| 86 } | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void MIDIAccessPromise::reject(PassRefPtr<DOMError> error) | |
| 91 { | |
| 92 if (m_state == Pending) { | |
| 93 if (m_errorCallback) { | |
| 94 m_state = Invoked; | |
| 95 m_errorCallback->handleEvent(error.get()); | |
| 96 clear(); | |
| 97 } else { | |
| 98 m_state = Rejected; | |
| 99 m_error = error; | |
| 100 } | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void MIDIAccessPromise::then(PassOwnPtr<MIDISuccessCallback> successCallback, Pa
ssOwnPtr<MIDIErrorCallback> errorCallback) | |
| 105 { | |
| 106 // Lazily request access. | |
| 107 if (!m_access) | |
| 108 m_access = MIDIAccess::create(executionContext(), this); | |
| 109 | |
| 110 switch (m_state) { | |
| 111 case Accepted: | |
| 112 m_state = Invoked; | |
| 113 successCallback->handleEvent(m_access.get(), m_options->sysex); | |
| 114 clear(); | |
| 115 break; | |
| 116 case Rejected: | |
| 117 m_state = Invoked; | |
| 118 errorCallback->handleEvent(m_error.get()); | |
| 119 clear(); | |
| 120 break; | |
| 121 case Pending: | |
| 122 m_successCallback = successCallback; | |
| 123 m_errorCallback = errorCallback; | |
| 124 break; | |
| 125 case Invoked: | |
| 126 break; | |
| 127 default: | |
| 128 ASSERT_NOT_REACHED(); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void MIDIAccessPromise::clear() | |
| 133 { | |
| 134 ASSERT(m_state == Invoked); | |
| 135 m_access.clear(); | |
| 136 m_error.clear(); | |
| 137 m_options.clear(); | |
| 138 m_successCallback.clear(); | |
| 139 m_errorCallback.clear(); | |
| 140 } | |
| 141 | |
| 142 void MIDIAccessPromise::trace(Visitor* visitor) | |
| 143 { | |
| 144 visitor->trace(m_access); | |
| 145 } | |
| 146 | |
| 147 } // namespace WebCore | |
| OLD | NEW |