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/extensions/api/downloads/downloads_api.h" | 5 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cctype> | 8 #include <cctype> |
9 #include <iterator> | 9 #include <iterator> |
10 #include <set> | 10 #include <set> |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 if (include_incognito && profile->HasOffTheRecordProfile()) { | 323 if (include_incognito && profile->HasOffTheRecordProfile()) { |
324 *incognito_manager = BrowserContext::GetDownloadManager( | 324 *incognito_manager = BrowserContext::GetDownloadManager( |
325 profile->GetOffTheRecordProfile()); | 325 profile->GetOffTheRecordProfile()); |
326 } | 326 } |
327 } | 327 } |
328 | 328 |
329 DownloadItem* GetActiveItem(Profile* profile, bool include_incognito, int id) { | 329 DownloadItem* GetActiveItem(Profile* profile, bool include_incognito, int id) { |
330 DownloadManager* manager = NULL; | 330 DownloadManager* manager = NULL; |
331 DownloadManager* incognito_manager = NULL; | 331 DownloadManager* incognito_manager = NULL; |
332 GetManagers(profile, include_incognito, &manager, &incognito_manager); | 332 GetManagers(profile, include_incognito, &manager, &incognito_manager); |
333 DownloadItem* download_item = manager->GetActiveDownloadItem(id); | 333 DownloadItem* download_item = manager->GetDownload(id); |
334 if (!download_item && incognito_manager) | 334 if (!download_item && incognito_manager) |
335 download_item = incognito_manager->GetActiveDownloadItem(id); | 335 download_item = incognito_manager->GetDownload(id); |
336 return download_item; | 336 return download_item && download_item->IsInProgress() ? download_item : NULL; |
337 } | 337 } |
338 | 338 |
339 enum DownloadsFunctionName { | 339 enum DownloadsFunctionName { |
340 DOWNLOADS_FUNCTION_DOWNLOAD = 0, | 340 DOWNLOADS_FUNCTION_DOWNLOAD = 0, |
341 DOWNLOADS_FUNCTION_SEARCH = 1, | 341 DOWNLOADS_FUNCTION_SEARCH = 1, |
342 DOWNLOADS_FUNCTION_PAUSE = 2, | 342 DOWNLOADS_FUNCTION_PAUSE = 2, |
343 DOWNLOADS_FUNCTION_RESUME = 3, | 343 DOWNLOADS_FUNCTION_RESUME = 3, |
344 DOWNLOADS_FUNCTION_CANCEL = 4, | 344 DOWNLOADS_FUNCTION_CANCEL = 4, |
345 DOWNLOADS_FUNCTION_ERASE = 5, | 345 DOWNLOADS_FUNCTION_ERASE = 5, |
346 DOWNLOADS_FUNCTION_SET_DESTINATION = 6, | 346 DOWNLOADS_FUNCTION_SET_DESTINATION = 6, |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 return; | 447 return; |
448 } | 448 } |
449 } | 449 } |
450 } | 450 } |
451 | 451 |
452 DownloadManager* manager = NULL; | 452 DownloadManager* manager = NULL; |
453 DownloadManager* incognito_manager = NULL; | 453 DownloadManager* incognito_manager = NULL; |
454 GetManagers(profile, include_incognito, &manager, &incognito_manager); | 454 GetManagers(profile, include_incognito, &manager, &incognito_manager); |
455 DownloadQuery::DownloadVector all_items; | 455 DownloadQuery::DownloadVector all_items; |
456 if (query_in.id.get()) { | 456 if (query_in.id.get()) { |
457 DownloadItem* item = manager->GetDownloadItem(*query_in.id.get()); | 457 DownloadItem* item = manager->GetDownload(*query_in.id.get()); |
458 if (!item && incognito_manager) | 458 if (!item && incognito_manager) |
459 item = incognito_manager->GetDownloadItem(*query_in.id.get()); | 459 item = incognito_manager->GetDownload(*query_in.id.get()); |
460 if (item) | 460 if (item) |
461 all_items.push_back(item); | 461 all_items.push_back(item); |
462 } else { | 462 } else { |
463 manager->GetAllDownloads(FilePath(FILE_PATH_LITERAL("")), &all_items); | 463 manager->GetAllDownloads(FilePath(FILE_PATH_LITERAL("")), &all_items); |
464 if (incognito_manager) | 464 if (incognito_manager) |
465 incognito_manager->GetAllDownloads( | 465 incognito_manager->GetAllDownloads( |
466 FilePath(FILE_PATH_LITERAL("")), &all_items); | 466 FilePath(FILE_PATH_LITERAL("")), &all_items); |
467 } | 467 } |
468 query_out.Search(all_items.begin(), all_items.end(), results); | 468 query_out.Search(all_items.begin(), all_items.end(), results); |
469 } | 469 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 | 588 |
589 DownloadsPauseFunction::DownloadsPauseFunction() {} | 589 DownloadsPauseFunction::DownloadsPauseFunction() {} |
590 DownloadsPauseFunction::~DownloadsPauseFunction() {} | 590 DownloadsPauseFunction::~DownloadsPauseFunction() {} |
591 | 591 |
592 bool DownloadsPauseFunction::RunImpl() { | 592 bool DownloadsPauseFunction::RunImpl() { |
593 scoped_ptr<extensions::api::downloads::Pause::Params> params( | 593 scoped_ptr<extensions::api::downloads::Pause::Params> params( |
594 extensions::api::downloads::Pause::Params::Create(*args_)); | 594 extensions::api::downloads::Pause::Params::Create(*args_)); |
595 EXTENSION_FUNCTION_VALIDATE(params.get()); | 595 EXTENSION_FUNCTION_VALIDATE(params.get()); |
596 DownloadItem* download_item = GetActiveItem( | 596 DownloadItem* download_item = GetActiveItem( |
597 profile(), include_incognito(), params->download_id); | 597 profile(), include_incognito(), params->download_id); |
598 if ((download_item == NULL) || !download_item->IsInProgress()) { | 598 if (download_item == NULL) { |
599 // This could be due to an invalid download ID, or it could be due to the | 599 // This could be due to an invalid download ID, or it could be due to the |
600 // download not being currently active. | 600 // download not being currently active. |
601 error_ = download_extension_errors::kInvalidOperationError; | 601 error_ = download_extension_errors::kInvalidOperationError; |
602 } else if (!download_item->IsPaused()) { | 602 } else if (!download_item->IsPaused()) { |
603 // If download_item->IsPaused() already then we treat it as a success. | 603 // If download_item->IsPaused() already then we treat it as a success. |
604 download_item->TogglePause(); | 604 download_item->TogglePause(); |
605 } | 605 } |
606 if (error_.empty()) | 606 if (error_.empty()) |
607 RecordApiFunctions(DOWNLOADS_FUNCTION_PAUSE); | 607 RecordApiFunctions(DOWNLOADS_FUNCTION_PAUSE); |
608 return error_.empty(); | 608 return error_.empty(); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
745 extensions::api::downloads::GetFileIcon::Params::Create(*args_)); | 745 extensions::api::downloads::GetFileIcon::Params::Create(*args_)); |
746 EXTENSION_FUNCTION_VALIDATE(params.get()); | 746 EXTENSION_FUNCTION_VALIDATE(params.get()); |
747 const extensions::api::downloads::GetFileIconOptions* options = | 747 const extensions::api::downloads::GetFileIconOptions* options = |
748 params->options.get(); | 748 params->options.get(); |
749 int icon_size = kDefaultIconSize; | 749 int icon_size = kDefaultIconSize; |
750 if (options && options->size.get()) | 750 if (options && options->size.get()) |
751 icon_size = *options->size.get(); | 751 icon_size = *options->size.get(); |
752 DownloadManager* manager = NULL; | 752 DownloadManager* manager = NULL; |
753 DownloadManager* incognito_manager = NULL; | 753 DownloadManager* incognito_manager = NULL; |
754 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); | 754 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); |
755 DownloadItem* download_item = manager->GetDownloadItem(params->download_id); | 755 DownloadItem* download_item = manager->GetDownload(params->download_id); |
756 if (!download_item && incognito_manager) | 756 if (!download_item && incognito_manager) |
757 download_item = incognito_manager->GetDownloadItem(params->download_id); | 757 download_item = incognito_manager->GetDownload(params->download_id); |
758 if (!download_item) { | 758 if (!download_item) { |
759 // The DownloadItem is is added to history when the path is determined. If | 759 // The DownloadItem is is added to history when the path is determined. If |
760 // the download is not in history, then we don't have a path / final | 760 // the download is not in history, then we don't have a path / final |
761 // filename and no icon. | 761 // filename and no icon. |
762 error_ = download_extension_errors::kInvalidOperationError; | 762 error_ = download_extension_errors::kInvalidOperationError; |
763 return false; | 763 return false; |
764 } | 764 } |
765 // In-progress downloads return the intermediate filename for GetFullPath() | 765 // In-progress downloads return the intermediate filename for GetFullPath() |
766 // which doesn't have the final extension. Therefore we won't be able to | 766 // which doesn't have the final extension. Therefore we won't be able to |
767 // derive a good file icon for it. So we use GetTargetFilePath() instead. | 767 // derive a good file icon for it. So we use GetTargetFilePath() instead. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
883 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); | 883 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); |
884 ++(on_changed_stats_[download_id]->fires); | 884 ++(on_changed_stats_[download_id]->fires); |
885 } | 885 } |
886 item_jsons_[download_id]->Swap(new_json.get()); | 886 item_jsons_[download_id]->Swap(new_json.get()); |
887 } | 887 } |
888 | 888 |
889 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { | 889 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { |
890 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 890 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
891 } | 891 } |
892 | 892 |
893 void ExtensionDownloadsEventRouter::ModelChanged(DownloadManager* manager) { | 893 void ExtensionDownloadsEventRouter::OnDownloadCreated( |
| 894 DownloadManager* manager, DownloadItem* download_item) { |
894 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 895 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
895 DCHECK(manager_ == manager); | 896 DCHECK(manager_ == manager); |
896 typedef std::set<int> DownloadIdSet; | 897 // TODO(benjhayden): Filter out temporary downloads? save pages? |
897 | 898 download_item->AddObserver(this); |
898 // Get all the download items. | 899 scoped_ptr<base::DictionaryValue> json_item( |
899 DownloadManager::DownloadVector current_vec; | 900 DownloadItemToJSON(download_item)); |
900 manager_->SearchDownloads(string16(), ¤t_vec); | 901 DispatchEvent(extension_event_names::kOnDownloadCreated, |
901 | 902 json_item->DeepCopy()); |
902 // Populate set<>s of download item identifiers so that we can find | 903 int32 download_id = download_item->GetId(); |
903 // differences between the old and the new set of download items. | 904 DCHECK(item_jsons_.find(download_id) == item_jsons_.end()); |
904 DownloadIdSet current_set, prev_set; | 905 on_changed_stats_[download_id] = new OnChangedStat(); |
905 for (ItemMap::const_iterator iter = downloads_.begin(); | 906 item_jsons_[download_id] = json_item.release(); |
906 iter != downloads_.end(); ++iter) { | 907 downloads_[download_id] = download_item; |
907 prev_set.insert(iter->first); | |
908 } | |
909 ItemMap current_map; | |
910 for (DownloadManager::DownloadVector::const_iterator iter = | |
911 current_vec.begin(); | |
912 iter != current_vec.end(); ++iter) { | |
913 DownloadItem* item = *iter; | |
914 int item_id = item->GetId(); | |
915 CHECK(item_id >= 0); | |
916 DCHECK(current_map.find(item_id) == current_map.end()); | |
917 current_set.insert(item_id); | |
918 current_map[item_id] = item; | |
919 } | |
920 DownloadIdSet new_set; // current_set - prev_set; | |
921 std::set_difference(current_set.begin(), current_set.end(), | |
922 prev_set.begin(), prev_set.end(), | |
923 std::insert_iterator<DownloadIdSet>( | |
924 new_set, new_set.begin())); | |
925 | |
926 // For each download that was not in the old set of downloads but is in the | |
927 // new set of downloads, fire an onCreated event, register as an Observer of | |
928 // the item, store a json representation of the item so that we can easily | |
929 // find changes in that json representation, and make an OnChangedStat. | |
930 for (DownloadIdSet::const_iterator iter = new_set.begin(); | |
931 iter != new_set.end(); ++iter) { | |
932 scoped_ptr<base::DictionaryValue> item( | |
933 DownloadItemToJSON(current_map[*iter])); | |
934 DispatchEvent(extension_event_names::kOnDownloadCreated, item->DeepCopy()); | |
935 DCHECK(item_jsons_.find(*iter) == item_jsons_.end()); | |
936 on_changed_stats_[*iter] = new OnChangedStat(); | |
937 current_map[*iter]->AddObserver(this); | |
938 item_jsons_[*iter] = item.release(); | |
939 } | |
940 downloads_.swap(current_map); | |
941 | |
942 // Dispatching onErased is handled in OnDownloadUpdated when an item | |
943 // transitions to the REMOVING state. | |
944 } | 908 } |
945 | 909 |
946 void ExtensionDownloadsEventRouter::ManagerGoingDown( | 910 void ExtensionDownloadsEventRouter::ManagerGoingDown( |
947 DownloadManager* manager) { | 911 DownloadManager* manager) { |
948 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 912 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
949 manager_->RemoveObserver(this); | 913 manager_->RemoveObserver(this); |
950 manager_ = NULL; | 914 manager_ = NULL; |
951 } | 915 } |
952 | 916 |
953 void ExtensionDownloadsEventRouter::DispatchEvent( | 917 void ExtensionDownloadsEventRouter::DispatchEvent( |
(...skipping 11 matching lines...) Expand all Loading... |
965 extensions::EventFilteringInfo()); | 929 extensions::EventFilteringInfo()); |
966 | 930 |
967 DownloadsNotificationSource notification_source; | 931 DownloadsNotificationSource notification_source; |
968 notification_source.event_name = event_name; | 932 notification_source.event_name = event_name; |
969 notification_source.profile = profile_; | 933 notification_source.profile = profile_; |
970 content::NotificationService::current()->Notify( | 934 content::NotificationService::current()->Notify( |
971 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, | 935 chrome::NOTIFICATION_EXTENSION_DOWNLOADS_EVENT, |
972 content::Source<DownloadsNotificationSource>(¬ification_source), | 936 content::Source<DownloadsNotificationSource>(¬ification_source), |
973 content::Details<std::string>(&json_args)); | 937 content::Details<std::string>(&json_args)); |
974 } | 938 } |
OLD | NEW |