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