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

Side by Side Diff: chrome/browser/extensions/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: second iteration of review fixes. 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/extensions/extension_speech_input_api.h" 5 #include "chrome/browser/extensions/extension_speech_input_api.h"
6 6
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "chrome/browser/extensions/extension_speech_input_api_constants.h"
8 #include "chrome/browser/extensions/extension_speech_input_manager.h" 9 #include "chrome/browser/extensions/extension_speech_input_manager.h"
9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/chrome_notification_types.h"
10 12
11 SpeechInputAsyncFunction::SpeechInputAsyncFunction() { 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() {
12 } 28 }
13 29
14 void SpeechInputAsyncFunction::Run() { 30 void SpeechInputAsyncFunction::Run() {
31 if (failed_) {
32 registrar_.RemoveAll();
33 SendResponse(false);
34 return;
35 }
15 36
16 // TODO(leandrogracia): implement the management of the asynchronous 37 ExtensionSpeechInputManager::State state =
17 // operations involved and the corresponding state transitions. 38 ExtensionSpeechInputManager::GetForProfile(profile())->state();
18 RunImpl();
19 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();
20 SendResponse(true); 72 SendResponse(true);
21 } 73 }
22 74
23 void SpeechInputAsyncFunction::Observe(int type, 75 void SpeechInputAsyncFunction::Observe(int type,
24 const NotificationSource& source, 76 const NotificationSource& source,
25 const NotificationDetails& details) { 77 const NotificationDetails& details) {
26 // TODO(leandrogracia): to be implemented. 78 DCHECK_EQ(profile(), Source<Profile>(source).ptr());
79
80 if (type == chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED) {
81 ExtensionSpeechInputManager::ExtensionError *error_details =
Satish 2011/10/11 13:37:16 move '*' to be before the space
Leandro Graciá Gil 2011/10/11 18:08:00 Done.
82 Details<ExtensionSpeechInputManager::ExtensionError>(details).ptr();
Satish 2011/10/11 13:37:16 indent by 2 more spaces
Leandro Graciá Gil 2011/10/11 18:08:00 Done.
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 !expecting_transition_) {
Satish 2011/10/11 13:37:16 should you do a DCHECK(expecting_transition_) if t
Leandro Graciá Gil 2011/10/11 18:08:00 Done.
92 return;
93 }
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) {
27 } 105 }
28 106
29 bool StartSpeechInputFunction::RunImpl() { 107 bool StartSpeechInputFunction::RunImpl() {
30 ExtensionSpeechInputManager::GetForProfile(profile())->Start(GetExtension()); 108 std::string language = constants::kDefaultLanguage;
31 return true; 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 return ExtensionSpeechInputManager::GetForProfile(profile())->Start(
130 extension_id(), language, grammar, filter_profanities, &error_);
131 }
132
133 StopSpeechInputFunction::StopSpeechInputFunction()
134 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kRecording,
135 ExtensionSpeechInputManager::kStopping,
136 ExtensionSpeechInputManager::kIdle,
137 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED) {
32 } 138 }
33 139
34 bool StopSpeechInputFunction::RunImpl() { 140 bool StopSpeechInputFunction::RunImpl() {
35 ExtensionSpeechInputManager::GetForProfile(profile())->Stop(GetExtension()); 141 return ExtensionSpeechInputManager::GetForProfile(
36 return true; 142 profile())->Stop(extension_id(), &error_);
37 } 143 }
38 144
39 bool IsRecordingSpeechInputFunction::RunImpl() { 145 bool IsRecordingSpeechInputFunction::RunImpl() {
40 // TODO(leandrogracia): to be implemented. 146 switch (ExtensionSpeechInputManager::GetForProfile(profile())->state()) {
41 result_.reset(Value::CreateBooleanValue(false)); 147 case ExtensionSpeechInputManager::kIdle:
148 case ExtensionSpeechInputManager::kStarting:
149 result_.reset(Value::CreateBooleanValue(false));
150 break;
151
152 case ExtensionSpeechInputManager::kRecording:
153 case ExtensionSpeechInputManager::kStopping:
154 result_.reset(Value::CreateBooleanValue(true));
155 break;
156
157 default:
158 NOTREACHED();
159 }
160
42 return true; 161 return true;
43 } 162 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698