start_page_handler.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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.
4
5#include "chrome/browser/ui/webui/app_list/start_page_handler.h"
6
7#include <string>
8
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"
27
28namespace app_list {
29
30namespace {
31
32scoped_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());
38
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());
48
49  return dict.Pass();
50}
51
52}  // namespace
53
54StartPageHandler::StartPageHandler() : recommended_apps_(NULL) {}
55
56StartPageHandler::~StartPageHandler() {
57  if (recommended_apps_)
58    recommended_apps_->RemoveObserver(this);
59}
60
61void 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)));
80}
81
82void StartPageHandler::OnRecommendedAppsChanged() {
83  SendRecommendedApps();
84}
85
86void StartPageHandler::SendRecommendedApps() {
87  const RecommendedApps::Apps& recommends = recommended_apps_->apps();
88
89  base::ListValue recommended_list;
90  for (size_t i = 0; i < recommends.size(); ++i) {
91    recommended_list.Append(CreateAppInfo(recommends[i].get()).release());
92  }
93
94  web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps",
95                                   recommended_list);
96}
97
98void StartPageHandler::HandleInitialize(const base::ListValue* args) {
99  Profile* profile = Profile::FromWebUI(web_ui());
100  StartPageService* service = StartPageService::Get(profile);
101  if (!service)
102    return;
103
104  recommended_apps_ = service->recommended_apps();
105  recommended_apps_->AddObserver(this);
106
107  SendRecommendedApps();
108}
109
110void StartPageHandler::HandleLaunchApp(const base::ListValue* args) {
111  std::string app_id;
112  CHECK(args->GetString(0, &app_id));
113
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;
121  }
122
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);
131}
132
133void 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));
138
139  StartPageService::Get(Profile::FromWebUI(web_ui()))->OnSpeechResult(
140      query, is_final);
141}
142
143void StartPageHandler::HandleSpeechSoundLevel(const base::ListValue* args) {
144  double level;
145  CHECK(args->GetDouble(0, &level));
146
147  StartPageService* service =
148      StartPageService::Get(Profile::FromWebUI(web_ui()));
149  service->OnSpeechSoundLevelChanged(static_cast<int16>(level));
150}
151
152void StartPageHandler::HandleSpeechRecognition(const base::ListValue* args) {
153  std::string state_string;
154  CHECK(args->GetString(0, &state_string));
155
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;
161
162  StartPageService* service =
163      StartPageService::Get(Profile::FromWebUI(web_ui()));
164  service->OnSpeechRecognitionStateChanged(new_state);
165}
166
167}  // namespace app_list
168