| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 5 #include <string> |
| 6 #include <set> | 6 #include <set> |
| 7 | 7 |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/i18n/number_formatting.h" | 9 #include "base/i18n/number_formatting.h" |
| 10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 const char* url; | 124 const char* url; |
| 125 const char* mime_type; | 125 const char* mime_type; |
| 126 bool save_as; | 126 bool save_as; |
| 127 bool prompt_for_download; | 127 bool prompt_for_download; |
| 128 bool expected_save_as; | 128 bool expected_save_as; |
| 129 } kStartDownloadCases[] = { | 129 } kStartDownloadCases[] = { |
| 130 { "http://www.foo.com/dont-open.html", | 130 { "http://www.foo.com/dont-open.html", |
| 131 "text/html", | 131 "text/html", |
| 132 false, | 132 false, |
| 133 false, | 133 false, |
| 134 false, }, | 134 true, }, |
| 135 { "http://www.foo.com/save-as.html", | 135 { "http://www.foo.com/save-as.html", |
| 136 "text/html", | 136 "text/html", |
| 137 true, | 137 true, |
| 138 false, | 138 false, |
| 139 true, }, | 139 true, }, |
| 140 { "http://www.foo.com/always-prompt.html", | 140 { "http://www.foo.com/always-prompt.html", |
| 141 "text/html", | 141 "text/html", |
| 142 false, | 142 false, |
| 143 true, | 143 true, |
| 144 true, }, | 144 true, }, |
| 145 { "http://www.foo.com/user-script-text-html-mimetype.user.js", | 145 { "http://www.foo.com/user-script-text-html-mimetype.user.js", |
| 146 "text/html", | 146 "text/html", |
| 147 false, | 147 false, |
| 148 false, | 148 false, |
| 149 false, }, | 149 true, }, |
| 150 { "http://www.foo.com/extensionless-extension", | 150 { "http://www.foo.com/extensionless-extension", |
| 151 "application/x-chrome-extension", | 151 "application/x-chrome-extension", |
| 152 true, | 152 true, |
| 153 false, | 153 false, |
| 154 true, }, | 154 true, }, |
| 155 { "http://www.foo.com/save-as.pdf", | 155 { "http://www.foo.com/save-as.pdf", |
| 156 "application/pdf", | 156 "application/pdf", |
| 157 true, | 157 true, |
| 158 false, | 158 false, |
| 159 true, }, | 159 true, }, |
| 160 { "http://www.foo.com/sometimes_prompt.pdf", | 160 { "http://www.foo.com/sometimes_prompt.pdf", |
| 161 "application/pdf", | 161 "application/pdf", |
| 162 false, | 162 false, |
| 163 true, | 163 true, |
| 164 false, }, | 164 true, }, |
| 165 { "http://www.foo.com/always_prompt.jar", | 165 { "http://www.foo.com/always_prompt.jar", |
| 166 "application/jar", | 166 "application/jar", |
| 167 false, | 167 false, |
| 168 true, | 168 true, |
| 169 true, }, | 169 true, }, |
| 170 }; | 170 }; |
| 171 | 171 |
| 172 const struct { | 172 const struct { |
| 173 FilePath::StringType suggested_path; | 173 FilePath::StringType suggested_path; |
| 174 bool is_dangerous_file; | 174 bool is_dangerous_file; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 download_manager_->AddObserver(this); | 227 download_manager_->AddObserver(this); |
| 228 } | 228 } |
| 229 | 229 |
| 230 ~SelectFileObserver() { | 230 ~SelectFileObserver() { |
| 231 download_manager_->RemoveObserver(this); | 231 download_manager_->RemoveObserver(this); |
| 232 } | 232 } |
| 233 | 233 |
| 234 // Downloadmanager::Observer functions. | 234 // Downloadmanager::Observer functions. |
| 235 virtual void ModelChanged() {} | 235 virtual void ModelChanged() {} |
| 236 virtual void ManagerGoingDown() {} | 236 virtual void ManagerGoingDown() {} |
| 237 virtual void SelectFileDialogDisplayed(int32 id) { | 237 virtual void SelectFileDialogDisplayed(int32 id, |
| 238 const FilePath& suggested_path) { |
| 238 file_dialog_ids_.insert(id); | 239 file_dialog_ids_.insert(id); |
| 239 } | 240 } |
| 240 | 241 |
| 241 bool ShowedFileDialogForId(int32 id) { | 242 bool ShowedFileDialogForId(int32 id) { |
| 242 return file_dialog_ids_.find(id) != file_dialog_ids_.end(); | 243 return file_dialog_ids_.find(id) != file_dialog_ids_.end(); |
| 243 } | 244 } |
| 244 | 245 |
| 245 private: | 246 private: |
| 246 std::set<int32> file_dialog_ids_; | 247 std::set<int32> file_dialog_ids_; |
| 247 scoped_refptr<DownloadManager> download_manager_; | 248 scoped_refptr<DownloadManager> download_manager_; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 308 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 308 info->download_id = static_cast<int>(i); | 309 info->download_id = static_cast<int>(i); |
| 309 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; | 310 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; |
| 310 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); | 311 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); |
| 311 info->mime_type = kStartDownloadCases[i].mime_type; | 312 info->mime_type = kStartDownloadCases[i].mime_type; |
| 312 download_manager_->CreateDownloadItem(info.get()); | 313 download_manager_->CreateDownloadItem(info.get()); |
| 313 | 314 |
| 314 DownloadFile* download_file( | 315 DownloadFile* download_file( |
| 315 new DownloadFile(info.get(), download_manager_)); | 316 new DownloadFile(info.get(), download_manager_)); |
| 316 AddDownloadToFileManager(info->download_id, download_file); | 317 AddDownloadToFileManager(info->download_id, download_file); |
| 317 download_file->Initialize(false); | 318 |
| 319 ScopedTempDir temp_dir; |
| 320 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 321 download_file->Initialize(false, temp_dir.path()); |
| 318 download_manager_->StartDownload(info->download_id); | 322 download_manager_->StartDownload(info->download_id); |
| 319 message_loop_.RunAllPending(); | 323 message_loop_.RunAllPending(); |
| 320 | 324 |
| 321 // SelectFileObserver will have recorded any attempt to open the | 325 // SelectFileObserver will have recorded any attempt to open the |
| 322 // select file dialog. | 326 // select file dialog. |
| 323 // Note that DownloadManager::FileSelectionCanceled() is never called. | 327 // Note that DownloadManager::FileSelectionCanceled() is never called. |
| 324 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, | 328 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, |
| 325 observer.ShowedFileDialogForId(i)); | 329 observer.ShowedFileDialogForId(i)); |
| 326 } | 330 } |
| 327 } | 331 } |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 584 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 581 | 585 |
| 582 // Create and initialize the download file. We're bypassing the first part | 586 // Create and initialize the download file. We're bypassing the first part |
| 583 // of the download process and skipping to the part after the final file | 587 // of the download process and skipping to the part after the final file |
| 584 // name has been chosen, so we need to initialize the download file | 588 // name has been chosen, so we need to initialize the download file |
| 585 // properly. | 589 // properly. |
| 586 DownloadFile* download_file( | 590 DownloadFile* download_file( |
| 587 new DownloadFile(info.get(), download_manager_)); | 591 new DownloadFile(info.get(), download_manager_)); |
| 588 download_file->Rename(cr_path); | 592 download_file->Rename(cr_path); |
| 589 // This creates the .crdownload version of the file. | 593 // This creates the .crdownload version of the file. |
| 590 download_file->Initialize(false); | 594 ScopedTempDir temp_dir; |
| 595 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 596 download_file->Initialize(false, temp_dir.path()); |
| 591 // |download_file| is owned by DownloadFileManager. | 597 // |download_file| is owned by DownloadFileManager. |
| 592 AddDownloadToFileManager(info->download_id, download_file); | 598 AddDownloadToFileManager(info->download_id, download_file); |
| 593 | 599 |
| 594 ContinueDownloadWithPath(download, new_path); | 600 ContinueDownloadWithPath(download, new_path); |
| 595 message_loop_.RunAllPending(); | 601 message_loop_.RunAllPending(); |
| 596 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 602 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 597 | 603 |
| 598 download_file->AppendDataToFile(kTestData, kTestDataLen); | 604 download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 599 | 605 |
| 600 // Finish the download. | 606 // Finish the download. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 662 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 657 | 663 |
| 658 // Create and initialize the download file. We're bypassing the first part | 664 // Create and initialize the download file. We're bypassing the first part |
| 659 // of the download process and skipping to the part after the final file | 665 // of the download process and skipping to the part after the final file |
| 660 // name has been chosen, so we need to initialize the download file | 666 // name has been chosen, so we need to initialize the download file |
| 661 // properly. | 667 // properly. |
| 662 DownloadFile* download_file( | 668 DownloadFile* download_file( |
| 663 new DownloadFile(info.get(), download_manager_)); | 669 new DownloadFile(info.get(), download_manager_)); |
| 664 download_file->Rename(cr_path); | 670 download_file->Rename(cr_path); |
| 665 // This creates the .crdownload version of the file. | 671 // This creates the .crdownload version of the file. |
| 666 download_file->Initialize(false); | 672 ScopedTempDir temp_dir; |
| 673 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 674 download_file->Initialize(false, temp_dir.path()); |
| 667 // |download_file| is owned by DownloadFileManager. | 675 // |download_file| is owned by DownloadFileManager. |
| 668 AddDownloadToFileManager(info->download_id, download_file); | 676 AddDownloadToFileManager(info->download_id, download_file); |
| 669 | 677 |
| 670 ContinueDownloadWithPath(download, new_path); | 678 ContinueDownloadWithPath(download, new_path); |
| 671 message_loop_.RunAllPending(); | 679 message_loop_.RunAllPending(); |
| 672 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 680 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 673 | 681 |
| 674 download_file->AppendDataToFile(kTestData, kTestDataLen); | 682 download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 675 | 683 |
| 676 // Finish the download. | 684 // Finish the download. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 706 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 714 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 707 EXPECT_TRUE(observer->was_updated()); | 715 EXPECT_TRUE(observer->was_updated()); |
| 708 EXPECT_FALSE(observer->was_opened()); | 716 EXPECT_FALSE(observer->was_opened()); |
| 709 EXPECT_TRUE(download->file_externally_removed()); | 717 EXPECT_TRUE(download->file_externally_removed()); |
| 710 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 718 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
| 711 EXPECT_EQ(download_item_model->GetStatusText(), | 719 EXPECT_EQ(download_item_model->GetStatusText(), |
| 712 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); | 720 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); |
| 713 | 721 |
| 714 EXPECT_FALSE(file_util::PathExists(new_path)); | 722 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 715 } | 723 } |
| OLD | NEW |