summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/downloads_ui.cc
blob: 6ec23ae3cb8897a9275d5ac8ed1e7f3c496858e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/downloads_ui.h"

#include "base/memory/ref_counted_memory.h"
#include "base/memory/singleton.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_piece.h"
#include "base/threading/thread.h"
#include "base/values.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/download/download_service.h"
#include "chrome/browser/download/download_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/downloads_dom_handler.h"
#include "chrome/browser/ui/webui/theme_source.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "grit/browser_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"

using content::BrowserContext;
using content::DownloadManager;
using content::WebContents;

namespace {

content::WebUIDataSource* CreateDownloadsUIHTMLSource(Profile* profile) {
  content::WebUIDataSource* source =
      content::WebUIDataSource::Create(chrome::kChromeUIDownloadsHost);

  source->AddLocalizedString("title", IDS_DOWNLOAD_TITLE);
  source->AddLocalizedString("searchResultsFor", IDS_DOWNLOAD_SEARCHRESULTSFOR);
  source->AddLocalizedString("downloads", IDS_DOWNLOAD_TITLE);
  source->AddLocalizedString("clearAll", IDS_DOWNLOAD_LINK_CLEAR_ALL);
  source->AddLocalizedString("openDownloadsFolder",
                             IDS_DOWNLOAD_LINK_OPEN_DOWNLOADS_FOLDER);

  // No results/downloads messages that show instead of the downloads list.
  source->AddLocalizedString("noDownloads", IDS_DOWNLOAD_NO_DOWNLOADS);
  source->AddLocalizedString("noSearchResults",
                             IDS_DOWNLOAD_NO_SEARCH_RESULTS);

  // Status.
  source->AddLocalizedString("statusCancelled", IDS_DOWNLOAD_TAB_CANCELLED);
  source->AddLocalizedString("statusRemoved", IDS_DOWNLOAD_FILE_REMOVED);

  // Dangerous file.
  source->AddLocalizedString("dangerFileDesc", IDS_PROMPT_DANGEROUS_DOWNLOAD);
  source->AddLocalizedString("dangerUrlDesc",
                             IDS_PROMPT_MALICIOUS_DOWNLOAD_URL);
  source->AddLocalizedString("dangerContentDesc",
                             IDS_PROMPT_MALICIOUS_DOWNLOAD_CONTENT);
  source->AddLocalizedString("dangerUncommonDesc",
                             IDS_PROMPT_UNCOMMON_DOWNLOAD_CONTENT);
  source->AddLocalizedString("dangerSettingsDesc",
                             IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS);
  source->AddLocalizedString("dangerSave", IDS_CONFIRM_DOWNLOAD);
  source->AddLocalizedString("dangerRestore", IDS_CONFIRM_DOWNLOAD_RESTORE);
  source->AddLocalizedString("dangerDiscard", IDS_DISCARD_DOWNLOAD);

  // Controls.
  source->AddLocalizedString("controlPause", IDS_DOWNLOAD_LINK_PAUSE);
  if (browser_defaults::kDownloadPageHasShowInFolder)
    source->AddLocalizedString("controlShowInFolder", IDS_DOWNLOAD_LINK_SHOW);
  source->AddLocalizedString("controlCancel", IDS_DOWNLOAD_LINK_CANCEL);
  source->AddLocalizedString("controlResume", IDS_DOWNLOAD_LINK_RESUME);
  source->AddLocalizedString("controlRemoveFromList",
                             IDS_DOWNLOAD_LINK_REMOVE);
  source->AddLocalizedString("controlByExtension",
                             IDS_DOWNLOAD_BY_EXTENSION);

  PrefService* prefs = profile->GetPrefs();
  source->AddBoolean("allowDeletingHistory",
                     prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory) &&
                     !profile->IsSupervised());

