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