| 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 23 matching lines...) Expand all Loading... |
| 34 #include "core/dom/DOMError.h" | 34 #include "core/dom/DOMError.h" |
| 35 #include "modules/webmidi/MIDIAccess.h" | 35 #include "modules/webmidi/MIDIAccess.h" |
| 36 #include "modules/webmidi/MIDIErrorCallback.h" | 36 #include "modules/webmidi/MIDIErrorCallback.h" |
| 37 #include "modules/webmidi/MIDISuccessCallback.h" | 37 #include "modules/webmidi/MIDISuccessCallback.h" |
| 38 | 38 |
| 39 namespace WebCore { | 39 namespace WebCore { |
| 40 | 40 |
| 41 PassRefPtr<MIDIAccessPromise> MIDIAccessPromise::create(ScriptExecutionContext*
context, const Dictionary& options) | 41 PassRefPtr<MIDIAccessPromise> MIDIAccessPromise::create(ScriptExecutionContext*
context, const Dictionary& options) |
| 42 { | 42 { |
| 43 RefPtr<MIDIAccessPromise> midiAccessPromise(adoptRef(new MIDIAccessPromise(c
ontext, options))); | 43 RefPtr<MIDIAccessPromise> midiAccessPromise(adoptRef(new MIDIAccessPromise(c
ontext, options))); |
| 44 midiAccessPromise->setPendingActivity(midiAccessPromise.get()); | |
| 45 midiAccessPromise->suspendIfNeeded(); | 44 midiAccessPromise->suspendIfNeeded(); |
| 46 return midiAccessPromise.release(); | 45 return midiAccessPromise.release(); |
| 47 } | 46 } |
| 48 | 47 |
| 49 MIDIAccessPromise::MIDIAccessPromise(ScriptExecutionContext* context, const Dict
ionary& options) | 48 MIDIAccessPromise::MIDIAccessPromise(ScriptExecutionContext* context, const Dict
ionary& options) |
| 50 : ActiveDOMObject(context) | 49 : ActiveDOMObject(context) |
| 51 , m_state(Pending) | 50 , m_state(Pending) |
| 52 , m_options(adoptPtr(new MIDIOptions(options))) | 51 , m_options(adoptPtr(new MIDIOptions(options))) |
| 53 , m_access(MIDIAccess::create(context, this)) | |
| 54 { | 52 { |
| 55 ScriptWrappable::init(this); | 53 ScriptWrappable::init(this); |
| 56 } | 54 } |
| 57 | 55 |
| 58 MIDIAccessPromise::~MIDIAccessPromise() | 56 MIDIAccessPromise::~MIDIAccessPromise() |
| 59 { | 57 { |
| 60 stop(); | 58 stop(); |
| 61 } | 59 } |
| 62 | 60 |
| 63 void MIDIAccessPromise::fulfill() | 61 void MIDIAccessPromise::fulfill() |
| 64 { | 62 { |
| 65 if (m_state == Pending) { | 63 if (m_state == Pending) { |
| 66 if (m_successCallback) { | 64 if (m_successCallback) { |
| 67 m_state = Invoked; | 65 m_state = Invoked; |
| 66 ASSERT(m_access.get()); |
| 68 m_successCallback->handleEvent(m_access.release().leakRef(), m_optio
ns->sysexEnabled); | 67 m_successCallback->handleEvent(m_access.release().leakRef(), m_optio
ns->sysexEnabled); |
| 69 m_options.clear(); | 68 m_options.clear(); |
| 70 } else { | 69 } else { |
| 71 m_state = Accepted; | 70 m_state = Accepted; |
| 72 } | 71 } |
| 73 unsetPendingActivity(this); | 72 unsetPendingActivity(this); |
| 74 } | 73 } |
| 75 m_successCallback.clear(); | 74 m_successCallback.clear(); |
| 76 m_errorCallback.clear(); | 75 m_errorCallback.clear(); |
| 77 } | 76 } |
| 78 | 77 |
| 79 void MIDIAccessPromise::reject(DOMError* error) | 78 void MIDIAccessPromise::reject(PassRefPtr<DOMError> error) |
| 80 { | 79 { |
| 81 if (m_state == Pending) { | 80 if (m_state == Pending) { |
| 82 if (m_errorCallback) { | 81 if (m_errorCallback) { |
| 83 m_state = Invoked; | 82 m_state = Invoked; |
| 84 m_errorCallback->handleEvent(error); | 83 m_errorCallback->handleEvent(error.leakRef()); |
| 85 } else { | 84 } else { |
| 86 m_state = Rejected; | 85 m_state = Rejected; |
| 87 m_error = adoptRef(error); | 86 m_error = error; |
| 88 } | 87 } |
| 89 unsetPendingActivity(this); | 88 unsetPendingActivity(this); |
| 90 } | 89 } |
| 91 m_successCallback.clear(); | 90 m_successCallback.clear(); |
| 92 m_errorCallback.clear(); | 91 m_errorCallback.clear(); |
| 93 } | 92 } |
| 94 | 93 |
| 95 void MIDIAccessPromise::then(PassRefPtr<MIDISuccessCallback> successCallback, Pa
ssRefPtr<MIDIErrorCallback> errorCallback) | 94 void MIDIAccessPromise::then(PassRefPtr<MIDISuccessCallback> successCallback, Pa
ssRefPtr<MIDIErrorCallback> errorCallback) |
| 96 { | 95 { |
| 96 // Lazily request access. |
| 97 if (!m_access) { |
| 98 setPendingActivity(this); |
| 99 m_access = MIDIAccess::create(scriptExecutionContext(), this); |
| 100 } |
| 101 |
| 97 switch (m_state) { | 102 switch (m_state) { |
| 98 case Accepted: | 103 case Accepted: |
| 99 successCallback->handleEvent(m_access.release().leakRef(), m_options->sy
sexEnabled); | 104 successCallback->handleEvent(m_access.release().leakRef(), m_options->sy
sexEnabled); |
| 100 m_options.clear(); | 105 m_options.clear(); |
| 101 m_state = Invoked; | 106 m_state = Invoked; |
| 102 break; | 107 break; |
| 103 case Rejected: | 108 case Rejected: |
| 104 errorCallback->handleEvent(m_error.release().leakRef()); | 109 errorCallback->handleEvent(m_error.release().leakRef()); |
| 105 m_state = Invoked; | 110 m_state = Invoked; |
| 106 break; | 111 break; |
| 107 case Pending: | 112 case Pending: |
| 108 m_successCallback = successCallback; | 113 m_successCallback = successCallback; |
| 109 m_errorCallback = errorCallback; | 114 m_errorCallback = errorCallback; |
| 110 break; | 115 break; |
| 111 case Invoked: | 116 case Invoked: |
| 112 break; | 117 break; |
| 113 default: | 118 default: |
| 114 ASSERT_NOT_REACHED(); | 119 ASSERT_NOT_REACHED(); |
| 115 } | 120 } |
| 116 } | 121 } |
| 117 | 122 |
| 118 } // namespace WebCore | 123 } // namespace WebCore |
| OLD | NEW |