Chromium Code Reviews| 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" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 | 113 |
| 114 // A download has started or been deleted. Query our DownloadManager for the | 114 // A download has started or been deleted. Query our DownloadManager for the |
| 115 // current set of downloads. | 115 // current set of downloads. |
| 116 void DownloadsDOMHandler::ModelChanged() { | 116 void DownloadsDOMHandler::ModelChanged() { |
| 117 ClearDownloadItems(); | 117 ClearDownloadItems(); |
| 118 download_manager_->SearchDownloads(WideToUTF16(search_text_), | 118 download_manager_->SearchDownloads(WideToUTF16(search_text_), |
| 119 &download_items_); | 119 &download_items_); |
| 120 sort(download_items_.begin(), download_items_.end(), DownloadItemSorter()); | 120 sort(download_items_.begin(), download_items_.end(), DownloadItemSorter()); |
| 121 | 121 |
| 122 // Scan for any in progress downloads and add ourself to them as an observer. | 122 // Scan for any in progress downloads and add ourself to them as an observer. |
| 123 for (OrderedDownloads::iterator it = download_items_.begin(); | 123 for (OrderedDownloads::iterator it = download_items_.begin(); |
|
Paweł Hajdan Jr.
2011/05/10 11:37:51
Oh, the loop is now empty and the comment is also
| |
| 124 it != download_items_.end(); ++it) { | 124 it != download_items_.end(); ++it) { |
| 125 if (static_cast<int>(it - download_items_.begin()) > kMaxDownloads) | 125 if (static_cast<int>(it - download_items_.begin()) > kMaxDownloads) |
| 126 break; | 126 break; |
| 127 | |
| 128 DownloadItem* download = *it; | |
| 129 if (download->IsInProgress()) { | |
| 130 // We want to know what happens as the download progresses. | |
| 131 download->AddObserver(this); | |
| 132 } else if (download->safety_state() == DownloadItem::DANGEROUS) { | |
| 133 // We need to be notified when the user validates the dangerous download. | |
| 134 download->AddObserver(this); | |
| 135 } | |
| 136 } | 127 } |
| 137 | 128 |
| 138 SendCurrentDownloads(); | 129 SendCurrentDownloads(); |
| 139 } | 130 } |
| 140 | 131 |
| 141 void DownloadsDOMHandler::HandleGetDownloads(const ListValue* args) { | 132 void DownloadsDOMHandler::HandleGetDownloads(const ListValue* args) { |
| 142 std::wstring new_search = UTF16ToWideHack(ExtractStringValue(args)); | 133 std::wstring new_search = UTF16ToWideHack(ExtractStringValue(args)); |
| 143 if (search_text_.compare(new_search) != 0) { | 134 if (search_text_.compare(new_search) != 0) { |
| 144 search_text_ = new_search; | 135 search_text_ = new_search; |
| 145 ModelChanged(); | 136 ModelChanged(); |
| 146 } else { | 137 } else { |
| 147 SendCurrentDownloads(); | 138 SendCurrentDownloads(); |
| 148 } | 139 } |
| 140 | |
| 141 download_manager_->CheckForFilesRemoval(); | |
| 149 } | 142 } |
| 150 | 143 |
| 151 void DownloadsDOMHandler::HandleOpenFile(const ListValue* args) { | 144 void DownloadsDOMHandler::HandleOpenFile(const ListValue* args) { |
| 152 DownloadItem* file = GetDownloadByValue(args); | 145 DownloadItem* file = GetDownloadByValue(args); |
| 153 if (file) | 146 if (file) |
| 154 file->OpenDownload(); | 147 file->OpenDownload(); |
| 155 } | 148 } |
| 156 | 149 |
| 157 void DownloadsDOMHandler::HandleDrag(const ListValue* args) { | 150 void DownloadsDOMHandler::HandleDrag(const ListValue* args) { |
| 158 DownloadItem* file = GetDownloadByValue(args); | 151 DownloadItem* file = GetDownloadByValue(args); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 void DownloadsDOMHandler::HandleClearAll(const ListValue* args) { | 197 void DownloadsDOMHandler::HandleClearAll(const ListValue* args) { |
| 205 download_manager_->RemoveAllDownloads(); | 198 download_manager_->RemoveAllDownloads(); |
| 206 } | 199 } |
| 207 | 200 |
| 208 // DownloadsDOMHandler, private: ---------------------------------------------- | 201 // DownloadsDOMHandler, private: ---------------------------------------------- |
| 209 | 202 |
| 210 void DownloadsDOMHandler::SendCurrentDownloads() { | 203 void DownloadsDOMHandler::SendCurrentDownloads() { |
| 211 ListValue results_value; | 204 ListValue results_value; |
| 212 for (OrderedDownloads::iterator it = download_items_.begin(); | 205 for (OrderedDownloads::iterator it = download_items_.begin(); |
| 213 it != download_items_.end(); ++it) { | 206 it != download_items_.end(); ++it) { |
| 207 // Add observers for all download items just once | |
| 208 if (!(*it)->HasObserver(this)) | |
| 209 (*it)->AddObserver(this); | |
| 214 int index = static_cast<int>(it - download_items_.begin()); | 210 int index = static_cast<int>(it - download_items_.begin()); |
| 215 if (index > kMaxDownloads) | 211 if (index > kMaxDownloads) |
| 216 break; | 212 break; |
| 217 results_value.Append(download_util::CreateDownloadItemValue(*it, index)); | 213 results_value.Append(download_util::CreateDownloadItemValue(*it, index)); |
| 218 } | 214 } |
| 219 | 215 |
| 220 web_ui_->CallJavascriptFunction("downloadsList", results_value); | 216 web_ui_->CallJavascriptFunction("downloadsList", results_value); |
| 221 } | 217 } |
| 222 | 218 |
| 223 void DownloadsDOMHandler::ClearDownloadItems() { | 219 void DownloadsDOMHandler::ClearDownloadItems() { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 240 return NULL; | 236 return NULL; |
| 241 } | 237 } |
| 242 | 238 |
| 243 DownloadItem* DownloadsDOMHandler::GetDownloadByValue(const ListValue* args) { | 239 DownloadItem* DownloadsDOMHandler::GetDownloadByValue(const ListValue* args) { |
| 244 int id; | 240 int id; |
| 245 if (ExtractIntegerValue(args, &id)) { | 241 if (ExtractIntegerValue(args, &id)) { |
| 246 return GetDownloadById(id); | 242 return GetDownloadById(id); |
| 247 } | 243 } |
| 248 return NULL; | 244 return NULL; |
| 249 } | 245 } |
| OLD | NEW |