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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/app_list/start_page_handler.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/omaha_query_params/omaha_query_params.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search/hotword_service.h"
17 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
18 #include "chrome/browser/ui/app_list/app_list_service.h"
19 #include "chrome/browser/ui/app_list/recommended_apps.h"
20 #include "chrome/browser/ui/app_list/start_page_service.h"
21 #include "chrome/browser/ui/host_desktop.h"
22 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
23 #include "chrome/common/pref_names.h"
24 #include "content/public/browser/notification_details.h"
25 #include "content/public/browser/notification_source.h"
26 #include "content/public/browser/web_ui.h"
27 #include "extensions/browser/extension_system.h"
28 #include "extensions/common/extension.h"
29 #include "extensions/common/extension_icon_set.h"
30 #include "ui/app_list/app_list_switches.h"
31 #include "ui/app_list/speech_ui_model_observer.h"
32 #include "ui/events/event_constants.h"
38 scoped_ptr
<base::DictionaryValue
> CreateAppInfo(
39 const extensions::Extension
* app
) {
40 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
41 dict
->SetString("appId", app
->id());
42 dict
->SetString("textTitle", app
->short_name());
43 dict
->SetString("title", app
->name());
45 const bool grayscale
= false;
46 bool icon_exists
= true;
47 GURL icon_url
= extensions::ExtensionIconSource::GetIconURL(
49 extension_misc::EXTENSION_ICON_MEDIUM
,
50 ExtensionIconSet::MATCH_BIGGER
,
53 dict
->SetString("iconUrl", icon_url
.spec());
60 StartPageHandler::StartPageHandler() : recommended_apps_(NULL
) {}
62 StartPageHandler::~StartPageHandler() {
63 if (recommended_apps_
)
64 recommended_apps_
->RemoveObserver(this);
67 void StartPageHandler::RegisterMessages() {
68 web_ui()->RegisterMessageCallback(
70 base::Bind(&StartPageHandler::HandleInitialize
, base::Unretained(this)));
71 web_ui()->RegisterMessageCallback(
73 base::Bind(&StartPageHandler::HandleLaunchApp
, base::Unretained(this)));
74 web_ui()->RegisterMessageCallback(
76 base::Bind(&StartPageHandler::HandleSpeechResult
,
77 base::Unretained(this)));
78 web_ui()->RegisterMessageCallback(
80 base::Bind(&StartPageHandler::HandleSpeechSoundLevel
,
81 base::Unretained(this)));
82 web_ui()->RegisterMessageCallback(
83 "setSpeechRecognitionState",
84 base::Bind(&StartPageHandler::HandleSpeechRecognition
,
85 base::Unretained(this)));
88 void StartPageHandler::Observe(int type
,
89 const content::NotificationSource
& source
,
90 const content::NotificationDetails
& details
) {
91 #if defined(OS_CHROMEOS)
92 DCHECK_EQ(Profile::FromWebUI(web_ui()),
93 content::Source
<Profile
>(source
).ptr());
95 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
: {
96 extensions::Extension
* extension
=
97 content::Details
<extensions::Extension
>(details
).ptr();
98 if (extension
->id() == extension_misc::kHotwordExtensionId
)
99 OnHotwordEnabledChanged();
102 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
: {
103 extensions::UnloadedExtensionInfo
* info
=
104 content::Details
<extensions::UnloadedExtensionInfo
>(details
).ptr();
105 if (info
->extension
->id() == extension_misc::kHotwordExtensionId
)
106 OnHotwordEnabledChanged();
116 void StartPageHandler::OnRecommendedAppsChanged() {
117 SendRecommendedApps();
120 void StartPageHandler::SendRecommendedApps() {
121 const RecommendedApps::Apps
& recommends
= recommended_apps_
->apps();
123 base::ListValue recommended_list
;
124 for (size_t i
= 0; i
< recommends
.size(); ++i
) {
125 recommended_list
.Append(CreateAppInfo(recommends
[i
].get()).release());
128 web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps",
132 #if defined(OS_CHROMEOS)
133 void StartPageHandler::OnHotwordEnabledChanged() {
134 StartPageService
* service
= StartPageService::Get(
135 Profile::FromWebUI(web_ui()));
136 web_ui()->CallJavascriptFunction(
137 "appList.startPage.setHotwordEnabled",
138 base::FundamentalValue(service
->HotwordEnabled()));
142 void StartPageHandler::HandleInitialize(const base::ListValue
* args
) {
143 Profile
* profile
= Profile::FromWebUI(web_ui());
144 StartPageService
* service
= StartPageService::Get(profile
);
148 recommended_apps_
= service
->recommended_apps();
149 recommended_apps_
->AddObserver(this);
151 SendRecommendedApps();
153 #if defined(OS_CHROMEOS)
154 if (app_list::switches::IsVoiceSearchEnabled() &&
155 HotwordService::DoesHotwordSupportLanguage(profile
)) {
156 OnHotwordEnabledChanged();
157 pref_change_registrar_
.Init(profile
->GetPrefs());
158 pref_change_registrar_
.Add(
159 prefs::kHotwordSearchEnabled
,
160 base::Bind(&StartPageHandler::OnHotwordEnabledChanged
,
161 base::Unretained(this)));
163 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
164 content::Source
<Profile
>(profile
));
165 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED
,
166 content::Source
<Profile
>(profile
));
170 web_ui()->CallJavascriptFunction(
171 "appList.startPage.setNaclArch",
172 base::StringValue(chrome::OmahaQueryParams::GetNaclArch()));
174 if (!app_list::switches::IsExperimentalAppListEnabled()) {
175 web_ui()->CallJavascriptFunction(
176 "appList.startPage.onAppListShown",
177 base::FundamentalValue(service
->HotwordEnabled()));
181 void StartPageHandler::HandleLaunchApp(const base::ListValue
* args
) {
183 CHECK(args
->GetString(0, &app_id
));
185 Profile
* profile
= Profile::FromWebUI(web_ui());
186 ExtensionService
* service
=
187 extensions::ExtensionSystem::Get(profile
)->extension_service();
188 const extensions::Extension
* app
= service
->GetInstalledExtension(app_id
);
194 AppListControllerDelegate
* controller
= AppListService::Get(
195 chrome::GetHostDesktopTypeForNativeView(
196 web_ui()->GetWebContents()->GetNativeView()))->
197 GetControllerDelegate();
198 controller
->ActivateApp(profile
,
200 AppListControllerDelegate::LAUNCH_FROM_APP_LIST
,
204 void StartPageHandler::HandleSpeechResult(const base::ListValue
* args
) {
205 base::string16 query
;
206 bool is_final
= false;
207 CHECK(args
->GetString(0, &query
));
208 CHECK(args
->GetBoolean(1, &is_final
));
210 StartPageService::Get(Profile::FromWebUI(web_ui()))->OnSpeechResult(
214 void StartPageHandler::HandleSpeechSoundLevel(const base::ListValue
* args
) {
216 CHECK(args
->GetDouble(0, &level
));
218 StartPageService
* service
=
219 StartPageService::Get(Profile::FromWebUI(web_ui()));
221 service
->OnSpeechSoundLevelChanged(static_cast<int16
>(level
));
224 void StartPageHandler::HandleSpeechRecognition(const base::ListValue
* args
) {
225 std::string state_string
;
226 CHECK(args
->GetString(0, &state_string
));
228 SpeechRecognitionState new_state
= SPEECH_RECOGNITION_OFF
;
229 if (state_string
== "READY")
230 new_state
= SPEECH_RECOGNITION_READY
;
231 else if (state_string
== "HOTWORD_RECOGNIZING")
232 new_state
= SPEECH_RECOGNITION_HOTWORD_LISTENING
;
233 else if (state_string
== "RECOGNIZING")
234 new_state
= SPEECH_RECOGNITION_RECOGNIZING
;
235 else if (state_string
== "IN_SPEECH")
236 new_state
= SPEECH_RECOGNITION_IN_SPEECH
;
237 else if (state_string
== "STOPPING")
238 new_state
= SPEECH_RECOGNITION_STOPPING
;
240 StartPageService
* service
=
241 StartPageService::Get(Profile::FromWebUI(web_ui()));
243 service
->OnSpeechRecognitionStateChanged(new_state
);
246 } // namespace app_list