Commit 76749b35 authored by Etienne Bergeron's avatar Etienne Bergeron Committed by Commit Bot

Make Linux TTS PlatformImpl a leaky instance

This CL is making the TtsPlatformImpl instance a leaky
instance since the singleton is alive for the whole
duration of the process.

The use of WeakPtr is no longer required.

Bug: 1133813
Change-Id: Ice2ff18c88c9168c6471ceaa7613c7b2f344ce99
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2441201
Commit-Queue: Dominic Mazzoni <dmazzoni@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812795}
parent 9a9b71a2
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/debug/leak_annotations.h" #include "base/debug/leak_annotations.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/singleton.h" #include "base/no_destructor.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "base/task/thread_pool.h" #include "base/task/thread_pool.h"
#include "content/browser/speech/tts_platform_impl.h" #include "content/browser/speech/tts_platform_impl.h"
...@@ -37,6 +37,9 @@ struct SPDChromeVoice { ...@@ -37,6 +37,9 @@ struct SPDChromeVoice {
class TtsPlatformImplLinux : public TtsPlatformImpl { class TtsPlatformImplLinux : public TtsPlatformImpl {
public: public:
TtsPlatformImplLinux(const TtsPlatformImplLinux&) = delete;
TtsPlatformImplLinux& operator=(const TtsPlatformImplLinux&) = delete;
bool PlatformImplAvailable() override; bool PlatformImplAvailable() override;
void Speak(int utterance_id, void Speak(int utterance_id,
const std::string& utterance, const std::string& utterance,
...@@ -56,8 +59,8 @@ class TtsPlatformImplLinux : public TtsPlatformImpl { ...@@ -56,8 +59,8 @@ class TtsPlatformImplLinux : public TtsPlatformImpl {
static TtsPlatformImplLinux* GetInstance(); static TtsPlatformImplLinux* GetInstance();
private: private:
friend base::NoDestructor<TtsPlatformImplLinux>;
TtsPlatformImplLinux(); TtsPlatformImplLinux();
~TtsPlatformImplLinux() override;
// Initiate the connection with the speech dispatcher. // Initiate the connection with the speech dispatcher.
void Initialize(); void Initialize();
...@@ -89,23 +92,17 @@ class TtsPlatformImplLinux : public TtsPlatformImpl { ...@@ -89,23 +92,17 @@ class TtsPlatformImplLinux : public TtsPlatformImpl {
// These apply to the current utterance only. // These apply to the current utterance only.
std::string utterance_; std::string utterance_;
int utterance_id_; int utterance_id_ = 0;
// Map a string composed of a voicename and module to the voicename. Used to // Map a string composed of a voicename and module to the voicename. Used to
// uniquely identify a voice across all available modules. // uniquely identify a voice across all available modules.
std::unique_ptr<std::map<std::string, SPDChromeVoice>> all_native_voices_; std::unique_ptr<std::map<std::string, SPDChromeVoice>> all_native_voices_;
friend struct base::DefaultSingletonTraits<TtsPlatformImplLinux>;
base::WeakPtrFactory<TtsPlatformImplLinux> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplLinux);
}; };
// static // static
SPDNotificationType TtsPlatformImplLinux::current_notification_ = SPD_EVENT_END; SPDNotificationType TtsPlatformImplLinux::current_notification_ = SPD_EVENT_END;
TtsPlatformImplLinux::TtsPlatformImplLinux() : utterance_id_(0) { TtsPlatformImplLinux::TtsPlatformImplLinux() {
const base::CommandLine& command_line = const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess(); *base::CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(switches::kEnableSpeechDispatcher)) if (!command_line.HasSwitch(switches::kEnableSpeechDispatcher))
...@@ -146,14 +143,6 @@ void TtsPlatformImplLinux::Initialize() { ...@@ -146,14 +143,6 @@ void TtsPlatformImplLinux::Initialize() {
libspeechd_loader_.spd_set_notification_on(conn_, SPD_RESUME); libspeechd_loader_.spd_set_notification_on(conn_, SPD_RESUME);
} }
TtsPlatformImplLinux::~TtsPlatformImplLinux() {
base::AutoLock lock(initialization_lock_);
if (conn_) {
libspeechd_loader_.spd_close(conn_);
conn_ = nullptr;
}
}
void TtsPlatformImplLinux::Reset() { void TtsPlatformImplLinux::Reset() {
base::AutoLock lock(initialization_lock_); base::AutoLock lock(initialization_lock_);
if (conn_) if (conn_)
...@@ -186,7 +175,7 @@ void TtsPlatformImplLinux::Speak( ...@@ -186,7 +175,7 @@ void TtsPlatformImplLinux::Speak(
// Parse SSML and process speech. // Parse SSML and process speech.
TtsController::GetInstance()->StripSSML( TtsController::GetInstance()->StripSSML(
utterance, base::BindOnce(&TtsPlatformImplLinux::ProcessSpeech, utterance, base::BindOnce(&TtsPlatformImplLinux::ProcessSpeech,
weak_factory_.GetWeakPtr(), utterance_id, lang, base::Unretained(this), utterance_id, lang,
voice, params, std::move(on_speak_finished))); voice, params, std::move(on_speak_finished)));
} }
...@@ -305,7 +294,6 @@ void TtsPlatformImplLinux::GetVoices(std::vector<VoiceData>* out_voices) { ...@@ -305,7 +294,6 @@ void TtsPlatformImplLinux::GetVoices(std::vector<VoiceData>* out_voices) {
} }
void TtsPlatformImplLinux::OnSpeechEvent(SPDNotificationType type) { void TtsPlatformImplLinux::OnSpeechEvent(SPDNotificationType type) {
// hummmmmm
TtsController* controller = TtsController::GetInstance(); TtsController* controller = TtsController::GetInstance();
switch (type) { switch (type) {
case SPD_EVENT_BEGIN: case SPD_EVENT_BEGIN:
...@@ -374,9 +362,8 @@ void TtsPlatformImplLinux::IndexMarkCallback(size_t msg_id, ...@@ -374,9 +362,8 @@ void TtsPlatformImplLinux::IndexMarkCallback(size_t msg_id,
// static // static
TtsPlatformImplLinux* TtsPlatformImplLinux::GetInstance() { TtsPlatformImplLinux* TtsPlatformImplLinux::GetInstance() {
return base::Singleton< static base::NoDestructor<TtsPlatformImplLinux> tts_platform;
TtsPlatformImplLinux, return tts_platform.get();
base::LeakySingletonTraits<TtsPlatformImplLinux>>::get();
} }
// static // static
......
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