| 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 extension->id() != extension_misc::kHotwordSharedModuleId) || | 242 extension->id() != extension_misc::kHotwordSharedModuleId) || |
| 266 profile_ != Profile::FromBrowserContext(browser_context) || | 243 profile_ != Profile::FromBrowserContext(browser_context) || |
| 267 !GetExtensionService(profile_)) | 244 !GetExtensionService(profile_)) |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile_).get(); | 387 PluginPrefs* plugin_prefs = PluginPrefs::GetForProfile(profile_).get(); |
| 411 if (content::PluginService::GetInstance()->GetPluginInfoByPath(path, &info)) | 388 if (content::PluginService::GetInstance()->GetPluginInfoByPath(path, &info)) |
| 412 nacl_enabled = plugin_prefs->IsPluginEnabled(info); | 389 nacl_enabled = plugin_prefs->IsPluginEnabled(info); |
| 413 } | 390 } |
| 414 if (!nacl_enabled) | 391 if (!nacl_enabled) |
| 415 error_message_ = IDS_HOTWORD_NACL_DISABLED_ERROR_MESSAGE; | 392 error_message_ = IDS_HOTWORD_NACL_DISABLED_ERROR_MESSAGE; |
| 416 | 393 |
| 417 RecordErrorMetrics(error_message_); | 394 RecordErrorMetrics(error_message_); |
| 418 | 395 |
| 419 // Determine if the proper audio capabilities exist. | 396 // Determine if the proper audio capabilities exist. |
| 420 bool audio_capture_allowed = | 397 // The first time this is called, it probably won't return in time, but that's |
| 421 profile_->GetPrefs()->GetBoolean(prefs::kAudioCaptureAllowed); | 398 // why it won't be included in the error calculation (i.e., the call to |
| 422 if (!audio_capture_allowed || !HotwordServiceFactory::IsMicrophoneAvailable()) | 399 // IsAudioDeviceStateUpdated()). However, this use case is rare and typically |
| 423 error_message_ = IDS_HOTWORD_MICROPHONE_ERROR_MESSAGE; | 400 // the devices will be initialized by the time a user goes to settings. |
| 401 bool audio_device_state_updated = |
| 402 HotwordServiceFactory::IsAudioDeviceStateUpdated(); |
| 403 HotwordServiceFactory::GetInstance()->UpdateMicrophoneState(); |
| 404 if (audio_device_state_updated) { |
| 405 bool audio_capture_allowed = |
| 406 profile_->GetPrefs()->GetBoolean(prefs::kAudioCaptureAllowed); |
| 407 if (!audio_capture_allowed || |
| 408 !HotwordServiceFactory::IsMicrophoneAvailable()) |
| 409 error_message_ = IDS_HOTWORD_MICROPHONE_ERROR_MESSAGE; |
| 410 } |
| 424 | 411 |
| 425 return (error_message_ == 0) && IsHotwordAllowed(); | 412 return (error_message_ == 0) && IsHotwordAllowed(); |
| 426 } | 413 } |
| 427 | 414 |
| 428 bool HotwordService::IsHotwordAllowed() { | 415 bool HotwordService::IsHotwordAllowed() { |
| 429 std::string group = base::FieldTrialList::FindFullName( | 416 std::string group = base::FieldTrialList::FindFullName( |
| 430 hotword_internal::kHotwordFieldTrialName); | 417 hotword_internal::kHotwordFieldTrialName); |
| 431 return !group.empty() && | 418 return !group.empty() && |
| 432 group != hotword_internal::kHotwordFieldTrialDisabledGroupName && | 419 group != hotword_internal::kHotwordFieldTrialDisabledGroupName && |
| 433 DoesHotwordSupportLanguage(profile_); | 420 DoesHotwordSupportLanguage(profile_); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 533 return false; | 520 return false; |
| 534 | 521 |
| 535 std::string previous_locale = | 522 std::string previous_locale = |
| 536 profile_->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage); | 523 profile_->GetPrefs()->GetString(prefs::kHotwordPreviousLanguage); |
| 537 std::string locale = GetCurrentLocale(profile_); | 524 std::string locale = GetCurrentLocale(profile_); |
| 538 | 525 |
| 539 // If it's a new locale, then the old extension should be uninstalled. | 526 // If it's a new locale, then the old extension should be uninstalled. |
| 540 return locale != previous_locale && | 527 return locale != previous_locale && |
| 541 HotwordService::DoesHotwordSupportLanguage(profile_); | 528 HotwordService::DoesHotwordSupportLanguage(profile_); |
| 542 } | 529 } |
| OLD | NEW |