Commit b92acce8 authored by Etienne Bergeron's avatar Etienne Bergeron Committed by Commit Bot

Make Windows 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: Iceeb676cd23a1867132255b51c4fb3439e049985
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2441674
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@{#812791}
parent b8da407f
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/singleton.h" #include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
...@@ -34,6 +34,9 @@ const wchar_t kLanguageValue[] = L"Language"; ...@@ -34,6 +34,9 @@ const wchar_t kLanguageValue[] = L"Language";
class TtsPlatformImplWin : public TtsPlatformImpl { class TtsPlatformImplWin : public TtsPlatformImpl {
public: public:
TtsPlatformImplWin(const TtsPlatformImplWin&) = delete;
TtsPlatformImplWin& operator=(const TtsPlatformImplWin&) = delete;
bool PlatformImplAvailable() override { return true; } bool PlatformImplAvailable() override { return true; }
void Speak(int utterance_id, void Speak(int utterance_id,
...@@ -59,8 +62,8 @@ class TtsPlatformImplWin : public TtsPlatformImpl { ...@@ -59,8 +62,8 @@ class TtsPlatformImplWin : public TtsPlatformImpl {
static void __stdcall SpeechEventCallback(WPARAM w_param, LPARAM l_param); static void __stdcall SpeechEventCallback(WPARAM w_param, LPARAM l_param);
private: private:
friend base::NoDestructor<TtsPlatformImplWin>;
TtsPlatformImplWin(); TtsPlatformImplWin();
~TtsPlatformImplWin() override {}
void OnSpeechEvent(); void OnSpeechEvent();
...@@ -77,19 +80,13 @@ class TtsPlatformImplWin : public TtsPlatformImpl { ...@@ -77,19 +80,13 @@ class TtsPlatformImplWin : public TtsPlatformImpl {
// These apply to the current utterance only. // These apply to the current utterance only.
std::wstring utterance_; std::wstring utterance_;
int utterance_id_; int utterance_id_ = 0;
int prefix_len_; int prefix_len_ = 0;
ULONG stream_number_; ULONG stream_number_ = 0u;
int char_position_; int char_position_ = 0;
int char_length_; int char_length_ = 0;
bool paused_; bool paused_ = false;
std::string last_voice_name_; std::string last_voice_name_;
friend struct base::DefaultSingletonTraits<TtsPlatformImplWin>;
base::WeakPtrFactory<TtsPlatformImplWin> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplWin);
}; };
// static // static
...@@ -107,7 +104,7 @@ void TtsPlatformImplWin::Speak( ...@@ -107,7 +104,7 @@ void TtsPlatformImplWin::Speak(
// Parse SSML and process speech. // Parse SSML and process speech.
TtsController::GetInstance()->StripSSML( TtsController::GetInstance()->StripSSML(
utterance, base::BindOnce(&TtsPlatformImplWin::ProcessSpeech, utterance, base::BindOnce(&TtsPlatformImplWin::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)));
} }
...@@ -333,12 +330,7 @@ void TtsPlatformImplWin::SetVoiceFromName(const std::string& name) { ...@@ -333,12 +330,7 @@ void TtsPlatformImplWin::SetVoiceFromName(const std::string& name) {
} }
} }
TtsPlatformImplWin::TtsPlatformImplWin() TtsPlatformImplWin::TtsPlatformImplWin() {
: utterance_id_(0),
prefix_len_(0),
stream_number_(0),
char_position_(0),
paused_(false) {
::CoCreateInstance(CLSID_SpVoice, nullptr, CLSCTX_ALL, ::CoCreateInstance(CLSID_SpVoice, nullptr, CLSCTX_ALL,
IID_PPV_ARGS(&speech_synthesizer_)); IID_PPV_ARGS(&speech_synthesizer_));
if (speech_synthesizer_.Get()) { if (speech_synthesizer_.Get()) {
...@@ -354,8 +346,8 @@ TtsPlatformImplWin::TtsPlatformImplWin() ...@@ -354,8 +346,8 @@ TtsPlatformImplWin::TtsPlatformImplWin()
// static // static
TtsPlatformImplWin* TtsPlatformImplWin::GetInstance() { TtsPlatformImplWin* TtsPlatformImplWin::GetInstance() {
return base::Singleton<TtsPlatformImplWin, static base::NoDestructor<TtsPlatformImplWin> tts_platform;
base::LeakySingletonTraits<TtsPlatformImplWin>>::get(); return tts_platform.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