Skip to content

Commit

Permalink
Make SpeechSynthesis an ActiveDOMObject
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=260207

Reviewed by Ryosuke Niwa.

Keep wrapper of SpeechSynthesis alive when voiceschanged event might be fired and it has event handler.

Test: fast/speechsynthesis/speech-synthesis-voiceschanged-gc.html.

* LayoutTests/fast/speechsynthesis/speech-synthesis-voiceschanged-gc-expected.txt: Added.
* LayoutTests/fast/speechsynthesis/speech-synthesis-voiceschanged-gc.html: Added.
* Source/WebCore/Modules/speech/SpeechSynthesis.cpp:
(WebCore::Ref<SpeechSynthesis>SpeechSynthesis::create):
(WebCore::SpeechSynthesis::SpeechSynthesis):
(WebCore::SpeechSynthesis::setPlatformSynthesizer):
(WebCore::SpeechSynthesis::voicesDidChange):
(WebCore::SpeechSynthesis::getVoices):
(WebCore::SpeechSynthesis::resumeSynthesis):
(WebCore::SpeechSynthesis::simulateVoicesListChange):
(WebCore::SpeechSynthesis::activeDOMObjectName const):
(WebCore::SpeechSynthesis::virtualHasPendingActivity const):
(WebCore::SpeechSynthesis::eventListenersDidChange):
(WebCore::SpeechSynthesis::resume): Deleted.
* Source/WebCore/Modules/speech/SpeechSynthesis.h:
* Source/WebCore/Modules/speech/SpeechSynthesis.idl:
* Source/WebCore/testing/Internals.cpp:
(WebCore::Internals::simulateSpeechSynthesizerVoiceListChange):
* Source/WebCore/testing/Internals.h:
* Source/WebCore/testing/Internals.idl:

Canonical link: https://commits.webkit.org/267385@main
  • Loading branch information
szewai committed Aug 29, 2023
1 parent f4bbd99 commit 5eb9dd2
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This tests SpeechSynthesis can fire voiceschanged event without crash.

On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".


PASS speechSynthesis.testProperty !== undefined is true
PASS speechSynthesis.testProperty is 1
PASS successfullyParsed is true

TEST COMPLETE

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<script>
description("This tests SpeechSynthesis can fire voiceschanged event without crash.");

if (window.internals)
window.internals.enableMockSpeechSynthesizer();

window.jsTestIsAsync = true;
if (window.testRunner)
testRunner.waitUntilDone();

speechSynthesis.testProperty = 1;
const voices = speechSynthesis.getVoices();
speechSynthesis.onvoiceschanged = () => {
shouldBeTrue("speechSynthesis.testProperty !== undefined");
shouldBe("speechSynthesis.testProperty", '1');
finishJSTest();
};
gc();

if (window.internals) {
setTimeout(() => {
if (window.internals)
window.internals.simulateSpeechSynthesizerVoiceListChange();
}, 0);
} else {
debug("Test requires window.internals to run.");
finishJSTest();
}
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
48 changes: 40 additions & 8 deletions Source/WebCore/Modules/speech/SpeechSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ WTF_MAKE_ISO_ALLOCATED_IMPL(SpeechSynthesis);

Ref<SpeechSynthesis>SpeechSynthesis::create(ScriptExecutionContext& context)
{
return adoptRef(*new SpeechSynthesis(context));
auto synthesis = adoptRef(*new SpeechSynthesis(context));
synthesis->suspendIfNeeded();
return synthesis;
}

