Add ability to gather metrics to BubbleManager.
[chromium-blink-merge.git] / chrome / browser / ui / webui / downloads_ui.cc
blob0d4a2580b78e64cdc467693d71f542d9909df491
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;
38 namespace {
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);
55 // Status.
56 source->AddLocalizedString("statusCancelled", IDS_DOWNLOAD_TAB_CANCELLED);
57 source->AddLocalizedString("statusRemoved", IDS_DOWNLOAD_FILE_REMOVED);
59 // Dangerous file.
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);
73 // Controls.
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);
82 PrefService* prefs = profile->GetPrefs();
83 source->AddBoolean("allowDeletingHistory",
84 prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory) &&
85 !profile->IsSupervised());
87 source->SetJsonPath("strings.js");
88 source->AddResourcePath("constants.html", IDR_DOWNLOADS_CONSTANTS_HTML);
89 source->AddResourcePath("constants.js", IDR_DOWNLOADS_CONSTANTS_JS);
90 source->AddResourcePath("throttled_icon_loader.html",
91 IDR_DOWNLOADS_THROTTLED_ICON_LOADER_HTML);
92 source->AddResourcePath("throttled_icon_loader.js",
93 IDR_DOWNLOADS_THROTTLED_ICON_LOADER_JS);
95 if (MdDownloadsEnabled()) {
96 source->AddLocalizedString("controlRetry", IDS_MD_DOWNLOAD_LINK_RETRY);
97 source->AddLocalizedString("controlledByUrl",
98 IDS_DOWNLOAD_BY_EXTENSION_URL);
99 source->AddLocalizedString("noDownloads", IDS_MD_DOWNLOAD_NO_DOWNLOADS);
100 source->AddLocalizedString("search", IDS_MD_DOWNLOAD_SEARCH);
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);
121 } else {
122 source->AddLocalizedString("controlRetry", IDS_DOWNLOAD_LINK_RETRY);
123 source->AddLocalizedString("controlByExtension",
124 IDS_DOWNLOAD_BY_EXTENSION);
125 source->AddLocalizedString("noDownloads", IDS_DOWNLOAD_NO_DOWNLOADS);
126 source->AddLocalizedString("searchButton", IDS_DOWNLOAD_SEARCH_BUTTON);
128 source->AddResourcePath("item_view.js", IDR_DOWNLOADS_ITEM_VIEW_JS);
129 source->AddResourcePath("focus_row.js", IDR_DOWNLOADS_FOCUS_ROW_JS);
130 source->AddResourcePath("manager.js", IDR_DOWNLOADS_MANAGER_JS);
131 source->SetDefaultResource(IDR_DOWNLOADS_DOWNLOADS_HTML);
134 return source;
137 } // namespace
139 ///////////////////////////////////////////////////////////////////////////////
141 // DownloadsUI
143 ///////////////////////////////////////////////////////////////////////////////
145 DownloadsUI::DownloadsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
146 Profile* profile = Profile::FromWebUI(web_ui);
147 DownloadManager* dlm = BrowserContext::GetDownloadManager(profile);
149 DownloadsDOMHandler* handler = new DownloadsDOMHandler(dlm);
150 web_ui->AddMessageHandler(handler);
152 // Set up the chrome://downloads/ source.
153 content::WebUIDataSource* source = CreateDownloadsUIHTMLSource(profile);
154 content::WebUIDataSource::Add(profile, source);
155 #if defined(ENABLE_THEMES)
156 ThemeSource* theme = new ThemeSource(profile);
157 content::URLDataSource::Add(profile, theme);
158 #endif
161 // static
162 base::RefCountedMemory* DownloadsUI::GetFaviconResourceBytes(
163 ui::ScaleFactor scale_factor) {
164 return ResourceBundle::GetSharedInstance().
165 LoadDataResourceBytesForScale(IDR_DOWNLOADS_FAVICON, scale_factor);