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" |
11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/extensions/extension_constants.h" | 15 #include "chrome/common/extensions/extension_constants.h" |
16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
19 #include "extensions/browser/extension_system.h" | 19 #include "extensions/browser/extension_system.h" |
20 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
22 | 22 |
| 23 // The whole file relies on the extension systems but this file is built on |
| 24 // some non-extension supported platforms and including an API header will cause |
| 25 // a compile error since it depends on header files generated by .idl. |
| 26 // TODO(mukai): clean up file dependencies and remove this clause. |
| 27 #if defined(ENABLE_EXTENSIONS) |
| 28 #include "chrome/browser/extensions/api/hotword_private/hotword_private_api.h" |
| 29 #endif |
| 30 |
| 31 #if defined(ENABLE_EXTENSIONS) |
| 32 using extensions::BrowserContextKeyedAPIFactory; |
| 33 using extensions::HotwordPrivateEventService; |
| 34 #endif |
| 35 |
23 namespace { | 36 namespace { |
24 const int kMaxTimesToShowOptInPopup = 10; | 37 const int kMaxTimesToShowOptInPopup = 10; |
25 | 38 |
26 // Allowed languages for hotwording. | 39 // Allowed languages for hotwording. |
27 static const char* kSupportedLocales[] = { | 40 static const char* kSupportedLocales[] = { |
28 "en", | 41 "en", |
29 "de", | 42 "de", |
30 "fr", | 43 "fr", |
31 "ru" | 44 "ru" |
32 }; | 45 }; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 StringToLowerASCII(&normalized_locale); | 128 StringToLowerASCII(&normalized_locale); |
116 | 129 |
117 for (size_t i = 0; i < arraysize(kSupportedLocales); i++) { | 130 for (size_t i = 0; i < arraysize(kSupportedLocales); i++) { |
118 if (normalized_locale.compare(0, 2, kSupportedLocales[i]) == 0) | 131 if (normalized_locale.compare(0, 2, kSupportedLocales[i]) == 0) |
119 return true; | 132 return true; |
120 } | 133 } |
121 return false; | 134 return false; |
122 } | 135 } |
123 | 136 |
124 HotwordService::HotwordService(Profile* profile) | 137 HotwordService::HotwordService(Profile* profile) |
125 : profile_(profile) { | 138 : profile_(profile), |
| 139 client_(NULL) { |
126 // This will be called during profile initialization which is a good time | 140 // This will be called during profile initialization which is a good time |
127 // to check the user's hotword state. | 141 // to check the user's hotword state. |
128 HotwordEnabled enabled_state = UNSET; | 142 HotwordEnabled enabled_state = UNSET; |
129 if (profile_->GetPrefs()->HasPrefPath(prefs::kHotwordSearchEnabled)) { | 143 if (profile_->GetPrefs()->HasPrefPath(prefs::kHotwordSearchEnabled)) { |
130 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled)) | 144 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled)) |
131 enabled_state = ENABLED; | 145 enabled_state = ENABLED; |
132 else | 146 else |
133 enabled_state = DISABLED; | 147 enabled_state = DISABLED; |
134 } else { | 148 } else { |
135 // If the preference has not been set the hotword extension should | 149 // If the preference has not been set the hotword extension should |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 void HotwordService::OnHotwordSearchEnabledChanged( | 282 void HotwordService::OnHotwordSearchEnabledChanged( |
269 const std::string& pref_name) { | 283 const std::string& pref_name) { |
270 DCHECK_EQ(pref_name, std::string(prefs::kHotwordSearchEnabled)); | 284 DCHECK_EQ(pref_name, std::string(prefs::kHotwordSearchEnabled)); |
271 | 285 |
272 ExtensionService* extension_service = GetExtensionService(profile_); | 286 ExtensionService* extension_service = GetExtensionService(profile_); |
273 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled)) | 287 if (profile_->GetPrefs()->GetBoolean(prefs::kHotwordSearchEnabled)) |
274 EnableHotwordExtension(extension_service); | 288 EnableHotwordExtension(extension_service); |
275 else | 289 else |
276 DisableHotwordExtension(extension_service); | 290 DisableHotwordExtension(extension_service); |
277 } | 291 } |
| 292 |
| 293 void HotwordService::RequestHotwordSession(HotwordClient* client) { |
| 294 #if defined(ENABLE_EXTENSIONS) |
| 295 if (!IsServiceAvailable() || client_) |
| 296 return; |
| 297 |
| 298 client_ = client; |
| 299 |
| 300 HotwordPrivateEventService* event_service = |
| 301 BrowserContextKeyedAPIFactory<HotwordPrivateEventService>::Get(profile_); |
| 302 if (event_service) |
| 303 event_service->OnHotwordSessionRequested(); |
| 304 #endif |
| 305 } |
| 306 |
| 307 void HotwordService::StopHotwordSession(HotwordClient* client) { |
| 308 #if defined(ENABLE_EXTENSIONS) |
| 309 if (!IsServiceAvailable()) |
| 310 return; |
| 311 |
| 312 DCHECK(client_ == client); |
| 313 |
| 314 client_ = NULL; |
| 315 HotwordPrivateEventService* event_service = |
| 316 BrowserContextKeyedAPIFactory<HotwordPrivateEventService>::Get(profile_); |
| 317 if (event_service) |
| 318 event_service->OnHotwordSessionStopped(); |
| 319 #endif |
| 320 } |
OLD | NEW |