| 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/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 availability_state = PENDING_DOWNLOAD; | 53 availability_state = PENDING_DOWNLOAD; |
| 54 } else if (!service->IsExtensionEnabled( | 54 } else if (!service->IsExtensionEnabled( |
| 55 extension_misc::kHotwordExtensionId)) { | 55 extension_misc::kHotwordExtensionId)) { |
| 56 availability_state = DISABLED_EXTENSION; | 56 availability_state = DISABLED_EXTENSION; |
| 57 } | 57 } |
| 58 UMA_HISTOGRAM_ENUMERATION("Hotword.HotwordExtensionAvailability", | 58 UMA_HISTOGRAM_ENUMERATION("Hotword.HotwordExtensionAvailability", |
| 59 availability_state, | 59 availability_state, |
| 60 NUM_HOTWORD_EXTENSION_AVAILABILITY_METRICS); | 60 NUM_HOTWORD_EXTENSION_AVAILABILITY_METRICS); |
| 61 } | 61 } |
| 62 | 62 |
| 63 ExtensionService* GetExtensionService(Profile* profile) { |
| 64 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 65 |
| 66 extensions::ExtensionSystem* extension_system = |
| 67 extensions::ExtensionSystem::Get(profile); |
| 68 if (extension_system) |
| 69 return extension_system->extension_service(); |
| 70 return NULL; |
| 71 } |
| 72 |
| 63 } // namespace | 73 } // namespace |
| 64 | 74 |
| 65 namespace hotword_internal { | 75 namespace hotword_internal { |
| 66 // Constants for the hotword field trial. | 76 // Constants for the hotword field trial. |
| 67 const char kHotwordFieldTrialName[] = "VoiceTrigger"; | 77 const char kHotwordFieldTrialName[] = "VoiceTrigger"; |
| 68 const char kHotwordFieldTrialDisabledGroupName[] = "Disabled"; | 78 const char kHotwordFieldTrialDisabledGroupName[] = "Disabled"; |
| 69 } // namespace hotword_internal | 79 } // namespace hotword_internal |
| 70 | 80 |
| 71 // static | 81 // static |
| 72 bool HotwordService::DoesHotwordSupportLanguage(Profile* profile) { | 82 bool HotwordService::DoesHotwordSupportLanguage(Profile* profile) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 86 HotwordService::HotwordService(Profile* profile) | 96 HotwordService::HotwordService(Profile* profile) |
| 87 : profile_(profile) { | 97 : profile_(profile) { |
| 88 // This will be called during profile initialization which is a good time | 98 // This will be called during profile initialization which is a good time |
| 89 // to check the user's hotword state. | 99 // to check the user's hotword state. |
| 90 HotwordEnabled enabled_state = UNSET; | 100 HotwordEnabled enabled_state = UNSET; |
| 91 if (profile_->GetPrefs()->HasPrefPath(prefs::kHotwordSearchEnabled)) { | 101 if (profile_->GetPrefs()->HasPrefPath(prefs::kHotwordSearchEnabled)) { |
| 92 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled)) | 102 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled)) |
| 93 enabled_state = ENABLED; | 103 enabled_state = ENABLED; |
| 94 else | 104 else |
| 95 enabled_state = DISABLED; | 105 enabled_state = DISABLED; |
| 106 } else { |
| 107 // If the preference has not been set the hotword extension should |
| 108 // not be running. |
| 109 DisableHotwordExtension(GetExtensionService(profile_)); |
| 96 } | 110 } |
| 97 UMA_HISTOGRAM_ENUMERATION("Hotword.Enabled", enabled_state, | 111 UMA_HISTOGRAM_ENUMERATION("Hotword.Enabled", enabled_state, |
| 98 NUM_HOTWORD_ENABLED_METRICS); | 112 NUM_HOTWORD_ENABLED_METRICS); |
| 113 |
| 114 pref_registrar_.Init(profile_->GetPrefs()); |
| 115 pref_registrar_.Add( |
| 116 prefs::kHotwordSearchEnabled, |
| 117 base::Bind(&HotwordService::OnHotwordSearchEnabledChanged, |
| 118 base::Unretained(this))); |
| 99 } | 119 } |
| 100 | 120 |
| 101 HotwordService::~HotwordService() { | 121 HotwordService::~HotwordService() { |
| 102 } | 122 } |
| 103 | 123 |
| 104 bool HotwordService::ShouldShowOptInPopup() { | 124 bool HotwordService::ShouldShowOptInPopup() { |
| 105 if (profile_->IsOffTheRecord()) | 125 if (profile_->IsOffTheRecord()) |
| 106 return false; | 126 return false; |
| 107 | 127 |
| 108 // Profile is not off the record. | 128 // Profile is not off the record. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 123 prefs::kHotwordOptInPopupTimesShown); | 143 prefs::kHotwordOptInPopupTimesShown); |
| 124 profile_->GetPrefs()->SetInteger(prefs::kHotwordOptInPopupTimesShown, | 144 profile_->GetPrefs()->SetInteger(prefs::kHotwordOptInPopupTimesShown, |
| 125 ++number_shown); | 145 ++number_shown); |
| 126 // TODO(rlp): actually show opt in popup when linked up to extension. | 146 // TODO(rlp): actually show opt in popup when linked up to extension. |
| 127 } | 147 } |
| 128 | 148 |
| 129 bool HotwordService::IsServiceAvailable() { | 149 bool HotwordService::IsServiceAvailable() { |
| 130 extensions::ExtensionSystem* system = | 150 extensions::ExtensionSystem* system = |
| 131 extensions::ExtensionSystem::Get(profile_); | 151 extensions::ExtensionSystem::Get(profile_); |
| 132 ExtensionService* service = system->extension_service(); | 152 ExtensionService* service = system->extension_service(); |
| 133 // Do not include disabled extension (false parameter) because if the | 153 // Include disabled extensions (true parameter) since it may not be enabled |
| 134 // extension is disabled, it's not available. | 154 // if the user opted out. |
| 135 const extensions::Extension* extension = | 155 const extensions::Extension* extension = |
| 136 service->GetExtensionById(extension_misc::kHotwordExtensionId, false); | 156 service->GetExtensionById(extension_misc::kHotwordExtensionId, true); |
| 137 | 157 |
| 138 RecordAvailabilityMetrics(service, extension); | 158 RecordAvailabilityMetrics(service, extension); |
| 139 | 159 |
| 140 return extension && IsHotwordAllowed(); | 160 return extension && IsHotwordAllowed(); |
| 141 } | 161 } |
| 142 | 162 |
| 143 bool HotwordService::IsHotwordAllowed() { | 163 bool HotwordService::IsHotwordAllowed() { |
| 144 std::string group = base::FieldTrialList::FindFullName( | 164 std::string group = base::FieldTrialList::FindFullName( |
| 145 hotword_internal::kHotwordFieldTrialName); | 165 hotword_internal::kHotwordFieldTrialName); |
| 146 return !group.empty() && | 166 return !group.empty() && |
| 147 group != hotword_internal::kHotwordFieldTrialDisabledGroupName && | 167 group != hotword_internal::kHotwordFieldTrialDisabledGroupName && |
| 148 DoesHotwordSupportLanguage(profile_); | 168 DoesHotwordSupportLanguage(profile_); |
| 149 } | 169 } |
| 170 |
| 171 bool HotwordService::RetryHotwordExtension() { |
| 172 ExtensionService* extension_service = GetExtensionService(profile_); |
| 173 if (!extension_service) |
| 174 return false; |
| 175 |
| 176 extension_service->ReloadExtension(extension_misc::kHotwordExtensionId); |
| 177 return true; |
| 178 } |
| 179 |
| 180 void HotwordService::EnableHotwordExtension( |
| 181 ExtensionService* extension_service) { |
| 182 if (extension_service) |
| 183 extension_service->EnableExtension(extension_misc::kHotwordExtensionId); |
| 184 } |
| 185 |
| 186 void HotwordService::DisableHotwordExtension( |
| 187 ExtensionService* extension_service) { |
| 188 if (extension_service) { |
| 189 extension_service->DisableExtension( |
| 190 extension_misc::kHotwordExtensionId, |
| 191 extensions::Extension::DISABLE_USER_ACTION); |
| 192 } |
| 193 } |
| 194 |
| 195 void HotwordService::OnHotwordSearchEnabledChanged( |
| 196 const std::string& pref_name) { |
| 197 DCHECK_EQ(pref_name, std::string(prefs::kHotwordSearchEnabled)); |
| 198 |
| 199 ExtensionService* extension_service = GetExtensionService(profile_); |
| 200 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled)) |
| 201 EnableHotwordExtension(extension_service); |
| 202 else |
| 203 DisableHotwordExtension(extension_service); |
| 204 } |
| OLD | NEW |