Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / app_launcher_page_ui.cc
blob84dde771260ab10624906e83349bc9376224039b
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.
5 #include "chrome/browser/ui/webui/app_launcher_page_ui.h"
7 #include <string>
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/metrics/histogram.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/webui/app_launcher_login_handler.h"
13 #include "chrome/browser/ui/webui/metrics_handler.h"
14 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
15 #include "chrome/browser/ui/webui/ntp/app_resource_cache_factory.h"
16 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h"
17 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h"
18 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
19 #include "chrome/common/url_constants.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/web_ui.h"
24 #include "extensions/browser/extension_system.h"
25 #include "grit/theme_resources.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
29 #if defined(ENABLE_THEMES)
30 #include "chrome/browser/ui/webui/theme_handler.h"
31 #endif
33 class ExtensionService;
35 using content::BrowserThread;
37 ///////////////////////////////////////////////////////////////////////////////
38 // AppLauncherPageUI
40 AppLauncherPageUI::AppLauncherPageUI(content::WebUI* web_ui)
41 : content::WebUIController(web_ui) {
42 web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_APP_LAUNCHER_TAB_TITLE));
44 if (!GetProfile()->IsOffTheRecord()) {
45 ExtensionService* service =
46 extensions::ExtensionSystem::Get(GetProfile())->extension_service();
47 // We should not be launched without an ExtensionService.
48 DCHECK(service);
49 web_ui->AddMessageHandler(new AppLauncherHandler(service));
50 web_ui->AddMessageHandler(new CoreAppLauncherHandler());
51 web_ui->AddMessageHandler(new FaviconWebUIHandler());
52 web_ui->AddMessageHandler(new MetricsHandler());
55 #if defined(ENABLE_THEMES)
56 // The theme handler can require some CPU, so do it after hooking up the most
57 // visited handler. This allows the DB query for the new tab thumbs to happen
58 // earlier.
59 web_ui->AddMessageHandler(new ThemeHandler());
60 #endif
62 scoped_ptr<HTMLSource> html_source(
63 new HTMLSource(GetProfile()->GetOriginalProfile()));
64 content::URLDataSource::Add(GetProfile(), html_source.release());
67 AppLauncherPageUI::~AppLauncherPageUI() {
70 // static
71 base::RefCountedMemory* AppLauncherPageUI::GetFaviconResourceBytes(
72 ui::ScaleFactor scale_factor) {
73 return ui::ResourceBundle::GetSharedInstance().
74 LoadDataResourceBytesForScale(IDR_BOOKMARK_BAR_APPS_SHORTCUT,
75 scale_factor);
78 bool AppLauncherPageUI::OverrideHandleWebUIMessage(
79 const GURL& source_url,
80 const std::string& message,
81 const base::ListValue& args) {
82 if (message == "getApps" &&
83 AppLauncherLoginHandler::ShouldShow(GetProfile())) {
84 web_ui()->AddMessageHandler(new AppLauncherLoginHandler());
86 return false;
90 Profile* AppLauncherPageUI::GetProfile() const {
91 return Profile::FromWebUI(web_ui());
94 ///////////////////////////////////////////////////////////////////////////////
95 // HTMLSource
97 AppLauncherPageUI::HTMLSource::HTMLSource(Profile* profile)
98 : profile_(profile) {
101 std::string AppLauncherPageUI::HTMLSource::GetSource() const {
102 return chrome::kChromeUIAppLauncherPageHost;
105 void AppLauncherPageUI::HTMLSource::StartDataRequest(
106 const std::string& path,
107 int render_process_id,
108 int render_frame_id,
109 const content::URLDataSource::GotDataCallback& callback) {
110 DCHECK_CURRENTLY_ON(BrowserThread::UI);
112 NTPResourceCache* resource = AppResourceCacheFactory::GetForProfile(profile_);
113 resource->set_should_show_other_devices_menu(false);
115 content::RenderProcessHost* render_host =
116 content::RenderProcessHost::FromID(render_process_id);
117 NTPResourceCache::WindowType win_type = NTPResourceCache::GetWindowType(
118 profile_, render_host);
119 scoped_refptr<base::RefCountedMemory> html_bytes(
120 resource->GetNewTabHTML(win_type));
122 callback.Run(html_bytes.get());
125 std::string AppLauncherPageUI::HTMLSource::GetMimeType(
126 const std::string& resource) const {
127 return "text/html";
130 bool AppLauncherPageUI::HTMLSource::ShouldReplaceExistingSource() const {
131 return false;
134 bool AppLauncherPageUI::HTMLSource::ShouldAddContentSecurityPolicy() const {
135 return false;
138 AppLauncherPageUI::HTMLSource::~HTMLSource() {}