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.
5 #include "chrome/browser/ui/webui/downloads_ui.h"
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/downloads_util.h"
19 #include "chrome/browser/ui/webui/theme_source.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/grit/chromium_strings.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "content/public/browser/download_manager.h"
26 #include "content/public/browser/url_data_source.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_ui.h"
29 #include "content/public/browser/web_ui_data_source.h"
30 #include "grit/browser_resources.h"
31 #include "grit/theme_resources.h"
32 #include "ui/base/resource/resource_bundle.h"
34 using content::BrowserContext
;
35 using content::DownloadManager
;
36 using content::WebContents
;
40 content::WebUIDataSource
* CreateDownloadsUIHTMLSource(Profile
* profile
) {
41 content::WebUIDataSource
* source
=
42 content::WebUIDataSource::Create(chrome::kChromeUIDownloadsHost
);
44 source
->AddLocalizedString("title", IDS_DOWNLOAD_TITLE
);
45 source
->AddLocalizedString("searchResultsFor", IDS_DOWNLOAD_SEARCHRESULTSFOR
);
46 source
->AddLocalizedString("downloads", IDS_DOWNLOAD_TITLE
);
47 source
->AddLocalizedString("clearAll", IDS_DOWNLOAD_LINK_CLEAR_ALL
);
48 source
->AddLocalizedString("openDownloadsFolder",
49 IDS_DOWNLOAD_LINK_OPEN_DOWNLOADS_FOLDER
);
51 // No results message that shows instead of the downloads list.
52 source
->AddLocalizedString("noSearchResults",
53 IDS_DOWNLOAD_NO_SEARCH_RESULTS
);
56 source
->AddLocalizedString("statusCancelled", IDS_DOWNLOAD_TAB_CANCELLED
);
57 source
->AddLocalizedString("statusRemoved", IDS_DOWNLOAD_FILE_REMOVED
);
60 source
->AddLocalizedString("dangerFileDesc", IDS_PROMPT_DANGEROUS_DOWNLOAD
);
61 source
->AddLocalizedString("dangerUrlDesc",
62 IDS_PROMPT_MALICIOUS_DOWNLOAD_URL
);
63 source
->AddLocalizedString("dangerContentDesc",
64 IDS_PROMPT_MALICIOUS_DOWNLOAD_CONTENT
);
65 source
->AddLocalizedString("dangerUncommonDesc",
66 IDS_PROMPT_UNCOMMON_DOWNLOAD_CONTENT
);
67 source
->AddLocalizedString("dangerSettingsDesc",
68 IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS
);
69 source
->AddLocalizedString("dangerSave", IDS_CONFIRM_DOWNLOAD
);
70 source
->AddLocalizedString("dangerRestore", IDS_CONFIRM_DOWNLOAD_RESTORE
);
71 source
->AddLocalizedString("dangerDiscard", IDS_DISCARD_DOWNLOAD
);
74 source
->AddLocalizedString("controlPause", IDS_DOWNLOAD_LINK_PAUSE
);
75 if (browser_defaults::kDownloadPageHasShowInFolder
)
76 source
->AddLocalizedString("controlShowInFolder", IDS_DOWNLOAD_LINK_SHOW
);
77 source
->AddLocalizedString("controlCancel", IDS_DOWNLOAD_LINK_CANCEL
);
78 source
->AddLocalizedString("controlResume", IDS_DOWNLOAD_LINK_RESUME
);
79 source
->AddLocalizedString("controlRemoveFromList",
80 IDS_DOWNLOAD_LINK_REMOVE
);
81 source
->AddLocalizedString("controlByExtension",
82 IDS_DOWNLOAD_BY_EXTENSION
);
84 PrefService
* prefs
= profile
->GetPrefs();
85 source
->AddBoolean("allowDeletingHistory",
86 prefs
->GetBoolean(prefs::kAllowDeletingBrowserHistory
) &&
87 !profile
->IsSupervised());
89 source
->SetJsonPath("strings.js");
90 source
->AddResourcePath("constants.html", IDR_DOWNLOADS_CONSTANTS_HTML
);
91 source
->AddResourcePath("constants.js", IDR_DOWNLOADS_CONSTANTS_JS
);
92 source
->AddResourcePath("throttled_icon_loader.html",
93 IDR_DOWNLOADS_THROTTLED_ICON_LOADER_HTML
);
94 source
->AddResourcePath("throttled_icon_loader.js",
95 IDR_DOWNLOADS_THROTTLED_ICON_LOADER_JS
);
97 if (MdDownloadsEnabled()) {
98 source
->AddLocalizedString("search", IDS_MD_DOWNLOAD_SEARCH
);
99 source
->AddLocalizedString("controlRetry", IDS_MD_DOWNLOAD_LINK_RETRY
);
100 source
->AddLocalizedString("noDownloads", IDS_MD_DOWNLOAD_NO_DOWNLOADS
);
102 source
->AddResourcePath("action_service.html",
103 IDR_MD_DOWNLOADS_ACTION_SERVICE_HTML
);
104 source
->AddResourcePath("action_service.js",
105 IDR_MD_DOWNLOADS_ACTION_SERVICE_JS
);
106 source
->AddResourcePath("focus_row.html", IDR_MD_DOWNLOADS_FOCUS_ROW_HTML
);
107 source
->AddResourcePath("focus_row.js", IDR_MD_DOWNLOADS_FOCUS_ROW_JS
);
108 source
->AddResourcePath("item.css", IDR_MD_DOWNLOADS_ITEM_CSS
);
109 source
->AddResourcePath("item.html", IDR_MD_DOWNLOADS_ITEM_HTML
);
110 source
->AddResourcePath("item.js", IDR_MD_DOWNLOADS_ITEM_JS
);
111 source
->AddResourcePath("manager.css", IDR_MD_DOWNLOADS_MANAGER_CSS
);
112 source
->AddResourcePath("manager.html", IDR_MD_DOWNLOADS_MANAGER_HTML
);
113 source
->AddResourcePath("manager.js", IDR_MD_DOWNLOADS_MANAGER_JS
);
114 source
->AddResourcePath("shared_style.css",
115 IDR_MD_DOWNLOADS_SHARED_STYLE_CSS
);
116 source
->AddResourcePath("strings.html", IDR_MD_DOWNLOADS_STRINGS_HTML
);
117 source
->AddResourcePath("toolbar.css", IDR_MD_DOWNLOADS_TOOLBAR_CSS
);
118 source
->AddResourcePath("toolbar.html", IDR_MD_DOWNLOADS_TOOLBAR_HTML
);
119 source
->AddResourcePath("toolbar.js", IDR_MD_DOWNLOADS_TOOLBAR_JS
);
120 source
->SetDefaultResource(IDR_MD_DOWNLOADS_DOWNLOADS_HTML
);
122 source
->AddLocalizedString("searchButton", IDS_DOWNLOAD_SEARCH_BUTTON
);
123 source
->AddLocalizedString("controlRetry", IDS_DOWNLOAD_LINK_RETRY
);
124 source
->AddLocalizedString("noDownloads", IDS_DOWNLOAD_NO_DOWNLOADS
);
126 source
->AddResourcePath("item_view.js", IDR_DOWNLOADS_ITEM_VIEW_JS
);
127 source
->AddResourcePath("focus_row.js", IDR_DOWNLOADS_FOCUS_ROW_JS
);
128 source
->AddResourcePath("manager.js", IDR_DOWNLOADS_MANAGER_JS
);
129 source
->SetDefaultResource(IDR_DOWNLOADS_DOWNLOADS_HTML
);
137 ///////////////////////////////////////////////////////////////////////////////
141 ///////////////////////////////////////////////////////////////////////////////
143 DownloadsUI::DownloadsUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
144 Profile
* profile
= Profile::FromWebUI(web_ui
);
145 DownloadManager
* dlm
= BrowserContext::GetDownloadManager(profile
);
147 DownloadsDOMHandler
* handler
= new DownloadsDOMHandler(dlm
);
148 web_ui
->AddMessageHandler(handler
);
150 // Set up the chrome://downloads/ source.
151 content::WebUIDataSource
* source
= CreateDownloadsUIHTMLSource(profile
);
152 content::WebUIDataSource::Add(profile
, source
);
153 #if defined(ENABLE_THEMES)
154 ThemeSource
* theme
= new ThemeSource(profile
);
155 content::URLDataSource::Add(profile
, theme
);
160 base::RefCountedMemory
* DownloadsUI::GetFaviconResourceBytes(
161 ui::ScaleFactor scale_factor
) {
162 return ResourceBundle::GetSharedInstance().
163 LoadDataResourceBytesForScale(IDR_DOWNLOADS_FAVICON
, scale_factor
);