Chromium Code Reviews| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 #endif | 52 #endif |
| 53 | 53 |
| 54 using content::BrowserContext; | 54 using content::BrowserContext; |
| 55 using content::BrowserThread; | 55 using content::BrowserThread; |
| 56 using content::UserMetricsAction; | 56 using content::UserMetricsAction; |
| 57 | 57 |
| 58 namespace { | 58 namespace { |
| 59 | 59 |
| 60 // Maximum number of downloads to show. TODO(glen): Remove this and instead | 60 // Maximum number of downloads to show. TODO(glen): Remove this and instead |
| 61 // stuff the downloads down the pipe slowly. | 61 // stuff the downloads down the pipe slowly. |
| 62 static const int kMaxDownloads = 150; | 62 static const size_t kMaxDownloads = 150; |
| 63 | 63 |
| 64 // Sort DownloadItems into descending order by their start time. | 64 // Sort DownloadItems into descending order by their start time. |
| 65 class DownloadItemSorter : public std::binary_function<content::DownloadItem*, | 65 class DownloadItemSorter : public std::binary_function<content::DownloadItem*, |
| 66 content::DownloadItem*, | 66 content::DownloadItem*, |
| 67 bool> { | 67 bool> { |
| 68 public: | 68 public: |
| 69 bool operator()(const content::DownloadItem* lhs, | 69 bool operator()(const content::DownloadItem* lhs, |
| 70 const content::DownloadItem* rhs) { | 70 const content::DownloadItem* rhs) { |
| 71 return lhs->GetStartTime() > rhs->GetStartTime(); | 71 return lhs->GetStartTime() > rhs->GetStartTime(); |
| 72 } | 72 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 NOTREACHED(); | 113 NOTREACHED(); |
| 114 return ""; | 114 return ""; |
| 115 } | 115 } |
| 116 } | 116 } |
| 117 | 117 |
| 118 // Return a JSON dictionary containing some of the attributes of |download|. | 118 // Return a JSON dictionary containing some of the attributes of |download|. |
| 119 // The JSON dictionary will also have a field "id" set to |id|, and a field | 119 // The JSON dictionary will also have a field "id" set to |id|, and a field |
| 120 // "otr" set to |incognito|. | 120 // "otr" set to |incognito|. |
| 121 DictionaryValue* CreateDownloadItemValue( | 121 DictionaryValue* CreateDownloadItemValue( |
| 122 content::DownloadItem* download, | 122 content::DownloadItem* download, |
| 123 int id, | |
| 124 bool incognito) { | 123 bool incognito) { |
| 125 DictionaryValue* file_value = new DictionaryValue(); | 124 DictionaryValue* file_value = new DictionaryValue(); |
| 126 | 125 |
| 127 file_value->SetInteger( | 126 file_value->SetInteger( |
| 128 "started", static_cast<int>(download->GetStartTime().ToTimeT())); | 127 "started", static_cast<int>(download->GetStartTime().ToTimeT())); |
| 129 file_value->SetString( | 128 file_value->SetString( |
| 130 "since_string", TimeFormat::RelativeDate(download->GetStartTime(), NULL)); | 129 "since_string", TimeFormat::RelativeDate(download->GetStartTime(), NULL)); |
| 131 file_value->SetString( | 130 file_value->SetString( |
| 132 "date_string", base::TimeFormatShortDate(download->GetStartTime())); | 131 "date_string", base::TimeFormatShortDate(download->GetStartTime())); |
| 133 file_value->SetInteger("id", id); | 132 file_value->SetInteger("id", download->GetId()); |
| 134 | 133 |
| 135 FilePath download_path(download->GetTargetFilePath()); | 134 FilePath download_path(download->GetTargetFilePath()); |
| 136 file_value->Set("file_path", base::CreateFilePathValue(download_path)); | 135 file_value->Set("file_path", base::CreateFilePathValue(download_path)); |
| 137 file_value->SetString("file_url", | 136 file_value->SetString("file_url", |
| 138 net::FilePathToFileURL(download_path).spec()); | 137 net::FilePathToFileURL(download_path).spec()); |
| 139 | 138 |
| 140 // Keep file names as LTR. | 139 // Keep file names as LTR. |
| 141 string16 file_name = download->GetFileNameToReportUser().LossyDisplayName(); | 140 string16 file_name = download->GetFileNameToReportUser().LossyDisplayName(); |
| 142 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); | 141 file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); |
| 143 file_value->SetString("file_name", file_name); | 142 file_value->SetString("file_name", file_name); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 // incognito download manager, if one exists. | 206 // incognito download manager, if one exists. |
| 208 bool IsItemIncognito( | 207 bool IsItemIncognito( |
| 209 int32 download_id, | 208 int32 download_id, |
| 210 content::DownloadManager* manager, | 209 content::DownloadManager* manager, |
| 211 content::DownloadManager* original_manager) { | 210 content::DownloadManager* original_manager) { |
| 212 // |original_manager| is only non-NULL if |manager| is incognito. | 211 // |original_manager| is only non-NULL if |manager| is incognito. |
| 213 return (original_manager && | 212 return (original_manager && |
| 214 (manager->GetDownload(download_id) != NULL)); | 213 (manager->GetDownload(download_id) != NULL)); |
| 215 } | 214 } |
| 216 | 215 |
| 216 // Filter out extension downloads and downloads that don't have a filename yet. | |
| 217 bool IsDownloadDisplayable(const content::DownloadItem& item) { | |
| 218 return (!download_crx_util::IsExtensionDownload(item) && | |
| 219 !item.GetFileNameToReportUser().empty() && | |
| 220 !item.GetTargetFilePath().empty()); | |
|
Randy Smith (Not in Mondays)
2012/08/17 17:49:47
This plus the shift to OnDownloadCreated() means t
benjhayden
2012/08/17 20:54:15
Tested manually by calling Cancel(true) as soon as
| |
| 221 } | |
| 222 | |
| 217 } // namespace | 223 } // namespace |
| 218 | 224 |
| 219 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) | 225 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) |
| 220 : search_text_(), | 226 : search_text_(), |
| 221 download_manager_(dlm), | 227 download_manager_(dlm), |
| 222 original_profile_download_manager_(NULL), | 228 original_profile_download_manager_(NULL), |
| 223 initialized_(false), | 229 update_scheduled_(false), |
| 224 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 230 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 225 // Create our fileicon data source. | 231 // Create our fileicon data source. |
| 226 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); | 232 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); |
| 227 ChromeURLDataManager::AddDataSource(profile, new FileIconSource()); | 233 ChromeURLDataManager::AddDataSource(profile, new FileIconSource()); |
| 228 | 234 |
| 229 // Figure out our parent DownloadManager, if any. | 235 // Observe the DownloadManagers. |
| 230 Profile* original_profile = profile->GetOriginalProfile(); | 236 download_manager_->AddObserver(this); |
| 231 if (original_profile != profile) { | 237 if (profile->IsOffTheRecord()) { |
| 232 original_profile_download_manager_ = | 238 original_profile_download_manager_ = |
| 233 BrowserContext::GetDownloadManager(original_profile); | 239 BrowserContext::GetDownloadManager(profile->GetOriginalProfile()); |
| 240 original_profile_download_manager_->AddObserver(this); | |
| 241 } | |
| 242 | |
| 243 // Observe all the DownloadItems. | |
| 244 content::DownloadManager::DownloadVector downloads; | |
| 245 SearchDownloads(&downloads); | |
| 246 for (content::DownloadManager::DownloadVector::const_iterator | |
| 247 iter = downloads.begin(); | |
| 248 iter != downloads.end(); ++iter) { | |
| 249 (*iter)->AddObserver(this); | |
| 250 observing_items_.insert(*iter); | |
| 234 } | 251 } |
| 235 } | 252 } |
| 236 | 253 |
| 237 DownloadsDOMHandler::~DownloadsDOMHandler() { | 254 DownloadsDOMHandler::~DownloadsDOMHandler() { |
| 238 ClearDownloadItems(); | 255 for (DownloadSet::const_iterator it = observing_items_.begin(); |
| 256 it != observing_items_.end(); ++it) { | |
| 257 (*it)->RemoveObserver(this); | |
| 258 } | |
| 259 observing_items_.clear(); | |
| 239 download_manager_->RemoveObserver(this); | 260 download_manager_->RemoveObserver(this); |
| 240 if (original_profile_download_manager_) | 261 if (original_profile_download_manager_) { |
| 241 original_profile_download_manager_->RemoveObserver(this); | 262 original_profile_download_manager_->RemoveObserver(this); |
| 263 } | |
| 242 } | 264 } |
| 243 | 265 |
| 244 // DownloadsDOMHandler, public: ----------------------------------------------- | 266 // DownloadsDOMHandler, public: ----------------------------------------------- |
| 245 | 267 |
| 246 void DownloadsDOMHandler::OnPageLoaded(const base::ListValue* args) { | 268 void DownloadsDOMHandler::OnPageLoaded(const base::ListValue* args) { |
| 247 if (initialized_) | 269 SendCurrentDownloads(); |
| 248 return; | |
| 249 initialized_ = true; | |
| 250 | |
| 251 download_manager_->AddObserver(this); | |
| 252 if (original_profile_download_manager_) | |
| 253 original_profile_download_manager_->AddObserver(this); | |
| 254 } | 270 } |
| 255 | 271 |
| 256 void DownloadsDOMHandler::RegisterMessages() { | 272 void DownloadsDOMHandler::RegisterMessages() { |
| 257 web_ui()->RegisterMessageCallback("onPageLoaded", | 273 web_ui()->RegisterMessageCallback("onPageLoaded", |
| 258 base::Bind(&DownloadsDOMHandler::OnPageLoaded, | 274 base::Bind(&DownloadsDOMHandler::OnPageLoaded, |
| 259 base::Unretained(this))); | 275 weak_ptr_factory_.GetWeakPtr())); |
| 260 web_ui()->RegisterMessageCallback("getDownloads", | 276 web_ui()->RegisterMessageCallback("getDownloads", |
| 261 base::Bind(&DownloadsDOMHandler::HandleGetDownloads, | 277 base::Bind(&DownloadsDOMHandler::HandleGetDownloads, |
| 262 base::Unretained(this))); | 278 weak_ptr_factory_.GetWeakPtr())); |
| 263 web_ui()->RegisterMessageCallback("openFile", | 279 web_ui()->RegisterMessageCallback("openFile", |
| 264 base::Bind(&DownloadsDOMHandler::HandleOpenFile, | 280 base::Bind(&DownloadsDOMHandler::HandleOpenFile, |
| 265 base::Unretained(this))); | 281 weak_ptr_factory_.GetWeakPtr())); |
| 266 web_ui()->RegisterMessageCallback("drag", | 282 web_ui()->RegisterMessageCallback("drag", |
| 267 base::Bind(&DownloadsDOMHandler::HandleDrag, | 283 base::Bind(&DownloadsDOMHandler::HandleDrag, |
| 268 base::Unretained(this))); | 284 weak_ptr_factory_.GetWeakPtr())); |
| 269 web_ui()->RegisterMessageCallback("saveDangerous", | 285 web_ui()->RegisterMessageCallback("saveDangerous", |
| 270 base::Bind(&DownloadsDOMHandler::HandleSaveDangerous, | 286 base::Bind(&DownloadsDOMHandler::HandleSaveDangerous, |
| 271 base::Unretained(this))); | 287 weak_ptr_factory_.GetWeakPtr())); |
| 272 web_ui()->RegisterMessageCallback("discardDangerous", | 288 web_ui()->RegisterMessageCallback("discardDangerous", |
| 273 base::Bind(&DownloadsDOMHandler::HandleDiscardDangerous, | 289 base::Bind(&DownloadsDOMHandler::HandleDiscardDangerous, |
| 274 base::Unretained(this))); | 290 weak_ptr_factory_.GetWeakPtr())); |
| 275 web_ui()->RegisterMessageCallback("show", | 291 web_ui()->RegisterMessageCallback("show", |
| 276 base::Bind(&DownloadsDOMHandler::HandleShow, | 292 base::Bind(&DownloadsDOMHandler::HandleShow, |
| 277 base::Unretained(this))); | 293 weak_ptr_factory_.GetWeakPtr())); |
| 278 web_ui()->RegisterMessageCallback("togglepause", | 294 web_ui()->RegisterMessageCallback("togglepause", |
| 279 base::Bind(&DownloadsDOMHandler::HandlePause, | 295 base::Bind(&DownloadsDOMHandler::HandlePause, |
| 280 base::Unretained(this))); | 296 weak_ptr_factory_.GetWeakPtr())); |
| 281 web_ui()->RegisterMessageCallback("resume", | 297 web_ui()->RegisterMessageCallback("resume", |
| 282 base::Bind(&DownloadsDOMHandler::HandlePause, | 298 base::Bind(&DownloadsDOMHandler::HandlePause, |
| 283 base::Unretained(this))); | 299 weak_ptr_factory_.GetWeakPtr())); |
| 284 web_ui()->RegisterMessageCallback("remove", | 300 web_ui()->RegisterMessageCallback("remove", |
| 285 base::Bind(&DownloadsDOMHandler::HandleRemove, | 301 base::Bind(&DownloadsDOMHandler::HandleRemove, |
| 286 base::Unretained(this))); | 302 weak_ptr_factory_.GetWeakPtr())); |
| 287 web_ui()->RegisterMessageCallback("cancel", | 303 web_ui()->RegisterMessageCallback("cancel", |
| 288 base::Bind(&DownloadsDOMHandler::HandleCancel, | 304 base::Bind(&DownloadsDOMHandler::HandleCancel, |
| 289 base::Unretained(this))); | 305 weak_ptr_factory_.GetWeakPtr())); |
| 290 web_ui()->RegisterMessageCallback("clearAll", | 306 web_ui()->RegisterMessageCallback("clearAll", |
| 291 base::Bind(&DownloadsDOMHandler::HandleClearAll, | 307 base::Bind(&DownloadsDOMHandler::HandleClearAll, |
| 292 base::Unretained(this))); | 308 weak_ptr_factory_.GetWeakPtr())); |
| 293 web_ui()->RegisterMessageCallback("openDownloadsFolder", | 309 web_ui()->RegisterMessageCallback("openDownloadsFolder", |
| 294 base::Bind(&DownloadsDOMHandler::HandleOpenDownloadsFolder, | 310 base::Bind(&DownloadsDOMHandler::HandleOpenDownloadsFolder, |
| 295 base::Unretained(this))); | 311 weak_ptr_factory_.GetWeakPtr())); |
| 312 } | |
| 313 | |
| 314 void DownloadsDOMHandler::OnDownloadCreated( | |
| 315 content::DownloadManager* manager, content::DownloadItem* download_item) { | |
| 316 download_item->AddObserver(this); | |
| 317 observing_items_.insert(download_item); | |
| 318 if (IsDownloadDisplayable(*download_item) && | |
| 319 !update_scheduled_) { | |
| 320 // Don't call SendCurrentDownloads() every time anything changes. Batch them | |
| 321 // together instead. We may handle hundreds of OnDownloadCreated() calls in | |
| 322 // a single UI message loop iteration when the history is loaded. | |
| 323 update_scheduled_ = true; | |
| 324 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 325 base::Bind(&DownloadsDOMHandler::SendCurrentDownloads, | |
| 326 weak_ptr_factory_.GetWeakPtr())); | |
| 327 } | |
| 296 } | 328 } |
| 297 | 329 |
| 298 void DownloadsDOMHandler::OnDownloadUpdated(content::DownloadItem* download) { | 330 void DownloadsDOMHandler::OnDownloadUpdated(content::DownloadItem* download) { |
| 299 // Get the id for the download. Our downloads are sorted latest to first, | 331 if (IsDownloadDisplayable(*download)) { |
| 300 // and the id is the index into that list. We should be careful of sync | 332 base::ListValue results_value; |
| 301 // errors between the UI and the download_items_ list (we may wish to use | 333 results_value.Append(CreateDownloadItemValue(download, IsItemIncognito( |
| 302 // something other than 'id'). | 334 download->GetId(), |
| 303 OrderedDownloads::iterator it = std::find(download_items_.begin(), | 335 download_manager_, |
| 304 download_items_.end(), | 336 original_profile_download_manager_))); |
| 305 download); | 337 CallDownloadUpdated(results_value); |
| 306 if (it == download_items_.end()) | 338 } |
| 307 return; | |
| 308 | |
| 309 const int id = static_cast<int>(it - download_items_.begin()); | |
| 310 | |
| 311 ListValue results_value; | |
| 312 results_value.Append(CreateDownloadItemValue(download, id, IsItemIncognito( | |
| 313 download->GetId(), | |
| 314 download_manager_, | |
| 315 original_profile_download_manager_))); | |
| 316 web_ui()->CallJavascriptFunction("downloadUpdated", results_value); | |
| 317 } | 339 } |
| 318 | 340 |
| 319 void DownloadsDOMHandler::OnDownloadDestroyed( | 341 void DownloadsDOMHandler::OnDownloadDestroyed( |
| 320 content::DownloadItem* download) { | 342 content::DownloadItem* download_item) { |
|
Randy Smith (Not in Mondays)
2012/08/17 17:49:47
Worth DCHECKing to make sure we have it?
benjhayden
2012/08/17 20:54:15
Who would call OnDownloadDestroyed besides ~Downlo
| |
| 321 download->RemoveObserver(this); | 343 download_item->RemoveObserver(this); |
| 322 OrderedDownloads::iterator it = std::find(download_items_.begin(), | 344 observing_items_.erase(download_item); |
| 323 download_items_.end(), | 345 // Don't call SendCurrentDownloads() every time anything changes. Batch them |
| 324 download); | 346 // together instead. We may handle hundreds of OnDownloadDestroyed() calls in |
| 325 if (it != download_items_.end()) | 347 // a single UI message loop iteration when the user Clears All downloads. |
| 326 *it = NULL; | 348 if (!update_scheduled_) { |
| 327 // A later ModelChanged() notification will change the WebUI's | 349 update_scheduled_ = true; |
| 328 // view of the downloads list. | 350 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 329 } | 351 base::Bind(&DownloadsDOMHandler::SendCurrentDownloads, |
| 330 | 352 weak_ptr_factory_.GetWeakPtr())); |
| 331 // A download has started or been deleted. Query our DownloadManager for the | |
| 332 // current set of downloads. | |
| 333 void DownloadsDOMHandler::ModelChanged(content::DownloadManager* manager) { | |
| 334 DCHECK(manager == download_manager_ || | |
| 335 manager == original_profile_download_manager_); | |
| 336 | |
| 337 ClearDownloadItems(); | |
| 338 download_manager_->SearchDownloads(WideToUTF16(search_text_), | |
| 339 &download_items_); | |
| 340 // If we have a parent DownloadManager, let it add its downloads to the | |
| 341 // results. | |
| 342 if (original_profile_download_manager_) { | |
| 343 original_profile_download_manager_->SearchDownloads( | |
| 344 WideToUTF16(search_text_), &download_items_); | |
| 345 } | 353 } |
| 346 | |
| 347 sort(download_items_.begin(), download_items_.end(), DownloadItemSorter()); | |
| 348 | |
| 349 // Remove any extension downloads. | |
| 350 for (size_t i = 0; i < download_items_.size();) { | |
| 351 if (download_crx_util::IsExtensionDownload(*download_items_[i])) | |
| 352 download_items_.erase(download_items_.begin() + i); | |
| 353 else | |
| 354 i++; | |
| 355 } | |
| 356 | |
| 357 // Add ourself to all download items as an observer. | |
| 358 for (OrderedDownloads::iterator it = download_items_.begin(); | |
| 359 it != download_items_.end(); ++it) { | |
| 360 if (static_cast<int>(it - download_items_.begin()) > kMaxDownloads) | |
| 361 break; | |
| 362 | |
| 363 // We should never see anything that isn't already in the history. | |
| 364 DCHECK(*it); | |
| 365 DCHECK((*it)->IsPersisted()); | |
| 366 | |
| 367 (*it)->AddObserver(this); | |
| 368 } | |
| 369 | |
| 370 SendCurrentDownloads(); | |
| 371 } | 354 } |
| 372 | 355 |
| 373 void DownloadsDOMHandler::ManagerGoingDown(content::DownloadManager* manager) { | 356 void DownloadsDOMHandler::ManagerGoingDown(content::DownloadManager* manager) { |
| 374 // This should never happen. The lifetime of the DownloadsDOMHandler | 357 // This should never happen. The lifetime of the DownloadsDOMHandler |
| 375 // is tied to the tab in which downloads.html is displayed, which cannot | 358 // is tied to the tab in which downloads.html is displayed, which cannot |
| 376 // outlive the Browser that contains it, which cannot outlive the Profile | 359 // outlive the Browser that contains it, which cannot outlive the Profile |
| 377 // it is associated with. If that profile is an incognito profile, | 360 // it is associated with. If that profile is an incognito profile, |
| 378 // it cannot outlive its original profile. Thus this class should be | 361 // it cannot outlive its original profile. Thus this class should be |
| 379 // destroyed before a ManagerGoingDown() notification occurs. | 362 // destroyed before a ManagerGoingDown() notification occurs. |
| 380 NOTREACHED(); | 363 NOTREACHED(); |
| 381 } | 364 } |
| 382 | 365 |
| 383 void DownloadsDOMHandler::HandleGetDownloads(const ListValue* args) { | 366 void DownloadsDOMHandler::HandleGetDownloads(const base::ListValue* args) { |
| 384 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_GET_DOWNLOADS); | 367 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_GET_DOWNLOADS); |
| 385 std::wstring new_search = UTF16ToWideHack(ExtractStringValue(args)); | 368 search_text_ = ExtractStringValue(args); |
| 386 if (search_text_.compare(new_search) != 0) { | 369 SendCurrentDownloads(); |
| 387 search_text_ = new_search; | |
| 388 ModelChanged(download_manager_); | |
| 389 } else { | |
| 390 SendCurrentDownloads(); | |
| 391 } | |
| 392 | |
| 393 download_manager_->CheckForHistoryFilesRemoval(); | 370 download_manager_->CheckForHistoryFilesRemoval(); |
| 394 } | 371 } |
| 395 | 372 |
| 396 void DownloadsDOMHandler::HandleOpenFile(const ListValue* args) { | 373 void DownloadsDOMHandler::HandleOpenFile(const base::ListValue* args) { |
| 397 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FILE); | 374 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FILE); |
| 398 content::DownloadItem* file = GetDownloadByValue(args); | 375 content::DownloadItem* file = GetDownloadByValue(args); |
| 399 if (file) | 376 if (file) |
| 400 file->OpenDownload(); | 377 file->OpenDownload(); |
| 401 } | 378 } |
| 402 | 379 |
| 403 void DownloadsDOMHandler::HandleDrag(const ListValue* args) { | 380 void DownloadsDOMHandler::HandleDrag(const base::ListValue* args) { |
| 404 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DRAG); | 381 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DRAG); |
| 405 content::DownloadItem* file = GetDownloadByValue(args); | 382 content::DownloadItem* file = GetDownloadByValue(args); |
| 406 if (file) { | 383 content::WebContents* web_contents = GetWebUIWebContents(); |
| 407 IconManager* im = g_browser_process->icon_manager(); | 384 if (!file || !web_contents) |
| 408 gfx::Image* icon = im->LookupIcon(file->GetUserVerifiedFilePath(), | 385 return; |
| 409 IconLoader::NORMAL); | 386 gfx::Image* icon = g_browser_process->icon_manager()->LookupIcon( |
| 410 gfx::NativeView view = web_ui()->GetWebContents()->GetNativeView(); | 387 file->GetUserVerifiedFilePath(), IconLoader::NORMAL); |
| 411 { | 388 gfx::NativeView view = web_contents->GetNativeView(); |
| 412 // Enable nested tasks during DnD, while |DragDownload()| blocks. | 389 { |
| 413 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 390 // Enable nested tasks during DnD, while |DragDownload()| blocks. |
| 414 download_util::DragDownload(file, icon, view); | 391 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 415 } | 392 download_util::DragDownload(file, icon, view); |
| 416 } | 393 } |
| 417 } | 394 } |
| 418 | 395 |
| 419 void DownloadsDOMHandler::HandleSaveDangerous(const ListValue* args) { | 396 void DownloadsDOMHandler::HandleSaveDangerous(const base::ListValue* args) { |
| 420 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS); | 397 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS); |
| 421 content::DownloadItem* file = GetDownloadByValue(args); | 398 content::DownloadItem* file = GetDownloadByValue(args); |
| 422 if (file) | 399 if (file) |
| 423 ShowDangerPrompt(file); | 400 ShowDangerPrompt(file); |
| 424 // TODO(benjhayden): else ModelChanged()? Downloads might be able to disappear | |
| 425 // out from under us, so update our idea of the downloads as soon as possible. | |
| 426 } | 401 } |
| 427 | 402 |
| 428 void DownloadsDOMHandler::HandleDiscardDangerous(const ListValue* args) { | 403 void DownloadsDOMHandler::HandleDiscardDangerous(const base::ListValue* args) { |
| 429 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS); | 404 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS); |
| 430 content::DownloadItem* file = GetDownloadByValue(args); | 405 content::DownloadItem* file = GetDownloadByValue(args); |
| 431 if (file) | 406 if (file) |
| 432 file->Delete(content::DownloadItem::DELETE_DUE_TO_USER_DISCARD); | 407 file->Delete(content::DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
| 433 } | 408 } |
| 434 | 409 |
| 435 void DownloadsDOMHandler::HandleShow(const ListValue* args) { | 410 void DownloadsDOMHandler::HandleShow(const base::ListValue* args) { |
| 436 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SHOW); | 411 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SHOW); |
| 437 content::DownloadItem* file = GetDownloadByValue(args); | 412 content::DownloadItem* file = GetDownloadByValue(args); |
| 438 if (file) | 413 if (file) |
| 439 file->ShowDownloadInShell(); | 414 file->ShowDownloadInShell(); |
| 440 } | 415 } |
| 441 | 416 |
| 442 void DownloadsDOMHandler::HandlePause(const ListValue* args) { | 417 void DownloadsDOMHandler::HandlePause(const base::ListValue* args) { |
| 443 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE); | 418 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE); |
| 444 content::DownloadItem* file = GetDownloadByValue(args); | 419 content::DownloadItem* file = GetDownloadByValue(args); |
| 445 if (file) | 420 if (file) |
| 446 file->TogglePause(); | 421 file->TogglePause(); |
| 447 } | 422 } |
| 448 | 423 |
| 449 void DownloadsDOMHandler::HandleRemove(const ListValue* args) { | 424 void DownloadsDOMHandler::HandleRemove(const base::ListValue* args) { |
| 450 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE); | 425 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE); |
| 451 content::DownloadItem* file = GetDownloadByValue(args); | 426 content::DownloadItem* file = GetDownloadByValue(args); |
| 452 if (file) { | 427 if (file) { |
| 453 DCHECK(file->IsPersisted()); | 428 DCHECK(file->IsPersisted()); |
| 454 file->Remove(); | 429 file->Remove(); |
| 455 } | 430 } |
| 456 } | 431 } |
| 457 | 432 |
| 458 void DownloadsDOMHandler::HandleCancel(const ListValue* args) { | 433 void DownloadsDOMHandler::HandleCancel(const base::ListValue* args) { |
| 459 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); | 434 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); |
| 460 content::DownloadItem* file = GetDownloadByValue(args); | 435 content::DownloadItem* file = GetDownloadByValue(args); |
| 461 if (file) | 436 if (file) |
| 462 file->Cancel(true); | 437 file->Cancel(true); |
| 463 } | 438 } |
| 464 | 439 |
| 465 void DownloadsDOMHandler::HandleClearAll(const ListValue* args) { | 440 void DownloadsDOMHandler::HandleClearAll(const base::ListValue* args) { |
| 466 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CLEAR_ALL); | 441 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CLEAR_ALL); |
| 467 download_manager_->RemoveAllDownloads(); | 442 download_manager_->RemoveAllDownloads(); |
| 468 | 443 |
| 469 // If this is an incognito downloader, clear All should clear main download | 444 // If this is an incognito downloader, clear All should clear main download |
| 470 // manager as well. | 445 // manager as well. |
| 471 if (original_profile_download_manager_) | 446 if (original_profile_download_manager_) |
| 472 original_profile_download_manager_->RemoveAllDownloads(); | 447 original_profile_download_manager_->RemoveAllDownloads(); |
| 473 } | 448 } |
| 474 | 449 |
| 475 void DownloadsDOMHandler::HandleOpenDownloadsFolder(const ListValue* args) { | 450 void DownloadsDOMHandler::HandleOpenDownloadsFolder( |
| 451 const base::ListValue* args) { | |
| 476 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FOLDER); | 452 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FOLDER); |
| 477 platform_util::OpenItem( | 453 platform_util::OpenItem( |
| 478 DownloadPrefs::FromDownloadManager(download_manager_)->DownloadPath()); | 454 DownloadPrefs::FromDownloadManager(download_manager_)->DownloadPath()); |
| 479 } | 455 } |
| 480 | 456 |
| 481 // DownloadsDOMHandler, private: ---------------------------------------------- | 457 // DownloadsDOMHandler, private: ---------------------------------------------- |
| 482 | 458 |
| 483 void DownloadsDOMHandler::SendCurrentDownloads() { | 459 void DownloadsDOMHandler::SendCurrentDownloads() { |
| 484 ListValue results_value; | 460 update_scheduled_ = false; |
| 485 for (OrderedDownloads::iterator it = download_items_.begin(); | 461 content::DownloadManager::DownloadVector downloads; |
| 486 it != download_items_.end(); ++it) { | 462 SearchDownloads(&downloads); |
| 487 if (!*it) | 463 sort(downloads.begin(), downloads.end(), DownloadItemSorter()); |
| 488 continue; | 464 base::ListValue results_value; |
| 489 int index = static_cast<int>(it - download_items_.begin()); | 465 for (content::DownloadManager::DownloadVector::const_iterator |
| 490 if (index <= kMaxDownloads) | 466 iter = downloads.begin(); |
| 491 results_value.Append(CreateDownloadItemValue(*it, index, IsItemIncognito( | 467 iter != downloads.end(); ++iter) { |
| 492 (*it)->GetId(), | 468 if (IsDownloadDisplayable(**iter)) { |
| 469 results_value.Append(CreateDownloadItemValue(*iter, IsItemIncognito( | |
| 470 (*iter)->GetId(), | |
| 493 download_manager_, | 471 download_manager_, |
| 494 original_profile_download_manager_))); | 472 original_profile_download_manager_))); |
| 473 } | |
| 474 if (results_value.GetSize() == kMaxDownloads) | |
| 475 break; | |
| 495 } | 476 } |
| 477 CallDownloadsList(results_value); | |
| 478 } | |
| 496 | 479 |
| 497 web_ui()->CallJavascriptFunction("downloadsList", results_value); | 480 void DownloadsDOMHandler::SearchDownloads( |
| 481 content::DownloadManager::DownloadVector* downloads) { | |
| 482 download_manager_->SearchDownloads(search_text_, downloads); | |
| 483 if (original_profile_download_manager_) { | |
| 484 original_profile_download_manager_->SearchDownloads( | |
| 485 search_text_, downloads); | |
| 486 } | |
| 498 } | 487 } |
| 499 | 488 |
| 500 void DownloadsDOMHandler::ShowDangerPrompt( | 489 void DownloadsDOMHandler::ShowDangerPrompt( |
| 501 content::DownloadItem* dangerous_item) { | 490 content::DownloadItem* dangerous_item) { |
| 502 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( | 491 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( |
| 503 dangerous_item, | 492 dangerous_item, |
| 504 TabContents::FromWebContents(web_ui()->GetWebContents()), | 493 TabContents::FromWebContents(GetWebUIWebContents()), |
| 505 base::Bind(&DownloadsDOMHandler::DangerPromptAccepted, | 494 base::Bind(&DownloadsDOMHandler::DangerPromptAccepted, |
| 506 weak_ptr_factory_.GetWeakPtr(), dangerous_item->GetId()), | 495 weak_ptr_factory_.GetWeakPtr(), dangerous_item->GetId()), |
| 507 base::Closure()); | 496 base::Closure()); |
| 508 // danger_prompt will delete itself. | 497 // danger_prompt will delete itself. |
| 509 DCHECK(danger_prompt); | 498 DCHECK(danger_prompt); |
| 510 } | 499 } |
| 511 | 500 |
| 512 void DownloadsDOMHandler::DangerPromptAccepted(int download_id) { | 501 void DownloadsDOMHandler::DangerPromptAccepted(int download_id) { |
| 513 content::DownloadItem* item = download_manager_->GetActiveDownloadItem( | 502 content::DownloadItem* item = download_manager_->GetActiveDownloadItem( |
| 514 download_id); | 503 download_id); |
| 515 if (!item) | 504 if (!item) |
| 516 return; | 505 return; |
| 517 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS); | 506 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS); |
| 518 item->DangerousDownloadValidated(); | 507 item->DangerousDownloadValidated(); |
| 519 } | 508 } |
| 520 | 509 |
| 521 void DownloadsDOMHandler::ClearDownloadItems() { | 510 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( |
| 522 // Clear out old state and remove self as observer for each download. | 511 const base::ListValue* args) { |
| 523 for (OrderedDownloads::iterator it = download_items_.begin(); | 512 int id = -1; |
| 524 it != download_items_.end(); ++it) { | 513 if (!ExtractIntegerValue(args, &id)) |
| 525 if (!*it) | 514 return NULL; |
| 526 continue; | 515 content::DownloadItem* download = download_manager_->GetDownload(id); |
| 527 (*it)->RemoveObserver(this); | 516 if (download == NULL) { |
| 517 download = original_profile_download_manager_->GetDownload(id); | |
| 528 } | 518 } |
| 529 download_items_.clear(); | 519 return download; |
| 530 } | 520 } |
| 531 | 521 |
| 532 content::DownloadItem* DownloadsDOMHandler::GetDownloadById(int id) { | 522 content::WebContents* DownloadsDOMHandler::GetWebUIWebContents() { |
| 533 for (OrderedDownloads::iterator it = download_items_.begin(); | 523 return web_ui()->GetWebContents(); |
| 534 it != download_items_.end(); ++it) { | |
| 535 if (static_cast<int>(it - download_items_.begin() == id)) { | |
| 536 return (*it); | |
| 537 } | |
| 538 } | |
| 539 | |
| 540 return NULL; | |
| 541 } | 524 } |
| 542 | 525 |
| 543 content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue( | 526 void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) { |
| 544 const ListValue* args) { | 527 web_ui()->CallJavascriptFunction("downloadsList", downloads); |
| 545 int id; | |
| 546 if (ExtractIntegerValue(args, &id)) { | |
| 547 return GetDownloadById(id); | |
| 548 } | |
| 549 return NULL; | |
| 550 } | 528 } |
| 529 | |
| 530 void DownloadsDOMHandler::CallDownloadUpdated(const base::ListValue& download) { | |
| 531 web_ui()->CallJavascriptFunction("downloadUpdated", download); | |
| 532 } | |
| OLD | NEW |