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 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 return true; | 690 return true; |
683 } | 691 } |
684 | 692 |
685 DownloadsPauseFunction::DownloadsPauseFunction() {} | 693 DownloadsPauseFunction::DownloadsPauseFunction() {} |
686 DownloadsPauseFunction::~DownloadsPauseFunction() {} | 694 DownloadsPauseFunction::~DownloadsPauseFunction() {} |
687 | 695 |
688 bool DownloadsPauseFunction::RunImpl() { | 696 bool DownloadsPauseFunction::RunImpl() { |
689 scoped_ptr<extensions::api::downloads::Pause::Params> params( | 697 scoped_ptr<extensions::api::downloads::Pause::Params> params( |
690 extensions::api::downloads::Pause::Params::Create(*args_)); | 698 extensions::api::downloads::Pause::Params::Create(*args_)); |
691 EXTENSION_FUNCTION_VALIDATE(params.get()); | 699 EXTENSION_FUNCTION_VALIDATE(params.get()); |
692 DownloadItem* download_item = GetActiveItem( | 700 DownloadItem* download_item = GetDownloadIfInProgress( |
693 profile(), include_incognito(), params->download_id); | 701 profile(), include_incognito(), params->download_id); |
694 if (download_item == NULL) { | 702 if (download_item == NULL) { |
695 // This could be due to an invalid download ID, or it could be due to the | 703 // This could be due to an invalid download ID, or it could be due to the |
696 // download not being currently active. | 704 // download not being currently active. |
697 error_ = download_extension_errors::kInvalidOperationError; | 705 error_ = download_extension_errors::kInvalidOperationError; |
698 } else if (!download_item->IsPaused()) { | 706 } else if (!download_item->IsPaused()) { |
699 // If download_item->IsPaused() already then we treat it as a success. | 707 // If download_item->IsPaused() already then we treat it as a success. |
700 download_item->TogglePause(); | 708 download_item->TogglePause(); |
701 } | 709 } |
702 if (error_.empty()) | 710 if (error_.empty()) |
703 RecordApiFunctions(DOWNLOADS_FUNCTION_PAUSE); | 711 RecordApiFunctions(DOWNLOADS_FUNCTION_PAUSE); |
704 return error_.empty(); | 712 return error_.empty(); |
705 } | 713 } |
706 | 714 |
707 DownloadsResumeFunction::DownloadsResumeFunction() {} | 715 DownloadsResumeFunction::DownloadsResumeFunction() {} |
708 DownloadsResumeFunction::~DownloadsResumeFunction() {} | 716 DownloadsResumeFunction::~DownloadsResumeFunction() {} |
709 | 717 |
710 bool DownloadsResumeFunction::RunImpl() { | 718 bool DownloadsResumeFunction::RunImpl() { |
711 scoped_ptr<extensions::api::downloads::Resume::Params> params( | 719 scoped_ptr<extensions::api::downloads::Resume::Params> params( |
712 extensions::api::downloads::Resume::Params::Create(*args_)); | 720 extensions::api::downloads::Resume::Params::Create(*args_)); |
713 EXTENSION_FUNCTION_VALIDATE(params.get()); | 721 EXTENSION_FUNCTION_VALIDATE(params.get()); |
714 DownloadItem* download_item = GetActiveItem( | 722 DownloadItem* download_item = GetDownloadIfInProgress( |
715 profile(), include_incognito(), params->download_id); | 723 profile(), include_incognito(), params->download_id); |
716 if (download_item == NULL) { | 724 if (download_item == NULL) { |
717 // This could be due to an invalid download ID, or it could be due to the | 725 // This could be due to an invalid download ID, or it could be due to the |
718 // download not being currently active. | 726 // download not being currently active. |
719 error_ = download_extension_errors::kInvalidOperationError; | 727 error_ = download_extension_errors::kInvalidOperationError; |
720 } else if (download_item->IsPaused()) { | 728 } else if (download_item->IsPaused()) { |
721 // If !download_item->IsPaused() already, then we treat it as a success. | 729 // If !download_item->IsPaused() already, then we treat it as a success. |
722 download_item->TogglePause(); | 730 download_item->TogglePause(); |
723 } | 731 } |
724 if (error_.empty()) | 732 if (error_.empty()) |
725 RecordApiFunctions(DOWNLOADS_FUNCTION_RESUME); | 733 RecordApiFunctions(DOWNLOADS_FUNCTION_RESUME); |
726 return error_.empty(); | 734 return error_.empty(); |
727 } | 735 } |
728 | 736 |
729 DownloadsCancelFunction::DownloadsCancelFunction() {} | 737 DownloadsCancelFunction::DownloadsCancelFunction() {} |
730 DownloadsCancelFunction::~DownloadsCancelFunction() {} | 738 DownloadsCancelFunction::~DownloadsCancelFunction() {} |
731 | 739 |
732 bool DownloadsCancelFunction::RunImpl() { | 740 bool DownloadsCancelFunction::RunImpl() { |
733 scoped_ptr<extensions::api::downloads::Resume::Params> params( | 741 scoped_ptr<extensions::api::downloads::Resume::Params> params( |
734 extensions::api::downloads::Resume::Params::Create(*args_)); | 742 extensions::api::downloads::Resume::Params::Create(*args_)); |
735 EXTENSION_FUNCTION_VALIDATE(params.get()); | 743 EXTENSION_FUNCTION_VALIDATE(params.get()); |
736 DownloadItem* download_item = GetActiveItem( | 744 DownloadItem* download_item = GetDownloadIfInProgress( |
737 profile(), include_incognito(), params->download_id); | 745 profile(), include_incognito(), params->download_id); |
738 if (download_item != NULL) | 746 if (download_item != NULL) |
739 download_item->Cancel(true); | 747 download_item->Cancel(true); |
740 // |download_item| can be NULL if the download ID was invalid or if the | 748 // |download_item| can be NULL if the download ID was invalid or if the |
741 // download is not currently active. Either way, we don't consider it a | 749 // download is not currently active. Either way, we don't consider it a |
742 // failure. | 750 // failure. |
743 if (error_.empty()) | 751 if (error_.empty()) |
744 RecordApiFunctions(DOWNLOADS_FUNCTION_CANCEL); | 752 RecordApiFunctions(DOWNLOADS_FUNCTION_CANCEL); |
745 return error_.empty(); | 753 return error_.empty(); |
746 } | 754 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 return error_.empty(); | 805 return error_.empty(); |
798 } | 806 } |
799 | 807 |
800 DownloadsOpenFunction::DownloadsOpenFunction() {} | 808 DownloadsOpenFunction::DownloadsOpenFunction() {} |
801 DownloadsOpenFunction::~DownloadsOpenFunction() {} | 809 DownloadsOpenFunction::~DownloadsOpenFunction() {} |
802 | 810 |
803 bool DownloadsOpenFunction::RunImpl() { | 811 bool DownloadsOpenFunction::RunImpl() { |
804 scoped_ptr<extensions::api::downloads::Open::Params> params( | 812 scoped_ptr<extensions::api::downloads::Open::Params> params( |
805 extensions::api::downloads::Open::Params::Create(*args_)); | 813 extensions::api::downloads::Open::Params::Create(*args_)); |
806 EXTENSION_FUNCTION_VALIDATE(params.get()); | 814 EXTENSION_FUNCTION_VALIDATE(params.get()); |
807 error_ = download_extension_errors::kNotImplementedError; | 815 DownloadItem* download_item = GetDownload( |
808 if (error_.empty()) | 816 profile(), include_incognito(), params->download_id); |
809 RecordApiFunctions(DOWNLOADS_FUNCTION_OPEN); | 817 if (!download_item || !download_item->IsComplete()) { |
810 return error_.empty(); | 818 error_ = download_extension_errors::kInvalidOperationError; |
| 819 return false; |
| 820 } |
| 821 download_item->OpenDownload(); |
| 822 RecordApiFunctions(DOWNLOADS_FUNCTION_OPEN); |
| 823 return true; |
811 } | 824 } |
812 | 825 |
813 DownloadsDragFunction::DownloadsDragFunction() {} | 826 DownloadsDragFunction::DownloadsDragFunction() {} |
814 DownloadsDragFunction::~DownloadsDragFunction() {} | 827 DownloadsDragFunction::~DownloadsDragFunction() {} |
815 | 828 |
816 bool DownloadsDragFunction::RunImpl() { | 829 bool DownloadsDragFunction::RunImpl() { |
817 scoped_ptr<extensions::api::downloads::Drag::Params> params( | 830 scoped_ptr<extensions::api::downloads::Drag::Params> params( |
818 extensions::api::downloads::Drag::Params::Create(*args_)); | 831 extensions::api::downloads::Drag::Params::Create(*args_)); |
819 EXTENSION_FUNCTION_VALIDATE(params.get()); | 832 EXTENSION_FUNCTION_VALIDATE(params.get()); |
820 error_ = download_extension_errors::kNotImplementedError; | 833 error_ = download_extension_errors::kNotImplementedError; |
(...skipping 16 matching lines...) Expand all Loading... |
837 | 850 |
838 bool DownloadsGetFileIconFunction::RunImpl() { | 851 bool DownloadsGetFileIconFunction::RunImpl() { |
839 scoped_ptr<extensions::api::downloads::GetFileIcon::Params> params( | 852 scoped_ptr<extensions::api::downloads::GetFileIcon::Params> params( |
840 extensions::api::downloads::GetFileIcon::Params::Create(*args_)); | 853 extensions::api::downloads::GetFileIcon::Params::Create(*args_)); |
841 EXTENSION_FUNCTION_VALIDATE(params.get()); | 854 EXTENSION_FUNCTION_VALIDATE(params.get()); |
842 const extensions::api::downloads::GetFileIconOptions* options = | 855 const extensions::api::downloads::GetFileIconOptions* options = |
843 params->options.get(); | 856 params->options.get(); |
844 int icon_size = kDefaultIconSize; | 857 int icon_size = kDefaultIconSize; |
845 if (options && options->size.get()) | 858 if (options && options->size.get()) |
846 icon_size = *options->size.get(); | 859 icon_size = *options->size.get(); |
847 DownloadManager* manager = NULL; | 860 DownloadItem* download_item = GetDownload( |
848 DownloadManager* incognito_manager = NULL; | 861 profile(), include_incognito(), params->download_id); |
849 GetManagers(profile(), include_incognito(), &manager, &incognito_manager); | |
850 DownloadItem* download_item = manager->GetDownload(params->download_id); | |
851 if (!download_item && incognito_manager) | |
852 download_item = incognito_manager->GetDownload(params->download_id); | |
853 if (!download_item || download_item->GetTargetFilePath().empty()) { | 862 if (!download_item || download_item->GetTargetFilePath().empty()) { |
854 error_ = download_extension_errors::kInvalidOperationError; | 863 error_ = download_extension_errors::kInvalidOperationError; |
855 return false; | 864 return false; |
856 } | 865 } |
857 // In-progress downloads return the intermediate filename for GetFullPath() | 866 // In-progress downloads return the intermediate filename for GetFullPath() |
858 // which doesn't have the final extension. Therefore we won't be able to | 867 // which doesn't have the final extension. Therefore we won't be able to |
859 // derive a good file icon for it. So we use GetTargetFilePath() instead. | 868 // derive a good file icon for it. So we use GetTargetFilePath() instead. |
860 DCHECK(icon_extractor_.get()); | 869 DCHECK(icon_extractor_.get()); |
861 DCHECK(icon_size == 16 || icon_size == 32); | 870 DCHECK(icon_size == 16 || icon_size == 32); |
862 EXTENSION_FUNCTION_VALIDATE(icon_extractor_->ExtractIconURLForPath( | 871 EXTENSION_FUNCTION_VALIDATE(icon_extractor_->ExtractIconURLForPath( |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 if (profile_->HasOffTheRecordProfile() && | 1002 if (profile_->HasOffTheRecordProfile() && |
994 !profile_->IsOffTheRecord()) { | 1003 !profile_->IsOffTheRecord()) { |
995 DispatchEventInternal( | 1004 DispatchEventInternal( |
996 profile_->GetOffTheRecordProfile(), | 1005 profile_->GetOffTheRecordProfile(), |
997 event_name, | 1006 event_name, |
998 json_args, | 1007 json_args, |
999 scoped_ptr<base::ListValue>(args->DeepCopy())); | 1008 scoped_ptr<base::ListValue>(args->DeepCopy())); |
1000 } | 1009 } |
1001 DispatchEventInternal(profile_, event_name, json_args, args.Pass()); | 1010 DispatchEventInternal(profile_, event_name, json_args, args.Pass()); |
1002 } | 1011 } |
OLD | NEW |