OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 // Get the id for the download. Our downloads are sorted latest to first, | 172 // Get the id for the download. Our downloads are sorted latest to first, |
173 // and the id is the index into that list. We should be careful of sync | 173 // and the id is the index into that list. We should be careful of sync |
174 // errors between the UI and the download_items_ list (we may wish to use | 174 // errors between the UI and the download_items_ list (we may wish to use |
175 // something other than 'id'). | 175 // something other than 'id'). |
176 OrderedDownloads::iterator it = std::find(download_items_.begin(), | 176 OrderedDownloads::iterator it = std::find(download_items_.begin(), |
177 download_items_.end(), | 177 download_items_.end(), |
178 download); | 178 download); |
179 if (it == download_items_.end()) | 179 if (it == download_items_.end()) |
180 return; | 180 return; |
181 | 181 |
182 if (download->GetState() == content::DownloadItem::REMOVING) { | |
183 (*it)->RemoveObserver(this); | |
184 *it = NULL; | |
185 // A later ModelChanged() notification will change the WebUI's | |
186 // view of the downloads list. | |
187 return; | |
188 } | |
189 | |
190 const int id = static_cast<int>(it - download_items_.begin()); | 182 const int id = static_cast<int>(it - download_items_.begin()); |
191 | 183 |
192 ListValue results_value; | 184 ListValue results_value; |
193 results_value.Append(download_util::CreateDownloadItemValue(download, id)); | 185 results_value.Append(download_util::CreateDownloadItemValue(download, id)); |
194 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); | 186 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); |
195 } | 187 } |
196 | 188 |
| 189 void DownloadsDOMHandler::OnDownloadDestroyed( |
| 190 content::DownloadItem* download) { |
| 191 download->RemoveObserver(this); |
| 192 OrderedDownloads::iterator it = std::find(download_items_.begin(), |
| 193 download_items_.end(), |
| 194 download); |
| 195 *it = NULL; |
| 196 // A later ModelChanged() notification will change the WebUI's |
| 197 // view of the downloads list. |
| 198 } |
| 199 |
197 // A download has started or been deleted. Query our DownloadManager for the | 200 // A download has started or been deleted. Query our DownloadManager for the |
198 // current set of downloads. | 201 // current set of downloads. |
199 void DownloadsDOMHandler::ModelChanged(content::DownloadManager* manager) { | 202 void DownloadsDOMHandler::ModelChanged(content::DownloadManager* manager) { |
200 DCHECK(manager == download_manager_ || | 203 DCHECK(manager == download_manager_ || |
201 manager == original_profile_download_manager_); | 204 manager == original_profile_download_manager_); |
202 | 205 |
203 ClearDownloadItems(); | 206 ClearDownloadItems(); |
204 download_manager_->SearchDownloads(WideToUTF16(search_text_), | 207 download_manager_->SearchDownloads(WideToUTF16(search_text_), |
205 &download_items_); | 208 &download_items_); |
206 // If we have a parent DownloadManager, let it add its downloads to the | 209 // If we have a parent DownloadManager, let it add its downloads to the |
(...skipping 14 matching lines...) Expand all Loading... |
221 } | 224 } |
222 | 225 |
223 // Add ourself to all download items as an observer. | 226 // Add ourself to all download items as an observer. |
224 for (OrderedDownloads::iterator it = download_items_.begin(); | 227 for (OrderedDownloads::iterator it = download_items_.begin(); |
225 it != download_items_.end(); ++it) { | 228 it != download_items_.end(); ++it) { |
226 if (static_cast<int>(it - download_items_.begin()) > kMaxDownloads) | 229 if (static_cast<int>(it - download_items_.begin()) > kMaxDownloads) |
227 break; | 230 break; |
228 | 231 |
229 // We should never see anything that isn't already in the history. | 232 // We should never see anything that isn't already in the history. |
230 DCHECK(*it); | 233 DCHECK(*it); |
231 DCHECK((*it)->IsPersisted()); | |
232 | 234 |
233 (*it)->AddObserver(this); | 235 (*it)->AddObserver(this); |
234 } | 236 } |
235 | 237 |
236 SendCurrentDownloads(); | 238 SendCurrentDownloads(); |
237 } | 239 } |
238 | 240 |
239 void DownloadsDOMHandler::ManagerGoingDown(content::DownloadManager* manager) { | 241 void DownloadsDOMHandler::ManagerGoingDown(content::DownloadManager* manager) { |
240 // This should never happen. The lifetime of the DownloadsDOMHandler | 242 // This should never happen. The lifetime of the DownloadsDOMHandler |
241 // is tied to the tab in which downloads.html is displayed, which cannot | 243 // is tied to the tab in which downloads.html is displayed, which cannot |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE); | 311 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE); |
310 content::DownloadItem* file = GetDownloadByValue(args); | 312 content::DownloadItem* file = GetDownloadByValue(args); |
311 if (file) | 313 if (file) |
312 file->TogglePause(); | 314 file->TogglePause(); |
313 } | 315 } |
314 | 316 |
315 void DownloadsDOMHandler::HandleRemove(const ListValue* args) { | 317 void DownloadsDOMHandler::HandleRemove(const ListValue* args) { |
316 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE); | 318 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE); |
317 content::DownloadItem* file = GetDownloadByValue(args); | 319 content::DownloadItem* file = GetDownloadByValue(args); |
318 if (file) { | 320 if (file) { |
319 DCHECK(file->IsPersisted()); | |
320 file->Remove(); | 321 file->Remove(); |
321 } | 322 } |
322 } | 323 } |
323 | 324 |
324 void DownloadsDOMHandler::HandleCancel(const ListValue* args) { | 325 void DownloadsDOMHandler::HandleCancel(const ListValue* args) { |
325 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); | 326 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); |
326 content::DownloadItem* file = GetDownloadByValue(args); | 327 content::DownloadItem* file = GetDownloadByValue(args); |
327 if (file) | 328 if (file) |
328 file->Cancel(true); | 329 file->Cancel(true); |
329 } | 330 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 } | 405 } |
405 | 406 |
406 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( | 407 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( |
407 const ListValue* args) { | 408 const ListValue* args) { |
408 int id; | 409 int id; |
409 if (ExtractIntegerValue(args, &id)) { | 410 if (ExtractIntegerValue(args, &id)) { |
410 return GetDownloadById(id); | 411 return GetDownloadById(id); |
411 } | 412 } |
412 return NULL; | 413 return NULL; |
413 } | 414 } |
OLD | NEW |