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 22 matching lines...) Expand all Loading... |
33 | 33 |
34 #include "bindings/v8/ScriptFunction.h" | 34 #include "bindings/v8/ScriptFunction.h" |
35 #include "bindings/v8/ScriptPromise.h" | 35 #include "bindings/v8/ScriptPromise.h" |
36 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | 36 #include "bindings/v8/ScriptPromiseResolverWithContext.h" |
37 #include "core/dom/DOMError.h" | 37 #include "core/dom/DOMError.h" |
38 #include "core/dom/Document.h" | 38 #include "core/dom/Document.h" |
39 #include "modules/webmidi/MIDIAccess.h" | 39 #include "modules/webmidi/MIDIAccess.h" |
40 #include "modules/webmidi/MIDIConnectionEvent.h" | 40 #include "modules/webmidi/MIDIConnectionEvent.h" |
41 #include "modules/webmidi/MIDIController.h" | 41 #include "modules/webmidi/MIDIController.h" |
42 #include "modules/webmidi/MIDIOptions.h" | 42 #include "modules/webmidi/MIDIOptions.h" |
| 43 #include "modules/webmidi/MIDIPort.h" |
43 | 44 |
44 namespace WebCore { | 45 namespace WebCore { |
45 | 46 |
46 class MIDIAccessInitializer::PostAction : public ScriptFunction { | 47 MIDIAccessInitializer::MIDIAccessInitializer(const MIDIOptions& options) |
47 public: | 48 : m_resolver(nullptr) |
48 static PassOwnPtr<MIDIAccessInitializer::PostAction> create(v8::Isolate* iso
late, WeakPtr<MIDIAccessInitializer> owner, State state) { return adoptPtr(new P
ostAction(isolate, owner, state)); } | 49 , m_options(options) |
49 | 50 , m_sysexEnabled(false) |
50 private: | 51 { |
51 PostAction(v8::Isolate* isolate, WeakPtr<MIDIAccessInitializer> owner, State
state): ScriptFunction(isolate), m_owner(owner), m_state(state) { } | 52 } |
52 virtual ScriptValue call(ScriptValue value) OVERRIDE | |
53 { | |
54 if (!m_owner.get()) | |
55 return value; | |
56 m_owner->doPostAction(m_state); | |
57 return value; | |
58 } | |
59 | |
60 WeakPtr<MIDIAccessInitializer> m_owner; | |
61 State m_state; | |
62 }; | |
63 | 53 |
64 MIDIAccessInitializer::~MIDIAccessInitializer() | 54 MIDIAccessInitializer::~MIDIAccessInitializer() |
65 { | 55 { |
66 ASSERT(m_state != Requesting); | 56 cancel(); |
67 } | 57 } |
68 | 58 |
69 MIDIAccessInitializer::MIDIAccessInitializer(const MIDIOptions& options, MIDIAcc
ess* access) | 59 void MIDIAccessInitializer::start(AsyncInitializerResolver<MIDIAccessInitializer
>* resolver) |
70 : m_state(Requesting) | |
71 , m_weakPtrFactory(this) | |
72 , m_options(options) | |
73 , m_sysexEnabled(false) | |
74 , m_access(access) | |
75 { | 60 { |
| 61 m_resolver = resolver; |
76 m_accessor = MIDIAccessor::create(this); | 62 m_accessor = MIDIAccessor::create(this); |
| 63 |
| 64 if (!m_options.sysex) { |
| 65 m_accessor->startSession(); |
| 66 return; |
| 67 } |
| 68 Document* document = toDocument(executionContext()); |
| 69 ASSERT(document); |
| 70 MIDIController* controller = MIDIController::from(document->page()); |
| 71 if (controller) { |
| 72 controller->requestSysexPermission(this); |
| 73 } else { |
| 74 m_resolver->reject(DOMError::create("SecurityError")); |
| 75 } |
77 } | 76 } |
78 | 77 |
79 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version) | 78 void MIDIAccessInitializer::didAddInputPort(const String& id, const String& manu
facturer, const String& name, const String& version) |
80 { | 79 { |
81 m_access->didAddInputPort(id, manufacturer, name, version); | 80 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MI
DIPortTypeInput, version)); |
82 } | 81 } |
83 | 82 |
84 void MIDIAccessInitializer::didAddOutputPort(const String& id, const String& man
ufacturer, const String& name, const String& version) | 83 void MIDIAccessInitializer::didAddOutputPort(const String& id, const String& man
ufacturer, const String& name, const String& version) |
85 { | 84 { |
86 m_access->didAddOutputPort(id, manufacturer, name, version); | 85 m_portDescriptors.append(PortDescriptor(id, manufacturer, name, MIDIPort::MI
DIPortTypeOutput, version)); |
87 } | 86 } |
88 | 87 |
89 void MIDIAccessInitializer::didStartSession(bool success, const String& error, c
onst String& message) | 88 void MIDIAccessInitializer::didStartSession(bool success, const String& error, c
onst String& message) |
90 { | 89 { |
91 if (success) | 90 if (success) { |
92 m_resolver->resolve(m_access); | 91 m_resolver->resolve(MIDIAccess::create(m_accessor.release(), m_sysexEnab
led, m_portDescriptors, executionContext())); |
93 else | 92 } else { |
94 m_resolver->reject(DOMError::create(error, message)); | 93 m_resolver->reject(DOMError::create(error, message)); |
| 94 } |
95 } | 95 } |
96 | 96 |
97 void MIDIAccessInitializer::setSysexEnabled(bool enable) | 97 void MIDIAccessInitializer::setSysexEnabled(bool enable) |
98 { | 98 { |
99 m_sysexEnabled = enable; | 99 m_sysexEnabled = enable; |
100 if (enable) { | 100 if (enable) { |
101 m_accessor->startSession(); | 101 m_accessor->startSession(); |
102 } else { | 102 } else { |
103 m_resolver->reject(DOMError::create("SecurityError")); | 103 m_resolver->reject(DOMError::create("SecurityError")); |
104 } | 104 } |
105 } | 105 } |
106 | 106 |
107 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const | 107 SecurityOrigin* MIDIAccessInitializer::securityOrigin() const |
108 { | 108 { |
109 return m_access->executionContext()->securityOrigin(); | 109 return executionContext()->securityOrigin(); |
110 } | 110 } |
111 | 111 |
112 void MIDIAccessInitializer::cancel() | 112 void MIDIAccessInitializer::cancel() |
113 { | 113 { |
114 if (m_state != Requesting) | |
115 return; | |
116 m_accessor.clear(); | 114 m_accessor.clear(); |
117 m_weakPtrFactory.revokeAll(); | |
118 Document* document = toDocument(executionContext()); | 115 Document* document = toDocument(executionContext()); |
119 ASSERT(document); | 116 ASSERT(document); |
120 MIDIController* controller = MIDIController::from(document->page()); | 117 MIDIController* controller = MIDIController::from(document->page()); |
121 ASSERT(controller); | 118 ASSERT(controller); |
122 controller->cancelSysexPermissionRequest(this); | 119 controller->cancelSysexPermissionRequest(this); |
123 m_state = Stopped; | |
124 } | |
125 | |
126 ExecutionContext* MIDIAccessInitializer::executionContext() const | |
127 { | |
128 return m_access->executionContext(); | |
129 } | 120 } |
130 | 121 |
131 void MIDIAccessInitializer::permissionDenied() | 122 void MIDIAccessInitializer::permissionDenied() |
132 { | 123 { |
133 ASSERT(isMainThread()); | 124 ASSERT(isMainThread()); |
134 m_resolver->reject(DOMError::create("SecurityError")); | 125 m_resolver->reject(DOMError::create("SecurityError")); |
135 } | 126 } |
136 | 127 |
137 ScriptPromise MIDIAccessInitializer::initialize(ScriptState* scriptState) | 128 ExecutionContext* MIDIAccessInitializer::executionContext() const |
138 { | 129 { |
139 m_resolver = ScriptPromiseResolverWithContext::create(scriptState); | 130 ASSERT(m_resolver); |
140 ScriptPromise promise = m_resolver->promise(); | 131 return m_resolver->scriptState()->executionContext(); |
141 promise.then(PostAction::create(scriptState->isolate(), m_weakPtrFactory.cre
ateWeakPtr(), Resolved), | |
142 PostAction::create(scriptState->isolate(), m_weakPtrFactory.createWeakPt
r(), Stopped)); | |
143 | |
144 if (!m_options.sysex) { | |
145 m_accessor->startSession(); | |
146 return promise; | |
147 } | |
148 Document* document = toDocument(executionContext()); | |
149 ASSERT(document); | |
150 MIDIController* controller = MIDIController::from(document->page()); | |
151 if (controller) { | |
152 controller->requestSysexPermission(this); | |
153 } else { | |
154 m_resolver->reject(DOMError::create("SecurityError")); | |
155 } | |
156 return promise; | |
157 } | |
158 | |
159 void MIDIAccessInitializer::doPostAction(State state) | |
160 { | |
161 ASSERT(m_state == Requesting); | |
162 ASSERT(state == Resolved || state == Stopped); | |
163 if (state == Resolved) { | |
164 m_access->initialize(m_accessor.release(), m_sysexEnabled); | |
165 } | |
166 m_accessor.clear(); | |
167 m_weakPtrFactory.revokeAll(); | |
168 m_state = state; | |
169 } | 132 } |
170 | 133 |
171 } // namespace WebCore | 134 } // namespace WebCore |
OLD | NEW |