Home | History | Annotate | Download | only in webui
      1 // Copyright (c) 2012 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/downloads_ui.h"
      6 
      7 #include "base/memory/ref_counted_memory.h"
      8 #include "base/memory/singleton.h"
      9 #include "base/prefs/pref_service.h"
     10 #include "base/strings/string_piece.h"
     11 #include "base/threading/thread.h"
     12 #include "base/values.h"
     13 #include "chrome/browser/defaults.h"
     14 #include "chrome/browser/download/download_service.h"
     15 #include "chrome/browser/download/download_service_factory.h"
     16 #include "chrome/browser/profiles/profile.h"
     17 #include "chrome/browser/ui/webui/downloads_dom_handler.h"
     18 #include "chrome/browser/ui/webui/theme_source.h"
     19 #include "chrome/common/pref_names.h"
     20 #include "chrome/common/url_constants.h"
     21 #include "content/public/browser/download_manager.h"
     22 #include "content/public/browser/url_data_source.h"
     23 #include "content/public/browser/web_contents.h"
     24 #include "content/public/browser/web_ui.h"
     25 #include "content/public/browser/web_ui_data_source.h"
     26 #include "grit/browser_resources.h"
     27 #include "grit/chromium_strings.h"
     28 #include "grit/generated_resources.h"
     29 #include "grit/theme_resources.h"
     30 #include "ui/base/resource/resource_bundle.h"
     31 
     32 using content::BrowserContext;
     33 using content::DownloadManager;
     34 using content::WebContents;
     35 
     36 namespace {
     37 
     38 content::WebUIDataSource* CreateDownloadsUIHTMLSource(Profile* profile) {
     39   content::WebUIDataSource* source =
     40       content::WebUIDataSource::Create(chrome::kChromeUIDownloadsHost);
     41 
     42   source->AddLocalizedString("title", IDS_DOWNLOAD_TITLE);
     43   source->AddLocalizedString("searchbutton", IDS_DOWNLOAD_SEARCH_BUTTON);
     44   source->AddLocalizedString("searchresultsfor", IDS_DOWNLOAD_SEARCHRESULTSFOR);
     45   source->AddLocalizedString("downloads", IDS_DOWNLOAD_TITLE);
     46   source->AddLocalizedString("clear_all", IDS_DOWNLOAD_LINK_CLEAR_ALL);
     47   source->AddLocalizedString("open_downloads_folder",
     48                              IDS_DOWNLOAD_LINK_OPEN_DOWNLOADS_FOLDER);
     49 
     50   // Status.
     51   source->AddLocalizedString("status_cancelled", IDS_DOWNLOAD_TAB_CANCELLED);
     52   source->AddLocalizedString("status_removed", IDS_DOWNLOAD_FILE_REMOVED);
     53   source->AddLocalizedString("status_paused", IDS_DOWNLOAD_PROGRESS_PAUSED);
     54 
     55   // Dangerous file.
     56   source->AddLocalizedString("danger_file_desc", IDS_PROMPT_DANGEROUS_DOWNLOAD);
     57   source->AddLocalizedString("danger_url_desc",
     58                              IDS_PROMPT_MALICIOUS_DOWNLOAD_URL);
     59   source->AddLocalizedString("danger_content_desc",
     60                              IDS_PROMPT_MALICIOUS_DOWNLOAD_CONTENT);
     61   source->AddLocalizedString("danger_uncommon_desc",
     62                              IDS_PROMPT_UNCOMMON_DOWNLOAD_CONTENT);
     63   source->AddLocalizedString("danger_settings_desc",
     64                              IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS);
     65   source->AddLocalizedString("danger_save", IDS_CONFIRM_DOWNLOAD);
     66   source->AddLocalizedString("danger_restore", IDS_CONFIRM_DOWNLOAD_RESTORE);
     67   source->AddLocalizedString("danger_discard", IDS_DISCARD_DOWNLOAD);
     68 
     69   // Controls.
     70   source->AddLocalizedString("control_pause", IDS_DOWNLOAD_LINK_PAUSE);
     71   if (browser_defaults::kDownloadPageHasShowInFolder) {
     72     source->AddLocalizedString("control_showinfolder", IDS_DOWNLOAD_LINK_SHOW);
     73   }
     74   source->AddLocalizedString("control_retry", IDS_DOWNLOAD_LINK_RETRY);
     75   source->AddLocalizedString("control_cancel", IDS_DOWNLOAD_LINK_CANCEL);
     76   source->AddLocalizedString("control_resume", IDS_DOWNLOAD_LINK_RESUME);
     77   source->AddLocalizedString("control_removefromlist",
     78                              IDS_DOWNLOAD_LINK_REMOVE);
     79   source->AddLocalizedString("control_by_extension",
     80                              IDS_DOWNLOAD_BY_EXTENSION);
     81 
     82   PrefService* prefs = profile->GetPrefs();
     83   source->AddBoolean("allow_deleting_history",
     84                      prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory));
     85   source->AddBoolean("show_delete_history", !profile->IsSupervised());
     86 
     87   source->SetJsonPath("strings.js");
     88   source->AddResourcePath("downloads.css", IDR_DOWNLOADS_CSS);
     89   source->AddResourcePath("downloads.js", IDR_DOWNLOADS_JS);
     90   source->SetDefaultResource(IDR_DOWNLOADS_HTML);
     91   source->SetUseJsonJSFormatV2();
     92 
     93   return source;
     94 }
     95 
     96 }  // namespace
     97 
     98 ///////////////////////////////////////////////////////////////////////////////
     99 //
    100 // DownloadsUI
    101 //
    102 ///////////////////////////////////////////////////////////////////////////////
    103 
    104 DownloadsUI::DownloadsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
    105   Profile* profile = Profile::FromWebUI(web_ui);
    106   DownloadManager* dlm = BrowserContext::GetDownloadManager(profile);
    107 
    108   DownloadsDOMHandler* handler = new DownloadsDOMHandler(dlm);
    109   web_ui->AddMessageHandler(handler);
    110 
    111   // Set up the chrome://downloads/ source.
    112   content::WebUIDataSource* source = CreateDownloadsUIHTMLSource(profile);
    113   content::WebUIDataSource::Add(profile, source);
    114 #if defined(ENABLE_THEMES)
    115   ThemeSource* theme = new ThemeSource(profile);
    116   content::URLDataSource::Add(profile, theme);
    117 #endif
    118 }
    119 
    120 // static
    121 base::RefCountedMemory* DownloadsUI::GetFaviconResourceBytes(
    122       ui::ScaleFactor scale_factor) {
    123   return ResourceBundle::GetSharedInstance().
    124       LoadDataResourceBytesForScale(IDR_DOWNLOADS_FAVICON, scale_factor);
    125 }
    126