Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/dom_ui/downloads_ui.h" | 5 #include "chrome/browser/dom_ui/downloads_ui.h" |
| 6 | 6 |
| 7 #include "base/gfx/png_encoder.h" | 7 #include "base/gfx/png_encoder.h" |
| 8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
| 9 #include "base/thread.h" | 9 #include "base/thread.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| 11 #include "base/time_format.h" | 11 #include "base/time_format.h" |
| 12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/browser/dom_ui/fileicon_source.h" | 13 #include "chrome/browser/dom_ui/fileicon_source.h" |
| 14 #include "chrome/browser/download/download_util.h" | 14 #include "chrome/browser/download/download_util.h" |
| 15 #include "chrome/browser/metrics/user_metrics.h" | 15 #include "chrome/browser/metrics/user_metrics.h" |
| 16 #include "chrome/browser/profile.h" | 16 #include "chrome/browser/profile.h" |
| 17 #include "chrome/common/jstemplate_builder.h" | 17 #include "chrome/common/jstemplate_builder.h" |
| 18 #include "chrome/common/l10n_util.h" | 18 #include "chrome/common/l10n_util.h" |
| 19 #include "chrome/common/time_format.h" | 19 #include "chrome/common/time_format.h" |
| 20 #include "grit/browser_resources.h" | 20 #include "grit/browser_resources.h" |
| 21 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 22 | 22 |
| 23 using base::Time; | 23 using base::Time; |
| 24 | 24 |
| 25 // DownloadsUI is accessible from chrome-ui://downloads. | 25 // DownloadsUI is accessible from chrome-ui://downloads. |
| 26 static const char kDownloadsHost[] = "downloads"; | 26 static const char kDownloadsHost[] = "downloads"; |
| 27 | 27 |
| 28 // Maximum number of downloads to show. TODO(glen): Remove this and instead | |
|
Dan Beam
2015/12/11 00:25:47
well, that only took 6.75 years :)
https://codere
| |
| 29 // stuff the downloads down the pipe slowly. | |
| 30 static const int kMaxDownloads = 150; | |
| 31 | |
| 28 /////////////////////////////////////////////////////////////////////////////// | 32 /////////////////////////////////////////////////////////////////////////////// |
| 29 // | 33 // |
| 30 // DownloadsHTMLSource | 34 // DownloadsHTMLSource |
| 31 // | 35 // |
| 32 /////////////////////////////////////////////////////////////////////////////// | 36 /////////////////////////////////////////////////////////////////////////////// |
| 33 | 37 |
| 34 DownloadsUIHTMLSource::DownloadsUIHTMLSource() | 38 DownloadsUIHTMLSource::DownloadsUIHTMLSource() |
| 35 : DataSource(kDownloadsHost, MessageLoop::current()) { | 39 : DataSource(kDownloadsHost, MessageLoop::current()) { |
| 36 } | 40 } |
| 37 | 41 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 // DownloadsDOMHandler | 95 // DownloadsDOMHandler |
| 92 // | 96 // |
| 93 /////////////////////////////////////////////////////////////////////////////// | 97 /////////////////////////////////////////////////////////////////////////////// |
| 94 | 98 |
| 95 // Sort DownloadItems into descending order by their start time. | 99 // Sort DownloadItems into descending order by their start time. |
| 96 class DownloadItemSorter : public std::binary_function<DownloadItem*, | 100 class DownloadItemSorter : public std::binary_function<DownloadItem*, |
| 97 DownloadItem*, | 101 DownloadItem*, |
| 98 bool> { | 102 bool> { |
| 99 public: | 103 public: |
| 100 bool operator()(const DownloadItem* lhs, const DownloadItem* rhs) { | 104 bool operator()(const DownloadItem* lhs, const DownloadItem* rhs) { |
| 101 return lhs->start_time() < rhs->start_time(); | 105 return lhs->start_time() > rhs->start_time(); |
| 102 } | 106 } |
| 103 }; | 107 }; |
| 104 | 108 |
| 105 DownloadsDOMHandler::DownloadsDOMHandler(DOMUI* dom_ui, DownloadManager* dlm) | 109 DownloadsDOMHandler::DownloadsDOMHandler(DOMUI* dom_ui, DownloadManager* dlm) |
| 106 : DOMMessageHandler(dom_ui), | 110 : DOMMessageHandler(dom_ui), |
| 107 download_manager_(dlm), | 111 download_manager_(dlm), |
| 108 search_text_() { | 112 search_text_() { |
| 109 dom_ui_->RegisterMessageCallback("getDownloads", | 113 dom_ui_->RegisterMessageCallback("getDownloads", |
| 110 NewCallback(this, &DownloadsDOMHandler::HandleGetDownloads)); | 114 NewCallback(this, &DownloadsDOMHandler::HandleGetDownloads)); |
| 111 dom_ui_->RegisterMessageCallback("openFile", | 115 dom_ui_->RegisterMessageCallback("openFile", |
| 112 NewCallback(this, &DownloadsDOMHandler::HandleOpenFile)); | 116 NewCallback(this, &DownloadsDOMHandler::HandleOpenFile)); |
| 113 | 117 |
| 114 dom_ui_->RegisterMessageCallback("drag", | 118 dom_ui_->RegisterMessageCallback("drag", |
| 115 NewCallback(this, &DownloadsDOMHandler::HandleDrag)); | 119 NewCallback(this, &DownloadsDOMHandler::HandleDrag)); |
| 116 | 120 |
| 117 dom_ui_->RegisterMessageCallback("saveDangerous", | 121 dom_ui_->RegisterMessageCallback("saveDangerous", |
| 118 NewCallback(this, &DownloadsDOMHandler::HandleSaveDangerous)); | 122 NewCallback(this, &DownloadsDOMHandler::HandleSaveDangerous)); |
| 119 dom_ui_->RegisterMessageCallback("discardDangerous", | 123 dom_ui_->RegisterMessageCallback("discardDangerous", |
| 120 NewCallback(this, &DownloadsDOMHandler::HandleDiscardDangerous)); | 124 NewCallback(this, &DownloadsDOMHandler::HandleDiscardDangerous)); |
| 121 dom_ui_->RegisterMessageCallback("show", | 125 dom_ui_->RegisterMessageCallback("show", |
| 122 NewCallback(this, &DownloadsDOMHandler::HandleShow)); | 126 NewCallback(this, &DownloadsDOMHandler::HandleShow)); |
| 123 dom_ui_->RegisterMessageCallback("pause", | 127 dom_ui_->RegisterMessageCallback("togglepause", |
| 128 NewCallback(this, &DownloadsDOMHandler::HandlePause)); | |
| 129 dom_ui_->RegisterMessageCallback("resume", | |
| 124 NewCallback(this, &DownloadsDOMHandler::HandlePause)); | 130 NewCallback(this, &DownloadsDOMHandler::HandlePause)); |
| 125 dom_ui_->RegisterMessageCallback("cancel", | 131 dom_ui_->RegisterMessageCallback("cancel", |
| 126 NewCallback(this, &DownloadsDOMHandler::HandleCancel)); | 132 NewCallback(this, &DownloadsDOMHandler::HandleCancel)); |
| 127 | 133 |
| 128 // Create our fileicon data source. | 134 // Create our fileicon data source. |
| 129 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, | 135 g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE, |
| 130 NewRunnableMethod(&chrome_url_data_manager, | 136 NewRunnableMethod(&chrome_url_data_manager, |
| 131 &ChromeURLDataManager::AddDataSource, | 137 &ChromeURLDataManager::AddDataSource, |
| 132 new FileIconSource())); | 138 new FileIconSource())); |
| 133 } | 139 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 std::vector<DownloadItem*>& downloads) { | 178 std::vector<DownloadItem*>& downloads) { |
| 173 ClearDownloadItems(); | 179 ClearDownloadItems(); |
| 174 | 180 |
| 175 // Swap new downloads in. | 181 // Swap new downloads in. |
| 176 download_items_.swap(downloads); | 182 download_items_.swap(downloads); |
| 177 sort(download_items_.begin(), download_items_.end(), DownloadItemSorter()); | 183 sort(download_items_.begin(), download_items_.end(), DownloadItemSorter()); |
| 178 | 184 |
| 179 // Scan for any in progress downloads and add ourself to them as an observer. | 185 // Scan for any in progress downloads and add ourself to them as an observer. |
| 180 for (OrderedDownloads::iterator it = download_items_.begin(); | 186 for (OrderedDownloads::iterator it = download_items_.begin(); |
| 181 it != download_items_.end(); ++it) { | 187 it != download_items_.end(); ++it) { |
| 188 if (static_cast<int>(it - download_items_.begin()) > kMaxDownloads) | |
| 189 break; | |
| 190 | |
| 182 DownloadItem* download = *it; | 191 DownloadItem* download = *it; |
| 183 if (download->state() == DownloadItem::IN_PROGRESS) { | 192 if (download->state() == DownloadItem::IN_PROGRESS) { |
| 184 // We want to know what happens as the download progresses. | 193 // We want to know what happens as the download progresses. |
| 185 download->AddObserver(this); | 194 download->AddObserver(this); |
| 186 } else if (download->safety_state() == DownloadItem::DANGEROUS) { | 195 } else if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 187 // We need to be notified when the user validates the dangerous download. | 196 // We need to be notified when the user validates the dangerous download. |
| 188 download->AddObserver(this); | 197 download->AddObserver(this); |
| 189 } | 198 } |
| 190 } | 199 } |
| 191 | 200 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 void DownloadsDOMHandler::HandleCancel(const Value* value) { | 255 void DownloadsDOMHandler::HandleCancel(const Value* value) { |
| 247 DownloadItem* file = GetDownloadByValue(value); | 256 DownloadItem* file = GetDownloadByValue(value); |
| 248 if (file) | 257 if (file) |
| 249 file->Cancel(true); | 258 file->Cancel(true); |
| 250 } | 259 } |
| 251 | 260 |
| 252 // DownloadsDOMHandler, private: ---------------------------------------------- | 261 // DownloadsDOMHandler, private: ---------------------------------------------- |
| 253 | 262 |
| 254 void DownloadsDOMHandler::SendCurrentDownloads() { | 263 void DownloadsDOMHandler::SendCurrentDownloads() { |
| 255 ListValue results_value; | 264 ListValue results_value; |
| 256 | |
| 257 for (OrderedDownloads::iterator it = download_items_.begin(); | 265 for (OrderedDownloads::iterator it = download_items_.begin(); |
| 258 it != download_items_.end(); ++it) { | 266 it != download_items_.end(); ++it) { |
| 259 results_value.Append(CreateDownloadItemValue(*it, | 267 int index = static_cast<int>(it - download_items_.begin()); |
| 260 static_cast<int>(it - download_items_.begin()))); | 268 if (index > kMaxDownloads) |
| 269 break; | |
| 270 results_value.Append(CreateDownloadItemValue(*it,index)); | |
| 261 } | 271 } |
| 262 | 272 |
| 263 dom_ui_->CallJavascriptFunction(L"downloadsList", results_value); | 273 dom_ui_->CallJavascriptFunction(L"downloadsList", results_value); |
| 264 } | 274 } |
| 265 | 275 |
| 266 DictionaryValue* DownloadsDOMHandler::CreateDownloadItemValue( | 276 DictionaryValue* DownloadsDOMHandler::CreateDownloadItemValue( |
| 267 DownloadItem* download, int id) { | 277 DownloadItem* download, int id) { |
| 268 DictionaryValue* file_value = new DictionaryValue(); | 278 DictionaryValue* file_value = new DictionaryValue(); |
| 269 | 279 |
| 270 file_value->SetInteger(L"time", | 280 file_value->SetInteger(L"started", |
| 271 static_cast<int>(download->start_time().ToTimeT())); | 281 static_cast<int>(download->start_time().ToTimeT())); |
| 272 file_value->SetInteger(L"id", id); | 282 file_value->SetInteger(L"id", id); |
| 273 file_value->SetString(L"file_path", download->full_path().ToWStringHack()); | 283 file_value->SetString(L"file_path", download->full_path().ToWStringHack()); |
| 274 file_value->SetString(L"file_name", download->GetFileName().ToWStringHack()); | 284 file_value->SetString(L"file_name", download->GetFileName().ToWStringHack()); |
| 275 file_value->SetString(L"url", download->url().spec()); | 285 file_value->SetString(L"url", download->url().spec()); |
| 276 | 286 |
| 277 if (download->state() == DownloadItem::IN_PROGRESS) { | 287 if (download->state() == DownloadItem::IN_PROGRESS) { |
| 278 if (download->safety_state() == DownloadItem::DANGEROUS) { | 288 if (download->safety_state() == DownloadItem::DANGEROUS) { |
| 279 file_value->SetString(L"state", L"DANGEROUS"); | 289 file_value->SetString(L"state", L"DANGEROUS"); |
| 280 } else if (download->is_paused()) { | 290 } else if (download->is_paused()) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 358 } | 368 } |
| 359 | 369 |
| 360 // static | 370 // static |
| 361 GURL DownloadsUI::GetBaseURL() { | 371 GURL DownloadsUI::GetBaseURL() { |
| 362 std::string url = DOMUIContents::GetScheme(); | 372 std::string url = DOMUIContents::GetScheme(); |
| 363 url += "://"; | 373 url += "://"; |
| 364 url += kDownloadsHost; | 374 url += kDownloadsHost; |
| 365 return GURL(url); | 375 return GURL(url); |
| 366 } | 376 } |
| 367 | 377 |
| OLD | NEW |