Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / webui / app_list / start_page_handler.cc
blobc93dab21c51e98841359d841ff8384d02523d7a0
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 "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/profiles/profile.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/extensions/extension_icon_set.h"
22 #include "content/public/browser/web_contents_view.h"
23 #include "content/public/browser/web_ui.h"
24 #include "extensions/common/extension.h"
25 #include "ui/app_list/speech_ui_model_observer.h"
26 #include "ui/events/event_constants.h"
28 namespace app_list {
30 namespace {
32 scoped_ptr<base::DictionaryValue> CreateAppInfo(
33 const extensions::Extension* app) {
34 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
35 dict->SetString("appId", app->id());
36 dict->SetString("textTitle", app->short_name());
37 dict->SetString("title", app->name());
39 const bool grayscale = false;
40 bool icon_exists = true;
41 GURL icon_url = extensions::ExtensionIconSource::GetIconURL(
42 app,
43 extension_misc::EXTENSION_ICON_MEDIUM,
44 ExtensionIconSet::MATCH_BIGGER,
45 grayscale,
46 &icon_exists);
47 dict->SetString("iconUrl", icon_url.spec());
49 return dict.Pass();
52 } // namespace
54 StartPageHandler::StartPageHandler() : recommended_apps_(NULL) {}
56 StartPageHandler::~StartPageHandler() {
57 if (recommended_apps_)
58 recommended_apps_->RemoveObserver(this);
61 void StartPageHandler::RegisterMessages() {
62 web_ui()->RegisterMessageCallback(
63 "initialize",
64 base::Bind(&StartPageHandler::HandleInitialize, base::Unretained(this)));
65 web_ui()->RegisterMessageCallback(
66 "launchApp",
67 base::Bind(&StartPageHandler::HandleLaunchApp, base::Unretained(this)));
68 web_ui()->RegisterMessageCallback(
69 "speechResult",
70 base::Bind(&StartPageHandler::HandleSpeechResult,
71 base::Unretained(this)));
72 web_ui()->RegisterMessageCallback(
73 "speechSoundLevel",
74 base::Bind(&StartPageHandler::HandleSpeechSoundLevel,
75 base::Unretained(this)));
76 web_ui()->RegisterMessageCallback(
77 "setSpeechRecognitionState",
78 base::Bind(&StartPageHandler::HandleSpeechRecognition,
79 base::Unretained(this)));
82 void StartPageHandler::OnRecommendedAppsChanged() {
83 SendRecommendedApps();
86 void StartPageHandler::SendRecommendedApps() {
87 const RecommendedApps::Apps& recommends = recommended_apps_->apps();
89 base::ListValue recommended_list;
90 for (size_t i = 0; i < recommends.size(); ++i) {
91 recommended_list.Append(CreateAppInfo(recommends[i].get()).release());
94 web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps",
95 recommended_list);
98 void StartPageHandler::HandleInitialize(const base::ListValue* args) {
99 Profile* profile = Profile::FromWebUI(web_ui());
100 StartPageService* service = StartPageService::Get(profile);
101 if (!service)
102 return;
104 recommended_apps_ = service->recommended_apps();
105 recommended_apps_->AddObserver(this);
107 SendRecommendedApps();
110 void StartPageHandler::HandleLaunchApp(const base::ListValue* args) {
111 std::string app_id;
112 CHECK(args->GetString(0, &app_id));
114 Profile* profile = Profile::FromWebUI(web_ui());
115 ExtensionService* service =
116 extensions::ExtensionSystem::Get(profile)->extension_service();
117 const extensions::Extension* app = service->GetInstalledExtension(app_id);
118 if (!app) {
119 NOTREACHED();
120 return;
123 AppListControllerDelegate* controller = AppListService::Get(
124 chrome::GetHostDesktopTypeForNativeView(
125 web_ui()->GetWebContents()->GetView()->GetNativeView()))
126 ->GetControllerDelegate();
127 controller->ActivateApp(profile,
128 app,
129 AppListControllerDelegate::LAUNCH_FROM_APP_LIST,
130 ui::EF_NONE);
133 void StartPageHandler::HandleSpeechResult(const base::ListValue* args) {
134 base::string16 query;
135 bool is_final = false;
136 CHECK(args->GetString(0, &query));
137 CHECK(args->GetBoolean(1, &is_final));
139 StartPageService::Get(Profile::FromWebUI(web_ui()))->OnSpeechResult(
140 query, is_final);
143 void StartPageHandler::HandleSpeechSoundLevel(const base::ListValue* args) {
144 double level;
145 CHECK(args->GetDouble(0, &level));
147 StartPageService* service =
148 StartPageService::Get(Profile::FromWebUI(web_ui()));
149 service->OnSpeechSoundLevelChanged(static_cast<int16>(level));
152 void StartPageHandler::HandleSpeechRecognition(const base::ListValue* args) {
153 std::string state_string;
154 CHECK(args->GetString(0, &state_string));
156 SpeechRecognitionState new_state = SPEECH_RECOGNITION_NOT_STARTED;
157 if (state_string == "on")
158 new_state = SPEECH_RECOGNITION_ON;
159 else if (state_string == "in-speech")
160 new_state = SPEECH_RECOGNITION_IN_SPEECH;
162 StartPageService* service =
163 StartPageService::Get(Profile::FromWebUI(web_ui()));
164 service->OnSpeechRecognitionStateChanged(new_state);
167 } // namespace app_list