OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/search/hotword_service.h" | 5 #include "chrome/browser/search/hotword_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 } | 208 } |
209 UMA_HISTOGRAM_ENUMERATION("Hotword.Enabled", enabled_state, | 209 UMA_HISTOGRAM_ENUMERATION("Hotword.Enabled", enabled_state, |
210 NUM_HOTWORD_ENABLED_METRICS); | 210 NUM_HOTWORD_ENABLED_METRICS); |
211 | 211 |
212 pref_registrar_.Init(profile_->GetPrefs()); | 212 pref_registrar_.Init(profile_->GetPrefs()); |
213 pref_registrar_.Add( | 213 pref_registrar_.Add( |
214 prefs::kHotwordSearchEnabled, | 214 prefs::kHotwordSearchEnabled, |
215 base::Bind(&HotwordService::OnHotwordSearchEnabledChanged, | 215 base::Bind(&HotwordService::OnHotwordSearchEnabledChanged, |
216 base::Unretained(this))); | 216 base::Unretained(this))); |
217 | 217 |
218 registrar_.Add(this, | |
219 chrome::NOTIFICATION_BROWSER_WINDOW_READY, | |
220 content::NotificationService::AllSources()); | |
221 | |
222 extensions::ExtensionSystem::Get(profile_)->ready().Post( | 218 extensions::ExtensionSystem::Get(profile_)->ready().Post( |
223 FROM_HERE, | 219 FROM_HERE, |
224 base::Bind(base::IgnoreResult( | 220 base::Bind(base::IgnoreResult( |
225 &HotwordService::MaybeReinstallHotwordExtension), | 221 &HotwordService::MaybeReinstallHotwordExtension), |
226 weak_factory_.GetWeakPtr())); | 222 weak_factory_.GetWeakPtr())); |
227 | 223 |
228 // Clear the old user pref because it became unusable. | 224 // Clear the old user pref because it became unusable. |
229 // TODO(rlp): Remove this code per crbug.com/358789. | 225 // TODO(rlp): Remove this code per crbug.com/358789. |
230 if (profile_->GetPrefs()->HasPrefPath( | 226 if (profile_->GetPrefs()->HasPrefPath( |
231 hotword_internal::kHotwordUnusablePrefName)) { | 227 hotword_internal::kHotwordUnusablePrefName)) { |
232 profile_->GetPrefs()->ClearPref(hotword_internal::kHotwordUnusablePrefName); | 228 profile_->GetPrefs()->ClearPref(hotword_internal::kHotwordUnusablePrefName); |
233 } | 229 } |
234 } | 230 } |
235 | 231 |
236 HotwordService::~HotwordService() { | 232 HotwordService::~HotwordService() { |
237 } | 233 } |
238 | 234 |
239 void HotwordService::Observe(int type, | |
240 const content::NotificationSource& source, | |
241 const content::NotificationDetails& details) { | |
242 if (type == chrome::NOTIFICATION_BROWSER_WINDOW_READY) { | |
243 // The microphone monitor must be initialized as the page is loading | |
244 // so that the state of the microphone is available when the page | |
245 // loads. The Ok Google Hotword setting will display an error if there | |
246 // is no microphone but this information will not be up-to-date unless | |
247 // the monitor had already been started. Furthermore, the pop up to | |
248 // opt in to hotwording won't be available if it thinks there is no | |
249 // microphone. There is no hard guarantee that the monitor will actually | |
250 // be up by the time it's needed, but this is the best we can do without | |
251 // starting it at start up which slows down start up too much. | |
252 // The content/media for microphone uses the same observer design and | |
253 // makes use of the same audio device monitor. | |
254 HotwordServiceFactory::GetInstance()->UpdateMicrophoneState(); | |
255 } | |
256 } | |
257 | |
258 void HotwordService::OnExtensionUninstalled( | 235 void HotwordService::OnExtensionUninstalled( |
259 content::BrowserContext* browser_context, | 236 content::BrowserContext* browser_context, |
260 const extensions::Extension* extension, | 237 const extensions::Extension* extension, |
261 extensions::UninstallReason reason) { | 238 extensions::UninstallReason reason) { |
262 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 239 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
263 | 240 |
264 if (extension->id() != extension_misc::kHotwordExtensionId || | 241 if (extension->id() != extension_misc::kHotwordExtensionId || |
265 profile_ != Profile::FromBrowserContext(browser_context) || | 242 profile_ != Profile::FromBrowserContext(browser_context) || |
266 !GetExtensionService(profile_)) | 243 !GetExtensionService(profile_)) |
267 return; | 244 return; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile_).get(); | 385 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile_).get(); |
409 if (content::PluginService::GetInstance()->GetPluginInfoByPath(path, &info)) | 386 if (content::PluginService::GetInstance()->GetPluginInfoByPath(path, &info)) |
410 nacl_enabled = plugin_prefs->IsPluginEnabled(info); | 387 nacl_enabled = plugin_prefs->IsPluginEnabled(info); |
411 } | 388 } |
412 if (!nacl_enabled) | 389 if (!nacl_enabled) |
413 error_message_ = IDS_HOTWORD_NACL_DISABLED_ERROR_MESSAGE; | 390 error_message_ = IDS_HOTWORD_NACL_DISABLED_ERROR_MESSAGE; |
414 | 391 |
415 RecordErrorMetrics(error_message_); | 392 RecordErrorMetrics(error_message_); |
416 | 393 |
417 // Determine if the proper audio capabilities exist. | 394 // Determine if the proper audio capabilities exist. |
418 bool audio_capture_allowed = | 395 // The first time this is called, it probably won't return in time, but that's |
419 profile_->GetPrefs()->GetBoolean(prefs::kAudioCaptureAllowed); | 396 // why it won't be included in the error calculation (i.e., the call to |
420 if (!audio_capture_allowed || !HotwordServiceFactory::IsMicrophoneAvailable()) | 397 // IsAudioDeviceStateUpdated()). However, this use case is rare and typically |
421 error_message_ = IDS_HOTWORD_MICROPHONE_ERROR_MESSAGE; | 398 // the devices will be initialized by the time a user goes to settings. |
399 bool audio_device_state_updated = | |
400 HotwordServiceFactory::IsAudioDeviceStateUpdated(); | |
401 HotwordServiceFactory::GetInstance()->UpdateMicrophoneState(); | |
Anand Mistry (off Chromium)
2014/10/17 20:32:42
If you call UpdateMicrophoneState() first, then Is
rpetterson
2014/10/20 06:25:50
Thanks for your comment. It made me look a little
| |
402 if (audio_device_state_updated) { | |
403 bool audio_capture_allowed = | |
404 profile_->GetPrefs()->GetBoolean(prefs::kAudioCaptureAllowed); | |
405 if (!audio_capture_allowed || | |
406 !HotwordServiceFactory::IsMicrophoneAvailable()) | |
407 error_message_ = IDS_HOTWORD_MICROPHONE_ERROR_MESSAGE; | |
408 } | |
422 | 409 |
423 return (error_message_ == 0) && IsHotwordAllowed(); | 410 return (error_message_ == 0) && IsHotwordAllowed(); |
424 } | 411 } |
425 | 412 |
426 bool HotwordService::IsHotwordAllowed() { | 413 bool HotwordService::IsHotwordAllowed() { |
427 std::string group = base::FieldTrialList::FindFullName( | 414 std::string group = base::FieldTrialList::FindFullName( |
428 hotword_internal::kHotwordFieldTrialName); | 415 hotword_internal::kHotwordFieldTrialName); |
429 return !group.empty() && | 416 return !group.empty() && |
430 group != hotword_internal::kHotwordFieldTrialDisabledGroupName && | 417 group != hotword_internal::kHotwordFieldTrialDisabledGroupName && |
431 DoesHotwordSupportLanguage(profile_); | 418 DoesHotwordSupportLanguage(profile_); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 return false; | 518 return false; |
532 | 519 |
533 std::string previous_locale = | 520 std::string previous_locale = |
534 profile_->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage); | 521 profile_->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage); |
535 std::string locale = GetCurrentLocale(profile_); | 522 std::string locale = GetCurrentLocale(profile_); |
536 | 523 |
537 // If it's a new locale, then the old extension should be uninstalled. | 524 // If it's a new locale, then the old extension should be uninstalled. |
538 return locale != previous_locale && | 525 return locale != previous_locale && |
539 HotwordService::DoesHotwordSupportLanguage(profile_); | 526 HotwordService::DoesHotwordSupportLanguage(profile_); |
540 } | 527 } |
OLD | NEW |