Skip to content

Commit

Permalink
Adapt speech synthesis changes from 2.28
Browse files Browse the repository at this point in the history
- Add missing functions
- Add proper error handling (similar to #823 with optional as in #875) for both UIProcess and WebProcess
  • Loading branch information
Scony authored and magomez committed Oct 3, 2024
1 parent 1d43040 commit b666c24
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 26 deletions.
10 changes: 10 additions & 0 deletions Source/WebCore/Headers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,16 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS
Modules/reporting/TestReportBody.h
Modules/reporting/ViolationReportType.h

Modules/speech/LocalDOMWindowSpeechSynthesis.h
Modules/speech/SpeechSynthesisErrorCode.h
Modules/speech/SpeechSynthesisErrorEvent.h
Modules/speech/SpeechSynthesisErrorEventInit.h
Modules/speech/SpeechSynthesisEvent.h
Modules/speech/SpeechSynthesisEventInit.h
Modules/speech/SpeechSynthesis.h
Modules/speech/SpeechSynthesisUtterance.h
Modules/speech/SpeechSynthesisVoice.h

Modules/speech/SpeechRecognitionCaptureSource.h
Modules/speech/SpeechRecognitionCaptureSourceImpl.h
Modules/speech/SpeechRecognitionConnection.h
Expand Down
22 changes: 11 additions & 11 deletions Source/WebCore/Modules/speech/SpeechSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void SpeechSynthesis::setPlatformSynthesizer(Ref<PlatformSpeechSynthesizer>&& sy
m_voiceList = std::nullopt;
m_utteranceQueue.clear();
// Finish current utterance.
speakingErrorOccurred();
speakingErrorOccurred(SpeechSynthesisErrorCode::Canceled);
m_isPaused = false;
m_speechSynthesisClient = nullptr;
}
Expand Down Expand Up @@ -174,7 +174,7 @@ void SpeechSynthesis::cancel()
m_speechSynthesisClient->cancel();
// If we wait for cancel to callback speakingErrorOccurred, then m_currentSpeechUtterance will be null
// and the event won't be processed. Instead we process the error immediately.
speakingErrorOccurred();
speakingErrorOccurred(SpeechSynthesisErrorCode::Canceled);
m_currentSpeechUtterance = nullptr;
} else if (m_platformSpeechSynthesizer)
m_platformSpeechSynthesizer->cancel();
Expand Down Expand Up @@ -202,18 +202,18 @@ void SpeechSynthesis::resumeSynthesis()
}
}

void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance& utterance, bool errorOccurred)
void SpeechSynthesis::handleSpeakingCompleted(SpeechSynthesisUtterance& utterance, std::optional<SpeechSynthesisErrorCode> error)
{
ASSERT(m_currentSpeechUtterance);
Ref<SpeechSynthesisUtterance> protect(utterance);

m_currentSpeechUtterance = nullptr;

if (errorOccurred)
utterance.errorEventOccurred(eventNames().errorEvent, SpeechSynthesisErrorCode::Canceled);
if (error)
utterance.errorEventOccurred(eventNames().errorEvent, *error);
else
utterance.eventOccurred(eventNames().endEvent, 0, 0, String());

if (m_utteranceQueue.size()) {
Ref<SpeechSynthesisUtterance> firstUtterance = m_utteranceQueue.takeFirst();
ASSERT(&utterance == firstUtterance.ptr());
Expand Down Expand Up @@ -272,11 +272,11 @@ void SpeechSynthesis::didResumeSpeaking()
didResumeSpeaking(*protectedCurrentSpeechUtterance()->platformUtterance());
}

void SpeechSynthesis::speakingErrorOccurred()
void SpeechSynthesis::speakingErrorOccurred(std::optional<SpeechSynthesisErrorCode> error)
{
if (!m_currentSpeechUtterance)
return;
speakingErrorOccurred(*protectedCurrentSpeechUtterance()->platformUtterance());
speakingErrorOccurred(*protectedCurrentSpeechUtterance()->platformUtterance(), error);
}

void SpeechSynthesis::boundaryEventOccurred(bool wordBoundary, unsigned charIndex, unsigned charLength)
Expand Down Expand Up @@ -314,13 +314,13 @@ void SpeechSynthesis::didResumeSpeaking(PlatformSpeechSynthesisUtterance& uttera
void SpeechSynthesis::didFinishSpeaking(PlatformSpeechSynthesisUtterance& utterance)
{
if (utterance.client())
handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), false);
handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), std::nullopt);
}

