| 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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 const char* url; | 117 const char* url; |
| 118 const char* mime_type; | 118 const char* mime_type; |
| 119 bool save_as; | 119 bool save_as; |
| 120 bool prompt_for_download; | 120 bool prompt_for_download; |
| 121 bool expected_save_as; | 121 bool expected_save_as; |
| 122 } kStartDownloadCases[] = { | 122 } kStartDownloadCases[] = { |
| 123 { "http://www.foo.com/dont-open.html", | 123 { "http://www.foo.com/dont-open.html", |
| 124 "text/html", | 124 "text/html", |
| 125 false, | 125 false, |
| 126 false, | 126 false, |
| 127 false, }, | 127 true, }, |
| 128 { "http://www.foo.com/save-as.html", | 128 { "http://www.foo.com/save-as.html", |
| 129 "text/html", | 129 "text/html", |
| 130 true, | 130 true, |
| 131 false, | 131 false, |
| 132 true, }, | 132 true, }, |
| 133 { "http://www.foo.com/always-prompt.html", | 133 { "http://www.foo.com/always-prompt.html", |
| 134 "text/html", | 134 "text/html", |
| 135 false, | 135 false, |
| 136 true, | 136 true, |
| 137 true, }, | 137 true, }, |
| 138 { "http://www.foo.com/user-script-text-html-mimetype.user.js", | 138 { "http://www.foo.com/user-script-text-html-mimetype.user.js", |
| 139 "text/html", | 139 "text/html", |
| 140 false, | 140 false, |
| 141 false, | 141 false, |
| 142 false, }, | 142 true, }, |
| 143 { "http://www.foo.com/extensionless-extension", | 143 { "http://www.foo.com/extensionless-extension", |
| 144 "application/x-chrome-extension", | 144 "application/x-chrome-extension", |
| 145 true, | 145 true, |
| 146 false, | 146 false, |
| 147 true, }, | 147 true, }, |
| 148 { "http://www.foo.com/save-as.pdf", | 148 { "http://www.foo.com/save-as.pdf", |
| 149 "application/pdf", | 149 "application/pdf", |
| 150 true, | 150 true, |
| 151 false, | 151 false, |
| 152 true, }, | 152 true, }, |
| 153 { "http://www.foo.com/sometimes_prompt.pdf", | 153 { "http://www.foo.com/sometimes_prompt.pdf", |
| 154 "application/pdf", | 154 "application/pdf", |
| 155 false, | 155 false, |
| 156 true, | 156 true, |
| 157 false, }, | 157 true, }, |
| 158 { "http://www.foo.com/always_prompt.jar", | 158 { "http://www.foo.com/always_prompt.jar", |
| 159 "application/jar", | 159 "application/jar", |
| 160 false, | 160 false, |
| 161 true, | 161 true, |
| 162 true, }, | 162 true, }, |
| 163 }; | 163 }; |
| 164 | 164 |
| 165 const struct { | 165 const struct { |
| 166 FilePath::StringType suggested_path; | 166 FilePath::StringType suggested_path; |
| 167 bool is_dangerous_file; | 167 bool is_dangerous_file; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 download_manager_->AddObserver(this); | 220 download_manager_->AddObserver(this); |
| 221 } | 221 } |
| 222 | 222 |
| 223 ~SelectFileObserver() { | 223 ~SelectFileObserver() { |
| 224 download_manager_->RemoveObserver(this); | 224 download_manager_->RemoveObserver(this); |
| 225 } | 225 } |
| 226 | 226 |
| 227 // Downloadmanager::Observer functions. | 227 // Downloadmanager::Observer functions. |
| 228 virtual void ModelChanged() {} | 228 virtual void ModelChanged() {} |
| 229 virtual void ManagerGoingDown() {} | 229 virtual void ManagerGoingDown() {} |
| 230 virtual void SelectFileDialogDisplayed(int32 id) { | 230 virtual void SelectFileDialogDisplayed(int32 id, |
| 231 const FilePath& suggested_path) { |
| 231 file_dialog_ids_.insert(id); | 232 file_dialog_ids_.insert(id); |
| 232 } | 233 } |
| 233 | 234 |
| 234 bool ShowedFileDialogForId(int32 id) { | 235 bool ShowedFileDialogForId(int32 id) { |
| 235 return file_dialog_ids_.find(id) != file_dialog_ids_.end(); | 236 return file_dialog_ids_.find(id) != file_dialog_ids_.end(); |
| 236 } | 237 } |
| 237 | 238 |
| 238 private: | 239 private: |
| 239 std::set<int32> file_dialog_ids_; | 240 std::set<int32> file_dialog_ids_; |
| 240 scoped_refptr<DownloadManager> download_manager_; | 241 scoped_refptr<DownloadManager> download_manager_; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 301 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 301 info->download_id = static_cast<int>(i); | 302 info->download_id = static_cast<int>(i); |
| 302 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; | 303 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; |
| 303 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); | 304 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); |
| 304 info->mime_type = kStartDownloadCases[i].mime_type; | 305 info->mime_type = kStartDownloadCases[i].mime_type; |
| 305 download_manager_->CreateDownloadItem(info.get()); | 306 download_manager_->CreateDownloadItem(info.get()); |
| 306 | 307 |
| 307 DownloadFile* download_file( | 308 DownloadFile* download_file( |
| 308 new DownloadFile(info.get(), download_manager_)); | 309 new DownloadFile(info.get(), download_manager_)); |
| 309 AddDownloadToFileManager(info->download_id, download_file); | 310 AddDownloadToFileManager(info->download_id, download_file); |
| 310 download_file->Initialize(false); | 311 |
| 312 ScopedTempDir temp_dir; |
| 313 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 314 download_file->Initialize(false, temp_dir.path()); |
| 311 download_manager_->StartDownload(info->download_id); | 315 download_manager_->StartDownload(info->download_id); |
| 312 message_loop_.RunAllPending(); | 316 message_loop_.RunAllPending(); |
| 313 | 317 |
| 314 // SelectFileObserver will have recorded any attempt to open the | 318 // SelectFileObserver will have recorded any attempt to open the |
| 315 // select file dialog. | 319 // select file dialog. |
| 316 // Note that DownloadManager::FileSelectionCanceled() is never called. | 320 // Note that DownloadManager::FileSelectionCanceled() is never called. |
| 317 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, | 321 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, |
| 318 observer.ShowedFileDialogForId(i)); | 322 observer.ShowedFileDialogForId(i)); |
| 319 } | 323 } |
| 320 } | 324 } |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 553 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 550 | 554 |
| 551 // Create and initialize the download file. We're bypassing the first part | 555 // Create and initialize the download file. We're bypassing the first part |
| 552 // of the download process and skipping to the part after the final file | 556 // of the download process and skipping to the part after the final file |
| 553 // name has been chosen, so we need to initialize the download file | 557 // name has been chosen, so we need to initialize the download file |
| 554 // properly. | 558 // properly. |
| 555 DownloadFile* download_file( | 559 DownloadFile* download_file( |
| 556 new DownloadFile(info.get(), download_manager_)); | 560 new DownloadFile(info.get(), download_manager_)); |
| 557 download_file->Rename(cr_path); | 561 download_file->Rename(cr_path); |
| 558 // This creates the .crdownload version of the file. | 562 // This creates the .crdownload version of the file. |
| 559 download_file->Initialize(false); | 563 ScopedTempDir temp_dir; |
| 564 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 565 download_file->Initialize(false, temp_dir.path()); |
| 560 // |download_file| is owned by DownloadFileManager. | 566 // |download_file| is owned by DownloadFileManager. |
| 561 AddDownloadToFileManager(info->download_id, download_file); | 567 AddDownloadToFileManager(info->download_id, download_file); |
| 562 | 568 |
| 563 ContinueDownloadWithPath(download, new_path); | 569 ContinueDownloadWithPath(download, new_path); |
| 564 message_loop_.RunAllPending(); | 570 message_loop_.RunAllPending(); |
| 565 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 571 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 566 | 572 |
| 567 download_file->AppendDataToFile(kTestData, kTestDataLen); | 573 download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 568 | 574 |
| 569 // Finish the download. | 575 // Finish the download. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 581 EXPECT_FALSE(observer->was_opened()); | 587 EXPECT_FALSE(observer->was_opened()); |
| 582 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 588 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
| 583 | 589 |
| 584 EXPECT_TRUE(file_util::PathExists(new_path)); | 590 EXPECT_TRUE(file_util::PathExists(new_path)); |
| 585 EXPECT_FALSE(file_util::PathExists(cr_path)); | 591 EXPECT_FALSE(file_util::PathExists(cr_path)); |
| 586 EXPECT_FALSE(file_util::PathExists(unique_new_path)); | 592 EXPECT_FALSE(file_util::PathExists(unique_new_path)); |
| 587 std::string file_contents; | 593 std::string file_contents; |
| 588 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); | 594 EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); |
| 589 EXPECT_EQ(std::string(kTestData), file_contents); | 595 EXPECT_EQ(std::string(kTestData), file_contents); |
| 590 } | 596 } |
| OLD | NEW |