start_page_handler.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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.h" 22#include "chrome/common/extensions/extension_icon_set.h" 23#include "content/public/browser/web_contents_view.h" 24#include "content/public/browser/web_ui.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} 68 69void StartPageHandler::OnRecommendedAppsChanged() { 70 SendRecommendedApps(); 71} 72 73void StartPageHandler::SendRecommendedApps() { 74 const RecommendedApps::Apps& recommends = recommended_apps_->apps(); 75 76 base::ListValue recommended_list; 77 for (size_t i = 0; i < recommends.size(); ++i) { 78 recommended_list.Append(CreateAppInfo(recommends[i].get()).release()); 79 } 80 81 web_ui()->CallJavascriptFunction("appList.startPage.setRecommendedApps", 82 recommended_list); 83} 84 85void StartPageHandler::HandleInitialize(const base::ListValue* args) { 86 Profile* profile = Profile::FromWebUI(web_ui()); 87 StartPageService* service = StartPageService::Get(profile); 88 if (!service) 89 return; 90 91 recommended_apps_ = service->recommended_apps(); 92 recommended_apps_->AddObserver(this); 93 94 SendRecommendedApps(); 95} 96 97void StartPageHandler::HandleLaunchApp(const base::ListValue* args) { 98 std::string app_id; 99 CHECK(args->GetString(0, &app_id)); 100 101 Profile* profile = Profile::FromWebUI(web_ui()); 102 ExtensionService* service = 103 extensions::ExtensionSystem::Get(profile)->extension_service(); 104 const extensions::Extension* app = service->GetInstalledExtension(app_id); 105 if (!app) { 106 NOTREACHED(); 107 return; 108 } 109 110 AppListService* app_list_service = AppListService::Get( 111 chrome::GetHostDesktopTypeForNativeView( 112 web_ui()->GetWebContents()->GetView()->GetNativeView())); 113 scoped_ptr<AppListControllerDelegate> controller( 114 app_list_service->CreateControllerDelegate()); 115 controller->ActivateApp(profile, 116 app, 117 AppListControllerDelegate::LAUNCH_FROM_APP_LIST, 118 ui::EF_NONE); 119} 120 121} // namespace app_list 122