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 "chrome/common/extensions/api/speech/tts_engine_manifest_handler.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "extensions/common/error_utils.h"
12 #include "extensions/common/manifest.h"
13 #include "extensions/common/manifest_constants.h"
14 #include "ui/base/l10n/l10n_util.h"
16 namespace extensions
{
18 namespace keys
= manifest_keys
;
19 namespace errors
= manifest_errors
;
23 struct TtsVoices
: public Extension::ManifestData
{
25 ~TtsVoices() override
{}
27 std::vector
<extensions::TtsVoice
> voices
;
32 TtsVoice::TtsVoice() : remote(false) {}
34 TtsVoice::~TtsVoice() {}
37 const std::vector
<TtsVoice
>* TtsVoice::GetTtsVoices(
38 const Extension
* extension
) {
39 TtsVoices
* info
= static_cast<TtsVoices
*>(
40 extension
->GetManifestData(keys::kTtsVoices
));
41 return info
? &info
->voices
: NULL
;
44 TtsEngineManifestHandler::TtsEngineManifestHandler() {
47 TtsEngineManifestHandler::~TtsEngineManifestHandler() {
50 bool TtsEngineManifestHandler::Parse(Extension
* extension
,
51 base::string16
* error
) {
52 scoped_ptr
<TtsVoices
> info(new TtsVoices
);
53 const base::DictionaryValue
* tts_dict
= NULL
;
54 if (!extension
->manifest()->GetDictionary(keys::kTtsEngine
, &tts_dict
)) {
55 *error
= base::ASCIIToUTF16(errors::kInvalidTts
);
59 if (!tts_dict
->HasKey(keys::kTtsVoices
))
62 const base::ListValue
* tts_voices
= NULL
;
63 if (!tts_dict
->GetList(keys::kTtsVoices
, &tts_voices
)) {
64 *error
= base::ASCIIToUTF16(errors::kInvalidTtsVoices
);
68 for (size_t i
= 0; i
< tts_voices
->GetSize(); i
++) {
69 const base::DictionaryValue
* one_tts_voice
= NULL
;
70 if (!tts_voices
->GetDictionary(i
, &one_tts_voice
)) {
71 *error
= base::ASCIIToUTF16(errors::kInvalidTtsVoices
);
76 if (one_tts_voice
->HasKey(keys::kTtsVoicesVoiceName
)) {
77 if (!one_tts_voice
->GetString(
78 keys::kTtsVoicesVoiceName
, &voice_data
.voice_name
)) {
79 *error
= base::ASCIIToUTF16(errors::kInvalidTtsVoicesVoiceName
);
83 if (one_tts_voice
->HasKey(keys::kTtsVoicesLang
)) {
84 if (!one_tts_voice
->GetString(
85 keys::kTtsVoicesLang
, &voice_data
.lang
) ||
86 !l10n_util::IsValidLocaleSyntax(voice_data
.lang
)) {
87 *error
= base::ASCIIToUTF16(errors::kInvalidTtsVoicesLang
);
91 if (one_tts_voice
->HasKey(keys::kTtsVoicesGender
)) {
92 if (!one_tts_voice
->GetString(
93 keys::kTtsVoicesGender
, &voice_data
.gender
) ||
94 (voice_data
.gender
!= keys::kTtsGenderMale
&&
95 voice_data
.gender
!= keys::kTtsGenderFemale
)) {
96 *error
= base::ASCIIToUTF16(errors::kInvalidTtsVoicesGender
);
100 if (one_tts_voice
->HasKey(keys::kTtsVoicesRemote
)) {
101 if (!one_tts_voice
->GetBoolean(
102 keys::kTtsVoicesRemote
, &voice_data
.remote
)) {
103 *error
= base::ASCIIToUTF16(errors::kInvalidTtsVoicesRemote
);
107 if (one_tts_voice
->HasKey(keys::kTtsVoicesEventTypes
)) {
108 const base::ListValue
* event_types_list
;
109 if (!one_tts_voice
->GetList(
110 keys::kTtsVoicesEventTypes
,
111 &event_types_list
)) {
112 *error
= base::ASCIIToUTF16(
113 errors::kInvalidTtsVoicesEventTypes
);
116 for (size_t i
= 0; i
< event_types_list
->GetSize(); i
++) {
117 std::string event_type
;
118 if (!event_types_list
->GetString(i
, &event_type
)) {
119 *error
= base::ASCIIToUTF16(errors::kInvalidTtsVoicesEventTypes
);
122 if (event_type
!= keys::kTtsVoicesEventTypeEnd
&&
123 event_type
!= keys::kTtsVoicesEventTypeError
&&
124 event_type
!= keys::kTtsVoicesEventTypeMarker
&&
125 event_type
!= keys::kTtsVoicesEventTypeSentence
&&
126 event_type
!= keys::kTtsVoicesEventTypeStart
&&
127 event_type
!= keys::kTtsVoicesEventTypeWord
) {
128 *error
= base::ASCIIToUTF16(errors::kInvalidTtsVoicesEventTypes
);
131 if (voice_data
.event_types
.find(event_type
) !=
132 voice_data
.event_types
.end()) {
133 *error
= base::ASCIIToUTF16(errors::kInvalidTtsVoicesEventTypes
);
136 voice_data
.event_types
.insert(event_type
);
140 info
->voices
.push_back(voice_data
);
143 extension
->SetManifestData(keys::kTtsVoices
, info
.release());
147 const std::vector
<std::string
> TtsEngineManifestHandler::Keys() const {
148 return SingleKey(keys::kTtsEngine
);
151 } // namespace extensions