SpeechSynthesis::SpeechSynthesis(ScriptExecutionContext& context)
: ContextDestructionObserver(&context)
: ActiveDOMObject(&context)
, m_currentSpeechUtterance(nullptr)
, m_isPaused(false)
, m_restrictions(NoRestrictions)
Expand All @@ -77,7 +79,8 @@ SpeechSynthesis::~SpeechSynthesis() = default;
void SpeechSynthesis::setPlatformSynthesizer(Ref<PlatformSpeechSynthesizer>&& synthesizer)
{
m_platformSpeechSynthesizer = synthesizer.ptr();
m_voiceList.clear();
if (m_voiceList)
m_voiceList = std::nullopt;
m_utteranceQueue.clear();
// Finish current utterance.
speakingErrorOccurred();
Expand All @@ -87,7 +90,9 @@ void SpeechSynthesis::setPlatformSynthesizer(Ref<PlatformSpeechSynthesizer>&& sy

void SpeechSynthesis::voicesDidChange()
{
m_voiceList.clear();
if (m_voiceList)
m_voiceList = std::nullopt;

dispatchEvent(Event::create(eventNames().voiceschangedEvent, Event::CanBubble::No, Event::IsCancelable::No));
}

Expand All @@ -100,15 +105,16 @@ PlatformSpeechSynthesizer& SpeechSynthesis::ensurePlatformSpeechSynthesizer()

const Vector<Ref<SpeechSynthesisVoice>>& SpeechSynthesis::getVoices()
{
if (!m_voiceList.isEmpty())
return m_voiceList;
if (m_voiceList)
return *m_voiceList;

// If the voiceList is empty, that's the cue to get the voices from the platform again.
auto& voiceList = m_speechSynthesisClient ? m_speechSynthesisClient->voiceList() : ensurePlatformSpeechSynthesizer().voiceList();
m_voiceList = voiceList.map([](auto& voice) {
return SpeechSynthesisVoice::create(*voice);
});
return m_voiceList;

return *m_voiceList;
}

bool SpeechSynthesis::speaking() const
Expand Down Expand Up @@ -186,7 +192,7 @@ void SpeechSynthesis::pause()
}
}

void SpeechSynthesis::resume()
void SpeechSynthesis::resumeSynthesis()
{
if (m_currentSpeechUtterance) {
if (m_speechSynthesisClient)
Expand Down Expand Up @@ -322,6 +328,32 @@ RefPtr<SpeechSynthesisUtterance> SpeechSynthesis::protectedCurrentSpeechUtteranc
return m_currentSpeechUtterance ? &m_currentSpeechUtterance->utterance() : nullptr;
}

void SpeechSynthesis::simulateVoicesListChange()
{
if (m_speechSynthesisClient) {
voicesChanged();
return;
}

if (m_platformSpeechSynthesizer)
voicesDidChange();
}

const char* SpeechSynthesis::activeDOMObjectName() const
{
return "SpeechSynthesis";
}

bool SpeechSynthesis::virtualHasPendingActivity() const
{
return m_voiceList && m_hasEventListener;
}

void SpeechSynthesis::eventListenersDidChange()
{
m_hasEventListener = hasEventListeners(eventNames().voiceschangedEvent);
}

} // namespace WebCore

#endif // ENABLE(SPEECH_SYNTHESIS)
22 changes: 15 additions & 7 deletions Source/WebCore/Modules/speech/SpeechSynthesis.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Document;
class PlatformSpeechSynthesizerClient;
class SpeechSynthesisVoice;

class SpeechSynthesis : public PlatformSpeechSynthesizerClient, public SpeechSynthesisClientObserver, public RefCounted<SpeechSynthesis>, public ContextDestructionObserver, public EventTarget {
class SpeechSynthesis : public PlatformSpeechSynthesizerClient, public SpeechSynthesisClientObserver, public RefCounted<SpeechSynthesis>, public ActiveDOMObject, public EventTarget {
WTF_MAKE_ISO_ALLOCATED(SpeechSynthesis);
public:
static Ref<SpeechSynthesis> create(ScriptExecutionContext&);
Expand All @@ -58,7 +58,7 @@ class SpeechSynthesis : public PlatformSpeechSynthesizerClient, public SpeechSyn
void speak(SpeechSynthesisUtterance&);
void cancel();
void pause();
void resume();
void resumeSynthesis();

const Vector<Ref<SpeechSynthesisVoice>>& getVoices();

Expand All @@ -74,12 +74,13 @@ class SpeechSynthesis : public PlatformSpeechSynthesizerClient, public SpeechSyn

bool userGestureRequiredForSpeechStart() const { return m_restrictions & RequireUserGestureForSpeechStartRestriction; }
void removeBehaviorRestriction(BehaviorRestrictions restriction) { m_restrictions &= ~restriction; }
WEBCORE_EXPORT void simulateVoicesListChange();

private:
SpeechSynthesis(ScriptExecutionContext&);
RefPtr<SpeechSynthesisUtterance> protectedCurrentSpeechUtterance();

// PlatformSpeechSynthesizerClient override methods.
// PlatformSpeechSynthesizerClient
void voicesDidChange() override;
void didStartSpeaking(PlatformSpeechSynthesisUtterance&) override;
void didPauseSpeaking(PlatformSpeechSynthesisUtterance&) override;
Expand All @@ -88,32 +89,39 @@ class SpeechSynthesis : public PlatformSpeechSynthesizerClient, public SpeechSyn
void speakingErrorOccurred(PlatformSpeechSynthesisUtterance&) override;
void boundaryEventOccurred(PlatformSpeechSynthesisUtterance&, SpeechBoundary, unsigned charIndex, unsigned charLength) override;

// SpeechSynthesisClient override methods
// SpeechSynthesisClientObserver
void didStartSpeaking() override;
void didFinishSpeaking() override;
void didPauseSpeaking() override;
void didResumeSpeaking() override;
void speakingErrorOccurred() override;
void boundaryEventOccurred(bool wordBoundary, unsigned charIndex, unsigned charLength) override;
void voicesChanged() override;


// ActiveDOMObject
const char* activeDOMObjectName() const final;
bool virtualHasPendingActivity() const final;

void startSpeakingImmediately(SpeechSynthesisUtterance&);
void handleSpeakingCompleted(SpeechSynthesisUtterance&, bool errorOccurred);

ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
// EventTarget
ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
EventTargetInterface eventTargetInterface() const final { return SpeechSynthesisEventTargetInterfaceType; }
void refEventTarget() final { ref(); }
void derefEventTarget() final { deref(); }
void eventListenersDidChange() final;

PlatformSpeechSynthesizer& ensurePlatformSpeechSynthesizer();

RefPtr<PlatformSpeechSynthesizer> m_platformSpeechSynthesizer;
Vector<Ref<SpeechSynthesisVoice>> m_voiceList;
std::optional<Vector<Ref<SpeechSynthesisVoice>>> m_voiceList;
std::unique_ptr<SpeechSynthesisUtteranceActivity> m_currentSpeechUtterance;
Deque<Ref<SpeechSynthesisUtterance>> m_utteranceQueue;
bool m_isPaused;
BehaviorRestrictions m_restrictions;
WeakPtr<SpeechSynthesisClient> m_speechSynthesisClient;
bool m_hasEventListener { false };
};

} // namespace WebCore
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/Modules/speech/SpeechSynthesis.idl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

