Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "modules/webmidi/NavigatorWebMIDI.h" | 32 #include "modules/webmidi/NavigatorWebMIDI.h" |
| 33 | 33 |
| 34 #include "bindings/v8/DOMRequestState.h" | |
| 35 #include "bindings/v8/ScriptPromise.h" | |
| 36 #include "bindings/v8/ScriptPromiseResolver.h" | |
| 37 #include "core/dom/DOMError.h" | |
| 34 #include "core/dom/Document.h" | 38 #include "core/dom/Document.h" |
| 35 #include "core/dom/ExecutionContext.h" | 39 #include "core/dom/ExecutionContext.h" |
| 36 #include "core/frame/Frame.h" | 40 #include "core/frame/Frame.h" |
| 37 #include "core/frame/Navigator.h" | 41 #include "core/frame/Navigator.h" |
| 38 #include "modules/webmidi/MIDIAccessPromise.h" | 42 #include "modules/webmidi/MIDIAccess.h" |
| 43 #include "modules/webmidi/MIDIOptions.h" | |
| 39 | 44 |
| 40 namespace WebCore { | 45 namespace WebCore { |
| 41 | 46 |
| 42 NavigatorWebMIDI::NavigatorWebMIDI(Frame* frame) | 47 NavigatorWebMIDI::NavigatorWebMIDI(Frame* frame) |
| 43 : DOMWindowProperty(frame) | 48 : DOMWindowProperty(frame) |
| 44 { | 49 { |
| 45 } | 50 } |
| 46 | 51 |
| 47 NavigatorWebMIDI::~NavigatorWebMIDI() | 52 NavigatorWebMIDI::~NavigatorWebMIDI() |
| 48 { | 53 { |
| 54 clearPending(); | |
| 49 } | 55 } |
| 50 | 56 |
| 51 const char* NavigatorWebMIDI::supplementName() | 57 const char* NavigatorWebMIDI::supplementName() |
| 52 { | 58 { |
| 53 return "NavigatorWebMIDI"; | 59 return "NavigatorWebMIDI"; |
| 54 } | 60 } |
| 55 | 61 |
| 56 NavigatorWebMIDI* NavigatorWebMIDI::from(Navigator* navigator) | 62 NavigatorWebMIDI* NavigatorWebMIDI::from(Navigator* navigator) |
| 57 { | 63 { |
| 58 NavigatorWebMIDI* supplement = static_cast<NavigatorWebMIDI*>(Supplement<Nav igator>::from(navigator, supplementName())); | 64 NavigatorWebMIDI* supplement = static_cast<NavigatorWebMIDI*>(Supplement<Nav igator>::from(navigator, supplementName())); |
| 59 if (!supplement) { | 65 if (!supplement) { |
| 60 supplement = new NavigatorWebMIDI(navigator->frame()); | 66 supplement = new NavigatorWebMIDI(navigator->frame()); |
| 61 provideTo(navigator, supplementName(), adoptPtr(supplement)); | 67 provideTo(navigator, supplementName(), adoptPtr(supplement)); |
| 62 } | 68 } |
| 63 return supplement; | 69 return supplement; |
| 64 } | 70 } |
| 65 | 71 |
| 66 PassRefPtr<MIDIAccessPromise> NavigatorWebMIDI::requestMIDIAccess(Navigator* nav igator, const Dictionary& options) | 72 ScriptPromise NavigatorWebMIDI::requestMIDIAccess(Navigator* navigator, const Di ctionary& options) |
| 67 { | 73 { |
| 68 return NavigatorWebMIDI::from(navigator)->requestMIDIAccess(options); | 74 return NavigatorWebMIDI::from(navigator)->requestMIDIAccess(options); |
| 69 } | 75 } |
| 70 | 76 |
| 71 PassRefPtr<MIDIAccessPromise> NavigatorWebMIDI::requestMIDIAccess(const Dictiona ry& options) | 77 ScriptPromise NavigatorWebMIDI::requestMIDIAccess(const Dictionary& options) |
| 72 { | 78 { |
| 73 if (!frame()) | 79 if (!frame()) { |
| 74 return 0; | 80 ScriptPromise promise = ScriptPromise::createPending(); |
| 81 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(p romise); | |
| 82 // FIXME: Currently this rejection does not work because the context is stopped. | |
| 83 resolver->reject(DOMError::create("AbortError")); | |
|
abarth-chromium
2013/12/03 16:57:57
Maybe we should throw an exception instead?
yhirano
2013/12/04 05:20:24
I think the spec doesn't want to throw an error ex
Takashi Toyoshima
2013/12/04 06:48:37
How about other Promise or asynchronous API usages
yhirano
2013/12/05 08:19:12
I think so as well.
Anyway, this may be a Promise
| |
| 84 return promise; | |
| 85 } | |
| 75 | 86 |
| 76 ExecutionContext* context = frame()->document(); | 87 ExecutionContext* context = frame()->document(); |
| 77 ASSERT(context); | 88 ASSERT(context); |
| 89 if (!m_requestState.get()) | |
| 90 m_requestState = adoptPtr(new DOMRequestState(context)); | |
|
abarth-chromium
2013/12/03 16:57:57
Why is there only one m_requestState? When is m_r
yhirano
2013/12/04 05:20:24
Done.
Now each MIDIAccessor has its DOMRequestStat
| |
| 91 ScriptPromise promise = ScriptPromise::createPending(context); | |
| 92 RefPtr<MIDIAccess> access = MIDIAccess::create(MIDIOptions(options), context , this); | |
| 93 m_pending.add(access, ScriptPromiseResolver::create(promise, context)); | |
| 94 access->startRequest(); | |
| 95 return promise; | |
| 96 } | |
| 78 | 97 |
| 79 return MIDIAccessPromise::create(context, options); | 98 void NavigatorWebMIDI::didFinishRequest(MIDIAccess* access) |
| 99 { | |
| 100 // FIXME: We should care about suspend / stop. | |
| 101 RefPtr<MIDIAccess> key(access); | |
| 102 if (!m_pending.contains(key)) | |
| 103 return; | |
| 104 RefPtr<ScriptPromiseResolver> resolver = m_pending.get(key); | |
| 105 m_pending.remove(key); | |
| 106 | |
| 107 if (!frame()) { | |
| 108 // We can break the promise if the frame does not exist. | |
| 109 return; | |
| 110 } | |
| 111 ASSERT(m_requestState); | |
| 112 DOMRequestState::Scope scope(*m_requestState); | |
| 113 resolver->resolve(access, access->executionContext()); | |
| 114 } | |
| 115 | |
| 116 void NavigatorWebMIDI::didFailRequest(MIDIAccess* access, PassRefPtr<DOMError> e rror) | |
| 117 { | |
| 118 // FIXME: We should care about suspend / stop. | |
| 119 RefPtr<MIDIAccess> key(access); | |
| 120 if (!m_pending.contains(key)) | |
| 121 return; | |
| 122 RefPtr<ScriptPromiseResolver> resolver = m_pending.get(key); | |
| 123 m_pending.remove(key); | |
| 124 if (!frame()) { | |
| 125 // We can break the promise if the frame does not exist. | |
| 126 return; | |
| 127 } | |
| 128 ASSERT(m_requestState); | |
| 129 DOMRequestState::Scope scope(*m_requestState); | |
| 130 resolver->reject(error, access->executionContext()); | |
| 131 } | |
| 132 | |
| 133 void NavigatorWebMIDI::clearPending() | |
| 134 { | |
| 135 // FIXME: We should care about suspend / stop. | |
| 136 HashMap<RefPtr<MIDIAccess>, RefPtr<ScriptPromiseResolver> > pending; | |
| 137 pending.swap(m_pending); | |
| 138 | |
| 139 if (!frame()) { | |
| 140 // We can break promises if the frame does not exist. | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 ASSERT(m_requestState); | |
| 145 for (HashMap<RefPtr<MIDIAccess>, RefPtr<ScriptPromiseResolver> >::iterator i = pending.begin(); | |
| 146 i != pending.end(); ++i) { | |
| 147 DOMRequestState::Scope scope(*m_requestState); | |
| 148 MIDIAccess* access = i->key.get(); | |
| 149 i->value->reject(DOMError::create("AbortError"), access->executionContex t()); | |
| 150 } | |
| 80 } | 151 } |
| 81 | 152 |
| 82 } // namespace WebCore | 153 } // namespace WebCore |
| OLD | NEW |