OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "modules/remoteplayback/RemotePlayback.h" | 5 #include "modules/remoteplayback/RemotePlayback.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
8 #include "bindings/modules/v8/RemotePlaybackAvailabilityCallback.h" | 8 #include "bindings/modules/v8/RemotePlaybackAvailabilityCallback.h" |
9 #include "core/HTMLNames.h" | 9 #include "core/HTMLNames.h" |
10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 return promise; | 79 return promise; |
80 } | 80 } |
81 | 81 |
82 if (MemoryCoordinator::IsLowEndDevice()) { | 82 if (MemoryCoordinator::IsLowEndDevice()) { |
83 resolver->Reject(DOMException::Create( | 83 resolver->Reject(DOMException::Create( |
84 kNotSupportedError, | 84 kNotSupportedError, |
85 "Availability monitoring is not supported on this device.")); | 85 "Availability monitoring is not supported on this device.")); |
86 return promise; | 86 return promise; |
87 } | 87 } |
88 | 88 |
89 int id; | 89 std::unique_ptr<WTF::Closure> callback_closure = |
90 do { | 90 WTF::Bind(&RemotePlayback::AvailabilityCallbackWithBindings, |
91 id = GetExecutionContext()->CircularSequentialID(); | 91 WrapPersistent(this), WrapPersistent(callback)); |
92 } while ( | 92 int id = WatchAvailabilityInternal(std::move(callback_closure)); |
93 !availability_callbacks_ | |
94 .insert(id, TraceWrapperMember<RemotePlaybackAvailabilityCallback>( | |
95 this, callback)) | |
96 .is_new_entry); | |
97 | |
98 // Report the current availability via the callback. | |
99 // TODO(yuryu): Wrapping notifyInitialAvailability with WTF::Closure as | |
100 // InspectorInstrumentation requires a globally unique pointer to track tasks. | |
101 // We can remove the wrapper if InspectorInstrumentation returns a task id. | |
102 std::unique_ptr<WTF::Closure> task = WTF::Bind( | |
103 &RemotePlayback::NotifyInitialAvailability, WrapPersistent(this), id); | |
104 probe::AsyncTaskScheduled(GetExecutionContext(), "watchAvailabilityCallback", | |
105 task.get()); | |
106 TaskRunnerHelper::Get(TaskType::kMediaElementEvent, GetExecutionContext()) | |
107 ->PostTask(BLINK_FROM_HERE, | |
108 WTF::Bind(RunNotifyInitialAvailabilityTask, | |
109 WrapPersistent(GetExecutionContext()), | |
110 WTF::Passed(std::move(task)))); | |
111 | 93 |
112 // TODO(avayvod): Currently the availability is tracked for each media element | 94 // TODO(avayvod): Currently the availability is tracked for each media element |
113 // as soon as it's created, we probably want to limit that to when the | 95 // as soon as it's created, we probably want to limit that to when the |
114 // page/element is visible (see https://crbug.com/597281) and has default | 96 // page/element is visible (see https://crbug.com/597281) and has default |
115 // controls. If there are no default controls, we should also start tracking | 97 // controls. If there are no default controls, we should also start tracking |
116 // availability on demand meaning the Promise returned by watchAvailability() | 98 // availability on demand meaning the Promise returned by watchAvailability() |
117 // will be resolved asynchronously. | 99 // will be resolved asynchronously. |
118 resolver->Resolve(id); | 100 resolver->Resolve(id); |
119 return promise; | 101 return promise; |
120 } | 102 } |
121 | 103 |
122 ScriptPromise RemotePlayback::cancelWatchAvailability(ScriptState* script_state, | 104 ScriptPromise RemotePlayback::cancelWatchAvailability(ScriptState* script_state, |
123 int id) { | 105 int id) { |
124 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 106 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
125 ScriptPromise promise = resolver->Promise(); | 107 ScriptPromise promise = resolver->Promise(); |
126 | 108 |
127 if (media_element_->FastHasAttribute(HTMLNames::disableremoteplaybackAttr)) { | 109 if (media_element_->FastHasAttribute(HTMLNames::disableremoteplaybackAttr)) { |
128 resolver->Reject(DOMException::Create( | 110 resolver->Reject(DOMException::Create( |
129 kInvalidStateError, "disableRemotePlayback attribute is present.")); | 111 kInvalidStateError, "disableRemotePlayback attribute is present.")); |
130 return promise; | 112 return promise; |
131 } | 113 } |
132 | 114 |
133 auto iter = availability_callbacks_.Find(id); | 115 if (!CancelWatchAvailabilityInternal(id)) { |
134 if (iter == availability_callbacks_.end()) { | |
135 resolver->Reject(DOMException::Create( | 116 resolver->Reject(DOMException::Create( |
136 kNotFoundError, "A callback with the given id is not found.")); | 117 kNotFoundError, "A callback with the given id is not found.")); |
137 return promise; | 118 return promise; |
138 } | 119 } |
139 | 120 |
140 availability_callbacks_.erase(iter); | |
141 | |
142 resolver->Resolve(); | 121 resolver->Resolve(); |
143 return promise; | 122 return promise; |
144 } | 123 } |
145 | 124 |
146 ScriptPromise RemotePlayback::cancelWatchAvailability( | 125 ScriptPromise RemotePlayback::cancelWatchAvailability( |
147 ScriptState* script_state) { | 126 ScriptState* script_state) { |
148 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 127 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
149 ScriptPromise promise = resolver->Promise(); | 128 ScriptPromise promise = resolver->Promise(); |
150 | 129 |
151 if (media_element_->FastHasAttribute(HTMLNames::disableremoteplaybackAttr)) { | 130 if (media_element_->FastHasAttribute(HTMLNames::disableremoteplaybackAttr)) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 } | 172 } |
194 | 173 |
195 if (availability_ == WebRemotePlaybackAvailability::kSourceNotSupported || | 174 if (availability_ == WebRemotePlaybackAvailability::kSourceNotSupported || |
196 availability_ == WebRemotePlaybackAvailability::kSourceNotCompatible) { | 175 availability_ == WebRemotePlaybackAvailability::kSourceNotCompatible) { |
197 resolver->Reject(DOMException::Create( | 176 resolver->Reject(DOMException::Create( |
198 kNotSupportedError, | 177 kNotSupportedError, |
199 "The currentSrc is not compatible with remote playback")); | 178 "The currentSrc is not compatible with remote playback")); |
200 return promise; | 179 return promise; |
201 } | 180 } |
202 | 181 |
203 if (state_ == WebRemotePlaybackState::kDisconnected) { | 182 prompt_promise_resolver_ = resolver; |
204 prompt_promise_resolver_ = resolver; | 183 PromptInternal(); |
205 media_element_->RequestRemotePlayback(); | |
206 } else { | |
207 prompt_promise_resolver_ = resolver; | |
208 media_element_->RequestRemotePlaybackControl(); | |
209 } | |
210 | 184 |
211 return promise; | 185 return promise; |
212 } | 186 } |
213 | 187 |
214 String RemotePlayback::state() const { | 188 String RemotePlayback::state() const { |
215 return RemotePlaybackStateToString(state_); | 189 return RemotePlaybackStateToString(state_); |
216 } | 190 } |
217 | 191 |
218 bool RemotePlayback::HasPendingActivity() const { | 192 bool RemotePlayback::HasPendingActivity() const { |
219 return HasEventListeners() || !availability_callbacks_.IsEmpty() || | 193 return HasEventListeners() || !availability_callbacks_.IsEmpty() || |
220 prompt_promise_resolver_; | 194 prompt_promise_resolver_; |
221 } | 195 } |
222 | 196 |
| 197 void RemotePlayback::PromptInternal() { |
| 198 if (state_ == WebRemotePlaybackState::kDisconnected) |
| 199 media_element_->RequestRemotePlayback(); |
| 200 else |
| 201 media_element_->RequestRemotePlaybackControl(); |
| 202 } |
| 203 |
| 204 int RemotePlayback::WatchAvailabilityInternal( |
| 205 std::unique_ptr<WTF::Closure> callback) { |
| 206 Member<WTF::Closure> callback_member(callback.release()); |
| 207 int id; |
| 208 do { |
| 209 id = GetExecutionContext()->CircularSequentialID(); |
| 210 } while (!availability_callbacks_.insert(id, callback_member).is_new_entry); |
| 211 |
| 212 // Report the current availability via the callback. |
| 213 // TODO(yuryu): Wrapping notifyInitialAvailability with WTF::Closure as |
| 214 // InspectorInstrumentation requires a globally unique pointer to track tasks. |
| 215 // We can remove the wrapper if InspectorInstrumentation returns a task id. |
| 216 std::unique_ptr<WTF::Closure> task = WTF::Bind( |
| 217 &RemotePlayback::NotifyInitialAvailability, WrapPersistent(this), id); |
| 218 probe::AsyncTaskScheduled(GetExecutionContext(), "watchAvailabilityCallback", |
| 219 task.get()); |
| 220 TaskRunnerHelper::Get(TaskType::kMediaElementEvent, GetExecutionContext()) |
| 221 ->PostTask(BLINK_FROM_HERE, |
| 222 WTF::Bind(RunNotifyInitialAvailabilityTask, |
| 223 WrapPersistent(GetExecutionContext()), |
| 224 WTF::Passed(std::move(task)))); |
| 225 return id; |
| 226 } |
| 227 |
| 228 bool RemotePlayback::CancelWatchAvailabilityInternal(int id) { |
| 229 auto iter = availability_callbacks_.Find(id); |
| 230 if (iter == availability_callbacks_.end()) |
| 231 return false; |
| 232 availability_callbacks_.erase(iter); |
| 233 return true; |
| 234 } |
| 235 |
| 236 void RemotePlayback::AvailabilityCallbackWithBindings( |
| 237 RemotePlaybackAvailabilityCallback* callback) { |
| 238 callback->call(this, RemotePlaybackAvailable()); |
| 239 } |
| 240 |
223 void RemotePlayback::NotifyInitialAvailability(int callback_id) { | 241 void RemotePlayback::NotifyInitialAvailability(int callback_id) { |
224 // May not find the callback if the website cancels it fast enough. | 242 // May not find the callback if the website cancels it fast enough. |
225 auto iter = availability_callbacks_.Find(callback_id); | 243 auto iter = availability_callbacks_.Find(callback_id); |
226 if (iter == availability_callbacks_.end()) | 244 if (iter == availability_callbacks_.end()) |
227 return; | 245 return; |
228 | 246 |
229 iter->value->call(this, RemotePlaybackAvailable()); | 247 (*iter->value)(); |
230 } | 248 } |
231 | 249 |
232 void RemotePlayback::StateChanged(WebRemotePlaybackState state) { | 250 void RemotePlayback::StateChanged(WebRemotePlaybackState state) { |
233 if (state_ == state) | 251 if (state_ == state) |
234 return; | 252 return; |
235 | 253 |
236 if (prompt_promise_resolver_) { | 254 if (prompt_promise_resolver_) { |
237 // Changing state to Disconnected from "disconnected" or "connecting" means | 255 // Changing state to Disconnected from "disconnected" or "connecting" means |
238 // that establishing connection with remote playback device failed. | 256 // that establishing connection with remote playback device failed. |
239 // Changing state to anything else means the state change intended by | 257 // Changing state to anything else means the state change intended by |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 if (availability_ == availability) | 289 if (availability_ == availability) |
272 return; | 290 return; |
273 | 291 |
274 bool old_availability = RemotePlaybackAvailable(); | 292 bool old_availability = RemotePlaybackAvailable(); |
275 availability_ = availability; | 293 availability_ = availability; |
276 bool new_availability = RemotePlaybackAvailable(); | 294 bool new_availability = RemotePlaybackAvailable(); |
277 if (new_availability == old_availability) | 295 if (new_availability == old_availability) |
278 return; | 296 return; |
279 | 297 |
280 for (auto& callback : availability_callbacks_.Values()) | 298 for (auto& callback : availability_callbacks_.Values()) |
281 callback->call(this, new_availability); | 299 (*callback)(); |
282 } | 300 } |
283 | 301 |
284 void RemotePlayback::PromptCancelled() { | 302 void RemotePlayback::PromptCancelled() { |
285 if (!prompt_promise_resolver_) | 303 if (!prompt_promise_resolver_) |
286 return; | 304 return; |
287 | 305 |
288 prompt_promise_resolver_->Reject( | 306 prompt_promise_resolver_->Reject( |
289 DOMException::Create(kNotAllowedError, "The prompt was dismissed.")); | 307 DOMException::Create(kNotAllowedError, "The prompt was dismissed.")); |
290 prompt_promise_resolver_ = nullptr; | 308 prompt_promise_resolver_ = nullptr; |
291 } | 309 } |
(...skipping 15 matching lines...) Expand all Loading... |
307 media_element_->RequestRemotePlaybackStop(); | 325 media_element_->RequestRemotePlaybackStop(); |
308 } | 326 } |
309 | 327 |
310 DEFINE_TRACE(RemotePlayback) { | 328 DEFINE_TRACE(RemotePlayback) { |
311 visitor->Trace(availability_callbacks_); | 329 visitor->Trace(availability_callbacks_); |
312 visitor->Trace(prompt_promise_resolver_); | 330 visitor->Trace(prompt_promise_resolver_); |
313 visitor->Trace(media_element_); | 331 visitor->Trace(media_element_); |
314 EventTargetWithInlineData::Trace(visitor); | 332 EventTargetWithInlineData::Trace(visitor); |
315 } | 333 } |
316 | 334 |
317 DEFINE_TRACE_WRAPPERS(RemotePlayback) { | |
318 for (auto callback : availability_callbacks_.Values()) { | |
319 visitor->TraceWrappers(callback); | |
320 } | |
321 EventTargetWithInlineData::TraceWrappers(visitor); | |
322 } | |
323 | |
324 } // namespace blink | 335 } // namespace blink |
OLD | NEW |