1// Copyright (c) 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_launcher_page_ui.h" 6 7#include "base/memory/ref_counted_memory.h" 8#include "base/metrics/histogram.h" 9#include "chrome/browser/profiles/profile.h" 10#include "chrome/browser/ui/webui/metrics_handler.h" 11#include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" 12#include "chrome/browser/ui/webui/ntp/app_resource_cache_factory.h" 13#include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" 14#include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" 15#include "chrome/browser/ui/webui/ntp/ntp_login_handler.h" 16#include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" 17#include "chrome/common/url_constants.h" 18#include "chrome/grit/generated_resources.h" 19#include "content/public/browser/browser_thread.h" 20#include "content/public/browser/render_process_host.h" 21#include "content/public/browser/web_ui.h" 22#include "extensions/browser/extension_system.h" 23#include "grit/theme_resources.h" 24#include "ui/base/l10n/l10n_util.h" 25#include "ui/base/resource/resource_bundle.h" 26 27#if defined(ENABLE_THEMES) 28#include "chrome/browser/ui/webui/theme_handler.h" 29#endif 30 31class ExtensionService; 32 33using content::BrowserThread; 34 35/////////////////////////////////////////////////////////////////////////////// 36// AppLauncherPageUI 37 38AppLauncherPageUI::AppLauncherPageUI(content::WebUI* web_ui) 39 : content::WebUIController(web_ui) { 40 web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_APP_LAUNCHER_TAB_TITLE)); 41 42 if (NTPLoginHandler::ShouldShow(GetProfile())) 43 web_ui->AddMessageHandler(new NTPLoginHandler()); 44 45 if (!GetProfile()->IsOffTheRecord()) { 46 ExtensionService* service = 47 extensions::ExtensionSystem::Get(GetProfile())->extension_service(); 48 // We should not be launched without an ExtensionService. 49 DCHECK(service); 50 web_ui->AddMessageHandler(new AppLauncherHandler(service)); 51 web_ui->AddMessageHandler(new CoreAppLauncherHandler()); 52 web_ui->AddMessageHandler(new FaviconWebUIHandler()); 53 web_ui->AddMessageHandler(new MetricsHandler()); 54 } 55 56#if defined(ENABLE_THEMES) 57 // The theme handler can require some CPU, so do it after hooking up the most 58 // visited handler. This allows the DB query for the new tab thumbs to happen 59 // earlier. 60 web_ui->AddMessageHandler(new ThemeHandler()); 61#endif 62 63 scoped_ptr<HTMLSource> html_source( 64 new HTMLSource(GetProfile()->GetOriginalProfile())); 65 content::URLDataSource::Add(GetProfile(), html_source.release()); 66} 67 68AppLauncherPageUI::~AppLauncherPageUI() { 69} 70 71// static 72base::RefCountedMemory* AppLauncherPageUI::GetFaviconResourceBytes( 73 ui::ScaleFactor scale_factor) { 74 return ui::ResourceBundle::GetSharedInstance(). 75 LoadDataResourceBytesForScale(IDR_BOOKMARK_BAR_APPS_SHORTCUT, 76 scale_factor); 77} 78 79Profile* AppLauncherPageUI::GetProfile() const { 80 return Profile::FromWebUI(web_ui()); 81} 82 83/////////////////////////////////////////////////////////////////////////////// 84// HTMLSource 85 86AppLauncherPageUI::HTMLSource::HTMLSource(Profile* profile) 87 : profile_(profile) { 88} 89 90std::string AppLauncherPageUI::HTMLSource::GetSource() const { 91 return chrome::kChromeUIAppLauncherPageHost; 92} 93 94void AppLauncherPageUI::HTMLSource::StartDataRequest( 95 const std::string& path, 96 int render_process_id, 97 int render_frame_id, 98 const content::URLDataSource::GotDataCallback& callback) { 99 DCHECK_CURRENTLY_ON(BrowserThread::UI); 100 101 NTPResourceCache* resource = AppResourceCacheFactory::GetForProfile(profile_); 102 resource->set_should_show_most_visited_page(false); 103 resource->set_should_show_other_devices_menu(false); 104 resource->set_should_show_recently_closed_menu(false); 105 106 content::RenderProcessHost* render_host = 107 content::RenderProcessHost::FromID(render_process_id); 108 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType( 109 profile_, render_host); 110 scoped_refptr<base::RefCountedMemory> html_bytes( 111 resource->GetNewTabHTML(win_type)); 112 113 callback.Run(html_bytes.get()); 114} 115 116std::string AppLauncherPageUI::HTMLSource::GetMimeType( 117 const std::string& resource) const { 118 return "text/html"; 119} 120 121bool AppLauncherPageUI::HTMLSource::ShouldReplaceExistingSource() const { 122 return false; 123} 124 125bool AppLauncherPageUI::HTMLSource::ShouldAddContentSecurityPolicy() const { 126 return false; 127} 128 129AppLauncherPageUI::HTMLSource::~HTMLSource() {} 130