Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: Source/modules/webmidi/MIDIAccessPromise.cpp

Issue 15995010: Web MIDI: implement MIDIAccessPromise (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: review #19 Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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(ScriptExecutionContext* context, const Dict ionary& options)
49 : ActiveDOMObject(context)
50 , m_context(context)
51 , m_state(Pending)
52 , m_options(adoptPtr(new MIDIOptions(options)))
53 , m_access(MIDIAccess::create(m_context, this))
54 {
55 ScriptWrappable::init(this);
56 }
57
58 MIDIAccessPromise::~MIDIAccessPromise()
59 {
60 stop();
61 }
62
63 void MIDIAccessPromise::fulfill()
64 {
65 if (m_state == Pending) {
66 if (m_successCallback) {
67 m_state = Invoked;
68 m_successCallback->handleEvent(m_access.release().leakRef(), m_optio ns->sysexEnabled);
69 m_options.clear();
70 } else {
71 m_state = Accepted;
72 }
73 }
74 m_successCallback.clear();
75 m_errorCallback.clear();
76 }
77
78 void MIDIAccessPromise::reject(DOMError* error)
79 {
80 if (m_state == Pending) {
81 if (m_errorCallback) {
82 m_state = Invoked;
83 m_errorCallback->handleEvent(error);
84 } else {
85 m_state = Rejected;
86 m_error = adoptRef(error);
87 }
88 }
89 m_successCallback.clear();
90 m_errorCallback.clear();
91 }
92
93 void MIDIAccessPromise::then(PassRefPtr<MIDISuccessCallback> successCallback, Pa ssRefPtr<MIDIErrorCallback> errorCallback)
94 {
95 switch (m_state) {
96 case Accepted:
97 successCallback->handleEvent(m_access.release().leakRef(), m_options->sy sexEnabled);
98 m_options.clear();
99 m_state = Invoked;
100 break;
101 case Rejected:
102 errorCallback->handleEvent(m_error.release().leakRef());
103 m_state = Invoked;
104 break;
105 case Pending:
106 m_successCallback = successCallback;
107 m_errorCallback = errorCallback;
108 break;
109 case Invoked:
110 break;
111 default:
112 ASSERT_NOT_REACHED();
113 }
114 }
115
116 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698