| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "chrome/browser/extensions/extension_tts_api.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/extensions/extension_tts_api_constants.h" | |
| 11 #include "chrome/browser/extensions/extension_tts_api_controller.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "ui/base/l10n/l10n_util.h" | |
| 14 | |
| 15 namespace constants = extension_tts_api_constants; | |
| 16 | |
| 17 bool ExtensionTtsSpeakFunction::RunImpl() { | |
| 18 std::string text; | |
| 19 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &text)); | |
| 20 if (text.size() > 32768) { | |
| 21 error_ = constants::kErrorUtteranceTooLong; | |
| 22 return false; | |
| 23 } | |
| 24 | |
| 25 scoped_ptr<DictionaryValue> options(new DictionaryValue()); | |
| 26 if (args_->GetSize() >= 2) { | |
| 27 DictionaryValue* temp_options = NULL; | |
| 28 if (args_->GetDictionary(1, &temp_options)) | |
| 29 options.reset(temp_options->DeepCopy()); | |
| 30 } | |
| 31 | |
| 32 std::string voice_name; | |
| 33 if (options->HasKey(constants::kVoiceNameKey)) { | |
| 34 EXTENSION_FUNCTION_VALIDATE( | |
| 35 options->GetString(constants::kVoiceNameKey, &voice_name)); | |
| 36 } | |
| 37 | |
| 38 std::string lang; | |
| 39 if (options->HasKey(constants::kLangKey)) | |
| 40 EXTENSION_FUNCTION_VALIDATE(options->GetString(constants::kLangKey, &lang)); | |
| 41 if (!lang.empty() && !l10n_util::IsValidLocaleSyntax(lang)) { | |
| 42 error_ = constants::kErrorInvalidLang; | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 std::string gender; | |
| 47 if (options->HasKey(constants::kGenderKey)) | |
| 48 EXTENSION_FUNCTION_VALIDATE( | |
| 49 options->GetString(constants::kGenderKey, &gender)); | |
| 50 if (!gender.empty() && | |
| 51 gender != constants::kGenderFemale && | |
| 52 gender != constants::kGenderMale) { | |
| 53 error_ = constants::kErrorInvalidGender; | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 double rate = 1.0; | |
| 58 if (options->HasKey(constants::kRateKey)) { | |
| 59 EXTENSION_FUNCTION_VALIDATE( | |
| 60 options->GetDouble(constants::kRateKey, &rate)); | |
| 61 if (rate < 0.1 || rate > 10.0) { | |
| 62 error_ = constants::kErrorInvalidRate; | |
| 63 return false; | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 double pitch = 1.0; | |
| 68 if (options->HasKey(constants::kPitchKey)) { | |
| 69 EXTENSION_FUNCTION_VALIDATE( | |
| 70 options->GetDouble(constants::kPitchKey, &pitch)); | |
| 71 if (pitch < 0.0 || pitch > 2.0) { | |
| 72 error_ = constants::kErrorInvalidPitch; | |
| 73 return false; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 double volume = 1.0; | |
| 78 if (options->HasKey(constants::kVolumeKey)) { | |
| 79 EXTENSION_FUNCTION_VALIDATE( | |
| 80 options->GetDouble(constants::kVolumeKey, &volume)); | |
| 81 if (volume < 0.0 || volume > 1.0) { | |
| 82 error_ = constants::kErrorInvalidVolume; | |
| 83 return false; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 bool can_enqueue = false; | |
| 88 if (options->HasKey(constants::kEnqueueKey)) { | |
| 89 EXTENSION_FUNCTION_VALIDATE( | |
| 90 options->GetBoolean(constants::kEnqueueKey, &can_enqueue)); | |
| 91 } | |
| 92 | |
| 93 std::set<std::string> required_event_types; | |
| 94 if (options->HasKey(constants::kRequiredEventTypesKey)) { | |
| 95 ListValue* list; | |
| 96 EXTENSION_FUNCTION_VALIDATE( | |
| 97 options->GetList(constants::kRequiredEventTypesKey, &list)); | |
| 98 for (size_t i = 0; i < list->GetSize(); i++) { | |
| 99 std::string event_type; | |
| 100 if (!list->GetString(i, &event_type)) | |
| 101 required_event_types.insert(event_type); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 std::set<std::string> desired_event_types; | |
| 106 if (options->HasKey(constants::kDesiredEventTypesKey)) { | |
| 107 ListValue* list; | |
| 108 EXTENSION_FUNCTION_VALIDATE( | |
| 109 options->GetList(constants::kDesiredEventTypesKey, &list)); | |
| 110 for (size_t i = 0; i < list->GetSize(); i++) { | |
| 111 std::string event_type; | |
| 112 if (!list->GetString(i, &event_type)) | |
| 113 desired_event_types.insert(event_type); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 std::string voice_extension_id; | |
| 118 if (options->HasKey(constants::kExtensionIdKey)) { | |
| 119 EXTENSION_FUNCTION_VALIDATE( | |
| 120 options->GetString(constants::kExtensionIdKey, &voice_extension_id)); | |
| 121 } | |
| 122 | |
| 123 int src_id = -1; | |
| 124 if (options->HasKey(constants::kSrcIdKey)) { | |
| 125 EXTENSION_FUNCTION_VALIDATE( | |
| 126 options->GetInteger(constants::kSrcIdKey, &src_id)); | |
| 127 } | |
| 128 | |
| 129 // If we got this far, the arguments were all in the valid format, so | |
| 130 // send the success response to the callback now - this ensures that | |
| 131 // the callback response always arrives before events, which makes | |
| 132 // the behavior more predictable and easier to write unit tests for too. | |
| 133 SendResponse(true); | |
| 134 | |
| 135 UtteranceContinuousParameters continuous_params; | |
| 136 continuous_params.rate = rate; | |
| 137 continuous_params.pitch = pitch; | |
| 138 continuous_params.volume = volume; | |
| 139 | |
| 140 Utterance* utterance = new Utterance(profile()); | |
| 141 utterance->set_text(text); | |
| 142 utterance->set_voice_name(voice_name); | |
| 143 utterance->set_src_extension_id(extension_id()); | |
| 144 utterance->set_src_id(src_id); | |
| 145 utterance->set_src_url(source_url()); | |
| 146 utterance->set_lang(lang); | |
| 147 utterance->set_gender(gender); | |
| 148 utterance->set_continuous_parameters(continuous_params); | |
| 149 utterance->set_can_enqueue(can_enqueue); | |
| 150 utterance->set_required_event_types(required_event_types); | |
| 151 utterance->set_desired_event_types(desired_event_types); | |
| 152 utterance->set_extension_id(voice_extension_id); | |
| 153 utterance->set_options(options.get()); | |
| 154 | |
| 155 ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); | |
| 156 controller->SpeakOrEnqueue(utterance); | |
| 157 return true; | |
| 158 } | |
| 159 | |
| 160 bool ExtensionTtsStopSpeakingFunction::RunImpl() { | |
| 161 ExtensionTtsController::GetInstance()->Stop(); | |
| 162 return true; | |
| 163 } | |
| 164 | |
| 165 bool ExtensionTtsIsSpeakingFunction::RunImpl() { | |
| 166 result_.reset(Value::CreateBooleanValue( | |
| 167 ExtensionTtsController::GetInstance()->IsSpeaking())); | |
| 168 return true; | |
| 169 } | |
| 170 | |
| 171 bool ExtensionTtsGetVoicesFunction::RunImpl() { | |
| 172 result_.reset(ExtensionTtsController::GetInstance()->GetVoices(profile())); | |
| 173 return true; | |
| 174 } | |
| OLD | NEW |