start_page_handler.cc revision f2477e01787aa58f445919b809d89e252beef54f
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/events/event_constants.h"
26
27namespace app_list {
28
29namespace {
30
31scoped_ptr<base::DictionaryValue> CreateAppInfo(
32    const extensions::Extension* app) {
33  scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
34  dict->SetString("appId", app->id());
35  dict->SetString("textTitle", app->short_name());
36  dict->SetString("title", app->name());
37
38  const bool grayscale = false;
39  bool icon_exists = true;
40  GURL icon_url = extensions::ExtensionIconSource::GetIconURL(
41      app,
42      extension_misc::EXTENSION_ICON_MEDIUM,
43      ExtensionIconSet::MATCH_BIGGER,
44      grayscale,
45      &icon_exists);
46  dict->SetString("iconUrl", icon_url.spec());
47
48  return dict.Pass();
49}
50
51}  // namespace
52
53StartPageHandler::StartPageHandler() : recommended_apps_(NULL) {}
54
55StartPageHandler::~StartPageHandler() {
56  if (recommended_apps_)
57    recommended_apps_->RemoveObserver(this);
58}
59
60void StartPageHandler::RegisterMessages() {
61  web_ui()->RegisterMessageCallback(
62      "initialize",
63      base::Bind(&StartPageHandler::HandleInitialize, base::Unretained(this)));
64  web_ui()->RegisterMessageCallback(
65      "launchApp",
66      base::Bind(&StartPageHandler::HandleLaunchApp, base::Unretained(this)));
67  web_ui()->RegisterMessageCallback(
68      "search",
69      base::Bind(&StartPageHandler::HandleSearch, base::Unretained(this)));
70  web_ui()->RegisterMessageCallback(
71      "setSpeechRecognitionState",
72      base::Bind(&StartPageHandler::HandleSpeechRecognition,
73                 base::Unretained(this)));
74}
75
76void StartPageHandler::OnRecommendedAppsChanged() {
77  SendRecommendedApps();
78}
79
80void StartPageHandler::SendRecommendedApps() {
81  const RecommendedApps::Apps& recommends = recommended_apps_->apps();
82
83  base::ListValue recommended_list;
84  for (size_t i = 0; i < recommends.size(); ++i) {
85    recommended_list.Append(CreateAppInfo(recommends[i].get()).release());
86  }
87
88  web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps",
89                                   recommended_list);
90}
91
92void StartPageHandler::HandleInitialize(const base::ListValue* args) {
93  Profile* profile = Profile::FromWebUI(web_ui());
94  StartPageService* service = StartPageService::Get(profile);
95  if (!service)
96    return;
97
98  recommended_apps_ = service->recommended_apps();
99  recommended_apps_->AddObserver(this);
100
101  SendRecommendedApps();
102}
103
104void StartPageHandler::HandleLaunchApp(const base::ListValue* args) {
105  std::string app_id;
106  CHECK(args->GetString(0, &app_id));
107
108  Profile* profile = Profile::FromWebUI(web_ui());
109  ExtensionService* service =
110      extensions::ExtensionSystem::Get(profile)->extension_service();
111  const extensions::Extension* app = service->GetInstalledExtension(app_id);
112  if (!app) {
113    NOTREACHED();
114    return;
115  }
116
117  AppListControllerDelegate* controller = AppListService::Get(
118      chrome::GetHostDesktopTypeForNativeView(
119          web_ui()->GetWebContents()->GetView()->GetNativeView()))
120      ->GetControllerDelegate();
121  controller->ActivateApp(profile,
122                          app,
123                          AppListControllerDelegate::LAUNCH_FROM_APP_LIST,
124                          ui::EF_NONE);
125}
126
127void StartPageHandler::HandleSearch(const base::ListValue* args) {
128  base::string16 query;
129  CHECK(args->GetString(0, &query));
130
131  StartPageService::Get(Profile::FromWebUI(web_ui()))->OnSearch(query);
132}
133
134void StartPageHandler::HandleSpeechRecognition(const base::ListValue* args) {
135  bool recognizing;
136  CHECK(args->GetBoolean(0, &recognizing));
137
138  StartPageService* service =
139      StartPageService::Get(Profile::FromWebUI(web_ui()));
140  service->OnSpeechRecognitionStateChanged(recognizing);
141}
142
143}  // namespace app_list
144