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/theme_source.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/chromium_strings.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "content/public/browser/download_manager.h"
24 #include "content/public/browser/url_data_source.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_ui.h"
27 #include "content/public/browser/web_ui_data_source.h"
28 #include "grit/browser_resources.h"
29 #include "grit/theme_resources.h"
30 #include "ui/base/resource/resource_bundle.h"
32 using content::BrowserContext
;
33 using content::DownloadManager
;
34 using content::WebContents
;
38 content::WebUIDataSource
* CreateDownloadsUIHTMLSource(Profile
* profile
) {
39 content::WebUIDataSource
* source
=
40 content::WebUIDataSource::Create(chrome::kChromeUIDownloadsHost
);
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
);
50 // No results/downloads messages that show instead of the downloads list.
51 source
->AddLocalizedString("no_downloads", IDS_DOWNLOAD_NO_DOWNLOADS
);
52 source
->AddLocalizedString("no_search_results",
53 IDS_DOWNLOAD_NO_SEARCH_RESULTS
);
56 source
->AddLocalizedString("status_cancelled", IDS_DOWNLOAD_TAB_CANCELLED
);
57 source
->AddLocalizedString("status_removed", IDS_DOWNLOAD_FILE_REMOVED
);
60 source
->AddLocalizedString("danger_file_desc", IDS_PROMPT_DANGEROUS_DOWNLOAD
);
61 source
->AddLocalizedString("danger_url_desc",
62 IDS_PROMPT_MALICIOUS_DOWNLOAD_URL
);
63 source
->AddLocalizedString("danger_content_desc",
64 IDS_PROMPT_MALICIOUS_DOWNLOAD_CONTENT
);
65 source
->AddLocalizedString("danger_uncommon_desc",
66 IDS_PROMPT_UNCOMMON_DOWNLOAD_CONTENT
);
67 source
->AddLocalizedString("danger_settings_desc",
68 IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS
);
69 source
->AddLocalizedString("danger_save", IDS_CONFIRM_DOWNLOAD
);
70 source
->AddLocalizedString("danger_restore", IDS_CONFIRM_DOWNLOAD_RESTORE
);
71 source
->AddLocalizedString("danger_discard", IDS_DISCARD_DOWNLOAD
);
74 source
->AddLocalizedString("control_pause", IDS_DOWNLOAD_LINK_PAUSE
);
75 if (browser_defaults::kDownloadPageHasShowInFolder
) {
76 source
->AddLocalizedString("control_showinfolder", IDS_DOWNLOAD_LINK_SHOW
);
78 source
->AddLocalizedString("control_retry", IDS_DOWNLOAD_LINK_RETRY
);
79 source
->AddLocalizedString("control_cancel", IDS_DOWNLOAD_LINK_CANCEL
);
80 source
->AddLocalizedString("control_resume", IDS_DOWNLOAD_LINK_RESUME
);
81 source
->AddLocalizedString("control_removefromlist",
82 IDS_DOWNLOAD_LINK_REMOVE
);
83 source
->AddLocalizedString("control_by_extension",
84 IDS_DOWNLOAD_BY_EXTENSION
);
86 PrefService
* prefs
= profile
->GetPrefs();
87 source
->AddBoolean("allow_deleting_history",
88 prefs
->GetBoolean(prefs::kAllowDeletingBrowserHistory
) &&
89 !profile
->IsSupervised());
91 source
->SetJsonPath("strings.js");
92 source
->AddResourcePath("downloads.css", IDR_DOWNLOADS_CSS
);
93 source
->AddResourcePath("item.js", IDR_DOWNLOAD_ITEM_JS
);
94 source
->AddResourcePath("item_view.js", IDR_DOWNLOAD_ITEM_VIEW_JS
);
95 source
->AddResourcePath("focus_row.js", IDR_DOWNLOAD_FOCUS_ROW_JS
);
96 source
->AddResourcePath("manager.js", IDR_DOWNLOAD_MANAGER_JS
);
97 source
->SetDefaultResource(IDR_DOWNLOADS_HTML
);
104 ///////////////////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////////////////
110 DownloadsUI::DownloadsUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
111 Profile
* profile
= Profile::FromWebUI(web_ui
);
112 DownloadManager
* dlm
= BrowserContext::GetDownloadManager(profile
);
114 DownloadsDOMHandler
* handler
= new DownloadsDOMHandler(dlm
);
115 web_ui
->AddMessageHandler(handler
);
117 // Set up the chrome://downloads/ source.
118 content::WebUIDataSource
* source
= CreateDownloadsUIHTMLSource(profile
);
119 content::WebUIDataSource::Add(profile
, source
);
120 #if defined(ENABLE_THEMES)
121 ThemeSource
* theme
= new ThemeSource(profile
);
122 content::URLDataSource::Add(profile
, theme
);
127 base::RefCountedMemory
* DownloadsUI::GetFaviconResourceBytes(
128 ui::ScaleFactor scale_factor
) {
129 return ResourceBundle::GetSharedInstance().
130 LoadDataResourceBytesForScale(IDR_DOWNLOADS_FAVICON
, scale_factor
);