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

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 media code and dependencies. Addressed in a different patch. 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/extensions/speech_input/extension_speech_input_api_cons tants.h"
9 #include "chrome/browser/extensions/speech_input/extension_speech_input_manager. h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/chrome_notification_types.h"
12
13 namespace constants = extension_speech_input_api_constants;
14
15 SpeechInputAsyncFunction::SpeechInputAsyncFunction(int start_state,
16 int transition_state, int end_state, int transition_notification)
17 : start_state_(start_state),
18 transition_state_(transition_state),
19 end_state_(end_state),
20 transition_notification_(transition_notification),
21 expecting_transition_(false),
22 failed_(false) {
23 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED,
24 Source<Profile>(profile()));
25 }
26
27 SpeechInputAsyncFunction::~SpeechInputAsyncFunction() {
28 }
29
30 void SpeechInputAsyncFunction::Run() {
31 if (failed_) {
32 registrar_.RemoveAll();
33 SendResponse(false);
34 return;
35 }
36
37 ExtensionSpeechInputManager::State state =
38 ExtensionSpeechInputManager::GetForProfile(profile())->state();
39
40 // RunImpl should be always called once independently of the state we're in,
41 // otherwise we might miss requestDenied error situations.
42 if (!expecting_transition_) {
43 ExtensionSpeechInputManager::State state_before_call = state;
44
45 // Register before RunImpl to ensure it's received if generated.
46 if (state_before_call == start_state_) {
47 registrar_.Add(this, transition_notification_,
48 Source<Profile>(profile()));
49 AddRef(); // Balanced in Observe().
50 }
51
52 if (!RunImpl()) {
53 registrar_.RemoveAll();
54 SendResponse(false);
55 return;
56 }
57
58 // RunImpl should always return false and set the appropriate error code
59 // when called in a state different to the start one.
60 DCHECK_EQ(state_before_call, start_state_);
61
62 state = ExtensionSpeechInputManager::GetForProfile(profile())->state();
63 DCHECK_EQ(state, transition_state_);
64 expecting_transition_ = true;
65 }
66
67 if (state == transition_state_)
68 return;
69
70 DCHECK_EQ(state, end_state_);
71 registrar_.RemoveAll();
72 SendResponse(true);
73 }
74
75 void SpeechInputAsyncFunction::Observe(int type,
76 const NotificationSource& source,
77 const NotificationDetails& details) {
78 DCHECK_EQ(profile(), Source<Profile>(source).ptr());
79
80 if (type == chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED) {
81 ExtensionSpeechInputManager::ExtensionError* error_details =
82 Details<ExtensionSpeechInputManager::ExtensionError>(details).ptr();
83 if (error_details->extension_id_ != extension_id())
84 return;
85
86 error_ = error_details->error_;
87 failed_ = true;
88 } else {
89 DCHECK_EQ(type, transition_notification_);
90 if (*Details<std::string>(details).ptr() != extension_id())
91 return;
92 DCHECK(expecting_transition_);
93 }
94
95 Run();
96 Release(); // Balanced in Run().
97 }
98
99 StartSpeechInputFunction::StartSpeechInputFunction()
100 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kIdle,
101 ExtensionSpeechInputManager::kStarting,
102 ExtensionSpeechInputManager::kRecording,
103 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED) {
104 }
105
106 bool StartSpeechInputFunction::RunImpl() {
107 std::string language = constants::kDefaultLanguage;
108 std::string grammar = constants::kDefaultGrammar;
109 bool filter_profanities = constants::kDefaultFilterProfanities;
110
111 if (!args_->empty()) {
112 DictionaryValue *options;
113 if (!args_->GetDictionary(0, &options))
114 return false;
115 DCHECK(options);
116
117 if (options->HasKey(constants::kLanguageKey))
118 options->GetString(constants::kLanguageKey, &language);
119 if (options->HasKey(constants::kGrammarKey))
120 options->GetString(constants::kGrammarKey, &grammar);
121
122 if (options->HasKey(constants::kFilterProfanitiesKey)) {
123 options->GetBoolean(constants::kFilterProfanitiesKey,
124 &filter_profanities);
125 }
126 }
127
128 return ExtensionSpeechInputManager::GetForProfile(profile())->Start(
129 extension_id(), language, grammar, filter_profanities, &error_);
130 }
131
132 StopSpeechInputFunction::StopSpeechInputFunction()
133 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kRecording,
134 ExtensionSpeechInputManager::kStopping,
135 ExtensionSpeechInputManager::kIdle,
136 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED) {
137 }
138
139 bool StopSpeechInputFunction::RunImpl() {
140 return ExtensionSpeechInputManager::GetForProfile(
141 profile())->Stop(extension_id(), &error_);
142 }
143
144 bool IsRecordingSpeechInputFunction::RunImpl() {
145 // TODO(leandrogracia): to be implemented.
146 result_.reset(Value::CreateBooleanValue(false));
147 return true;
148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698