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/ui/webui/app_list/start_page_handler.h" | 5 #include "chrome/browser/ui/webui/app_list/start_page_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/search/hotword_service.h" | 16 #include "chrome/browser/search/hotword_service.h" |
17 #include "chrome/browser/search/hotword_service_factory.h" | 17 #include "chrome/browser/search/hotword_service_factory.h" |
18 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" | 18 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" |
19 #include "chrome/browser/ui/app_list/app_list_service.h" | 19 #include "chrome/browser/ui/app_list/app_list_service.h" |
20 #include "chrome/browser/ui/app_list/hotword_background_activity_monitor.h" | |
21 #include "chrome/browser/ui/app_list/recommended_apps.h" | 20 #include "chrome/browser/ui/app_list/recommended_apps.h" |
22 #include "chrome/browser/ui/app_list/start_page_service.h" | 21 #include "chrome/browser/ui/app_list/start_page_service.h" |
23 #include "chrome/browser/ui/host_desktop.h" | 22 #include "chrome/browser/ui/host_desktop.h" |
24 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 23 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
25 #include "chrome/common/extensions/extension_icon_set.h" | 24 #include "chrome/common/extensions/extension_icon_set.h" |
26 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
27 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
28 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
29 #include "content/public/browser/render_process_host.h" | |
30 #include "content/public/browser/web_contents.h" | |
31 #include "content/public/browser/web_contents_view.h" | 28 #include "content/public/browser/web_contents_view.h" |
32 #include "content/public/browser/web_ui.h" | 29 #include "content/public/browser/web_ui.h" |
33 #include "content/public/common/child_process_host.h" | |
34 #include "extensions/browser/extension_system.h" | 30 #include "extensions/browser/extension_system.h" |
35 #include "extensions/common/extension.h" | 31 #include "extensions/common/extension.h" |
36 #include "ui/app_list/app_list_switches.h" | 32 #include "ui/app_list/app_list_switches.h" |
| 33 #include "ui/app_list/speech_ui_model_observer.h" |
37 #include "ui/events/event_constants.h" | 34 #include "ui/events/event_constants.h" |
38 | 35 |
39 namespace app_list { | 36 namespace app_list { |
40 | 37 |
41 namespace { | 38 namespace { |
42 | 39 |
43 scoped_ptr<base::DictionaryValue> CreateAppInfo( | 40 scoped_ptr<base::DictionaryValue> CreateAppInfo( |
44 const extensions::Extension* app) { | 41 const extensions::Extension* app) { |
45 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 42 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
46 dict->SetString("appId", app->id()); | 43 dict->SetString("appId", app->id()); |
47 dict->SetString("textTitle", app->short_name()); | 44 dict->SetString("textTitle", app->short_name()); |
48 dict->SetString("title", app->name()); | 45 dict->SetString("title", app->name()); |
49 | 46 |
50 const bool grayscale = false; | 47 const bool grayscale = false; |
51 bool icon_exists = true; | 48 bool icon_exists = true; |
52 GURL icon_url = extensions::ExtensionIconSource::GetIconURL( | 49 GURL icon_url = extensions::ExtensionIconSource::GetIconURL( |
53 app, | 50 app, |
54 extension_misc::EXTENSION_ICON_MEDIUM, | 51 extension_misc::EXTENSION_ICON_MEDIUM, |
55 ExtensionIconSet::MATCH_BIGGER, | 52 ExtensionIconSet::MATCH_BIGGER, |
56 grayscale, | 53 grayscale, |
57 &icon_exists); | 54 &icon_exists); |
58 dict->SetString("iconUrl", icon_url.spec()); | 55 dict->SetString("iconUrl", icon_url.spec()); |
59 | 56 |
60 return dict.Pass(); | 57 return dict.Pass(); |
61 } | 58 } |
62 | 59 |
63 } // namespace | 60 } // namespace |
64 | 61 |
65 StartPageHandler::StartPageHandler() | 62 StartPageHandler::StartPageHandler() : recommended_apps_(NULL) {} |
66 : recommended_apps_(NULL), | |
67 has_hotword_recognizer_(false), | |
68 last_state_(SPEECH_RECOGNITION_OFF) { | |
69 } | |
70 | 63 |
71 StartPageHandler::~StartPageHandler() { | 64 StartPageHandler::~StartPageHandler() { |
72 if (recommended_apps_) | 65 if (recommended_apps_) |
73 recommended_apps_->RemoveObserver(this); | 66 recommended_apps_->RemoveObserver(this); |
74 } | 67 } |
75 | 68 |
76 void StartPageHandler::RegisterMessages() { | 69 void StartPageHandler::RegisterMessages() { |
77 web_ui()->RegisterMessageCallback( | 70 web_ui()->RegisterMessageCallback( |
78 "initialize", | 71 "initialize", |
79 base::Bind(&StartPageHandler::HandleInitialize, base::Unretained(this))); | 72 base::Bind(&StartPageHandler::HandleInitialize, base::Unretained(this))); |
80 web_ui()->RegisterMessageCallback( | 73 web_ui()->RegisterMessageCallback( |
81 "launchApp", | 74 "launchApp", |
82 base::Bind(&StartPageHandler::HandleLaunchApp, base::Unretained(this))); | 75 base::Bind(&StartPageHandler::HandleLaunchApp, base::Unretained(this))); |
83 web_ui()->RegisterMessageCallback( | 76 web_ui()->RegisterMessageCallback( |
84 "setHotwordRecognizerState", | |
85 base::Bind(&StartPageHandler::HandleHotwordRecognizerState, | |
86 base::Unretained(this))); | |
87 web_ui()->RegisterMessageCallback( | |
88 "speechResult", | 77 "speechResult", |
89 base::Bind(&StartPageHandler::HandleSpeechResult, | 78 base::Bind(&StartPageHandler::HandleSpeechResult, |
90 base::Unretained(this))); | 79 base::Unretained(this))); |
91 web_ui()->RegisterMessageCallback( | 80 web_ui()->RegisterMessageCallback( |
92 "speechSoundLevel", | 81 "speechSoundLevel", |
93 base::Bind(&StartPageHandler::HandleSpeechSoundLevel, | 82 base::Bind(&StartPageHandler::HandleSpeechSoundLevel, |
94 base::Unretained(this))); | 83 base::Unretained(this))); |
95 web_ui()->RegisterMessageCallback( | 84 web_ui()->RegisterMessageCallback( |
96 "setSpeechRecognitionState", | 85 "setSpeechRecognitionState", |
97 base::Bind(&StartPageHandler::HandleSpeechRecognition, | 86 base::Bind(&StartPageHandler::HandleSpeechRecognition, |
(...skipping 21 matching lines...) Expand all Loading... |
119 OnHotwordEnabledChanged(); | 108 OnHotwordEnabledChanged(); |
120 break; | 109 break; |
121 } | 110 } |
122 default: | 111 default: |
123 NOTREACHED(); | 112 NOTREACHED(); |
124 break; | 113 break; |
125 } | 114 } |
126 #endif | 115 #endif |
127 } | 116 } |
128 | 117 |
129 int StartPageHandler::GetRenderProcessID() { | |
130 if (!web_ui()) | |
131 return content::ChildProcessHost::kInvalidUniqueID; | |
132 content::WebContents* web_contents = web_ui()->GetWebContents(); | |
133 return web_contents->GetRenderProcessHost()->GetID(); | |
134 } | |
135 | |
136 void StartPageHandler::OnHotwordBackgroundActivityChanged() { | |
137 #if defined(OS_CHROMEOS) | |
138 if (has_hotword_recognizer_ && switches::IsHotwordAlwaysOnEnabled()) { | |
139 web_ui()->CallJavascriptFunction( | |
140 (hotword_monitor_ && hotword_monitor_->IsHotwordBackgroundActive()) ? | |
141 "appList.startPage.startHotwordRecognition" : | |
142 "appList.startPage.stopHotwordRecognition"); | |
143 } | |
144 #endif | |
145 } | |
146 | |
147 void StartPageHandler::OnRecommendedAppsChanged() { | 118 void StartPageHandler::OnRecommendedAppsChanged() { |
148 SendRecommendedApps(); | 119 SendRecommendedApps(); |
149 } | 120 } |
150 | 121 |
151 void StartPageHandler::SendRecommendedApps() { | 122 void StartPageHandler::SendRecommendedApps() { |
152 const RecommendedApps::Apps& recommends = recommended_apps_->apps(); | 123 const RecommendedApps::Apps& recommends = recommended_apps_->apps(); |
153 | 124 |
154 base::ListValue recommended_list; | 125 base::ListValue recommended_list; |
155 for (size_t i = 0; i < recommends.size(); ++i) { | 126 for (size_t i = 0; i < recommends.size(); ++i) { |
156 recommended_list.Append(CreateAppInfo(recommends[i].get()).release()); | 127 recommended_list.Append(CreateAppInfo(recommends[i].get()).release()); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 OnHotwordEnabledChanged(); | 179 OnHotwordEnabledChanged(); |
209 pref_change_registrar_.Init(profile->GetPrefs()); | 180 pref_change_registrar_.Init(profile->GetPrefs()); |
210 pref_change_registrar_.Add( | 181 pref_change_registrar_.Add( |
211 prefs::kHotwordSearchEnabled, | 182 prefs::kHotwordSearchEnabled, |
212 base::Bind(&StartPageHandler::OnHotwordEnabledChanged, | 183 base::Bind(&StartPageHandler::OnHotwordEnabledChanged, |
213 base::Unretained(this))); | 184 base::Unretained(this))); |
214 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 185 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
215 content::Source<Profile>(profile)); | 186 content::Source<Profile>(profile)); |
216 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 187 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
217 content::Source<Profile>(profile)); | 188 content::Source<Profile>(profile)); |
218 hotword_monitor_.reset(new HotwordBackgroundActivityMonitor(this)); | |
219 } | 189 } |
220 #endif | 190 #endif |
221 } | 191 } |
222 | 192 |
223 bool StartPageHandler::ShouldRunHotwordBackground() { | |
224 return has_hotword_recognizer_ && switches::IsHotwordAlwaysOnEnabled() && | |
225 hotword_monitor_ && hotword_monitor_->IsHotwordBackgroundActive(); | |
226 } | |
227 | |
228 void StartPageHandler::HandleLaunchApp(const base::ListValue* args) { | 193 void StartPageHandler::HandleLaunchApp(const base::ListValue* args) { |
229 std::string app_id; | 194 std::string app_id; |
230 CHECK(args->GetString(0, &app_id)); | 195 CHECK(args->GetString(0, &app_id)); |
231 | 196 |
232 Profile* profile = Profile::FromWebUI(web_ui()); | 197 Profile* profile = Profile::FromWebUI(web_ui()); |
233 ExtensionService* service = | 198 ExtensionService* service = |
234 extensions::ExtensionSystem::Get(profile)->extension_service(); | 199 extensions::ExtensionSystem::Get(profile)->extension_service(); |
235 const extensions::Extension* app = service->GetInstalledExtension(app_id); | 200 const extensions::Extension* app = service->GetInstalledExtension(app_id); |
236 if (!app) { | 201 if (!app) { |
237 NOTREACHED(); | 202 NOTREACHED(); |
238 return; | 203 return; |
239 } | 204 } |
240 | 205 |
241 AppListControllerDelegate* controller = AppListService::Get( | 206 AppListControllerDelegate* controller = AppListService::Get( |
242 chrome::GetHostDesktopTypeForNativeView( | 207 chrome::GetHostDesktopTypeForNativeView( |
243 web_ui()->GetWebContents()->GetView()->GetNativeView())) | 208 web_ui()->GetWebContents()->GetView()->GetNativeView())) |
244 ->GetControllerDelegate(); | 209 ->GetControllerDelegate(); |
245 controller->ActivateApp(profile, | 210 controller->ActivateApp(profile, |
246 app, | 211 app, |
247 AppListControllerDelegate::LAUNCH_FROM_APP_LIST, | 212 AppListControllerDelegate::LAUNCH_FROM_APP_LIST, |
248 ui::EF_NONE); | 213 ui::EF_NONE); |
249 } | 214 } |
250 | 215 |
251 void StartPageHandler::HandleHotwordRecognizerState( | |
252 const base::ListValue* args) { | |
253 CHECK(args->GetBoolean(0, &has_hotword_recognizer_)); | |
254 if (last_state_ == SPEECH_RECOGNITION_READY && ShouldRunHotwordBackground()) { | |
255 web_ui()->CallJavascriptFunction( | |
256 "appList.startPage.startHotwordRecognition"); | |
257 } | |
258 } | |
259 | |
260 void StartPageHandler::HandleSpeechResult(const base::ListValue* args) { | 216 void StartPageHandler::HandleSpeechResult(const base::ListValue* args) { |
261 base::string16 query; | 217 base::string16 query; |
262 bool is_final = false; | 218 bool is_final = false; |
263 CHECK(args->GetString(0, &query)); | 219 CHECK(args->GetString(0, &query)); |
264 CHECK(args->GetBoolean(1, &is_final)); | 220 CHECK(args->GetBoolean(1, &is_final)); |
265 | 221 |
266 StartPageService::Get(Profile::FromWebUI(web_ui()))->OnSpeechResult( | 222 StartPageService::Get(Profile::FromWebUI(web_ui()))->OnSpeechResult( |
267 query, is_final); | 223 query, is_final); |
268 } | 224 } |
269 | 225 |
(...skipping 16 matching lines...) Expand all Loading... |
286 new_state = SPEECH_RECOGNITION_READY; | 242 new_state = SPEECH_RECOGNITION_READY; |
287 else if (state_string == "HOTWORD_RECOGNIZING") | 243 else if (state_string == "HOTWORD_RECOGNIZING") |
288 new_state = SPEECH_RECOGNITION_HOTWORD_LISTENING; | 244 new_state = SPEECH_RECOGNITION_HOTWORD_LISTENING; |
289 else if (state_string == "RECOGNIZING") | 245 else if (state_string == "RECOGNIZING") |
290 new_state = SPEECH_RECOGNITION_RECOGNIZING; | 246 new_state = SPEECH_RECOGNITION_RECOGNIZING; |
291 else if (state_string == "IN_SPEECH") | 247 else if (state_string == "IN_SPEECH") |
292 new_state = SPEECH_RECOGNITION_IN_SPEECH; | 248 new_state = SPEECH_RECOGNITION_IN_SPEECH; |
293 else if (state_string == "STOPPING") | 249 else if (state_string == "STOPPING") |
294 new_state = SPEECH_RECOGNITION_STOPPING; | 250 new_state = SPEECH_RECOGNITION_STOPPING; |
295 | 251 |
296 last_state_ = new_state; | 252 StartPageService* service = |
297 Profile* profile = Profile::FromWebUI(web_ui()); | 253 StartPageService::Get(Profile::FromWebUI(web_ui())); |
298 StartPageService* service = StartPageService::Get(profile); | |
299 if (service) | 254 if (service) |
300 service->OnSpeechRecognitionStateChanged(new_state); | 255 service->OnSpeechRecognitionStateChanged(new_state); |
301 | |
302 if (new_state == SPEECH_RECOGNITION_READY && ShouldRunHotwordBackground()) { | |
303 web_ui()->CallJavascriptFunction( | |
304 "appList.startPage.startHotwordRecognition"); | |
305 } | |
306 } | 256 } |
307 | 257 |
308 } // namespace app_list | 258 } // namespace app_list |
OLD | NEW |