OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind_helpers.h" | 5 #include "chrome/browser/speech/tts_extension_loader_chromeos.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" | 6 #include "chrome/browser/speech/tts_platform.h" |
10 #include "chrome/common/extensions/extension_constants.h" | |
11 #include "chrome/common/pref_names.h" | |
12 | |
13 namespace { | |
14 | |
15 // Trigger installing high-quality speech after this many utterances | |
16 // have been spoken in one session. | |
17 const int kHighQualitySpeechUtteranceThreshold = 100; | |
18 | |
19 } // anonymous namespace | |
20 | 7 |
21 // Chrome OS doesn't have native TTS, instead it includes a built-in | 8 // Chrome OS doesn't have native TTS, instead it includes a built-in |
22 // component extension that provides speech synthesis. This class monitors | 9 // component extension that provides speech synthesis. This class includes |
23 // use of this component extension and triggers installing a higher-quality | 10 // an implementation of LoadBuiltInTtsExtension and dummy implementations of |
24 // speech synthesis extension if a certain number of utterances are spoken | 11 // everything else. |
25 // in a single session. | |
26 class TtsPlatformImplChromeOs | 12 class TtsPlatformImplChromeOs |
27 : public TtsPlatformImpl { | 13 : public TtsPlatformImpl { |
28 public: | 14 public: |
29 // TtsPlatformImpl overrides: | 15 // TtsPlatformImpl overrides: |
30 virtual bool PlatformImplAvailable() OVERRIDE { | 16 virtual bool PlatformImplAvailable() OVERRIDE { |
31 return false; | 17 return false; |
32 } | 18 } |
33 | 19 |
| 20 virtual bool LoadBuiltInTtsExtension(Profile* profile) OVERRIDE { |
| 21 return TtsExtensionLoaderChromeOs::GetInstance(profile)->LoadTtsExtension(); |
| 22 } |
| 23 |
34 virtual bool Speak( | 24 virtual bool Speak( |
35 int utterance_id, | 25 int utterance_id, |
36 const std::string& utterance, | 26 const std::string& utterance, |
37 const std::string& lang, | 27 const std::string& lang, |
38 const VoiceData& voice, | 28 const VoiceData& voice, |
39 const UtteranceContinuousParameters& params) OVERRIDE { | 29 const UtteranceContinuousParameters& params) OVERRIDE { |
40 return false; | 30 return false; |
41 } | 31 } |
42 | 32 |
43 virtual bool StopSpeaking() OVERRIDE { return false; } | 33 virtual bool StopSpeaking() OVERRIDE { |
| 34 return false; |
| 35 } |
| 36 |
44 virtual void Pause() OVERRIDE {} | 37 virtual void Pause() OVERRIDE {} |
| 38 |
45 virtual void Resume() OVERRIDE {} | 39 virtual void Resume() OVERRIDE {} |
46 virtual bool IsSpeaking() OVERRIDE { return false; } | |
47 virtual void GetVoices(std::vector<VoiceData>* out_voices) OVERRIDE {} | |
48 | 40 |
49 virtual void WillSpeakUtteranceWithVoice( | 41 virtual bool IsSpeaking() OVERRIDE { |
50 const Utterance* utterance, | 42 return false; |
51 const VoiceData& voice_data) OVERRIDE; | 43 } |
| 44 |
| 45 virtual void GetVoices(std::vector<VoiceData>* out_voices) OVERRIDE { |
| 46 } |
52 | 47 |
53 // Get the single instance of this class. | 48 // Get the single instance of this class. |
54 static TtsPlatformImplChromeOs* GetInstance(); | 49 static TtsPlatformImplChromeOs* GetInstance(); |
55 | 50 |
56 private: | 51 private: |
57 TtsPlatformImplChromeOs(); | 52 TtsPlatformImplChromeOs() {} |
58 virtual ~TtsPlatformImplChromeOs() {} | 53 virtual ~TtsPlatformImplChromeOs() {} |
59 | 54 |
60 friend struct DefaultSingletonTraits<TtsPlatformImplChromeOs>; | 55 friend struct DefaultSingletonTraits<TtsPlatformImplChromeOs>; |
61 | 56 |
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_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplChromeOs); | 57 DISALLOW_COPY_AND_ASSIGN(TtsPlatformImplChromeOs); |
69 }; | 58 }; |
70 | 59 |
71 TtsPlatformImplChromeOs::TtsPlatformImplChromeOs() { | |
72 } | |
73 | |
74 void TtsPlatformImplChromeOs::WillSpeakUtteranceWithVoice( | |
75 const Utterance* utterance, | |
76 const VoiceData& voice_data) { | |
77 CHECK(utterance); | |
78 CHECK(utterance->profile()); | |
79 | |
80 if (utterance->profile()->IsOffTheRecord()) | |
81 return; | |
82 | |
83 if (voice_data.extension_id != extension_misc::kSpeechSynthesisExtensionId) | |
84 return; | |
85 | |
86 lang_utterance_count_[voice_data.lang]++; | |
87 if (lang_utterance_count_[voice_data.lang] != | |
88 kHighQualitySpeechUtteranceThreshold) { | |
89 return; | |
90 } | |
91 | |
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)); | |
98 } | |
99 | |
100 // static | 60 // static |
101 TtsPlatformImpl* TtsPlatformImpl::GetInstance() { | 61 TtsPlatformImpl* TtsPlatformImpl::GetInstance() { |
102 return TtsPlatformImplChromeOs::GetInstance(); | 62 return TtsPlatformImplChromeOs::GetInstance(); |
103 } | 63 } |
104 | 64 |
105 // static | 65 // static |
106 TtsPlatformImplChromeOs* | 66 TtsPlatformImplChromeOs* |
107 TtsPlatformImplChromeOs::GetInstance() { | 67 TtsPlatformImplChromeOs::GetInstance() { |
108 return Singleton<TtsPlatformImplChromeOs>::get(); | 68 return Singleton<TtsPlatformImplChromeOs>::get(); |
109 } | 69 } |
OLD | NEW |