OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/downloads_dom_handler.h" | 5 #include "chrome/browser/ui/webui/downloads_dom_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
13 #include "base/string_piece.h" | 13 #include "base/string_piece.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/download/download_history.h" | 18 #include "chrome/browser/download/download_history.h" |
19 #include "chrome/browser/download/download_item.h" | 19 #include "chrome/browser/download/download_item.h" |
| 20 #include "chrome/browser/download/download_prefs.h" |
20 #include "chrome/browser/download/download_util.h" | 21 #include "chrome/browser/download/download_util.h" |
| 22 #include "chrome/browser/platform_util.h" |
21 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 24 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
23 #include "chrome/browser/ui/webui/fileicon_source.h" | 25 #include "chrome/browser/ui/webui/fileicon_source.h" |
24 #include "chrome/browser/ui/webui/fileicon_source_cros.h" | 26 #include "chrome/browser/ui/webui/fileicon_source_cros.h" |
25 #include "chrome/common/jstemplate_builder.h" | 27 #include "chrome/common/jstemplate_builder.h" |
26 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
27 #include "content/browser/browser_thread.h" | 29 #include "content/browser/browser_thread.h" |
28 #include "content/browser/tab_contents/tab_contents.h" | 30 #include "content/browser/tab_contents/tab_contents.h" |
29 #include "content/browser/user_metrics.h" | 31 #include "content/browser/user_metrics.h" |
30 #include "grit/generated_resources.h" | 32 #include "grit/generated_resources.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 web_ui_->RegisterMessageCallback("togglepause", | 92 web_ui_->RegisterMessageCallback("togglepause", |
91 NewCallback(this, &DownloadsDOMHandler::HandlePause)); | 93 NewCallback(this, &DownloadsDOMHandler::HandlePause)); |
92 web_ui_->RegisterMessageCallback("resume", | 94 web_ui_->RegisterMessageCallback("resume", |
93 NewCallback(this, &DownloadsDOMHandler::HandlePause)); | 95 NewCallback(this, &DownloadsDOMHandler::HandlePause)); |
94 web_ui_->RegisterMessageCallback("remove", | 96 web_ui_->RegisterMessageCallback("remove", |
95 NewCallback(this, &DownloadsDOMHandler::HandleRemove)); | 97 NewCallback(this, &DownloadsDOMHandler::HandleRemove)); |
96 web_ui_->RegisterMessageCallback("cancel", | 98 web_ui_->RegisterMessageCallback("cancel", |
97 NewCallback(this, &DownloadsDOMHandler::HandleCancel)); | 99 NewCallback(this, &DownloadsDOMHandler::HandleCancel)); |
98 web_ui_->RegisterMessageCallback("clearAll", | 100 web_ui_->RegisterMessageCallback("clearAll", |
99 NewCallback(this, &DownloadsDOMHandler::HandleClearAll)); | 101 NewCallback(this, &DownloadsDOMHandler::HandleClearAll)); |
| 102 web_ui_->RegisterMessageCallback("openDownloadsFolder", |
| 103 NewCallback(this, &DownloadsDOMHandler::HandleOpenDownloadsFolder)); |
100 } | 104 } |
101 | 105 |
102 void DownloadsDOMHandler::OnDownloadUpdated(DownloadItem* download) { | 106 void DownloadsDOMHandler::OnDownloadUpdated(DownloadItem* download) { |
103 // Get the id for the download. Our downloads are sorted latest to first, | 107 // Get the id for the download. Our downloads are sorted latest to first, |
104 // and the id is the index into that list. We should be careful of sync | 108 // and the id is the index into that list. We should be careful of sync |
105 // errors between the UI and the download_items_ list (we may wish to use | 109 // errors between the UI and the download_items_ list (we may wish to use |
106 // something other than 'id'). | 110 // something other than 'id'). |
107 OrderedDownloads::iterator it = find(download_items_.begin(), | 111 OrderedDownloads::iterator it = find(download_items_.begin(), |
108 download_items_.end(), | 112 download_items_.end(), |
109 download); | 113 download); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 void DownloadsDOMHandler::HandleCancel(const ListValue* args) { | 224 void DownloadsDOMHandler::HandleCancel(const ListValue* args) { |
221 DownloadItem* file = GetDownloadByValue(args); | 225 DownloadItem* file = GetDownloadByValue(args); |
222 if (file) | 226 if (file) |
223 file->Cancel(true); | 227 file->Cancel(true); |
224 } | 228 } |
225 | 229 |
226 void DownloadsDOMHandler::HandleClearAll(const ListValue* args) { | 230 void DownloadsDOMHandler::HandleClearAll(const ListValue* args) { |
227 download_manager_->RemoveAllDownloads(); | 231 download_manager_->RemoveAllDownloads(); |
228 } | 232 } |
229 | 233 |
| 234 void DownloadsDOMHandler::HandleOpenDownloadsFolder(const ListValue* args) { |
| 235 FilePath path = download_manager_->download_prefs()->download_path(); |
| 236 |
| 237 #if defined(OS_MACOSX) |
| 238 // Must be called from the UI thread on Mac. |
| 239 platform_util::OpenItem(path); |
| 240 #else |
| 241 BrowserThread::PostTask( |
| 242 BrowserThread::FILE, FROM_HERE, |
| 243 NewRunnableFunction(&platform_util::OpenItem, path)); |
| 244 #endif |
| 245 } |
| 246 |
230 // DownloadsDOMHandler, private: ---------------------------------------------- | 247 // DownloadsDOMHandler, private: ---------------------------------------------- |
231 | 248 |
232 void DownloadsDOMHandler::SendCurrentDownloads() { | 249 void DownloadsDOMHandler::SendCurrentDownloads() { |
233 ListValue results_value; | 250 ListValue results_value; |
234 for (OrderedDownloads::iterator it = download_items_.begin(); | 251 for (OrderedDownloads::iterator it = download_items_.begin(); |
235 it != download_items_.end(); ++it) { | 252 it != download_items_.end(); ++it) { |
236 int index = static_cast<int>(it - download_items_.begin()); | 253 int index = static_cast<int>(it - download_items_.begin()); |
237 if (index > kMaxDownloads) | 254 if (index > kMaxDownloads) |
238 break; | 255 break; |
239 if (!*it) | 256 if (!*it) |
(...skipping 26 matching lines...) Expand all Loading... |
266 return NULL; | 283 return NULL; |
267 } | 284 } |
268 | 285 |
269 DownloadItem* DownloadsDOMHandler::GetDownloadByValue(const ListValue* args) { | 286 DownloadItem* DownloadsDOMHandler::GetDownloadByValue(const ListValue* args) { |
270 int id; | 287 int id; |
271 if (ExtractIntegerValue(args, &id)) { | 288 if (ExtractIntegerValue(args, &id)) { |
272 return GetDownloadById(id); | 289 return GetDownloadById(id); |
273 } | 290 } |
274 return NULL; | 291 return NULL; |
275 } | 292 } |
OLD | NEW |