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

Side by Side Diff: chrome/browser/speech/speech_input_extension_api.cc

Issue 8386074: Add a tray notification UI for speech input recording in the extension API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: creating the notification object dynamically on first use. Created 9 years, 1 month 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/speech_input/extension_speech_input_api.h" 5 #include "chrome/browser/speech/speech_input_extension_api.h"
6 6
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "chrome/browser/browser_process.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" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/speech/speech_input_extension_manager.h"
12 #include "chrome/common/chrome_notification_types.h" 11 #include "chrome/common/chrome_notification_types.h"
13 #include "content/public/browser/notification_details.h" 12 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_source.h" 13 #include "content/public/browser/notification_source.h"
15 14
16 namespace constants = extension_speech_input_api_constants; 15 namespace {
16
17 const char kLanguageKey[] = "language";
18 const char kGrammarKey[] = "grammar";
19 const char kFilterProfanitiesKey[] = "filterProfanities";
20
21 const char kDefaultGrammar[] = "builtin:dictation";
22 const bool kDefaultFilterProfanities = true;
23
24 } // anonymous namespace
17 25
18 SpeechInputAsyncFunction::SpeechInputAsyncFunction( 26 SpeechInputAsyncFunction::SpeechInputAsyncFunction(
19 int start_state, 27 int start_state,
20 int transition_state, 28 int transition_state,
21 int end_state, 29 int end_state,
22 int transition_notification) 30 int transition_notification)
23 : start_state_(start_state), 31 : start_state_(start_state),
24 transition_state_(transition_state), 32 transition_state_(transition_state),
25 end_state_(end_state), 33 end_state_(end_state),
26 transition_notification_(transition_notification), 34 transition_notification_(transition_notification),
27 expecting_transition_(false), 35 expecting_transition_(false),
28 failed_(false) { 36 failed_(false) {
29 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED, 37 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED,
30 content::Source<Profile>(profile())); 38 content::Source<Profile>(profile()));
31 } 39 }
32 40
33 SpeechInputAsyncFunction::~SpeechInputAsyncFunction() { 41 SpeechInputAsyncFunction::~SpeechInputAsyncFunction() {
34 } 42 }
35 43
36 void SpeechInputAsyncFunction::Run() { 44 void SpeechInputAsyncFunction::Run() {
37 if (failed_) { 45 if (failed_) {
38 registrar_.RemoveAll(); 46 registrar_.RemoveAll();
39 SendResponse(false); 47 SendResponse(false);
40 return; 48 return;
41 } 49 }
42 50
43 ExtensionSpeechInputManager::State state = 51 SpeechInputExtensionManager::State state =
44 ExtensionSpeechInputManager::GetForProfile(profile())->state(); 52 SpeechInputExtensionManager::GetForProfile(profile())->state();
45 53
46 // RunImpl should be always called once independently of the state we're in, 54 // RunImpl should be always called once independently of the state we're in,
47 // otherwise we might miss requestDenied error situations. 55 // otherwise we might miss requestDenied error situations.
48 if (!expecting_transition_) { 56 if (!expecting_transition_) {
49 ExtensionSpeechInputManager::State state_before_call = state; 57 SpeechInputExtensionManager::State state_before_call = state;
50 58
51 // Register before RunImpl to ensure it's received if generated. 59 // Register before RunImpl to ensure it's received if generated.
52 if (state_before_call == start_state_) { 60 if (state_before_call == start_state_) {
53 registrar_.Add(this, transition_notification_, 61 registrar_.Add(this, transition_notification_,
54 content::Source<Profile>(profile())); 62 content::Source<Profile>(profile()));
55 AddRef(); // Balanced in Observe(). 63 AddRef(); // Balanced in Observe().
56 } 64 }
57 65
58 if (!RunImpl()) { 66 if (!RunImpl()) {
59 registrar_.RemoveAll(); 67 registrar_.RemoveAll();
60 SendResponse(false); 68 SendResponse(false);
61 return; 69 return;
62 } 70 }
63 71
64 // RunImpl should always return false and set the appropriate error code 72 // RunImpl should always return false and set the appropriate error code
65 // when called in a state different to the start one. 73 // when called in a state different to the start one.
66 DCHECK_EQ(state_before_call, start_state_); 74 DCHECK_EQ(state_before_call, start_state_);
67 75
68 state = ExtensionSpeechInputManager::GetForProfile(profile())->state(); 76 state = SpeechInputExtensionManager::GetForProfile(profile())->state();
69 DCHECK_EQ(state, transition_state_); 77 DCHECK_EQ(state, transition_state_);
70 expecting_transition_ = true; 78 expecting_transition_ = true;
71 } 79 }
72 80
73 if (state == transition_state_) 81 if (state == transition_state_)
74 return; 82 return;
75 83
76 DCHECK_EQ(state, end_state_); 84 DCHECK_EQ(state, end_state_);
77 registrar_.RemoveAll(); 85 registrar_.RemoveAll();
78 SendResponse(true); 86 SendResponse(true);
79 } 87 }
80 88
81 void SpeechInputAsyncFunction::Observe( 89 void SpeechInputAsyncFunction::Observe(
82 int type, 90 int type,
83 const content::NotificationSource& source, 91 const content::NotificationSource& source,
84 const content::NotificationDetails& details) { 92 const content::NotificationDetails& details) {
85 DCHECK_EQ(profile(), content::Source<Profile>(source).ptr()); 93 DCHECK_EQ(profile(), content::Source<Profile>(source).ptr());
86 94
87 if (type == chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED) { 95 if (type == chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_FAILED) {
88 ExtensionSpeechInputManager::ExtensionError* error_details = 96 SpeechInputExtensionManager::ExtensionError* error_details =
89 content::Details<ExtensionSpeechInputManager::ExtensionError>( 97 content::Details<SpeechInputExtensionManager::ExtensionError>(
90 details).ptr(); 98 details).ptr();
91 if (error_details->extension_id_ != extension_id()) 99 if (error_details->extension_id_ != extension_id())
92 return; 100 return;
93 101
94 error_ = error_details->error_; 102 error_ = error_details->error_;
95 failed_ = true; 103 failed_ = true;
96 } else { 104 } else {
97 DCHECK_EQ(type, transition_notification_); 105 DCHECK_EQ(type, transition_notification_);
98 if (*content::Details<std::string>(details).ptr() != extension_id()) 106 if (*content::Details<std::string>(details).ptr() != extension_id())
99 return; 107 return;
100 DCHECK(expecting_transition_); 108 DCHECK(expecting_transition_);
101 } 109 }
102 110
103 Run(); 111 Run();
104 Release(); // Balanced in Run(). 112 Release(); // Balanced in Run().
105 } 113 }
106 114
107 StartSpeechInputFunction::StartSpeechInputFunction() 115 StartSpeechInputFunction::StartSpeechInputFunction()
108 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kIdle, 116 : SpeechInputAsyncFunction(SpeechInputExtensionManager::kIdle,
109 ExtensionSpeechInputManager::kStarting, 117 SpeechInputExtensionManager::kStarting,
110 ExtensionSpeechInputManager::kRecording, 118 SpeechInputExtensionManager::kRecording,
111 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED) { 119 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STARTED) {
112 } 120 }
113 121
114 bool StartSpeechInputFunction::RunImpl() { 122 bool StartSpeechInputFunction::RunImpl() {
115 std::string language; 123 std::string language;
116 std::string grammar = constants::kDefaultGrammar; 124 std::string grammar = kDefaultGrammar;
117 bool filter_profanities = constants::kDefaultFilterProfanities; 125 bool filter_profanities = kDefaultFilterProfanities;
118 126
119 if (!args_->empty()) { 127 if (!args_->empty()) {
120 DictionaryValue *options; 128 DictionaryValue *options;
121 if (!args_->GetDictionary(0, &options)) 129 if (!args_->GetDictionary(0, &options))
122 return false; 130 return false;
123 DCHECK(options); 131 DCHECK(options);
124 132
125 if (options->HasKey(constants::kLanguageKey)) 133 if (options->HasKey(kLanguageKey))
126 options->GetString(constants::kLanguageKey, &language); 134 options->GetString(kLanguageKey, &language);
127 if (options->HasKey(constants::kGrammarKey)) 135 if (options->HasKey(kGrammarKey))
128 options->GetString(constants::kGrammarKey, &grammar); 136 options->GetString(kGrammarKey, &grammar);
129 137
130 if (options->HasKey(constants::kFilterProfanitiesKey)) { 138 if (options->HasKey(kFilterProfanitiesKey)) {
131 options->GetBoolean(constants::kFilterProfanitiesKey, 139 options->GetBoolean(kFilterProfanitiesKey,
132 &filter_profanities); 140 &filter_profanities);
133 } 141 }
134 } 142 }
135 143
136 // Use the application locale if the language is empty or not provided. 144 // Use the application locale if the language is empty or not provided.
137 if (language.empty()) { 145 if (language.empty()) {
138 language = g_browser_process->GetApplicationLocale(); 146 language = g_browser_process->GetApplicationLocale();
139 VLOG(1) << "Language not specified. Using application locale " << language; 147 VLOG(1) << "Language not specified. Using application locale " << language;
140 } 148 }
141 149
142 return ExtensionSpeechInputManager::GetForProfile(profile())->Start( 150 return SpeechInputExtensionManager::GetForProfile(profile())->Start(
143 extension_id(), language, grammar, filter_profanities, &error_); 151 extension_id(), language, grammar, filter_profanities, &error_);
144 } 152 }
145 153
146 StopSpeechInputFunction::StopSpeechInputFunction() 154 StopSpeechInputFunction::StopSpeechInputFunction()
147 : SpeechInputAsyncFunction(ExtensionSpeechInputManager::kRecording, 155 : SpeechInputAsyncFunction(SpeechInputExtensionManager::kRecording,
148 ExtensionSpeechInputManager::kStopping, 156 SpeechInputExtensionManager::kStopping,
149 ExtensionSpeechInputManager::kIdle, 157 SpeechInputExtensionManager::kIdle,
150 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED) { 158 chrome::NOTIFICATION_EXTENSION_SPEECH_INPUT_RECORDING_STOPPED) {
151 } 159 }
152 160
153 bool StopSpeechInputFunction::RunImpl() { 161 bool StopSpeechInputFunction::RunImpl() {
154 return ExtensionSpeechInputManager::GetForProfile( 162 return SpeechInputExtensionManager::GetForProfile(
155 profile())->Stop(extension_id(), &error_); 163 profile())->Stop(extension_id(), &error_);
156 } 164 }
157 165
158 bool IsRecordingSpeechInputFunction::RunImpl() { 166 bool IsRecordingSpeechInputFunction::RunImpl() {
159 // Do not access the AudioManager directly here to ensure the proper 167 // Do not access the AudioManager directly here to ensure the proper
160 // IsRecording behaviour in the API tests. 168 // IsRecording behaviour in the API tests.
161 result_.reset(Value::CreateBooleanValue( 169 result_.reset(Value::CreateBooleanValue(
162 ExtensionSpeechInputManager::GetForProfile(profile())->IsRecording())); 170 SpeechInputExtensionManager::GetForProfile(profile())->IsRecording()));
163 return true; 171 return true;
164 } 172 }
OLDNEW
« no previous file with comments | « chrome/browser/speech/speech_input_extension_api.h ('k') | chrome/browser/speech/speech_input_extension_apitest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698