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 "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/app_launcher_login_handler.h"
11 #include "chrome/browser/ui/webui/metrics_handler.h"
12 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
13 #include "chrome/browser/ui/webui/ntp/app_resource_cache_factory.h"
14 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h"
15 #include "chrome/browser/ui/webui/ntp/favicon_webui_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"
27 #if defined(ENABLE_THEMES)
28 #include "chrome/browser/ui/webui/theme_handler.h"
31 class ExtensionService
;
33 using content::BrowserThread
;
35 ///////////////////////////////////////////////////////////////////////////////
38 AppLauncherPageUI::AppLauncherPageUI(content::WebUI
* web_ui
)
39 : content::WebUIController(web_ui
) {
40 web_ui
->OverrideTitle(l10n_util::GetStringUTF16(IDS_APP_LAUNCHER_TAB_TITLE
));
42 if (AppLauncherLoginHandler::ShouldShow(GetProfile()))
43 web_ui
->AddMessageHandler(new AppLauncherLoginHandler());
45 if (!GetProfile()->IsOffTheRecord()) {
46 ExtensionService
* service
=
47 extensions::ExtensionSystem::Get(GetProfile())->extension_service();
48 // We should not be launched without an ExtensionService.
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());
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
60 web_ui
->AddMessageHandler(new ThemeHandler());
63 scoped_ptr
<HTMLSource
> html_source(
64 new HTMLSource(GetProfile()->GetOriginalProfile()));
65 content::URLDataSource::Add(GetProfile(), html_source
.release());
68 AppLauncherPageUI::~AppLauncherPageUI() {
72 base::RefCountedMemory
* AppLauncherPageUI::GetFaviconResourceBytes(
73 ui::ScaleFactor scale_factor
) {
74 return ui::ResourceBundle::GetSharedInstance().
75 LoadDataResourceBytesForScale(IDR_BOOKMARK_BAR_APPS_SHORTCUT
,
79 Profile
* AppLauncherPageUI::GetProfile() const {
80 return Profile::FromWebUI(web_ui());
83 ///////////////////////////////////////////////////////////////////////////////
86 AppLauncherPageUI::HTMLSource::HTMLSource(Profile
* profile
)
90 std::string
AppLauncherPageUI::HTMLSource::GetSource() const {
91 return chrome::kChromeUIAppLauncherPageHost
;
94 void AppLauncherPageUI::HTMLSource::StartDataRequest(
95 const std::string
& path
,
96 int render_process_id
,
98 const content::URLDataSource::GotDataCallback
& callback
) {
99 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
101 NTPResourceCache
* resource
= AppResourceCacheFactory::GetForProfile(profile_
);
102 resource
->set_should_show_other_devices_menu(false);
104 content::RenderProcessHost
* render_host
=
105 content::RenderProcessHost::FromID(render_process_id
);
106 NTPResourceCache::WindowType win_type
= NTPResourceCache::GetWindowType(
107 profile_
, render_host
);
108 scoped_refptr
<base::RefCountedMemory
> html_bytes(
109 resource
->GetNewTabHTML(win_type
));
111 callback
.Run(html_bytes
.get());
114 std::string
AppLauncherPageUI::HTMLSource::GetMimeType(
115 const std::string
& resource
) const {
119 bool AppLauncherPageUI::HTMLSource::ShouldReplaceExistingSource() const {
123 bool AppLauncherPageUI::HTMLSource::ShouldAddContentSecurityPolicy() const {
127 AppLauncherPageUI::HTMLSource::~HTMLSource() {}