Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(641)

Side by Side Diff: chrome/browser/extensions/speech_input/extension_speech_input_api.cc

Issue 8201030: Introducing the basic functionality to the speech input experimental API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: removing nit and adding application locale as default language. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/speech_input/extension_speech_input_api.h"
6
7 #include "base/values.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/extensions/speech_input/extension_speech_input_api_cons tants.h"
10 #include "chrome/browser/extensions/speech_input/extension_speech_input_manager. h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/chrome_notification_types.h"
13
14 namespace constants = extension_speech_input_api_constants;
15
16 SpeechInputAsyncFunction::SpeechInputAsyncFunction(int start_state,
17 int transition_state, int end_state, int transition_notification)
Satish 2011/10/12 20:11:59 indent parameters at the same column and one per l
Leandro GraciĆ” Gil 2011/10/13 10:40:22 Done.
18 : start_state_(start_state),
19 transition_state_(transition_state),
20 end_state_(end_state),
21 transition_notification_(transition_notification),
22 expecting_transition_(false),
23 failed_(false) {
24 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED,
25 Source<Profile>(profile()));
26 }
27
28 SpeechInputAsyncFunction::~SpeechInputAsyncFunction() {
29 }
30
31 void SpeechInputAsyncFunction::Run() {
32 if (failed_) {
33 registrar_.RemoveAll();
34 SendResponse(false);
35 return;
36 }
37
38 ExtensionSpeechInputManager::State state =
39 ExtensionSpeechInputManager::GetForProfile(profile())->state();
40
41 // RunImpl should be always called once independently of the state we're in,
42 // otherwise we might miss requestDenied error situations.
43 if (!expecting_transition_) {
44 ExtensionSpeechInputManager::State state_before_call = state;
45
46 // Register before RunImpl to ensure it's received if generated.
47 if (state_before_call == start_state_) {
48 registrar_.Add(this, transition_notification_,
49 Source<Profile>(profile()));
50 AddRef(); // Balanced in Observe().
51 }
52
53 if (!RunImpl()) {
54 registrar_.RemoveAll();
55 SendResponse(false);
56 return;
57 }
58
59 // RunImpl should always return false and set the appropriate error code
60 // when called in a state different to the start one.
61 DCHECK_EQ(state_before_call, start_state_);
62
63 state = ExtensionSpeechInputManager::GetForProfile(profile())->state();
64 DCHECK_EQ(state, transition_state_);
65 expecting_transition_ = true;
66 }
67
68 if (state == transition_state_)
69 return;
70
71 DCHECK_EQ(state, end_state_);
72 registrar_.RemoveAll();
73 SendResponse(true);
74 }
75
76 void SpeechInputAsyncFunction::Observe(int type,
77 const NotificationSource& source,
78 const NotificationDetails& details) {
79 DCHECK_EQ(profile(), Source<Profile>(source).ptr());
80
81 if (type == chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED) {
82 ExtensionSpeechInputManager::ExtensionError* error_details =
83 Details<ExtensionSpeechInputManager::ExtensionError>(details).ptr();
84 if (error_details->extension_id_ != extension_id())
85 return;
86
87 error_ = error_details->error_;
88 failed_ = true;
89 } else {
90 DCHECK_EQ(type, transition_notification_);
91 if (*Details<std::string>(details).ptr() != extension_id())
92 return;
93 DCHECK(expecting_transition_);
94 }
95
96 Run();
97 Release(); // Balanced in Run().
98 }
99
100 StartSpeechInputFunction::StartSpeechInputFunction()
101 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kIdle,
102 ExtensionSpeechInputManager::kStarting,
103 ExtensionSpeechInputManager::kRecording,
104 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED) {
105 }
106
107 bool StartSpeechInputFunction::RunImpl() {
108 std::string language;
109 std::string grammar = constants::kDefaultGrammar;
110 bool filter_profanities = constants::kDefaultFilterProfanities;
111
112 if (!args_->empty()) {
113 DictionaryValue *options;
114 if (!args_->GetDictionary(0, &options))
115 return false;
116 DCHECK(options);
117
118 if (options->HasKey(constants::kLanguageKey))
119 options->GetString(constants::kLanguageKey, &language);
120 if (options->HasKey(constants::kGrammarKey))
121 options->GetString(constants::kGrammarKey, &grammar);
122
123 if (options->HasKey(constants::kFilterProfanitiesKey)) {
124 options->GetBoolean(constants::kFilterProfanitiesKey,
125 &filter_profanities);
126 }
127 }
128
129 // Use the application locale if the language is empty or not provided.
130 if (language.empty()) {
131 language = g_browser_process->GetApplicationLocale();
132 VLOG(1) << "Language not specified. Using application locale " << language;
133 }
134
135 return ExtensionSpeechInputManager::GetForProfile(profile())->Start(
136 extension_id(), language, grammar, filter_profanities, &error_);
137 }
138
139 StopSpeechInputFunction::StopSpeechInputFunction()
140 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kRecording,
141 ExtensionSpeechInputManager::kStopping,
142 ExtensionSpeechInputManager::kIdle,
143 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED) {
144 }
145
146 bool StopSpeechInputFunction::RunImpl() {
147 return ExtensionSpeechInputManager::GetForProfile(
148 profile())->Stop(extension_id(), &error_);
149 }
150
151 bool IsRecordingSpeechInputFunction::RunImpl() {
152 // TODO(leandrogracia): to be implemented.
153 result_.reset(Value::CreateBooleanValue(false));
154 return true;
155 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698