Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / webui / app_list / start_page_handler.cc
blobee155071addd9299305991694e193250f32a615f
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"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
12 #include "base/version.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search/hotword_service.h"
15 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
16 #include "chrome/browser/ui/app_list/app_list_service.h"
17 #include "chrome/browser/ui/app_list/recommended_apps.h"
18 #include "chrome/browser/ui/app_list/start_page_service.h"
19 #include "chrome/browser/ui/host_desktop.h"
20 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
21 #include "chrome/common/pref_names.h"
22 #include "components/omaha_query_params/omaha_query_params.h"
23 #include "content/public/browser/web_ui.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "extensions/browser/extension_system.h"
26 #include "extensions/common/constants.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/extension_icon_set.h"
29 #include "ui/app_list/app_list_switches.h"
30 #include "ui/app_list/speech_ui_model_observer.h"
31 #include "ui/events/event_constants.h"
33 namespace app_list {
35 namespace {
37 #if defined(OS_CHROMEOS)
38 const char kOldHotwordExtensionVersionString[] = "0.1.1.5023";
39 #endif
41 scoped_ptr<base::DictionaryValue> CreateAppInfo(
42 const extensions::Extension* app) {
43 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
44 dict->SetString("appId", app->id());
45 dict->SetString("textTitle", app->short_name());
46 dict->SetString("title", app->name());
48 const bool grayscale = false;
49 bool icon_exists = true;
50 GURL icon_url = extensions::ExtensionIconSource::GetIconURL(
51 app,
52 extension_misc::EXTENSION_ICON_MEDIUM,
53 ExtensionIconSet::MATCH_BIGGER,
54 grayscale,
55 &icon_exists);
56 dict->SetString("iconUrl", icon_url.spec());
58 return dict.Pass();
61 } // namespace
63 StartPageHandler::StartPageHandler()
64 : recommended_apps_(NULL),
65 extension_registry_observer_(this) {
68 StartPageHandler::~StartPageHandler() {
69 if (recommended_apps_)
70 recommended_apps_->RemoveObserver(this);
73 void StartPageHandler::RegisterMessages() {
74 web_ui()->RegisterMessageCallback(
75 "initialize",
76 base::Bind(&StartPageHandler::HandleInitialize, base::Unretained(this)));
77 web_ui()->RegisterMessageCallback(
78 "launchApp",
79 base::Bind(&StartPageHandler::HandleLaunchApp, base::Unretained(this)));
80 web_ui()->RegisterMessageCallback(
81 "speechResult",
82 base::Bind(&StartPageHandler::HandleSpeechResult,
83 base::Unretained(this)));
84 web_ui()->RegisterMessageCallback(
85 "speechSoundLevel",
86 base::Bind(&StartPageHandler::HandleSpeechSoundLevel,
87 base::Unretained(this)));
88 web_ui()->RegisterMessageCallback(
89 "setSpeechRecognitionState",
90 base::Bind(&StartPageHandler::HandleSpeechRecognition,
91 base::Unretained(this)));
94 void StartPageHandler::OnExtensionLoaded(
95 content::BrowserContext* browser_context,
96 const extensions::Extension* extension) {
97 #if defined(OS_CHROMEOS)
98 DCHECK_EQ(Profile::FromWebUI(web_ui()),
99 Profile::FromBrowserContext(browser_context));
100 if (extension->id() == extension_misc::kHotwordExtensionId)
101 OnHotwordEnabledChanged();
102 #endif
105 void StartPageHandler::OnExtensionUnloaded(
106 content::BrowserContext* browser_context,
107 const extensions::Extension* extension,
108 extensions::UnloadedExtensionInfo::Reason reason) {
109 #if defined(OS_CHROMEOS)
110 DCHECK_EQ(Profile::FromWebUI(web_ui()),
111 Profile::FromBrowserContext(browser_context));
112 if (extension->id() == extension_misc::kHotwordExtensionId)
113 OnHotwordEnabledChanged();
114 #endif
117 void StartPageHandler::OnRecommendedAppsChanged() {
118 SendRecommendedApps();
121 void StartPageHandler::SendRecommendedApps() {
122 const RecommendedApps::Apps& recommends = recommended_apps_->apps();
124 base::ListValue recommended_list;
125 for (size_t i = 0; i < recommends.size(); ++i) {
126 recommended_list.Append(CreateAppInfo(recommends[i].get()).release());
129 web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps",
130 recommended_list);
133 #if defined(OS_CHROMEOS)
134 void StartPageHandler::OnHotwordEnabledChanged() {
135 // If the hotword extension is new enough, we should use the new
136 // hotwordPrivate API to provide the feature.
137 // TODO(mukai): remove this after everything gets stable.
138 Profile* profile = Profile::FromWebUI(web_ui());
140 extensions::ExtensionRegistry* registry =
141 extensions::ExtensionRegistry::Get(profile);
142 const extensions::Extension* hotword_extension =
143 registry->GetExtensionById(extension_misc::kHotwordExtensionId,
144 extensions::ExtensionRegistry::ENABLED);
145 if (hotword_extension &&
146 hotword_extension->version()->CompareTo(
147 base::Version(kOldHotwordExtensionVersionString)) <= 0 &&
148 !HotwordService::IsExperimentalHotwordingEnabled()) {
149 StartPageService* service = StartPageService::Get(profile);
150 web_ui()->CallJavascriptFunction(
151 "appList.startPage.setHotwordEnabled",
152 base::FundamentalValue(service->HotwordEnabled()));
155 #endif
157 void StartPageHandler::HandleInitialize(const base::ListValue* args) {
158 Profile* profile = Profile::FromWebUI(web_ui());
159 StartPageService* service = StartPageService::Get(profile);
160 if (!service)
161 return;
163 service->WebUILoaded();
165 recommended_apps_ = service->recommended_apps();
166 recommended_apps_->AddObserver(this);
168 SendRecommendedApps();
170 #if defined(OS_CHROMEOS)
171 if (app_list::switches::IsVoiceSearchEnabled() &&
172 HotwordService::DoesHotwordSupportLanguage(profile)) {
173 OnHotwordEnabledChanged();
174 pref_change_registrar_.Init(profile->GetPrefs());
175 pref_change_registrar_.Add(
176 prefs::kHotwordSearchEnabled,
177 base::Bind(&StartPageHandler::OnHotwordEnabledChanged,
178 base::Unretained(this)));
180 extension_registry_observer_.Add(
181 extensions::ExtensionRegistry::Get(profile));
184 extensions::ExtensionRegistry* registry =
185 extensions::ExtensionRegistry::Get(profile);
186 const extensions::Extension* hotword_extension =
187 registry->GetExtensionById(extension_misc::kHotwordExtensionId,
188 extensions::ExtensionRegistry::ENABLED);
189 if (hotword_extension &&
190 hotword_extension->version()->CompareTo(
191 base::Version(kOldHotwordExtensionVersionString)) <= 0) {
192 web_ui()->CallJavascriptFunction(
193 "appList.startPage.setNaclArch",
194 base::StringValue(omaha_query_params::OmahaQueryParams::GetNaclArch()));
196 #endif
198 if (!app_list::switches::IsExperimentalAppListEnabled()) {
199 // If experimental hotwording is enabled, don't enable hotwording in the
200 // start page, since the hotword extension is taking care of this.
201 bool hotword_enabled = service->HotwordEnabled() &&
202 !HotwordService::IsExperimentalHotwordingEnabled();
203 web_ui()->CallJavascriptFunction(
204 "appList.startPage.onAppListShown",
205 base::FundamentalValue(hotword_enabled));
209 void StartPageHandler::HandleLaunchApp(const base::ListValue* args) {
210 std::string app_id;
211 CHECK(args->GetString(0, &app_id));
213 Profile* profile = Profile::FromWebUI(web_ui());
214 const extensions::Extension* app =
215 extensions::ExtensionRegistry::Get(profile)
216 ->GetExtensionById(app_id, extensions::ExtensionRegistry::EVERYTHING);
217 if (!app) {
218 NOTREACHED();
219 return;
222 AppListControllerDelegate* controller = AppListService::Get(
223 chrome::GetHostDesktopTypeForNativeView(
224 web_ui()->GetWebContents()->GetNativeView()))->
225 GetControllerDelegate();
226 controller->ActivateApp(profile,
227 app,
228 AppListControllerDelegate::LAUNCH_FROM_APP_LIST,
229 ui::EF_NONE);
232 void StartPageHandler::HandleSpeechResult(const base::ListValue* args) {
233 base::string16 query;
234 bool is_final = false;
235 CHECK(args->GetString(0, &query));
236 CHECK(args->GetBoolean(1, &is_final));
238 StartPageService::Get(Profile::FromWebUI(web_ui()))->OnSpeechResult(
239 query, is_final);
242 void StartPageHandler::HandleSpeechSoundLevel(const base::ListValue* args) {
243 double level;
244 CHECK(args->GetDouble(0, &level));
246 StartPageService* service =
247 StartPageService::Get(Profile::FromWebUI(web_ui()));
248 if (service)
249 service->OnSpeechSoundLevelChanged(static_cast<int16>(level));
252 void StartPageHandler::HandleSpeechRecognition(const base::ListValue* args) {
253 std::string state_string;
254 CHECK(args->GetString(0, &state_string));
256 SpeechRecognitionState new_state = SPEECH_RECOGNITION_OFF;
257 if (state_string == "READY")
258 new_state = SPEECH_RECOGNITION_READY;
259 else if (state_string == "HOTWORD_RECOGNIZING")
260 new_state = SPEECH_RECOGNITION_HOTWORD_LISTENING;
261 else if (state_string == "RECOGNIZING")
262 new_state = SPEECH_RECOGNITION_RECOGNIZING;
263 else if (state_string == "IN_SPEECH")
264 new_state = SPEECH_RECOGNITION_IN_SPEECH;
265 else if (state_string == "STOPPING")
266 new_state = SPEECH_RECOGNITION_STOPPING;
267 else if (state_string == "NETWORK_ERROR")
268 new_state = SPEECH_RECOGNITION_NETWORK_ERROR;
270 StartPageService* service =
271 StartPageService::Get(Profile::FromWebUI(web_ui()));
272 if (service)
273 service->OnSpeechRecognitionStateChanged(new_state);
276 } // namespace app_list