Commit b75436e5 authored by Hongchan Choi's avatar Hongchan Choi Committed by Commit Bot

Use SupportsWeakPtr for messaging from rendering thread to main thread

In cross-thread messaging, the associated execution context can be
already gone when a posted task is performed sometime later in the task
runner's queue.

By using WeakPtr, the task runner will not perform a scheduled task
in the queue when the target object is invalid.

Bug: 1057735
Change-Id: I3f218ad073ff5f6dcb08aba176d722c5c729316b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2086051Reviewed-by: default avatarRaymond Toy <rtoy@chromium.org>
Commit-Queue: Hongchan Choi <hongchan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747043}
parent ebde8d31
...@@ -178,7 +178,7 @@ void AudioWorkletHandler::SetProcessorOnRenderThread( ...@@ -178,7 +178,7 @@ void AudioWorkletHandler::SetProcessorOnRenderThread(
PostCrossThreadTask( PostCrossThreadTask(
*main_thread_task_runner_, FROM_HERE, *main_thread_task_runner_, FROM_HERE,
CrossThreadBindOnce( CrossThreadBindOnce(
&AudioWorkletHandler::NotifyProcessorError, WrapRefCounted(this), &AudioWorkletHandler::NotifyProcessorError, AsWeakPtr(),
AudioWorkletProcessorErrorState::kConstructionError)); AudioWorkletProcessorErrorState::kConstructionError));
} }
} }
...@@ -193,7 +193,7 @@ void AudioWorkletHandler::FinishProcessorOnRenderThread() { ...@@ -193,7 +193,7 @@ void AudioWorkletHandler::FinishProcessorOnRenderThread() {
PostCrossThreadTask( PostCrossThreadTask(
*main_thread_task_runner_, FROM_HERE, *main_thread_task_runner_, FROM_HERE,
CrossThreadBindOnce(&AudioWorkletHandler::NotifyProcessorError, CrossThreadBindOnce(&AudioWorkletHandler::NotifyProcessorError,
WrapRefCounted(this), error_state)); AsWeakPtr(), error_state));
} }
// TODO(hongchan): After this point, The handler has no more pending activity // TODO(hongchan): After this point, The handler has no more pending activity
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_WORKLET_NODE_H_ #define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_WORKLET_NODE_H_
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_audio_worklet_node_options.h" #include "third_party/blink/renderer/bindings/modules/v8/v8_audio_worklet_node_options.h"
#include "third_party/blink/renderer/modules/webaudio/audio_node.h" #include "third_party/blink/renderer/modules/webaudio/audio_node.h"
#include "third_party/blink/renderer/modules/webaudio/audio_param_map.h" #include "third_party/blink/renderer/modules/webaudio/audio_param_map.h"
...@@ -31,7 +32,9 @@ class ScriptState; ...@@ -31,7 +32,9 @@ class ScriptState;
// AudioWorkletNode <-> AudioWorkletHandler <==|==> AudioWorkletProcessor // AudioWorkletNode <-> AudioWorkletHandler <==|==> AudioWorkletProcessor
// (JS interface) (Renderer access) | (V8 audio processing) // (JS interface) (Renderer access) | (V8 audio processing)
class AudioWorkletHandler final : public AudioHandler { class AudioWorkletHandler final
: public AudioHandler,
public base::SupportsWeakPtr<AudioWorkletHandler> {
public: public:
static scoped_refptr<AudioWorkletHandler> Create( static scoped_refptr<AudioWorkletHandler> Create(
AudioNode&, AudioNode&,
......
...@@ -340,7 +340,7 @@ void DeferredTaskHandler::RequestToDeleteHandlersOnMainThread() { ...@@ -340,7 +340,7 @@ void DeferredTaskHandler::RequestToDeleteHandlersOnMainThread() {
PostCrossThreadTask( PostCrossThreadTask(
*task_runner_, FROM_HERE, *task_runner_, FROM_HERE,
CrossThreadBindOnce(&DeferredTaskHandler::DeleteHandlersOnMainThread, CrossThreadBindOnce(&DeferredTaskHandler::DeleteHandlersOnMainThread,
scoped_refptr<DeferredTaskHandler>(this))); AsWeakPtr()));
} }
void DeferredTaskHandler::DeleteHandlersOnMainThread() { void DeferredTaskHandler::DeleteHandlersOnMainThread() {
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <atomic> #include <atomic>
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "third_party/blink/renderer/modules/modules_export.h" #include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h" #include "third_party/blink/renderer/platform/wtf/hash_set.h"
...@@ -62,7 +63,8 @@ class AudioSummingJunction; ...@@ -62,7 +63,8 @@ class AudioSummingJunction;
// - GC happens and it collects the BaseAudioContext before the task execution. // - GC happens and it collects the BaseAudioContext before the task execution.
// //
class MODULES_EXPORT DeferredTaskHandler final class MODULES_EXPORT DeferredTaskHandler final
: public ThreadSafeRefCounted<DeferredTaskHandler> { : public ThreadSafeRefCounted<DeferredTaskHandler>,
public base::SupportsWeakPtr<DeferredTaskHandler> {
public: public:
static scoped_refptr<DeferredTaskHandler> Create( static scoped_refptr<DeferredTaskHandler> Create(
scoped_refptr<base::SingleThreadTaskRunner> task_runner); scoped_refptr<base::SingleThreadTaskRunner> task_runner);
......
...@@ -204,7 +204,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) { ...@@ -204,7 +204,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) {
PostCrossThreadTask( PostCrossThreadTask(
*task_runner_, FROM_HERE, *task_runner_, FROM_HERE,
CrossThreadBindOnce(&ScriptProcessorHandler::FireProcessEvent, CrossThreadBindOnce(&ScriptProcessorHandler::FireProcessEvent,
WrapRefCounted(this), double_buffer_index_)); AsWeakPtr(), double_buffer_index_));
} else { } else {
// If this node is in the offline audio context, use the // If this node is in the offline audio context, use the
// waitable event to synchronize to the offline rendering thread. // waitable event to synchronize to the offline rendering thread.
...@@ -215,7 +215,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) { ...@@ -215,7 +215,7 @@ void ScriptProcessorHandler::Process(uint32_t frames_to_process) {
*task_runner_, FROM_HERE, *task_runner_, FROM_HERE,
CrossThreadBindOnce( CrossThreadBindOnce(
&ScriptProcessorHandler::FireProcessEventForOfflineAudioContext, &ScriptProcessorHandler::FireProcessEventForOfflineAudioContext,
WrapRefCounted(this), double_buffer_index_, AsWeakPtr(), double_buffer_index_,
CrossThreadUnretained(waitable_event.get()))); CrossThreadUnretained(waitable_event.get())));
// Okay to block the offline audio rendering thread since it is // Okay to block the offline audio rendering thread since it is
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/waitable_event.h" #include "base/synchronization/waitable_event.h"
#include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h" #include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
#include "third_party/blink/renderer/modules/webaudio/audio_node.h" #include "third_party/blink/renderer/modules/webaudio/audio_node.h"
...@@ -51,7 +52,9 @@ class WaitableEvent; ...@@ -51,7 +52,9 @@ class WaitableEvent;
// periodically with an AudioProcessingEvent which has AudioBuffers for each // periodically with an AudioProcessingEvent which has AudioBuffers for each
// input and output. // input and output.
class ScriptProcessorHandler final : public AudioHandler { class ScriptProcessorHandler final
: public AudioHandler,
public base::SupportsWeakPtr<ScriptProcessorHandler> {
public: public:
static scoped_refptr<ScriptProcessorHandler> Create( static scoped_refptr<ScriptProcessorHandler> Create(
AudioNode&, AudioNode&,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment