Index: third_party/WebKit/Source/modules/remoteplayback/RemotePlayback.cpp |
diff --git a/third_party/WebKit/Source/modules/remoteplayback/RemotePlayback.cpp b/third_party/WebKit/Source/modules/remoteplayback/RemotePlayback.cpp |
index c367c3c30e9cfa0d0d26a9199fa71be2d82cf9f9..1f3e14e4f4d67e2a62d50a914930f523ac5306dc 100644 |
--- a/third_party/WebKit/Source/modules/remoteplayback/RemotePlayback.cpp |
+++ b/third_party/WebKit/Source/modules/remoteplayback/RemotePlayback.cpp |
@@ -47,6 +47,29 @@ void RunNotifyInitialAvailabilityTask(ExecutionContext* context, |
} // anonymous namespace |
+RemotePlayback::AvailabilityCallback::AvailabilityCallback( |
+ RemotePlaybackAvailabilityCallback* callback) |
+ : bindings_cb_(this, callback) {} |
+ |
+RemotePlayback::AvailabilityCallback::AvailabilityCallback( |
+ std::unique_ptr<WTF::Closure> callback) |
+ : bindings_cb_(nullptr, nullptr), internal_cb_(std::move(callback)) {} |
+ |
+void RemotePlayback::AvailabilityCallback::Run(RemotePlayback* remote_playback, |
+ bool new_availability) { |
+ if (internal_cb_) { |
+ DCHECK(!bindings_cb_); |
+ (*internal_cb_.get())(); |
+ return; |
+ } |
+ |
+ bindings_cb_->call(remote_playback, new_availability); |
+} |
+ |
+DEFINE_TRACE_WRAPPERS(RemotePlayback::AvailabilityCallback) { |
+ visitor->TraceWrappers(bindings_cb_); |
+} |
+ |
// static |
RemotePlayback* RemotePlayback::Create(HTMLMediaElement& element) { |
return new RemotePlayback(element); |
@@ -86,28 +109,7 @@ ScriptPromise RemotePlayback::watchAvailability( |
return promise; |
} |
- int id; |
- do { |
- id = GetExecutionContext()->CircularSequentialID(); |
- } while ( |
- !availability_callbacks_ |
- .insert(id, TraceWrapperMember<RemotePlaybackAvailabilityCallback>( |
- this, callback)) |
- .is_new_entry); |
- |
- // Report the current availability via the callback. |
- // TODO(yuryu): Wrapping notifyInitialAvailability with WTF::Closure as |
- // InspectorInstrumentation requires a globally unique pointer to track tasks. |
- // We can remove the wrapper if InspectorInstrumentation returns a task id. |
- std::unique_ptr<WTF::Closure> task = WTF::Bind( |
- &RemotePlayback::NotifyInitialAvailability, WrapPersistent(this), id); |
- probe::AsyncTaskScheduled(GetExecutionContext(), "watchAvailabilityCallback", |
- task.get()); |
- TaskRunnerHelper::Get(TaskType::kMediaElementEvent, GetExecutionContext()) |
- ->PostTask(BLINK_FROM_HERE, |
- WTF::Bind(RunNotifyInitialAvailabilityTask, |
- WrapPersistent(GetExecutionContext()), |
- WTF::Passed(std::move(task)))); |
+ int id = WatchAvailabilityInternal(new AvailabilityCallback(callback)); |
// TODO(avayvod): Currently the availability is tracked for each media element |
// as soon as it's created, we probably want to limit that to when the |
@@ -130,15 +132,12 @@ ScriptPromise RemotePlayback::cancelWatchAvailability(ScriptState* script_state, |
return promise; |
} |
- auto iter = availability_callbacks_.Find(id); |
- if (iter == availability_callbacks_.end()) { |
+ if (!CancelWatchAvailabilityInternal(id)) { |
resolver->Reject(DOMException::Create( |
kNotFoundError, "A callback with the given id is not found.")); |
return promise; |
} |
- availability_callbacks_.erase(iter); |
- |
resolver->Resolve(); |
return promise; |
} |
@@ -200,13 +199,8 @@ ScriptPromise RemotePlayback::prompt(ScriptState* script_state) { |
return promise; |
} |
- if (state_ == WebRemotePlaybackState::kDisconnected) { |
- prompt_promise_resolver_ = resolver; |
- media_element_->RequestRemotePlayback(); |
- } else { |
- prompt_promise_resolver_ = resolver; |
- media_element_->RequestRemotePlaybackControl(); |
- } |
+ prompt_promise_resolver_ = resolver; |
+ PromptInternal(); |
return promise; |
} |
@@ -220,13 +214,50 @@ bool RemotePlayback::HasPendingActivity() const { |
prompt_promise_resolver_; |
} |
+void RemotePlayback::PromptInternal() { |
+ if (state_ == WebRemotePlaybackState::kDisconnected) |
+ media_element_->RequestRemotePlayback(); |
+ else |
+ media_element_->RequestRemotePlaybackControl(); |
+} |
+ |
+int RemotePlayback::WatchAvailabilityInternal(AvailabilityCallback* callback) { |
+ int id; |
+ do { |
+ id = GetExecutionContext()->CircularSequentialID(); |
+ } while (!availability_callbacks_.insert(id, callback).is_new_entry); |
+ |
+ // Report the current availability via the callback. |
+ // TODO(yuryu): Wrapping notifyInitialAvailability with WTF::Closure as |
+ // InspectorInstrumentation requires a globally unique pointer to track tasks. |
+ // We can remove the wrapper if InspectorInstrumentation returns a task id. |
+ std::unique_ptr<WTF::Closure> task = WTF::Bind( |
+ &RemotePlayback::NotifyInitialAvailability, WrapPersistent(this), id); |
+ probe::AsyncTaskScheduled(GetExecutionContext(), "watchAvailabilityCallback", |
+ task.get()); |
+ TaskRunnerHelper::Get(TaskType::kMediaElementEvent, GetExecutionContext()) |
+ ->PostTask(BLINK_FROM_HERE, |
+ WTF::Bind(RunNotifyInitialAvailabilityTask, |
+ WrapPersistent(GetExecutionContext()), |
+ WTF::Passed(std::move(task)))); |
+ return id; |
+} |
+ |
+bool RemotePlayback::CancelWatchAvailabilityInternal(int id) { |
+ auto iter = availability_callbacks_.Find(id); |
+ if (iter == availability_callbacks_.end()) |
+ return false; |
+ availability_callbacks_.erase(iter); |
+ return true; |
+} |
+ |
void RemotePlayback::NotifyInitialAvailability(int callback_id) { |
// May not find the callback if the website cancels it fast enough. |
auto iter = availability_callbacks_.Find(callback_id); |
if (iter == availability_callbacks_.end()) |
return; |
- iter->value->call(this, RemotePlaybackAvailable()); |
+ iter->value->Run(this, RemotePlaybackAvailable()); |
} |
void RemotePlayback::StateChanged(WebRemotePlaybackState state) { |
@@ -278,7 +309,7 @@ void RemotePlayback::AvailabilityChanged( |
return; |
for (auto& callback : availability_callbacks_.Values()) |
- callback->call(this, new_availability); |
+ callback->Run(this, new_availability); |
} |
void RemotePlayback::PromptCancelled() { |
@@ -314,11 +345,4 @@ DEFINE_TRACE(RemotePlayback) { |
EventTargetWithInlineData::Trace(visitor); |
} |
-DEFINE_TRACE_WRAPPERS(RemotePlayback) { |
- for (auto callback : availability_callbacks_.Values()) { |
- visitor->TraceWrappers(callback); |
- } |
- EventTargetWithInlineData::TraceWrappers(visitor); |
-} |
- |
} // namespace blink |