// https://wicg.github.io/speech-api/#speechsynthesis
[
ActiveDOMObject,
EnabledBySetting=SpeechSynthesisAPIEnabled,
Conditional=SPEECH_SYNTHESIS,
Exposed=Window,
Expand All @@ -39,6 +40,6 @@
undefined speak(SpeechSynthesisUtterance utterance);
undefined cancel();
undefined pause();
undefined resume();
[ImplementedAs=resumeSynthesis] undefined resume();
sequence<SpeechSynthesisVoice> getVoices();
};
14 changes: 14 additions & 0 deletions Source/WebCore/testing/Internals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,20 @@ ExceptionOr<void> Internals::setFormControlStateOfPreviousHistoryItem(const Vect
}

#if ENABLE(SPEECH_SYNTHESIS)
void Internals::simulateSpeechSynthesizerVoiceListChange()
{
if (m_platformSpeechSynthesizer) {
m_platformSpeechSynthesizer->client().voicesDidChange();
return;
}

RefPtr document = contextDocument();
if (!document || !document->domWindow())
return;

if (RefPtr synthesis = LocalDOMWindowSpeechSynthesis::speechSynthesis(*document->domWindow()))
synthesis->simulateVoicesListChange();
}

void Internals::enableMockSpeechSynthesizer()
{
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/testing/Internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ class Internals final : public RefCounted<Internals>, private ContextDestruction
void enableMockMediaCapabilities();

#if ENABLE(SPEECH_SYNTHESIS)
void simulateSpeechSynthesizerVoiceListChange();
void enableMockSpeechSynthesizer();
void enableMockSpeechSynthesizerForMediaElement(HTMLMediaElement&);
ExceptionOr<void> setSpeechUtteranceDuration(double);
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/testing/Internals.idl
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ typedef (FetchRequest or FetchResponse) FetchObject;
undefined enableMockMediaCapabilities();

[Conditional=SPEECH_SYNTHESIS] undefined enableMockSpeechSynthesizer();
[Conditional=SPEECH_SYNTHESIS] undefined simulateSpeechSynthesizerVoiceListChange();
[Conditional=SPEECH_SYNTHESIS] undefined enableMockSpeechSynthesizerForMediaElement(HTMLMediaElement element);
[Conditional=SPEECH_SYNTHESIS] undefined setSpeechUtteranceDuration(double duration);
[Conditional=SPEECH_SYNTHESIS] readonly attribute unsigned long minimumExpectedVoiceCount;
Expand Down

0 comments on commit 5eb9dd2

Please sign in to comment.