OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "modules/webmidi/MIDIAccessInitializer.h" | 6 #include "modules/webmidi/MIDIAccessInitializer.h" |
7 | 7 |
| 8 #include "bindings/v8/ScriptFunction.h" |
8 #include "bindings/v8/ScriptPromise.h" | 9 #include "bindings/v8/ScriptPromise.h" |
9 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | 10 #include "bindings/v8/ScriptPromiseResolverWithContext.h" |
10 #include "core/dom/DOMError.h" | 11 #include "core/dom/DOMError.h" |
11 #include "core/dom/Document.h" | 12 #include "core/dom/Document.h" |
12 #include "core/frame/Navigator.h" | |
13 #include "modules/webmidi/MIDIAccess.h" | 13 #include "modules/webmidi/MIDIAccess.h" |
14 #include "modules/webmidi/MIDIController.h" | 14 #include "modules/webmidi/MIDIController.h" |
15 #include "modules/webmidi/MIDIOptions.h" | 15 #include "modules/webmidi/MIDIOptions.h" |
16 #include "modules/webmidi/MIDIPort.h" | |
17 | 16 |
18 namespace WebCore { | 17 namespace WebCore { |
19 | 18 |
20 MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState, const MID
IOptions& options) | 19 class MIDIAccessInitializer::PostAction : public ScriptFunction { |
21 : ScriptPromiseResolverWithContext(scriptState, ScriptPromiseResolverWithCon
text::KeepAliveWhilePending) | 20 public: |
22 , m_options(options) | 21 static PassOwnPtr<MIDIAccessInitializer::PostAction> create(v8::Isolate* iso
late, WeakPtr<MIDIAccessInitializer> owner, State state) { return adoptPtr(new P
ostAction(isolate, owner, state)); } |
23 , m_sysexEnabled(false) | 22 |
24 { | 23 private: |
25 } | 24 PostAction(v8::Isolate* isolate, WeakPtr<MIDIAccessInitializer> owner, State
state): ScriptFunction(isolate), m_owner(owner), m_state(state) { } |
| 25 virtual ScriptValue call(ScriptValue value) OVERRIDE |
| 26 { |
| 27 if (!m_owner.get()) |
| 28 return value; |
| 29 m_owner->doPostAction(m_state); |
| 30 return value; |
| 31 } |
| 32 |
| 33 WeakPtr<MIDIAccessInitializer> m_owner; |
| 34 State m_state; |
| 35 }; |
26 | 36 |
27 MIDIAccessInitializer::~MIDIAccessInitializer() | 37 MIDIAccessInitializer::~MIDIAccessInitializer() |
28 { | 38 { |
29 // It is safe to cancel a request which is already finished or canceld. | 39 ASSERT(m_state != Requesting); |
| 40 } |
| 41 |
| 42 MIDIAccessInitializer::MIDIAccessInitializer(const MIDIOptions& options, MIDIAcc
ess* access) |
| 43 : m_state(Requesting) |
| 44 , m_weakPtrFactory(this) |
| 45 , m_options(options) |
| 46 , m_sysexEnabled(false) |
| 47 , m_access(access) |
| 48 { |
| 49 m_accessor = MIDIAccessor::create(this); |
| 50 } |
| 51 |
| 52 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version) |
| 53 { |
| 54 m_access->didAddInputPort(id, manufacturer, name, version); |
| 55 } |
| 56 |
| 57 void MIDIAccessInitializer::didAddOutputPort(const String& id, const String& man
ufacturer, const String& name, const String& version) |
| 58 { |
| 59 m_access->didAddOutputPort(id, manufacturer, name, version); |
| 60 } |
| 61 |
| 62 void MIDIAccessInitializer::didStartSession(bool success, const String& error, c
onst String& message) |
| 63 { |
| 64 if (success) |
| 65 m_resolver->resolve(m_access); |
| 66 else |
| 67 m_resolver->reject(DOMError::create(error, message)); |
| 68 } |
| 69 |
| 70 void MIDIAccessInitializer::setSysexEnabled(bool enable) |
| 71 { |
| 72 m_sysexEnabled = enable; |
| 73 if (enable) |
| 74 m_accessor->startSession(); |
| 75 else |
| 76 m_resolver->reject(DOMError::create("SecurityError")); |
| 77 } |
| 78 |
| 79 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const |
| 80 { |
| 81 return m_access->executionContext()->securityOrigin(); |
| 82 } |
| 83 |
| 84 void MIDIAccessInitializer::cancel() |
| 85 { |
| 86 if (m_state != Requesting) |
| 87 return; |
| 88 m_accessor.clear(); |
| 89 m_weakPtrFactory.revokeAll(); |
30 Document* document = toDocument(executionContext()); | 90 Document* document = toDocument(executionContext()); |
31 ASSERT(document); | 91 ASSERT(document); |
32 MIDIController* controller = MIDIController::from(document->frame()); | 92 MIDIController* controller = MIDIController::from(document->frame()); |
33 if (controller) | 93 ASSERT(controller); |
34 controller->cancelSysexPermissionRequest(this); | 94 controller->cancelSysexPermissionRequest(this); |
| 95 m_state = Stopped; |
35 } | 96 } |
36 | 97 |
37 ScriptPromise MIDIAccessInitializer::start() | 98 ExecutionContext* MIDIAccessInitializer::executionContext() const |
38 { | 99 { |
39 ScriptPromise promise = this->promise(); | 100 return m_access->executionContext(); |
40 m_accessor = MIDIAccessor::create(this); | 101 } |
| 102 |
| 103 void MIDIAccessInitializer::permissionDenied() |
| 104 { |
| 105 ASSERT(isMainThread()); |
| 106 m_resolver->reject(DOMError::create("SecurityError")); |
| 107 } |
| 108 |
| 109 ScriptPromise MIDIAccessInitializer::initialize(ScriptState* scriptState) |
| 110 { |
| 111 m_resolver = ScriptPromiseResolverWithContext::create(scriptState); |
| 112 ScriptPromise promise = m_resolver->promise(); |
| 113 promise.then(PostAction::create(scriptState->isolate(), m_weakPtrFactory.cre
ateWeakPtr(), Resolved), |
| 114 PostAction::create(scriptState->isolate(), m_weakPtrFactory.createWeakPt
r(), Stopped)); |
41 | 115 |
42 if (!m_options.sysex) { | 116 if (!m_options.sysex) { |
43 m_accessor->startSession(); | 117 m_accessor->startSession(); |
44 return promise; | 118 return promise; |
45 } | 119 } |
46 Document* document = toDocument(executionContext()); | 120 Document* document = toDocument(executionContext()); |
47 ASSERT(document); | 121 ASSERT(document); |
48 MIDIController* controller = MIDIController::from(document->frame()); | 122 MIDIController* controller = MIDIController::from(document->frame()); |
49 if (controller) { | 123 if (controller) { |
50 controller->requestSysexPermission(this); | 124 controller->requestSysexPermission(this); |
51 } else { | 125 } else { |
52 reject(DOMError::create("SecurityError")); | 126 m_resolver->reject(DOMError::create("SecurityError")); |
53 } | 127 } |
54 return promise; | 128 return promise; |
55 } | 129 } |
56 | 130 |
57 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version) | 131 void MIDIAccessInitializer::doPostAction(State state) |
58 { | 132 { |
59 ASSERT(m_accessor); | 133 ASSERT(m_state == Requesting); |
60 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MI
DIPortTypeInput, version)); | 134 ASSERT(state == Resolved || state == Stopped); |
61 } | 135 if (state == Resolved) { |
62 | 136 m_access->initialize(m_accessor.release(), m_sysexEnabled); |
63 void MIDIAccessInitializer::didAddOutputPort(const String& id, const String& man
ufacturer, const String& name, const String& version) | |
64 { | |
65 ASSERT(m_accessor); | |
66 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MI
DIPortTypeOutput, version)); | |
67 } | |
68 | |
69 void MIDIAccessInitializer::didStartSession(bool success, const String& error, c
onst String& message) | |
70 { | |
71 ASSERT(m_accessor); | |
72 if (success) { | |
73 resolve(MIDIAccess::create(m_accessor.release(), m_sysexEnabled, m_portD
escriptors, executionContext())); | |
74 } else { | |
75 reject(DOMError::create(error, message)); | |
76 } | 137 } |
77 } | 138 m_accessor.clear(); |
78 | 139 m_weakPtrFactory.revokeAll(); |
79 void MIDIAccessInitializer::setSysexEnabled(bool enable) | 140 m_state = state; |
80 { | |
81 m_sysexEnabled = enable; | |
82 if (enable) | |
83 m_accessor->startSession(); | |
84 else | |
85 reject(DOMError::create("SecurityError")); | |
86 } | |
87 | |
88 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const | |
89 { | |
90 return executionContext()->securityOrigin(); | |
91 } | |
92 | |
93 ExecutionContext* MIDIAccessInitializer::executionContext() const | |
94 { | |
95 return scriptState()->executionContext(); | |
96 } | 141 } |
97 | 142 |
98 } // namespace WebCore | 143 } // namespace WebCore |
OLD | NEW |