1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind_helpers.h"
6 #include "base/logging.h"
7 #include "base/prefs/scoped_user_pref_update.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/speech/tts_platform.h"
10 #include "chrome/common/extensions/extension_constants.h"
11 #include "chrome/common/pref_names.h"
15 // Trigger installing high-quality speech after this many utterances
16 // have been spoken in one session.
17 const int kHighQualitySpeechUtteranceThreshold
= 100;
19 } // anonymous namespace
21 // Chrome OS doesn't have native TTS, instead it includes a built-in
22 // component extension that provides speech synthesis. This class monitors
23 // use of this component extension and triggers installing a higher-quality
24 // speech synthesis extension if a certain number of utterances are spoken
25 // in a single session.
26 class TtsPlatformImplChromeOs
27 : public TtsPlatformImpl
{
29 // TtsPlatformImpl overrides:
30 virtual bool PlatformImplAvailable() OVERRIDE
{
36 const std::string
& utterance
,
37 const std::string
& lang
,
38 const VoiceData
& voice
,
39 const UtteranceContinuousParameters
& params
) OVERRIDE
{
43 virtual bool StopSpeaking() OVERRIDE
{ return false; }
44 virtual void Pause() OVERRIDE
{}
45 virtual void Resume() OVERRIDE
{}
46 virtual bool IsSpeaking() OVERRIDE
{ return false; }
47 virtual void GetVoices(std::vector
<VoiceData
>* out_voices
) OVERRIDE
{}
49 virtual void WillSpeakUtteranceWithVoice(
50 const Utterance
* utterance
,
51 const VoiceData
& voice_data
) OVERRIDE
;
53 // Get the single instance of this class.
54 static TtsPlatformImplChromeOs
* GetInstance();
57 TtsPlatformImplChromeOs();
58 virtual ~TtsPlatformImplChromeOs() {}
60 friend struct DefaultSingletonTraits
<TtsPlatformImplChromeOs
>;
62 // A count of the number of utterances spoken for each language
63 // using the built-in speech synthesis. When enough utterances have
64 // been spoken in a single session, automatically enable install
65 // the high-quality speech synthesis extension for that language.
66 base::hash_map
<std::string
, int> lang_utterance_count_
;
68 DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplChromeOs
);
71 TtsPlatformImplChromeOs::TtsPlatformImplChromeOs() {
74 void TtsPlatformImplChromeOs::WillSpeakUtteranceWithVoice(
75 const Utterance
* utterance
,
76 const VoiceData
& voice_data
) {
78 CHECK(utterance
->profile());
80 if (utterance
->profile()->IsOffTheRecord())
83 if (voice_data
.extension_id
!= extension_misc::kSpeechSynthesisExtensionId
)
86 lang_utterance_count_
[voice_data
.lang
]++;
87 if (lang_utterance_count_
[voice_data
.lang
] !=
88 kHighQualitySpeechUtteranceThreshold
) {
92 // Add this language to the list that are allowed to install a
93 // component extension for high-quality speech synthesis, overriding
94 // the lower-quality one.
95 ListPrefUpdate
updater(utterance
->profile()->GetPrefs(),
96 prefs::kHighQualitySpeechSynthesisLanguages
);
97 updater
->AppendIfNotPresent(new base::StringValue(voice_data
.lang
));
101 TtsPlatformImpl
* TtsPlatformImpl::GetInstance() {
102 return TtsPlatformImplChromeOs::GetInstance();
106 TtsPlatformImplChromeOs
*
107 TtsPlatformImplChromeOs::GetInstance() {
108 return Singleton
<TtsPlatformImplChromeOs
>::get();