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