  source->SetJsonPath("strings.js");
  source->AddResourcePath("constants.html", IDR_DOWNLOADS_CONSTANTS_HTML);
  source->AddResourcePath("constants.js", IDR_DOWNLOADS_CONSTANTS_JS);
  source->AddResourcePath("throttled_icon_loader.html",
                          IDR_DOWNLOADS_THROTTLED_ICON_LOADER_HTML);
  source->AddResourcePath("throttled_icon_loader.js",
                          IDR_DOWNLOADS_THROTTLED_ICON_LOADER_JS);

  if (switches::MdDownloadsEnabled()) {
    source->AddLocalizedString("search", IDS_MD_DOWNLOAD_SEARCH);
    source->AddLocalizedString("controlRetry", IDS_MD_DOWNLOAD_LINK_RETRY);

    source->AddResourcePath("action_service.html",
                            IDR_MD_DOWNLOADS_ACTION_SERVICE_HTML);
    source->AddResourcePath("action_service.js",
                            IDR_MD_DOWNLOADS_ACTION_SERVICE_JS);
    source->AddResourcePath("item.css", IDR_MD_DOWNLOADS_ITEM_CSS);
    source->AddResourcePath("item.html", IDR_MD_DOWNLOADS_ITEM_HTML);
    source->AddResourcePath("item.js", IDR_MD_DOWNLOADS_ITEM_JS);
    source->AddResourcePath("manager.css", IDR_MD_DOWNLOADS_MANAGER_CSS);
    source->AddResourcePath("manager.html", IDR_MD_DOWNLOADS_MANAGER_HTML);
    source->AddResourcePath("manager.js", IDR_MD_DOWNLOADS_MANAGER_JS);
    source->AddResourcePath("shared_style.css",
                            IDR_MD_DOWNLOADS_SHARED_STYLE_CSS);
    source->AddResourcePath("strings.html", IDR_MD_DOWNLOADS_STRINGS_HTML);
    source->AddResourcePath("toolbar.css", IDR_MD_DOWNLOADS_TOOLBAR_CSS);
    source->AddResourcePath("toolbar.html", IDR_MD_DOWNLOADS_TOOLBAR_HTML);
    source->AddResourcePath("toolbar.js", IDR_MD_DOWNLOADS_TOOLBAR_JS);
    source->SetDefaultResource(IDR_MD_DOWNLOADS_DOWNLOADS_HTML);
  } else {
    source->AddLocalizedString("searchButton", IDS_DOWNLOAD_SEARCH_BUTTON);
    source->AddLocalizedString("controlRetry", IDS_DOWNLOAD_LINK_RETRY);

    source->AddResourcePath("item_view.js", IDR_DOWNLOADS_ITEM_VIEW_JS);
    source->AddResourcePath("focus_row.js", IDR_DOWNLOADS_FOCUS_ROW_JS);
    source->AddResourcePath("manager.js", IDR_DOWNLOADS_MANAGER_JS);
    source->SetDefaultResource(IDR_DOWNLOADS_DOWNLOADS_HTML);
  }

  return source;
}

}  // namespace

///////////////////////////////////////////////////////////////////////////////
//
// DownloadsUI
//
///////////////////////////////////////////////////////////////////////////////

DownloadsUI::DownloadsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
  Profile* profile = Profile::FromWebUI(web_ui);
  DownloadManager* dlm = BrowserContext::GetDownloadManager(profile);

  DownloadsDOMHandler* handler = new DownloadsDOMHandler(dlm);
  web_ui->AddMessageHandler(handler);

  // Set up the chrome://downloads/ source.
  content::WebUIDataSource* source = CreateDownloadsUIHTMLSource(profile);
  content::WebUIDataSource::Add(profile, source);
#if defined(ENABLE_THEMES)
  ThemeSource* theme = new ThemeSource(profile);
  content::URLDataSource::Add(profile, theme);
#endif
}

// static
base::RefCountedMemory* DownloadsUI::GetFaviconResourceBytes(
      ui::ScaleFactor scale_factor) {
  return ResourceBundle::GetSharedInstance().
      LoadDataResourceBytesForScale(IDR_DOWNLOADS_FAVICON, scale_factor);
}