Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / app_launcher_page_ui.cc
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 "content/public/browser/browser_thread.h"
19 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/web_ui.h"
21 #include "grit/generated_resources.h"
22 #include "grit/theme_resources.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
25
26 #if defined(ENABLE_THEMES)
27 #include "chrome/browser/ui/webui/theme_handler.h"
28 #endif
29
30 class ExtensionService;
31
32 using content::BrowserThread;
33
34 ///////////////////////////////////////////////////////////////////////////////
35 // AppLauncherPageUI
36
37 AppLauncherPageUI::AppLauncherPageUI(content::WebUI* web_ui)
38     : content::WebUIController(web_ui) {
39   web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_APP_LAUNCHER_TAB_TITLE));
40
41   if (NTPLoginHandler::ShouldShow(GetProfile()))
42     web_ui->AddMessageHandler(new NTPLoginHandler());
43
44   if (!GetProfile()->IsOffTheRecord()) {
45     ExtensionService* service = GetProfile()->GetExtensionService();
46     // We should not be launched without an ExtensionService.
47     DCHECK(service);
48     web_ui->AddMessageHandler(new AppLauncherHandler(service));
49     web_ui->AddMessageHandler(new CoreAppLauncherHandler());
50     web_ui->AddMessageHandler(new FaviconWebUIHandler());
51     web_ui->AddMessageHandler(new MetricsHandler());
52   }
53
54 #if defined(ENABLE_THEMES)
55   // The theme handler can require some CPU, so do it after hooking up the most
56   // visited handler. This allows the DB query for the new tab thumbs to happen
57   // earlier.
58   web_ui->AddMessageHandler(new ThemeHandler());
59 #endif
60
61   scoped_ptr<HTMLSource> html_source(
62       new HTMLSource(GetProfile()->GetOriginalProfile()));
63   content::URLDataSource::Add(GetProfile(), html_source.release());
64 }
65
66 AppLauncherPageUI::~AppLauncherPageUI() {
67 }
68
69 // static
70 base::RefCountedMemory* AppLauncherPageUI::GetFaviconResourceBytes(
71     ui::ScaleFactor scale_factor) {
72   return ui::ResourceBundle::GetSharedInstance().
73       LoadDataResourceBytesForScale(IDR_BOOKMARK_BAR_APPS_SHORTCUT,
74                                     scale_factor);
75 }
76
77 Profile* AppLauncherPageUI::GetProfile() const {
78   return Profile::FromWebUI(web_ui());
79 }
80
81 ///////////////////////////////////////////////////////////////////////////////
82 // HTMLSource
83
84 AppLauncherPageUI::HTMLSource::HTMLSource(Profile* profile)
85     : profile_(profile) {
86 }
87
88 std::string AppLauncherPageUI::HTMLSource::GetSource() const {
89   return chrome::kChromeUIAppLauncherPageHost;
90 }
91
92 void AppLauncherPageUI::HTMLSource::StartDataRequest(
93     const std::string& path,
94     int render_process_id,
95     int render_frame_id,
96     const content::URLDataSource::GotDataCallback& callback) {
97   DCHECK_CURRENTLY_ON(BrowserThread::UI);
98
99   NTPResourceCache* resource = AppResourceCacheFactory::GetForProfile(profile_);
100   resource->set_should_show_most_visited_page(false);
101   resource->set_should_show_other_devices_menu(false);
102   resource->set_should_show_recently_closed_menu(false);
103
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));
110
111   callback.Run(html_bytes.get());
112 }
113
114 std::string AppLauncherPageUI::HTMLSource::GetMimeType(
115     const std::string& resource) const {
116   return "text/html";
117 }
118
119 bool AppLauncherPageUI::HTMLSource::ShouldReplaceExistingSource() const {
120   return false;
121 }
122
123 bool AppLauncherPageUI::HTMLSource::ShouldAddContentSecurityPolicy() const {
124   return false;
125 }
126
127 AppLauncherPageUI::HTMLSource::~HTMLSource() {}