• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/singleton.h"
8 #include "base/string_piece.h"
9 #include "base/threading/thread.h"
10 #include "base/values.h"
11 #include "chrome/browser/defaults.h"
12 #include "chrome/browser/download/download_manager.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
15 #include "chrome/browser/ui/webui/downloads_dom_handler.h"
16 #include "chrome/common/jstemplate_builder.h"
17 #include "chrome/common/url_constants.h"
18 #include "content/browser/browser_thread.h"
19 #include "content/browser/tab_contents/tab_contents.h"
20 #include "grit/browser_resources.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 namespace {
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 //
30 // DownloadsHTMLSource
31 //
32 ///////////////////////////////////////////////////////////////////////////////
33 
34 class DownloadsUIHTMLSource : public ChromeURLDataManager::DataSource {
35  public:
36   DownloadsUIHTMLSource();
37 
38   // Called when the network layer has requested a resource underneath
39   // the path we registered.
40   virtual void StartDataRequest(const std::string& path,
41                                 bool is_incognito,
42                                 int request_id);
GetMimeType(const std::string &) const43   virtual std::string GetMimeType(const std::string&) const {
44     return "text/html";
45   }
46 
47  private:
~DownloadsUIHTMLSource()48   ~DownloadsUIHTMLSource() {}
49 
50   DISALLOW_COPY_AND_ASSIGN(DownloadsUIHTMLSource);
51 };
52 
DownloadsUIHTMLSource()53 DownloadsUIHTMLSource::DownloadsUIHTMLSource()
54     : DataSource(chrome::kChromeUIDownloadsHost, MessageLoop::current()) {
55 }
56 
StartDataRequest(const std::string & path,bool is_incognito,int request_id)57 void DownloadsUIHTMLSource::StartDataRequest(const std::string& path,
58                                              bool is_incognito,
59                                              int request_id) {
60   DictionaryValue localized_strings;
61   localized_strings.SetString("title",
62       l10n_util::GetStringUTF16(IDS_DOWNLOAD_TITLE));
63   localized_strings.SetString("searchbutton",
64       l10n_util::GetStringUTF16(IDS_DOWNLOAD_SEARCH_BUTTON));
65   localized_strings.SetString("no_results",
66       l10n_util::GetStringUTF16(IDS_DOWNLOAD_SEARCH_BUTTON));
67   localized_strings.SetString("searchresultsfor",
68       l10n_util::GetStringUTF16(IDS_DOWNLOAD_SEARCHRESULTSFOR));
69   localized_strings.SetString("downloads",
70       l10n_util::GetStringUTF16(IDS_DOWNLOAD_TITLE));
71   localized_strings.SetString("clear_all",
72       l10n_util::GetStringUTF16(IDS_DOWNLOAD_LINK_CLEAR_ALL));
73 
74   // Status.
75   localized_strings.SetString("status_cancelled",
76       l10n_util::GetStringUTF16(IDS_DOWNLOAD_TAB_CANCELED));
77   localized_strings.SetString("status_paused",
78       l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED));
79   localized_strings.SetString("status_interrupted",
80       l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_INTERRUPTED));
81 
82   // Dangerous file.
83   localized_strings.SetString("danger_file_desc",
84       l10n_util::GetStringUTF16(IDS_PROMPT_DANGEROUS_DOWNLOAD));
85   localized_strings.SetString("danger_url_desc",
86       l10n_util::GetStringUTF16(IDS_PROMPT_UNSAFE_DOWNLOAD_URL));
87   localized_strings.SetString("danger_save",
88       l10n_util::GetStringUTF16(IDS_SAVE_DOWNLOAD));
89   localized_strings.SetString("danger_discard",
90       l10n_util::GetStringUTF16(IDS_DISCARD_DOWNLOAD));
91 
92   // Controls.
93   localized_strings.SetString("control_pause",
94       l10n_util::GetStringUTF16(IDS_DOWNLOAD_LINK_PAUSE));
95   if (browser_defaults::kDownloadPageHasShowInFolder) {
96     localized_strings.SetString("control_showinfolder",
97         l10n_util::GetStringUTF16(IDS_DOWNLOAD_LINK_SHOW));
98   }
99   localized_strings.SetString("control_retry",
100       l10n_util::GetStringUTF16(IDS_DOWNLOAD_LINK_RETRY));
101   localized_strings.SetString("control_cancel",
102       l10n_util::GetStringUTF16(IDS_DOWNLOAD_LINK_CANCEL));
103   localized_strings.SetString("control_resume",
104       l10n_util::GetStringUTF16(IDS_DOWNLOAD_LINK_RESUME));
105   localized_strings.SetString("control_removefromlist",
106       l10n_util::GetStringUTF16(IDS_DOWNLOAD_LINK_REMOVE));
107 
108   SetFontAndTextDirection(&localized_strings);
109 
110   static const base::StringPiece downloads_html(
111       ResourceBundle::GetSharedInstance().GetRawDataResource(
112           IDR_DOWNLOADS_HTML));
113   const std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
114       downloads_html, &localized_strings);
115 
116   scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
117   html_bytes->data.resize(full_html.size());
118   std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
119 
120   SendResponse(request_id, html_bytes);
121 }
122 
123 }  // namespace
124 
125 ///////////////////////////////////////////////////////////////////////////////
126 //
127 // DownloadsUI
128 //
129 ///////////////////////////////////////////////////////////////////////////////
130 
DownloadsUI(TabContents * contents)131 DownloadsUI::DownloadsUI(TabContents* contents) : WebUI(contents) {
132   DownloadManager* dlm = GetProfile()->GetDownloadManager();
133 
134   DownloadsDOMHandler* handler = new DownloadsDOMHandler(dlm);
135   AddMessageHandler(handler);
136   handler->Attach(this);
137   handler->Init();
138 
139   DownloadsUIHTMLSource* html_source = new DownloadsUIHTMLSource();
140 
141   // Set up the chrome://downloads/ source.
142   contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source);
143 }
144 
145 // static
GetFaviconResourceBytes()146 RefCountedMemory* DownloadsUI::GetFaviconResourceBytes() {
147   return ResourceBundle::GetSharedInstance().
148       LoadDataResourceBytes(IDR_DOWNLOADS_FAVICON);
149 }
150