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 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
330 if (profile->HasOffTheRecordProfile() && | 330 if (profile->HasOffTheRecordProfile() && |
331 (include_incognito || | 331 (include_incognito || |
332 profile->IsOffTheRecord())) { | 332 profile->IsOffTheRecord())) { |
333 *incognito_manager = BrowserContext::GetDownloadManager( | 333 *incognito_manager = BrowserContext::GetDownloadManager( |
334 profile->GetOffTheRecordProfile()); | 334 profile->GetOffTheRecordProfile()); |
335 } else { | 335 } else { |
336 *incognito_manager = NULL; | 336 *incognito_manager = NULL; |
337 } | 337 } |
338 } | 338 } |
339 | 339 |
340 DownloadItem* GetActiveItem(Profile* profile, bool include_incognito, int id) { | 340 DownloadItem* GetDownload(Profile* profile, bool include_incognito, int id) { |
341 DownloadManager* manager = NULL; | 341 DownloadManager* manager = NULL; |
342 DownloadManager* incognito_manager = NULL; | 342 DownloadManager* incognito_manager = NULL; |
343 GetManagers(profile, include_incognito, &manager, &incognito_manager); | 343 GetManagers(profile, include_incognito, &manager, &incognito_manager); |
344 DownloadItem* download_item = manager->GetDownload(id); | 344 DownloadItem* download_item = manager->GetDownload(id); |
345 if (!download_item && incognito_manager) | 345 if (!download_item && incognito_manager) |
346 download_item = incognito_manager->GetDownload(id); | 346 download_item = incognito_manager->GetDownload(id); |
347 return download_item; | |
348 } | |
349 | |
350 DownloadItem* GetDownloadIfInProgress( | |
351 Profile* profile, | |
352 bool include_incognito, | |
353 int id) { | |
354 DownloadItem* download_item = GetDownload(profile, include_incognito, id); | |
347 return download_item && download_item->IsInProgress() ? download_item : NULL; | 355 return download_item && download_item->IsInProgress() ? download_item : NULL; |
348 } | 356 } |
349 | 357 |
350 enum DownloadsFunctionName { | 358 enum DownloadsFunctionName { |
351 DOWNLOADS_FUNCTION_DOWNLOAD = 0, | 359 DOWNLOADS_FUNCTION_DOWNLOAD = 0, |
352 DOWNLOADS_FUNCTION_SEARCH = 1, | 360 DOWNLOADS_FUNCTION_SEARCH = 1, |
353 DOWNLOADS_FUNCTION_PAUSE = 2, | 361 DOWNLOADS_FUNCTION_PAUSE = 2, |
354 DOWNLOADS_FUNCTION_RESUME = 3, | 362 DOWNLOADS_FUNCTION_RESUME = 3, |
355 DOWNLOADS_FUNCTION_CANCEL = 4, | 363 DOWNLOADS_FUNCTION_CANCEL = 4, |
356 DOWNLOADS_FUNCTION_ERASE = 5, | 364 DOWNLOADS_FUNCTION_ERASE = 5, |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
680 return true; | 688 return true; |
681 } | 689 } |
682 | 690 |
683 DownloadsPauseFunction::DownloadsPauseFunction() {} | 691 DownloadsPauseFunction::DownloadsPauseFunction() {} |
684 DownloadsPauseFunction::~DownloadsPauseFunction() {} | 692 DownloadsPauseFunction::~DownloadsPauseFunction() {} |
685 | 693 |
686 bool DownloadsPauseFunction::RunImpl() { | 694 bool DownloadsPauseFunction::RunImpl() { |
687 scoped_ptr<extensions::api::downloads::Pause::Params> params( | 695 scoped_ptr<extensions::api::downloads::Pause::Params> params( |
688 extensions::api::downloads::Pause::Params::Create(*args_)); | 696 extensions::api::downloads::Pause::Params::Create(*args_)); |
689 EXTENSION_FUNCTION_VALIDATE(params.get()); | 697 EXTENSION_FUNCTION_VALIDATE(params.get()); |
690 DownloadItem* download_item = GetActiveItem( | 698 DownloadItem* download_item = GetDownloadIfInProgress( |
691 profile(), include_incognito(), params->download_id); | 699 profile(), include_incognito(), params->download_id); |
692 if (download_item == NULL) { | 700 if (download_item == NULL) { |
693 // This could be due to an invalid download ID, or it could be due to the | 701 // This could be due to an invalid download ID, or it could be due to the |
694 // download not being currently active. | 702 // download not being currently active. |
695 error_ = download_extension_errors::kInvalidOperationError; | 703 error_ = download_extension_errors::kInvalidOperationError; |
696 } else if (!download_item->IsPaused()) { | 704 } else if (!download_item->IsPaused()) { |
697 // If download_item->IsPaused() already then we treat it as a success. | 705 // If download_item->IsPaused() already then we treat it as a success. |
698 download_item->TogglePause(); | 706 download_item->TogglePause(); |
699 } | 707 } |
700 if (error_.empty()) | 708 if (error_.empty()) |
701 RecordApiFunctions(DOWNLOADS_FUNCTION_PAUSE); | 709 RecordApiFunctions(DOWNLOADS_FUNCTION_PAUSE); |
702 return error_.empty(); | 710 return error_.empty(); |
703 } | 711 } |
704 | 712 |
705 DownloadsResumeFunction::DownloadsResumeFunction() {} | 713 DownloadsResumeFunction::DownloadsResumeFunction() {} |
706 DownloadsResumeFunction::~DownloadsResumeFunction() {} | 714 DownloadsResumeFunction::~DownloadsResumeFunction() {} |
707 | 715 |
708 bool DownloadsResumeFunction::RunImpl() { | 716 bool DownloadsResumeFunction::RunImpl() { |
709 scoped_ptr<extensions::api::downloads::Resume::Params> params( | 717 scoped_ptr<extensions::api::downloads::Resume::Params> params( |
710 extensions::api::downloads::Resume::Params::Create(*args_)); | 718 extensions::api::downloads::Resume::Params::Create(*args_)); |
711 EXTENSION_FUNCTION_VALIDATE(params.get()); | 719 EXTENSION_FUNCTION_VALIDATE(params.get()); |
712 DownloadItem* download_item = GetActiveItem( | 720 DownloadItem* download_item = GetDownloadIfInProgress( |
713 profile(), include_incognito(), params->download_id); | 721 profile(), include_incognito(), params->download_id); |
714 if (download_item == NULL) { | 722 if (download_item == NULL) { |
715 // This could be due to an invalid download ID, or it could be due to the | 723 // This could be due to an invalid download ID, or it could be due to the |
716 // download not being currently active. | 724 // download not being currently active. |
717 error_ = download_extension_errors::kInvalidOperationError; | 725 error_ = download_extension_errors::kInvalidOperationError; |
718 } else if (download_item->IsPaused()) { | 726 } else if (download_item->IsPaused()) { |
719 // If !download_item->IsPaused() already, then we treat it as a success. | 727 // If !download_item->IsPaused() already, then we treat it as a success. |
720 download_item->TogglePause(); | 728 download_item->TogglePause(); |
721 } | 729 } |
722 if (error_.empty()) | 730 if (error_.empty()) |
723 RecordApiFunctions(DOWNLOADS_FUNCTION_RESUME); | 731 RecordApiFunctions(DOWNLOADS_FUNCTION_RESUME); |
724 return error_.empty(); | 732 return error_.empty(); |
725 } | 733 } |
726 | 734 |
727 DownloadsCancelFunction::DownloadsCancelFunction() {} | 735 DownloadsCancelFunction::DownloadsCancelFunction() {} |
728 DownloadsCancelFunction::~DownloadsCancelFunction() {} | 736 DownloadsCancelFunction::~DownloadsCancelFunction() {} |
729 | 737 |
730 bool DownloadsCancelFunction::RunImpl() { | 738 bool DownloadsCancelFunction::RunImpl() { |
731 scoped_ptr<extensions::api::downloads::Resume::Params> params( | 739 scoped_ptr<extensions::api::downloads::Resume::Params> params( |
732 extensions::api::downloads::Resume::Params::Create(*args_)); | 740 extensions::api::downloads::Resume::Params::Create(*args_)); |
733 EXTENSION_FUNCTION_VALIDATE(params.get()); | 741 EXTENSION_FUNCTION_VALIDATE(params.get()); |
734 DownloadItem* download_item = GetActiveItem( | 742 DownloadItem* download_item = GetDownloadIfInProgress( |
735 profile(), include_incognito(), params->download_id); | 743 profile(), include_incognito(), params->download_id); |
736 if (download_item != NULL) | 744 if (download_item != NULL) |
737 download_item->Cancel(true); | 745 download_item->Cancel(true); |
738 // |download_item| can be NULL if the download ID was invalid or if the | 746 // |download_item| can be NULL if the download ID was invalid or if the |
739 // download is not currently active. Either way, we don't consider it a | 747 // download is not currently active. Either way, we don't consider it a |
740 // failure. | 748 // failure. |
741 if (error_.empty()) | 749 if (error_.empty()) |
742 RecordApiFunctions(DOWNLOADS_FUNCTION_CANCEL); | 750 RecordApiFunctions(DOWNLOADS_FUNCTION_CANCEL); |
743 return error_.empty(); | 751 return error_.empty(); |
744 } | 752 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
795 return error_.empty(); | 803 return error_.empty(); |
796 } | 804 } |
797 | 805 |
798 DownloadsOpenFunction::DownloadsOpenFunction() {} | 806 DownloadsOpenFunction::DownloadsOpenFunction() {} |
799 DownloadsOpenFunction::~DownloadsOpenFunction() {} | 807 DownloadsOpenFunction::~DownloadsOpenFunction() {} |
800 | 808 |
801 bool DownloadsOpenFunction::RunImpl() { | 809 bool DownloadsOpenFunction::RunImpl() { |
802 scoped_ptr<extensions::api::downloads::Open::Params> params( | 810 scoped_ptr<extensions::api::downloads::Open::Params> params( |
803 extensions::api::downloads::Open::Params::Create(*args_)); | 811 extensions::api::downloads::Open::Params::Create(*args_)); |
804 EXTENSION_FUNCTION_VALIDATE(params.get()); | 812 EXTENSION_FUNCTION_VALIDATE(params.get()); |
805 error_ = download_extension_errors::kNotImplementedError; | 813 DownloadItem* download_item = GetDownload( |
806 if (error_.empty()) | 814 profile(), include_incognito(), params->download_id); |
807 RecordApiFunctions(DOWNLOADS_FUNCTION_OPEN); | 815 if (!download_item) { |
808 return error_.empty(); | 816 error_ = download_extension_errors::kInvalidOperationError; |
817 return false; | |
818 } | |
819 if (download_item->IsComplete()) { | |
820 download_item->OpenDownload(); | |
Randy Smith (Not in Mondays)
2012/12/05 19:12:05
You know that download_item->OpenDownload() curren
| |
821 }else { | |
822 download_item->SetOpenWhenComplete(true); | |
823 } | |
824 RecordApiFunctions(DOWNLOADS_FUNCTION_OPEN); | |
825 return true; | |
809 } | 826 } |
810 | 827 |
811 DownloadsDragFunction::DownloadsDragFunction() {} | 828 DownloadsDragFunction::DownloadsDragFunction() {} |
812 DownloadsDragFunction::~DownloadsDragFunction() {} | 829 DownloadsDragFunction::~DownloadsDragFunction() {} |
813 | 830 |
814 bool DownloadsDragFunction::RunImpl() { | 831 bool DownloadsDragFunction::RunImpl() { |
815 scoped_ptr<extensions::api::downloads::Drag::Params> params( | 832 scoped_ptr<extensions::api::downloads::Drag::Params> params( |
816 extensions::api::downloads::Drag::Params::Create(*args_)); | 833 extensions::api::downloads::Drag::Params::Create(*args_)); |
817 EXTENSION_FUNCTION_VALIDATE(params.get()); | 834 EXTENSION_FUNCTION_VALIDATE(params.get()); |
818 error_ = download_extension_errors::kNotImplementedError; | 835 error_ = download_extension_errors::kNotImplementedError; |
(...skipping 16 matching lines...) Expand all Loading... | |
835 | 852 |
836 bool DownloadsGetFileIconFunction::RunImpl() { | 853 bool DownloadsGetFileIconFunction::RunImpl() { |
837 scoped_ptr<extensions::api::downloads::GetFileIcon::Params> params( | 854 scoped_ptr<extensions::api::downloads::GetFileIcon::Params> params( |
838 extensions::api::downloads::GetFileIcon::Params::Create(*args_)); | 855 extensions::api::downloads::GetFileIcon::Params::Create(*args_)); |
839 EXTENSION_FUNCTION_VALIDATE(params.get()); | 856 EXTENSION_FUNCTION_VALIDATE(params.get()); |
840 const extensions::api::downloads::GetFileIconOptions* options = | 857 const extensions::api::downloads::GetFileIconOptions* options = |
841 params->options.get(); | 858 params->options.get(); |
842 int icon_size = kDefaultIconSize; | 859 int icon_size = kDefaultIconSize; |
843 if (options && options->size.get()) | 860 if (options && options->size.get()) |
844 icon_size = *options->size.get(); | 861 icon_size = *options->size.get(); |
845 DownloadManager* manager = NULL; | 862 DownloadItem* download_item = GetDownload( |
846 DownloadManager* incognito_manager = NULL; | 863 profile(), include_incognito(), params->download_id); |
847 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); | |
848 DownloadItem* download_item = manager->GetDownload(params->download_id); | |
849 if (!download_item && incognito_manager) | |
850 download_item = incognito_manager->GetDownload(params->download_id); | |
851 if (!download_item || download_item->GetTargetFilePath().empty()) { | 864 if (!download_item || download_item->GetTargetFilePath().empty()) { |
852 error_ = download_extension_errors::kInvalidOperationError; | 865 error_ = download_extension_errors::kInvalidOperationError; |
853 return false; | 866 return false; |
854 } | 867 } |
855 // In-progress downloads return the intermediate filename for GetFullPath() | 868 // In-progress downloads return the intermediate filename for GetFullPath() |
856 // which doesn't have the final extension. Therefore we won't be able to | 869 // which doesn't have the final extension. Therefore we won't be able to |
857 // derive a good file icon for it. So we use GetTargetFilePath() instead. | 870 // derive a good file icon for it. So we use GetTargetFilePath() instead. |
858 DCHECK(icon_extractor_.get()); | 871 DCHECK(icon_extractor_.get()); |
859 DCHECK(icon_size == 16 || icon_size == 32); | 872 DCHECK(icon_size == 16 || icon_size == 32); |
860 EXTENSION_FUNCTION_VALIDATE(icon_extractor_->ExtractIconURLForPath( | 873 EXTENSION_FUNCTION_VALIDATE(icon_extractor_->ExtractIconURLForPath( |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
991 if (profile_->HasOffTheRecordProfile() && | 1004 if (profile_->HasOffTheRecordProfile() && |
992 !profile_->IsOffTheRecord()) { | 1005 !profile_->IsOffTheRecord()) { |
993 DispatchEventInternal( | 1006 DispatchEventInternal( |
994 profile_->GetOffTheRecordProfile(), | 1007 profile_->GetOffTheRecordProfile(), |
995 event_name, | 1008 event_name, |
996 json_args, | 1009 json_args, |
997 scoped_ptr<base::ListValue>(args->DeepCopy())); | 1010 scoped_ptr<base::ListValue>(args->DeepCopy())); |
998 } | 1011 } |
999 DispatchEventInternal(profile_, event_name, json_args, args.Pass()); | 1012 DispatchEventInternal(profile_, event_name, json_args, args.Pass()); |
1000 } | 1013 } |
OLD | NEW |