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

Side by Side Diff: chrome/browser/search/hotword_service.cc

Issue 196753002: [Hotword] Making enabling/disabling the setting enable/disable the hotword extension. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months 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
« no previous file with comments | « chrome/browser/search/hotword_service.h ('k') | extensions/browser/admin_policy.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/threading/worker_pool.h"
11 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h"
12 #include "chrome/browser/extensions/extension_service.h" 14 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/extensions/extension_constants.h" 16 #include "chrome/common/extensions/extension_constants.h"
15 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
16 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 #include "extensions/browser/browser_context_keyed_api_factory.h"
17 #include "extensions/browser/extension_system.h" 20 #include "extensions/browser/extension_system.h"
18 #include "extensions/common/extension.h" 21 #include "extensions/common/extension.h"
19 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
20 23
21 namespace { 24 namespace {
22 const int kMaxTimesToShowOptInPopup = 10; 25 const int kMaxTimesToShowOptInPopup = 10;
23 26
24 // Enum describing the state of the hotword preference. 27 // Enum describing the state of the hotword preference.
25 // This is used for UMA stats -- do not reorder or delete items; only add to 28 // This is used for UMA stats -- do not reorder or delete items; only add to
26 // the end. 29 // the end.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 std::string normalized_locale = l10n_util::NormalizeLocale(locale); 85 std::string normalized_locale = l10n_util::NormalizeLocale(locale);
83 return normalized_locale == "en" || normalized_locale == "en_us" || 86 return normalized_locale == "en" || normalized_locale == "en_us" ||
84 normalized_locale =="en_US"; 87 normalized_locale =="en_US";
85 } 88 }
86 89
87 HotwordService::HotwordService(Profile* profile) 90 HotwordService::HotwordService(Profile* profile)
88 : profile_(profile) { 91 : profile_(profile) {
89 // This will be called during profile initialization which is a good time 92 // This will be called during profile initialization which is a good time
90 // to check the user's hotword state. 93 // to check the user's hotword state.
91 HotwordEnabled enabled_state = UNSET; 94 HotwordEnabled enabled_state = UNSET;
92 if (profile_->GetPrefs()->HasPrefPath(prefs::kHotwordSearchEnabled)) { 95 if (profile_->GetPrefs()->HasPrefPath(prefs::kHotwordSearchEnabled)) {
Jered 2014/03/12 20:43:45 Do you also need to call OnHotwordSearchEnabledCha
rpetterson 2014/03/12 20:51:25 No we don't. By default, extensions are enabled. S
rpetterson 2014/03/12 20:52:28 Let me clarify just a bit more. When the preferenc
93 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled)) 96 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled))
94 enabled_state = ENABLED; 97 enabled_state = ENABLED;
95 else 98 else
96 enabled_state = DISABLED; 99 enabled_state = DISABLED;
100 } else {
101 // If the preference has not been set the hotword extension should
102 // not be running.
103 DisableHotwordExtension();
97 } 104 }
98 UMA_HISTOGRAM_ENUMERATION("Hotword.Enabled", enabled_state, 105 UMA_HISTOGRAM_ENUMERATION("Hotword.Enabled", enabled_state,
99 NUM_HOTWORD_ENABLED_METRICS); 106 NUM_HOTWORD_ENABLED_METRICS);
107
108 pref_registrar_.Init(profile_->GetPrefs());
109 pref_registrar_.Add(
110 prefs::kHotwordSearchEnabled,
111 base::Bind(&HotwordService::OnHotwordSearchEnabledChanged,
112 base::Unretained(this)));
100 } 113 }
101 114
102 HotwordService::~HotwordService() { 115 HotwordService::~HotwordService() {
103 } 116 }
104 117
105 bool HotwordService::ShouldShowOptInPopup() { 118 bool HotwordService::ShouldShowOptInPopup() {
106 if (profile_->IsOffTheRecord()) 119 if (profile_->IsOffTheRecord())
107 return false; 120 return false;
108 121
109 // Profile is not off the record. 122 // Profile is not off the record.
(...skipping 14 matching lines...) Expand all
124 prefs::kHotwordOptInPopupTimesShown); 137 prefs::kHotwordOptInPopupTimesShown);
125 profile_->GetPrefs()->SetInteger(prefs::kHotwordOptInPopupTimesShown, 138 profile_->GetPrefs()->SetInteger(prefs::kHotwordOptInPopupTimesShown,
126 ++number_shown); 139 ++number_shown);
127 // TODO(rlp): actually show opt in popup when linked up to extension. 140 // TODO(rlp): actually show opt in popup when linked up to extension.
128 } 141 }
129 142
130 bool HotwordService::IsServiceAvailable() { 143 bool HotwordService::IsServiceAvailable() {
131 extensions::ExtensionSystem* system = 144 extensions::ExtensionSystem* system =
132 extensions::ExtensionSystem::Get(profile_); 145 extensions::ExtensionSystem::Get(profile_);
133 ExtensionService* service = system->extension_service(); 146 ExtensionService* service = system->extension_service();
134 // Do not include disabled extension (false parameter) because if the 147 // Do not include disabled extension (false parameter) because if the
Jered 2014/03/12 20:43:45 nit: Update this comment.
rpetterson 2014/03/12 20:51:25 Done.
135 // extension is disabled, it's not available. 148 // extension is disabled, it's not available.
136 const extensions::Extension* extension = 149 const extensions::Extension* extension =
137 service->GetExtensionById(extension_misc::kHotwordExtensionId, false); 150 service->GetExtensionById(extension_misc::kHotwordExtensionId, true);
138 151
139 RecordAvailabilityMetrics(service, extension); 152 RecordAvailabilityMetrics(service, extension);
140 153
141 return extension && IsHotwordAllowed(); 154 return extension && IsHotwordAllowed();
142 } 155 }
143 156
144 bool HotwordService::IsHotwordAllowed() { 157 bool HotwordService::IsHotwordAllowed() {
145 std::string group = base::FieldTrialList::FindFullName( 158 std::string group = base::FieldTrialList::FindFullName(
146 hotword_internal::kHotwordFieldTrialName); 159 hotword_internal::kHotwordFieldTrialName);
147 return !group.empty() && 160 return !group.empty() &&
148 group != hotword_internal::kHotwordFieldTrialDisabledGroupName && 161 group != hotword_internal::kHotwordFieldTrialDisabledGroupName &&
149 DoesHotwordSupportLanguage(profile_); 162 DoesHotwordSupportLanguage(profile_);
150 } 163 }
151 164
152 bool HotwordService::RetryHotwordExtension() { 165 bool HotwordService::RetryHotwordExtension() {
153 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 166 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
154 167
155 extensions::ExtensionSystem* extension_system = 168 extensions::ExtensionSystem* extension_system =
156 extensions::ExtensionSystem::Get(profile_); 169 extensions::ExtensionSystem::Get(profile_);
157 if (!extension_system || !extension_system->extension_service()) 170 if (!extension_system || !extension_system->extension_service())
158 return false; 171 return false;
159 ExtensionService* extension_service = extension_system->extension_service(); 172 ExtensionService* extension_service = extension_system->extension_service();
160 173
161 extension_service->ReloadExtension(extension_misc::kHotwordExtensionId); 174 extension_service->ReloadExtension(extension_misc::kHotwordExtensionId);
162 return true; 175 return true;
163 } 176 }
177
178 void HotwordService::DisableHotwordExtension() {
179 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
180
181 extensions::ExtensionSystem* extension_system =
182 extensions::ExtensionSystem::Get(profile_);
183 if (!extension_system || !extension_system->extension_service())
184 return;
185 ExtensionService* extension_service = extension_system->extension_service();
186
187 extension_service->DisableExtension(
188 extension_misc::kHotwordExtensionId,
189 extensions::Extension::DISABLE_USER_ACTION);
190 }
191
192 void HotwordService::EnableHotwordExtension() {
193 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
194
195 extensions::ExtensionSystem* extension_system =
196 extensions::ExtensionSystem::Get(profile_);
197 if (!extension_system || !extension_system->extension_service())
198 return;
199 ExtensionService* extension_service = extension_system->extension_service();
200
201 extension_service->EnableExtension(extension_misc::kHotwordExtensionId);
202 }
203
204 void HotwordService::OnHotwordSearchEnabledChanged(
205 const std::string& pref_name) {
206 DCHECK_EQ(pref_name, std::string(prefs::kHotwordSearchEnabled));
207
208 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled))
209 EnableHotwordExtension();
210 else
211 DisableHotwordExtension();
212 }
OLDNEW
« no previous file with comments | « chrome/browser/search/hotword_service.h ('k') | extensions/browser/admin_policy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698