void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance& utterance)
void SpeechSynthesis::speakingErrorOccurred(PlatformSpeechSynthesisUtterance& utterance, std::optional<SpeechSynthesisErrorCode> error)
{
if (utterance.client())
handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), true);
handleSpeakingCompleted(static_cast<SpeechSynthesisUtterance&>(*utterance.client()), error);
}

RefPtr<SpeechSynthesisUtterance> SpeechSynthesis::protectedCurrentSpeechUtterance()
Expand Down
6 changes: 3 additions & 3 deletions Source/WebCore/Modules/speech/SpeechSynthesis.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,23 @@ class SpeechSynthesis : public PlatformSpeechSynthesizerClient, public SpeechSyn
void didPauseSpeaking(PlatformSpeechSynthesisUtterance&) override;
void didResumeSpeaking(PlatformSpeechSynthesisUtterance&) override;
void didFinishSpeaking(PlatformSpeechSynthesisUtterance&) override;
void speakingErrorOccurred(PlatformSpeechSynthesisUtterance&) override;
void speakingErrorOccurred(PlatformSpeechSynthesisUtterance&, std::optional<SpeechSynthesisErrorCode>) override;
void boundaryEventOccurred(PlatformSpeechSynthesisUtterance&, SpeechBoundary, unsigned charIndex, unsigned charLength) override;

// SpeechSynthesisClientObserver
void didStartSpeaking() override;
void didFinishSpeaking() override;
void didPauseSpeaking() override;
void didResumeSpeaking() override;
void speakingErrorOccurred() override;
void speakingErrorOccurred(std::optional<SpeechSynthesisErrorCode>) override;
void boundaryEventOccurred(bool wordBoundary, unsigned charIndex, unsigned charLength) override;
void voicesChanged() override;

// ActiveDOMObject
bool virtualHasPendingActivity() const final;

void startSpeakingImmediately(SpeechSynthesisUtterance&);
void handleSpeakingCompleted(SpeechSynthesisUtterance&, bool errorOccurred);
void handleSpeakingCompleted(SpeechSynthesisUtterance&, std::optional<SpeechSynthesisErrorCode>);

