Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(657)

Side by Side Diff: third_party/WebKit/Source/modules/remoteplayback/RemotePlayback.cpp

Issue 2698153003: Reduce createSameThreadTask usage in modules/ (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "core/dom/Document.h" 11 #include "core/dom/Document.h"
12 #include "core/dom/ExecutionContextTask.h"
13 #include "core/dom/TaskRunnerHelper.h" 12 #include "core/dom/TaskRunnerHelper.h"
14 #include "core/events/Event.h" 13 #include "core/events/Event.h"
15 #include "core/html/HTMLMediaElement.h" 14 #include "core/html/HTMLMediaElement.h"
15 #include "core/inspector/InspectorInstrumentation.h"
16 #include "modules/EventTargetModules.h" 16 #include "modules/EventTargetModules.h"
17 #include "platform/MemoryCoordinator.h" 17 #include "platform/MemoryCoordinator.h"
18 #include "platform/UserGestureIndicator.h" 18 #include "platform/UserGestureIndicator.h"
19 19
20 namespace blink { 20 namespace blink {
21 21
22 namespace { 22 namespace {
23 23
24 const AtomicString& remotePlaybackStateToString(WebRemotePlaybackState state) { 24 const AtomicString& remotePlaybackStateToString(WebRemotePlaybackState state) {
25 DEFINE_STATIC_LOCAL(const AtomicString, connectingValue, ("connecting")); 25 DEFINE_STATIC_LOCAL(const AtomicString, connectingValue, ("connecting"));
(...skipping 28 matching lines...) Expand all
54 m_mediaElement(&element) {} 54 m_mediaElement(&element) {}
55 55
56 const AtomicString& RemotePlayback::interfaceName() const { 56 const AtomicString& RemotePlayback::interfaceName() const {
57 return EventTargetNames::RemotePlayback; 57 return EventTargetNames::RemotePlayback;
58 } 58 }
59 59
60 ExecutionContext* RemotePlayback::getExecutionContext() const { 60 ExecutionContext* RemotePlayback::getExecutionContext() const {
61 return &m_mediaElement->document(); 61 return &m_mediaElement->document();
62 } 62 }
63 63
64 static void runNotifyInitialAvailabilityTask(
nhiroki 2017/02/17 03:47:15 nit: Can you move this into the anonymous namespac
yuryu 2017/02/17 05:14:33 Done.
65 ExecutionContext* context,
66 std::unique_ptr<WTF::Closure> task) {
67 InspectorInstrumentation::AsyncTask asyncTask(context, task.get());
68 (*task)();
69 }
70
64 ScriptPromise RemotePlayback::watchAvailability( 71 ScriptPromise RemotePlayback::watchAvailability(
65 ScriptState* scriptState, 72 ScriptState* scriptState,
66 RemotePlaybackAvailabilityCallback* callback) { 73 RemotePlaybackAvailabilityCallback* callback) {
67 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 74 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
68 ScriptPromise promise = resolver->promise(); 75 ScriptPromise promise = resolver->promise();
69 76
70 if (m_mediaElement->fastHasAttribute(HTMLNames::disableremoteplaybackAttr)) { 77 if (m_mediaElement->fastHasAttribute(HTMLNames::disableremoteplaybackAttr)) {
71 resolver->reject(DOMException::create( 78 resolver->reject(DOMException::create(
72 InvalidStateError, "disableRemotePlayback attribute is present.")); 79 InvalidStateError, "disableRemotePlayback attribute is present."));
73 return promise; 80 return promise;
74 } 81 }
75 82
76 if (MemoryCoordinator::isLowEndDevice()) { 83 if (MemoryCoordinator::isLowEndDevice()) {
77 resolver->reject(DOMException::create( 84 resolver->reject(DOMException::create(
78 NotSupportedError, 85 NotSupportedError,
79 "Availability monitoring is not supported on this device.")); 86 "Availability monitoring is not supported on this device."));
80 return promise; 87 return promise;
81 } 88 }
82 89
83 int id; 90 int id;
84 do { 91 do {
85 id = getExecutionContext()->circularSequentialID(); 92 id = getExecutionContext()->circularSequentialID();
86 } while ( 93 } while (
87 !m_availabilityCallbacks 94 !m_availabilityCallbacks
88 .insert(id, TraceWrapperMember<RemotePlaybackAvailabilityCallback>( 95 .insert(id, TraceWrapperMember<RemotePlaybackAvailabilityCallback>(
89 this, callback)) 96 this, callback))
90 .isNewEntry); 97 .isNewEntry);
91 98
92 // Report the current availability via the callback. 99 // Report the current availability via the callback.
93 getExecutionContext()->postTask( 100 // TODO(yuryu): Wrapping notifyInitialAvailability with WTF::Closure as
94 TaskType::MediaElementEvent, BLINK_FROM_HERE, 101 // InspectorInstrumentation requires a globally unique pointer to track tasks.
95 createSameThreadTask(&RemotePlayback::notifyInitialAvailability, 102 // We can remove the wrapper if InspectorInstrumentation returns a task id.
96 wrapPersistent(this), id), 103 std::unique_ptr<WTF::Closure> task = WTF::bind(
97 "watchAvailabilityCallback"); 104 &RemotePlayback::notifyInitialAvailability, wrapPersistent(this), id);
105 InspectorInstrumentation::asyncTaskScheduled(
106 getExecutionContext(), "watchAvailabilityCallback", task.get());
107 TaskRunnerHelper::get(TaskType::MediaElementEvent, getExecutionContext())
108 ->postTask(BLINK_FROM_HERE,
109 WTF::bind(runNotifyInitialAvailabilityTask,
110 wrapPersistent(getExecutionContext()),
111 WTF::passed(std::move(task))));
98 112
99 // TODO(avayvod): Currently the availability is tracked for each media element 113 // TODO(avayvod): Currently the availability is tracked for each media element
100 // as soon as it's created, we probably want to limit that to when the 114 // as soon as it's created, we probably want to limit that to when the
101 // page/element is visible (see https://crbug.com/597281) and has default 115 // page/element is visible (see https://crbug.com/597281) and has default
102 // controls. If there are no default controls, we should also start tracking 116 // controls. If there are no default controls, we should also start tracking
103 // availability on demand meaning the Promise returned by watchAvailability() 117 // availability on demand meaning the Promise returned by watchAvailability()
104 // will be resolved asynchronously. 118 // will be resolved asynchronously.
105 resolver->resolve(id); 119 resolver->resolve(id);
106 return promise; 120 return promise;
107 } 121 }
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 } 315 }
302 316
303 DEFINE_TRACE_WRAPPERS(RemotePlayback) { 317 DEFINE_TRACE_WRAPPERS(RemotePlayback) {
304 for (auto callback : m_availabilityCallbacks.values()) { 318 for (auto callback : m_availabilityCallbacks.values()) {
305 visitor->traceWrappers(callback); 319 visitor->traceWrappers(callback);
306 } 320 }
307 EventTargetWithInlineData::traceWrappers(visitor); 321 EventTargetWithInlineData::traceWrappers(visitor);
308 } 322 }
309 323
310 } // namespace blink 324 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698