Index: Source/modules/webmidi/NavigatorWebMIDI.cpp |
diff --git a/Source/modules/webmidi/NavigatorWebMIDI.cpp b/Source/modules/webmidi/NavigatorWebMIDI.cpp |
index 87f7b25d31ef0fbc61ed12c4644be689c87b3033..72d3a5e1b6d1452c376cc3c6ce14c01132277f51 100644 |
--- a/Source/modules/webmidi/NavigatorWebMIDI.cpp |
+++ b/Source/modules/webmidi/NavigatorWebMIDI.cpp |
@@ -31,11 +31,16 @@ |
#include "config.h" |
#include "modules/webmidi/NavigatorWebMIDI.h" |
+#include "bindings/v8/DOMRequestState.h" |
+#include "bindings/v8/ScriptPromise.h" |
+#include "bindings/v8/ScriptPromiseResolver.h" |
+#include "core/dom/DOMError.h" |
#include "core/dom/Document.h" |
#include "core/dom/ExecutionContext.h" |
#include "core/frame/Frame.h" |
#include "core/frame/Navigator.h" |
-#include "modules/webmidi/MIDIAccessPromise.h" |
+#include "modules/webmidi/MIDIAccess.h" |
+#include "modules/webmidi/MIDIOptions.h" |
namespace WebCore { |
@@ -46,6 +51,7 @@ NavigatorWebMIDI::NavigatorWebMIDI(Frame* frame) |
NavigatorWebMIDI::~NavigatorWebMIDI() |
{ |
+ clearPending(); |
} |
const char* NavigatorWebMIDI::supplementName() |
@@ -63,20 +69,84 @@ NavigatorWebMIDI* NavigatorWebMIDI::from(Navigator* navigator) |
return supplement; |
} |
-PassRefPtr<MIDIAccessPromise> NavigatorWebMIDI::requestMIDIAccess(Navigator* navigator, const Dictionary& options) |
+ScriptPromise NavigatorWebMIDI::requestMIDIAccess(Navigator* navigator, const Dictionary& options) |
{ |
return NavigatorWebMIDI::from(navigator)->requestMIDIAccess(options); |
} |
-PassRefPtr<MIDIAccessPromise> NavigatorWebMIDI::requestMIDIAccess(const Dictionary& options) |
+ScriptPromise NavigatorWebMIDI::requestMIDIAccess(const Dictionary& options) |
{ |
- if (!frame()) |
- return 0; |
+ if (!frame()) { |
Takashi Toyoshima
2013/12/03 10:48:50
optional: Can we have a layout test which confirm
yhirano
2013/12/03 12:14:16
I found that this rejection is currently not worki
|
+ ScriptPromise promise = ScriptPromise::createPending(); |
+ RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(promise); |
+ resolver->reject(DOMError::create("InvalidStateError")); |
Takashi Toyoshima
2013/12/03 10:48:50
This is not the case described in the spec, but I
Takashi Toyoshima
2013/12/03 11:34:49
Sent a pull request to use AbortError here.
https:
yhirano
2013/12/03 12:14:16
Done.
|
+ return promise; |
+ } |
ExecutionContext* context = frame()->document(); |
ASSERT(context); |
+ if (!m_requestState.get()) |
+ m_requestState = adoptPtr(new DOMRequestState(context)); |
+ ScriptPromise promise = ScriptPromise::createPending(context); |
+ RefPtr<MIDIAccess> access = MIDIAccess::create(MIDIOptions(options), context, this); |
+ m_pending.add(access, ScriptPromiseResolver::create(promise, context)); |
+ access->startRequest(); |
+ return promise; |
+} |
+ |
+void NavigatorWebMIDI::didFinishRequest(MIDIAccess* access) |
+{ |
+ // FIXME: We should care about suspend / stop. |
+ RefPtr<MIDIAccess> key(access); |
+ if (!m_pending.contains(key)) |
+ return; |
+ RefPtr<ScriptPromiseResolver> resolver = m_pending.get(key); |
+ m_pending.remove(key); |
+ |
+ if (!frame()) { |
+ // We can break the promise if the frame does not exist. |
+ return; |
+ } |
+ ASSERT(m_requestState); |
+ DOMRequestState::Scope scope(*m_requestState); |
+ resolver->resolve(access, access->executionContext()); |
Takashi Toyoshima
2013/12/03 11:34:49
I proposed to remove the second argument of the MI
|
+} |
+ |
+void NavigatorWebMIDI::didFailRequest(MIDIAccess* access, PassRefPtr<DOMError> error) |
+{ |
+ // FIXME: We should care about suspend / stop. |
+ RefPtr<MIDIAccess> key(access); |
+ if (!m_pending.contains(key)) |
+ return; |
+ RefPtr<ScriptPromiseResolver> resolver = m_pending.get(key); |
+ m_pending.remove(key); |
+ if (!frame()) { |
+ // We can break the promise if the frame does not exist. |
+ return; |
+ } |
+ ASSERT(m_requestState); |
+ DOMRequestState::Scope scope(*m_requestState); |
+ resolver->reject(error, access->executionContext()); |
+} |
+ |
+void NavigatorWebMIDI::clearPending() |
+{ |
+ // FIXME: We should care about suspend / stop. |
+ HashMap<RefPtr<MIDIAccess>, RefPtr<ScriptPromiseResolver> > pending; |
+ pending.swap(m_pending); |
+ |
+ if (!frame()) { |
+ // We can break promises if the frame does not exist. |
+ return; |
+ } |
- return MIDIAccessPromise::create(context, options); |
+ ASSERT(m_requestState); |
+ for (HashMap<RefPtr<MIDIAccess>, RefPtr<ScriptPromiseResolver> >::iterator i = pending.begin(); |
+ i != pending.end(); ++i) { |
+ DOMRequestState::Scope scope(*m_requestState); |
+ MIDIAccess* access = i->key.get(); |
+ i->value->reject(DOMError::create("InvalidStateError"), access->executionContext()); |
Takashi Toyoshima
2013/12/03 10:48:50
AbortError?
yhirano
2013/12/03 12:14:16
Done.
|
+ } |
} |
} // namespace WebCore |