// EventTarget
ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/Modules/speech/SpeechSynthesisErrorCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace WebCore {

enum class SpeechSynthesisErrorCode {
enum class SpeechSynthesisErrorCode : uint8_t {
Canceled,
Interrupted,
AudioBusy,
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/page/SpeechSynthesisClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace WebCore {
class PlatformSpeechSynthesisUtterance;
class SpeechSynthesisClientObserver;
class PlatformSpeechSynthesisVoice;
enum class SpeechSynthesisErrorCode : uint8_t;

class SpeechSynthesisClient : public CanMakeWeakPtr<SpeechSynthesisClient> {
public:
Expand All @@ -70,7 +71,7 @@ class SpeechSynthesisClientObserver : public CanMakeWeakPtr<SpeechSynthesisClien
virtual void didFinishSpeaking() = 0;
virtual void didPauseSpeaking() = 0;
virtual void didResumeSpeaking() = 0;
virtual void speakingErrorOccurred() = 0;
virtual void speakingErrorOccurred(std::optional<SpeechSynthesisErrorCode>) = 0;
virtual void boundaryEventOccurred(bool wordBoundary, unsigned charIndex, unsigned charLength) = 0;
virtual void voicesChanged() = 0;
};
Expand Down
5 changes: 3 additions & 2 deletions Source/WebCore/platform/PlatformSpeechSynthesizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#if ENABLE(SPEECH_SYNTHESIS)

#include "PlatformSpeechSynthesisVoice.h"
#include <wtf/RefPtr.h>
#include "SpeechSynthesisErrorCode.h"
#include <optional>
#include <wtf/Vector.h>

#if PLATFORM(COCOA)
Expand All @@ -55,7 +56,7 @@ class PlatformSpeechSynthesizerClient {
virtual void didFinishSpeaking(PlatformSpeechSynthesisUtterance&) = 0;
virtual void didPauseSpeaking(PlatformSpeechSynthesisUtterance&) = 0;
virtual void didResumeSpeaking(PlatformSpeechSynthesisUtterance&) = 0;
virtual void speakingErrorOccurred(PlatformSpeechSynthesisUtterance&) = 0;
virtual void speakingErrorOccurred(PlatformSpeechSynthesisUtterance&, std::optional<SpeechSynthesisErrorCode>) = 0;
virtual void boundaryEventOccurred(PlatformSpeechSynthesisUtterance&, SpeechBoundary, unsigned charIndex, unsigned charLength) = 0;
virtual void voicesDidChange() = 0;
protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void PlatformSpeechSynthesizerMock::cancel()

m_speakingFinishedTimer.stop();
auto utterance = std::exchange(m_utterance, nullptr);
client().speakingErrorOccurred(*utterance);
client().speakingErrorOccurred(*utterance, SpeechSynthesisErrorCode::Canceled);
}

void PlatformSpeechSynthesizerMock::pause()
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/UIProcess/WebPageProxyInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ struct WebPageProxy::Internals final : WebPopupMenuProxy::Client
void didFinishSpeaking(WebCore::PlatformSpeechSynthesisUtterance&) final;
void didPauseSpeaking(WebCore::PlatformSpeechSynthesisUtterance&) final;
void didResumeSpeaking(WebCore::PlatformSpeechSynthesisUtterance&) final;
void speakingErrorOccurred(WebCore::PlatformSpeechSynthesisUtterance&) final;
void speakingErrorOccurred(WebCore::PlatformSpeechSynthesisUtterance&, std::optional<WebCore::SpeechSynthesisErrorCode>) final;
void boundaryEventOccurred(WebCore::PlatformSpeechSynthesisUtterance&, WebCore::SpeechBoundary, unsigned characterIndex, unsigned characterLength) final;

// PlatformSpeechSynthesizerClient
Expand Down
7 changes: 5 additions & 2 deletions Source/WebKit/UIProcess/gstreamer/WebPageProxyGStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ void WebPageProxy::Internals::didResumeSpeaking(WebCore::PlatformSpeechSynthesis
handler();
}

void WebPageProxy::Internals::speakingErrorOccurred(WebCore::PlatformSpeechSynthesisUtterance&)
void WebPageProxy::Internals::speakingErrorOccurred(WebCore::PlatformSpeechSynthesisUtterance&, std::optional<WebCore::SpeechSynthesisErrorCode> error)
{
page.legacyMainFrameProcess().send(Messages::WebPage::SpeakingErrorOccurred(), page.webPageIDInMainFrameProcess());
if (!error)
page.legacyMainFrameProcess().send(Messages::WebPage::SpeakingErrorOccurred(std::nullopt), page.webPageIDInMainFrameProcess());
else
page.legacyMainFrameProcess().send(Messages::WebPage::SpeakingErrorOccurred(static_cast<uint8_t>(*error)), page.webPageIDInMainFrameProcess());
}

void WebPageProxy::Internals::boundaryEventOccurred(WebCore::PlatformSpeechSynthesisUtterance&, WebCore::SpeechBoundary speechBoundary, unsigned charIndex, unsigned charLength)
Expand Down
7 changes: 5 additions & 2 deletions Source/WebKit/WebProcess/WebPage/WebPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
#include <WebCore/PlatformKeyboardEvent.h>
#include <WebCore/PlatformMediaSessionManager.h>
#include <WebCore/PlatformMouseEvent.h>
#include <WebCore/PlatformSpeechSynthesizer.h>
#include <WebCore/PlatformStrategies.h>
#include <WebCore/PluginDocument.h>
#include <WebCore/PointerCaptureController.h>
Expand Down Expand Up @@ -8486,10 +8487,12 @@ void WebPage::systemPreviewActionTriggered(WebCore::SystemPreviewInfo previewInf
#endif

#if ENABLE(SPEECH_SYNTHESIS)
void WebPage::speakingErrorOccurred()
void WebPage::speakingErrorOccurred(std::optional<uint8_t> error)
{
if (auto observer = corePage()->speechSynthesisClient()->observer())
observer->speakingErrorOccurred();
observer->speakingErrorOccurred(!error
? std::nullopt
: std::make_optional(static_cast<WebCore::SpeechSynthesisErrorCode>(*error)));
}

void WebPage::boundaryEventOccurred(bool wordBoundary, unsigned charIndex, unsigned charLength)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/WebProcess/WebPage/WebPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,7 @@ class WebPage : public API::ObjectImpl<API::Object::Type::BundlePage>, public IP
#endif

#if ENABLE(SPEECH_SYNTHESIS)
void speakingErrorOccurred();
void speakingErrorOccurred(std::optional<uint8_t> error);
void boundaryEventOccurred(bool wordBoundary, unsigned charIndex, unsigned charLength);
void voicesDidChange();
#endif
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/WebProcess/WebPage/WebPage.messages.in
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ GenerateSyntheticEditingCommand(enum:uint8_t WebKit::SyntheticEditingCommandType
SimulateDeviceOrientationChange(double alpha, double beta, double gamma)

#if ENABLE(SPEECH_SYNTHESIS)
SpeakingErrorOccurred()
SpeakingErrorOccurred(std::optional<uint8_t> error)
BoundaryEventOccurred(bool wordBoundary, unsigned charIndex, unsigned charLength)
VoicesDidChange()
#endif
Expand Down

0 comments on commit b666c24

Please sign in to comment.